Skip to content

Commit 2bb1bcf

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
fix tests
1 parent 8757a0e commit 2bb1bcf

File tree

6 files changed

+67
-82
lines changed

6 files changed

+67
-82
lines changed

dsc/locales/en-us.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ noParameters = "No parameters specified"
6767
[resource_command]
6868
implementedAs = "implemented as"
6969
invalidOperationOnAdapter = "Can not perform this operation on the adapter itself"
70-
adapterNotFound = "Adapter not found"
7170
setInputEmpty = "Desired input is empty"
7271
testInputEmpty = "Expected input is required"
7372

dsc_lib/locales/en-us.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ parameterNotArray = "Parameter '%{name}' is not an array"
6666
parameterNotObject = "Parameter '%{name}' is not an object"
6767
invokePropertyExpressions = "Invoke property expressions"
6868
invokeExpression = "Invoke property expression for %{name}: %{value}"
69-
adapterNotFound = "Adapter '%{adapter}' not found for resource '%{resource}'"
7069

7170
[discovery.commandDiscovery]
7271
couldNotReadSetting = "Could not read 'resourcePath' setting"

powershell-adapter/Tests/powershellgroup.config.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ Describe 'PowerShell adapter resource tests' {
230230
}
231231
'set' {
232232
$out.results[0].result.beforeState.Name | Should -BeExactly 'TestClassResource1' -Because $text
233-
$out.results[0].result.afterState | Should -BeNullOrEmpty
233+
$out.results[0].result.afterState.Name | Should -BeExactly 'TestClassResource1' -Because $text
234234
}
235235
'test' {
236236
$out.results[0].result.actualState.InDesiredState | Should -BeFalse -Because $text

powershell-adapter/Tests/powershellgroup.resource.tests.ps1

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,17 @@ Describe 'PowerShell adapter resource tests' {
297297
}
298298

299299
It 'Dsc can process large resource output' -Pending {
300-
$env:TestClassResourceResultCount = 5000 # with sync resource invocations this was not possible
301-
302-
$r = dsc -l trace resource export -r TestClassResource/TestClassResource 2> $TestDrive/tracing.txt
303-
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/tracing.txt | Out-String)
304-
$res = $r | ConvertFrom-Json
305-
$res.resources[0].properties.result.count | Should -Be 5000
300+
try {
301+
$env:TestClassResourceResultCount = 5000 # with sync resource invocations this was not possible
306302

307-
$env:TestClassResourceResultCount = $null
303+
$r = dsc -l trace resource export -r TestClassResource/TestClassResource 2> $TestDrive/tracing.txt
304+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/tracing.txt | Out-String)
305+
$res = $r | ConvertFrom-Json
306+
$res.resources[0].properties.result.count | Should -Be 5000
307+
}
308+
finally {
309+
$env:TestClassResourceResultCount = $null
310+
}
308311
}
309312

310313
It 'Verify that there are no cache rebuilds for several sequential executions' {

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
4040
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File' -f -
4141
$LASTEXITCODE | Should -Be 0
4242
$res = $r | ConvertFrom-Json
43-
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
43+
$res.actualState.DestinationPath | Should -Be "$testFile"
4444
}
4545

4646
It 'Set works on Binary "File" resource' -Skip:(!$IsWindows){
@@ -58,7 +58,7 @@ Describe 'WindowsPowerShell adapter resource tests - requires elevated permissio
5858
$r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\','\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script' -f -
5959
$LASTEXITCODE | Should -Be 0
6060
$res = $r | ConvertFrom-Json
61-
$res.actualState.result.properties.result | Should -Be 'test'
61+
$res.actualState.result | Should -Be 'test'
6262
}
6363

