mirror of
https://github.com/inspec/inspec
synced 2024-11-10 07:04:15 +00:00
wmi unit test
This commit is contained in:
parent
f97301882e
commit
cd57b26bd0
5 changed files with 76 additions and 9 deletions
|
@ -5,6 +5,10 @@
|
|||
require 'utils/object_traversal'
|
||||
|
||||
module Inspec::Resources
|
||||
# This resource simplifies the access to wmi
|
||||
# on CLI you would use:
|
||||
# WMIC /NAMESPACE:\\root\rsop\computer PATH RSOP_SecuritySettingNumeric WHERE "KeyName = 'MinimumPasswordAge' And precedence=1" GET Setting
|
||||
# We use Get-WmiObject via Powershell to retrieve all values.
|
||||
class WMI < Inspec.resource(1)
|
||||
name 'wmi'
|
||||
desc 'request wmi information'
|
||||
|
@ -18,24 +22,26 @@ module Inspec::Resources
|
|||
"
|
||||
|
||||
include ObjectTraverser
|
||||
|
||||
# This would be the same as:
|
||||
# WMIC /NAMESPACE:\\root\rsop\computer PATH RSOP_SecuritySettingNumeric WHERE "KeyName = 'MinimumPasswordAge' And precedence=1" GET Setting
|
||||
attr_accessor :content
|
||||
|
||||
def initialize(wmiclass, opts = {})
|
||||
# verify that this resource is only supported on Windows
|
||||
return skip_resource 'The `windows_feature` resource is not supported on your OS.' unless inspec.os.windows?
|
||||
|
||||
@wmiclass = wmiclass
|
||||
@wminamespace = opts[:namespace]
|
||||
@wmifilter = opts[:filter]
|
||||
|
||||
# verify that this resource is only supported on Windows
|
||||
return skip_resource 'The `windows_feature` resource is not supported on your OS.' unless inspec.os.windows?
|
||||
end
|
||||
|
||||
# returns nil, if not existant or value
|
||||
def method_missing(*keys)
|
||||
# catch bahavior of rspec its implementation
|
||||
# catch behavior of rspec its implementation
|
||||
# @see https://github.com/rspec/rspec-its/blob/master/lib/rspec/its.rb#L110
|
||||
keys.shift if keys.is_a?(Array) && keys[0] == :[]
|
||||
|
||||
# map all symbols to strings
|
||||
keys = keys.map(&:to_s) if keys.is_a?(Array)
|
||||
|
||||
value(keys)
|
||||
end
|
||||
|
||||
|
@ -47,6 +53,10 @@ module Inspec::Resources
|
|||
return @content if defined?(@content)
|
||||
@content = {}
|
||||
|
||||
# we should abort execution, if wmi class is not given or wmi resource is
|
||||
# executed on a non-windows system
|
||||
return @content if @wmiclass.nil?
|
||||
|
||||
# optional params
|
||||
cmd_namespace = "-namespace #{@wminamespace}" unless @wminamespace.nil?
|
||||
cmd_filter = "-filter \"#{@wmifilter}\"" unless @wmifilter.nil?
|
||||
|
@ -54,7 +64,7 @@ module Inspec::Resources
|
|||
# run wmi command
|
||||
cmd = inspec.command("Get-WmiObject -class #{@wmiclass} #{cmd_namespace} #{cmd_filter} | ConvertTo-Json")
|
||||
@content = JSON.parse(cmd.stdout)
|
||||
rescue JSON::ParserError => e
|
||||
rescue JSON::ParserError => _e
|
||||
@content
|
||||
end
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
module ObjectTraverser
|
||||
def extract_value(keys, value)
|
||||
key = keys.shift
|
||||
return nil if key.nil?
|
||||
return nil if key.nil? || value.nil?
|
||||
|
||||
# if value is an array, iterate over each child
|
||||
if value.is_a?(Array)
|
||||
|
|
|
@ -237,6 +237,8 @@ class MockLoader
|
|||
'netstat -an -f inet -f inet6' => cmd.call('s11-netstat-an-finet-finet6'),
|
||||
# xinetd configuration
|
||||
'find /etc/xinetd.d -type f' => cmd.call('find-xinetd.d'),
|
||||
# wmi test
|
||||
"Get-WmiObject -class win32_service -filter \"name like '%winrm%'\" | ConvertTo-Json" => cmd.call('get-wmiobject'),
|
||||
}
|
||||
|
||||
@backend
|
||||
|
|
10
test/unit/mock/cmd/get-wmiobject
Normal file
10
test/unit/mock/cmd/get-wmiobject
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"Path": {
|
||||
"ClassName": "Win32_Service"
|
||||
},
|
||||
"Caption": "Windows Remote Management (WS-Management)",
|
||||
"CreationClassName": "Win32_Service",
|
||||
"DisplayName": "Windows Remote Management (WS-Management)",
|
||||
"Name": "WinRM",
|
||||
"PathName": "C:\\Windows\\System32\\svchost.exe -k NetworkService"
|
||||
}
|
45
test/unit/resources/wmi_test.rb
Normal file
45
test/unit/resources/wmi_test.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
# encoding: utf-8
|
||||
# author: Christoph Hartmann
|
||||
# author: Dominik Richter
|
||||
|
||||
require 'helper'
|
||||
require 'inspec/resource'
|
||||
|
||||
describe 'Inspec::Resources::WMI' do
|
||||
|
||||
# Check the following as unit test
|
||||
# describe wmi('win32_service', {
|
||||
# filter: "name like '%winrm%'"
|
||||
# }) do
|
||||
# its(['Path','ClassName']) { should eq 'Win32_Service' }
|
||||
# its('DisplayName') { should eq 'Windows Remote Management (WS-Management)'}
|
||||
# end
|
||||
|
||||
# windows
|
||||
it 'verify wmi parsing on windows' do
|
||||
resource = MockLoader.new(:windows).load_resource('wmi', 'win32_service', { filter: "name like '%winrm%'" })
|
||||
_(resource.send('DisplayName')).must_equal 'Windows Remote Management (WS-Management)'
|
||||
_(resource.send('method_missing', 'Path', 'ClassName')).must_equal 'Win32_Service'
|
||||
end
|
||||
|
||||
# ubuntu 14.04 with upstart
|
||||
it 'fail wmi on ubuntu' do
|
||||
resource = MockLoader.new(:ubuntu1404).load_resource('wmi', 'win32_service', { filter: "name like '%winrm%'" })
|
||||
_(resource.send('DisplayName')).must_equal nil
|
||||
_(resource.send('method_missing', 'Path', 'ClassName')).must_equal nil
|
||||
end
|
||||
|
||||
# centos 7 with systemd
|
||||
it 'fail wmi on centos' do
|
||||
resource = MockLoader.new(:centos7).load_resource('wmi', 'win32_service', { filter: "name like '%winrm%'" })
|
||||
_(resource.send('DisplayName')).must_equal nil
|
||||
_(resource.send('method_missing', 'Path', 'ClassName')).must_equal nil
|
||||
end
|
||||
|
||||
# unknown OS
|
||||
it 'fail wmi on unknown os' do
|
||||
resource = MockLoader.new(:undefined).load_resource('wmi', 'win32_service', { filter: "name like '%winrm%'" })
|
||||
_(resource.send('DisplayName')).must_equal nil
|
||||
_(resource.send('method_missing', 'Path', 'ClassName')).must_equal nil
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue