From 948d3be9055e900dea1624ffd2b5c817878f8f4e Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Wed, 24 Jun 2020 18:50:26 -0500 Subject: [PATCH] Updated Per PR comments and Updated Pester --- .../DSC_CMPullDistributionPoint.psm1 | 10 +++ .../DSC_CMPullDistributionPoint.strings.psd1 | 7 +- tests/Unit/CMPullDistributionPoint.tests.ps1 | 69 ++++++++++++++++--- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/source/DSCResources/DSC_CMPullDistributionPoint/DSC_CMPullDistributionPoint.psm1 b/source/DSCResources/DSC_CMPullDistributionPoint/DSC_CMPullDistributionPoint.psm1 index d97f8d4..4a1834a 100644 --- a/source/DSCResources/DSC_CMPullDistributionPoint/DSC_CMPullDistributionPoint.psm1 +++ b/source/DSCResources/DSC_CMPullDistributionPoint/DSC_CMPullDistributionPoint.psm1 @@ -178,6 +178,11 @@ function Set-TargetResource if ($SourceDistributionPoint) { + if ($SourceDistributionPoint.SourceDP.Contains($SiteServerName)) + { + throw ($script:localizedData.SourceDPSiteServer -f $SiteServerName) + } + if ($state.SourceDistributionPoint) { $comparesParam = @{ @@ -294,6 +299,11 @@ function Test-TargetResource if ($SourceDistributionPoint) { + if ($SourceDistributionPoint.SourceDP.Contains($SiteServerName)) + { + Write-Warning -Message ($script:localizedData.SourceDPSiteServer -f $SiteServerName) + } + if ($state.SourceDistributionPoint) { $comparesParam = @{ diff --git a/source/DSCResources/DSC_CMPullDistributionPoint/en-US/DSC_CMPullDistributionPoint.strings.psd1 b/source/DSCResources/DSC_CMPullDistributionPoint/en-US/DSC_CMPullDistributionPoint.strings.psd1 index 1c48ead..a3cdec3 100644 --- a/source/DSCResources/DSC_CMPullDistributionPoint/en-US/DSC_CMPullDistributionPoint.strings.psd1 +++ b/source/DSCResources/DSC_CMPullDistributionPoint/en-US/DSC_CMPullDistributionPoint.strings.psd1 @@ -5,8 +5,9 @@ ConvertFrom-StringData @' SourceDPMatch = MATCH: The settings is present {0} and {1}. SourceDPMissing = NOTMATCH: The setting is missing {0} and {1}. SourceDPExtra = NOTMATCH: The setting is extra {0} and {1}. - InvalidConfig = EnablePullDP is being set to false or is currently false and can not specify a SourceDistributionPoints, set to enable of remove SourceDistributionPoints from the configuration. - PullDPEnabledThrow = When enabling a Pull DP SourceDistributionPoints must be specified. + InvalidConfig = EnablePullDP is being set to false or is currently false and can not specify a SourceDistributionPoint, set to enable of remove SourceDistributionPoint from the configuration. + PullDPEnabledThrow = When enabling a Pull DP SourceDistributionPoint must be specified. EnablePullDP = Setting EnablePullDP to true. - SourceDPMismatch = SourceDistributionPoints do not match setting to desired state. + SourceDPMismatch = SourceDistributionPoint do not match setting to desired state. + SourceDPSiteServer = You can not specify the Pull DP {0} as a SourceDistributionPoint. '@ diff --git a/tests/Unit/CMPullDistributionPoint.tests.ps1 b/tests/Unit/CMPullDistributionPoint.tests.ps1 index 1395bfb..f8d37b6 100644 --- a/tests/Unit/CMPullDistributionPoint.tests.ps1 +++ b/tests/Unit/CMPullDistributionPoint.tests.ps1 @@ -271,9 +271,10 @@ try SourceDistributionPoint = $getSourceDPReturn } - $invalidConfig = 'EnablePullDP is being set to false or is currently false and can not specify a sourcedistribution point, set to enable of remove SourceDistributionPoint from the configuration.' - + $invalidConfig = 'EnablePullDP is being set to false or is currently false and can not specify a SourceDistributionPoint, set to enable of remove SourceDistributionPoint from the configuration.' $dpRoleAbsent = 'The Distribution Point role on DP01.contoso.com is not installed, run DSC_CMDistibutionPoint to install the role.' + $pullDPEnableNoSource = 'When enabling a Pull DP SourceDistributionPoint must be specified.' + $sourceDPSiteServer = 'You can not specify the Pull DP DP01.contoso.com as a SourceDistributionPoint.' $inputInvalid = @{ SiteCode = 'Lab' @@ -281,7 +282,23 @@ try EnablePullDP = $true } - $pullDPEnableNoSource = 'When enabling a Pull DP sourceDistribution Point must be specified.' + $inputMatchSourceAndSite = @( + (New-CimInstance -ClassName DSC_CMPullDistributionPointSourceDP ` + -Namespace root/microsoft/Windows/DesiredStateConfiguration ` + -Property @{ + 'SourceDP' = 'DP01.contoso.com' + 'DPRank' = '1' + } ` + -ClientOnly + ) + ) + + $inputMatchingSourceAndSite = @{ + SiteCode = 'Lab' + SiteServerName = 'DP01.contoso.com' + EnablePullDP = $true + SourceDistributionPoint = $inputMatchSourceAndSite + } } It 'Should throw and call expected commands when distribution point role is not installed' { @@ -313,6 +330,16 @@ try Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Set-CMDistributionPoint -Exactly -Times 0 -Scope It } + + It 'Should throw and call expected commands Source Distribution Points contains SiteServerName' { + Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } + + { Set-TargetResource @inputMatchingSourceAndSite } | Should -Throw -ExpectedMessage $sourceDPSiteServer + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMDistributionPoint -Exactly -Times 0 -Scope It + } } } @@ -405,43 +432,67 @@ try SiteServerName = 'DP01.contoso.com' EnablePullDP = $false } + + $inputMatchSourceAndSite = @( + (New-CimInstance -ClassName DSC_CMPullDistributionPointSourceDP ` + -Namespace root/microsoft/Windows/DesiredStateConfiguration ` + -Property @{ + 'SourceDP' = 'DP01.contoso.com' + 'DPRank' = '1' + } ` + -ClientOnly + ) + ) + + $inputMatchingSourceAndSite = @{ + SiteCode = 'Lab' + SiteServerName = 'DP01.contoso.com' + EnablePullDP = $false + SourceDistributionPoint = $inputMatchSourceAndSite + } } It 'Should return desired result true settings match' { - Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } + Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } Test-TargetResource @inputMatch | Should -Be $true } It 'Should return desired result false when Source DP settings do not match' { - Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } + Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } Test-TargetResource @inputSourceDPMisMatch | Should -Be $false } It 'Should return desired result false when Pull DP is disabled and expected enabled' { - Mock -CommandName Get-TargetResource -MockWith { $pullDPDisabled } + Mock -CommandName Get-TargetResource -MockWith { $pullDPDisabled } Test-TargetResource @inputSourceDPMisMatch | Should -Be $false } It 'Should return desired result false when Pull DP is enabled and expected disabled' { - Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } + Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } Test-TargetResource @inputAbsent | Should -Be $false } It 'Should return desired result false when Distribution Point role is not installed' { - Mock -CommandName Get-TargetResource -MockWith { $dpRoleNotInstalledReturn } + Mock -CommandName Get-TargetResource -MockWith { $dpRoleNotInstalledReturn } Test-TargetResource @inputMatch | Should -Be $false } It 'Should return desired result true when Pull DP is disabled and expected disabled' { - Mock -CommandName Get-TargetResource -MockWith { $pullDPDisabled } + Mock -CommandName Get-TargetResource -MockWith { $pullDPDisabled } Test-TargetResource @inputAbsent | Should -Be $true } + + It 'Should return desired result false when SourceDP does not match get return' { + Mock -CommandName Get-TargetResource -MockWith { $getTargetReturn } + + Test-TargetResource @inputMatchingSourceAndSite | Should -Be $false + } } } }