6464
It 'Get works on config with File resource for WinPS' -Skip:(!$IsWindows){

wmi-adapter/wmi.resource.ps1

Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ function Write-Trace {
3232
$host.ui.WriteErrorLine($trace)
3333
}
3434

35-
function IsConfiguration($obj) {
36-
if ($null -ne $obj.metadata -and $null -ne $obj.metadata.'Microsoft.DSC' -and $obj.metadata.'Microsoft.DSC'.context -eq 'configuration') {
37-
return $true
38-
}
39-
40-
return $false
41-
}
42-
4335
if ($Operation -eq 'List')
4436
{
4537
$clases = Get-CimClass
@@ -89,89 +81,81 @@ elseif ($Operation -eq 'Get')
8981

9082
$result = @()
9183

92-
if (IsConfiguration $inputobj_pscustomobj) # we are processing a config batch
84+
foreach($r in $inputobj_pscustomobj.resources)
9385
{
94-
foreach($r in $inputobj_pscustomobj.resources)
95-
{
96-
$type_fields = $r.type -split "/"
97-
$wmi_namespace = $type_fields[0].Replace('.','\')
98-
$wmi_classname = $type_fields[1]
86+
$type_fields = $r.type -split "/"
87+
$wmi_namespace = $type_fields[0].Replace('.','\')
88+
$wmi_classname = $type_fields[1]
9989

100-
# TODO: identify key properties and add WHERE clause to the query
101-
if ($r.properties)
90+
# TODO: identify key properties and add WHERE clause to the query
91+
if ($r.properties)
92+
{
93+
$query = "SELECT $($r.properties.psobject.properties.name -join ',') FROM $wmi_classname"
94+
$where = " WHERE "
95+
$useWhere = $false
96+
$first = $true
97+
foreach ($property in $r.properties.psobject.properties)
10298
{
103-
$query = "SELECT $($r.properties.psobject.properties.name -join ',') FROM $wmi_classname"
104-
$where = " WHERE "
105-
$useWhere = $false
106-
$first = $true
107-
foreach ($property in $r.properties.psobject.properties)
99+
# TODO: validate property against the CIM class to give better error message
100+
if ($null -ne $property.value)
108101
{
109-
# TODO: validate property against the CIM class to give better error message
110-
if ($null -ne $property.value)
102+
$useWhere = $true
103+
if ($first)
111104
{
112-
$useWhere = $true
113-
if ($first)
114-
{
115-
$first = $false
116-
}
117-
else
118-
{
119-
$where += " AND "
120-
}
105+
$first = $false
106+
}
107+
else
108+
{
109+
$where += " AND "
110+
}
121111

122-
if ($property.TypeNameOfValue -eq "System.String")
123-
{
124-
$where += "$($property.Name) = '$($property.Value)'"
125-
}
126-
else
127-
{
128-
$where += "$($property.Name) = $($property.Value)"
129-
}
112+
if ($property.TypeNameOfValue -eq "System.String")
113+
{
114+
$where += "$($property.Name) = '$($property.Value)'"
115+
}
116+
else
117+
{
118+
$where += "$($property.Name) = $($property.Value)"
130119
}
131120
}
132-
if ($useWhere)
133-
{
134-
$query += $where
135-
}
136-
Write-Trace -Level Trace -message "Query: $query"
137-
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query -ErrorAction Stop
138121
}
139-
else
122+
if ($useWhere)
140123
{
141-
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname -ErrorAction Stop
124+
$query += $where
142125
}
126+
Write-Trace -Level Trace -message "Query: $query"
127+
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query -ErrorAction Stop
128+
}
129+
else
130+
{
131+
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname -ErrorAction Stop
132+
}
143133

144-
if ($wmi_instances)
145-
{
146-
$instance_result = @{}
147-
# TODO: for a `Get`, they key property must be provided so a specific instance is returned rather than just the first
148-
$wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances
149-
$wmi_instance.psobject.properties | %{
150-
if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim")))
134+
if ($wmi_instances)
135+
{
136+
$instance_result = @{}
137+
# TODO: for a `Get`, they key property must be provided so a specific instance is returned rather than just the first
138+
$wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances
139+
$wmi_instance.psobject.properties | %{
140+
if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim")))
141+
{
142+
if ($r.properties)
151143
{
152-
if ($r.properties)
153-
{
154-
if ($r.properties.psobject.properties.name -contains $_.Name)
155-
{
156-
$instance_result[$_.Name] = $_.Value
157-
}
158-
}
159-
else
144+
if ($r.properties.psobject.properties.name -contains $_.Name)
160145
{
161146
$instance_result[$_.Name] = $_.Value
162147
}
163148
}
149+
else
150+
{
151+
$instance_result[$_.Name] = $_.Value
152+
}
164153
}
165-
166-
$result += @($instance_result)
167154
}
155+
156+
$result += @($instance_result)
168157
}
169158
}
170-
else
171-
{
172-
Write-Trace -Level Error -message "Operation requires a configuration object as input"
173-
exit 1
174-
}
175159

176160
$result | ConvertTo-Json -Compress
177161
}

0 commit comments

Comments
 (0)