Add version method to kernel_module

Signed-off-by: Andrey Aleksandrov <postgred@gmail.com>
This commit is contained in:
Andrey Aleksandrov 2017-01-25 23:47:39 +03:00
parent 185d1185fd
commit 3783357e50
No known key found for this signature in database
GPG key ID: 9D6E8C96EDFE7CB7
5 changed files with 24 additions and 0 deletions

View file

@ -49,6 +49,12 @@ The `be_loaded` matcher tests if the module is a loadable kernel module:
<%= partial "/shared/matcher_match" %>
### version
The `version` matcher tests if the named module version is on the system:
its(:version) { should eq '3.2.2' }
## Examples
The following examples show how to use this InSpec audit resource.
@ -57,4 +63,5 @@ The following examples show how to use this InSpec audit resource.
describe kernel_module('bridge') do
it { should be_loaded }
its(:version) { should cmp >= '2.2.2' }
end

View file

@ -36,6 +36,16 @@ module Inspec::Resources
!found.nil?
end
def version
# default modinfo command
modinfo_cmd = "modinfo -F version #{@module}"
# command for CentOS 5 and sudo
modinfo_cmd = "/sbin/modinfo -F version #{@module}" if inspec.os[:name] == 'centos' && inspec.os[:release].to_i == 5
cmd = inspec.command(modinfo_cmd)
cmd.exit_status.zero? ? cmd.stdout.gsub("\n", '') : false
end
def to_s
"Kernel Module #{@module}"
end

View file

@ -269,6 +269,7 @@ class MockLoader
"schtasks /query /v /fo csv /tn 'does-not-exist' | ConvertFrom-Csv | Select @{N='URI';E={$_.TaskName}},@{N='State';E={$_.Status.ToString()}},'Logon Mode','Last Result','Task To Run','Run As User','Scheduled Task State' | ConvertTo-Json -Compress" => cmd.call('schtasks-error'),
# windows_task exist
"schtasks /query /v /fo csv /tn 'WeLovePizza' | ConvertFrom-Csv | Select @{N='URI';E={$_.TaskName}},@{N='State';E={$_.Status.ToString()}},'Logon Mode','Last Result','Task To Run','Run As User','Scheduled Task State' | ConvertTo-Json -Compress" => cmd.call('schtasks-success'),
'modinfo -F version dhcp' => cmd.call('modinfo-f-version-dhcp')
}
@backend

View file

@ -0,0 +1 @@
3.2.2

View file

@ -20,4 +20,9 @@ describe 'Inspec::Resources::KernelModule' do
resource = load_resource('kernel_module', 'dhcp')
_(resource.loaded?).must_equal false
end
it 'verify kernel_module version' do
resource = load_resource('kernel_module', 'dhcp')
_(resource.version).must_equal '3.2.2'
end
end