Skip to content

Commit

Permalink
xDnsServerZoneScope: Add integration test (#188)
Browse files Browse the repository at this point in the history
- xDnsServerZoneScope
  - Added integration tests (issue #177).
  - New read-only property `ZoneFile` was added to return the zone scope
    file name used for the zone scope.
  - Correctly returns the zone scope name when calling `Get-TargetResource`.
  • Loading branch information
johlju authored Feb 28, 2021
1 parent 56f39a6 commit ec75a94
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added more examples.
- xDnsRecordMx
- Added new resource to manage MX records
- xDnsServerZoneScope
- Added integration tests ([issue #177](https://github.com/dsccommunity/xDnsServer/issues/177)).
- New read-only property `ZoneFile` was added to return the zone scope
file name used for the zone scope.
- xDnsServerZoneAging
- Added integration tests ([issue #176](https://github.com/dsccommunity/xDnsServer/issues/176)).
- xDnsServerForwarder
Expand Down Expand Up @@ -110,6 +114,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- xDnsServerAdZone
- Now the parameter `ComputerName` can be used without throwing an exception
([issue 79](https://github.com/PowerShell/xDnsServer/issues/79)).
- xDnsServerZoneScope
- Correctly returns the zone scope name when calling `Get-TargetResource`.
- xDnsServerForwarder
- Now it is possible to just enforce the property `UseRooHint` without
changing forwarders.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ function Get-TargetResource
return @{
Name = $Name
ZoneName = $ZoneName
ZoneFile = $null
Ensure = 'Absent'
}
}

return @{
Name = $record.Name
Name = $record.ZoneScope
ZoneName = $record.ZoneName
ZoneFile = $record.FileName
Ensure = 'Present'
}
} #end function Get-TargetResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class MSFT_xDnsServerZoneScope : OMI_BaseResource
[Key, Description("Specifies the name of the Zone Scope.")] string Name;
[Key, Description("Specify the existing DNS Zone to add a scope to.")] string ZoneName;
[Write, Description("Should this DNS Server Zone Scope be present or absent"), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Read, Description("Returns the zone scope filename.")] string ZoneFile;
};

190 changes: 190 additions & 0 deletions tests/Integration/MSFT_xDnsServerZoneScope.Integration.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
$script:dscModuleName = 'xDnsServer'
$script:dscResourceFriendlyName = 'xDnsServerZoneScope'
$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)"

try
{
Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop'
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.'
}

$script:testEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:dscModuleName `
-DSCResourceName $script:dscResourceName `
-ResourceType 'Mof' `
-TestType 'Integration'

try
{
$configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1"
. $configFile

Describe "$($script:dscResourceName)_Integration" {
BeforeAll {
$resourceId = "[$($script:dscResourceFriendlyName)]Integration_Test"
}

$configurationName = "$($script:dscResourceName)_Prerequisites_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
}

Wait-ForIdleLcm -Clear

$configurationName = "$($script:dscResourceName)_AddZoneScope_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop
} | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $ConfigurationData.AllNodes.ZoneScopeName
$resourceCurrentState.ZoneName | Should -Be $ConfigurationData.AllNodes.ForwardZoneName
$resourceCurrentState.ZoneFile | Should -Be ('{0}.dns' -f $ConfigurationData.AllNodes.ZoneScopeName)
}

It 'Should return ''True'' when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be 'True'
}
}

Wait-ForIdleLcm -Clear

$configurationName = "$($script:dscResourceName)_RemoveZoneScope_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop
} | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Absent'
$resourceCurrentState.Name | Should -Be $ConfigurationData.AllNodes.ZoneScopeName
$resourceCurrentState.ZoneName | Should -Be $ConfigurationData.AllNodes.ForwardZoneName
$resourceCurrentState.ZoneFile | Should -BeNullOrEmpty
}

It 'Should return ''True'' when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be 'True'
}
}

Wait-ForIdleLcm -Clear

$configurationName = "$($script:dscResourceName)_Cleanup_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
}

Wait-ForIdleLcm -Clear
}
}
finally
{
Restore-TestEnvironment -TestEnvironment $script:testEnvironment
}
68 changes: 68 additions & 0 deletions tests/Integration/MSFT_xDnsServerZoneScope.config.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
$ConfigurationData = @{
AllNodes = @(
@{
NodeName = 'localhost'
CertificateFile = $env:DscPublicCertificatePath

ForwardZoneName = 'dsc.test'
ZoneScopeName = 'dsc_Europe'
}
)
}

configuration MSFT_xDnsServerZoneScope_Prerequisites_Config
{
Import-DscResource -ModuleName 'xDnsServer'

node $AllNodes.NodeName
{
xDnsServerPrimaryZone 'ForwardZone'
{
Name = $Node.ForwardZoneName
}
}
}

configuration MSFT_xDnsServerZoneScope_AddZoneScope_Config
{
Import-DscResource -ModuleName 'xDnsServer'

node $AllNodes.NodeName
{
xDnsServerZoneScope 'Integration_Test'
{
Name = $Node.ZoneScopeName
ZoneName = $Node.ForwardZoneName
}
}
}

configuration MSFT_xDnsServerZoneScope_RemoveZoneScope_Config
{
Import-DscResource -ModuleName 'xDnsServer'

node $AllNodes.NodeName
{
xDnsServerZoneScope 'Integration_Test'
{
Ensure = 'Absent'
Name = $Node.ZoneScopeName
ZoneName = $Node.ForwardZoneName
}
}
}


configuration MSFT_xDnsServerZoneScope_Cleanup_Config
{
Import-DscResource -ModuleName 'xDnsServer'

node $AllNodes.NodeName
{
xDnsServerPrimaryZone 'ForwardZone'
{
Ensure = 'Absent'
Name = $Node.ForwardZoneName
}
}
}
6 changes: 4 additions & 2 deletions tests/Unit/MSFT_xDnsServerZoneScope.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ try
$mocks = @{
ZoneScopePresent = {
[PSCustomObject]@{
ZoneName = 'contoso.com'
Name = 'ZoneScope'
ZoneName = 'contoso.com'
ZoneScope = 'ZoneScope'
FileName = 'ZoneScope.dns'
}
}
Absent = { }
Expand All @@ -53,6 +54,7 @@ try
$getTargetResourceResult.Ensure | Should -Be 'Present'
$getTargetResourceResult.Name | Should -Be 'ZoneScope'
$getTargetResourceResult.ZoneName | Should -Be 'contoso.com'
$getTargetResourceResult.ZoneFile | Should -Be 'ZoneScope.dns'

Assert-MockCalled -CommandName Get-DnsServerZoneScope -Exactly -Times 1 -Scope It
}
Expand Down

0 comments on commit ec75a94

Please sign in to comment.