-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathPSResourceGetDSCResource.Tests.ps1
More file actions
572 lines (460 loc) · 24.8 KB
/
Copy pathPSResourceGetDSCResource.Tests.ps1
File metadata and controls
572 lines (460 loc) · 24.8 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
Import-Module $modPath -Force -Verbose
function SetupDsc {
$script:DSC_ROOT = $env:DSC_ROOT
if (-not (Test-Path -Path $script:DSC_ROOT)) {
throw "DSC_ROOT environment variable is not set or path does not exist."
}
$pathSeparator = [System.IO.Path]::PathSeparator
$env:PATH += "$pathSeparator$script:DSC_ROOT"
# Ensure DSC v3 is available
if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {
throw "DSC v3 is not installed"
}
$script:dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
$expectedModulePath = Join-Path $env:BUILD_SOURCESDIRECTORY 'out'
$resources = Get-ChildItem $expectedModulePath/*resource.json -ErrorAction SilentlyContinue -Recurse
if (-not $script:dscExe) {
throw "Could not find dsc executable in PATH after setup."
}
if (-not $resources.Count -ge 2) {
throw "Expected at least 2 resource schema files in PSResourceGet module directory, found $($resources.Count)."
}
$resourcePath = Split-Path $resources[0].FullName -Parent
Write-Verbose -Verbose "Adding DSC resource path to PATH environment variable: $resourcePath"
$env:PATH += "$pathSeparator$resourcePath"
}
function SetupTestRepos {
$script:localRepo = "psgettestlocal"
$script:testModuleName = "test_local_mod"
$script:testModuleName2 = "test_local_mod2"
$script:testModuleName3 = "Test_Local_Mod3"
Get-NewPSResourceRepositoryFile
Register-LocalRepos
New-TestModule -moduleName $script:testModuleName -repoName $script:localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()
New-TestModule -moduleName $script:testModuleName -repoName $script:localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags @()
New-TestModule -moduleName $script:testModuleName2 -repoName $script:localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags @()
New-TestModule -moduleName $script:testModuleName3 -repoName $script:localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()
}
Describe "DSC resource schema tests" -tags 'CI' {
BeforeAll {
SetupDsc
}
It 'DSC v3 resources can be found' {
$repoResource = & $script:dscExe resource list Microsoft.PowerShell.PSResourceGet/Repository -o json | convertfrom-json | select-object -ExpandProperty type
$repoResource | Should -BeExactly 'Microsoft.PowerShell.PSResourceGet/Repository'
$pkgResource = & $script:dscExe resource list Microsoft.PowerShell.PSResourceGet/PSResourceList -o json | convertfrom-json | select-object -ExpandProperty type
$pkgResource | Should -BeExactly 'Microsoft.PowerShell.PSResourceGet/PSResourceList'
}
It 'Repository resource has expected properties' {
$repoResource = & $script:dscExe resource schema --resource Microsoft.PowerShell.PSResourceGet/Repository -o json | convertfrom-json | select-object -first 1
$repoResource.properties.name.title | Should -BeExactly 'Name'
$repoResource.properties.uri.title | Should -BeExactly 'URI'
$repoResource.properties.trusted.title | Should -BeExactly 'Trusted'
$repoResource.properties.priority.title | Should -BeExactly 'Priority'
$repoResource.properties.repositoryType.title | Should -BeExactly 'Repository Type'
$repoResource.properties._exist.'$ref' | Should -Not -BeNullOrEmpty
}
It 'PSResourceList resource has expected properties' {
$psresourceListResource = & $script:dscExe resource schema --resource Microsoft.PowerShell.PSResourceGet/PSResourceList -o json | convertfrom-json | select-object -first 1
$psresourceListResource.properties.repositoryName.title | Should -BeExactly 'Repository Name'
$psresourceListResource.properties.trustedRepository.title | Should -BeExactly 'Trusted Repository'
$psresourceListResource.properties.resources.title | Should -BeExactly 'Resources'
$psresourceListResource.properties.resources.type | Should -BeExactly 'array'
$psresourceListResource.properties.resources.minItems | Should -Be 0
$psresourceListResource.properties.resources.items.'$ref' | Should -BeExactly '#/$defs/PSResource'
}
}
Describe 'Repository Resource Tests' -Tags 'CI' {
BeforeAll {
# Register a test repository to ensure DSC can access repositories
Register-PSResourceRepository -Name 'TestRepo' -uri 'https://www.doesnotexist.com' -ErrorAction SilentlyContinue -APIVersion Local
}
AfterAll {
# Clean up the test repository
Unregister-PSResourceRepository -Name 'TestRepo' -ErrorAction SilentlyContinue
}
It 'Can get a Repository resource instance' {
$repoParams = @{
name = 'TestRepo'
uri = 'https://www.doesnotexist.com'
_exist = $true
}
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
Get-Module -Name Microsoft.PowerShell.PSResourceGet | Write-Verbose -Verbose
$getResult = & $script:dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput -o json | ConvertFrom-Json
$getResult.actualState.name | Should -BeExactly 'TestRepo'
$getResult.actualState.uri | Should -BeExactly 'https://www.doesnotexist.com/'
$getResult.actualState._exist | Should -Be $true
$getResult.actualState.trusted | Should -Be $false
$getResult.actualState.priority | Should -Be 50
$getResult.actualState.repositoryType | Should -Be 'Local'
}
It 'Can set a Repository resource instance' {
$repoParams = @{
name = 'TestRepo2'
uri = 'https://www.doesnotexist.com'
_exist = $true
repositoryType = 'Local'
priority = 51
}
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
try {
& $script:dscExe resource set --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput
$repo = Get-PSResourceRepository -Name 'TestRepo2' -ErrorAction SilentlyContinue
$repo.Name | Should -BeExactly 'TestRepo2'
$repo.Uri.AbsoluteUri | Should -BeExactly 'https://www.doesnotexist.com/'
$repo.APIVersion | Should -Be 'Local'
$repo.Priority | Should -Be 51
}
finally {
if ($repo) {
Unregister-PSResourceRepository -Name 'TestRepo2' -ErrorAction SilentlyContinue
}
}
}
It 'Can delete a Repository resource instance' {
# First, create a repository to delete
Register-PSResourceRepository -Name 'TestRepoToDelete' -uri 'https://www.doesnotexist.com' -ErrorAction SilentlyContinue -APIVersion Local
$repoParams = @{
name = 'TestRepoToDelete'
uri = 'https://www.doesnotexist.com'
_exist = $false
}
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
& $script:dscExe resource delete --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput
$repo = Get-PSResourceRepository -Name 'TestRepoToDelete' -ErrorAction SilentlyContinue
$repo | Should -BeNullOrEmpty
}
}
Describe "PSResourceList Resource Tests" -Tags 'CI' {
BeforeAll {
SetupDsc
SetupTestRepos
}
AfterAll {
# Clean up the test repository
Get-RevertPSResourceRepositoryFile
}
It 'Can get a PSResourceList resource instance' {
$psResourceListParams = @{
repositoryName = $localRepo
resources = @()
}
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
$getResult = & $script:dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$getResult.actualState.repositoryName | Should -BeExactly $localRepo
$getResult.actualState.resources.Count | Should -Be 0
}
It 'Can get a PSResourceList resource instance with resources' {
$psResourceListParams = @{
repositoryName = $localRepo
resources = @(
@{
name = $testModuleName
version = '1.0.0'
},
@{
name = $testModuleName2
version = '5.0.0'
}
)
}
## Setup expected state by ensuring the modules are not installed
Uninstall-PSResource -name $script:testModuleName -ErrorAction SilentlyContinue
Uninstall-PSResource -name $script:testModuleName2 -ErrorAction SilentlyContinue
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
$getResult = & $script:dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$getResult.actualState.repositoryName | Should -BeExactly $script:localRepo
$getResult.actualState.resources.Count | Should -Be 2
$getResult.actualState.resources[0].name | Should -BeExactly $script:testModuleName
$getResult.actualState.resources[0]._exist | Should -BeFalse
$getResult.actualState.resources[1].name | Should -BeExactly $script:testModuleName2
$getResult.actualState.resources[1]._exist | Should -BeFalse
}
It 'Can set a PSResourceList resource instance with resources' {
$psResourceListParams = @{
repositoryName = $script:localRepo
trustedRepository = $true
resources = @(
@{
name = $script:testModuleName
version = '5.0.0'
},
@{
name = $script:testModuleName2
version = '5.0.0'
}
)
}
Uninstall-PSResource -name $script:testModuleName -ErrorAction SilentlyContinue
Uninstall-PSResource -name $script:testModuleName2 -ErrorAction SilentlyContinue
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
$setResult = & $script:dscExe resource set --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$setResult.afterState.repositoryName | Should -BeExactly $script:localRepo
$setResult.afterState.resources.Count | Should -Be 2
$setResult.afterState.resources[0].name | Should -BeIn @($script:testModuleName, $script:testModuleName2)
$setResult.afterState.resources[0].version | Should -Be '5.0.0'
$setResult.afterState.resources[1].name | Should -BeIn @($script:testModuleName, $script:testModuleName2)
$setResult.afterState.resources[1].version | Should -Be '5.0.0'
}
It 'Can test a PSResourceList resource instance with resources' {
$psResourceListParams = @{
repositoryName = $script:localRepo
resources = @(
@{
name = $script:testModuleName
version = '5.0.0'
},
@{
name = $script:testModuleName2
version = '5.0.0'
}
)
}
Install-PSResource -name $script:testModuleName -version '5.0.0' -Repository $script:localRepo -ErrorAction SilentlyContinue -Reinstall -TrustRepository
Install-PSResource -name $script:testModuleName2 -version '5.0.0' -Repository $script:localRepo -ErrorAction SilentlyContinue -Reinstall -TrustRepository
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
$testResult = & $script:dscExe resource test --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$testResult.inDesiredState | Should -BeTrue
}
It 'Can test a PSResourceList resource instance that is not in desired state' {
$psResourceListParams = @{
repositoryName = $script:localRepo
resources = @(
@{
name = $script:testModuleName
version = '9.0.0'
},
@{
name = $script:testModuleName2
version = '9.0.0'
}
)
}
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
$testResult = & $script:dscExe resource test --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$testResult.inDesiredState | Should -BeFalse
}
}
Describe 'E2E tests for Repository resource' -Tags 'CI' {
BeforeAll {
Get-PSResourceRepository -Name 'TestRepository' -ErrorAction SilentlyContinue | Unregister-PSResourceRepository -ErrorAction SilentlyContinue
}
It 'Register test repository via DSC configuration' {
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.get.dsc.yaml'
& $dscExe config set -f $configPath
$repo = Get-PSResourceRepository -Name 'TestRepository' -ErrorAction SilentlyContinue
$repo.Name | Should -BeExactly 'TestRepository'
$repo.Uri.AbsoluteUri | Should -BeExactly 'https://www.powershellgallery.com/api/v2'
$repo.Priority | Should -Be 55
$repo.Trusted | Should -Be $true
$repo.ApiVersion | Should -Be 'V2'
}
It 'Get test repository via DSC configuration' {
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.get.dsc.yaml'
$out = & $dscExe config set -f $configPath -o json | ConvertFrom-Json
$out.results.result.afterState.name | Should -BeExactly 'TestRepository'
$out.results.result.afterState.uri | Should -BeExactly 'https://www.powershellgallery.com/api/v2'
$out.results.result.afterState._exist | Should -Be $true
$out.results.result.afterState.trusted | Should -Be $true
$out.results.result.afterState.priority | Should -Be 55
$out.results.result.afterState.repositoryType | Should -Be 'V2'
}
It 'Export test repository via DSC configuration' {
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.export.dsc.yaml'
$out = & $dscExe config export -f $configPath -o json | ConvertFrom-Json
# Verify exported file content
$testRepo = $out.resources.properties | Where-Object { $_.name -eq 'TestRepository' }
$testRepo | Should -Not -BeNullOrEmpty
$testRepo.name | Should -BeExactly 'TestRepository'
$testRepo.uri | Should -BeExactly 'https://www.powershellgallery.com/api/v2'
$testRepo._exist | Should -Be $true
}
It 'Unregister test repository via DSC configuration' {
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.unregister.dsc.yaml'
& $dscExe config set -f $configPath
$repo = Get-PSResourceRepository -Name 'TestRepository' -ErrorAction SilentlyContinue
$repo | Should -BeNullOrEmpty
}
}
Describe 'E2E tests for PSResourceList resource' -Tags 'CI' {
BeforeAll {
SetupDsc
## The installed modules will not be found in the Windows PowerShell module path because DSC will install them in the PowerShell 7 context.
## Therefore, skipping these tests on Windows PowerShell is necessary because the modules installed by DSC will not be visible in that environment.
$isOnWindowsPowerShell = $PSVersionTable.PSVersion.Major -lt 6
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues['it:skip'] = $isOnWindowsPowerShell
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
It 'Can Install testmodule99' {
$mod = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue -Version '0.0.93'
if ($mod) {
$mod | Uninstall-PSResource -ErrorAction SilentlyContinue
}
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.install.dsc.yaml'
& $script:dscExe config set -f $configPath
$psresource = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue -Version '0.0.93'
$psresource.Name | Should -Be 'testmodule99'
$psresource.Version | Should -Be '0.0.93'
}
It 'Can Uninstall testmodule99' {
Install-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue -Repository PSGallery -Reinstall -TrustRepository -Version '0.0.93'
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.uninstall.dsc.yaml'
& $script:dscExe config set -f $configPath
$psresource = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue -Version '0.0.93'
$psresource | Should -BeNullOrEmpty
}
It 'Can export PSResourceList with testmodule99' {
Install-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue -Repository PSGallery -Reinstall -TrustRepository -Version '0.0.93'
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.export.dsc.yaml'
$out = & $script:dscExe config export -f $configPath -o json | ConvertFrom-Json
$psResourceList = $out.resources.properties | Where-Object { $_.repositoryName -eq 'PSGallery' }
$psResourceList | Should -Not -BeNullOrEmpty
$psResourceList.repositoryName | Should -BeExactly 'PSGallery'
$psResourceList.resources.Count | Should -BeGreaterThan 0
$psResourceList.resources.name | Should -Contain 'testmodule99'
}
It 'Can Install module with dependency via PSResourceList' {
$modulelist = @('TestModuleWithDependencyA', 'TestModuleWithDependencyB', 'TestModuleWithDependencyC', 'TestModuleWithDependencyD', 'TestModuleWithDependencyE')
$mods = Get-PSResource -Name $modulelist -ErrorAction SilentlyContinue
if ($mods) {
$mods | Uninstall-PSResource -ErrorAction SilentlyContinue
}
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.moddeps.install.dsc.yaml'
& $script:dscExe config set -f $configPath
$psresource = Get-PSResource -Name $modulelist -ErrorAction SilentlyContinue
$psresource | Should -HaveCount 5
}
It 'Can install modules with prerelease versions via PSResourceList' {
$mod = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue
if ($mod) {
$mod | Uninstall-PSResource -ErrorAction SilentlyContinue
}
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.prerelease.install.dsc.yaml'
& $script:dscExe config set -f $configPath
$psresource = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue
$psresource | Should -HaveCount 2
$psresource | ForEach-Object {
$version = if ($_.prerelease) { "$($_.Version)" + '.' + "$($_.PreRelease)" } else { $_.Version.ToString() }
$version | Should -BeIn @("101.0.99.beta1", "0.0.93")
}
}
It 'Can install modules with one existing other not' {
$mod = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue
if ($mod) {
$mod | Uninstall-PSResource -ErrorAction SilentlyContinue
}
Install-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue -Repository PSGallery -Reinstall -TrustRepository -Version '0.0.93'
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.oneexisting.install.dsc.yaml'
& $script:dscExe config set -f $configPath
$psresource = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue
$psresource | Should -HaveCount 2
$psresource | ForEach-Object {
$version = if ($_.prerelease) { "$($_.Version)" + '.' + "$($_.PreRelease)" } else { $_.Version.ToString() }
$version | Should -BeIn @("101.0.99.beta1", "0.0.93")
}
}
}
Describe "Error code tests" -Tags 'CI' {
BeforeAll {
SetupDsc
$mod = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue
if ($mod) {
$mod | Uninstall-PSResource -ErrorAction SilentlyContinue
}
}
It 'Repository not found should return error code 2' {
$out = & $script:dscExe config set -f (Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.error.norepo.dsc.yaml') 2>&1
$out[-1] | Should -BeLike "*Repository not found (during set operation)*"
}
It 'Repository not trusted should return error code 3' {
$out = & $script:dscExe config set -f (Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.error.install.untrustedrepo.dsc.yaml') 2>&1
$out[-1] | Should -BeLike "*Repository not trusted (during set operation)*"
}
It 'Resource not found should return error code 4' {
$out = & $script:dscExe config set -f (Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.error.noresource.dsc.yaml') 2>&1
$out[-1] | Should -BeLike '*Could not install one or more resources (during set operation)*'
}
}
Describe 'PSResourceList what-if tests' -Tags 'CI' {
BeforeAll {
SetupDsc
SetupTestRepos
$isOnWindowsPowerShell = $PSVersionTable.PSVersion.Major -lt 6
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues['it:skip'] = $isOnWindowsPowerShell
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
Get-RevertPSResourceRepositoryFile
}
It 'What-if install does not modify the system' {
Uninstall-PSResource -Name $script:testModuleName -ErrorAction SilentlyContinue
Uninstall-PSResource -Name $script:testModuleName2 -ErrorAction SilentlyContinue
$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: PSResourceList what-if install
type: Microsoft.PowerShell.PSResourceGet/PSResourceList
properties:
repositoryName: $script:localRepo
trustedRepository: true
resources:
- name: $script:testModuleName
version: '5.0.0'
- name: $script:testModuleName2
version: '5.0.0'
"@
$out = & $script:dscExe config set --what-if --input $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw)
$result = $out.results.result[0].afterState
$result.repositoryName | Should -BeExactly $script:localRepo
$result.resources.Count | Should -Be 2
$result.resources[0]._metadata.whatIf[0] | Should -Match 'Would install'
$result.resources[1]._metadata.whatIf[0] | Should -Match 'Would install'
Get-PSResource -Name $script:testModuleName -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
Get-PSResource -Name $script:testModuleName2 -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
}
It 'What-if uninstall does not modify the system' {
Install-PSResource -Name $script:testModuleName -Version '5.0.0' -Repository $script:localRepo -TrustRepository -Reinstall
$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: PSResourceList what-if uninstall
type: Microsoft.PowerShell.PSResourceGet/PSResourceList
properties:
repositoryName: $script:localRepo
resources:
- name: $script:testModuleName
_exist: false
"@
$out = & $script:dscExe config set --what-if --input $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw)
$result = $out.results.result[0].afterState
$result.resources[0]._exist | Should -BeFalse
$result.resources[0]._metadata.whatIf[0] | Should -Match 'Would uninstall'
Get-PSResource -Name $script:testModuleName -ErrorAction SilentlyContinue | Should -Not -BeNullOrEmpty
}
It 'What-if returns no metadata for resources already in desired state' {
Install-PSResource -Name $script:testModuleName -Version '5.0.0' -Repository $script:localRepo -TrustRepository -Reinstall
$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: PSResourceList what-if in desired state
type: Microsoft.PowerShell.PSResourceGet/PSResourceList
properties:
repositoryName: $script:localRepo
resources:
- name: $script:testModuleName
version: '5.0.0'
"@
$out = & $script:dscExe config set --what-if --input $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw)
$result = $out.results.result[0].afterState
$result.resources[0]._metadata | Should -BeNullOrEmpty
}
}