Skip to content

Commit 7bd4ab8

Browse files
Bug fixes and tests
1 parent cc9ae96 commit 7bd4ab8

7 files changed

Lines changed: 314 additions & 128 deletions

src/dsc/psresourceget.ps1

Lines changed: 132 additions & 100 deletions
Large diffs are not rendered by default.

src/dsc/psresourcelist.dsc.resource.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979
"description": "The name of the repository from where the resources are acquired.",
8080
"type": "string"
8181
},
82+
"trustedRepository": {
83+
"title": "Trusted Repository",
84+
"description": "Indicates whether the repository is a trusted repository and installation should continue without prompting.",
85+
"type": "boolean",
86+
"default": false
87+
},
8288
"resources": {
8389
"title": "Resources",
8490
"description": "The list of resources to manage.",

test/DscResource/PSResourceGetDSCResource.Tests.ps1

Lines changed: 138 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
22
Import-Module $modPath -Force -Verbose
33

4-
Describe "DSC resource tests" -tags 'CI' {
4+
Describe "DSC resource schema tests" -tags 'CI' {
55
BeforeAll {
66
$DSC_ROOT = $env:DSC_ROOT
77
if (-not (Test-Path -Path $DSC_ROOT)) {
@@ -33,16 +33,17 @@ Describe "DSC resource tests" -tags 'CI' {
3333
$repoResource.properties.trusted.title | Should -BeExactly 'Trusted'
3434
$repoResource.properties.priority.title | Should -BeExactly 'Priority'
3535
$repoResource.properties.repositoryType.title | Should -BeExactly 'Repository Type'
36-
$repoResource.properties._exist.{$ref} | Should -Not -BeNullOrEmpty
36+
$repoResource.properties._exist.'$ref' | Should -Not -BeNullOrEmpty
3737
}
3838

3939
It 'PSResourceList resource has expected properties' {
4040
$psresourceListResource = & $dscExe resource schema --resource Microsoft.PowerShell.PSResourceGet/PSResourceList -o json | convertfrom-json | select-object -first 1
4141
$psresourceListResource.properties.repositoryName.title | Should -BeExactly 'Repository Name'
42+
$psresourceListResource.properties.trustedRepository.title | Should -BeExactly 'Trusted Repository'
4243
$psresourceListResource.properties.resources.title | Should -BeExactly 'Resources'
43-
$psresourceListResource.properties.resources.type| Should -BeExactly 'array'
44+
$psresourceListResource.properties.resources.type | Should -BeExactly 'array'
4445
$psresourceListResource.properties.resources.minItems | Should -Be 0
45-
$psresourceListResource.properties.resources.items.{$ref} | Should -BeExactly '#/$defs/PSResource'
46+
$psresourceListResource.properties.resources.items.'$ref' | Should -BeExactly '#/$defs/PSResource'
4647
}
4748
}
4849

@@ -62,7 +63,7 @@ Describe 'Repository Resource Tests' -Tags 'CI' {
6263

6364
$dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
6465

65-
# Register a test repository to ensure DSC can access repositories
66+
# Register a test repository to ensure DSC can access repositories
6667
Register-PSResourceRepository -Name 'TestRepo' -uri 'https://www.doesnotexist.com' -ErrorAction SilentlyContinue -APIVersion Local
6768
}
6869
AfterAll {
@@ -72,9 +73,9 @@ Describe 'Repository Resource Tests' -Tags 'CI' {
7273

7374
It 'Can get a Repository resource instance' {
7475
$repoParams = @{
75-
name = 'TestRepo'
76-
uri = 'https://www.doesnotexist.com'
77-
_exist = $true
76+
name = 'TestRepo'
77+
uri = 'https://www.doesnotexist.com'
78+
_exist = $true
7879
}
7980

8081
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
@@ -93,9 +94,9 @@ Describe 'Repository Resource Tests' -Tags 'CI' {
9394
$repoParams = @{
9495
name = 'TestRepo2'
9596
uri = 'https://www.doesnotexist.com'
96-
_exist = $true
97+
_exist = $true
9798
repositoryType = 'Local'
98-
priority = 51
99+
priority = 51
99100
}
100101

101102
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
@@ -121,9 +122,9 @@ Describe 'Repository Resource Tests' -Tags 'CI' {
121122
Register-PSResourceRepository -Name 'TestRepoToDelete' -uri 'https://www.doesnotexist.com' -ErrorAction SilentlyContinue -APIVersion Local
122123

123124
$repoParams = @{
124-
name = 'TestRepoToDelete'
125-
uri = 'https://www.doesnotexist.com'
126-
_exist = $false
125+
name = 'TestRepoToDelete'
126+
uri = 'https://www.doesnotexist.com'
127+
_exist = $false
127128
}
128129

129130
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
@@ -189,16 +190,20 @@ Describe "PSResourceList Resource Tests" -Tags 'CI' {
189190
repositoryName = $localRepo
190191
resources = @(
191192
@{
192-
name = $testModuleName
193-
version = '1.0.0'
193+
name = $testModuleName
194+
version = '1.0.0'
194195
},
195196
@{
196-
name = $testModuleName2
197-
version = '5.0.0'
197+
name = $testModuleName2
198+
version = '5.0.0'
198199
}
199200
)
200201
}
201202

203+
## Setup expected state by ensuring the modules are not installed
204+
Uninstall-PSResource -name $TestmoduleName -ErrorAction SilentlyContinue
205+
Uninstall-PSResource -name $testModuleName2 -ErrorAction SilentlyContinue
206+
202207
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
203208
$getResult = & $dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
204209
$getResult.actualState.repositoryName | Should -BeExactly $localRepo
@@ -212,28 +217,133 @@ Describe "PSResourceList Resource Tests" -Tags 'CI' {
212217
It 'Can set a PSResourceList resource instance with resources' {
213218
$psResourceListParams = @{
214219
repositoryName = $localRepo
220+
trustedRepository = $true
215221
resources = @(
216222
@{
217-
name = $testModuleName
218-
version = '1.0.0'
223+
name = $testModuleName
224+
version = '5.0.0'
219225
},
220226
@{
221-
name = $testModuleName2
222-
version = '5.0.0'
227+
name = $testModuleName2
228+
version = '5.0.0'
223229
}
224230
)
225231
}
226232

233+
Uninstall-PSResource -name $TestmoduleName -ErrorAction SilentlyContinue
234+
Uninstall-PSResource -name $testModuleName2 -ErrorAction SilentlyContinue
235+
227236
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
228-
$getResult = & $dscExe resource set --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
229-
$getResult.actualState.repositoryName | Should -BeExactly $localRepo
230-
$getResult.actualState.resources.Count | Should -Be 2
231-
$getResult.actualState.resources[0].name | Should -BeExactly $testModuleName
232-
$getResult.actualState.resources[0].version | Should -BeExactly '5.0.0'
233-
$getResult.actualState.resources[1].name | Should -BeExactly $testModuleName2
234-
$getResult.actualState.resources[1].version | Should -BeExactly '1.0.0'
237+
$setResult = & $dscExe resource set --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
238+
$setResult.afterState.repositoryName | Should -BeExactly $localRepo
239+
$setResult.afterState.resources.Count | Should -Be 2
240+
$setResult.afterState.resources[0].name | Should -BeExactly $testModuleName
241+
$setResult.afterState.resources[0].version | Should -BeExactly '5.0.0'
242+
$setResult.afterState.resources[1].name | Should -BeExactly $testModuleName2
243+
$setResult.afterState.resources[1].version | Should -BeExactly '5.0.0'
244+
}
245+
246+
It 'Can test a PSResourceList resource instance with resources' {
247+
$psResourceListParams = @{
248+
repositoryName = $localRepo
249+
resources = @(
250+
@{
251+
name = $testModuleName
252+
version = '5.0.0'
253+
},
254+
@{
255+
name = $testModuleName2
256+
version = '5.0.0'
257+
}
258+
)
259+
}
260+
261+
Install-PSResource -name $TestmoduleName -version '5.0.0' -Repository $localRepo -ErrorAction SilentlyContinue -Reinstall -TrustRepository
262+
Install-PSResource -name $testModuleName2 -version '5.0.0' -Repository $localRepo -ErrorAction SilentlyContinue -Reinstall -TrustRepository
263+
264+
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
265+
$testResult = & $dscExe resource test --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
266+
$testResult.inDesiredState | Should -BeTrue
235267
}
236268

269+
It 'Can test a PSResourceList resource instance that is not in desired state' {
270+
$psResourceListParams = @{
271+
repositoryName = $localRepo
272+
resources = @(
273+
@{
274+
name = $testModuleName
275+
version = '9.0.0'
276+
},
277+
@{
278+
name = $testModuleName2
279+
version = '9.0.0'
280+
}
281+
)
282+
}
237283

284+
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
285+
$testResult = & $dscExe resource test --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
286+
$testResult.inDesiredState | Should -BeFalse
287+
}
238288
}
239289

290+
Describe 'E2E tests for Repository resource' -Tags 'CI' {
291+
BeforeAll {
292+
$DSC_ROOT = $env:DSC_ROOT
293+
if (-not (Test-Path -Path $DSC_ROOT)) {
294+
throw "DSC_ROOT environment variable is not set or path does not exist."
295+
}
296+
297+
$env:PATH += ";$DSC_ROOT"
298+
299+
# Ensure DSC v3 is available
300+
if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {
301+
throw "DSC v3 is not installed"
302+
}
303+
304+
$dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
305+
306+
Get-PSResourceRepository -Name 'TestRepository' -ErrorAction SilentlyContinue | Unregister-PSResourceRepository -ErrorAction SilentlyContinue
307+
}
308+
309+
It 'Register test repository via DSC configuration' {
310+
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.get.dsc.yaml'
311+
& $dscExe config set -f $configPath
312+
$repo = Get-PSResourceRepository -Name 'TestRepository' -ErrorAction SilentlyContinue
313+
$repo.Name | Should -BeExactly 'TestRepository'
314+
$repo.Uri.AbsoluteUri | Should -BeExactly 'https://www.powershellgallery.com/api/v2'
315+
$repo.Priority | Should -Be 55
316+
$repo.Trusted | Should -Be $true
317+
$repo.ApiVersion | Should -Be 'V2'
318+
}
319+
320+
It 'Get test repository via DSC configuration' {
321+
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.get.dsc.yaml'
322+
$out = & $dscExe config set -f $configPath -o json | ConvertFrom-Json
323+
$out.results.result.afterState.name | Should -BeExactly 'TestRepository'
324+
$out.results.result.afterState.uri | Should -BeExactly 'https://www.powershellgallery.com/api/v2'
325+
$out.results.result.afterState._exist | Should -Be $true
326+
$out.results.result.afterState.trusted | Should -Be $true
327+
$out.results.result.afterState.priority | Should -Be 55
328+
$out.results.result.afterState.repositoryType | Should -Be 'V2'
329+
}
330+
331+
It 'Export test repository via DSC configuration' {
332+
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.export.dsc.yaml'
333+
$out = & $dscExe config export -f $configPath -o json | ConvertFrom-Json
334+
335+
# Verify exported file content
336+
$testRepo = $out.resources.properties | Where-Object { $_.name -eq 'TestRepository' }
337+
$testRepo | Should -Not -BeNullOrEmpty
338+
$testRepo.name | Should -BeExactly 'TestRepository'
339+
$testRepo.uri | Should -BeExactly 'https://www.powershellgallery.com/api/v2'
340+
$testRepo._exist | Should -Be $true
341+
}
342+
343+
It 'Unregister test repository via DSC configuration' {
344+
$configPath = Join-Path -Path $PSScriptRoot -ChildPath 'configs/repository.unregister.dsc.yaml'
345+
& $dscExe config set -f $configPath
346+
$repo = Get-PSResourceRepository -Name 'TestRepository' -ErrorAction SilentlyContinue
347+
$repo | Should -BeNullOrEmpty
348+
}
349+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: verify PSGallery settings
4+
type: Microsoft.PowerShell.PSResourceGet/Repository
5+
properties:
6+
name: TestRepository
7+
uri: https://www.powershellgallery.com/api/v2
8+
trusted: true
9+
priority: 55
10+
repositoryType: V2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: verify PSGallery settings
4+
type: Microsoft.PowerShell.PSResourceGet/Repository
5+
properties:
6+
name: TestRepository
7+
uri: https://www.powershellgallery.com/api/v2
8+
trusted: true
9+
priority: 55
10+
repositoryType: V2
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: register PSGallery
4+
type: Microsoft.PowerShell.PSResourceGet/Repository
5+
properties:
6+
name: TestRepository
7+
uri: https://www.powershellgallery.com/api/v2
8+
trusted: true
9+
priority: 55
10+
repositoryType: V2
11+
_exist: true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: register PSGallery
4+
type: Microsoft.PowerShell.PSResourceGet/Repository
5+
properties:
6+
name: TestRepository
7+
_exist: false

0 commit comments

Comments
 (0)