Skip to content

Commit da3f83c

Browse files
Add tests
1 parent fe2647c commit da3f83c

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
Describe "DSC resource tests" -tags 'CI' {
2+
BeforeAll {
3+
$DSC_ROOT = $env:DSC_ROOT
4+
if (-not (Test-Path -Path $DSC_ROOT)) {
5+
throw "DSC_ROOT environment variable is not set or path does not exist."
6+
}
7+
8+
$env:PATH += ";$DSC_ROOT"
9+
10+
# Ensure DSC v3 is available
11+
if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {
12+
throw "DSC v3 is not installed"
13+
}
14+
15+
$dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
16+
}
17+
18+
It 'DSC v3 resources can be found' {
19+
$repoResource = & $dscExe resource list Microsoft.PowerShell.PSResourceGet/Repository -o json | convertfrom-json | select-object -ExpandProperty type
20+
$repoResource | Should -BeExactly 'Microsoft.PowerShell.PSResourceGet/Repository'
21+
22+
$pkgResource = & $dscExe resource list Microsoft.PowerShell.PSResourceGet/PSResourceList -o json | convertfrom-json | select-object -ExpandProperty type
23+
$pkgResource | Should -BeExactly 'Microsoft.PowerShell.PSResourceGet/PSResourceList'
24+
}
25+
26+
It 'Repository resource has expected properties' {
27+
$repoResource = & $dscExe resource schema --resource Microsoft.PowerShell.PSResourceGet/Repository -o json | convertfrom-json | select-object -first 1
28+
$repoResource.properties.name.title | Should -BeExactly 'Name'
29+
$repoResource.properties.uri.title | Should -BeExactly 'URI'
30+
$repoResource.properties.trusted.title | Should -BeExactly 'Trusted'
31+
$repoResource.properties.priority.title | Should -BeExactly 'Priority'
32+
$repoResource.properties.repositoryType.title | Should -BeExactly 'Repository Type'
33+
$repoResource.properties._exist.{$ref} | Should -Not -BeNullOrEmpty
34+
}
35+
36+
It 'PSResourceList resource has expected properties' {
37+
$psresourceListResource = & $dscExe resource schema --resource Microsoft.PowerShell.PSResourceGet/PSResourceList -o json | convertfrom-json | select-object -first 1
38+
$psresourceListResource.properties.repositoryName.title | Should -BeExactly 'Repository Name'
39+
$psresourceListResource.properties.resources.title | Should -BeExactly 'Resources'
40+
$psresourceListResource.properties.resources.type| Should -BeExactly 'array'
41+
$psresourceListResource.properties.resources.minItems | Should -Be 0
42+
$psresourceListResource.properties.resources.items.{$ref} | Should -BeExactly '#/$defs/PSResource'
43+
}
44+
}
45+
46+
Describe 'Repository Resource Tests' -Tags 'CI' {
47+
BeforeAll {
48+
$DSC_ROOT = $env:DSC_ROOT
49+
if (-not (Test-Path -Path $DSC_ROOT)) {
50+
throw "DSC_ROOT environment variable is not set or path does not exist."
51+
}
52+
53+
$env:PATH += ";$DSC_ROOT"
54+
55+
# Ensure DSC v3 is available
56+
if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {
57+
throw "DSC v3 is not installed"
58+
}
59+
60+
$dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
61+
62+
# Register a test repository to ensure DSC can access repositories
63+
Register-PSResourceRepository -Name 'TestRepo' -uri 'https://www.doesnotexist.com' -ErrorAction SilentlyContinue -APIVersion Local
64+
}
65+
AfterAll {
66+
# Clean up the test repository
67+
Unregister-PSResourceRepository -Name 'TestRepo' -ErrorAction SilentlyContinue
68+
}
69+
70+
It 'Can get a Repository resource instance' {
71+
$repoParams = @{
72+
name = 'TestRepo'
73+
uri = 'https://www.doesnotexist.com'
74+
_exist = $true
75+
}
76+
77+
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
78+
79+
$getResult = & $dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput -o json | ConvertFrom-Json
80+
81+
$getResult.actualState.name | Should -BeExactly 'TestRepo'
82+
$getResult.actualState.uri | Should -BeExactly 'https://www.doesnotexist.com/'
83+
$getResult.actualState._exist | Should -Be $true
84+
$getResult.actualState.trusted | Should -Be $false
85+
$getResult.actualState.priority | Should -Be 50
86+
$getResult.actualState.repositoryType | Should -Be 'Local'
87+
}
88+
89+
It 'Can set a Repository resource instance' {
90+
$repoParams = @{
91+
name = 'TestRepo2'
92+
uri = 'https://www.doesnotexist.com'
93+
_exist = $true
94+
repositoryType = 'Local'
95+
priority = 51
96+
}
97+
98+
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
99+
100+
try {
101+
& $dscExe resource set --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput
102+
103+
$repo = Get-PSResourceRepository -Name 'TestRepo2' -ErrorAction SilentlyContinue
104+
$repo.Name | Should -BeExactly 'TestRepo2'
105+
$repo.Uri.AbsoluteUri | Should -BeExactly 'https://www.doesnotexist.com/'
106+
$repo.APIVersion | Should -Be 'Local'
107+
$repo.Priority | Should -Be 51
108+
}
109+
finally {
110+
if ($repo) {
111+
Unregister-PSResourceRepository -Name 'TestRepo2' -ErrorAction SilentlyContinue
112+
}
113+
}
114+
}
115+
116+
It 'Can delete a Repository resource instance' {
117+
# First, create a repository to delete
118+
Register-PSResourceRepository -Name 'TestRepoToDelete' -uri 'https://www.doesnotexist.com' -ErrorAction SilentlyContinue -APIVersion Local
119+
120+
$repoParams = @{
121+
name = 'TestRepoToDelete'
122+
uri = 'https://www.doesnotexist.com'
123+
_exist = $false
124+
}
125+
126+
$resourceInput = $repoParams | ConvertTo-Json -Depth 5
127+
128+
& $dscExe resource delete --resource Microsoft.PowerShell.PSResourceGet/Repository --input $resourceInput
129+
130+
$repo = Get-PSResourceRepository -Name 'TestRepoToDelete' -ErrorAction SilentlyContinue
131+
$repo | Should -BeNullOrEmpty
132+
}
133+
}
134+

0 commit comments

Comments
 (0)