Skip to content

Commit d769405

Browse files
authored
Merge pull request #713 from Gijsreyn/fix-keyvalue-pair
Add validation to check key-value pairs for class-based and script-based DSC resources
2 parents 33f286d + d020666 commit d769405

File tree

5 files changed

+237
-209
lines changed

5 files changed

+237
-209
lines changed

Diff for: powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class TestClassResource : BaseTestClass
2323
[DscProperty()]
2424
[string] $Prop1
2525

26+
[DscProperty()]
27+
[hashtable] $HashTableProp
28+
2629
[DscProperty()]
2730
[string] $EnumProp
2831

Diff for: powershell-adapter/Tests/powershellgroup.config.tests.ps1

+117-97
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,38 @@
33

44
Describe 'PowerShell adapter resource tests' {
55

6-
BeforeAll {
7-
$OldPSModulePath = $env:PSModulePath
8-
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
9-
$pwshConfigPath = Join-path $PSScriptRoot "class_ps_resources.dsc.yaml"
10-
11-
if ($IsLinux -or $IsMacOS) {
12-
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
13-
}
14-
else
15-
{
16-
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
17-
}
6+
BeforeAll {
7+
$OldPSModulePath = $env:PSModulePath
8+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
9+
$pwshConfigPath = Join-path $PSScriptRoot "class_ps_resources.dsc.yaml"
10+
11+
if ($IsLinux -or $IsMacOS) {
12+
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
1813
}
19-
AfterAll {
20-
$env:PSModulePath = $OldPSModulePath
14+
else {
15+
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
2116
}
17+
}
18+
AfterAll {
19+
$env:PSModulePath = $OldPSModulePath
20+
}
2221

23-
BeforeEach {
24-
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
25-
}
22+
BeforeEach {
23+
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
24+
}
2625

27-
It 'Get works on config with class-based resources' {
26+
It 'Get works on config with class-based resources' {
2827

29-
$r = Get-Content -Raw $pwshConfigPath | dsc config get -f -
30-
$LASTEXITCODE | Should -Be 0
31-
$res = $r | ConvertFrom-Json
32-
$res.results[0].result.actualState.result[0].properties.Prop1 | Should -BeExactly 'ValueForProp1'
33-
$res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected'
34-
}
28+
$r = Get-Content -Raw $pwshConfigPath | dsc config get -f -
29+
$LASTEXITCODE | Should -Be 0
30+
$res = $r | ConvertFrom-Json
31+
$res.results[0].result.actualState.result[0].properties.Prop1 | Should -BeExactly 'ValueForProp1'
32+
$res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected'
33+
}
3534

36-
It 'Get does not work on config when module does not exist' {
35+
It 'Get does not work on config when module does not exist' {
3736

38-
$yaml = @'
37+
$yaml = @'
3938
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
4039
resources:
4140
- name: Working with class-based resources
@@ -45,30 +44,30 @@ Describe 'PowerShell adapter resource tests' {
4544
- name: Class-resource Info
4645
type: TestClassResourceNotExist/TestClassResourceNotExist
4746
'@
48-
$yaml | dsc -l trace config get -f - 2> "$TestDrive/tracing.txt"
49-
$LASTEXITCODE | Should -Be 2
50-
"$TestDrive/tracing.txt" | Should -FileContentMatch 'DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.'
51-
}
47+
$yaml | dsc -l trace config get -f - 2> "$TestDrive/tracing.txt"
48+
$LASTEXITCODE | Should -Be 2
49+
"$TestDrive/tracing.txt" | Should -FileContentMatch 'DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.'
50+
}
5251

53-
It 'Test works on config with class-based resources' {
52+
It 'Test works on config with class-based resources' {
5453

55-
$r = Get-Content -Raw $pwshConfigPath | dsc config test -f -
56-
$LASTEXITCODE | Should -Be 0
57-
$res = $r | ConvertFrom-Json
58-
$res.results[0].result.actualState.result[0] | Should -Not -BeNull
59-
}
54+
$r = Get-Content -Raw $pwshConfigPath | dsc config test -f -
55+
$LASTEXITCODE | Should -Be 0
56+
$res = $r | ConvertFrom-Json
57+
$res.results[0].result.actualState.result[0] | Should -Not -BeNull
58+
}
6059

61-
It 'Set works on config with class-based resources' {
60+
It 'Set works on config with class-based resources' {
6261

63-
$r = Get-Content -Raw $pwshConfigPath | dsc config set -f -
64-
$LASTEXITCODE | Should -Be 0
65-
$res = $r | ConvertFrom-Json
66-
$res.results.result.afterState.result[0].type | Should -Be "TestClassResource/TestClassResource"
67-
}
62+
$r = Get-Content -Raw $pwshConfigPath | dsc config set -f -
63+
$LASTEXITCODE | Should -Be 0
64+
$res = $r | ConvertFrom-Json
65+
$res.results.result.afterState.result[0].type | Should -Be "TestClassResource/TestClassResource"
66+
}
6867

69-
It 'Export works on config with class-based resources' {
68+
It 'Export works on config with class-based resources' {
7069

71-
$yaml = @'
70+
$yaml = @'
7271
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
7372
resources:
7473
- name: Working with class-based resources
@@ -78,18 +77,18 @@ Describe 'PowerShell adapter resource tests' {
7877
- name: Class-resource Info
7978
type: TestClassResource/TestClassResource
8079
'@
81-
$out = $yaml | dsc config export -f -
82-
$LASTEXITCODE | Should -Be 0
83-
$res = $out | ConvertFrom-Json
84-
$res.'$schema' | Should -BeExactly 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
85-
$res.'resources' | Should -Not -BeNullOrEmpty
86-
$res.resources[0].properties.result.count | Should -Be 5
87-
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
88-
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
89-
}
90-
91-
It 'Export fails when class-based resource does not implement' {
92-
$yaml = @'
80+
$out = $yaml | dsc config export -f -
81+
$LASTEXITCODE | Should -Be 0
82+
$res = $out | ConvertFrom-Json
83+
$res.'$schema' | Should -BeExactly 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
84+
$res.'resources' | Should -Not -BeNullOrEmpty
85+
$res.resources[0].properties.result.count | Should -Be 5
86+
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
87+
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
88+
}
89+
90+
It 'Export fails when class-based resource does not implement' {
91+
$yaml = @'
9392
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
9493
resources:
9594
- name: Working with class-based resources
@@ -99,21 +98,21 @@ Describe 'PowerShell adapter resource tests' {
9998
- name: Class-resource Info
10099
type: TestClassResource/NoExport
101100
'@
102-
$out = $yaml | dsc config export -f - 2>&1 | Out-String
103-
$LASTEXITCODE | Should -Be 2
104-
$out | Should -Not -BeNullOrEmpty
105-
$out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
106-
}
101+
$out = $yaml | dsc config export -f - 2>&1 | Out-String
102+
$LASTEXITCODE | Should -Be 2
103+
$out | Should -Not -BeNullOrEmpty
104+
$out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
105+
}
107106

108-
It 'Custom psmodulepath in config works' {
107+
It 'Custom psmodulepath in config works' {
109108

110-
$OldPSModulePath = $env:PSModulePath
111-
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
112-
Rename-Item -Path "$PSScriptRoot/TestClassResource" -NewName "_TestClassResource"
109+
$OldPSModulePath = $env:PSModulePath
110+
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
111+
Rename-Item -Path "$PSScriptRoot/TestClassResource" -NewName "_TestClassResource"
113112

114-
try {
115-
$psmp = "`$env:PSModulePath"+[System.IO.Path]::PathSeparator+$TestDrive
116-
$yaml = @"
113+
try {
114+
$psmp = "`$env:PSModulePath" + [System.IO.Path]::PathSeparator + $TestDrive
115+
$yaml = @"
117116
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
118117
resources:
119118
- name: Working with class-based resources
@@ -124,24 +123,24 @@ Describe 'PowerShell adapter resource tests' {
124123
- name: Class-resource Info
125124
type: TestClassResource/TestClassResource
126125
"@
127-
$out = $yaml | dsc config export -f -
128-
$LASTEXITCODE | Should -Be 0
129-
$res = $out | ConvertFrom-Json
130-
$res.'$schema' | Should -BeExactly 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
131-
$res.'resources' | Should -Not -BeNullOrEmpty
132-
$res.resources[0].properties.result.count | Should -Be 5
133-
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
134-
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
135-
}
136-
finally {
137-
Rename-Item -Path "$PSScriptRoot/_TestClassResource" -NewName "TestClassResource"
138-
$env:PSModulePath = $OldPSModulePath
139-
}
126+
$out = $yaml | dsc config export -f -
127+
$LASTEXITCODE | Should -Be 0
128+
$res = $out | ConvertFrom-Json
129+
$res.'$schema' | Should -BeExactly 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
130+
$res.'resources' | Should -Not -BeNullOrEmpty
131+
$res.resources[0].properties.result.count | Should -Be 5
132+
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
133+
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
140134
}
135+
finally {
136+
Rename-Item -Path "$PSScriptRoot/_TestClassResource" -NewName "TestClassResource"
137+
$env:PSModulePath = $OldPSModulePath
138+
}
139+
}
141140

142-
It 'DSCConfigRoot macro is working when config is from a file' {
141+
It 'DSCConfigRoot macro is working when config is from a file' {
143142

144-
$yaml = @"
143+
$yaml = @"
145144
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
146145
resources:
147146
- name: Working with class-based resources
@@ -154,19 +153,19 @@ Describe 'PowerShell adapter resource tests' {
154153
Name: "[envvar('DSC_CONFIG_ROOT')]"
155154
"@
156155

157-
$config_path = "$TestDrive/test_config.dsc.yaml"
158-
$yaml | Set-Content -Path $config_path
156+
$config_path = "$TestDrive/test_config.dsc.yaml"
157+
$yaml | Set-Content -Path $config_path
159158

160-
$out = dsc config get --file $config_path
161-
$LASTEXITCODE | Should -Be 0
162-
$res = $out | ConvertFrom-Json
163-
$res.results.result.actualState.result.properties.Name | Should -Be $TestDrive
164-
$res.results.result.actualState.result.properties.Prop1 | Should -Be $TestDrive
165-
}
159+
$out = dsc config get --file $config_path
160+
$LASTEXITCODE | Should -Be 0
161+
$res = $out | ConvertFrom-Json
162+
$res.results.result.actualState.result.properties.Name | Should -Be $TestDrive
163+
$res.results.result.actualState.result.properties.Prop1 | Should -Be $TestDrive
164+
}
166165

167-
It 'DSC_CONFIG_ROOT env var is cwd when config is piped from stdin' {
166+
It 'DSC_CONFIG_ROOT env var is cwd when config is piped from stdin' {
168167

169-
$yaml = @"
168+
$yaml = @"
170169
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
171170
resources:
172171
- name: Working with class-based resources
@@ -178,8 +177,29 @@ Describe 'PowerShell adapter resource tests' {
178177
properties:
179178
Name: "[envvar('DSC_CONFIG_ROOT')]"
180179
"@
181-
$out = $yaml | dsc config get -f - | ConvertFrom-Json
182-
$LASTEXITCODE | Should -Be 0
183-
$out.results[0].result.actualState.result[0].properties.Name | Should -BeExactly (Get-Location).Path
184-
}
180+
$out = $yaml | dsc config get -f - | ConvertFrom-Json
181+
$LASTEXITCODE | Should -Be 0
182+
$out.results[0].result.actualState.result[0].properties.Name | Should -BeExactly (Get-Location).Path
183+
}
184+
185+
It 'DSC Configuration Document with key-value pair works' {
186+
$yaml = @"
187+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
188+
resources:
189+
- name: Working with class-based resources
190+
type: Microsoft.DSC/PowerShell
191+
properties:
192+
resources:
193+
- name: Class-resource Info
194+
type: TestClassResource/TestClassResource
195+
properties:
196+
Name: 'TestClassResource1'
197+
HashTableProp:
198+
Name: 'DSCv3'
199+
"@
200+
201+
$out = $yaml | dsc config get -f - | ConvertFrom-Json
202+
$LASTEXITCODE | Should -Be 0
203+
$out.results.result.actualState.result.properties.HashTableProp.Name | Should -BeExactly 'DSCv3'
204+
}
185205
}

0 commit comments

Comments
 (0)