Merge pull request #32 from chef/kernel

add linux kernel resources
This commit is contained in:
Christoph Hartmann 2015-09-21 13:08:32 +02:00 committed by Dominik Richter
commit 2eeba59db3
8 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,33 @@
# encoding: utf-8
# license: All rights reserved
# Verifies if a kernel module is loaded
# Usage:
# describe kernel_module('bridge') do
# it { should be_loaded }
# end
class KernelModule < Vulcano.resource(1)
name 'kernel_module'
def initialize(modulename = nil)
@module = modulename
# this resource is only supported on Linux
return skip_resource 'The `kernel_module` resource is not supported on your OS.' if !%w{ubuntu debian redhat fedora arch}.include? vulcano.os[:family]
end
def loaded?
# get list of all modules
cmd = vulcano.run_command('lsmod')
return false if cmd.exit_status != 0
# check if module is loaded
re = Regexp.new('^'+Regexp.quote(@module)+'\s')
found = cmd.stdout.match(re)
!found.nil?
end
def to_s
"Kernel Module #{@module}"
end
end

View file

@ -0,0 +1,54 @@
# encoding: utf-8
# license: All rights reserved
# Verifies if a kernel parameter is set
# describe kernel_parameter('net.ipv4.conf.all.forwarding') do
# its(:value) { should eq 0 }
# end
class KernelParameter < Vulcano.resource(1)
name 'kernel_parameter'
def initialize(parameter = nil)
@parameter = parameter
# this resource is only supported on Linux
return skip_resource 'The `kernel_module` resource is not supported on your OS.' if !%w{ubuntu debian redhat fedora arch}.include? vulcano.os[:family]
end
def value
cmd = vulcano.run_command("/sbin/sysctl -q -n #{@parameter}")
return nil if cmd.exit_status != 0
# remove whitespace
cmd = cmd.stdout.chomp.strip
# convert to number if possible
cmd = cmd.to_i if cmd.match(/^\d+$/)
cmd
end
def to_s
"Kernel Parameter #{@parameter}"
end
end
# for compatability with serverspec
# this is deprecated syntax and will be removed in future versions
class LinuxKernelParameter < KernelParameter
name 'linux_kernel_parameter'
def initialize(parameter)
super(parameter)
end
def value
deprecated
super()
end
def deprecated
warn '[DEPRECATION] `linux_kernel_parameter(parameter)` is deprecated. Please use `kernel_parameter(parameter)` instead.'
end
def to_s
"Kernel Parameter #{@parameter}"
end
end

View file

@ -30,6 +30,8 @@ require 'resources/file'
require 'resources/gem'
require 'resources/group_policy'
require 'resources/inetd_conf'
require 'resources/kernel_module'
require 'resources/kernel_parameter'
require 'resources/limits_conf'
require 'resources/login_def'
require 'resources/mysql'

View file

@ -70,6 +70,8 @@ def loadResource (resource, *args)
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call('get-package'),
"New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name dhcp| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json" => cmd.call('get-service-dhcp'),
"Get-WindowsFeature | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" => cmd.call('get-windows-feature'),
'lsmod' => cmd.call('lsmod'),
'/sbin/sysctl -q -n net.ipv4.conf.all.forwarding' => cmd.call('sbin_sysctl'),
}
# load resource

2
test/unit/mock/cmd/lsmod Normal file
View file

@ -0,0 +1,2 @@
Module Size Used by
bridge 73728 1 br_netfilter

View file

@ -0,0 +1 @@
1

View file

@ -0,0 +1,21 @@
# encoding: utf-8
require 'helper'
require 'vulcano/resource'
describe 'Vulcano::Resources::KernelModule' do
it 'verify kernel_module parsing' do
resource = loadResource('kernel_module', 'bridge')
_(resource.loaded?).must_equal true
end
it 'verify kernel_module parsing' do
resource = loadResource('kernel_module', 'bridges')
_(resource.loaded?).must_equal false
end
it 'verify kernel_module parsing' do
resource = loadResource('kernel_module', 'dhcp')
_(resource.loaded?).must_equal false
end
end

View file

@ -0,0 +1,15 @@
# encoding: utf-8
require 'helper'
require 'vulcano/resource'
describe 'Vulcano::Resources::KernelParameter' do
describe 'kernel_parameter' do
let(:resource) { loadResource('kernel_parameter', 'net.ipv4.conf.all.forwarding') }
it 'verify kernel_parameter parsing' do
_(resource.value).must_equal 1
end
end
end