diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-08-19 19:13:30 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-08-25 09:57:00 -0700 |
commit | 9e398d9540c70e5a0bcaab554f3648055d97e8ba (patch) | |
tree | a5f8fcc45093d9e5d704dac0989c35129173053d | |
parent | 16c56e408366e4af6bd469442342ecf597c5d9d9 (diff) | |
download | chef-9e398d9540c70e5a0bcaab554f3648055d97e8ba.tar.gz |
fix supports hash issues in service providers
- redhat provider now allows the user to override :status
- gentoo provider now allows the user to override :status and :restart
- service providers now dup the status hash and mutate their private
copy instead of mutating the new_resource
-rw-r--r-- | lib/chef/provider/service.rb | 14 | ||||
-rw-r--r-- | lib/chef/provider/service/freebsd.rb | 2 | ||||
-rw-r--r-- | lib/chef/provider/service/gentoo.rb | 6 | ||||
-rw-r--r-- | lib/chef/provider/service/init.rb | 6 | ||||
-rw-r--r-- | lib/chef/provider/service/openbsd.rb | 3 | ||||
-rw-r--r-- | lib/chef/provider/service/redhat.rb | 3 | ||||
-rw-r--r-- | lib/chef/provider/service/simple.rb | 4 | ||||
-rw-r--r-- | lib/chef/resource/service.rb | 2 | ||||
-rw-r--r-- | spec/unit/provider/service/gentoo_service_spec.rb | 8 | ||||
-rw-r--r-- | spec/unit/provider/service/openbsd_service_spec.rb | 18 | ||||
-rw-r--r-- | spec/unit/provider/service/redhat_spec.rb | 36 | ||||
-rw-r--r-- | spec/unit/resource/service_spec.rb | 8 |
12 files changed, 70 insertions, 40 deletions
diff --git a/lib/chef/provider/service.rb b/lib/chef/provider/service.rb index 15dd72cb04..e7bb2a76d7 100644 --- a/lib/chef/provider/service.rb +++ b/lib/chef/provider/service.rb @@ -1,6 +1,6 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,6 +25,10 @@ class Chef include Chef::Mixin::Command + def supports + @supports ||= new_resource.supports.dup + end + def initialize(new_resource, run_context) super @enabled = nil @@ -34,6 +38,12 @@ class Chef true end + def load_current_resource + supports[:status] = false if supports[:status].nil? + supports[:reload] = false if supports[:reload].nil? + supports[:restart] = false if supports[:restart].nil? + end + def load_new_resource_state # If the user didn't specify a change in enabled state, # it will be the same as the old resource @@ -50,7 +60,7 @@ class Chef def define_resource_requirements requirements.assert(:reload) do |a| - a.assertion { @new_resource.supports[:reload] || @new_resource.reload_command } + a.assertion { supports[:reload] || @new_resource.reload_command } a.failure_message Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :reload" # if a service is not declared to support reload, that won't # typically change during the course of a run - so no whyrun diff --git a/lib/chef/provider/service/freebsd.rb b/lib/chef/provider/service/freebsd.rb index 6c78f86fe0..78ca0be235 100644 --- a/lib/chef/provider/service/freebsd.rb +++ b/lib/chef/provider/service/freebsd.rb @@ -99,7 +99,7 @@ class Chef def restart_service if new_resource.restart_command super - elsif new_resource.supports[:restart] + elsif supports[:restart] shell_out_with_systems_locale!("#{init_command} fastrestart") else stop_service diff --git a/lib/chef/provider/service/gentoo.rb b/lib/chef/provider/service/gentoo.rb index 3dab920f06..903c55af7a 100644 --- a/lib/chef/provider/service/gentoo.rb +++ b/lib/chef/provider/service/gentoo.rb @@ -1,7 +1,7 @@ # # Author:: Lee Jensen (<ljensen@engineyard.com>) # Author:: AJ Christensen (<aj@opscode.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,9 +26,9 @@ class Chef::Provider::Service::Gentoo < Chef::Provider::Service::Init provides :service, platform_family: "gentoo" def load_current_resource + supports[:status] = true if supports[:status].nil? + supports[:restart] = true if supports[:restart].nil? - @new_resource.supports[:status] = true - @new_resource.supports[:restart] = true @found_script = false super diff --git a/lib/chef/provider/service/init.rb b/lib/chef/provider/service/init.rb index 355e98a0eb..8fe5b0281f 100644 --- a/lib/chef/provider/service/init.rb +++ b/lib/chef/provider/service/init.rb @@ -1,6 +1,6 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -72,7 +72,7 @@ class Chef def restart_service if @new_resource.restart_command super - elsif @new_resource.supports[:restart] + elsif supports[:restart] shell_out_with_systems_locale!("#{default_init_command} restart") else stop_service @@ -84,7 +84,7 @@ class Chef def reload_service if @new_resource.reload_command super - elsif @new_resource.supports[:reload] + elsif supports[:reload] shell_out_with_systems_locale!("#{default_init_command} reload") end end diff --git a/lib/chef/provider/service/openbsd.rb b/lib/chef/provider/service/openbsd.rb index 173191e01c..36c9e8141e 100644 --- a/lib/chef/provider/service/openbsd.rb +++ b/lib/chef/provider/service/openbsd.rb @@ -40,11 +40,12 @@ class Chef @rc_conf = ::File.read(RC_CONF_PATH) rescue '' @rc_conf_local = ::File.read(RC_CONF_LOCAL_PATH) rescue '' @init_command = ::File.exist?(rcd_script_path) ? rcd_script_path : nil - new_resource.supports[:status] = true new_resource.status_command("#{default_init_command} check") end def load_current_resource + supports[:status] = true if supports[:status].nil? + @current_resource = Chef::Resource::Service.new(new_resource.name) current_resource.service_name(new_resource.service_name) diff --git a/lib/chef/provider/service/redhat.rb b/lib/chef/provider/service/redhat.rb index da86b2e2fc..33a9778715 100644 --- a/lib/chef/provider/service/redhat.rb +++ b/lib/chef/provider/service/redhat.rb @@ -42,7 +42,6 @@ class Chef def initialize(new_resource, run_context) super @init_command = "/sbin/service #{new_resource.service_name}" - new_resource.supports[:status] = true @service_missing = false @current_run_levels = [] end @@ -69,6 +68,8 @@ class Chef end def load_current_resource + supports[:status] = true if supports[:status].nil? + super if ::File.exists?("/sbin/chkconfig") diff --git a/lib/chef/provider/service/simple.rb b/lib/chef/provider/service/simple.rb index ee403ee163..d295513b42 100644 --- a/lib/chef/provider/service/simple.rb +++ b/lib/chef/provider/service/simple.rb @@ -76,7 +76,7 @@ class Chef end requirements.assert(:all_actions) do |a| - a.assertion { @new_resource.status_command or @new_resource.supports[:status] or + a.assertion { @new_resource.status_command or supports[:status] or (!ps_cmd.nil? and !ps_cmd.empty?) } a.failure_message Chef::Exceptions::Service, "#{@new_resource} could not determine how to inspect the process table, please set this node's 'command.ps' attribute" end @@ -127,7 +127,7 @@ class Chef nil end - elsif @new_resource.supports[:status] + elsif supports[:status] Chef::Log.debug("#{@new_resource} supports status, running") begin if shell_out("#{default_init_command} status").exitstatus == 0 diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index 1485a10eb1..6d1b81f9cb 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -45,7 +45,7 @@ class Chef @priority = nil @timeout = nil @run_levels = nil - @supports = { :restart => false, :reload => false, :status => false } + @supports = { :restart => nil, :reload => nil, :status => nil } end def service_name(arg=nil) diff --git a/spec/unit/provider/service/gentoo_service_spec.rb b/spec/unit/provider/service/gentoo_service_spec.rb index c08982acc3..0aa7bf4529 100644 --- a/spec/unit/provider/service/gentoo_service_spec.rb +++ b/spec/unit/provider/service/gentoo_service_spec.rb @@ -1,7 +1,7 @@ # # Author:: Lee Jensen (<ljensen@engineyard.com>) # Author:: AJ Christensen (<aj@opscode.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -108,17 +108,17 @@ describe Chef::Provider::Service::Gentoo do it "should support the status command automatically" do @provider.load_current_resource - expect(@new_resource.supports[:status]).to be_truthy + expect(@provider.supports[:status]).to be true end it "should support the restart command automatically" do @provider.load_current_resource - expect(@new_resource.supports[:restart]).to be_truthy + expect(@provider.supports[:restart]).to be true end it "should not support the reload command automatically" do @provider.load_current_resource - expect(@new_resource.supports[:reload]).not_to be_truthy + expect(@provider.supports[:reload]).to be_falsey end end diff --git a/spec/unit/provider/service/openbsd_service_spec.rb b/spec/unit/provider/service/openbsd_service_spec.rb index 1b5206470e..d3c150a14b 100644 --- a/spec/unit/provider/service/openbsd_service_spec.rb +++ b/spec/unit/provider/service/openbsd_service_spec.rb @@ -35,10 +35,12 @@ describe Chef::Provider::Service::Openbsd do node end + let(:supports) { {:status => false} } + let(:new_resource) do new_resource = Chef::Resource::Service.new("sndiod") new_resource.pattern("sndiod") - new_resource.supports({:status => false}) + new_resource.supports(supports) new_resource end @@ -106,9 +108,7 @@ describe Chef::Provider::Service::Openbsd do context "when the service supports status" do let(:status) { double(:stdout => "", :exitstatus => 0) } - before do - new_resource.supports({:status => true}) - end + let(:supports) { { :status => true } } it "should run '/etc/rc.d/service_name status'" do expect(provider).to receive(:shell_out).with("/etc/rc.d/#{new_resource.service_name} check").and_return(status) @@ -305,10 +305,12 @@ describe Chef::Provider::Service::Openbsd do end describe Chef::Provider::Service::Openbsd, "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_systems_locale!).with("/etc/rc.d/#{new_resource.service_name} restart") - provider.restart_service() + context "when the new_resource supports restart" do + let(:supports) { { restart: true } } + it "should call 'restart' on the service_name if the resource supports it" do + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{new_resource.service_name} restart") + provider.restart_service() + end end it "should call the restart_command if one has been specified" do diff --git a/spec/unit/provider/service/redhat_spec.rb b/spec/unit/provider/service/redhat_spec.rb index 1cb985714f..5aaf54d9f5 100644 --- a/spec/unit/provider/service/redhat_spec.rb +++ b/spec/unit/provider/service/redhat_spec.rb @@ -64,9 +64,33 @@ describe "Chef::Provider::Service::Redhat" do end describe "load current resource" do - it "sets the current enabled status to true if the service is enabled for any run level" do + before do status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "") - expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status) + allow(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status) + end + + it "sets supports[:status] to true by default" do + chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:off 2:off 3:off 4:off 5:on 6:off", :stderr => "") + expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) + expect(@provider.service_missing).to be false + @provider.load_current_resource + expect(@provider.supports[:status]).to be true + end + + it "lets the user override supports[:status] in the new_resource" do + @new_resource.supports( { status: false } ) + @new_resource.pattern "myservice" + chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:off 2:off 3:off 4:off 5:on 6:off", :stderr => "") + expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) + foo_out = double("ps_command", :exitstatus => 0, :stdout => "a line that matches myservice", :stderr => "") + expect(@provider).to receive(:shell_out!).with("foo").and_return(foo_out) + expect(@provider.service_missing).to be false + expect(@provider).not_to receive(:shell_out).with("/sbin/service chef status") + @provider.load_current_resource + expect(@provider.supports[:status]).to be false + end + + it "sets the current enabled status to true if the service is enabled for any run level" do chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:off 2:off 3:off 4:off 5:on 6:off", :stderr => "") expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) expect(@provider.service_missing).to be false @@ -75,8 +99,6 @@ describe "Chef::Provider::Service::Redhat" do end it "sets the current enabled status to false if the regex does not match" do - status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "") - expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status) chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:off 2:off 3:off 4:off 5:off 6:off", :stderr => "") expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) expect(@provider.service_missing).to be false @@ -85,9 +107,7 @@ describe "Chef::Provider::Service::Redhat" do end it "sets the current enabled status to true if the service is enabled at specified run levels" do - status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "") @new_resource.run_levels([1, 2]) - expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status) chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:on 2:on 3:off 4:off 5:off 6:off", :stderr => "") expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) expect(@provider.service_missing).to be false @@ -97,9 +117,7 @@ describe "Chef::Provider::Service::Redhat" do end it "sets the current enabled status to false if the service is enabled at a run level it should not" do - status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "") @new_resource.run_levels([1, 2]) - expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status) chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:on 2:on 3:on 4:off 5:off 6:off", :stderr => "") expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) expect(@provider.service_missing).to be false @@ -109,9 +127,7 @@ describe "Chef::Provider::Service::Redhat" do end it "sets the current enabled status to false if the service is not enabled at specified run levels" do - status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "") @new_resource.run_levels([ 2 ]) - expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status) chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef 0:off 1:on 2:off 3:off 4:off 5:off 6:off", :stderr => "") expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig) expect(@provider.service_missing).to be false diff --git a/spec/unit/resource/service_spec.rb b/spec/unit/resource/service_spec.rb index eb6f444e93..b9e3757255 100644 --- a/spec/unit/resource/service_spec.rb +++ b/spec/unit/resource/service_spec.rb @@ -1,7 +1,7 @@ # # Author:: AJ Christensen (<aj@hjksolutions.com>) # Author:: Tyler Cloke (<tyler@opscode.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -139,14 +139,14 @@ describe Chef::Resource::Service do expect { @resource.send(attrib, "poop") }.to raise_error(ArgumentError) end - it "should default all the feature support to false" do - support_hash = { :status => false, :restart => false, :reload=> false } + it "should default all the feature support to nil" do + support_hash = { :status => nil, :restart => nil, :reload=> nil } expect(@resource.supports).to eq(support_hash) end it "should allow you to set what features this resource supports as a array" do support_array = [ :status, :restart ] - support_hash = { :status => true, :restart => true, :reload => false } + support_hash = { :status => true, :restart => true, :reload => nil } @resource.supports(support_array) expect(@resource.supports).to eq(support_hash) end |