Skip to content

Commit 4856433

Browse files
authored
WSManDsc: Convert Tests to Pester 5 (#105)
1 parent 6f4163d commit 4856433

16 files changed

+2333
-1640
lines changed

CHANGELOG.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8-
### Changed
9-
10-
- CI Pipeline
11-
- Updated pipeline files to match current DSC Community patterns - fixes [Issue #103](https://github.com/dsccommunity/WSManDsc/issues/103).
12-
- Updated HQRM and build steps to use windows-latest image.
13-
- WSManDsc
14-
- Added support for changing the hostname and/or certificate thumbprint on the listener - fixes [Issue #23](https://github.com/dsccommunity/WSManDsc/issues/23).
15-
168
### Added
179

1810
- Added build task `Generate_Conceptual_Help` to generate conceptual help
@@ -55,6 +47,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5547
images - Fixes [Issue #96](https://github.com/dsccommunity/WSManDsc/issues/96).
5648
- Updated pipeline unit tests and integration tests to use Windows Server 2019 and
5749
Windows Server 2022 images - Fixes [Issue #96](https://github.com/dsccommunity/WSManDsc/issues/96).
50+
- CI Pipeline
51+
- Updated pipeline files to match current DSC Community patterns - fixes [Issue #103](https://github.com/dsccommunity/WSManDsc/issues/103).
52+
- Updated HQRM and build steps to use windows-latest image.
53+
- Pin gitversion to V5.
54+
- WSManDsc
55+
- Added support for changing the hostname and/or certificate thumbprint on the listener fixes [Issue #23](https://github.com/dsccommunity/WSManDsc/issues/23).
56+
- Converted tests to Pester 5 - fixes [#99](https://github.com/dsccommunity/WSManDsc/issues/99).
57+
- `DSC_WSManConfig`
58+
- Refactor `Test-TargetResource` to use `Test-DscParameterState`.
59+
- Remove unused strings.
60+
- `DSC_WSManServiceConfig`
61+
- Refactor `Test-TargetResource` to use `Test-DscParameterState`.
62+
- Remove unused strings
5863

5964
### Fixed
6065

RequiredModules.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
InvokeBuild = 'latest'
1111
PSScriptAnalyzer = 'latest'
12-
Pester = '4.10.1'
12+
Pester = 'latest'
1313
Plaster = 'latest'
1414
ModuleBuilder = 'latest'
1515
ChangelogManagement = 'latest'

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ stages:
2727
vmImage: 'windows-latest'
2828
steps:
2929
- pwsh: |
30-
dotnet tool install --global GitVersion.Tool
30+
dotnet tool install --global GitVersion.Tool --version 5.*
3131
$gitVersionObject = dotnet-gitversion | ConvertFrom-Json
3232
$gitVersionObject.PSObject.Properties.ForEach{
3333
Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."

build.yaml

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ BuildWorkflow:
2626
- package_module_nupkg
2727

2828
hqrmtest:
29-
- DscResource_Tests_Stop_On_Fail
29+
- Invoke_HQRM_Tests_Stop_On_Fail
3030

3131
test:
3232
- Pester_Tests_Stop_On_Fail
33+
- Convert_Pester_Coverage
3334
- Pester_If_Code_Coverage_Under_Threshold
3435

3536
publish:
36-
- publish_module_to_gallery
3737
- Publish_Release_To_GitHub
38+
- publish_module_to_gallery
3839
- Publish_GitHub_Wiki_Content
39-
- Create_ChangeLog_GitHub_PR
4040

4141
####################################################
4242
# ModuleBuilder Configuration #
@@ -86,16 +86,6 @@ NestedModule:
8686
####################################################
8787

8888
Pester:
89-
# Pester 4 configuration
90-
Script:
91-
- tests/Unit
92-
ExcludeTag:
93-
Tag:
94-
CodeCoverageOutputFile: JaCoCo_coverage.xml
95-
CodeCoverageOutputFileEncoding: ascii
96-
CodeCoverageThreshold: 85
97-
98-
# Pester 5 configuration
9989
Configuration:
10090
Run:
10191
Path:
@@ -105,7 +95,7 @@ Pester:
10595
StackTraceVerbosity: Full
10696
CIFormat: Auto
10797
CodeCoverage:
108-
CoveragePercentTarget: 80
98+
CoveragePercentTarget: 85
10999
OutputPath: JaCoCo_coverage.xml
110100
OutputEncoding: ascii
111101
UseBreakpoints: false
@@ -114,19 +104,36 @@ Pester:
114104
OutputEncoding: ascii
115105
ExcludeFromCodeCoverage:
116106
- Modules/DscResource.Common
107+
- Modules/DscResource.Base
108+
- prefix.ps1
109+
- WSManDsc.psm1
117110

118111
####################################################
119112
# Pester Configuration (DscResource.Test) #
120113
####################################################
121114
DscTest:
122-
OutputFormat: NUnitXML
123-
ExcludeTag:
124-
- 'Common Tests - New Error-Level Script Analyzer Rules'
125-
ExcludeSourceFile:
126-
- output
127-
ExcludeModuleFile:
128-
- Modules/DscResource.Common
129-
MainGitBranch: main
115+
Pester:
116+
Configuration:
117+
Filter:
118+
ExcludeTag:
119+
- "Common Tests - New Error-Level Script Analyzer Rules"
120+
Output:
121+
Verbosity: Detailed
122+
CIFormat: Auto
123+
TestResult:
124+
Enabled: true
125+
OutputFormat: NUnitXML
126+
OutputEncoding: ascii
127+
OutputPath: ./output/testResults/NUnitXml_HQRM_Tests.xml
128+
Script:
129+
ExcludeSourceFile:
130+
- output
131+
ExcludeModuleFile:
132+
- Modules/DscResource.Common
133+
- Modules/DscResource.Base
134+
# Must exclude built module file because it should not be tested like MOF-based resources
135+
- WSManDsc.psm1
136+
MainGitBranch: main
130137

131138
####################################################
132139
# PSDepend Configuration #
@@ -173,5 +180,5 @@ DscResource.DocGenerator:
173180
Type: CompositeResource
174181
Category: Resources
175182
Generate_Wiki_Sidebar:
176-
Debug: false
177-
AlwaysOverwrite: true
183+
Debug: false
184+
AlwaysOverwrite: true

source/DSCResources/DSC_WSManConfig/DSC_WSManConfig.psm1

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ function Set-TargetResource
8888
$IsSingleInstance,
8989

9090
[Parameter()]
91-
[ValidateRange(32,4294967295)]
91+
[ValidateRange(32, 4294967295)]
9292
[System.Uint32]
9393
$MaxEnvelopeSizekb,
9494

9595
[Parameter()]
96-
[ValidateRange(500,4294967295)]
96+
[ValidateRange(500, 4294967295)]
9797
[System.Uint32]
9898
$MaxTimeoutms,
9999

100100
[Parameter()]
101-
[ValidateRange(1,4294967295)]
101+
[ValidateRange(1, 4294967295)]
102102
[System.Uint32]
103103
$MaxBatchItems
104104
)
@@ -119,14 +119,14 @@ function Set-TargetResource
119119
$parameterNew = (Get-Variable -Name $parameter.Name).Value
120120

121121
if ($PSBoundParameters.ContainsKey($parameter.Name) `
122-
-and ($parameterCurrent -ne $parameterNew))
122+
-and ($parameterCurrent -ne $parameterNew))
123123
{
124124
Set-Item -Path $parameterPath -Value $parameterNew -Force
125125

126126
Write-Verbose -Message ( @(
127-
"$($MyInvocation.MyCommand): "
128-
$($script:localizedData.WSManConfigUpdateParameterMessage) `
129-
-f $parameter.Name,$parameterCurrent,$parameterNew
127+
"$($MyInvocation.MyCommand): "
128+
$($script:localizedData.WSManConfigUpdateParameterMessage) `
129+
-f $parameter.Name, $parameterCurrent, $parameterNew
130130
) -join '' )
131131
} # if
132132
} # foreach
@@ -160,17 +160,17 @@ function Test-TargetResource
160160
$IsSingleInstance,
161161

162162
[Parameter()]
163-
[ValidateRange(32,4294967295)]
163+
[ValidateRange(32, 4294967295)]
164164
[System.Uint32]
165165
$MaxEnvelopeSizekb,
166166

167167
[Parameter()]
168-
[ValidateRange(500,4294967295)]
168+
[ValidateRange(500, 4294967295)]
169169
[System.Uint32]
170170
$MaxTimeoutms,
171171

172172
[Parameter()]
173-
[ValidateRange(1,4294967295)]
173+
[ValidateRange(1, 4294967295)]
174174
[System.Uint32]
175175
$MaxBatchItems
176176
)
@@ -180,33 +180,16 @@ function Test-TargetResource
180180
$($script:localizedData.TestingWSManConfigMessage)
181181
) -join '' )
182182

183-
# Flag to signal whether settings are correct
184-
$desiredConfigurationMatch = $true
183+
$currentSettings = Get-TargetResource `
184+
-IsSingleInstance $IsSingleInstance `
185+
-Verbose:$VerbosePreference
185186

186-
# Check each parameter
187-
foreach ($parameter in $script:parameterList)
188-
{
189-
$parameterPath = Join-Path `
190-
-Path 'WSMan:\Localhost\' `
191-
-ChildPath $parameter.Path
192-
193-
$parameterCurrent = (Get-Item -Path $parameterPath).Value
194-
$parameterNew = (Get-Variable -Name $parameter.Name).Value
195-
196-
if ($PSBoundParameters.ContainsKey($parameter.Name) `
197-
-and ($parameterCurrent -ne $parameterNew))
198-
{
199-
Write-Verbose -Message ( @(
200-
"$($MyInvocation.MyCommand): "
201-
$($script:localizedData.WSManConfigParameterNeedsUpdateMessage) `
202-
-f $parameter.Name,$parameterCurrent,$parameterNew
203-
) -join '' )
204-
205-
$desiredConfigurationMatch = $false
206-
} # if
207-
} # foreach
208-
209-
return $desiredConfigurationMatch
187+
return Test-DscParameterState `
188+
-CurrentValues $currentSettings `
189+
-DesiredValues $PSBoundParameters `
190+
-TurnOffTypeChecking `
191+
-ExcludeProperties @('IsSingleInstance') `
192+
-Verbose:$VerbosePreference
210193
} # Test-TargetResource
211194

212195
Export-ModuleMember -Function *-TargetResource

source/DSCResources/DSC_WSManConfig/en-US/DSC_WSManConfig.strings.psd1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ ConvertFrom-StringData @'
33
SettingWSManConfigMessage = Setting WS-Man Config.
44
WSManConfigUpdateParameterMessage = Updating WS-Man Config parameter {0} from "{1}" to "{2}".
55
TestingWSManConfigMessage = Testing WS-Man Config.
6-
WSManConfigParameterNeedsUpdateMessage = WS-Man Config Setting "{0}" is "{1}" but should be "{2}". Change required.
76
'@

source/DSCResources/DSC_WSManListener/DSC_WSManListener.psm1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ function Get-TargetResource
5151

5252
if ($listener)
5353
{
54+
$certificate = ''
55+
5456
# An existing listener matching the transport was found
5557
Write-Verbose -Message ( @(
5658
"$($MyInvocation.MyCommand): "
@@ -647,8 +649,6 @@ function Find-Certificate
647649
$Hostname
648650
)
649651

650-
[System.String] $thumbprint = ''
651-
652652
if ($PSBoundParameters.ContainsKey('CertificateThumbprint'))
653653
{
654654
Write-Verbose -Message ( @(
@@ -659,7 +659,7 @@ function Find-Certificate
659659

660660
$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
661661
($_.Thumbprint -eq $CertificateThumbprint)
662-
} | Select-Object -First 1
662+
} | Select-Object -First 1
663663
}
664664
else
665665
{
@@ -689,7 +689,7 @@ function Find-Certificate
689689

690690
$certificate = (Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
691691
($_.Extensions.EnhancedKeyUsages.FriendlyName `
692-
-contains 'Server Authentication') -and
692+
-contains 'Server Authentication') -and
693693
($_.Issuer -eq $Issuer) -and
694694
($Hostname -in $_.DNSNameList.Unicode) -and
695695
($_.Subject -eq $Subject)
@@ -706,10 +706,10 @@ function Find-Certificate
706706

707707
$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
708708
($_.Extensions.EnhancedKeyUsages.FriendlyName `
709-
-contains 'Server Authentication') -and
709+
-contains 'Server Authentication') -and
710710
($_.Issuer -eq $Issuer) -and
711711
($_.Subject -eq $Subject)
712-
} | Select-Object -First 1
712+
} | Select-Object -First 1
713713
} # if
714714
}
715715

@@ -736,11 +736,11 @@ function Find-Certificate
736736

737737
$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
738738
($_.Extensions.EnhancedKeyUsages.FriendlyName `
739-
-contains 'Server Authentication') -and
739+
-contains 'Server Authentication') -and
740740
($_.Issuer -eq $Issuer) -and
741741
($Hostname -in $_.DNSNameList.Unicode) -and
742742
($_.Subject -eq $Subject)
743-
} | Select-Object -First 1
743+
} | Select-Object -First 1
744744
}
745745
else
746746
{
@@ -753,10 +753,10 @@ function Find-Certificate
753753

754754
$certificate = Get-ChildItem -Path Cert:\localmachine\my | Where-Object -FilterScript {
755755
($_.Extensions.EnhancedKeyUsages.FriendlyName `
756-
-contains 'Server Authentication') -and
756+
-contains 'Server Authentication') -and
757757
($_.Issuer -eq $Issuer) -and
758758
($_.Subject -eq $Subject)
759-
} | Select-Object -First 1
759+
} | Select-Object -First 1
760760
} # if
761761
} # if
762762
} # if

source/DSCResources/DSC_WSManServiceConfig/DSC_WSManServiceConfig.psm1

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function Set-TargetResource
202202
Write-Verbose -Message ( @(
203203
"$($MyInvocation.MyCommand): "
204204
$($script:localizedData.WSManServiceConfigUpdateParameterMessage) `
205-
-f $parameter.Name,$parameterCurrent,$parameterNew
205+
-f $parameter.Name, $parameterCurrent, $parameterNew
206206
) -join '' )
207207
} # if
208208
} # foreach
@@ -332,33 +332,16 @@ function Test-TargetResource
332332
$($script:localizedData.TestingWSManServiceConfigMessage)
333333
) -join '' )
334334

