Skip to content

Commit

Permalink
Merge pull request #64 from dsccommunity/CMDPGroupMembersBugFix
Browse files Browse the repository at this point in the history
BugFix for Adding DP to Groups
  • Loading branch information
jeffotterpohl authored Sep 9, 2020
2 parents 9fcdbe6 + f4a7ca1 commit ddfd66b
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added CMSecurityScopes Resource
- Added CMUserDiscovery Resource
- Added CMSecurityRoles Resource
- Added Add-DPToDPGroup to the ResourceHelper

### Changed

Expand Down Expand Up @@ -80,3 +81,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed Historic Changelog

### Fixed

- Fixed issue when adding a Distribution Point to Distribution Group immediately
after adding the Distribution Point would error saying invalid Distribution Point.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ function Set-TargetResource
}

Write-Verbose -Message ($script:localizedData.AddDistroGroup -f $add, $DistributionPoint)
Add-CMDistributionPointToGroup @addParams
$dpGroupAdd = Add-DPToDPGroup @addParams

if ($dpGroupAdd -eq $false)
{
$errorMsg += ($script:localizedData.GroupAddError -f $DistributionPoint, $add)
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ ConvertFrom-StringData @'
ParamIgnore = DistributionGroups was specifed, ignoring DistributionGroupsToInclude and DistributionGroupsToExclude.
ErrorGroup = Distribution Groups: {0} does not exist.
ErrorBoth = Distribution Group: {0} is a member of the include group and exclude group.
GroupAddError = Unable to add the Distribution Point: {0} to Group: {1}.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'ConvertTo-ScheduleInterval'
'ConvertTo-AnyCimInstance'
'Compare-MultipleCompares'
'Add-DPToDPGroup'
)

# Cmdlets to export from this module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ data LocalizedData
'@
}

$script:dscResourceCommonPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\DscResource.Common'
Import-Module -Name $script:dscResourceCommonPath

$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'

<#
.SYNOPSIS
Import Configuration Manager module commands.
Expand Down Expand Up @@ -520,6 +525,55 @@ function Compare-MultipleCompares
}
}

<#
.SYNOPSIS
Adds the Distribution Point to the Distribution Point Group.
.PARAMETER DistributionPointName
Specifies the Distribution Point to modify Distribution Point Group membership.
.PARAMETER DistributionPointGroupName
Specifies a Distribution Group to add to the Distribution Point.
#>
function Add-DPToDPGroup
{
[OutputType([System.Boolean])]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String]
$DistributionPointName,

[Parameter(Mandatory = $true)]
[String]
$DistributionPointGroupName
)

$count = 0
$success = $false

do
{
try
{
Write-Verbose -Message ($script:localizedData.AddDP -f $DistributionPointName, $DistributionPointGroupName) -Verbose
Add-CMDistributionPointToGroup -DistributionPointName $DistributionPointName -DistributionPointGroupName $DistributionPointGroupName
$success = $true
$count = 12
}
catch
{
Write-Warning -Message ($script:localizedData.Wait -f $DistributionPointName) -Verbose
Start-Sleep -Seconds 10
$count ++
}
}
until ($count -eq 12)

return $success
}

Export-ModuleMember -Function @(
'Import-ConfigMgrPowerShellModule'
'Convert-CidrToIP'
Expand All @@ -530,4 +584,5 @@ Export-ModuleMember -Function @(
'ConvertTo-ScheduleInterval'
'ConvertTo-AnyCimInstance'
'Compare-MultipleCompares'
'Add-DPToDPGroup'
)
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
ConvertFrom-StringData @'
AddDP = Distribution Point Name {0} Distribution Point Group Name: {1}.
Wait = Waiting 10 seconds to for the Distribution Point {0} to fully provision.
'@
37 changes: 28 additions & 9 deletions tests/Unit/CMDistributionPointGroupMembers.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ try

Mock -CommandName Import-ConfigMgrPowerShellModule
Mock -CommandName Set-Location
Mock -CommandName Add-CMDistributionPointToGroup
Mock -CommandName Remove-CMDistributionPointFromGroup
}

Expand All @@ -175,28 +174,31 @@ try
DistributionGroupsToExclude = 'TestGroup1'
}

Mock -CommandName Add-DPToDPGroup -MockWith { $true }
Mock -CommandName Get-CMDistributionPointGroup -MockWith { $true }
}

It 'Should call expected commands when groups match' {
Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent }

Set-TargetResource @groupInputMatch
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 Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It
}

It 'Should call expected commands when groups do not match' {
Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent }

Set-TargetResource @groupInput
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 Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It
}

Expand All @@ -208,7 +210,7 @@ try
Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It
Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It
}

Expand All @@ -220,7 +222,7 @@ try
Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It
Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It
}
}
Expand All @@ -234,45 +236,62 @@ try
DistributionGroupsToExclude = 'TestGroup1'
}

Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null }
$dpGroupAddError = "Unable to add the Distribution Point: DP01.contoso.com to Group: TestGroup2."
Mock -CommandName Add-DPToDPGroup
}

It 'Should call expected commands when DP is absent and throws' {
Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null }
Mock -CommandName Get-TargetResource -MockWith { $dpAbsent }

{ Set-TargetResource @groupInputMatch } | Should -Throw
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 Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It
}

It 'Should call expected commands when Include and Exclude contain same group and throws' {
Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null }
Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent }

{ Set-TargetResource @includeExclude } | Should -Throw
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 Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It
}

It 'Should call expected commands when mismatch and group does not exist' {
Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null }
Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent }

{ Set-TargetResource @groupInput } | Should -Throw
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 Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It
}

It 'Should call expected commands when DP errors while adding to group' {
Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent }
Mock -CommandName Get-CMDistributionPointGroup -MockWith { $true }
Mock -CommandName Add-DPToDPGroup -MockWith { $false }

{ Set-TargetResource @groupInput } | Should -Throw -ExpectedMessage $dpGroupAddError
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 Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Add-DPToDPGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,33 @@ InModuleScope $script:subModuleName {
}
}
}

Describe "ConfigMgrCBDsc - ConfigMgrCBDsc.ResourceHelper\Add-DPToDPGroup" {
BeforeAll {
$inputParam = @{
DistributionPointName = 'DP01.contoso.com'
DistributionPointGroupName = 'TestGroup1'
}

Mock -CommandName Start-Sleep
}

Context 'When return is as expected' {
It 'Should return desired result for when DP is added to the group' {
Mock -CommandName Add-CMDistributionPointToGroup

Add-DPToDPGroup @inputParam | Should -Be $true
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It
Assert-MockCalled Start-Sleep -Exactly -Times 0 -Scope It
}

It 'Should return desired result for when DP is added to the group' {
Mock -CommandName Add-CMDistributionPointToGroup -MockWith { throw }

Add-DPToDPGroup @inputParam | Should -Be $false
Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 12 -Scope It
Assert-MockCalled Start-Sleep -Exactly -Times 12 -Scope It
}
}
}
}

0 comments on commit ddfd66b

Please sign in to comment.