Skip to content

Commit 6df0729

Browse files
More tests and bug fixes
1 parent 844d36f commit 6df0729

7 files changed

Lines changed: 198 additions & 31 deletions

src/dsc/psresourceget.ps1

Lines changed: 102 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,13 @@ function GetPSResourceList {
273273
$repositoryState = Get-PSResourceRepository -Name $inputObj.repositoryName -ErrorAction SilentlyContinue
274274

275275
if (-not $repositoryState) {
276-
Write-Trace -message "Repository not found: $($inputObj.repositoryName). Returning PSResourceList with _exist = false." -level info
277-
return [PSResourceList]::new($inputObj.repositoryName, $inputResources, $false)
276+
Write-Trace -message "Repository not found: $($inputObj.repositoryName)" -level info
277+
$emptyResources = @()
278+
$emptyResources += $inputResources | ForEach-Object {
279+
[PSResource]::new($_.Name)
280+
}
281+
282+
return [PSResourceList]::new($inputObj.repositoryName, $emptyResources, $false)
278283
}
279284

280285
$inputPSResourceList = [PSResourceList]::new($inputObj.repositoryName, $inputResources, $repositoryState.Trusted)
@@ -368,13 +373,21 @@ function GetOperation {
368373
return ( $ret.ToJson() )
369374
}
370375

371-
'repositorylist' { throw [System.NotImplementedException]::new("Get operation is not implemented for RepositoryList resource.") }
372-
'psresource' { throw [System.NotImplementedException]::new("Get operation is not implemented for PSResource resource.") }
376+
'repositorylist' {
377+
Write-Trace -level error -message "Get operation is not implemented for RepositoryList resource."
378+
exit 6
379+
}
380+
'psresource' {
381+
Write-Trace -level error -message "Get operation is not implemented for PSResource resource."
382+
exit 6
383+
}
373384
'psresourcelist' {
374385
(GetPSResourceList -inputObj $inputObj).ToJson()
375386
}
376-
377-
default { throw "Unknown ResourceType: $ResourceType" }
387+
default {
388+
Write-Trace -level error -message "Unknown ResourceType: $ResourceType"
389+
exit 6
390+
}
378391
}
379392
}
380393

@@ -392,7 +405,7 @@ function TestPSResourceList {
392405
Write-Trace -message "Repository not found: $($inputObj.repositoryName). Returning PSResourceList with _inDesiredState = false." -level info
393406
$retValue = [PSResourceList]::new($inputObj.repositoryName, $inputResources, $false)
394407
$retValue._inDesiredState = $false
395-
return $retValue.ToJsonForTest()
408+
$retValue.ToJsonForTest()
396409
'["repositoryName", "resources"]'
397410
}
398411

@@ -424,14 +437,26 @@ function TestOperation {
424437
$inputObj = $stdinput | ConvertFrom-Json -ErrorAction Stop
425438

426439
switch ($ResourceType) {
427-
'repository' { throw [System.NotImplementedException]::new("Test operation is not implemented for RepositoryList resource.") }
428-
'repositorylist' { throw [System.NotImplementedException]::new("Test operation is not implemented for RepositoryList resource.") }
429-
'psresource' { throw [System.NotImplementedException]::new("Test operation is not implemented for PSResource resource.") }
440+
'repository' {
441+
Write-Trace -level error -message "Test operation is not implemented for Repository resource."
442+
exit 7
443+
}
444+
'repositorylist' {
445+
Write-Trace -level error -message "Test operation is not implemented for RepositoryList resource."
446+
exit 7
447+
}
448+
'psresource' {
449+
Write-Trace -level error -message "Test operation is not implemented for PSResource resource."
450+
exit 7
451+
}
430452
'psresourcelist' {
431453
TestPSResourceList -inputObj $inputObj
432454
}
433455

434-
default { throw "Unknown ResourceType: $ResourceType" }
456+
default {
457+
Write-Trace -level error -message "Unknown ResourceType: $ResourceType"
458+
exit 5
459+
}
435460
}
436461
}
437462

@@ -456,14 +481,23 @@ function ExportOperation {
456481
}
457482
}
458483

459-
'repositorylist' { throw [System.NotImplementedException]::new("Get operation is not implemented for RepositoryList resource.") }
460-
'psresource' { throw [System.NotImplementedException]::new("Get operation is not implemented for PSResource resource.") }
484+
'repositorylist' {
485+
Write-Trace -level error -message "Get operation is not implemented for RepositoryList resource."
486+
exit 8
487+
}
488+
'psresource' {
489+
Write-Trace -level error -message "Export operation is not implemented for PSResource resource."
490+
exit 8
491+
}
461492
'psresourcelist' {
462493
$currentUserPSResources = Get-PSResource
463494
$allUsersPSResources = Get-PSResource -Scope AllUsers
464495
PopulatePSResourceListObject -allUsersPSResources $allUsersPSResources -currentUserPSResources $currentUserPSResources
465496
}
466-
default { throw "Unknown ResourceType: $ResourceType" }
497+
default {
498+
Write-Trace -level error -message "Unknown ResourceType: $ResourceType"
499+
exit 5
500+
}
467501
}
468502
}
469503

