Skip to content

Commit 1c50334

Browse files
committed
Fix tests
1 parent 3666cbe commit 1c50334

File tree

3 files changed

+41
-56
lines changed

3 files changed

+41
-56
lines changed

wmi-adapter/Tests/wmi.tests.ps1

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,43 @@ Describe 'WMI adapter resource tests' {
6868
It 'Set does not work without a key property' -Skip:(!$IsWindows) {
6969
$i = @{
7070
VariableValue = "TestValue"
71-
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only property is key, but we need a real one
71+
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only property is key, but we require a key property to be set
7272
} | ConvertTo-Json
7373

7474
$s = dsc resource set -r root.cimv2/Win32_Environment -i $i 2>&1
7575
$LASTEXITCODE | Should -Be 1
7676
$s | Should -BeLike "*All key properties in the CIM class 'Win32_Environment' are read-only, which is not supported.*"
7777
}
7878

79-
# It 'Set works on a WMI resource' -Skip:(!$IsWindows) {
80-
# $i = @{
81-
# UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only property required
82-
# Name = 'test'
83-
# VariableValue = 'test'
84-
# } | ConvertTo-Json
79+
It 'Set works on a WMI resource' -Skip:(!$IsWindows) {
80+
$i = @{
81+
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only key property required
82+
Name = 'test'
83+
VariableValue = 'test'
84+
} | ConvertTo-Json
8585

86-
# $r = dsc -l trace resource set -r root.cimv2/Win32_Environment -i $i
86+
$r = dsc resource set -r root.cimv2/Win32_Environment -i $i
87+
$LASTEXITCODE | Should -Be 0
88+
89+
$out = $r | ConvertFrom-Json
90+
$out.afterState.Name | Should -Be 'test'
91+
$out.afterState.VariableValue | Should -Be 'test'
92+
$out.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
93+
}
8794

88-
# $s = dsc resource set --resource root.cimv2/Win32_Environment --input '{"Name":"TestVariable","Value":"TestValue"}'
89-
# $LASTEXITCODE | Should -Be 0
90-
# $s | Should -BeLike "*Set operation completed successfully*"
95+
It 'Update works on a WMI resource' -Skip:(!$IsWindows) {
96+
$i = @{
97+
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only key property required
98+
Name = 'test'
99+
VariableValue = 'update'
100+
} | ConvertTo-Json
101+
102+
$r = dsc resource set -r root.cimv2/Win32_Environment -i $i
103+
$LASTEXITCODE | Should -Be 0
91104

92-
# # Verify the variable was set
93-
# $get = dsc resource get -r root.cimv2/Win32_Environment -i '{"Name":"TestVariable"}'
94-
# $get | Should -BeLike "*TestValue*"
95-
# }
105+
$out = $r | ConvertFrom-Json
106+
$out.afterState.Name | Should -Be 'test'
107+
$out.afterState.VariableValue | Should -Be 'update'
108+
$out.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
109+
}
96110
}

wmi-adapter/wmi.dsc.resource.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"-Command",
4747
"$Input | ./wmi.resource.ps1 Set"
4848
],
49-
"input": "stdin"
49+
"input": "stdin",
50+
"implementsPretest": true
5051
},
5152
"validate": {
5253
"executable": "powershell",

wmi-adapter/wmiAdapter.psm1

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function Get-DscResourceObject {
3636
}
3737

3838
function GetValidCimProperties {
39-
[OutputType()]
4039
[CmdletBinding()]
4140
param
4241
(
@@ -113,38 +112,7 @@ function GetValidCimProperties {
113112
}
114113

115114
return $availableProperties
116-
}
117-
118-
# if ($SkipReadOnly.IsPresent) {
119-
# # For 'Set', we need to validate that the provided properties match the CIM class
120-
# $availableProperties = $cimClass.CimClassProperties | ForEach-Object {
121-
# [string[]]$flags = $_.Flags.ToString().Split(",").Trim()
122-
# if ($flags -notcontains 'ReadOnly' -or $flags -contains 'Key') {
123-
# $_
124-
# }
125-
# }
126-
127-
# # Reset the validated properties list as we only want to capture non-readonly properties for 'Set'
128-
# $validatedProperties = [System.Collections.Generic.List[Array]]::new()
129-
# foreach ($property in $availableProperties) {
130-
# $propName = $property.Name
131-
# $isKey = $property.IsKey
132-
133-
# if ($isKey) {
134-
# # Still check here if the key property is passed as we continue
135-
# if ($Properties.psobject.properties.name -notcontains $propName -or $null -eq $properties.$propName -or $Properties.$propName -eq '') {
136-
# "Key property '$propName' is required but not provided or is empty." | Write-DscTrace -Operation Error
137-
# exit 1
138-
# } else {
139-
# $validatedProperties.Add($property)
140-
# }
141-
# } elseif ($Properties.psobject.Properties.name -contains $propName) {
142-
# $validatedProperties.Add($property)
143-
# } else {
144-
# "Property '$propName' is not provided in the resource object." | Write-DscTrace -Operation Trace
145-
# }
146-
# }
147-
# }
115+
}
148116

149117
return $validatedProperties
150118
}
@@ -247,10 +215,12 @@ function GetCimSpace {
247215
}
248216

249217
$addToActualState.properties = $instance_result
250-
218+
$result += $addToActualState
219+
} else {
220+
"No WMI instances found for type '$($r.type)'." | Write-DscTrace -Operation Warn
221+
$addToActualState.properties = $null
251222
$result += $addToActualState
252223
}
253-
254224
}
255225
'Set' {
256226
$wmi_instance = ValidateCimMethodAndArguments -DesiredState $r
@@ -352,11 +322,11 @@ class dscResourceObject {
352322
}
353323

354324
$out = [dscResourceObject]@{
355-
name = 'root.cimv2/Win32_Environment'
356-
type = 'root.cimv2/Win32_Environment'
325+
name = "root.cimv2/Win32_Environment"
326+
type = "root.cimv2/Win32_Environment"
357327
properties = [PSCustomObject]@{
358-
UserName = "{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME
359-
VariableValue = 'update'
360-
Name = 'test'
328+
Name = "test"
329+
Value = "test"
330+
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only key property required
361331
}
362332
}

0 commit comments

Comments
 (0)