diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-08-20 10:39:47 -0700 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2014-08-29 07:58:43 -0400 |
commit | 70bf394d094fbc30f8e8ffe79574c11d2508d585 (patch) | |
tree | c96e1f7b4fb6d5d4bafdd8a9e7f58939db4ab399 | |
parent | 49f9e2a45dfe449ccff3bc013d19b59d31d5ee97 (diff) | |
download | chef-70bf394d094fbc30f8e8ffe79574c11d2508d585.tar.gz |
refactoring freebsd provider + unit tests
-rw-r--r-- | lib/chef/provider/service/freebsd.rb | 160 | ||||
-rw-r--r-- | lib/chef/provider/service/init.rb | 2 | ||||
-rw-r--r-- | lib/chef/provider/service/simple.rb | 2 | ||||
-rw-r--r-- | spec/unit/provider/service/freebsd_service_spec.rb | 541 | ||||
-rw-r--r-- | spec/unit/provider/service/simple_service_spec.rb | 2 |
5 files changed, 444 insertions, 263 deletions
diff --git a/lib/chef/provider/service/freebsd.rb b/lib/chef/provider/service/freebsd.rb index 4cd51088d6..7f49214878 100644 --- a/lib/chef/provider/service/freebsd.rb +++ b/lib/chef/provider/service/freebsd.rb @@ -25,89 +25,76 @@ class Chef class Service class Freebsd < Chef::Provider::Service::Init - def load_current_resource - @current_resource = Chef::Resource::Service.new(@new_resource.name) - @current_resource.service_name(@new_resource.service_name) - @rcd_script_found = true + attr_reader :enabled_state_found + + def initialize(new_resource, run_context) + super @enabled_state_found = false - # Determine if we're talking about /etc/rc.d or /usr/local/etc/rc.d - if ::File.exists?("/etc/rc.d/#{current_resource.service_name}") - @init_command = "/etc/rc.d/#{current_resource.service_name}" - elsif ::File.exists?("/usr/local/etc/rc.d/#{current_resource.service_name}") - @init_command = "/usr/local/etc/rc.d/#{current_resource.service_name}" - else - @rcd_script_found = false - return - end - Chef::Log.debug("#{@current_resource} found at #{@init_command}") - determine_current_status! - # Default to disabled if the service doesn't currently exist - # at all - var_name = service_enable_variable_name - if ::File.exists?("/etc/rc.conf") && var_name - read_rc_conf.each do |line| - case line - when /^#{Regexp.escape(var_name)}="(\w+)"/ - @enabled_state_found = true - if $1 =~ /[Yy][Ee][Ss]/ - @current_resource.enabled true - elsif $1 =~ /[Nn][Oo][Nn]?[Oo]?[Nn]?[Ee]?/ - @current_resource.enabled false - end - end - end - end - unless @current_resource.enabled - Chef::Log.debug("#{@new_resource.name} enable/disable state unknown") - @current_resource.enabled false + @init_command = nil + if ::File.exist?("/etc/rc.d/#{new_resource.service_name}") + @init_command = "/etc/rc.d/#{new_resource.service_name}" + elsif ::File.exist?("/usr/local/etc/rc.d/#{new_resource.service_name}") + @init_command = "/usr/local/etc/rc.d/#{new_resource.service_name}" end + end + + def load_current_resource + @current_resource = Chef::Resource::Service.new(new_resource.name) + current_resource.service_name(new_resource.service_name) - @current_resource + return current_resource unless init_command + + Chef::Log.debug("#{current_resource} found at #{init_command}") + + determine_current_status! # see Chef::Provider::Service::Simple + + determine_enabled_status! + current_resource end def define_resource_requirements shared_resource_requirements + requirements.assert(:start, :enable, :reload, :restart) do |a| - a.assertion { @rcd_script_found } - a.failure_message Chef::Exceptions::Service, "#{@new_resource}: unable to locate the rc.d script" + a.assertion { init_command } + a.failure_message Chef::Exceptions::Service, "#{new_resource}: unable to locate the rc.d script" end requirements.assert(:all_actions) do |a| - a.assertion { @enabled_state_found } + a.assertion { enabled_state_found } # for consistentcy with original behavior, this will not fail in non-whyrun mode; # rather it will silently set enabled state=>false a.whyrun "Unable to determine enabled/disabled state, assuming this will be correct for an actual run. Assuming disabled." end requirements.assert(:start, :enable, :reload, :restart) do |a| - a.assertion { @rcd_script_found && service_enable_variable_name != nil } - a.failure_message Chef::Exceptions::Service, "Could not find the service name in #{@init_command} and rcvar" + a.assertion { init_command && service_enable_variable_name != nil } + a.failure_message Chef::Exceptions::Service, "Could not find the service name in #{init_command} and rcvar" # No recovery in whyrun mode - the init file is present but not correct. end end def start_service - if @new_resource.start_command + if new_resource.start_command super else - shell_out!("#{@init_command} faststart") + shell_out!("#{init_command} faststart") end end def stop_service - if @new_resource.stop_command + if new_resource.stop_command super else - shell_out!("#{@init_command} faststop") + shell_out!("#{init_command} faststop") end end def restart_service - if @new_resource.restart_command - + if new_resource.restart_command super - elsif @new_resource.supports[:restart] - shell_out!("#{@init_command} fastrestart") + elsif new_resource.supports[:restart] + shell_out!("#{init_command} fastrestart") else stop_service sleep 1 @@ -115,6 +102,16 @@ class Chef end end + def enable_service + set_service_enable("YES") unless current_resource.enabled + end + + def disable_service + set_service_enable("NO") if current_resource.enabled + end + + private + def read_rc_conf ::File.open("/etc/rc.conf", 'r') { |file| file.readlines } end @@ -127,27 +124,51 @@ class Chef # The variable name used in /etc/rc.conf for enabling this service def service_enable_variable_name - # Look for name="foo" in the shell script @init_command. Use this for determining the variable name in /etc/rc.conf - # corresponding to this service - # For example: to enable the service mysql-server with the init command /usr/local/etc/rc.d/mysql-server, you need - # to set mysql_enable="YES" in /etc/rc.conf$ - if @rcd_script_found - ::File.open(@init_command) do |rcscript| - rcscript.each_line do |line| - if line =~ /^name="?(\w+)"?/ - return $1 + "_enable" + @service_enable_variable_name ||= + begin + # Look for name="foo" in the shell script @init_command. Use this for determining the variable name in /etc/rc.conf + # corresponding to this service + # For example: to enable the service mysql-server with the init command /usr/local/etc/rc.d/mysql-server, you need + # to set mysql_enable="YES" in /etc/rc.conf$ + if init_command + ::File.open(init_command) do |rcscript| + rcscript.each_line do |line| + if line =~ /^name="?(\w+)"?/ + return $1 + "_enable" + end + end + end + # some scripts support multiple instances through symlinks such as openvpn. + # We should get the service name from rcvar. + Chef::Log.debug("name=\"service\" not found at #{init_command}. falling back to rcvar") + sn = shell_out!("#{init_command} rcvar").stdout[/(\w+_enable)=/, 1] + else + # for why-run mode when the rcd_script is not there yet + new_resource.service_name + end + end + end + + def determine_enabled_status! + var_name = service_enable_variable_name + if ::File.exist?("/etc/rc.conf") && var_name + read_rc_conf.each do |line| + case line + when /^#{Regexp.escape(var_name)}="(\w+)"/ + enabled_state_found! + if $1 =~ /[Yy][Ee][Ss]/ # FIXME: anchors? + current_resource.enabled true + elsif $1 =~ /[Nn][Oo][Nn]?[Oo]?[Nn]?[Ee]?/ # FIXME: anchors? and wtf is this supposed to match? + current_resource.enabled false end end end - # some scripts support multiple instances through symlinks such as openvpn. - # We should get the service name from rcvar. - Chef::Log.debug("name=\"service\" not found at #{@init_command}. falling back to rcvar") - sn = shell_out!("#{@init_command} rcvar").stdout[/(\w+_enable)=/, 1] - return sn end - # Fallback allows us to keep running in whyrun mode when - # the script does not exist. - @new_resource.service_name + + if current_resource.enabled.nil? + Chef::Log.debug("#{new_resource.name} enable/disable state unknown") + current_resource.enabled false + end end def set_service_enable(value) @@ -159,14 +180,9 @@ class Chef write_rc_conf(lines) end - def enable_service() - set_service_enable("YES") unless @current_resource.enabled - end - - def disable_service() - set_service_enable("NO") if @current_resource.enabled + def enabled_state_found! + @enabled_state_found = true end - end end end diff --git a/lib/chef/provider/service/init.rb b/lib/chef/provider/service/init.rb index cf5da852c3..23d9dde80a 100644 --- a/lib/chef/provider/service/init.rb +++ b/lib/chef/provider/service/init.rb @@ -24,6 +24,8 @@ class Chef class Service class Init < Chef::Provider::Service::Simple + attr_accessor :init_command + def initialize(new_resource, run_context) super @init_command = "/etc/init.d/#{@new_resource.service_name}" diff --git a/lib/chef/provider/service/simple.rb b/lib/chef/provider/service/simple.rb index f03b8a18a1..0eb983a0bf 100644 --- a/lib/chef/provider/service/simple.rb +++ b/lib/chef/provider/service/simple.rb @@ -25,6 +25,8 @@ class Chef class Service class Simple < Chef::Provider::Service + attr_reader :status_load_success + def load_current_resource @current_resource = Chef::Resource::Service.new(@new_resource.name) @current_resource.service_name(@new_resource.service_name) diff --git a/spec/unit/provider/service/freebsd_service_spec.rb b/spec/unit/provider/service/freebsd_service_spec.rb index cfdfbeb85a..4339adfe04 100644 --- a/spec/unit/provider/service/freebsd_service_spec.rb +++ b/spec/unit/provider/service/freebsd_service_spec.rb @@ -18,8 +18,13 @@ require 'spec_helper' -describe Chef::Provider::Service::Freebsd do +class Chef::Provider::Service::Freebsd + public :service_enable_variable_name + public :determine_enabled_status! + public :determine_current_status! +end +describe Chef::Provider::Service::Freebsd do let(:node) do node = Chef::Node.new node.automatic_attrs[:command] = {:ps => "ps -ax"} @@ -46,310 +51,468 @@ describe Chef::Provider::Service::Freebsd do provider end - # ununsed? - # let(:init_command) { "/usr/local/etc/rc.d/apache22" } - before do allow(Chef::Resource::Service).to receive(:new).and_return(current_resource) end - describe "load_current_resource" do - let(:stdout) do - StringIO.new(<<-PS_SAMPLE) -413 ?? Ss 0:02.51 /usr/sbin/syslogd -s -539 ?? Is 0:00.14 /usr/sbin/sshd -545 ?? Ss 0:17.53 sendmail: accepting connections (sendmail) -PS_SAMPLE - end + def stub_etc_rcd_script + allow(::File).to receive(:exist?).and_return(false) + expect(::File).to receive(:exist?).with("/etc/rc.d/#{new_resource.service_name}").and_return(true) + end - let(:status) { double(:stdout => stdout, :exitstatus => 0) } + def stub_usr_local_rcd_script + allow(::File).to receive(:exist?).and_return(false) + expect(::File).to receive(:exist?).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(true) + end - before(:each) do - allow(provider).to receive(:shell_out!).with(node[:command][:ps]).and_return(status) + def run_load_current_resource + stub_usr_local_rcd_script + provider.load_current_resource + end - allow(::File).to receive(:exists?).and_return(false) - allow(::File).to receive(:exists?).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(true) - lines = double("lines") - allow(lines).to receive(:each).and_yield("sshd_enable=\"YES\""). - and_yield("#{new_resource.name}_enable=\"YES\"") - allow(::File).to receive(:open).and_return(lines) + describe Chef::Provider::Service::Freebsd, "initialize" do + it "should default enabled_state_found to false" do + expect(provider.enabled_state_found).to be false + end - rc_with_name = StringIO.new(<<-RC_SAMPLE) -name="apache22" -rcvar=`set_rcvar` -RC_SAMPLE - allow(::File).to receive(:open).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(rc_with_name) - allow(provider).to receive(:service_enable_variable_name).and_return nil + it "should find /usr/local/etc/rc.d init scripts" do + stub_usr_local_rcd_script + expect(provider.init_command).to eql "/usr/local/etc/rc.d/apache22" end - it "should create a current resource with the name of the new resource" do - expect(Chef::Resource::Service).to receive(:new).and_return(current_resource) - provider.load_current_resource + it "should find /etc/rc.d init scripts" do + stub_etc_rcd_script + expect(provider.init_command).to eql "/etc/rc.d/apache22" end - it "should set the current resources service name to the new resources service name" do - provider.load_current_resource - expect(current_resource.service_name).to eq(new_resource.service_name) + it "should set init_command to nil if it can't find anything" do + allow(::File).to receive(:exist?).and_return(false) + expect(provider.init_command).to be nil end + end - it "should not raise an exception if the rcscript have a name variable" do - provider.load_current_resource - expect { provider.service_enable_variable_name }.not_to raise_error + describe Chef::Provider::Service::Freebsd, "determine_current_status!" do + before do + stub_usr_local_rcd_script + provider.current_resource = current_resource + current_resource.service_name(new_resource.service_name) end - describe "when the service supports status" do + context "when a status command has been specified" do + let(:status) { double(:stdout => "", :exitstatus => 0) } + + before do + new_resource.status_command("/bin/chefhasmonkeypants status") + end + + it "should run the services status command if one has been specified" do + expect(provider).to receive(:shell_out).with("/bin/chefhasmonkeypants status").and_return(status) + provider.determine_current_status! + end + end + + context "when the service supports status" do + let(:status) { double(:stdout => "", :exitstatus => 0) } + before do new_resource.supports({:status => true}) end it "should run '/etc/init.d/service_name status'" do - expect(provider).to receive(:shell_out).with("/usr/local/etc/rc.d/#{current_resource.service_name} status").and_return(status) - provider.load_current_resource + expect(provider).to receive(:shell_out).with("/usr/local/etc/rc.d/#{new_resource.service_name} status").and_return(status) + provider.determine_current_status! end it "should set running to true if the status command returns 0" do - expect(provider).to receive(:shell_out).with("/usr/local/etc/rc.d/#{current_resource.service_name} status").and_return(status) - expect(current_resource).to receive(:running).with(true) - provider.load_current_resource + expect(provider).to receive(:shell_out).with("/usr/local/etc/rc.d/#{new_resource.service_name} status").and_return(status) + provider.determine_current_status! + expect(current_resource.running).to be true end it "should set running to false if the status command returns anything except 0" do - expect(provider).to receive(:shell_out).with("/usr/local/etc/rc.d/#{current_resource.service_name} status").and_raise(Mixlib::ShellOut::ShellCommandFailed) - expect(current_resource).to receive(:running).with(false) - provider.load_current_resource - # provider.current_resource.running.should be_false + expect(provider).to receive(:shell_out).with("/usr/local/etc/rc.d/#{new_resource.service_name} status").and_raise(Mixlib::ShellOut::ShellCommandFailed) + provider.determine_current_status! + expect(current_resource.running).to be false end end - describe "when a status command has been specified" do + context "when we have a 'ps' attribute" do + let(:stdout) do + StringIO.new(<<-PS_SAMPLE) +413 ?? Ss 0:02.51 /usr/sbin/syslogd -s +539 ?? Is 0:00.14 /usr/sbin/sshd +545 ?? Ss 0:17.53 sendmail: accepting connections (sendmail) +PS_SAMPLE + end + let(:status) { double(:stdout => stdout, :exitstatus => 0) } + before do - new_resource.status_command("/bin/chefhasmonkeypants status") + node.automatic_attrs[:command] = {:ps => "ps -ax"} end - it "should run the services status command if one has been specified" do - expect(provider).to receive(:shell_out).with("/bin/chefhasmonkeypants status").and_return(status) - provider.load_current_resource + it "should shell_out! the node's ps command" do + expect(provider).to receive(:shell_out!).with(node[:command][:ps]).and_return(status) + provider.determine_current_status! + end + + it "should read stdout of the ps command" do + allow(provider).to receive(:shell_out!).and_return(status) + expect(stdout).to receive(:each_line).and_return(true) + provider.determine_current_status! + end + + context "when the regex matches the output" do + let(:stdout) do + StringIO.new(<<-PS_SAMPLE) +555 ?? Ss 0:05.16 /usr/sbin/cron -s + 9881 ?? Ss 0:06.67 /usr/local/sbin/httpd -DNOHTTPACCEPT + PS_SAMPLE + end + + it "should set running to true" do + allow(provider).to receive(:shell_out!).and_return(status) + provider.determine_current_status! + expect(current_resource.running).to be_true + end + end + + it "should set running to false if the regex doesn't match" do + allow(provider).to receive(:shell_out!).and_return(status) + provider.determine_current_status! + expect(current_resource.running).to be_false end + it "should set running to nil if ps fails" do + allow(provider).to receive(:shell_out!).and_raise(Mixlib::ShellOut::ShellCommandFailed) + provider.determine_current_status! + expect(current_resource.running).to be_nil + expect(provider.status_load_success).to be_nil + end + + context "when ps command is nil" do + before do + node.automatic_attrs[:command] = {:ps => nil} + end + + it "should set running to nil" do + pending "superclass raises no conversion of nil to string which seems broken" + provider.determine_current_status! + expect(current_resource.running).to be_nil + end + end + + context "when ps is empty string" do + before do + node.automatic_attrs[:command] = {:ps => ""} + end + + it "should set running to nil" do + provider.determine_current_status! + expect(current_resource.running).to be_nil + end + end end + end - it "should raise error if the node has a nil ps attribute and no other means to get status" do - node.automatic_attrs[:command] = {:ps => nil} - provider.define_resource_requirements - expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) + describe Chef::Provider::Service::Freebsd, "determine_enabled_status!" do + before do + stub_usr_local_rcd_script + provider.current_resource = current_resource + current_resource.service_name(new_resource.service_name) + + allow(provider).to receive(:service_enable_variable_name).and_return("#{new_resource.service_name}_enable") end - it "should raise error if the node has an empty ps attribute and no other means to get status" do - node.automatic_attrs[:command] = {:ps => ""} - provider.define_resource_requirements - expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) + context "when /etc/rc.conf does not exist" do + before do + expect(::File).to receive(:exist?).with("/etc/rc.conf").and_return(false) + end + + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end end - describe "when executing assertions" do - it "should verify that /etc/rc.conf exists" do - expect(::File).to receive(:exists?).with("/etc/rc.conf") - allow(provider).to receive(:service_enable_variable_name).and_return("#{current_resource.service_name}_enable") - provider.load_current_resource + context "when /etc/rc.conf does exist" do + before do + expect(::File).to receive(:exist?).with("/etc/rc.conf").and_return(true) + expect(provider).to receive(:read_rc_conf).and_return(lines) end - context "and the init script is not found" do - [ "start", "reload", "restart", "enable" ].each do |action| - it "should raise an exception when the action is #{action}" do - allow(::File).to receive(:exists?).and_return(false) - provider.load_current_resource - provider.define_resource_requirements - expect(provider.instance_variable_get("@rcd_script_found")).to be_false - provider.action = action - expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) + %w{YES Yes yes yEs YeS}.each do |setting| + context "when the enable variable is set to #{setting}" do + let(:lines) { [ %Q{#{new_resource.service_name}_enable="#{setting}"} ] } + it "sets enabled to true" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be true end end + end - [ "stop", "disable" ].each do |action| - it "should not raise an error when the action is #{action}" do - provider.action = action - expect { provider.process_resource_requirements }.not_to raise_error + %w{No NO no}.each do |setting| + context "when the enable variable is set to #{setting}" do + let(:lines) { [ %Q{#{new_resource.service_name}_enable="#{setting}"} ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false end end end - it "update state when current resource enabled state could not be determined" do - expect(::File).to receive(:exists?).with("/etc/rc.conf").and_return false - provider.load_current_resource - expect(provider.instance_variable_get("@enabled_state_found")).to be_false + context "when the enable variable is garbage" do + let(:lines) { [ %Q{#{new_resource.service_name}_enable="alskdjflasdkjflakdfj"} ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end end - it "update state when current resource enabled state could be determined" do - allow(::File).to receive(:exist?).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(true) - expect(::File).to receive(:exists?).with("/etc/rc.conf").and_return true - provider.load_current_resource - expect(provider.instance_variable_get("@enabled_state_found")).to be_false - expect(provider.instance_variable_get("@rcd_script_found")).to be_true - provider.define_resource_requirements - expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service, - "Could not find the service name in /usr/local/etc/rc.d/#{current_resource.service_name} and rcvar") + context "when the enable variable partial matches some other service and we are disabled" do + let(:lines) { [ + %Q{thing_#{new_resource.service_name}_enable="YES"}, + %Q{#{new_resource.service_name}_enable="NO"}, + ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end end - it "should throw an exception if service line is missing from rc.d script" do - pending "not implemented" do - expect(false).to be_true - end + context "when the enable variable partial matches some other disabled service and we are enabled" do + let(:lines) { [ + %Q{thing_#{new_resource.service_name}_enable="NO"}, + %Q{#{new_resource.service_name}_enable="YES"}, + ] } + it "sets enabled to true" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be true + end + end + + context "when the enable variable only partial matches some other enabled service" do + let(:lines) { [ %Q{thing_#{new_resource.service_name}_enable="YES"} ] } + it "sets enabled to false" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end end + context "when nothing matches" do + let(:lines) { [] } + it "sets enabled to true" do + provider.determine_enabled_status! + expect(current_resource.enabled).to be false + end + end end + end - describe "when we have a 'ps' attribute" do - before do - node.automatic_attrs[:command] = {:ps => "ps -ax"} + describe Chef::Provider::Service::Freebsd, "service_enable_variable_name" do + before do + stub_usr_local_rcd_script + provider.current_resource = current_resource + current_resource.service_name(new_resource.service_name) + + expect(::File).to receive(:open).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_yield(rcscript) + end + + context "when the rc script has a 'name' variable" do + let(:rcscript) do + StringIO.new(<<-EOF) +name="#{new_resource.service_name}" +rcvar=`set_rcvar` +EOF end - it "should shell_out! the node's ps command" do - expect(provider).to receive(:shell_out!).with(node[:command][:ps]).and_return(status) - provider.load_current_resource + it "should not raise an exception if the rcscript have a name variable" do + expect { provider.service_enable_variable_name }.not_to raise_error end - it "should read stdout of the ps command" do - allow(provider).to receive(:shell_out!).and_return(status) - expect(stdout).to receive(:each_line).and_return(true) - provider.load_current_resource + it "should not run rcvar" do + expect(provider).not_to receive(:shell_out!) + provider.service_enable_variable_name end - it "should set running to true if the regex matches the output" do - allow(stdout).to receive(:each_line).and_yield("555 ?? Ss 0:05.16 /usr/sbin/cron -s"). - and_yield(" 9881 ?? Ss 0:06.67 /usr/local/sbin/httpd -DNOHTTPACCEPT") - provider.load_current_resource - expect(current_resource.running).to be_true + it "should return the enable variable determined from the rcscript name" do + expect(provider.service_enable_variable_name).to eql "#{new_resource.service_name}_enable" end + end - it "should set running to false if the regex doesn't match" do - allow(provider).to receive(:shell_out!).and_return(status) - provider.load_current_resource - expect(current_resource.running).to be_false + describe "when the rcscript does not have a name variable" do + let(:rcscript) do + StringIO.new <<-EOF +rcvar=`set_rcvar` +EOF end - it "should raise an exception if ps fails" do - allow(provider).to receive(:shell_out!).and_raise(Mixlib::ShellOut::ShellCommandFailed) - provider.load_current_resource - provider.define_resource_requirements - expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) + before do + status = double(:stdout => rcvar_stdout, :exitstatus => 0) + allow(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} rcvar").and_return(status) + end + + describe "when rcvar returns foobar_enable" do + let(:rcvar_stdout) do + rcvar_stdout = <<-EOF +# apache22 +# +# #{new_resource.service_name}_enable="YES" +# (default: "") +EOF + end + + it "should get the service name from rcvar if the rcscript does not have a name variable" do + expect(provider.service_enable_variable_name).to eq("#{new_resource.service_name}_enable") + end + + it "should not raise an exception if the rcscript does not have a name variable" do + expect { provider.service_enable_variable_name }.not_to raise_error + end end + + describe "when rcvar does not return foobar_enable" do + let(:rcvar_stdout) do + rcvar_stdout = <<-EOF +# service_with_noname +# +EOF + end + + it "should return nil" do + expect(provider.service_enable_variable_name).to be nil + end + end + end + end + + describe Chef::Provider::Service::Freebsd, "load_current_resource" do + before(:each) do + stub_usr_local_rcd_script + expect(provider).to receive(:determine_current_status!) + current_resource.running(false) + allow(provider).to receive(:service_enable_variable_name).and_return "#{new_resource.service_name}_enable" + end + + it "should create a current resource with the name of the new resource" do + expect(Chef::Resource::Service).to receive(:new).and_return(current_resource) + provider.load_current_resource + end + + it "should set the current resources service name to the new resources service name" do + provider.load_current_resource + expect(current_resource.service_name).to eq(new_resource.service_name) end it "should return the current resource" do expect(provider.load_current_resource).to eql(current_resource) end - describe "when starting the service" do + end + + context "when testing actions" do + before(:each) do + stub_usr_local_rcd_script + expect(provider).to receive(:determine_current_status!) + current_resource.running(false) + expect(provider).to receive(:determine_enabled_status!) + current_resource.enabled(false) + provider.load_current_resource + end + + describe Chef::Provider::Service::Freebsd, "start_service" do it "should call the start command if one is specified" do new_resource.start_command("/etc/rc.d/chef startyousillysally") expect(provider).to receive(:shell_out!).with("/etc/rc.d/chef startyousillysally") - provider.load_current_resource provider.start_service() end it "should call '/usr/local/etc/rc.d/service_name faststart' if no start command is specified" do expect(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} faststart") - provider.load_current_resource provider.start_service() end end - describe Chef::Provider::Service::Init, "stop_service" do + describe Chef::Provider::Service::Freebsd, "stop_service" do it "should call the stop command if one is specified" do new_resource.stop_command("/etc/init.d/chef itoldyoutostop") expect(provider).to receive(:shell_out!).with("/etc/init.d/chef itoldyoutostop") - provider.load_current_resource provider.stop_service() end it "should call '/usr/local/etc/rc.d/service_name faststop' if no stop command is specified" do expect(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} faststop") - provider.load_current_resource provider.stop_service() end end - describe "when restarting a service" do + describe Chef::Provider::Service::Freebsd, "restart_service" do it "should call 'restart' on the service_name if the resource supports it" do new_resource.supports({:restart => true}) expect(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} fastrestart") - provider.load_current_resource provider.restart_service() end it "should call the restart_command if one has been specified" do new_resource.restart_command("/etc/init.d/chef restartinafire") expect(provider).to receive(:shell_out!).with("/etc/init.d/chef restartinafire") - provider.load_current_resource provider.restart_service() end - end - describe "when the rcscript does not have a name variable" do - before do - rc_with_noname = StringIO.new(<<-RC_SAMPLE) -rcvar=`set_rcvar` -RC_SAMPLE - allow(::File).to receive(:open).with("/usr/local/etc/rc.d/#{current_resource.service_name}").and_return(rc_with_noname) - provider.current_resource = current_resource + it "otherwise it should call stop and start" do + expect(provider).to receive(:stop_service) + expect(provider).to receive(:start_service) + provider.restart_service() end + end + end - describe "when rcvar returns foobar_enable" do - let(:rcvar_stdout) do - rcvar_stdout = <<RCVAR_SAMPLE -# apache22 -# -# #{current_resource.service_name}_enable="YES" -# (default: "") -RCVAR_SAMPLE - end - - before do - status = double(:stdout => rcvar_stdout, :exitstatus => 0) - allow(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{current_resource.service_name} rcvar").and_return(status) - end + describe Chef::Provider::Service::Freebsd, "define_resource_requirements" do + before do + provider.current_resource = current_resource + end - it "should get the service name from rcvar if the rcscript does not have a name variable" do - provider.load_current_resource - allow(provider).to receive(:service_enable_variable_name).and_call_original - expect(provider.service_enable_variable_name).to eq("#{current_resource.service_name}_enable") - end + context "when the init script is not found" do + before do + provider.init_command = nil + allow(provider).to receive(:service_enable_variable_name).and_return("#{new_resource.service_name}_enable") + end - it "should not raise an exception if the rcscript does not have a name variable" do - provider.load_current_resource - expect { provider.service_enable_variable_name }.not_to raise_error + [ "start", "reload", "restart", "enable" ].each do |action| + it "should raise an exception when the action is #{action}" do + provider.define_resource_requirements + provider.action = action + expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) end end - describe "when rcvar does not return foobar_enable" do - let(:rcvar_stdout) do - rcvar_stdout = <<RCVAR_SAMPLE -# service_with_noname -# -RCVAR_SAMPLE + [ "stop", "disable" ].each do |action| + it "should not raise an error when the action is #{action}" do + provider.define_resource_requirements + provider.action = action + expect { provider.process_resource_requirements }.not_to raise_error end + end + end - before do - status = double(:stdout => rcvar_stdout, :exitstatus => 0) - allow(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{current_resource.service_name} rcvar").and_return(status) - end + context "when the init script is found, but the service_enable_variable_name is nil" do + before do + provider.init_command = nil + allow(provider).to receive(:service_enable_variable_name).and_return(nil) + end - [ "start", "reload", "restart", "enable" ].each do |action| - it "should raise an exception when the action is #{action}" do - provider.action = action - provider.load_current_resource - provider.define_resource_requirements - expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) - end + [ "start", "reload", "restart", "enable" ].each do |action| + it "should raise an exception when the action is #{action}" do + provider.action = action + provider.define_resource_requirements + expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service) end + end - [ "stop", "disable" ].each do |action| - it "should not raise an error when the action is #{action}" do - allow(::File).to receive(:exist?).with("/usr/local/etc/rc.d/#{new_resource.service_name}").and_return(true) - provider.action = action - provider.load_current_resource - provider.define_resource_requirements - expect { provider.process_resource_requirements }.not_to raise_error - end + [ "stop", "disable" ].each do |action| + it "should not raise an error when the action is #{action}" do + provider.action = action + provider.define_resource_requirements + expect { provider.process_resource_requirements }.not_to raise_error end end end @@ -358,20 +521,20 @@ RCVAR_SAMPLE describe Chef::Provider::Service::Freebsd, "enable_service" do before do provider.current_resource = current_resource - allow(provider).to receive(:service_enable_variable_name).and_return("#{current_resource.service_name}_enable") + allow(provider).to receive(:service_enable_variable_name).and_return("#{new_resource.service_name}_enable") end it "should enable the service if it is not enabled" do allow(current_resource).to receive(:enabled).and_return(false) - expect(provider).to receive(:read_rc_conf).and_return([ "foo", "#{current_resource.service_name}_enable=\"NO\"", "bar" ]) - expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{current_resource.service_name}_enable=\"YES\""]) + expect(provider).to receive(:read_rc_conf).and_return([ "foo", "#{new_resource.service_name}_enable=\"NO\"", "bar" ]) + expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{new_resource.service_name}_enable=\"YES\""]) provider.enable_service() end it "should enable the service if it is not enabled and not already specified in the rc.conf file" do allow(current_resource).to receive(:enabled).and_return(false) expect(provider).to receive(:read_rc_conf).and_return([ "foo", "bar" ]) - expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{current_resource.service_name}_enable=\"YES\""]) + expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{new_resource.service_name}_enable=\"YES\""]) provider.enable_service() end @@ -385,13 +548,13 @@ RCVAR_SAMPLE describe Chef::Provider::Service::Freebsd, "disable_service" do before do provider.current_resource = current_resource - allow(provider).to receive(:service_enable_variable_name).and_return("#{current_resource.service_name}_enable") + allow(provider).to receive(:service_enable_variable_name).and_return("#{new_resource.service_name}_enable") end it "should should disable the service if it is not disabled" do allow(current_resource).to receive(:enabled).and_return(true) - expect(provider).to receive(:read_rc_conf).and_return([ "foo", "#{current_resource.service_name}_enable=\"YES\"", "bar" ]) - expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{current_resource.service_name}_enable=\"NO\""]) + expect(provider).to receive(:read_rc_conf).and_return([ "foo", "#{new_resource.service_name}_enable=\"YES\"", "bar" ]) + expect(provider).to receive(:write_rc_conf).with(["foo", "bar", "#{new_resource.service_name}_enable=\"NO\""]) provider.disable_service() end diff --git a/spec/unit/provider/service/simple_service_spec.rb b/spec/unit/provider/service/simple_service_spec.rb index 1d94534320..61fb30fe13 100644 --- a/spec/unit/provider/service/simple_service_spec.rb +++ b/spec/unit/provider/service/simple_service_spec.rb @@ -104,8 +104,6 @@ NOMOCKINGSTRINGSPLZ @provider.load_current_resource.should eql(@current_resource) end - - describe "when starting the service" do it "should call the start command if one is specified" do @new_resource.stub(:start_command).and_return("#{@new_resource.start_command}") |