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.