Pester with DSC works in ISE but not in console

This topic contains 11 replies, has 2 voices, and was last updated by Profile photo of David Jones David Jones 2 years, 1 month ago.

  • Author
    Posts
  • #19486
    Profile photo of David Jones
    David Jones
    Participant

    I was trying to hone my skills with pest against DSC by writing a test file for the cDNSServerAddress resource and have run up against against an interesting issue. When I run the test as a standar user it works. when I run t as an admin it brakes.

    In the ISE

    Describing how Get-TargetResource (PDS_cDNSServerAddress.psm1) responds
       Context when interface has DNS entries
        [+] should call all the mocks 168ms
        [+] should fill all values in hash table 14ms
        [+] should return expected values 6ms
       Context When inteface has no DNS entries
        [+] should call all the mocks 47ms
        [+] should fill all values in hash table 4ms
        [+] should return expected values 4ms
    Describing how Test-TargetResource (PDS_cDNSServerAddress.psm1) responds
       Context when called with valid paramaters
        [+] should call ValidateProperties without -Apply  55ms
    Describing how Set-TargetResource (PDS_cDNSServerAddress.psm1) responds
       Context when called with valid paramaters
        [+] should call all the mocks 54ms
        [+] Should call ValidateProperties with -Apply 11ms
    Describing how ValidateProperties (PDS_cDNSServerAddress.psm1) responds
       Context when invalid address supplied
        [+] should throw error 83ms
       Context when Apply NOT set and values match
        [+] should call Get-DnsClientServerAddress  87ms
        [+] should NOT call Set-DnsClientServerAddress 3ms
        [+] should return true  3ms
       Context when Apply is NOT set and values do not match
        [+] should call Get-DnsClientServerAddress  56ms
        [+] should NOT call Set-DnsClientServerAddress 3ms
        [+] should return false  5ms
       Context when Apply is set and values match
        [+] should call Get-DnsClientServerAddress  56ms
        [+] should NOT call Set-DnsClientServerAddress 3ms
        [+] should return true  3ms
       Context when Apply is set and values do not match
        [+] should call Get-DnsClientServerAddress  95ms
        [+] should call Set-DnsClientServerAddress 3ms
        [+] should not return any value 3ms

    in the console

    Describing how Get-TargetResource (PDS_cDNSServerAddress.psm1) responds
       Context when interface has DNS entries
        [+] should call all the mocks 159ms
        [+] should fill all values in hash table 6ms
        [+] should return expected values 6ms
       Context When inteface has no DNS entries
        [+] should call all the mocks 51ms
        [+] should fill all values in hash table 8ms
        [+] should return expected values 5ms
    Describing how Test-TargetResource (PDS_cDNSServerAddress.psm1) responds
       Context when called with valid paramaters
        [+] should call ValidateProperties without -Apply  53ms
    Describing how Set-TargetResource (PDS_cDNSServerAddress.psm1) responds
       Context when called with valid paramaters
        [+] should call all the mocks 61ms
        [+] Should call ValidateProperties with -Apply 3ms
    Describing how ValidateProperties (PDS_cDNSServerAddress.psm1) responds
       Context when invalid address supplied
        [+] should throw error 63ms
       Context when Apply NOT set and values match
        [-] should call Get-DnsClientServerAddress  59ms
          You did not declare a mock of the Get-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [-] should NOT call Set-DnsClientServerAddress 5ms
          You did not declare a mock of the Set-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [+] should return true  7ms
       Context when Apply is NOT set and values do not match
        [-] should call Get-DnsClientServerAddress  62ms
          You did not declare a mock of the Get-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [-] should NOT call Set-DnsClientServerAddress 8ms
          You did not declare a mock of the Set-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [+] should return false  5ms
       Context when Apply is set and values match
        [-] should call Get-DnsClientServerAddress  57ms
          You did not declare a mock of the Get-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [-] should NOT call Set-DnsClientServerAddress 6ms
          You did not declare a mock of the Set-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [+] should return true  5ms
       Context when Apply is set and values do not match
        [-] should call Get-DnsClientServerAddress  64ms
          You did not declare a mock of the Get-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [-] should call Set-DnsClientServerAddress 6ms
          You did not declare a mock of the Set-DnsClientServerAddress Command.
          at line: 479 in C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1
        [+] should not return any value 6ms

    Here is the test file.

    $script:here = Split-Path -Parent $MyInvocation.MyCommand.Path
    $script:sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.ps1", ".psm1")
    $script:modName = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.ps1", "")
    $script:pathtosut = join-path $script:here $script:sut
    
    if (get-module -name $modName) { remove-module -Name $modName }
    
    $script:t = [scriptblock]::Create(( gc $script:pathtosut -Raw ) ) 
    New-Module -Name $modName -ScriptBlock $t  | Import-Module -Force
    
    Describe "how Get-TargetResource ($sut) responds" {
        Context 'when interface has DNS entries' {
         Mock -Verifiable -ModuleName $modName -CommandName  Get-DnsClientServerAddress -MockWith { 
            return @{ 
                ServerAddresses = '8.8.8.8','4.4.4.4'
            }
        }
           $arguments = @{ Address = '8.8.8.8','4.4.4.4'
                         AddressFamily = 'IPv4'
                         InterfaceAlias = 'Ethernet 2'
                         }
     
            $result = Get-TargetResource @arguments
    
            It "should call all the mocks" {
                Assert-VerifiableMocks
            }
            It 'should fill all values in hash table' {
                $result.Address | should not be $nul
                $result.AddressFamily | should not be $nul
                $result.InterfaceAlias | should not be $nul
            }
            it 'should return expected values' {
                $result.Address | should be '8.8.8.8','4.4.4.4'
                $result.AddressFamily | should be 'IPv4'
                $result.InterfaceAlias | should be 'Ethernet 2'
            }
        }
        Context 'When inteface has no DNS entries' {
         Mock -Verifiable -ModuleName $modName -CommandName  Get-DnsClientServerAddress -MockWith { 
            return @{ 
                ServerAddresses = $null
            }
        }
           $arguments = @{ Address = '8.8.8.8','4.4.4.4'
                         AddressFamily = 'IPv4'
                         InterfaceAlias = 'Ethernet 2'
                         }
     
            $result = Get-TargetResource @arguments
    
            It "should call all the mocks" {
                Assert-VerifiableMocks
            }
            It 'should fill all values in hash table' {
                $result.Address | should be $null
                $result.AddressFamily | should not be $null
                $result.InterfaceAlias | should not be $null
            }
            it 'should return expected values' {
                $result.Address | should be $null
                $result.AddressFamily | should be 'IPv4'
                $result.InterfaceAlias | should be 'Ethernet 2'
            }
         
        }
    }
    
    
    
    Describe "how Test-TargetResource ($sut) responds" {
        Context 'when called with valid paramaters' {
            Mock -Verifiable -ModuleName $modName -CommandName ValidateProperties -MockWith {
                param ([Switch]$Apply)
                return $Apply
            }
            $arguments = @{ 
                    Address = '8.8.8.8', '4.4.4.4' 
                    AddressFamily = 'IPv4'
                    InterfaceAlias = 'Ethernet 2'
                    }
            $expected = $false
            $result = Test-TargetResource @arguments
     
            It "should call ValidateProperties without -Apply " {
                $result | Should be $false
            }
    
        } 
    }
    
    Describe "how Set-TargetResource ($sut) responds" {
        Context 'when called with valid paramaters' {
            Mock -Verifiable -ModuleName $modName -CommandName ValidateProperties -MockWith {
                param ([Switch]$Apply)
                return $Apply
            }
            $arguments = @{ 
                    Address = '8.8.8.8', '4.4.4.4' 
                    AddressFamily = 'IPv4'
                    InterfaceAlias = 'Ethernet 2'
                    }
            $result = Set-TargetResource @arguments
     
            It "should call all the mocks" {
                Assert-VerifiableMocks
            }
            It "Should call ValidateProperties with -Apply"{
                $result | Should be $true
            }
        } 
    }
    
    Describe "how ValidateProperties ($sut) responds" {
        InModuleScope $modName {
            Context 'when invalid address supplied' {
                Mock -Verifiable -ModuleName $modName -CommandName Get-DnsClientServerAddress -MockWith {}
                Mock -Verifiable -ModuleName $modName -CommandName Set-DnsClientServerAddress -MockWith {}
                $arguments = @{ 
                        Address = 'Void'
                        AddressFamily = 'IPv4'
                        InterfaceAlias = 'Ethernet 2'
                        }
    
                It "should throw error" {
                     {ValidateProperties @arguments} | should throw
                }
            }
     
            Context 'when Apply NOT set and values match' {
                Mock -Verifiable -ModuleName $modName -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{ 
                        ServerAddresses = '8.8.8.8', '4.4.4.4' 
                    }
                }
                Mock -ModuleName $modName -CommandName Set-DnsClientServerAddress -MockWith {}       
                $arguments = @{ 
                        Address = '8.8.8.8', '4.4.4.4' 
                        AddressFamily = 'IPv4'
                        InterfaceAlias = 'Ethernet 2'
                        }
                $expected = $true
                $result = ValidateProperties @arguments
     
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled -ModuleName $modName Get-DnsClientServerAddress 
                }
                It 'should NOT call Set-DnsClientServerAddress' {
                    Assert-MockCalled -ModuleName $modName Set-DnsClientServerAddress 0
                }
                It 'should return true ' {
                    $result | should be ($expected)
                }
            }
            Context 'when Apply is NOT set and values do not match' {
                Mock -Verifiable -ModuleName $modName -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{ 
                        ServerAddresses = '8.8.8.8'
                    }
                }
                Mock -Verifiable -ModuleName $modName -CommandName Set-DnsClientServerAddress -MockWith {}       
                $arguments = @{ 
                        Address = '8.8.8.8', '4.4.4.4' 
                        AddressFamily = 'IPv4'
                        InterfaceAlias = 'Ethernet 2'
                        Apply = $false
                        }
                $expected = $false
                $result = ValidateProperties @arguments
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled -ModuleName $modName Get-DnsClientServerAddress 
                }
                It 'should NOT call Set-DnsClientServerAddress' {
                    Assert-MockCalled -ModuleName $modName Set-DnsClientServerAddress 0
                }
                It 'should return false ' {
                    $result | should be ($expected)
                }
            }
            Context 'when Apply is set and values match' {
                Mock -Verifiable -ModuleName $modName -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{ 
                        ServerAddresses = '8.8.8.8', '4.4.4.4' 
                    }
                }
                Mock -Verifiable -ModuleName $modName -CommandName Set-DnsClientServerAddress -MockWith {}       
                $arguments = @{ 
                        Address = '8.8.8.8', '4.4.4.4' 
                        AddressFamily = 'IPv4'
                        InterfaceAlias = 'Ethernet 2'
                        Apply = $true
                        }
                $expected = $true
                $result = ValidateProperties @arguments
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled -ModuleName $modName Get-DnsClientServerAddress 
                }
                It 'should NOT call Set-DnsClientServerAddress' {
                    Assert-MockCalled -ModuleName $modName Set-DnsClientServerAddress 0
                }
                It 'should return true ' {
                    $result | should be ($expected)
                }
            }
            Context 'when Apply is set and values do not match' {
                Mock -Verifiable -ModuleName $modName -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{ 
                        ServerAddresses = '8.8.8.8'
                    }
                }
                Mock -Verifiable -ModuleName $modName -CommandName Set-DnsClientServerAddress -MockWith {}       
                $arguments = @{ 
                        Address = '8.8.8.8', '4.4.4.4' 
                        AddressFamily = 'IPv4'
                        InterfaceAlias = 'Ethernet 2'
                        Apply = $true
                        }
                $expected = $nul
                $result = ValidateProperties @arguments
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled -ModuleName $modName Get-DnsClientServerAddress 
                }
                It 'should call Set-DnsClientServerAddress' {
                    Assert-MockCalled -ModuleName $modName Set-DnsClientServerAddress 
                }
                It 'should not return any value' {
                    $result | should be ($expected)
                }
            }
        }
    }
    
    
    if (get-module -name $modName) { remove-module -Name $modName }
    

    The fact that c_DNSServerAddress exports only exports *-TargetResorce I have to import it as a module to get access to the function ValidateProperties.

    I want it to work in the console so I can run invoke-build as a scheduled task and have no failed tests.

  • #19487
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'll check into this as soon as I can. Are you running this test against https://github.com/PowerShellOrg/DSC/tree/master/Resources/cNetworking/DSCResources/PSHOrg_cDNSServerAddress ?

  • #19488
    Profile photo of David Jones
    David Jones
    Participant

    To the most part. I added output types to my copy

    function Get-TargetResource
    {
        [OutputType([Hashtable])]
        param (
    function Test-TargetResource
    {
        [OutputType([boolean])]
        param (
  • #19497
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Ah, that's a subtle problem:

        InModuleScope $modName {
            Context 'when invalid address supplied' {
                Mock -Verifiable -ModuleName $modName -CommandName Get-DnsClientServerAddress -MockWith {}
                Mock -Verifiable -ModuleName $modName -CommandName Set-DnsClientServerAddress -MockWith {}
    
    

    You're inside an InModuleScope block when you call those Mock commands. The problem is that the $modName variable doesn't exist in the module's scope; it's out in the Script scope of the Tests.ps1 file. The reason this worked in the ISE, most likely, is that you ran the script once by pressing F5 at some point, which makes the script's scope global (it's basically dot-sourcing the file when you do that.) Then the $modName variable could be resolved from inside the module as well.

    The good news is, you don't have to get too creative to fix this. When you're using InModuleScope, you don't have to bother with the -ModuleName parameters on Mock or Assert-MockCalled; that value will default to the scope that the command is running in.

    Couple of other things I noticed while reviewing this: You don't have to use the -Verifiable switch when calling Mock unless you intend to use the Assert-VerifiableMocks command. Assert-MockCalled is a newer command, and works on all mocks regardless of whether -Verifiable was used or not. Also, when you're mocking a command, you shouldn't specify a param block inside the -MockWith script block. (I'm not quite sure why your tests were working when you did that... will try to figure that out later.) The way Pester works with mocking, it duplicates the parameter block of the command that is being mocked and just injects your new script block's code as the implementation. You can automatically access variables with names of the original command's parameters.

    I tested the code with these tweaks, and it worked for me. Here's what I wound up with:

    $script:here = Split-Path -Parent $MyInvocation.MyCommand.Path
    $script:sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.ps1", ".psm1")
    $script:modName = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.ps1", "")
    $script:pathtosut = join-path $script:here $script:sut
    
    if (get-module -name $modName) { remove-module -Name $modName }
    
    $script:t = [scriptblock]::Create(( gc $script:pathtosut -Raw ) )
    New-Module -Name $modName -ScriptBlock $t  | Import-Module -Force
    
    Describe "how Get-TargetResource ($sut) responds" {
        Context 'when interface has DNS entries' {
            Mock -Verifiable -ModuleName $modName -CommandName  Get-DnsClientServerAddress -MockWith {
                return @{
                    ServerAddresses = '8.8.8.8','4.4.4.4'
                }
            }
    
            $arguments = @{ Address = '8.8.8.8','4.4.4.4'
                AddressFamily = 'IPv4'
                InterfaceAlias = 'Ethernet 2'
            }
    
            $result = Get-TargetResource @arguments
    
            It "should call all the mocks" {
                Assert-VerifiableMocks
            }
    
            It 'should fill all values in hash table' {
                $result.Address | should not be $null
                $result.AddressFamily | should not be $null
                $result.InterfaceAlias | should not be $null
            }
    
            it 'should return expected values' {
                $result.Address | should be '8.8.8.8','4.4.4.4'
                $result.AddressFamily | should be 'IPv4'
                $result.InterfaceAlias | should be 'Ethernet 2'
            }
        }
    
        Context 'When inteface has no DNS entries' {
            Mock -Verifiable -ModuleName $modName -CommandName  Get-DnsClientServerAddress -MockWith {
                return @{
                    ServerAddresses = $null
                }
            }
    
            $arguments = @{ Address = '8.8.8.8','4.4.4.4'
                AddressFamily = 'IPv4'
                InterfaceAlias = 'Ethernet 2'
            }
    
            $result = Get-TargetResource @arguments
    
            It "should call all the mocks" {
                Assert-VerifiableMocks
            }
    
            It 'should fill all values in hash table' {
                $result.Address | should be $null
                $result.AddressFamily | should not be $null
                $result.InterfaceAlias | should not be $null
            }
    
            it 'should return expected values' {
                $result.Address | should be $null
                $result.AddressFamily | should be 'IPv4'
                $result.InterfaceAlias | should be 'Ethernet 2'
            }
        }
    }
    
    
    
    Describe "how Test-TargetResource ($sut) responds" {
        Context 'when called with valid paramaters' {
            Mock -ModuleName $modName -CommandName ValidateProperties -MockWith {
                return $Apply
            }
    
            $arguments = @{
                Address = '8.8.8.8', '4.4.4.4'
                AddressFamily = 'IPv4'
                InterfaceAlias = 'Ethernet 2'
            }
    
            $result = Test-TargetResource @arguments
    
            It "should call ValidateProperties without -Apply " {
                $result | Should be $false
            }
    
        }
    }
    
    Describe "how Set-TargetResource ($sut) responds" {
        Context 'when called with valid paramaters' {
            Mock -Verifiable -ModuleName $modName -CommandName ValidateProperties -MockWith {
                return $Apply
            }
    
            $arguments = @{
                Address = '8.8.8.8', '4.4.4.4'
                AddressFamily = 'IPv4'
                InterfaceAlias = 'Ethernet 2'
            }
    
            $result = Set-TargetResource @arguments
    
            It "should call all the mocks" {
                Assert-VerifiableMocks
            }
    
            It "Should call ValidateProperties with -Apply"{
                $result | Should be $true
            }
        }
    }
    
    Describe "how ValidateProperties ($sut) responds" {
        InModuleScope $modName {
            Context 'when invalid address supplied' {
                Mock -CommandName Get-DnsClientServerAddress -MockWith {}
                Mock -CommandName Set-DnsClientServerAddress -MockWith {}
    
                $arguments = @{
                    Address = 'Void'
                    AddressFamily = 'IPv4'
                    InterfaceAlias = 'Ethernet 2'
                }
    
                It "should throw error" {
                    {ValidateProperties @arguments} | should throw
                }
            }
    
            Context 'when Apply NOT set and values match' {
                Mock -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{
                        ServerAddresses = '8.8.8.8', '4.4.4.4'
                    }
                }
    
                Mock -CommandName Set-DnsClientServerAddress -MockWith {}
    
                $arguments = @{
                    Address = '8.8.8.8', '4.4.4.4'
                    AddressFamily = 'IPv4'
                    InterfaceAlias = 'Ethernet 2'
                }
    
                $expected = $true
                $result = ValidateProperties @arguments
    
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled Get-DnsClientServerAddress
                }
    
                It 'should NOT call Set-DnsClientServerAddress' {
                    Assert-MockCalled Set-DnsClientServerAddress 0
                }
    
                It 'should return true ' {
                    $result | should be ($expected)
                }
            }
    
            Context 'when Apply is NOT set and values do not match' {
                Mock -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{
                        ServerAddresses = '8.8.8.8'
                    }
                }
    
                Mock -CommandName Set-DnsClientServerAddress -MockWith {}
    
                $arguments = @{
                    Address = '8.8.8.8', '4.4.4.4'
                    AddressFamily = 'IPv4'
                    InterfaceAlias = 'Ethernet 2'
                    Apply = $false
                }
    
                $result = ValidateProperties @arguments
    
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled Get-DnsClientServerAddress
                }
    
                It 'should NOT call Set-DnsClientServerAddress' {
                    Assert-MockCalled Set-DnsClientServerAddress 0
                }
    
                It 'should return false ' {
                    $result | should be $false
                }
            }
    
            Context 'when Apply is set and values match' {
                Mock -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{
                        ServerAddresses = '8.8.8.8', '4.4.4.4'
                    }
                }
    
                Mock -CommandName Set-DnsClientServerAddress -MockWith {}
    
                $arguments = @{
                    Address = '8.8.8.8', '4.4.4.4'
                    AddressFamily = 'IPv4'
                    InterfaceAlias = 'Ethernet 2'
                    Apply = $true
                }
    
                $result = ValidateProperties @arguments
    
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled Get-DnsClientServerAddress
                }
    
                It 'should NOT call Set-DnsClientServerAddress' {
                    Assert-MockCalled Set-DnsClientServerAddress 0
                }
    
                It 'should return true ' {
                    $result | should be $true
                }
            }
    
            Context 'when Apply is set and values do not match' {
                Mock -CommandName Get-DnsClientServerAddress -MockWith {
                    return @{
                        ServerAddresses = '8.8.8.8'
                    }
                }
    
                Mock -CommandName Set-DnsClientServerAddress -MockWith {}
    
                $arguments = @{
                    Address = '8.8.8.8', '4.4.4.4'
                    AddressFamily = 'IPv4'
                    InterfaceAlias = 'Ethernet 2'
                    Apply = $true
                }
    
                $result = ValidateProperties @arguments
    
                It "should call Get-DnsClientServerAddress " {
                    Assert-MockCalled Get-DnsClientServerAddress
                }
    
                It 'should call Set-DnsClientServerAddress' {
                    Assert-MockCalled Set-DnsClientServerAddress
                }
    
                It 'should not return any value' {
                    $result | should be $null
                }
            }
        }
    }
    
    if (get-module -name $modName) { remove-module -Name $modName }
    
  • #19510
    Profile photo of David Jones
    David Jones
    Participant

    The extra -Verifiable are a product of the organic way the script evolved.

    Thank you for the help. I have been banging my head on the keyboard trying to figure out why that did not work. A lot of the unneeded code was due to the fact I could not get the mocks to be called. most the time it was still calling the build in function.

    I need to get this and the cIPaddress one cleaned up and submit them to github. cIPaddress was a a mess cDNSServerAddress at leaset worked as advertised.

  • #19677
    Profile photo of David Jones
    David Jones
    Participant

    The test works fine with the current DSC Master but DSC\Deveopment it fails something spectacular.

    Any idea.

  • #19678
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'm not sure I understand the problem. The DSC tooling modules don't change how Pester works. Do you have some specific error messages you can share?

  • #19679
    Profile photo of David Jones
    David Jones
    Participant

    when running the code you cleaned up with DSC\Development, invoke-dscbuild I get

    escribing how Get-TargetResource (PSHOrg_cDNSServerAddress.psm1) responds
      Context when interface has DNS entries
       [-] Error occurred in Context block 943ms
         Could not find Command Get-DnsClientServerAddress
         At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1:593 char:9
      Context When inteface has no DNS entries
       [-] Error occurred in Context block 522ms
         Could not find Command Get-DnsClientServerAddress
         At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1:593 char:9
    escribing how Test-TargetResource (PSHOrg_cDNSServerAddress.psm1) responds
      Context when called with valid paramaters
       [+] should call ValidateProperties without -Apply  364ms
    escribing how Set-TargetResource (PSHOrg_cDNSServerAddress.psm1) responds
      Context when called with valid paramaters
       [+] should call all the mocks 171ms
       [+] Should call ValidateProperties with -Apply 5ms
    escribing how ValidateProperties (PSHOrg_cDNSServerAddress.psm1) responds
      Context when invalid address supplied
       [-] Error occurred in Context block 272ms
         Could not find Command Get-DnsClientServerAddress
         At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1:593 char:9
      Context when Apply NOT set and values match
       [-] Error occurred in Context block 267ms
         Could not find Command Get-DnsClientServerAddress
         At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1:593 char:9
      Context when Apply is NOT set and values do not match
       [-] Error occurred in Context block 271ms
         Could not find Command Get-DnsClientServerAddress
         At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1:593 char:9
      Context when Apply is set and values match
       [-] Error occurred in Context block 302ms
         Could not find Command Get-DnsClientServerAddress
         At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1:593 char:9
      Context when Apply is set and values do not match
       [-] Error occurred in Context block 246ms
         Could not find Command Get-DnsClientServerAddress
         At C:\Program Files\WindowsPowerShell\Modules\Pester\Functions\Mock.ps1:593 char:9
    ERBOSE: Removing the imported "Get-TargetResource" function.
    ERBOSE: Removing the imported "Set-TargetResource" function.
    ERBOSE: Removing the imported "Test-TargetResource" function.
    ests completed in 3.37s
    assed: 3 Failed: 7
     Resource Unit Tests were failed.
    t C:\Program Files\WindowsPowerShell\Modules\dscbuild\Invoke-DscResourceUnitTest.ps1:20 char:17
                     throw "$failCount Resource Unit Tests were failed."
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       + CategoryInfo          : OperationStopped: (7 Resource Unit Tests were failed.:String) [], RuntimeException
       + FullyQualifiedErrorId : 7 Resource Unit Tests were failed.

    With DSC/Master it works. But only once. then you have to close PowerShell or it throws a bunch of errors on subsequent runs.

  • #19680
    Profile photo of David Jones
    David Jones
    Participant

    I should also note that after running invoke-dscbuild this happens.

    PS H:\> Import-Module DnsClient
    Import-Module : Cannot process Cmdlet Definition XML for the following file:
    C:\Windows\system32\WindowsPowerShell\v1.0\Modules\DnsClient\MSFT_DnsClient.cdxml. At line:363 char:17
    +                 $__cmdletization_objectModelWrapper = Microsoft.PowerShell.Utili ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    At line:364 char:17
    +                 $__cmdletization_objectModelWrapper.Initialize($PSCmdlet, $scrip ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    At line:368 char:21
    +                     ([System.Management.Automation.IDynamicParameters]$__cmdleti ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    At line:366 char:17
    +                 if ($__cmdletization_objectModelWrapper -is [System.Management.A ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    At line:361 char:13
    +             if (-not $__cmdletization_exceptionHasBeenThrown)
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    At line:374 char:13
    +             $__cmdletization_exceptionHasBeenThrown = $true
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    At line:375 char:13
    +             throw
    +             ~~~~~
    Executable script code found in signature block.
    At line:359 char:9
    +         try
    +         ~~~
    Executable script code found in signature block.
    At line:380 char:9
    +         $__cmdletization_exceptionHasBeenThrown = $false
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    At line:383 char:13
    +             __cmdletization_BindCommonParameters $__cmdletization_objectModelWra ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Executable script code found in signature block.
    Not all parse errors were reported.  Correct the reported errors and try again.
    At line:1 char:1
    + Import-Module DnsClient
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Import-Module], XmlException
        + FullyQualifiedErrorId : System.Xml.XmlException,Microsoft.PowerShell.Commands.ImportModuleCommand

    So something is happening that is effecting the environment that stops built in modules to fail to load. This happens even if the test file is not present. and has been a problem with invoke-dscbuild for a while. before It would happen after the modules were tested, so it would not brake the current build. Now it's happening before, so the build brakes.

  • #19682
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Hmm, weird. I'll see if I can reproduce the problem. You're using the cNetworking resource module here, right?

  • #19683
    Profile photo of David Jones
    David Jones
    Participant

    Yes. but with some more testing it is repeatable with no resources at all to process.

    I have so far tracked it down to Copy-CurrentDscTools, but that makes little sense on first glance as I ran invoke-dscbuild with -resources.

    Once Powershell cant load the the modules pester's Mock because useless.

    I filed an issue on github. and am continuing to look where the brake happens.

  • #19686
    Profile photo of David Jones
    David Jones
    Participant

    ok. Tracked it down to two things.
    * Having a signature on a psd1 file in DSC_Tooling or DSC_Resource
    * Having DscOperations in the DSC_Tooling folder
    In thoes cases Invoke-DscBuild messes up the Enviroment when it runs this line in Get-DscResorcesVersion.ps1
    line 19 in Get-ModuleVersion
    $Version = (Invoke-Expression -Command $psd1)['ModuleVersion']

    I removed the signature blocks and moved DscOperations out of the tooling folder and all it working for now.
    It was my understanding that the tooling folder was intended for both the configuration and the tools used to build. but I may be wrong and this is just a bug uncovered by me useing the tools in an unexpected way. (ahtow I'm sure it's now Steve had it in his presentation earlier this year)

You must be logged in to reply to this topic.