Merge pull request #5314 from inspec/ap-rm/wmi-fix

Return arrays for wmi properties
This commit is contained in:
Nick Schwaderer 2020-12-02 10:16:14 +00:00 committed by GitHub
commit a32dfb479b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 13 deletions

View file

@ -16,7 +16,10 @@ module Inspec::Resources
namespace: 'root\\rsop\\computer',
filter: 'KeyName = \'MinimumPasswordAge\' And precedence=1'
}) do
its('Setting') { should eq true }
its('Setting') { should cmp true }
end
describe wmi({namespace: "root\\cimv2", query: "SELECT installstate FROM win32_optionalfeature"}) do
its("installstate") { should include 2 }
end
EXAMPLE
@ -66,13 +69,18 @@ module Inspec::Resources
# run wmi command and filter empty wmi
script = <<-EOH
Filter Aggregate
{
$arr = @{}
$_.properties | % {
$arr.Add($_.name, $_.value)
Function Aggregate {
$propsHash = @{}
ForEach ($wmiObj in $Input) {
ForEach ($wmiProp in $wmiObj.properties) {
If($propsHash.ContainsKey($wmiProp.name)) {
$propsHash[$wmiProp.name].add($wmiProp.value) | Out-Null
} Else {
$propsHash[$wmiProp.name] = [System.Collections.ArrayList]@($wmiProp.value)
}
}
$arr
}
$propsHash
}
Get-WmiObject #{params} | Aggregate | ConvertTo-Json
EOH

View file

@ -191,6 +191,11 @@ class MockLoader
mock.mock_command("", "", stderr, 1)
}
# DEV NOTES: Most of the key=>value pairs below represent inspec commands=>responses to mock in testing.
# "cf04ce5615167da0133540398aa9989bf48b3d15a615f08f97eafaeec6e5b2ba" => cmd.call("get-wmiobject"),
# In this ^^^ case, the key is the sha256sum of the script that is sent to the 'inspec.powershell' method in resources/wmi.rb
# And the content of 'get-wmiobject' can be found in this file: 'test/fixtures/cmd/get-wmiobject'. If you change the script
# that the inspec resource sends, you have to calculate the new sha256sum of it and update it here
mock_cmds = {
"" => empty.call,
"sh -c 'find /no/such/mock -type f -maxdepth 1'" => empty.call,
@ -375,7 +380,7 @@ class MockLoader
# xinetd configuration
"find /etc/xinetd.d -type f" => cmd.call("find-xinetd.d"),
# wmi test
"2979ebeb80a475107d85411f109209a580ccf569071b3dc7acff030b8635c6b9" => cmd.call("get-wmiobject"),
"cf04ce5615167da0133540398aa9989bf48b3d15a615f08f97eafaeec6e5b2ba" => cmd.call("get-wmiobject"),
# user info on hpux
"logins -x -l root" => cmd.call("logins-x"),
# packages on hpux

View file

@ -26,7 +26,7 @@ describe wmi({
namespace: 'root\\rsop\\computer',
filter: 'KeyName = \'MinimumPasswordAge\' And precedence=1'
}) do
its('Setting') { should eq 1 }
its('Setting') { should eq [1] }
end
# new syntax
@ -34,14 +34,14 @@ describe wmi({
namespace: 'root\rsop\computer',
query: "SELECT Setting FROM RSOP_SecuritySettingBoolean WHERE KeyName='LSAAnonymousNameLookup' AND Precedence=1"
}) do
its('Setting') { should eq false }
its('Setting') { should cmp false }
end
describe wmi({
namespace: 'root\cimv2',
query: 'SELECT filesystem FROM win32_logicaldisk WHERE drivetype=3'
}).params.values.join do
it { should eq 'NTFS' }
it { should cmp 'NTFS' }
end
# deprecated syntax
@ -53,8 +53,8 @@ describe wmi('RSOP_SecuritySettingNumeric', {
namespace: 'root\\rsop\\computer',
filter: 'KeyName = \'MinimumPasswordAge\' And precedence=1'
}) do
its('Setting') { should eq 1 }
its('setting') { should eq 1 }
its('Setting') { should cmp 1 }
its('setting') { should include 1 }
end
describe wmi('win32_service', {