forked from PowerShell/DSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowershellgroup.resource.tests.ps1
255 lines (213 loc) · 9.94 KB
/
powershellgroup.resource.tests.ps1
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
# 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'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.Prop1 | Should -BeExactly 'ValueForProp1'
}
It 'Get uses enum names on class-based resource' {
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.EnumProp | Should -BeExactly 'Expected'
}
It 'Test works on class-based resource' {
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.InDesiredState | Should -Be $True
}
It 'Set works on class-based resource' {
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource set -r 'TestClassResource/TestClassResource'
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.afterState.result | Should -Not -BeNull
}
It 'Export works on PS class-based resource' {
$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"
}
It 'Get --all works on PS class-based resource' {
$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 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 adapted_dsc_type field in Get' {
$oldPath = $env:PATH
try {
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
$r = '{TestCaseId: 1}'| dsc resource get -r 'Test/TestCase'
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.actualState.result | Should -Be $True
}
finally {
$env:PATH = $oldPath
}
}
It 'Verify adapted_dsc_type field in Set' {
$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'
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.beforeState.result | Should -Be $True
$resources.afterState.result | Should -Be $True
}
finally {
$env:PATH = $oldPath
}
}
It 'Verify adapted_dsc_type field in Test' {
$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'
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.actualState.result | Should -Be $True
}
finally {
$env:PATH = $oldPath
}
}
It 'Verify adapted_dsc_type field in Export' {
$oldPath = $env:PATH
try {
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
$r = dsc resource export -r 'Test/TestCase'
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
$resources.resources[0].properties.result | Should -Be $True
}
finally {
$env:PATH = $oldPath
}
}
It 'Dsc can process large resource output' -Tag z1{
$env:TestClassResourceResultCount = 5000 # with sync resource invocations this was not possible
$r = dsc resource export -r TestClassResource/TestClassResource
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.resources[0].properties.result.count | Should -Be 5000
$env:TestClassResourceResultCount = $null
}
}