From e703ef3bf8d22f9f0a2137c73178f4210c03794b Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 10 Nov 2015 00:29:13 +0100 Subject: [PATCH 1/4] move test files to work with default test-kitchen --- test/integration/{ => test/integration}/default/_debug_spec.rb | 0 test/integration/{ => test/integration}/default/apt_spec.rb | 0 test/integration/{ => test/integration}/default/file_spec.rb | 0 test/integration/{ => test/integration}/default/group_spec.rb | 0 .../{ => test/integration}/default/kernel_module_spec.rb | 0 .../{ => test/integration}/default/kernel_parameter_spec.rb | 0 test/integration/{ => test/integration}/default/package_spec.rb | 0 test/integration/{ => test/integration}/default/service_spec.rb | 0 test/integration/{ => test/integration}/default/user_spec.rb | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename test/integration/{ => test/integration}/default/_debug_spec.rb (100%) rename test/integration/{ => test/integration}/default/apt_spec.rb (100%) rename test/integration/{ => test/integration}/default/file_spec.rb (100%) rename test/integration/{ => test/integration}/default/group_spec.rb (100%) rename test/integration/{ => test/integration}/default/kernel_module_spec.rb (100%) rename test/integration/{ => test/integration}/default/kernel_parameter_spec.rb (100%) rename test/integration/{ => test/integration}/default/package_spec.rb (100%) rename test/integration/{ => test/integration}/default/service_spec.rb (100%) rename test/integration/{ => test/integration}/default/user_spec.rb (100%) diff --git a/test/integration/default/_debug_spec.rb b/test/integration/test/integration/default/_debug_spec.rb similarity index 100% rename from test/integration/default/_debug_spec.rb rename to test/integration/test/integration/default/_debug_spec.rb diff --git a/test/integration/default/apt_spec.rb b/test/integration/test/integration/default/apt_spec.rb similarity index 100% rename from test/integration/default/apt_spec.rb rename to test/integration/test/integration/default/apt_spec.rb diff --git a/test/integration/default/file_spec.rb b/test/integration/test/integration/default/file_spec.rb similarity index 100% rename from test/integration/default/file_spec.rb rename to test/integration/test/integration/default/file_spec.rb diff --git a/test/integration/default/group_spec.rb b/test/integration/test/integration/default/group_spec.rb similarity index 100% rename from test/integration/default/group_spec.rb rename to test/integration/test/integration/default/group_spec.rb diff --git a/test/integration/default/kernel_module_spec.rb b/test/integration/test/integration/default/kernel_module_spec.rb similarity index 100% rename from test/integration/default/kernel_module_spec.rb rename to test/integration/test/integration/default/kernel_module_spec.rb diff --git a/test/integration/default/kernel_parameter_spec.rb b/test/integration/test/integration/default/kernel_parameter_spec.rb similarity index 100% rename from test/integration/default/kernel_parameter_spec.rb rename to test/integration/test/integration/default/kernel_parameter_spec.rb diff --git a/test/integration/default/package_spec.rb b/test/integration/test/integration/default/package_spec.rb similarity index 100% rename from test/integration/default/package_spec.rb rename to test/integration/test/integration/default/package_spec.rb diff --git a/test/integration/default/service_spec.rb b/test/integration/test/integration/default/service_spec.rb similarity index 100% rename from test/integration/default/service_spec.rb rename to test/integration/test/integration/default/service_spec.rb diff --git a/test/integration/default/user_spec.rb b/test/integration/test/integration/default/user_spec.rb similarity index 100% rename from test/integration/default/user_spec.rb rename to test/integration/test/integration/default/user_spec.rb From b33717fa551b31db7bc69091463340f73bf28260 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 10 Nov 2015 00:30:23 +0100 Subject: [PATCH 2/4] add test case for upstart with systemv service --- .../cookbooks/os_prepare/recipes/default.rb | 1 + .../cookbooks/os_prepare/recipes/service.rb | 12 ++++++++++++ .../test/integration/default/service_spec.rb | 9 +++++++++ 3 files changed, 22 insertions(+) create mode 100644 test/integration/cookbooks/os_prepare/recipes/service.rb diff --git a/test/integration/cookbooks/os_prepare/recipes/default.rb b/test/integration/cookbooks/os_prepare/recipes/default.rb index 711529ce2..42063c200 100644 --- a/test/integration/cookbooks/os_prepare/recipes/default.rb +++ b/test/integration/cookbooks/os_prepare/recipes/default.rb @@ -7,3 +7,4 @@ include_recipe('os_prepare::apt') include_recipe('os_prepare::file') include_recipe('os_prepare::package') +include_recipe('os_prepare::service') diff --git a/test/integration/cookbooks/os_prepare/recipes/service.rb b/test/integration/cookbooks/os_prepare/recipes/service.rb new file mode 100644 index 000000000..9fda0529b --- /dev/null +++ b/test/integration/cookbooks/os_prepare/recipes/service.rb @@ -0,0 +1,12 @@ +# encoding: utf-8 +# author: Christoph Hartmann +# author: Dominik Richter +# +# prepares services + +# install ntp as a service on ubuntu +case node['platform'] +when 'ubuntu' + include_recipe('apt') + package 'ntp' +end diff --git a/test/integration/test/integration/default/service_spec.rb b/test/integration/test/integration/default/service_spec.rb index 8009d5d44..d2ce3db16 100644 --- a/test/integration/test/integration/default/service_spec.rb +++ b/test/integration/test/integration/default/service_spec.rb @@ -26,3 +26,12 @@ describe service(available_service) do it { should be_installed } it { should be_running } end + +# extra test for ubuntu upstart with systemv service +if os[:family] == 'ubuntu' + describe service('ntp') do + it { should be_enabled } + it { should be_installed } + it { should be_running } + end +end From 7b179872bd9bb4e32c854cb2ab75fb90d92a6a45 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 10 Nov 2015 00:36:34 +0100 Subject: [PATCH 3/4] extend upstart implementation to support systemv services --- lib/resources/service.rb | 60 +++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/resources/service.rb b/lib/resources/service.rb index 156fa1e05..ef4bcd651 100644 --- a/lib/resources/service.rb +++ b/lib/resources/service.rb @@ -150,38 +150,42 @@ class Upstart < ServiceManager def info(service_name) # get the status of upstart service cmd = inspec.command("initctl status #{service_name}") - return nil if cmd.exit_status != 0 - # @see: http://upstart.ubuntu.com/cookbook/#job-states - # grep for running to indicate the service is there - match_running = /running/.match(cmd.stdout) - !match_running.nil? ? (running = true) : (running = false) + if cmd.exit_status.to_i == 0 + # @see: http://upstart.ubuntu.com/cookbook/#job-states + # grep for running to indicate the service is there + match_running = /running/.match(cmd.stdout) + !match_running.nil? ? (running = true) : (running = false) - # check if a service is enabled - # http://upstart.ubuntu.com/cookbook/#determine-if-a-job-is-disabled - # $ initctl show-config $job | grep -q "^ start on" && echo enabled || echo disabled - # Ubuntu 10.04 show-config is not supported - # @see http://manpages.ubuntu.com/manpages/maverick/man8/initctl.8.html - config = inspec.command("initctl show-config #{service_name}") - match_enabled = /^\s*start on/.match(config.stdout) - !match_enabled.nil? ? (enabled = true) : (enabled = false) + # check if a service is enabled + # http://upstart.ubuntu.com/cookbook/#determine-if-a-job-is-disabled + # $ initctl show-config $job | grep -q "^ start on" && echo enabled || echo disabled + # Ubuntu 10.04 show-config is not supported + # @see http://manpages.ubuntu.com/manpages/maverick/man8/initctl.8.html + config = inspec.command("initctl show-config #{service_name}") + match_enabled = /^\s*start on/.match(config.stdout) + !match_enabled.nil? ? (enabled = true) : (enabled = false) - # implement fallback for Ubuntu 10.04 - if inspec.os[:family] == 'ubuntu' && - inspec.os[:release].to_f >= 10.04 && - inspec.os[:release].to_f < 12.04 && - cmd.exit_status == 0 - enabled = true + # implement fallback for Ubuntu 10.04 + if inspec.os[:family] == 'ubuntu' && + inspec.os[:release].to_f >= 10.04 && + inspec.os[:release].to_f < 12.04 && + cmd.exit_status == 0 + enabled = true + end + + return { + name: service_name, + description: nil, + installed: true, + running: running, + enabled: enabled, + type: 'upstart', + } + # fallback for systemv services, those are not handled via `initctl` + else + return SysV.new(inspec).info(service_name) end - - { - name: service_name, - description: nil, - installed: true, - running: running, - enabled: enabled, - type: 'upstart', - } end end From 069075b48a91173e01cc12d83e742c992a21a19f Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Fri, 13 Nov 2015 10:30:37 +0100 Subject: [PATCH 4/4] lint --- lib/resources/service.rb | 70 +++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/lib/resources/service.rb b/lib/resources/service.rb index ef4bcd651..38e0dee55 100644 --- a/lib/resources/service.rb +++ b/lib/resources/service.rb @@ -149,43 +149,45 @@ end class Upstart < ServiceManager def info(service_name) # get the status of upstart service - cmd = inspec.command("initctl status #{service_name}") + status = inspec.command("initctl status #{service_name}") - if cmd.exit_status.to_i == 0 - # @see: http://upstart.ubuntu.com/cookbook/#job-states - # grep for running to indicate the service is there - match_running = /running/.match(cmd.stdout) - !match_running.nil? ? (running = true) : (running = false) - - # check if a service is enabled - # http://upstart.ubuntu.com/cookbook/#determine-if-a-job-is-disabled - # $ initctl show-config $job | grep -q "^ start on" && echo enabled || echo disabled - # Ubuntu 10.04 show-config is not supported - # @see http://manpages.ubuntu.com/manpages/maverick/man8/initctl.8.html - config = inspec.command("initctl show-config #{service_name}") - match_enabled = /^\s*start on/.match(config.stdout) - !match_enabled.nil? ? (enabled = true) : (enabled = false) - - # implement fallback for Ubuntu 10.04 - if inspec.os[:family] == 'ubuntu' && - inspec.os[:release].to_f >= 10.04 && - inspec.os[:release].to_f < 12.04 && - cmd.exit_status == 0 - enabled = true - end - - return { - name: service_name, - description: nil, - installed: true, - running: running, - enabled: enabled, - type: 'upstart', - } # fallback for systemv services, those are not handled via `initctl` - else - return SysV.new(inspec).info(service_name) + return SysV.new(inspec).info(service_name) if status.exit_status.to_i != 0 + + # @see: http://upstart.ubuntu.com/cookbook/#job-states + # grep for running to indicate the service is there + running = !status.stdout[/running/].nil? + + { + name: service_name, + description: nil, + installed: true, + running: running, + enabled: info_enabled(status, service_name), + type: 'upstart', + } + end + + private + + def info_enabled(status, service_name) + # check if a service is enabled + # http://upstart.ubuntu.com/cookbook/#determine-if-a-job-is-disabled + # $ initctl show-config $job | grep -q "^ start on" && echo enabled || echo disabled + # Ubuntu 10.04 show-config is not supported + # @see http://manpages.ubuntu.com/manpages/maverick/man8/initctl.8.html + config = inspec.command("initctl show-config #{service_name}") + enabled = !config.stdout[/^\s*start on/].nil? + + # implement fallback for Ubuntu 10.04 + if inspec.os[:family] == 'ubuntu' && + inspec.os[:release].to_f >= 10.04 && + inspec.os[:release].to_f < 12.04 && + status.exit_status == 0 + enabled = true end + + enabled end end