From 3e96da395d40c8667702b03a6b8665dc1ee35486 Mon Sep 17 00:00:00 2001 From: Mark Lewis <1800Zeta@users.noreply.github.com> Date: Wed, 25 Sep 2019 20:10:18 +0100 Subject: [PATCH] ADReplicationSubnet: Add Integration Testing, Add Description (#507) - Changes to ADReplicationSubnet - Added 'Description' attribute parameter (issue #503) - Added Integration testing (issue #357) --- CHANGELOG.md | 3 + .../MSFT_ADReplicationSubnet.psm1 | 48 ++- .../MSFT_ADReplicationSubnet.schema.mof | 1 + .../MSFT_ADReplicationSubnet.strings.psd1 | 1 + .../en-US/about_ADReplicationSubnet.help.txt | 11 +- ...nSubnet_CreateReplicationSubnet_Config.ps1 | 7 +- ..._ADReplicationSubnet.Integration.Tests.ps1 | 340 ++++++++++++++++++ .../MSFT_ADReplicationSubnet.config.ps1 | 171 +++++++++ Tests/Unit/MSFT_ADReplicationSubnet.Tests.ps1 | 80 ++++- 9 files changed, 629 insertions(+), 33 deletions(-) create mode 100644 Tests/Integration/MSFT_ADReplicationSubnet.Integration.Tests.ps1 create mode 100644 Tests/Integration/MSFT_ADReplicationSubnet.config.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f38a67bb..f2fa816bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ - Added 'Description' attribute parameter ([issue #500](https://github.com/PowerShell/ActiveDirectoryDsc/issues/500)). - Added Integration testing ([issue #355](https://github.com/PowerShell/ActiveDirectoryDsc/issues/355)). - Correct value returned for RenameDefaultFirstSiteName ([issue #502](https://github.com/PowerShell/ActiveDirectoryDsc/issues/502)). +- Changes to ADReplicationSubnet + - Added 'Description' attribute parameter ([issue #503](https://github.com/PowerShell/ActiveDirectoryDsc/issues/500)) + - Added Integration testing ([issue #357](https://github.com/PowerShell/ActiveDirectoryDsc/issues/357)) ## 4.1.0.0 diff --git a/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.psm1 b/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.psm1 index 25dce1a5b..9584c34e5 100644 --- a/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.psm1 +++ b/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.psm1 @@ -39,7 +39,7 @@ function Get-TargetResource #> Write-Verbose -Message ($script:localizedData.GetReplicationSubnet -f $Name) - $replicationSubnet = Get-ADReplicationSubnet -Filter { Name -eq $Name } + $replicationSubnet = Get-ADReplicationSubnet -Filter { Name -eq $Name } -Properties Description if ($null -eq $replicationSubnet) { @@ -47,10 +47,11 @@ function Get-TargetResource Write-Verbose -Message ($script:localizedData.ReplicationSubnetAbsent -f $Name) $returnValue = @{ - Ensure = 'Absent' - Name = $Name - Site = '' - Location = '' + Ensure = 'Absent' + Name = $Name + Site = '' + Location = $null + Description = $null } } else @@ -68,10 +69,11 @@ function Get-TargetResource Write-Verbose -Message ($script:localizedData.ReplicationSubnetPresent -f $Name) $returnValue = @{ - Ensure = 'Present' - Name = $Name - Site = $replicationSiteName - Location = [System.String] $replicationSubnet.Location + Ensure = 'Present' + Name = $Name + Site = $replicationSiteName + Location = [System.String] $replicationSubnet.Location + Description = [System.String] $replicationSubnet.Description } } @@ -116,7 +118,11 @@ function Set-TargetResource [Parameter()] [System.String] - $Location = '' + $Location = '', + + [Parameter()] + [System.String] + $Description ) <# @@ -169,6 +175,16 @@ function Set-TargetResource Set-ADReplicationSubnet -Identity $replicationSubnet.DistinguishedName -Location $nullableLocation -PassThru } + + if ($PSBoundParameters.ContainsKey('Description')) + { + if ($replicationSubnet.Description -ne $Description) + { + Write-Verbose -Message ($script:localizedData.SetReplicationSubnetDescription -f $Name, $Description) + + Set-ADReplicationSubnet -Identity $replicationSubnet.DistinguishedName -Description $Description + } + } } if ($Ensure -eq 'Absent') @@ -198,6 +214,9 @@ function Set-TargetResource .PARAMETER Location The location for the AD replication site. Default value is empty. + + .PARAMETER Description + Specifies a description of the object. This parameter sets the value of the Description property for the object. #> function Test-TargetResource { @@ -222,7 +241,11 @@ function Test-TargetResource [Parameter()] [System.String] - $Location = '' + $Location = '', + + [Parameter()] + [System.String] + $Description ) $currentConfiguration = Get-TargetResource -Name $Name -Site $Site @@ -233,7 +256,8 @@ function Test-TargetResource { $desiredConfigurationMatch = $desiredConfigurationMatch -and $currentConfiguration.Site -eq $Site -and - $currentConfiguration.Location -eq $Location + $currentConfiguration.Location -eq $Location -and + $currentConfiguration.Description -eq $Description } if ($desiredConfigurationMatch) diff --git a/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.schema.mof b/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.schema.mof index 73aec6d8b..e3535e91d 100644 --- a/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.schema.mof +++ b/DSCResources/MSFT_ADReplicationSubnet/MSFT_ADReplicationSubnet.schema.mof @@ -5,4 +5,5 @@ class MSFT_ADReplicationSubnet : OMI_BaseResource [Key, Description("The name of the Active Directory replication subnet, e.g. 10.0.0.0/24.")] String Name; [Required, Description("The name of the assigned Active Directory replication site, e.g. Default-First-Site-Name.")] String Site; [Write, Description("The location for the Active Directory replication site. Default value is empty ('') location.")] String Location; + [Write, Description("Specifies a description of the object. This parameter sets the value of the Description property for the object.")] String Description; }; diff --git a/DSCResources/MSFT_ADReplicationSubnet/en-US/MSFT_ADReplicationSubnet.strings.psd1 b/DSCResources/MSFT_ADReplicationSubnet/en-US/MSFT_ADReplicationSubnet.strings.psd1 index 06a350971..ea55efb4a 100644 --- a/DSCResources/MSFT_ADReplicationSubnet/en-US/MSFT_ADReplicationSubnet.strings.psd1 +++ b/DSCResources/MSFT_ADReplicationSubnet/en-US/MSFT_ADReplicationSubnet.strings.psd1 @@ -9,4 +9,5 @@ ConvertFrom-StringData @' ReplicationSubnetPresent = Replication subnet '{0}' is present. (ADRS0007) ReplicationSubnetInDesiredState = The replication subnet '{0}' is in the desired state. (ADRS0008) ReplicationSubnetNotInDesiredState = The replication subnet '{0}' is not in the desired state. (ADRS0009) + SetReplicationSubnetDescription = Set the replication subnet '{0}' description to '{1}'. (ADRS0010) '@ diff --git a/DSCResources/MSFT_ADReplicationSubnet/en-US/about_ADReplicationSubnet.help.txt b/DSCResources/MSFT_ADReplicationSubnet/en-US/about_ADReplicationSubnet.help.txt index 204f84423..fd7febf93 100644 --- a/DSCResources/MSFT_ADReplicationSubnet/en-US/about_ADReplicationSubnet.help.txt +++ b/DSCResources/MSFT_ADReplicationSubnet/en-US/about_ADReplicationSubnet.help.txt @@ -25,6 +25,10 @@ Write - String The location for the Active Directory replication site. Default value is empty ('') location. +.PARAMETER Description + Write - String + Specifies a description of the object. This parameter sets the value of the Description property for the object. + .EXAMPLE 1 This configuration will create an AD Replication Subnet. @@ -37,9 +41,10 @@ Configuration ADReplicationSubnet_CreateReplicationSubnet_Config { ADReplicationSubnet 'LondonSubnet' { - Name = '10.0.0.0/24' - Site = 'London' - Location = 'Datacenter 3' + Name = '10.0.0.0/24' + Site = 'London' + Location = 'Datacenter 3' + Description = 'Datacenter Management Subnet' } } } diff --git a/Examples/Resources/ADReplicationSubnet/1-ADReplicationSubnet_CreateReplicationSubnet_Config.ps1 b/Examples/Resources/ADReplicationSubnet/1-ADReplicationSubnet_CreateReplicationSubnet_Config.ps1 index 7f045d5e1..96cd421fb 100644 --- a/Examples/Resources/ADReplicationSubnet/1-ADReplicationSubnet_CreateReplicationSubnet_Config.ps1 +++ b/Examples/Resources/ADReplicationSubnet/1-ADReplicationSubnet_CreateReplicationSubnet_Config.ps1 @@ -29,9 +29,10 @@ Configuration ADReplicationSubnet_CreateReplicationSubnet_Config { ADReplicationSubnet 'LondonSubnet' { - Name = '10.0.0.0/24' - Site = 'London' - Location = 'Datacenter 3' + Name = '10.0.0.0/24' + Site = 'London' + Location = 'Datacenter 3' + Description = 'Datacenter Management Subnet' } } } diff --git a/Tests/Integration/MSFT_ADReplicationSubnet.Integration.Tests.ps1 b/Tests/Integration/MSFT_ADReplicationSubnet.Integration.Tests.ps1 new file mode 100644 index 000000000..086a0e281 --- /dev/null +++ b/Tests/Integration/MSFT_ADReplicationSubnet.Integration.Tests.ps1 @@ -0,0 +1,340 @@ +if ($env:APPVEYOR -eq $true) +{ + Write-Warning -Message 'Integration test is not supported in AppVeyor.' + return +} + +$script:dscModuleName = 'ActiveDirectoryDsc' +$script:dscResourceFriendlyName = 'ADReplicationSubnet' +$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)" + +#region HEADER +# Integration Test Template Version: 1.3.3 +[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests')) +} + +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -TestType Integration +#endregion + +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)_CreatePreReq_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + ConfigurationData = $ConfigurationData + } + + & $configurationName @configurationParameters + + $startDscConfigurationParameters = @{ + Path = $TestDrive + ComputerName = 'localhost' + Wait = $true + Verbose = $true + Force = $true + ErrorAction = 'Stop' + } + + Start-DscConfiguration @startDscConfigurationParameters + } | Should -Not -Throw + } + } + + $configurationName = "$($script:dscResourceName)_CreateSubnet_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + 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.Name | Should -Be '10.0.0.0/24' + $resourceCurrentState.Site | Should -Be 'IntegrationTestSite' + $resourceCurrentState.Location | Should -BeNullOrEmpty + $resourceCurrentState.Description | Should -BeNullOrEmpty + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be 'True' + } + } + + $configurationName = "$($script:dscResourceName)_ChangeSubnetSite_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + 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.Name | Should -Be '10.0.0.0/24' + $resourceCurrentState.Site | Should -Be 'IntegrationTestSite2' + $resourceCurrentState.Location | Should -BeNullOrEmpty + $resourceCurrentState.Description | Should -BeNullOrEmpty + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be 'True' + } + } + + $configurationName = "$($script:dscResourceName)_ChangeSubnetLocation_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + 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.Name | Should -Be '10.0.0.0/24' + $resourceCurrentState.Site | Should -Be 'IntegrationTestSite2' + $resourceCurrentState.Location | Should -Be 'Office 12' + $resourceCurrentState.Description | Should -BeNullOrEmpty + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be 'True' + } + } + + + $configurationName = "$($script:dscResourceName)_ChangeSubnetDescription_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + 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.Name | Should -Be '10.0.0.0/24' + $resourceCurrentState.Site | Should -Be 'IntegrationTestSite2' + $resourceCurrentState.Location | Should -Be 'Office 12' + $resourceCurrentState.Description | Should -Be 'Updated Subnet Description' + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be 'True' + } + } + + $configurationName = "$($script:dscResourceName)_RemoveSubnet_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + 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' + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be 'True' + } + } + + $configurationName = "$($script:dscResourceName)_RemovePreReq_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + ConfigurationData = $ConfigurationData + } + + & $configurationName @configurationParameters + + $startDscConfigurationParameters = @{ + Path = $TestDrive + ComputerName = 'localhost' + Wait = $true + Verbose = $true + Force = $true + ErrorAction = 'Stop' + } + + Start-DscConfiguration @startDscConfigurationParameters + } | Should -Not -Throw + } + } + } +} +finally +{ + #region FOOTER + Restore-TestEnvironment -TestEnvironment $TestEnvironment + #endregion +} diff --git a/Tests/Integration/MSFT_ADReplicationSubnet.config.ps1 b/Tests/Integration/MSFT_ADReplicationSubnet.config.ps1 new file mode 100644 index 000000000..0417e13eb --- /dev/null +++ b/Tests/Integration/MSFT_ADReplicationSubnet.config.ps1 @@ -0,0 +1,171 @@ +#region HEADER +# Integration Test Config Template Version: 1.2.0 +#endregion + +$configFile = [System.IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, 'json') +if (Test-Path -Path $configFile) +{ + <# + Allows reading the configuration data from a JSON file, for real testing + scenarios outside of the CI. + #> + $ConfigurationData = Get-Content -Path $configFile | ConvertFrom-Json +} +else +{ + $ConfigurationData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + CertificateFile = $env:DscPublicCertificatePath + } + ) + } +} + +<# + .SYNOPSIS + Creates a site as part of prerequisits. +#> +Configuration MSFT_ADReplicationSubnet_CreatePreReq_Config +{ + Import-DscResource -ModuleName 'ActiveDirectoryDsc' + + node $AllNodes.NodeName + { + ADReplicationSite 'Site1' + { + Ensure = 'Present' + Name = 'IntegrationTestSite' + } + + ADReplicationSite 'Site2' + { + Ensure = 'Present' + Name = 'IntegrationTestSite2' + } + } +} + +<# + .SYNOPSIS + Creates a site subnet. +#> +Configuration MSFT_ADReplicationSubnet_CreateSubnet_Config +{ + Import-DscResource -ModuleName 'ActiveDirectoryDsc' + + node $AllNodes.NodeName + { + ADReplicationSubnet 'Integration_Test' + { + Ensure = 'Present' + Name = '10.0.0.0/24' + Site = 'IntegrationTestSite' + } + } +} + +<# + .SYNOPSIS + Changes a site subnet Site to Default. +#> +Configuration MSFT_ADReplicationSubnet_ChangeSubnetSite_Config +{ + Import-DscResource -ModuleName 'ActiveDirectoryDsc' + + node $AllNodes.NodeName + { + ADReplicationSubnet 'Integration_Test' + { + Ensure = 'Present' + Name = '10.0.0.0/24' + Site = 'IntegrationTestSite2' + } + } +} + +<# + .SYNOPSIS + Changes a Replication Subnet Location. +#> +Configuration MSFT_ADReplicationSubnet_ChangeSubnetLocation_Config +{ + Import-DscResource -ModuleName 'ActiveDirectoryDsc' + + node $AllNodes.NodeName + { + ADReplicationSubnet 'Integration_Test' + { + Ensure = 'Present' + Name = '10.0.0.0/24' + Site = 'IntegrationTestSite2' + Location = 'Office 12' + } + } +} + +<# + .SYNOPSIS + Changes a Replication Subnet Description. +#> +Configuration MSFT_ADReplicationSubnet_ChangeSubnetDescription_Config +{ + Import-DscResource -ModuleName 'ActiveDirectoryDsc' + + node $AllNodes.NodeName + { + ADReplicationSubnet 'Integration_Test' + { + Ensure = 'Present' + Name = '10.0.0.0/24' + Site = 'IntegrationTestSite2' + Location = 'Office 12' + Description = 'Updated Subnet Description' + } + } +} + +<# + .SYNOPSIS + Removes a site subnet. +#> +Configuration MSFT_ADReplicationSubnet_RemoveSubnet_Config +{ + Import-DscResource -ModuleName 'ActiveDirectoryDsc' + + node $AllNodes.NodeName + { + ADReplicationSubnet 'Integration_Test' + { + Ensure = 'Absent' + Name = '10.0.0.0/24' + Site = 'IntegrationTestSite2' + Location = 'Datacenter 3' + } + } +} + +<# + .SYNOPSIS + Removes the sites as part of prerequisits. +#> +Configuration MSFT_ADReplicationSubnet_RemovePreReq_Config +{ + Import-DscResource -ModuleName 'ActiveDirectoryDsc' + + node $AllNodes.NodeName + { + ADReplicationSite 'Site1' + { + Ensure = 'Absent' + Name = 'IntegrationTestSite' + } + + ADReplicationSite 'Site2' + { + Ensure = 'Absent' + Name = 'IntegrationTestSite2' + } + } +} diff --git a/Tests/Unit/MSFT_ADReplicationSubnet.Tests.ps1 b/Tests/Unit/MSFT_ADReplicationSubnet.Tests.ps1 index dd98ed1ec..0706042a8 100644 --- a/Tests/Unit/MSFT_ADReplicationSubnet.Tests.ps1 +++ b/Tests/Unit/MSFT_ADReplicationSubnet.Tests.ps1 @@ -49,8 +49,8 @@ try #region Function Get-TargetResource Describe 'ADReplicationSubnet\Get-TargetResource' { $testDefaultParameters = @{ - Name = '10.0.0.0/8' - Site = 'Default-First-Site-Name' + Name = '10.0.0.0/8' + Site = 'Default-First-Site-Name' } Context 'Subnet does not exist' { @@ -61,10 +61,11 @@ try $result = Get-TargetResource @testDefaultParameters - $result.Ensure | Should -Be 'Absent' - $result.Name | Should -Be $testDefaultParameters.Name - $result.Site | Should -Be '' - $result.Location | Should -Be '' + $result.Ensure | Should -Be 'Absent' + $result.Name | Should -Be $testDefaultParameters.Name + $result.Site | Should -Be '' + $result.Location | Should -BeNullOrEmpty + $result.Description | Should -BeNullOrEmpty } } @@ -76,6 +77,7 @@ try Name = '10.0.0.0/8' Location = 'Seattle' Site = 'CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=contoso,DC=com' + Description = 'Default First Site Description' } } Mock -CommandName Get-ADObject -MockWith { @@ -90,6 +92,7 @@ try $result.Name | Should -Be $testDefaultParameters.Name $result.Site | Should -Be 'Default-First-Site-Name' $result.Location | Should -Be 'Seattle' + $result.Description | Should -Be 'Default First Site Description' } } @@ -121,9 +124,10 @@ try Describe 'ADReplicationSubnet\Test-TargetResource' { $testDefaultParameters = @{ - Name = '10.0.0.0/8' - Site = 'Default-First-Site-Name' - Location = 'Seattle' + Name = '10.0.0.0/8' + Site = 'Default-First-Site-Name' + Location = 'Seattle' + Description = 'Default First Site Description' } Context 'Subnet does not exist' { @@ -151,6 +155,7 @@ try Name = '10.0.0.0/8' Location = 'Seattle' Site = 'CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=contoso,DC=com' + Description = 'Default First Site Description' } } Mock -CommandName Get-ADObject -MockWith { @@ -171,15 +176,27 @@ try It 'Should return false for wrong site' { - $result = Test-TargetResource -Ensure 'Present' -Name $testDefaultParameters.Name -Site 'WrongSite' -Location $testDefaultParameters.Location + $result = Test-TargetResource -Ensure 'Present' -Name $testDefaultParameters.Name -Site 'WrongSite' -Location $testDefaultParameters.Location -Description $testDefaultParameters.Description $result | Should -BeFalse } It 'Should return false for wrong location' { - $result = Test-TargetResource -Ensure 'Present' -Name $testDefaultParameters.Name -Site $testDefaultParameters.Site -Location 'WringLocation' + $result = Test-TargetResource -Ensure 'Present' -Name $testDefaultParameters.Name -Site $testDefaultParameters.Site -Location 'WringLocation' -Description $testDefaultParameters.Description $result | Should -BeFalse } + + It 'Should return false for wrong Description' { + + $result = Test-TargetResource -Ensure 'Present' -Name $testDefaultParameters.Name -Site $testDefaultParameters.Site -Location $testDefaultParameters.Location -Description 'Test description mismatch' + $result | Should -BeFalse + } + + It 'Should return true for matching Description' { + + $result = Test-TargetResource -Ensure 'Present' -Name $testDefaultParameters.Name -Site $testDefaultParameters.Site -Location $testDefaultParameters.Location -Description $testDefaultParameters.Description + $result | Should -BeTrue + } } } #endregion @@ -187,10 +204,11 @@ try #region Function Set-TargetResource Describe 'ADReplicationSubnet\Set-TargetResource' { $testPresentParameters = @{ - Ensure = 'Present' - Name = '10.0.0.0/8' - Site = 'Default-First-Site-Name' - Location = 'Seattle' + Ensure = 'Present' + Name = '10.0.0.0/8' + Site = 'Default-First-Site-Name' + Location = 'Seattle' + Description = 'Default First Site Description' } $testAbsentParameters = @{ Ensure = 'Absent' @@ -211,6 +229,7 @@ try Name = '10.0.0.0/8' Location = 'Seattle' Site = 'CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=contoso,DC=com' + Description = 'Default First Site Description' } } @@ -232,6 +251,7 @@ try Name = '10.0.0.0/8' Location = 'Seattle' Site = 'CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=contoso,DC=com' + Description = 'Default First Site Description' } } Mock -CommandName Get-ADObject -MockWith { @@ -244,6 +264,7 @@ try Name = '10.0.0.0/8' Location = 'Seattle' Site = 'CN=OtherSite,CN=Sites,CN=Configuration,DC=contoso,DC=com' + Description = 'Default First Site Description' } } Mock -CommandName Set-ADReplicationSubnet -ParameterFilter { $Location -eq 'OtherLocation' } -MockWith { @@ -252,6 +273,17 @@ try Name = '10.0.0.0/8' Location = 'OtherLocation' Site = 'CN=OtherSite,CN=Sites,CN=Configuration,DC=contoso,DC=com' + Description = 'Default First Site Description' + } + } + + Mock -CommandName Set-ADReplicationSubnet -ParameterFilter { $Description -eq 'Test Description' } -MockWith { + [PSCustomObject] @{ + DistinguishedName = 'CN=10.0.0.0/8,CN=Subnets,CN=Sites,CN=Configuration,DC=arcade,DC=local' + Name = '10.0.0.0/8' + Location = 'OtherLocation' + Site = 'CN=OtherSite,CN=Sites,CN=Configuration,DC=contoso,DC=com' + Description = 'Test Description' } } Mock -CommandName Remove-ADReplicationSubnet @@ -274,6 +306,24 @@ try Assert-MockCalled -CommandName Set-ADReplicationSubnet -ParameterFilter { $Location -eq 'OtherLocation' } -Scope It -Times 1 -Exactly } + It 'Should update the subnet location to $null when an empty string is passed' { + + # Act + Set-TargetResource -Ensure $testPresentParameters.Ensure -Name $testPresentParameters.Name -Site $testPresentParameters.Site -Location '' + + # Assert + Assert-MockCalled -CommandName Set-ADReplicationSubnet -ParameterFilter { $Location -eq $null } -Scope It -Times 1 -Exactly + } + + It 'Should update the subnet description' { + + # Act + Set-TargetResource -Ensure $testPresentParameters.Ensure -Name $testPresentParameters.Name -Site $testPresentParameters.Site -Location $testPresentParameters.Location -Description 'Test description fail' + + # Assert + Assert-MockCalled -CommandName Set-ADReplicationSubnet -ParameterFilter { $Description -eq 'Test description fail' } -Scope It -Times 1 -Exactly + } + It 'Should remove the subnet' { # Act