mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
Merge pull request #50 from chef/service
Unit test for service resource
This commit is contained in:
commit
a7fffe4472
12 changed files with 192 additions and 32 deletions
|
@ -38,21 +38,21 @@ class Service < Vulcano.resource(1)
|
|||
# Upstart runs with PID 1 as /sbin/init.
|
||||
# Systemd runs with PID 1 as /lib/systemd/systemd.
|
||||
when 'ubuntu'
|
||||
version = os[:release].to_f
|
||||
version = vulcano.os[:release].to_f
|
||||
if version < 15.04
|
||||
@service_mgmt = Upstart.new(vulcano)
|
||||
else
|
||||
@service_mgmt = Systemd.new(vulcano)
|
||||
end
|
||||
when 'debian'
|
||||
version = os[:release].to_i
|
||||
if version >= 7
|
||||
version = vulcano.os[:release].to_i
|
||||
if version > 7
|
||||
@service_mgmt = Systemd.new(vulcano)
|
||||
else
|
||||
@service_mgmt = SysV.new(vulcano)
|
||||
end
|
||||
when 'redhat', 'fedora'
|
||||
version = os[:release].to_i
|
||||
version = vulcano.os[:release].to_i
|
||||
if (family == 'redhat' && version >= 7) || (family == 'fedora' && version >= 15)
|
||||
@service_mgmt = Systemd.new(vulcano)
|
||||
else
|
||||
|
@ -64,6 +64,8 @@ class Service < Vulcano.resource(1)
|
|||
@service_mgmt = WindowsSrv.new(vulcano)
|
||||
when 'freebsd'
|
||||
@service_mgmt = BSDInit.new(vulcano)
|
||||
when 'arch'
|
||||
@service_mgmt = Systemd.new(vulcano)
|
||||
end
|
||||
|
||||
return skip_resource 'The `service` resource is not supported on your OS yet.' if @service_mgmt.nil?
|
||||
|
@ -71,6 +73,7 @@ class Service < Vulcano.resource(1)
|
|||
|
||||
def info
|
||||
return @cache if !@cache.nil?
|
||||
return nil if @service_mgmt.nil?
|
||||
@cache = @service_mgmt.info(@service_name)
|
||||
end
|
||||
|
||||
|
@ -112,7 +115,7 @@ class Systemd < ServiceManager
|
|||
# parse data
|
||||
params = SimpleConfig.new(
|
||||
cmd.stdout.chomp,
|
||||
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
||||
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
|
||||
multiple_values: false,
|
||||
).params
|
||||
|
||||
|
@ -120,7 +123,7 @@ class Systemd < ServiceManager
|
|||
params['LoadState'] == 'loaded' ? (installed = true) : (installed = false)
|
||||
# test via 'systemctl is-active service'
|
||||
# SubState values running
|
||||
params['LoadState'] == 'running' ? (running = true) : (running = false)
|
||||
params['SubState'] == 'running' ? (running = true) : (running = false)
|
||||
# test via systemctl --quiet is-enabled
|
||||
# ActiveState values eg.g inactive, active
|
||||
params['ActiveState'] == 'active' ? (enabled = true) : (enabled = false)
|
||||
|
@ -157,7 +160,7 @@ class Upstart < ServiceManager
|
|||
|
||||
{
|
||||
name: service_name,
|
||||
description: '',
|
||||
description: nil,
|
||||
installed: true,
|
||||
running: running,
|
||||
enabled: enabled,
|
||||
|
@ -169,20 +172,24 @@ end
|
|||
class SysV < ServiceManager
|
||||
def info(service_name)
|
||||
# check if service is installed
|
||||
filename = "/etc/init.d/#{service_name}"
|
||||
service = @vulcano.file(filename)
|
||||
# read all available services via ls /etc/init.d/
|
||||
srvlist = @vulcano.run_command('ls -1 /etc/init.d/')
|
||||
return nil if srvlist.exit_status != 0
|
||||
|
||||
# check if service is installed
|
||||
return nil if !service.exist?
|
||||
# check if the service is in list
|
||||
service = srvlist.stdout.split("\n").select { |srv| srv == service_name }
|
||||
|
||||
# check if service is enabled
|
||||
configfile = "/etc/init/#{service_name}.conf"
|
||||
config = @vulcano.file(configfile)
|
||||
enabled = false
|
||||
if !config.nil?
|
||||
match_enabled = /^\s*start on/.match(config.content)
|
||||
!match_enabled.nil? ? (enabled = true) : (enabled = false)
|
||||
end
|
||||
# abort if we could not find any service
|
||||
return nil if service.empty?
|
||||
|
||||
# read all enabled services from runlevel
|
||||
# on rhel via: 'chkconfig --list', is not installed by default
|
||||
# bash: for i in `find /etc/rc*.d -name S*`; do basename $i | sed -r 's/^S[0-9]+//'; done | sort | uniq
|
||||
enabled_services_cmd = @vulcano.run_command('find /etc/rc*.d -name S*')
|
||||
enabled_services = enabled_services_cmd.stdout.split("\n").select { |line|
|
||||
/(^.*#{service_name}.*)/.match(line)
|
||||
}
|
||||
enabled_services.empty? ? enabled = false : enabled = true
|
||||
|
||||
# check if service is really running
|
||||
# service throws an exit code if the service is not installed or
|
||||
|
@ -192,7 +199,7 @@ class SysV < ServiceManager
|
|||
|
||||
{
|
||||
name: service_name,
|
||||
description: '',
|
||||
description: nil,
|
||||
installed: true,
|
||||
running: running,
|
||||
enabled: enabled,
|
||||
|
@ -221,12 +228,12 @@ class BSDInit < ServiceManager
|
|||
|
||||
# check if the service is running
|
||||
# if the service is not available or not running, we always get an error code
|
||||
cmd = @vulcano.run_command("service #{service_name} status")
|
||||
cmd = @vulcano.run_command("service #{service_name} onestatus")
|
||||
cmd.exit_status == 0 ? (running = true) : (running = false)
|
||||
|
||||
{
|
||||
name: service_name,
|
||||
description: '',
|
||||
description: nil,
|
||||
installed: true,
|
||||
running: running,
|
||||
enabled: enabled,
|
||||
|
@ -255,8 +262,11 @@ class LaunchCtl < ServiceManager
|
|||
pid = parsed_srv[0]
|
||||
!pid.nil? ? (running = true) : (running = false)
|
||||
|
||||
# extract service label
|
||||
srv = parsed_srv[3] || service_name
|
||||
|
||||
{
|
||||
name: service_name,
|
||||
name: srv,
|
||||
description: nil,
|
||||
installed: true,
|
||||
running: running,
|
||||
|
|
|
@ -31,6 +31,7 @@ class MockLoader
|
|||
osx104: { family: 'darwin', release: '10.10.4', arch: nil, name: 'mac_os_x' },
|
||||
ubuntu1204: { family: 'ubuntu', release: '12.04', arch: 'x86_64' },
|
||||
ubuntu1404: { family: 'ubuntu', release: '14.04', arch: 'x86_64' },
|
||||
ubuntu1504: { family: 'ubuntu', release: '15.04', arch: 'x86_64' },
|
||||
windows: { family: 'windows', release: nil, arch: nil },
|
||||
undefined: { family: nil, release: nil, arch: nil },
|
||||
}
|
||||
|
@ -98,11 +99,31 @@ class MockLoader
|
|||
"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'),
|
||||
# ports on windows
|
||||
'Get-NetTCPConnection | Select-Object -Property State, Caption, Description, LocalAddress, LocalPort, RemoteAddress, RemotePort, DisplayName, Status | ConvertTo-Json' => cmd.call('get-net-tcpconnection'),
|
||||
# ports on mac
|
||||
'lsof -nP -iTCP -iUDP -sTCP:LISTEN' => cmd.call('lsof-np-itcp'),
|
||||
# ports on linux
|
||||
'netstat -tulpen' => cmd.call('netstat-tulpen'),
|
||||
# ports on freebsd
|
||||
'sockstat -46l' => cmd.call('sockstat'),
|
||||
# packages on windows
|
||||
"Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq 'Microsoft Visual C++ 2008 Redistributable - x64 9.0.30729.6161'} | Select-Object -Property Name,Version,Vendor,PackageCode,Caption,Description | ConvertTo-Json" => cmd.call('win32_product'),
|
||||
# service status upstart on ubuntu
|
||||
'initctl status ssh' => cmd.call('initctl-status-ssh'),
|
||||
# service config for upstart on ubuntu
|
||||
'initctl show-config ssh' => cmd.call('initctl-show-config-ssh'),
|
||||
# show ssh service Centos 7
|
||||
'systemctl show --all sshd' => cmd.call('systemctl-show-all-sshd'),
|
||||
# services on macos
|
||||
'launchctl list' => cmd.call('launchctl-list'),
|
||||
# services on freebsd 10
|
||||
'service -e' => cmd.call('service-e'),
|
||||
'service sendmail onestatus' => cmd.call('service-sendmail-onestatus'),
|
||||
# services for system 5 e.g. centos6, debian 6
|
||||
'service sshd status' => cmd.call('service-sshd-status'),
|
||||
'find /etc/rc*.d -name S*' => cmd.call('find-etc-rc-d-name-S'),
|
||||
'ls -1 /etc/init.d/' => cmd.call('ls-1-etc-init.d'),
|
||||
}
|
||||
|
||||
# set os emulation
|
||||
|
|
12
test/unit/mock/cmd/find-etc-rc-d-name-S
Normal file
12
test/unit/mock/cmd/find-etc-rc-d-name-S
Normal file
|
@ -0,0 +1,12 @@
|
|||
/etc/rc.d/rc0.d/S01halt
|
||||
/etc/rc.d/rc0.d/S00killall
|
||||
/etc/rc.d/rc2.d/S90crond
|
||||
/etc/rc.d/rc2.d/S55sshd
|
||||
/etc/rc.d/rc5.d/S90crond
|
||||
/etc/rc.d/rc5.d/S55sshd
|
||||
/etc/rc.d/rc1.d/S99single
|
||||
/etc/rc.d/rc3.d/S90crond
|
||||
/etc/rc.d/rc3.d/S55sshd
|
||||
/etc/rc.d/rc4.d/S08ip6tables
|
||||
/etc/rc.d/rc4.d/S90crond
|
||||
/etc/rc.d/rc4.d/S55sshd
|
3
test/unit/mock/cmd/initctl-show-config-ssh
Normal file
3
test/unit/mock/cmd/initctl-show-config-ssh
Normal file
|
@ -0,0 +1,3 @@
|
|||
ssh
|
||||
start on (filesystem or runlevel [2345])
|
||||
stop on runlevel [!2345]
|
1
test/unit/mock/cmd/initctl-status-ssh
Normal file
1
test/unit/mock/cmd/initctl-status-ssh
Normal file
|
@ -0,0 +1 @@
|
|||
ssh start/running, process 1291
|
3
test/unit/mock/cmd/launchctl-list
Normal file
3
test/unit/mock/cmd/launchctl-list
Normal file
|
@ -0,0 +1,3 @@
|
|||
PID Status Label
|
||||
2892 0 org.openbsd.ssh-agent
|
||||
- 0 com.apple.FilesystemUI
|
2
test/unit/mock/cmd/ls-1-etc-init.d
Normal file
2
test/unit/mock/cmd/ls-1-etc-init.d
Normal file
|
@ -0,0 +1,2 @@
|
|||
crond
|
||||
sshd
|
2
test/unit/mock/cmd/service-e
Normal file
2
test/unit/mock/cmd/service-e
Normal file
|
@ -0,0 +1,2 @@
|
|||
/etc/rc.d/sshd
|
||||
/etc/rc.d/sendmail
|
3
test/unit/mock/cmd/service-sendmail-onestatus
Normal file
3
test/unit/mock/cmd/service-sendmail-onestatus
Normal file
|
@ -0,0 +1,3 @@
|
|||
sendmail is running as pid 671.
|
||||
sendmail_submit is running as pid 671.
|
||||
sendmail_msp_queue is running as pid 674.
|
1
test/unit/mock/cmd/service-sshd-status
Normal file
1
test/unit/mock/cmd/service-sshd-status
Normal file
|
@ -0,0 +1 @@
|
|||
openssh-daemon (pid 1220) is running...
|
6
test/unit/mock/cmd/systemctl-show-all-sshd
Normal file
6
test/unit/mock/cmd/systemctl-show-all-sshd
Normal file
|
@ -0,0 +1,6 @@
|
|||
Id=sshd.service
|
||||
Names=sshd.service
|
||||
Description=OpenSSH server daemon
|
||||
LoadState=loaded
|
||||
ActiveState=active
|
||||
SubState=running
|
|
@ -4,15 +4,111 @@ require 'helper'
|
|||
require 'vulcano/resource'
|
||||
|
||||
describe 'Vulcano::Resources::Service' do
|
||||
# TODO: make OS dependent
|
||||
let(:resource) { load_resource('service', 'dhcp') }
|
||||
|
||||
# windows
|
||||
# it 'verify service parsing' do
|
||||
# pkg = { name: 'dhcp', description: 'DHCP Client', installed: true, running: true, enabled: true, type: 'windows' }
|
||||
# _(resource.info).must_equal pkg
|
||||
# _(resource.installed?).must_equal true
|
||||
# _(resource.enabled?).must_equal true
|
||||
# _(resource.running?).must_equal true
|
||||
# end
|
||||
it 'verify service parsing' do
|
||||
resource = MockLoader.new(:windows).load_resource('service', 'dhcp')
|
||||
srv = { name: 'dhcp', description: 'DHCP Client', installed: true, running: true, enabled: true, type: 'windows' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# ubuntu 14.04 with upstart
|
||||
it 'verify ubuntu package parsing' do
|
||||
resource = MockLoader.new(:ubuntu1404).load_resource('service', 'ssh')
|
||||
srv = { name: 'ssh', description: nil, installed: true, running: true, enabled: true, type: 'upstart' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# ubuntu 15.04 with systemd
|
||||
it 'verify ubuntu package parsing' do
|
||||
resource = MockLoader.new(:ubuntu1504).load_resource('service', 'sshd')
|
||||
srv = { name: 'sshd.service', description: 'OpenSSH server daemon', installed: true, running: true, enabled: true, type: 'systemd' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# centos 6 with systemv
|
||||
it 'verify centos 6 package parsing' do
|
||||
resource = MockLoader.new(:centos6).load_resource('service', 'sshd')
|
||||
srv = { name: 'sshd', description: nil, installed: true, running: true, enabled: true, type: 'sysv' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# centos 7 with systemd
|
||||
it 'verify centos 7 package parsing' do
|
||||
resource = MockLoader.new(:centos7).load_resource('service', 'sshd')
|
||||
srv = { name: 'sshd.service', description: 'OpenSSH server daemon', installed: true, running: true, enabled: true, type: 'systemd' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# freebsd
|
||||
it 'verify freebsd10 package parsing' do
|
||||
resource = MockLoader.new(:freebsd10).load_resource('service', 'sendmail')
|
||||
srv = { name: 'sendmail', description: nil, installed: true, running: true, enabled: true, type: 'bsd-init' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# arch linux with systemd
|
||||
it 'verify arch linux package parsing' do
|
||||
resource = MockLoader.new(:arch).load_resource('service', 'sshd')
|
||||
srv = { name: 'sshd.service', description: 'OpenSSH server daemon', installed: true, running: true, enabled: true, type: 'systemd' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# debian 7 with systemv
|
||||
it 'verify debian 7 package parsing' do
|
||||
resource = MockLoader.new(:debian7).load_resource('service', 'sshd')
|
||||
srv = { name: 'sshd', description: nil, installed: true, running: true, enabled: true, type: 'sysv' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# debian 8 with systemd
|
||||
it 'verify arch linux package parsing' do
|
||||
resource = MockLoader.new(:debian8).load_resource('service', 'sshd')
|
||||
srv = { name: 'sshd.service', description: 'OpenSSH server daemon', installed: true, running: true, enabled: true, type: 'systemd' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# macos test
|
||||
it 'verify arch linux package parsing' do
|
||||
resource = MockLoader.new(:osx104).load_resource('service', 'ssh')
|
||||
srv = { name: 'org.openbsd.ssh-agent', description: nil, installed: true, running: true, enabled: true, type: 'darwin' }
|
||||
_(resource.info).must_equal srv
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.enabled?).must_equal true
|
||||
_(resource.running?).must_equal true
|
||||
end
|
||||
|
||||
# unknown OS
|
||||
it 'verify package handling on unsupported os' do
|
||||
resource = MockLoader.new(:undefined).load_resource('service', 'dhcp')
|
||||
_(resource.installed?).must_equal false
|
||||
_(resource.info).must_equal nil
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue