diff --git a/lib/puppet/provider/service/debian.rb b/lib/puppet/provider/service/debian.rb index 3d09e2849..58b808a8e 100755 --- a/lib/puppet/provider/service/debian.rb +++ b/lib/puppet/provider/service/debian.rb @@ -1,48 +1,52 @@ # Manage debian services. Start/stop is the same as InitSvc, but enable/disable # is special. Puppet::Type.type(:service).provide :debian, :parent => :init do desc "Debian's form of `init`-style management. The only difference is that this supports service enabling and disabling via `update-rc.d` and determines enabled status via `invoke-rc.d`. " commands :update_rc => "/usr/sbin/update-rc.d" # note this isn't being used as a command until # http://projects.reductivelabs.com/issues/2538 # is resolved. commands :invoke_rc => "/usr/sbin/invoke-rc.d" defaultfor :operatingsystem => [:debian, :ubuntu] def self.defpath superclass.defpath end # Remove the symlinks def disable - update_rc "-f", @resource[:name], "remove" - update_rc @resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", "." + if `dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?`.to_i == 0 + update_rc @resource[:name], "disable" + else + update_rc "-f", @resource[:name], "remove" + update_rc @resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", "." + end end def enabled? # TODO: Replace system call when Puppet::Util.execute gives us a way # to determine exit status. http://projects.reductivelabs.com/issues/2538 system("/usr/sbin/invoke-rc.d", "--quiet", "--query", @resource[:name], "start") # 104 is the exit status when you query start an enabled service. # 106 is the exit status when the policy layer supplies a fallback action # See x-man-page://invoke-rc.d if [104, 106].include?($CHILD_STATUS.exitstatus) return :true else return :false end end def enable update_rc "-f", @resource[:name], "remove" update_rc @resource[:name], "defaults" end end diff --git a/spec/unit/provider/service/debian_spec.rb b/spec/unit/provider/service/debian_spec.rb index 8dee2ee94..440d4491b 100755 --- a/spec/unit/provider/service/debian_spec.rb +++ b/spec/unit/provider/service/debian_spec.rb @@ -1,89 +1,101 @@ #!/usr/bin/env ruby # # Unit testing for the debian service provider # require File.dirname(__FILE__) + '/../../../spec_helper' provider_class = Puppet::Type.type(:service).provider(:debian) describe provider_class do before(:each) do # Create a mock resource @resource = stub 'resource' @provider = provider_class.new # A catch all; no parameters set @resource.stubs(:[]).returns(nil) # But set name, source and path @resource.stubs(:[]).with(:name).returns "myservice" @resource.stubs(:[]).with(:ensure).returns :enabled @resource.stubs(:ref).returns "Service[myservice]" @provider.resource = @resource @provider.stubs(:command).with(:update_rc).returns "update_rc" @provider.stubs(:command).with(:invoke_rc).returns "invoke_rc" @provider.stubs(:update_rc) @provider.stubs(:invoke_rc) end it "should have an enabled? method" do @provider.should respond_to(:enabled?) end it "should have an enable method" do @provider.should respond_to(:enable) end it "should have a disable method" do @provider.should respond_to(:disable) end describe "when enabling" do it "should call update-rc.d twice" do @provider.expects(:update_rc).twice @provider.enable end end describe "when disabling" do - it "should call update-rc.d twice" do - @provider.expects(:update_rc).twice + it "should be able to disable services with newer sysv-rc versions" do + @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "0" + + @provider.expects(:update_rc).with(@resource[:name], "disable") + + @provider.disable + end + + it "should be able to enable services with older sysv-rc versions" do + @provider.stubs(:`).with("dpkg --compare-versions $(dpkg-query -W --showformat '${Version}' sysv-rc) ge 2.88 ; echo $?").returns "1" + + @provider.expects(:update_rc).with("-f", @resource[:name], "remove") + @provider.expects(:update_rc).with(@resource[:name], "stop", "00", "1", "2", "3", "4", "5", "6", ".") + @provider.disable end end describe "when checking whether it is enabled" do it "should call Kernel.system() with the appropriate parameters" do @provider.expects(:system).with("/usr/sbin/invoke-rc.d", "--quiet", "--query", @resource[:name], "start").once @provider.enabled? end it "should return true when invoke-rc.d exits with 104 status" do @provider.stubs(:system) $CHILD_STATUS.stubs(:exitstatus).returns(104) @provider.enabled?.should == :true end it "should return true when invoke-rc.d exits with 106 status" do @provider.stubs(:system) $CHILD_STATUS.stubs(:exitstatus).returns(106) @provider.enabled?.should == :true end # pick a range of non-[104.106] numbers, strings and booleans to test with. [-100, -1, 0, 1, 100, "foo", "", :true, :false].each do |exitstatus| it "should return false when invoke-rc.d exits with #{exitstatus} status" do @provider.stubs(:system) $CHILD_STATUS.stubs(:exitstatus).returns(exitstatus) @provider.enabled?.should == :false end end end end