Skip to content

Commit 8d882c3

Browse files
committed
Add tests
1 parent 7fd2f90 commit 8d882c3

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

wmi-adapter/Tests/wmi.tests.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,40 @@ Describe 'WMI adapter resource tests' {
5858
$res.results[1].result.actualState.result[1].properties.BuildNumber | Should -BeNullOrEmpty
5959
$res.results[1].result.actualState.result[4].properties.AdapterType | Should -BeLike "Ethernet*"
6060
}
61+
62+
It 'Throws error when methodName is missing on set' -Skip:(!$IsWindows) {
63+
'{"Name":"wuauserv"}' | dsc -l trace resource set -r 'root.cimv2/Win32_Service' -f - 2> $TestDrive\tracing.txt
64+
$LASTEXITCODE | Should -Be 2
65+
"$TestDrive/tracing.txt" | Should -FileContentMatch "'methodName' property is required for invoking a WMI/CIM method."
66+
}
67+
68+
It 'Throws error when methodName is set but parameters are missing on set' -Skip:(!$IsWindows) {
69+
'{"Name":"wuauserv", "methodName":"StartService"}' | dsc -l trace resource set -r 'root.cimv2/Win32_Service' -f - 2> $TestDrive\tracing.txt
70+
$LASTEXITCODE | Should -Be 2
71+
"$TestDrive/tracing.txt" | Should -FileContentMatch "'parameters' property is required for invoking a WMI/CIM method."
72+
}
73+
74+
It 'Set works on a WMI resource with methodName and parameters' -Skip:(!$IsWindows) {
75+
BeforeAll {
76+
$script:service = Get-Service -Name wuauserv -ErrorAction Ignore
77+
78+
if ($service -and $service.Status -eq 'Running') {
79+
$service.Stop()
80+
}
81+
}
82+
83+
84+
$r = '{"Name":"wuauserv", "methodName":"StartService", "parameters":{}}' | dsc resource set -r 'root.cimv2/Win32_Service' -f -
85+
$LASTEXITCODE | Should -Be 0
86+
87+
$res = '{"Name":"wuauserv", "State": null}' | dsc resource get -r 'root.cimv2/Win32_Service' -f - | ConvertFrom-Json
88+
$res.actualState.Name | Should -Be 'wuauserv'
89+
$res.actualState.State | Should -Be 'Running'
90+
91+
AfterAll {
92+
if ($service -and $service.Status -eq 'Running') {
93+
$service.Stop()
94+
}
95+
}
96+
}
6197
}

wmi-adapter/wmi.dsc.resource.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@
3535
],
3636
"input": "stdin"
3737
},
38+
"set": {
39+
"executable": "powershell",
40+
"args": [
41+
"-NoLogo",
42+
"-NonInteractive",
43+
"-NoProfile",
44+
"-ExecutionPolicy",
45+
"Bypass",
46+
"-Command",
47+
"$Input | ./wmi.resource.ps1 Set"
48+
],
49+
"input": "stdin"
50+
},
3851
"validate": {
3952
"executable": "powershell",
4053
"args": [

wmi-adapter/wmiAdapter.psm1

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,20 @@ function GetCimSpace {
134134
}
135135
'Set' {
136136
$wmi_instance = ValidateCimMethodAndArguments -DesiredState $r
137-
InvokeCimMethod -CimInstance $wmi_instance.CimInstance -MethodName $wmi_instance.MethodName -Arguments $wmi_instance.Parameters
137+
InvokeCimMethod @wmi_instance
138138

139-
$result += [PSCustomObject]@{
139+
$addToActualState = [dscResourceObject]@{
140140
name = $r.name
141141
type = $r.type
142-
properties = $null # Set operation does not return properties
142+
properties = $null
143143
}
144+
145+
$result += $addToActualState
144146
}
145147
'Test' {
146148
# TODO: implement test
149+
"Test operation is not implemented for WMI/CIM methods." | Write-DscTrace -Operation Error
150+
exit 1
147151
}
148152
}
149153
}
@@ -164,6 +168,12 @@ function ValidateCimMethodAndArguments {
164168
exit 1
165169
}
166170

171+
# This is required for invoking a WMI/CIM method with parameters even if it is empty
172+
if (-not ($DesiredState.properties.psobject.properties | Where-Object -Property Name -EQ 'parameters')) {
173+
"'parameters' property is required for invoking a WMI/CIM method." | Write-DscTrace -Operation Error
174+
exit 1
175+
}
176+
167177
$className = $DesiredState.type.Split("/")[-1]
168178
$namespace = $DesiredState.type.Split("/")[0].Replace(".", "/")
169179

@@ -176,6 +186,7 @@ function ValidateCimMethodAndArguments {
176186

177187
foreach ($param in $parameters.psobject.Properties.name) {
178188
if ($cimClassParameters.Name -notcontains $param) {
189+
# Only warn about invalid parameters, do not exit as this allows to action to continue when calling InvokeCimMethod
179190
"'$param' is not a valid parameter for method '$methodName' in class '$className'." | Write-DscTrace -Operation Warn
180191
} else {
181192
$arguments += @{
@@ -186,14 +197,15 @@ function ValidateCimMethodAndArguments {
186197

187198
$cimInstance = GetWmiInstance -DesiredState $DesiredState
188199

189-
$i = [PSCustomObject]@{
200+
return @{
190201
CimInstance = $cimInstance
191-
Parameters = $arguments
202+
Arguments = $arguments
192203
MethodName = $methodName
193204
}
205+
} else {
206+
"'$className' class not found in namespace '$namespace'." | Write-DscTrace -Operation Error
207+
exit 1
194208
}
195-
196-
return $i
197209
}
198210

199211
function InvokeCimMethod

0 commit comments

Comments
 (0)