335-
# Flag to signal whether settings are correct
336-
$desiredConfigurationMatch = $true
335+
$currentSettings = Get-TargetResource `
336+
-IsSingleInstance $IsSingleInstance `
337+
-Verbose:$VerbosePreference
337338

338-
# Check each parameter
339-
foreach ($parameter in $script:parameterList)
340-
{
341-
$parameterPath = Join-Path `
342-
-Path 'WSMan:\Localhost\Service\' `
343-
-ChildPath $parameter.Path
344-
345-
$parameterCurrent = (Get-Item -Path $parameterPath).Value
346-
$parameterNew = (Get-Variable -Name $parameter.Name).Value
347-
348-
if ($PSBoundParameters.ContainsKey($parameter.Name) `
349-
-and ($parameterCurrent -ne $parameterNew))
350-
{
351-
Write-Verbose -Message ( @(
352-
"$($MyInvocation.MyCommand): "
353-
$($script:localizedData.WSManServiceConfigParameterNeedsUpdateMessage) `
354-
-f $parameter.Name,$parameterCurrent,$parameterNew
355-
) -join '' )
356-
357-
$desiredConfigurationMatch = $false
358-
} # if
359-
} # foreach
360-
361-
return $desiredConfigurationMatch
339+
return Test-DscParameterState `
340+
-CurrentValues $currentSettings `
341+
-DesiredValues $PSBoundParameters `
342+
-TurnOffTypeChecking `
343+
-ExcludeProperties @('IsSingleInstance') `
344+
-Verbose:$VerbosePreference
362345
} # Test-TargetResource
363346

364347
Export-ModuleMember -Function *-TargetResource

source/DSCResources/DSC_WSManServiceConfig/en-US/DSC_WSManServiceConfig.strings.psd1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ ConvertFrom-StringData @'
33
SettingWSManServiceConfigMessage = Setting WS-Man Service Config.
44
WSManServiceConfigUpdateParameterMessage = Updating WS-Man Service Config parameter {0} from "{1}" to "{2}".
55
TestingWSManServiceConfigMessage = Testing WS-Man Service Config.
6-
WSManServiceConfigParameterNeedsUpdateMessage = WS-Man Service Config Setting "{0}" is "{1}" but should be "{2}". Change required.
76
'@

0 commit comments

Comments
 (0)