Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version method for kernel_module #1435

Merged
merged 2 commits into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/resources/kernel_module.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
11 changes: 11 additions & 0 deletions lib/resources/kernel_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def loaded?
!found.nil?
end

def version
if inspec.os[:name] == 'centos' && inspec.os[:release].to_i == 5
modinfo_cmd = "/sbin/modinfo -F version #{@module}"
else
modinfo_cmd = "modinfo -F version #{@module}"
end

cmd = inspec.command(modinfo_cmd)
cmd.exit_status.zero? ? cmd.stdout.delete("\n") : nil
end

def to_s
"Kernel Module #{@module}"
end
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def md.directory?
"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
Expand Down
1 change: 1 addition & 0 deletions test/unit/mock/cmd/modinfo-f-version-dhcp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
5 changes: 5 additions & 0 deletions test/unit/resources/kernel_module_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@
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