-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathpowershellgroup.config.tests.ps1
More file actions
386 lines (347 loc) · 15 KB
/
powershellgroup.config.tests.ps1
File metadata and controls
386 lines (347 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'PowerShell adapter resource tests' {
BeforeAll {
$OldPSModulePath = $env:PSModulePath
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
$pwshConfigPath = Join-path $PSScriptRoot "class_ps_resources.dsc.yaml"
if ($IsLinux -or $IsMacOS) {
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
else {
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
}
}
AfterAll {
$env:PSModulePath = $OldPSModulePath
}
BeforeEach {
Remove-Item -Force -ErrorAction Ignore -Path $cacheFilePath
}
It 'Get works on config with class-based resources' {
$r = Get-Content -Raw $pwshConfigPath | dsc config get -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0].properties.Prop1 | Should -BeExactly 'ValueForProp1'
$res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected'
}
It 'Get does not work on config when module does not exist' {
$yaml = @'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResourceNotExist/TestClassResourceNotExist
'@
$yaml | dsc -l trace config get -f - 2> "$TestDrive/tracing.txt"
$LASTEXITCODE | Should -Be 2
"$TestDrive/tracing.txt" | Should -FileContentMatch "DSC resource 'TestClassResourceNotExist/TestClassResourceNotExist' module not found."
}
It 'Test works on config with class-based resources' {
$r = Get-Content -Raw $pwshConfigPath | dsc config test -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState.result[0] | Should -Not -BeNull
}
It 'Set works on config with class-based resources' {
$r = Get-Content -Raw $pwshConfigPath | dsc config set -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results.result.afterState.result[0].type | Should -Be "TestClassResource/TestClassResource"
}
It 'Export works on config with class-based resources' {
$yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
'@
$out = $yaml | dsc config export -f -
$LASTEXITCODE | Should -Be 0
$res = $out | ConvertFrom-Json
$res.'$schema' | Should -BeExactly 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
$res.'resources' | Should -Not -BeNullOrEmpty
$res.resources[0].properties.result.count | Should -Be 5
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}
It 'Export fails when class-based resource does not implement' {
$yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/NoExport
'@
$out = $yaml | dsc config export -f - 2>&1 | Out-String
$LASTEXITCODE | Should -Be 2
$out | Should -Not -BeNullOrEmpty
$out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
}
It 'Export works with filtered export property' {
$yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/FilteredExport
properties:
Name: 'FilteredExport'
'@
$out = $yaml | dsc -l trace config export -f - 2> "$TestDrive/export_trace.txt"
$LASTEXITCODE | Should -Be 0
$res = $out | ConvertFrom-Json
$res.'$schema' | Should -BeExactly 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
$res.'resources' | Should -Not -BeNullOrEmpty
$res.resources[0].properties.result.count | Should -Be 1
$res.resources[0].properties.result[0].Name | Should -Be "FilteredExport"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Filtered Property for FilteredExport"
"$TestDrive/export_trace.txt" | Should -FileContentMatch "Properties provided for filtered export"
}
It 'Export fails when filtered export is requested but not implemented' {
$yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/NoExport
properties:
Name: 'SomeFilter'
'@
$out = $yaml | dsc config export -f - 2>&1 | Out-String
$LASTEXITCODE | Should -Be 2
$out | Should -Not -BeNullOrEmpty
$out | Should -BeLike "*ERROR*Export method with parameters not implemented by resource 'TestClassResource/NoExport'*"
}
It 'Custom psmodulepath in config works' {
$OldPSModulePath = $env:PSModulePath
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
Rename-Item -Path "$PSScriptRoot/TestClassResource" -NewName "_TestClassResource"
try {
$psmp = "`$env:PSModulePath" + [System.IO.Path]::PathSeparator + $TestDrive
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
psmodulepath: $psmp
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
"@
$out = $yaml | dsc config export -f -
$LASTEXITCODE | Should -Be 0
$res = $out | ConvertFrom-Json
$res.'$schema' | Should -BeExactly 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
$res.'resources' | Should -Not -BeNullOrEmpty
$res.resources[0].properties.result.count | Should -Be 5
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}
finally {
Rename-Item -Path "$PSScriptRoot/_TestClassResource" -NewName "TestClassResource"
$env:PSModulePath = $OldPSModulePath
}
}
It 'DSCConfigRoot macro is working when config is from a file' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Name: "[envvar('DSC_CONFIG_ROOT')]"
"@
$config_path = "$TestDrive/test_config.dsc.yaml"
$yaml | Set-Content -Path $config_path
$out = dsc config get --file $config_path
$LASTEXITCODE | Should -Be 0
$res = $out | ConvertFrom-Json
$res.results.result.actualState.result.properties.Name | Should -Be $TestDrive
$res.results.result.actualState.result.properties.Prop1 | Should -Be $TestDrive
}
It 'DSC_CONFIG_ROOT env var is cwd when config is piped from stdin' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Name: "[envvar('DSC_CONFIG_ROOT')]"
"@
$out = $yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.result[0].properties.Name | Should -BeExactly (Get-Location).Path
}
It 'DSC Configuration Document with key-value pair works' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Name: 'TestClassResource1'
HashTableProp:
Name: 'DSCv3'
"@
$out = $yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results.result.actualState.result.properties.HashTableProp.Name | Should -BeExactly 'DSCv3'
}
It 'Config calling PS Resource directly works for <operation> with metadata <metadata> and adapter <adapter>' -TestCases @(
@{ Operation = 'get'; directive = 'requireAdapter: '; adapter = 'Microsoft.DSC/PowerShell' }
@{ Operation = 'set'; directive = 'requireAdapter: '; adapter = 'Microsoft.DSC/PowerShell' }
@{ Operation = 'test'; directive = 'requireAdapter: '; adapter = 'Microsoft.DSC/PowerShell' }
@{ Operation = 'get'; directive = 'requireAdapter: '; adapter = 'Microsoft.Adapter/PowerShell' }
@{ Operation = 'set'; directive = 'requireAdapter: '; adapter = 'Microsoft.Adapter/PowerShell' }
@{ Operation = 'test'; directive = 'requireAdapter: '; adapter = 'Microsoft.Adapter/PowerShell' }
@{ Operation = 'get'; directive = '' }
@{ Operation = 'set'; directive = '' }
@{ Operation = 'test'; directive = '' }
) {
param($Operation, $directive, $adapter)
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
directives:
$directive$adapter
properties:
Name: 'TestClassResource1'
HashTableProp:
Name: 'DSCv3'
Prop1: foo
"@
$out = dsc -l trace config $operation -i $yaml 2> $TestDrive/tracing.txt
$text = $out | Out-String
$out = $out | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path $TestDrive/tracing.txt)
switch ($Operation) {
'get' {
$out.results[0].result.actualState.Name | Should -BeExactly 'TestClassResource1' -Because ("$text`n" + (Get-Content -Raw -Path $TestDrive/tracing.txt))
}
'set' {
$out.results[0].result.beforeState.Name | Should -BeExactly 'TestClassResource1' -Because $text
$out.results[0].result.afterState.Name | Should -BeExactly 'TestClassResource1' -Because $text
}
'test' {
$out.results[0].result.inDesiredState | Should -BeFalse -Because $text
}
}
if ($directive -eq 'requireAdapter: ') {
"$TestDrive/tracing.txt" | Should -FileContentMatch "Invoking $Operation for '$adapter'" -Because (Get-Content -Raw -Path $TestDrive/tracing.txt)
}
}
It 'Config works with credential object' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Name: 'TestClassResource'
Credential:
UserName: 'User'
Password: 'Password'
"@
$out = dsc config get -i $yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results.result.actualstate.Credential.UserName | Should -Be 'User'
$out.results.result.actualState.result.Credential.Password.Length | Should -Not -BeNullOrEmpty
}
It 'Config does not work when credential properties are missing required fields' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource credential info
type: TestClassResource/TestClassResource
properties:
Name: 'TestClassResource'
Credential:
UserName: 'User'
OtherProperty: 'Password'
"@
$out = dsc config get -i $yaml 2>&1 | Out-String
$LASTEXITCODE | Should -Be 2
$out | Should -Not -BeNullOrEmpty
$out | Should -BeLike "*ERROR*Credential object 'Credential' requires both 'username' and 'password' properties*"
}
It 'Config get is able to return proper enum value' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Name: 'TestClassResource'
Ensure: 'Present'
"@
$out = dsc config get -i $yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results.result.actualState.Ensure | Should -Be 'Present'
}
It 'Config export is able to return proper enum value' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
"@
$out = dsc config export -i $yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.resources[0].properties.result.count | Should -Be 5
$out.resources[0].properties.result[0].Name | Should -Be "Object1"
$out.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}
It 'Expressions get passed correctly to adapted resource' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Name: EchoBack
Prop1: "[[this is a string literal]"
EnumProp: 'Expected'
"@
$out = dsc config get -i $yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results.result.actualState.Name | Should -BeExactly 'EchoBack'
$out.results.result.actualState.Prop1 | Should -BeExactly '[this is a string literal]'
$out.results.result.actualState.EnumProp | Should -BeExactly 'Expected'
}
}