@@ -521,22 +555,40 @@ function SetPSResourceList {
521555
}
522556

523557
if ($resourcesToInstall.Count -gt 0) {
524-
$psRepository = Get-PSResourceRepository -Name $repositoryName -ErrorAction Stop
558+
$psRepository = Get-PSResourceRepository -Name $repositoryName -ErrorAction SilentlyContinue
525559

526560
if (-not $psRepository) {
527561
Write-Trace -level error -message "Repository '$repositoryName' not found. Cannot install resources."
528-
return
562+
exit 2
529563
}
530564

531565
if (-not $psRepository.Trusted -and -not $inputObj.trustedRepository) {
532566
Write-Trace -level error -message "Repository '$repositoryName' is not trusted. Cannot install resources."
533-
return
567+
exit 3
534568
}
535569

536570
Write-Trace -message "Installing resources: $($resourcesToInstall.Values | ForEach-Object { " $($_.Name) -- $($_.Version) " })"
537571
$resourcesToInstall.Values | ForEach-Object {
538572
$usePrerelease = if ($_.preRelease) { $true } else { $false }
539-
Install-PSResource -Name $_.Name -Version $_.Version -Scope $scope -Repository $repositoryName -ErrorAction Stop -TrustRepository:$inputObj.trustedRepository -Prerelease:$usePrerelease -Reinstall
573+
574+
$installErrors = @()
575+
576+
$name = $_.Name
577+
$version = $_.Version
578+
579+
try {
580+
Install-PSResource -Name $_.Name -Version $_.Version -Scope $scope -Repository $repositoryName -ErrorAction Stop -TrustRepository:$inputObj.trustedRepository -Prerelease:$usePrerelease -Reinstall
581+
}
582+
catch {
583+
Write-Trace -level error -message "Failed to install resource '$name' with version '$version'. Error: $($_.Exception.Message)"
584+
$installErrors += $_.Exception.Message
585+
}
586+
587+
if ($installErrors.Count -gt 0) {
588+
Write-Trace -level error -message "One or more errors occurred while installing resource '$name' with version '$version': $($installErrors -join '; ')"
589+
Write-Trace -level trace -message "Exiting with error code 4 due to installation failure."
590+
exit 4
591+
}
540592
}
541593

542594
$resourcesChanged = $true
@@ -593,10 +645,19 @@ function SetOperation {
593645
return GetOperation -ResourceType $ResourceType
594646
}
595647

596-
'repositorylist' { throw [System.NotImplementedException]::new("Set operation is not implemented for RepositoryList resource.") }
597-
'psresource' { throw [System.NotImplementedException]::new("Set operation is not implemented for PSResource resource.") }
648+
'repositorylist' {
649+
Write-Trace -level error -message "Set operation is not implemented for RepositoryList resource."
650+
exit 10
651+
}
652+
'psresource' {
653+
Write-Trace -level error -message "Set operation is not implemented for PSResource resource."
654+
exit 11
655+
}
598656
'psresourcelist' { return SetPSResourceList -inputObj $inputObj }
599-
default { throw "Unknown ResourceType: $ResourceType" }
657+
default {
658+
Write-Trace -level error -message "Unknown ResourceType: $ResourceType"
659+
exit 5
660+
}
600661
}
601662
}
602663

@@ -623,11 +684,22 @@ function DeleteOperation {
623684

624685
return GetOperation -ResourceType $ResourceType
625686
}
626-
627-
'repositorylist' { throw [System.NotImplementedException]::new("Delete operation is not implemented for RepositoryList resource.") }
628-
'psresource' { throw [System.NotImplementedException]::new("Delete operation is not implemented for PSResource resource.") }
629-
'psresourcelist' { throw [System.NotImplementedException]::new("Delete operation is not implemented for PSResourceList resource.") }
630-
default { throw "Unknown ResourceType: $ResourceType" }
687+
'repositorylist' {
688+
Write-Trace -level error -message "Delete operation is not implemented for RepositoryList resource."
689+
exit 11
690+
}
691+
'psresource' {
692+
Write-Trace -level error -message "Delete operation is not implemented for PSResource resource."
693+
exit 11
694+
}
695+
'psresourcelist' {
696+
Write-Trace -level error -message "Delete operation is not implemented for PSResourceList resource."
697+
exit 11
698+
}
699+
default {
700+
Write-Trace -level error -message "Unknown ResourceType: $ResourceType"
701+
exit 5
702+
}
631703
}
632704
}
633705

@@ -714,5 +786,8 @@ switch ($Operation.ToLower()) {
714786
'test' { return (TestOperation -ResourceType $ResourceType) }
715787
'export' { return (ExportOperation -ResourceType $ResourceType) }
716788
'delete' { return (DeleteOperation -ResourceType $ResourceType) }
717-
default { throw "Unknown Operation: $Operation" }
789+
default {
790+
Write-Trace -level error -message "Unknown Operation: $Operation"
791+
exit 12
792+
}
718793
}

