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

Service-related docs, bugs, integration tests #463

Merged
merged 4 commits into from
Feb 17, 2016
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
39 changes: 39 additions & 0 deletions docs/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3875,6 +3875,45 @@ The following examples show how to use this InSpec audit resource.
end


**Test the runlevels for Sys-V services**

On targets using Sys-V services, the existing runlevels can also be checked:

.. code-block:: ruby

describe service('sshd').runlevels do
its('keys') { should include(2) }
end

describe service('sshd').runlevels(2,4) do
it { should be_enabled }
end

**Override the service manager**

Under some circumstances, it may be required to override the logic in place to select the right service manager. For example, if you want to check a service managed by Upstart, you can explicitly do so:

.. code-block:: ruby

describe upstart_service('service') do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be 'runit_service'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. I'd changed it halfway... and not everywhere. thanks

it { should_not be_enabled }
it { should be_installed }
it { should be_running }
end

This is also possible with `systemd_service`, `runit_service`, `sysv_service`, `bsd_service`, and `launchd_service`.
You can also provide the control command, for when it is not to be found at the default location.
For example, if your `sv` command for services managed by Runit is not in PATH:

.. code-block:: ruby

describe runit_service('service', '/opt/chef/embedded/sbin/sv') do
it { should be_enabled }
it { should be_installed }
it { should be_running }
end


ssh_config
=====================================================
Use the ``ssh_config`` |inspec resource| to test |openssh| |ssh| client configuration data located at ``/etc/ssh/ssh_config`` on |linux| and |unix| platforms.
Expand Down
14 changes: 7 additions & 7 deletions lib/resources/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def initialize(inspec, service_ctl = nil)
# @see: http://www.freedesktop.org/software/systemd/man/systemd-system.conf.html
class Systemd < ServiceManager
def initialize(inspec, service_ctl = nil)
@service_ctl ||= 'systemctl'
@service_ctl = service_ctl || 'systemctl'
super
end

Expand Down Expand Up @@ -270,7 +270,7 @@ def enabled_inittab?
# @see: http://upstart.ubuntu.com
class Upstart < ServiceManager
def initialize(service_name, service_ctl = nil)
@service_ctl ||= 'initctl'
@service_ctl = service_ctl || 'initctl'
super
end

Expand Down Expand Up @@ -334,7 +334,7 @@ class SysV < ServiceManager
RUNLEVELS = { 0=>false, 1=>false, 2=>false, 3=>false, 4=>false, 5=>false, 6=>false }.freeze

def initialize(service_name, service_ctl = nil)
@service_ctl ||= 'service'
@service_ctl = service_ctl || 'service'
super
end

Expand Down Expand Up @@ -386,7 +386,7 @@ def info(service_name)
# @see: https://www.freebsd.org/cgi/man.cgi?query=rc.conf&sektion=5
class BSDInit < ServiceManager
def initialize(service_name, service_ctl = nil)
@service_ctl ||= 'service'
@service_ctl = service_ctl || 'service'
super
end

Expand Down Expand Up @@ -423,7 +423,7 @@ def info(service_name)

class Runit < ServiceManager
def initialize(service_name, service_ctl = nil)
@service_ctl ||= 'sv'
@service_ctl = service_ctl || 'sv'
super
end

Expand Down Expand Up @@ -452,7 +452,7 @@ def info(service_name)
# new launctl on macos 10.10
class LaunchCtl < ServiceManager
def initialize(service_name, service_ctl = nil)
@service_ctl ||= 'launchctl'
@service_ctl = service_ctl || 'launchctl'
super
end

Expand Down Expand Up @@ -562,7 +562,7 @@ def service_running?(service)
# Solaris services
class Svcs < ServiceManager
def initialize(service_name, service_ctl = nil)
@service_ctl ||= 'svcs'
@service_ctl = service_ctl || 'svcs'
super
end

Expand Down
6 changes: 4 additions & 2 deletions test/integration/cookbooks/os_prepare/recipes/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

when 'centos'
# install runit for alternative service mgmt
include_recipe 'os_prepare::_runit_service_centos'
include_recipe 'os_prepare::_upstart_service_centos'
if node['platform_version'].to_i >= 6
include_recipe 'os_prepare::_runit_service_centos'
include_recipe 'os_prepare::_upstart_service_centos'
end
end
17 changes: 16 additions & 1 deletion test/integration/test/integration/default/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
end

# extra tests for alt. runit on centos with runit_service
if os[:family] == 'centos'
if os[:family] == 'centos' && os[:release].to_i >= 6
describe runit_service('running-runit-service') do
it { should be_enabled }
it { should be_installed }
Expand Down Expand Up @@ -103,3 +103,18 @@
it { should_not be_running }
end
end

# extra tests for sys-v runlevels
if os[:family] == 'centos' && os[:release].to_i <= 6
describe service('sshd').runlevels do
its('keys') { should include(2) }
end

describe service('sshd').runlevels(2, 4) do
it { should be_enabled }
end

describe service('sshd').runlevels(0, 1) do
it { should_not be_enabled }
end
end