-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathpowershellgroup.resource.tests.ps1
More file actions
348 lines (294 loc) · 14.9 KB
/
powershellgroup.resource.tests.ps1
File metadata and controls
348 lines (294 loc) · 14.9 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
# 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
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 -ea SilentlyContinue -Path $cacheFilePath
}
It 'Discovery includes class-based resources' {
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }).Count | Should -Be 1
}
It 'Get works on class-based resource' {
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.Prop1 | Should -BeExactly 'ValueForProp1'
# verify that only properties with DscProperty attribute are returned
$propertiesNames = $res.actualState | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
}
It 'Get uses enum names on class-based resource' {
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.EnumProp | Should -BeExactly 'Expected'
}
It 'Test works on class-based resource' {
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.InDesiredState | Should -Be $True
$res.actualState.InDesiredState.GetType().Name | Should -Be "Boolean"
# verify that only properties with DscProperty attribute are returned
$propertiesNames = $res.actualState.InDesiredState | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
}
It 'Set works on class-based resource' {
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource set -r 'TestClassResource/TestClassResource' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.afterState.Prop1 | Should -BeExactly 'ValueForProp1'
$res.changedProperties | Should -BeNullOrEmpty
}
It 'Export works on PS class-based resource' -Pending {
$r = dsc resource export -r TestClassResource/TestClassResource
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$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"
# verify that only properties with DscProperty attribute are returned
$res.resources[0].properties.result | % {
$propertiesNames = $_ | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
}
}
It 'Get --all works on PS class-based resource' -Pending {
$r = dsc resource get --all -r TestClassResource/TestClassResource
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.count | Should -Be 5
$res.actualState.result | % { $_.Name | Should -Not -BeNullOrEmpty }
}
It 'Verify that ClearCache works in PSAdapter' {
# generate the cache
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
# call the ClearCache operation
$scriptPath = Join-Path $PSScriptRoot '..' 'psDscAdapter' 'powershell.resource.ps1'
$null = & $scriptPath -Operation ClearCache
# verify that PSAdapter does not find the cache
dsc -l debug resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Cache file not found'
}
It 'Verify that a new PS Cache version results in cache rebuid' {
# generate the cache
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
# update the version in the cache file
$cacheFilePath = if ($IsWindows) {
# PS 6+ on Windows
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
}
else {
# either WinPS or PS 6+ on Linux/Mac
if ($PSVersionTable.PSVersion.Major -le 5) {
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
}
else {
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
}
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
$cache.CacheSchemaVersion = 0
$jsonCache = $cache | ConvertTo-Json -Depth 90
New-Item -Force -Path $cacheFilePath -Value $jsonCache -Type File | Out-Null
# verify that a new PS Cache version results in cache rebuid
dsc -l debug resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Incompatible version of cache in file'
}
It 'Verify that removing a module results in cache rebuid' {
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination "$PSScriptRoot/Backup/TestClassResource"
Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource"
$oldPath = $env:PSModulePath
try {
$env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive
# generate the cache
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
# remove the module files
Remove-Item -Recurse -Force -Path "$TestDrive/TestClassResource"
# verify that cache rebuid happened
dsc -l trace resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry'
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'
}
finally {
$env:PSModulePath = $oldPath
Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot"
Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup"
}
}
It 'Verify inheritance works in class-based resources' {
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$t = $resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }
$t.properties | Should -Contain "BaseProperty"
}
It 'Verify highest module version is loaded' {
$srcPath = Join-Path $PSScriptRoot 'TestClassResource'
$pathRoot1 = Join-Path $TestDrive 'A'
$pathRoot2 = Join-Path $TestDrive 'B'
$path1 = Join-Path $pathRoot1 'TestClassResource' '1.0'
$path2 = Join-Path $pathRoot1 'TestClassResource' '1.1'
$path3 = Join-Path $pathRoot2 'TestClassResource' '2.0'
$path4 = Join-Path $pathRoot2 'TestClassResource' '2.0.1'
New-Item -ItemType Directory -Force -Path $path1 | Out-Null
New-Item -ItemType Directory -Force -Path $path2 | Out-Null
New-Item -ItemType Directory -Force -Path $path3 | Out-Null
New-Item -ItemType Directory -Force -Path $path4 | Out-Null
$files = Get-ChildItem -Recurse -File -Path $srcPath
$files | Copy-Item -Destination $path1
$files | Copy-Item -Destination $path2
$files | Copy-Item -Destination $path3
$files | Copy-Item -Destination $path4
$filePath = Join-Path $path1 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = '0.0.1'", "ModuleVersion = `'1.0`'") | Set-Content $filePath
$filePath = Join-Path $path2 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = '0.0.1'", "ModuleVersion = `'1.1`'") | Set-Content $filePath
$filePath = Join-Path $path3 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = '0.0.1'", "ModuleVersion = `'2.0`'") | Set-Content $filePath
$filePath = Join-Path $path4 'TestClassResource.psd1'
(Get-Content -Raw $filePath).Replace("ModuleVersion = '0.0.1'", "ModuleVersion = '2.0.1'") | Set-Content $filePath
$oldPath = $env:PSModulePath
try {
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1
$env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$r = @($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' })
$r.Count | Should -Be 1
$r[0].Version | Should -Be '2.0.1'
}
finally {
$env:PSModulePath = $oldPath
}
}
It 'Verify invoke Get on adapted resource' {
$oldPath = $env:PATH
try {
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
$r = '{"TestCaseId": 1}' | dsc -l trace resource get -r 'Test/TestCase' -f - 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/tracing.txt | Out-String)
$resources = $r | ConvertFrom-Json
$resources.actualState.TestCaseid | Should -Be 1
}
finally {
$env:PATH = $oldPath
}
}
It 'Verify invoke Set on adapted resource' {
$oldPath = $env:PATH
try {
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
$r = '{"TestCaseId": 1}' | dsc resource set -r 'Test/TestCase' -f - 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/tracing.txt | Out-String)
$resources = $r | ConvertFrom-Json
$resources.beforeState.TestCaseid | Should -Be 1
$resources.afterState.TestCaseId | Should -Be 1
}
finally {
$env:PATH = $oldPath
}
}
It 'Verify invoke Test on adapted resource' {
$oldPath = $env:PATH
try {
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
$r = '{"TestCaseId": 1}' | dsc resource test -r 'Test/TestCase' -f -
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.actualState.TestCaseId | Should -Be 1
}
finally {
$env:PATH = $oldPath
}
}
It 'Verify invoke Export on adapted resource' {
$oldPath = $env:PATH
try {
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
$r = dsc -l trace resource export -r 'Test/TestCase' 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/tracing.txt | Out-String)
$resources = $r | ConvertFrom-Json
$resources.resources.count | Should -Be 2
$resources.resources[0].type | Should -BeExactly 'Test/TestCase'
$resources.resources[0].name | Should -BeExactly 'Test/TestCase-0'
$resources.resources[0].properties.TestCaseId | Should -Be 1
$resources.resources[1].type | Should -BeExactly 'Test/TestCase'
$resources.resources[1].name | Should -BeExactly 'Test/TestCase-1'
$resources.resources[1].properties.TestCaseId | Should -Be 2
}
finally {
$env:PATH = $oldPath
}
}
It 'Dsc can process large resource output' -Pending {
try {
$env:TestClassResourceResultCount = 5000 # with sync resource invocations this was not possible
$r = dsc -l trace resource export -r TestClassResource/TestClassResource 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/tracing.txt | Out-String)
$res = $r | ConvertFrom-Json
$res.resources[0].properties.result.count | Should -Be 5000
}
finally {
$env:TestClassResourceResultCount = $null
}
}
It 'Verify that there are no cache rebuilds for several sequential executions' {
# remove cache file
$cacheFilePath = if ($IsWindows) {
# PS 6+ on Windows
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
}
else {
# either WinPS or PS 6+ on Linux/Mac
if ($PSVersionTable.PSVersion.Major -le 5) {
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
}
else {
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
}
}
Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore
# first execution should build the cache
dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache'
# next executions following shortly after should Not rebuild the cache
1..3 | ForEach-Object {
dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache'
}
}
It 'Can process a key-value pair object' {
$r = '{"HashTableProp":{"Name":"DSCv3"},"Name":"TestClassResource1"}' | dsc resource get -r 'TestClassResource/TestClassResource' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.HashTableProp.Name | Should -Be 'DSCv3'
}
}