Skip to content

Commit

Permalink
Merge pull request #29 from PlagueHO/Issue-27-Update-Unit-Tests
Browse files Browse the repository at this point in the history
Updated Unit tests to use latest Unit Test templates, Added new Unit tests - fixes #27
  • Loading branch information
TravisEz13 committed Apr 11, 2016
2 parents 9539f63 + 3cc6846 commit 3484a07
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 | Where-Object {$_.Name -eq $Name}

@{
Name = $Name
Expand All @@ -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
}


Expand All @@ -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
Expand All @@ -117,71 +126,100 @@ 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 | Where-Object {$_.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
}
else
{
return $false
}
}
} # end SecondaryServer match

if(-not $Apply)
{
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
Expand All @@ -192,4 +230,3 @@ function Validate-ResourceProperties
}

Export-ModuleMember -Function *-TargetResource

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
* **xDnsServerSecondaryZone** sets a Secondary zone on a given DNS server.
Secondary zones allow client machine in primary DNS zones to do DNS resolution of machines in the secondary DNS zone.
* **xDnsServerZoneTransfer** This resource allows a DNS Server zone data to be replicated to another DNS server.
* **xDnsRecord** This resource allwos for the creation of IPv4 host (A) records or CNames against a specific zone on the DNS server
* **xDnsRecord** This resource allows for the creation of IPv4 host (A) records or CNames against a specific zone on the DNS server

### xDnsServerForwarder

Expand Down Expand Up @@ -82,6 +82,9 @@ Values include: { ARecord | CName }

### Unreleased

* 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.

### 1.6.0.0
Expand Down
17 changes: 9 additions & 8 deletions Tests/Unit/MSFT_xDnsRecord.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ try
#region Pester Tests

InModuleScope $Global:DSCResourceName {

#region Pester Test Initialization

$testPresentParams = @{
Name = "test"
Zone = "contoso.com"
Expand All @@ -52,9 +50,9 @@ try
}
}
}

#endregion


#region Function Get-TargetResource
Describe "$($Global:DSCResourceName)\Get-TargetResource" {

It "Returns Ensure is Present when DNS record exists" {
Expand All @@ -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" {
Expand Down Expand Up @@ -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" {
Expand All @@ -157,9 +159,8 @@ try
}
}
#endregion

} #end InModuleScope

} #end InModuleScope
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 3484a07

Please sign in to comment.