src/dsc/psresourcelist.dsc.resource.json

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"-ExecutionPolicy",
2020
"Bypass",
2121
"-Command",
22-
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation 'get'"
22+
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation get; exit $LASTEXITCODE"
2323
],
2424
"input": "stdin"
2525
},
@@ -32,7 +32,7 @@
3232
"-ExecutionPolicy",
3333
"Bypass",
3434
"-Command",
35-
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation set"
35+
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation set; exit $LASTEXITCODE"
3636
],
3737
"input": "stdin",
3838
"return": "stateAndDiff"
@@ -46,7 +46,7 @@
4646
"-ExecutionPolicy",
4747
"Bypass",
4848
"-Command",
49-
"./psresourceget.ps1 -resourcetype 'psresourcelist' -operation export"
49+
"./psresourceget.ps1 -resourcetype 'psresourcelist' -operation export; exit $LASTEXITCODE"
5050
],
5151
"input": "stdin"
5252
},
@@ -59,11 +59,26 @@
5959
"-ExecutionPolicy",
6060
"Bypass",
6161
"-Command",
62-
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation test"
62+
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation test; exit $LASTEXITCODE"
6363
],
6464
"input": "stdin",
6565
"return": "stateAndDiff"
6666
},
67+
"exitCodes": {
68+
"0": "Success",
69+
"1": "Error",
70+
"2": "Repository not found (during set operation)",
71+
"3": "Repository not trusted (during set operation)",
72+
"4": "Could not install one or more resources (during set operation)",
73+
"5": "Unknown resource type",
74+
"6": "Resource is not implemented",
75+
"7": "Test operation is not implemented for the resource",
76+
"8": "Export operation is not implemented for the resource",
77+
"9": "Get operation is not implemented for the resource",
78+
"10": "Set operation is not implemented for the resource",
79+
"11": "Delete operation is not implemented for the resource",
80+
"12": "Unknown operation"
81+
},
6782
"schema": {
6883
"embedded": {
6984
"$schema": "https://json-schema.org/draft/2020-12/schema",

test/DscResource/PSResourceGetDSCResource.Tests.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,30 @@ Describe 'E2E tests for PSResourceList resource' -Tags 'CI' {
409409
}
410410
}
411411
}
412+
413+
Describe "Error code tests" -Tags 'CI' {
414+
415+
BeforeAll {
416+
SetupDsc
417+
418+
$mod = Get-PSResource -Name 'testmodule99' -ErrorAction SilentlyContinue
419+
if ($mod) {
420+
$mod | Uninstall-PSResource -ErrorAction SilentlyContinue
421+
}
422+
}
423+
424+
It 'Repository not found should return error code 2' {
425+
$out = & $script:dscExe config set -f (Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.error.norepo.dsc.yaml') 2>&1
426+
$out[-1] | Should -BeLike "*Repository not found (during set operation)*"
427+
}
428+
429+
It 'Repository not trusted should return error code 3' {
430+
$out = & $script:dscExe config set -f (Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.error.install.untrustedrepo.dsc.yaml') 2>&1
431+
$out[-1] | Should -BeLike "*Repository not trusted (during set operation)*"
432+
}
433+
434+
It 'Resource not found should return error code 4' {
435+
$out = & $script:dscExe config set -f (Join-Path -Path $PSScriptRoot -ChildPath 'configs/psresourcegetlist.error.noresource.dsc.yaml') 2>&1
436+
$out[-1] | Should -BeLike '*Could not install one or more resources (during set operation)*'
437+
}
438+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: Install testmodule99
4+
type: Microsoft.PowerShell.PSResourceGet/PSResourceList
5+
properties:
6+
repositoryName: PSGallery
7+
trustedRepository: false
8+
resources:
9+
- name: testmodule99
10+
version: 0.0.93
11+
_exist: true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: Install testmodule99
4+
type: Microsoft.PowerShell.PSResourceGet/PSResourceList
5+
properties:
6+
repositoryName: DoesNotExist
7+
trustedRepository: true
8+
resources:
9+
- name: testmodule99
10+
version: 0.0.93
11+
_exist: true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: Install testmodule99
4+
type: Microsoft.PowerShell.PSResourceGet/PSResourceList
5+
properties:
6+
repositoryName: PSGallery
7+
trustedRepository: true
8+
resources:
9+
- name: doesNotExistModule
10+
version: 0.0.1
11+
_exist: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: Install testmodule99 stable and prerelease
4+
type: Microsoft.PowerShell.PSResourceGet/PSResourceList
5+
properties:
6+
repositoryName: PSGallery
7+
trustedRepository: true
8+
resources:
9+
- name: testmodule99
10+
version: '[[0.0.93,)'
11+
_exist: true
12+
preRelease: false
13+
- name: testmodule99
14+
version: '[[100.0.99,)'
15+
_exist: true
16+
preRelease: true
17+

0 commit comments

Comments
 (0)