From 01c848109892f4da0ae3f882915ce11b8d3e46f4 Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Thu, 7 Apr 2016 08:07:06 +1200 Subject: [PATCH 1/5] MSFT_xDnsARecord: Removed hard coding of Localhost computer name to eliminate PSSA rule violation. --- DSCResources/MSFT_xDnsARecord/MSFT_xDnsARecord.psm1 | 4 ++-- README.md | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/DSCResources/MSFT_xDnsARecord/MSFT_xDnsARecord.psm1 b/DSCResources/MSFT_xDnsARecord/MSFT_xDnsARecord.psm1 index b2d8cb00..0267c935 100644 --- a/DSCResources/MSFT_xDnsARecord/MSFT_xDnsARecord.psm1 +++ b/DSCResources/MSFT_xDnsARecord/MSFT_xDnsARecord.psm1 @@ -65,11 +65,11 @@ function Set-TargetResource ) if ($Ensure -eq 'Present') { Write-Verbose "Creating for DNS $Target in $Zone" - Add-DnsServerResourceRecordA -IPv4Address $Target -Name $Name -ZoneName $Zone -ComputerName "localhost" + Add-DnsServerResourceRecordA -IPv4Address $Target -Name $Name -ZoneName $Zone } elseif ($Ensure -eq 'Absent') { Write-Verbose "Removing DNS $Target in $Zone" - Remove-DnsServerResourceRecord -Name $Name -ZoneName $Zone -RRType A -ComputerName "localhost" -Force + Remove-DnsServerResourceRecord -Name $Name -ZoneName $Zone -RRType A } } diff --git a/README.md b/README.md index 3eee9fe0..cb0ce292 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ Values include: { ARecord | CName } ### Unreleased +* MSFT_xDnsARecord: Removed hard coding of Localhost computer name to eliminate PSSA rule violation. + ### 1.6.0.0 * Added Resource xDnsServerForwarder. From a49d0faba5ab8e5c9bdf3634a72864a35271a453 Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Thu, 7 Apr 2016 10:30:29 +1200 Subject: [PATCH 2/5] * Unit tests updated to use standard unit test templates. * MSFT_xDnsServerZoneTransfer: Added unit tests. Updated to meet Style Guidelines. --- .../MSFT_xDnsServerZoneTransfer.psm1 | 91 +++++++--- README.md | 4 + Tests/Unit/MSFT_xDnsRecord.Tests.ps1 | 17 +- .../MSFT_xDnsServerADZone.Tests.ps1 | 93 +++++++---- Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 | 15 +- .../MSFT_xDnsServerPrimaryZone.Tests.ps1} | 90 ++++++---- .../MSFT_xDnsServerZoneTransfer.Tests.ps1 | 157 ++++++++++++++++++ 7 files changed, 349 insertions(+), 118 deletions(-) rename Tests/{ => Unit}/MSFT_xDnsServerADZone.Tests.ps1 (81%) rename Tests/{xDnsServerPrimaryZone.Tests.ps1 => Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1} (74%) create mode 100644 Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 diff --git a/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 b/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 index 5714af11..078e263e 100644 --- a/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 +++ b/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 @@ -40,7 +40,10 @@ function Get-TargetResource #endregion - $currentZone = Get-CimInstance -ClassName MicrosoftDNS_Zone -Namespace root\MicrosoftDNS -Verbose:$false | ?{$_.Name -eq $Name} + $currentZone = Get-CimInstance ` + -ClassName MicrosoftDNS_Zone ` + -Namespace root\MicrosoftDNS ` + -Verbose:$false | ?{$_.Name -eq $Name} @{ Name = $Name @@ -64,11 +67,14 @@ function Set-TargetResource [String[]]$SecondaryServer ) - if($PSBoundParameters.ContainsKey('Debug')){$null = $PSBoundParameters.Remove('Debug')} + if($PSBoundParameters.ContainsKey('Debug')) + { + $null = $PSBoundParameters.Remove('Debug') + } Validate-ResourceProperties @PSBoundParameters -Apply # Restart the DNS service - Restart-Service DNS + Restart-Service -Name DNS } @@ -95,8 +101,11 @@ function Test-TargetResource #endregion - if($PSBoundParameters.ContainsKey('Debug')){$null = $PSBoundParameters.Remove('Debug')} - Validate-ResourceProperties @PSBoundParameters + if($PSBoundParameters.ContainsKey('Debug')) + { + $null = $PSBoundParameters.Remove('Debug') + } + Validate-ResourceProperties @PSBoundParameters } function Validate-ResourceProperties @@ -117,44 +126,68 @@ function Validate-ResourceProperties [Switch]$Apply ) - $checkZoneMessage = $($LocalizedData.CheckingZoneMessage) -f $Name + $checkZoneMessage = $($LocalizedData.CheckingZoneMessage) ` + -f $Name Write-Verbose -Message $checkZoneMessage # Get the current value of transfer zone - $currentZone = Get-CimInstance -ClassName MicrosoftDNS_Zone -Namespace root\MicrosoftDNS -Verbose:$false | ? {$_.Name -eq $Name} + $currentZone = Get-CimInstance ` + -ClassName MicrosoftDNS_Zone ` + -Namespace root\MicrosoftDNS ` + -Verbose:$false | ? {$_.Name -eq $Name} $currentZoneTransfer = $currentZone.SecureSecondaries # Hashtable with 2 keys: SecureSecondaries,SecondaryServers $Arguments = @{} - + switch ($Type) { - 'None' {$Arguments['SecureSecondaries'] = 3} - 'Any' {$Arguments['SecureSecondaries'] = 0} - 'Named' {$Arguments['SecureSecondaries'] = 1} - 'Specific' {$Arguments['SecureSecondaries'] = 2; $Arguments['SecondaryServers']=$SecondaryServer} + 'None' + { + $Arguments['SecureSecondaries'] = 3 + } + 'Any' + { + $Arguments['SecureSecondaries'] = 0 + } + 'Named' + { + $Arguments['SecureSecondaries'] = 1 + } + 'Specific' + { + $Arguments['SecureSecondaries'] = 2 + $Arguments['SecondaryServers']=$SecondaryServer + } } # Check the current value against expected value if($currentZoneTransfer -eq $Arguments.SecureSecondaries) { - $desiredZoneMessage = ($LocalizedData.DesiredZoneMessage) -f $XferId2Name[$currentZoneTransfer] + $desiredZoneMessage = ($LocalizedData.DesiredZoneMessage) ` + -f $XferId2Name[$currentZoneTransfer] Write-Verbose -Message $desiredZoneMessage # If the Type is specific, and SecondaryServer doesn't match - if(($currentZoneTransfer -eq 2) -and (Compare-Object $currentZone.SecondaryServers $SecondaryServer)) + if(($currentZoneTransfer -eq 2) ` + -and (Compare-Object $currentZone.SecondaryServers $SecondaryServer)) { - $notDesiredPropertyMessage = ($LocalizedData.NotDesiredPropertyMessage) -f ($SecondaryServer -join ','),($currentZone.SecondaryServers -join ',') + $notDesiredPropertyMessage = ($LocalizedData.NotDesiredPropertyMessage) ` + -f ($SecondaryServer -join ','),($currentZone.SecondaryServers -join ',') Write-Verbose -Message $notDesiredPropertyMessage # Set the SecondaryServer property if($Apply) { - $settingPropertyMessage = ($LocalizedData.SettingPropertyMessage) -f ($SecondaryServer -join ',') + $settingPropertyMessage = ($LocalizedData.SettingPropertyMessage) ` + -f ($SecondaryServer -join ',') Write-Verbose -Message $settingPropertyMessage - $null = Invoke-CimMethod -InputObject $currentZone -MethodName ResetSecondaries ` - -Arguments $Arguments -Verbose:$false + $null = Invoke-CimMethod ` + -InputObject $currentZone ` + -MethodName ResetSecondaries ` + -Arguments $Arguments ` + -Verbose:$false $setPropertyMessage = $LocalizedData.SetPropertyMessage Write-Verbose -Message $setPropertyMessage @@ -162,7 +195,7 @@ function Validate-ResourceProperties else { return $false - } + } } # end SecondaryServer match if(-not $Apply) @@ -170,18 +203,23 @@ function Validate-ResourceProperties return $true } } # end currentZoneTransfer -eq ExpectedZoneTransfer - - else + else { - $notDesiredZoneMessage = $($LocalizedData.NotDesiredZoneMessage) -f $XferId2Name[$Arguments.SecureSecondaries],$XferId2Name[$currentZoneTransfer] + $notDesiredZoneMessage = $($LocalizedData.NotDesiredZoneMessage) ` + -f $XferId2Name[$Arguments.SecureSecondaries], ` + $XferId2Name[$currentZoneTransfer] Write-Verbose -Message $notDesiredZoneMessage if($Apply) { - $null = Invoke-CimMethod -InputObject $currentZone -MethodName ResetSecondaries ` - -Arguments $Arguments -Verbose:$false - - $setZoneMessage = $($LocalizedData.SetZoneMessage) -f $Name,$XferId2Name[$Arguments.SecureSecondaries] + $null = Invoke-CimMethod ` + -InputObject $currentZone ` + -MethodName ResetSecondaries ` + -Arguments $Arguments ` + -Verbose:$false + + $setZoneMessage = $($LocalizedData.SetZoneMessage) ` + -f $Name,$XferId2Name[$Arguments.SecureSecondaries] Write-Verbose -Message $setZoneMessage } else @@ -192,4 +230,3 @@ function Validate-ResourceProperties } Export-ModuleMember -Function *-TargetResource - diff --git a/README.md b/README.md index 3eee9fe0..57ef868c 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,10 @@ Values include: { ARecord | CName } ### Unreleased +* Unit tests updated to use standard unit test templates. +* MSFT_xDnsServerZoneTransfer: Added unit tests. + Updated to meet Style Guidelines. + ### 1.6.0.0 * Added Resource xDnsServerForwarder. diff --git a/Tests/Unit/MSFT_xDnsRecord.Tests.ps1 b/Tests/Unit/MSFT_xDnsRecord.Tests.ps1 index d61d29c0..4cfdcd53 100644 --- a/Tests/Unit/MSFT_xDnsRecord.Tests.ps1 +++ b/Tests/Unit/MSFT_xDnsRecord.Tests.ps1 @@ -25,9 +25,7 @@ try #region Pester Tests InModuleScope $Global:DSCResourceName { - #region Pester Test Initialization - $testPresentParams = @{ Name = "test" Zone = "contoso.com" @@ -52,9 +50,9 @@ try } } } - #endregion - + + #region Function Get-TargetResource Describe "$($Global:DSCResourceName)\Get-TargetResource" { It "Returns Ensure is Present when DNS record exists" { @@ -69,7 +67,9 @@ try } #endregion - + + + #region Function Test-TargetResource Describe "$($Global:DSCResourceName)\Test-TargetResource" { It "Fails when no DNS record exists and Ensure is Present" { @@ -140,8 +140,10 @@ try Test-TargetResource @testAbsentParams | Should Be $true } } - #end region + #endregion + + #region Function Set-TargetResource Describe "$($Global:DSCResourceName)\Set-TargetResource" { It "Calls Add-DnsServerResourceRecord in the set method when Ensure is Present" { @@ -157,9 +159,8 @@ try } } #endregion - - } #end InModuleScope + } #end InModuleScope } finally { diff --git a/Tests/MSFT_xDnsServerADZone.Tests.ps1 b/Tests/Unit/MSFT_xDnsServerADZone.Tests.ps1 similarity index 81% rename from Tests/MSFT_xDnsServerADZone.Tests.ps1 rename to Tests/Unit/MSFT_xDnsServerADZone.Tests.ps1 index 0711c973..dffeb285 100644 --- a/Tests/MSFT_xDnsServerADZone.Tests.ps1 +++ b/Tests/Unit/MSFT_xDnsServerADZone.Tests.ps1 @@ -1,30 +1,31 @@ -if (!$PSScriptRoot) # $PSScriptRoot is not defined in 2.0 +$Global:DSCModuleName = 'xDnsServer' +$Global:DSCResourceName = 'MSFT_xDnsServerADZone' + +#region HEADER +[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)) +if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) { - $PSScriptRoot = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path) + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\')) } +else +{ + & git @('-C',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'),'pull') +} +Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $Global:DSCModuleName ` + -DSCResourceName $Global:DSCResourceName ` + -TestType Unit +#endregion + +# Begin Testing +try +{ + #region Pester Tests -$ErrorActionPreference = 'stop' -Set-StrictMode -Version latest - -$RepoRoot = (Resolve-Path $PSScriptRoot\..).Path - -$ModuleName = 'MSFT_xDnsServerADZone' -Import-Module (Join-Path $RepoRoot "DSCResources\$ModuleName\$ModuleName.psm1") -Force - -Describe 'xDnsServerADZone' { - InModuleScope $ModuleName { - - ## Stub DnsServer module cmdlets - function Get-DnsServerZone { } - function Add-DnsServerPrimaryZone { param ( $Name ) } - function Set-DnsServerPrimaryZone { [CmdletBinding()] param ( - $Name, - $DynamicUpdate, - $ReplicationScope, - $DirectoryPartitionName, - $CimSession ) } - function Remove-DnsServerZone { } - + InModuleScope $Global:DSCResourceName { + #region Pester Test Initialization $testZoneName = 'example.com'; $testDynamicUpdate = 'Secure'; $testReplicationScope = 'Domain'; @@ -57,10 +58,13 @@ Describe 'xDnsServerADZone' { } $fakeAbsentTargetResource = @{ Ensure = 'Absent' } + #endregion - Mock -CommandName 'Assert-Module' -MockWith { } + #region Function Get-TargetResource + Describe "$($Global:DSCResourceName)\Get-TargetResource" { + function Get-DnsServerZone { } - Context 'Validates Get-TargetResource Method' { + Mock -CommandName 'Assert-Module' -MockWith { } It 'Returns a "System.Collections.Hashtable" object type' { $targetResource = Get-TargetResource @testParams -ReplicationScope $testReplicationScope; @@ -90,11 +94,14 @@ Describe 'xDnsServerADZone' { $targetResource = Get-TargetResource @testParams -ReplicationScope $testReplicationScope -Ensure Absent; $targetResource.Ensure | Should Be 'Absent'; } + } + #endregion - } #end context Validates Get-TargetResource Method - - Context 'Validates Test-TargetResource Method' { + #region Function Test-TargetResource + Describe "$($Global:DSCResourceName)\Test-TargetResource" { + function Get-DnsServerZone { } + It 'Returns a "System.Boolean" object type' { Mock -CommandName Get-TargetResource -MockWith { return $fakePresentTargetResource; } $targetResource = Test-TargetResource @testParams -ReplicationScope $testReplicationScope; @@ -150,10 +157,21 @@ Describe 'xDnsServerADZone' { Mock -CommandName Get-TargetResource -MockWith { return $fakePresentTargetResource; } Test-TargetResource @testParams -Ensure Present -ReplicationScope $testReplicationScope -DirectoryPartitionName 'IncorrectDirectoryPartitionName' | Should Be $false; } + } + #endregion - } #end context Validates Test-TargetResource Method - Context 'Validates Set-TargetResource Method' { + #region Function Set-TargetResource + Describe "$($Global:DSCResourceName)\Set-TargetResource" { + function Get-DnsServerZone { } + function Add-DnsServerPrimaryZone { param ( $Name ) } + function Set-DnsServerPrimaryZone { [CmdletBinding()] param ( + $Name, + $DynamicUpdate, + $ReplicationScope, + $DirectoryPartitionName, + $CimSession ) } + function Remove-DnsServerZone { } It 'Calls "Add-DnsServerPrimaryZone" when DNS zone does not exist and "Ensure" = "Present"' { Mock -CommandName Get-TargetResource -MockWith { return $fakeAbsentTargetResource } @@ -189,8 +207,13 @@ Describe 'xDnsServerADZone' { Set-TargetResource @testParams -Ensure Present -ReplicationScope $testReplicationScope -DirectoryPartitionName 'IncorrectDirectoryPartitionName'; Assert-MockCalled -CommandName Set-DnsServerPrimaryZone -ParameterFilter { $DirectoryPartitionName -eq 'IncorrectDirectoryPartitionName' } -Scope It; } - - } #end context Validates Set-TargetResource Method - - } + } + #endregion + } #end InModuleScope +} +finally +{ + #region FOOTER + Restore-TestEnvironment -TestEnvironment $TestEnvironment + #endregion } diff --git a/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 b/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 index f8e3a062..7504d30a 100644 --- a/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 +++ b/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 @@ -19,18 +19,12 @@ $TestEnvironment = Initialize-TestEnvironment ` -TestType Unit #endregion -# TODO: Other Optional Init Code Goes Here... - # Begin Testing try { - #region Pester Tests - # The InModuleScope command allows you to perform white-box unit testing on the internal - # (non-exported) code of a Script Module. InModuleScope $Global:DSCResourceName { - #region Pester Test Initialization $forwarders = '192.168.0.1','192.168.0.2' $testParams = @{ @@ -95,18 +89,11 @@ try Assert-MockCalled -CommandName Set-CimInstance -Times 1 -Exactly -Scope It } } - #endregion - - # TODO: Pester Tests for any Helper Cmdlets - - } - #endregion + } #end InModuleScope } finally { #region FOOTER Restore-TestEnvironment -TestEnvironment $TestEnvironment #endregion - - # TODO: Other Optional Cleanup Code Goes Here... } \ No newline at end of file diff --git a/Tests/xDnsServerPrimaryZone.Tests.ps1 b/Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 similarity index 74% rename from Tests/xDnsServerPrimaryZone.Tests.ps1 rename to Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 index 8d9c202a..e304da2c 100644 --- a/Tests/xDnsServerPrimaryZone.Tests.ps1 +++ b/Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 @@ -1,28 +1,30 @@ -if (!$PSScriptRoot) # $PSScriptRoot is not defined in 2.0 +$Global:DSCModuleName = 'xDnsServer' +$Global:DSCResourceName = 'MSFT_xDnsServerPrimaryZone' + +#region HEADER +[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)) +if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) { - $PSScriptRoot = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path) + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\')) } - -$ErrorActionPreference = 'stop' -Set-StrictMode -Version latest - -$RepoRoot = (Resolve-Path $PSScriptRoot\..).Path - -$ModuleName = 'MSFT_xDnsServerPrimaryZone' -Import-Module (Join-Path $RepoRoot "DSCResources\$ModuleName\$ModuleName.psm1") -Force - -Describe 'xDnsServerPrimaryZone' { - InModuleScope $ModuleName { - - ## Stub DnsServer module cmdlets - function Get-DnsServerZone { } - function Add-DnsServerPrimaryZone { param ( $Name ) } - function Set-DnsServerPrimaryZone { [CmdletBinding()] param ( - [Parameter(ValueFromPipeline)] $Name, - [Parameter(ValueFromPipeline)] $DynamicUpdate, - [Parameter(ValueFromPipeline)] $ZoneFile ) } - function Remove-DnsServerZone { } - +else +{ + & git @('-C',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'),'pull') +} +Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $Global:DSCModuleName ` + -DSCResourceName $Global:DSCResourceName ` + -TestType Unit +#endregion + +# Begin Testing +try +{ + #region Pester Tests + InModuleScope $Global:DSCResourceName { + #region Pester Test Initialization $testZoneName = 'example.com'; $testZoneFile = 'example.com.dns'; $testDynamicUpdate = 'None'; @@ -37,10 +39,13 @@ Describe 'xDnsServerPrimaryZone' { DirectoryPartitionName = $null; ZoneFile = $testZoneFile; } + #endregion - Mock -CommandName 'Assert-Module' -MockWith { } + #region Function Get-TargetResource + Describe 'Validates Get-TargetResource Method' { + function Get-DnsServerZone { } - Context 'Validates Get-TargetResource Method' { + Mock -CommandName 'Assert-Module' -MockWith { } It 'Returns a "System.Collections.Hashtable" object type' { $targetResource = Get-TargetResource @testParams; @@ -70,10 +75,13 @@ Describe 'xDnsServerPrimaryZone' { $targetResource = Get-TargetResource @testParams -ZoneFile 'example.com.dns' -Ensure Absent; $targetResource.Ensure | Should Be 'Absent'; } + } + #endregion - } #end context Validates Get-TargetResource Method - Context 'Validates Test-TargetResource Method' { + #region Function Test-TargetResource + Describe 'Validates Test-TargetResource Method' { + function Get-DnsServerZone { } It 'Returns a "System.Boolean" object type' { Mock -CommandName Get-DnsServerZone -MockWith { return $fakeDnsFileZone; } @@ -115,12 +123,21 @@ Describe 'xDnsServerPrimaryZone' { Mock -CommandName Get-DnsServerZone -MockWith { return $fakeDnsFileZone; } Test-TargetResource @testParams -Ensure Present -DynamicUpdate $testDynamicUpdate -ZoneFile 'nonexistent.com.dns' | Should Be $false; } + } + #endregion - } #end context Validates Test-TargetResource Method - - Context 'Validates Set-TargetResource Method' { + #region Function Set-TargetResource + Describe 'Validates Set-TargetResource Method' { It 'Calls "Add-DnsServerPrimaryZone" when DNS zone does not exist and "Ensure" = "Present"' { + function Get-DnsServerZone { } + function Add-DnsServerPrimaryZone { param ( $Name ) } + function Set-DnsServerPrimaryZone { [CmdletBinding()] param ( + [Parameter(ValueFromPipeline)] $Name, + [Parameter(ValueFromPipeline)] $DynamicUpdate, + [Parameter(ValueFromPipeline)] $ZoneFile ) } + function Remove-DnsServerZone { } + Mock -CommandName Get-DnsServerZone -MockWith { } Mock -CommandName Add-DnsServerPrimaryZone -ParameterFilter { $Name -eq $testZoneName } -MockWith { } Set-TargetResource @testParams -Ensure Present -DynamicUpdate $testDynamicUpdate -ZoneFile $testZoneFile; @@ -147,8 +164,13 @@ Describe 'xDnsServerPrimaryZone' { Set-TargetResource @testParams -Ensure Present -DynamicUpdate $testDynamicUpdate -ZoneFile 'nonexistent.com.dns'; Assert-MockCalled -CommandName Set-DnsServerPrimaryZone -ParameterFilter { $ZoneFile -eq 'nonexistent.com.dns' } -Scope It; } - - } #end context Validates Set-TargetResource Method - - } + } + #endregion + } #end InModuleScope } +finally +{ + #region FOOTER + Restore-TestEnvironment -TestEnvironment $TestEnvironment + #endregion +} \ No newline at end of file diff --git a/Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 b/Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 new file mode 100644 index 00000000..2b3d7c05 --- /dev/null +++ b/Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 @@ -0,0 +1,157 @@ +$Global:DSCModuleName = 'xDnsServer' +$Global:DSCResourceName = 'MSFT_xDnsServerZoneTransfer' + +#region HEADER +[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)) +if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\')) +} +else +{ + & git @('-C',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'),'pull') +} +Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $Global:DSCModuleName ` + -DSCResourceName $Global:DSCResourceName ` + -TestType Unit +#endregion + +# Begin Testing +try +{ + #region Pester Tests + + InModuleScope $Global:DSCResourceName { + #region Pester Test Initialization + $testName = 'example.com'; + $testType = 'Any'; + $testSecondaryServer = '192.168.0.1','192.168.0.2'; + $testParams = @{ + Name = $testName; + Type = $testType; + } + $testParamsAny = @{ + Name = $testName; + Type = 'Any'; + SecondaryServer = ''; + } + $testParamsSpecific = @{ + Name = $testName; + Type = 'Specific'; + SecondaryServer = $testSecondaryServer; + } + $testParamsSpecificDifferent = @{ + Name = $testName; + Type = 'Specific'; + SecondaryServer = '192.168.0.1','192.168.0.2','192.168.0.3'; + } + $fakeCimInstanceAny = @{ + Name = $testName; + SecureSecondaries = $XferId2Name.IndexOf('Any'); + SecondaryServers = ''; + } + $fakeCimInstanceNamed = @{ + Name = $testName; + SecureSecondaries = $XferId2Name.IndexOf('Named'); + SecondaryServers = ''; + } + $fakeCimInstanceSpecific = @{ + Name = $testName; + SecureSecondaries = $XferId2Name.IndexOf('Specific'); + SecondaryServers = $testSecondaryServer; + } + #endregion + + + #region Function Get-TargetResource + Describe "$($Global:DSCResourceName)\Get-TargetResource" { + It 'Returns a "System.Collections.Hashtable" object type' { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceAny} + $targetResource = Get-TargetResource @testParams + $targetResource -is [System.Collections.Hashtable] | Should Be $true + } + + It "Returns SecondaryServer = $($testParams.SecondaryServer) when zone transfers set to specific" { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceSpecific} + $targetResource = Get-TargetResource @testParams + $targetResource.SecondaryServers | Should Be $testParams.SecondaryServers + } + } + #endregion + + + #region Function Test-TargetResource + Describe "$($Global:DSCResourceName)\Test-TargetResource" { + It 'Returns a "System.Boolean" object type' { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceAny} + $targetResource = Test-TargetResource @testParamsAny + $targetResource -is [System.Boolean] | Should Be $true + } + + It 'Passes when Zone Transfer Type matches' { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceAny} + Test-TargetResource @testParamsAny | Should Be $true + } + + It "Fails when Zone Transfer Type does not match" { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceNamed} + Test-TargetResource @testParamsAny | Should Be $false + } + + It 'Passes when Zone Transfer Secondaries matches' { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceSpecific} + Test-TargetResource @testParamsSpecific | Should Be $true + } + + It 'Passes when Zone Transfer Secondaries does not match' { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceSpecific} + Test-TargetResource @testParamsSpecificDifferent | Should Be $false + } + } + #endregion + + + #region Function Set-TargetResource + Describe "$($Global:DSCResourceName)\Set-TargetResource" { + function Invoke-CimMethod { [CmdletBinding()] + param ( $InputObject, $MethodName, $Arguments ) + } + + Mock -CommandName Invoke-CimMethod -MockWith {} + Mock -CommandName Restart-Service -MockWith {} + + It "Calls Invoke-CimMethod not called when Zone Transfer Type matches" { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceAny} + Set-TargetResource @testParamsAny + Assert-MockCalled -CommandName Invoke-CimMethod -Times 0 -Exactly -Scope It + } + + It "Calls Invoke-CimMethod called once when Zone Transfer Type does not match" { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceNamed} + Set-TargetResource @testParamsAny + Assert-MockCalled -CommandName Invoke-CimMethod -Times 1 -Exactly -Scope It + } + + It "Calls Invoke-CimMethod not called when Zone Transfer Secondaries matches" { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceSpecific} + Set-TargetResource @testParamsSpecific + Assert-MockCalled -CommandName Invoke-CimMethod -Times 0 -Exactly -Scope It + } + + It "Calls Invoke-CimMethod called once when Zone Transfer Secondaries does not match" { + Mock -CommandName Get-CimInstance -MockWith {return $fakeCimInstanceSpecific} + Set-TargetResource @testParamsSpecificDifferent + Assert-MockCalled -CommandName Invoke-CimMethod -Times 1 -Exactly -Scope It + } + } + } #end InModuleScope +} +finally +{ + #region FOOTER + Restore-TestEnvironment -TestEnvironment $TestEnvironment + #endregion +} \ No newline at end of file From 517f7cf3e012d4067fa03ff5e4588dbe5b2bef9f Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Thu, 7 Apr 2016 10:33:20 +1200 Subject: [PATCH 3/5] Added missing CRLFs --- Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 | 2 +- Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 | 2 +- Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 b/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 index 7504d30a..452c2999 100644 --- a/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 +++ b/Tests/Unit/MSFT_xDnsServerForwarder.Tests.ps1 @@ -96,4 +96,4 @@ finally #region FOOTER Restore-TestEnvironment -TestEnvironment $TestEnvironment #endregion -} \ No newline at end of file +} diff --git a/Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 b/Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 index e304da2c..a43bf66d 100644 --- a/Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 +++ b/Tests/Unit/MSFT_xDnsServerPrimaryZone.Tests.ps1 @@ -173,4 +173,4 @@ finally #region FOOTER Restore-TestEnvironment -TestEnvironment $TestEnvironment #endregion -} \ No newline at end of file +} diff --git a/Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 b/Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 index 2b3d7c05..40b284cc 100644 --- a/Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 +++ b/Tests/Unit/MSFT_xDnsServerZoneTransfer.Tests.ps1 @@ -154,4 +154,4 @@ finally #region FOOTER Restore-TestEnvironment -TestEnvironment $TestEnvironment #endregion -} \ No newline at end of file +} From 3a28aef8ce62db51a6ad128f765a31c8bf01dc34 Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Sat, 9 Apr 2016 10:01:13 +1200 Subject: [PATCH 4/5] Fix to remove PSSA warnings. --- .../MSFT_xDnsServerZoneTransfer.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 b/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 index 078e263e..35278ca0 100644 --- a/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 +++ b/DSCResources/MSFT_xDnsServerZoneTransfer/MSFT_xDnsServerZoneTransfer.psm1 @@ -43,7 +43,7 @@ function Get-TargetResource $currentZone = Get-CimInstance ` -ClassName MicrosoftDNS_Zone ` -Namespace root\MicrosoftDNS ` - -Verbose:$false | ?{$_.Name -eq $Name} + -Verbose:$false | Where-Object {$_.Name -eq $Name} @{ Name = $Name @@ -134,7 +134,7 @@ function Validate-ResourceProperties $currentZone = Get-CimInstance ` -ClassName MicrosoftDNS_Zone ` -Namespace root\MicrosoftDNS ` - -Verbose:$false | ? {$_.Name -eq $Name} + -Verbose:$false | Where-Object {$_.Name -eq $Name} $currentZoneTransfer = $currentZone.SecureSecondaries # Hashtable with 2 keys: SecureSecondaries,SecondaryServers From 1357842aa50aa4cf0a0fdd7398dc47619faec288 Mon Sep 17 00:00:00 2001 From: Katie Keim Date: Wed, 18 May 2016 12:57:18 -0700 Subject: [PATCH 5/5] Releasing version 1.7.0.0 --- README.md | 2 ++ appveyor.yml | 4 ++-- xDnsServer.psd1 | 9 +++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4e7c5d8b..98628698 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ Values include: { ARecord | CName } ### Unreleased +### 1.7.0.0 + * Unit tests updated to use standard unit test templates. * MSFT_xDnsServerZoneTransfer: Added unit tests. Updated to meet Style Guidelines. diff --git a/appveyor.yml b/appveyor.yml index 2468f778..a7b65efb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ #---------------------------------# # environment configuration # #---------------------------------# -version: 1.6.{build}.0 +version: 1.7.{build}.0 install: - cinst -y pester - git clone https://github.com/PowerShell/DscResource.Tests @@ -41,7 +41,7 @@ deploy_script: # Creating project artifact $stagingDirectory = (Resolve-Path ..).Path $manifest = Join-Path $pwd "xDnsServer.psd1" - (Get-Content $manifest -Raw).Replace("1.6.0.0", $env:APPVEYOR_BUILD_VERSION) | Out-File $manifest + (Get-Content $manifest -Raw).Replace("1.7.0.0", $env:APPVEYOR_BUILD_VERSION) | Out-File $manifest $zipFilePath = Join-Path $stagingDirectory "$(Split-Path $pwd -Leaf).zip" Add-Type -assemblyname System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::CreateFromDirectory($pwd, $zipFilePath) diff --git a/xDnsServer.psd1 b/xDnsServer.psd1 index 281e9bb7..4213c538 100644 --- a/xDnsServer.psd1 +++ b/xDnsServer.psd1 @@ -1,6 +1,6 @@ @{ # Version number of this module. -ModuleVersion = '1.6.0.0' +ModuleVersion = '1.7.0.0' # ID used to uniquely identify this module GUID = '5f70e6a1-f1b2-4ba0-8276-8967d43a7ec2' @@ -47,7 +47,12 @@ PrivateData = @{ # IconUri = '' # ReleaseNotes of this module - # ReleaseNotes = '' + ReleaseNotes = '* Unit tests updated to use standard unit test templates. +* MSFT_xDnsServerZoneTransfer: Added unit tests. + Updated to meet Style Guidelines. +* MSFT_xDnsARecord: Removed hard coding of Localhost computer name to eliminate PSSA rule violation. + +' } # End of PSData hashtable