3
3
4
4
Describe ' PowerShell adapter resource tests' {
5
5
6
- BeforeAll {
7
- $OldPSModulePath = $env: PSModulePath
8
- $env: PSModulePath += [System.IO.Path ]::PathSeparator + $PSScriptRoot
9
- $pwshConfigPath = Join-path $PSScriptRoot " class_ps_resources.dsc.yaml"
10
-
11
- if ($IsLinux -or $IsMacOS ) {
12
- $cacheFilePath = Join-Path $env: HOME " .dsc" " PSAdapterCache.json"
13
- }
14
- else
15
- {
16
- $cacheFilePath = Join-Path $env: LocalAppData " dsc" " PSAdapterCache.json"
17
- }
6
+ BeforeAll {
7
+ $OldPSModulePath = $env: PSModulePath
8
+ $env: PSModulePath += [System.IO.Path ]::PathSeparator + $PSScriptRoot
9
+ $pwshConfigPath = Join-path $PSScriptRoot " class_ps_resources.dsc.yaml"
10
+
11
+ if ($IsLinux -or $IsMacOS ) {
12
+ $cacheFilePath = Join-Path $env: HOME " .dsc" " PSAdapterCache.json"
18
13
}
19
- AfterAll {
20
- $env: PSModulePath = $OldPSModulePath
14
+ else {
15
+ $cacheFilePath = Join-Path $env: LocalAppData " dsc " " PSAdapterCache.json "
21
16
}
17
+ }
18
+ AfterAll {
19
+ $env: PSModulePath = $OldPSModulePath
20
+ }
22
21
23
- BeforeEach {
24
- Remove-Item - Force - ea SilentlyContinue - Path $cacheFilePath
25
- }
22
+ BeforeEach {
23
+ Remove-Item - Force - ea SilentlyContinue - Path $cacheFilePath
24
+ }
26
25
27
- It ' Get works on config with class-based resources' {
26
+ It ' Get works on config with class-based resources' {
28
27
29
- $r = Get-Content - Raw $pwshConfigPath | dsc config get -f -
30
- $LASTEXITCODE | Should - Be 0
31
- $res = $r | ConvertFrom-Json
32
- $res.results [0 ].result.actualState.result[0 ].properties.Prop1 | Should - BeExactly ' ValueForProp1'
33
- $res.results [0 ].result.actualState.result[0 ].properties.EnumProp | Should - BeExactly ' Expected'
34
- }
28
+ $r = Get-Content - Raw $pwshConfigPath | dsc config get -f -
29
+ $LASTEXITCODE | Should - Be 0
30
+ $res = $r | ConvertFrom-Json
31
+ $res.results [0 ].result.actualState.result[0 ].properties.Prop1 | Should - BeExactly ' ValueForProp1'
32
+ $res.results [0 ].result.actualState.result[0 ].properties.EnumProp | Should - BeExactly ' Expected'
33
+ }
35
34
36
- It ' Get does not work on config when module does not exist' {
35
+ It ' Get does not work on config when module does not exist' {
37
36
38
- $yaml = @'
37
+ $yaml = @'
39
38
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
40
39
resources:
41
40
- name: Working with class-based resources
@@ -45,30 +44,30 @@ Describe 'PowerShell adapter resource tests' {
45
44
- name: Class-resource Info
46
45
type: TestClassResourceNotExist/TestClassResourceNotExist
47
46
'@
48
- $yaml | dsc - l trace config get -f - 2> " $TestDrive /tracing.txt"
49
- $LASTEXITCODE | Should - Be 2
50
- " $TestDrive /tracing.txt" | Should - FileContentMatch ' DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.'
51
- }
47
+ $yaml | dsc - l trace config get -f - 2> " $TestDrive /tracing.txt"
48
+ $LASTEXITCODE | Should - Be 2
49
+ " $TestDrive /tracing.txt" | Should - FileContentMatch ' DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.'
50
+ }
52
51
53
- It ' Test works on config with class-based resources' {
52
+ It ' Test works on config with class-based resources' {
54
53
55
- $r = Get-Content - Raw $pwshConfigPath | dsc config test -f -
56
- $LASTEXITCODE | Should - Be 0
57
- $res = $r | ConvertFrom-Json
58
- $res.results [0 ].result.actualState.result[0 ] | Should -Not - BeNull
59
- }
54
+ $r = Get-Content - Raw $pwshConfigPath | dsc config test -f -
55
+ $LASTEXITCODE | Should - Be 0
56
+ $res = $r | ConvertFrom-Json
57
+ $res.results [0 ].result.actualState.result[0 ] | Should -Not - BeNull
58
+ }
60
59
61
- It ' Set works on config with class-based resources' {
60
+ It ' Set works on config with class-based resources' {
62
61
63
- $r = Get-Content - Raw $pwshConfigPath | dsc config set -f -
64
- $LASTEXITCODE | Should - Be 0
65
- $res = $r | ConvertFrom-Json
66
- $res.results.result.afterState.result [0 ].type | Should - Be " TestClassResource/TestClassResource"
67
- }
62
+ $r = Get-Content - Raw $pwshConfigPath | dsc config set -f -
63
+ $LASTEXITCODE | Should - Be 0
64
+ $res = $r | ConvertFrom-Json
65
+ $res.results.result.afterState.result [0 ].type | Should - Be " TestClassResource/TestClassResource"
66
+ }
68
67
69
- It ' Export works on config with class-based resources' {
68
+ It ' Export works on config with class-based resources' {
70
69
71
- $yaml = @'
70
+ $yaml = @'
72
71
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
73
72
resources:
74
73
- name: Working with class-based resources
@@ -78,18 +77,18 @@ Describe 'PowerShell adapter resource tests' {
78
77
- name: Class-resource Info
79
78
type: TestClassResource/TestClassResource
80
79
'@
81
- $out = $yaml | dsc config export -f -
82
- $LASTEXITCODE | Should - Be 0
83
- $res = $out | ConvertFrom-Json
84
- $res .' $schema' | Should - BeExactly ' https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
85
- $res .' resources' | Should -Not - BeNullOrEmpty
86
- $res.resources [0 ].properties.result.count | Should - Be 5
87
- $res.resources [0 ].properties.result[0 ].Name | Should - Be " Object1"
88
- $res.resources [0 ].properties.result[0 ].Prop1 | Should - Be " Property of object1"
89
- }
90
-
91
- It ' Export fails when class-based resource does not implement' {
92
- $yaml = @'
80
+ $out = $yaml | dsc config export -f -
81
+ $LASTEXITCODE | Should - Be 0
82
+ $res = $out | ConvertFrom-Json
83
+ $res .' $schema' | Should - BeExactly ' https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
84
+ $res .' resources' | Should -Not - BeNullOrEmpty
85
+ $res.resources [0 ].properties.result.count | Should - Be 5
86
+ $res.resources [0 ].properties.result[0 ].Name | Should - Be " Object1"
87
+ $res.resources [0 ].properties.result[0 ].Prop1 | Should - Be " Property of object1"
88
+ }
89
+
90
+ It ' Export fails when class-based resource does not implement' {
91
+ $yaml = @'
93
92
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
94
93
resources:
95
94
- name: Working with class-based resources
@@ -99,21 +98,21 @@ Describe 'PowerShell adapter resource tests' {
99
98
- name: Class-resource Info
100
99
type: TestClassResource/NoExport
101
100
'@
102
- $out = $yaml | dsc config export -f - 2>&1 | Out-String
103
- $LASTEXITCODE | Should - Be 2
104
- $out | Should -Not - BeNullOrEmpty
105
- $out | Should - BeLike " *ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
106
- }
101
+ $out = $yaml | dsc config export -f - 2>&1 | Out-String
102
+ $LASTEXITCODE | Should - Be 2
103
+ $out | Should -Not - BeNullOrEmpty
104
+ $out | Should - BeLike " *ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
105
+ }
107
106
108
- It ' Custom psmodulepath in config works' {
107
+ It ' Custom psmodulepath in config works' {
109
108
110
- $OldPSModulePath = $env: PSModulePath
111
- Copy-Item - Recurse - Force - Path " $PSScriptRoot /TestClassResource" - Destination $TestDrive
112
- Rename-Item - Path " $PSScriptRoot /TestClassResource" - NewName " _TestClassResource"
109
+ $OldPSModulePath = $env: PSModulePath
110
+ Copy-Item - Recurse - Force - Path " $PSScriptRoot /TestClassResource" - Destination $TestDrive
111
+ Rename-Item - Path " $PSScriptRoot /TestClassResource" - NewName " _TestClassResource"
113
112
114
- try {
115
- $psmp = " `$ env:PSModulePath" + [System.IO.Path ]::PathSeparator+ $TestDrive
116
- $yaml = @"
113
+ try {
114
+ $psmp = " `$ env:PSModulePath" + [System.IO.Path ]::PathSeparator + $TestDrive
115
+ $yaml = @"
117
116
`$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
118
117
resources:
119
118
- name: Working with class-based resources
@@ -124,24 +123,24 @@ Describe 'PowerShell adapter resource tests' {
124
123
- name: Class-resource Info
125
124
type: TestClassResource/TestClassResource
126
125
"@
127
- $out = $yaml | dsc config export -f -
128
- $LASTEXITCODE | Should - Be 0
129
- $res = $out | ConvertFrom-Json
130
- $res .' $schema' | Should - BeExactly ' https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
131
- $res .' resources' | Should -Not - BeNullOrEmpty
132
- $res.resources [0 ].properties.result.count | Should - Be 5
133
- $res.resources [0 ].properties.result[0 ].Name | Should - Be " Object1"
134
- $res.resources [0 ].properties.result[0 ].Prop1 | Should - Be " Property of object1"
135
- }
136
- finally {
137
- Rename-Item - Path " $PSScriptRoot /_TestClassResource" - NewName " TestClassResource"
138
- $env: PSModulePath = $OldPSModulePath
139
- }
126
+ $out = $yaml | dsc config export -f -
127
+ $LASTEXITCODE | Should - Be 0
128
+ $res = $out | ConvertFrom-Json
129
+ $res .' $schema' | Should - BeExactly ' https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
130
+ $res .' resources' | Should -Not - BeNullOrEmpty
131
+ $res.resources [0 ].properties.result.count | Should - Be 5
132
+ $res.resources [0 ].properties.result[0 ].Name | Should - Be " Object1"
133
+ $res.resources [0 ].properties.result[0 ].Prop1 | Should - Be " Property of object1"
140
134
}
135
+ finally {
136
+ Rename-Item - Path " $PSScriptRoot /_TestClassResource" - NewName " TestClassResource"
137
+ $env: PSModulePath = $OldPSModulePath
138
+ }
139
+ }
141
140
142
- It ' DSCConfigRoot macro is working when config is from a file' {
141
+ It ' DSCConfigRoot macro is working when config is from a file' {
143
142
144
- $yaml = @"
143
+ $yaml = @"
145
144
`$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
146
145
resources:
147
146
- name: Working with class-based resources
@@ -154,19 +153,19 @@ Describe 'PowerShell adapter resource tests' {
154
153
Name: "[envvar('DSC_CONFIG_ROOT')]"
155
154
"@
156
155
157
- $config_path = " $TestDrive /test_config.dsc.yaml"
158
- $yaml | Set-Content - Path $config_path
156
+ $config_path = " $TestDrive /test_config.dsc.yaml"
157
+ $yaml | Set-Content - Path $config_path
159
158
160
- $out = dsc config get -- file $config_path
161
- $LASTEXITCODE | Should - Be 0
162
- $res = $out | ConvertFrom-Json
163
- $res.results.result.actualState.result.properties.Name | Should - Be $TestDrive
164
- $res.results.result.actualState.result.properties.Prop1 | Should - Be $TestDrive
165
- }
159
+ $out = dsc config get -- file $config_path
160
+ $LASTEXITCODE | Should - Be 0
161
+ $res = $out | ConvertFrom-Json
162
+ $res.results.result.actualState.result.properties.Name | Should - Be $TestDrive
163
+ $res.results.result.actualState.result.properties.Prop1 | Should - Be $TestDrive
164
+ }
166
165
167
- It ' DSC_CONFIG_ROOT env var is cwd when config is piped from stdin' {
166
+ It ' DSC_CONFIG_ROOT env var is cwd when config is piped from stdin' {
168
167
169
- $yaml = @"
168
+ $yaml = @"
170
169
`$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
171
170
resources:
172
171
- name: Working with class-based resources
@@ -178,8 +177,29 @@ Describe 'PowerShell adapter resource tests' {
178
177
properties:
179
178
Name: "[envvar('DSC_CONFIG_ROOT')]"
180
179
"@
181
- $out = $yaml | dsc config get -f - | ConvertFrom-Json
182
- $LASTEXITCODE | Should - Be 0
183
- $out.results [0 ].result.actualState.result[0 ].properties.Name | Should - BeExactly (Get-Location ).Path
184
- }
180
+ $out = $yaml | dsc config get -f - | ConvertFrom-Json
181
+ $LASTEXITCODE | Should - Be 0
182
+ $out.results [0 ].result.actualState.result[0 ].properties.Name | Should - BeExactly (Get-Location ).Path
183
+ }
184
+
185
+ It ' DSC Configuration Document with key-value pair works' {
186
+ $yaml = @"
187
+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
188
+ resources:
189
+ - name: Working with class-based resources
190
+ type: Microsoft.DSC/PowerShell
191
+ properties:
192
+ resources:
193
+ - name: Class-resource Info
194
+ type: TestClassResource/TestClassResource
195
+ properties:
196
+ Name: 'TestClassResource1'
197
+ HashTableProp:
198
+ Name: 'DSCv3'
199
+ "@
200
+
201
+ $out = $yaml | dsc config get -f - | ConvertFrom-Json
202
+ $LASTEXITCODE | Should - Be 0
203
+ $out.results.result.actualState.result.properties.HashTableProp.Name | Should - BeExactly ' DSCv3'
204
+ }
185
205
}
0 commit comments