Because Windows allows a service to be both running and disabled, we now support that capability. If a service is explicitly requested to be running and disabled, we will set it to manual start if necessary, start it, and then disable it. If the service is requested to be running and enabled, we will now enable it before attempting to start it (previously, this would simply try to start it and fail).
The exception to this rule is a service which is disabled, and for which we are not managing the enable property. In that case, we assume that some other authority has disabled the service, and respect that, failing to start. Thus, if the user actually wants a service to be running and disabled, they must explicitly declare that intent. Reviewed-By: Jacob Helwig <[email protected]> Signed-off-by: Nick Lewis <[email protected]> --- Local-branch: ticket/master/8272 lib/puppet/provider/service/windows.rb | 13 +++++++++++++ spec/unit/provider/service/windows_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 0 deletions(-) diff --git a/lib/puppet/provider/service/windows.rb b/lib/puppet/provider/service/windows.rb index d77f3b4..f1485f2 100644 --- a/lib/puppet/provider/service/windows.rb +++ b/lib/puppet/provider/service/windows.rb @@ -59,6 +59,19 @@ Puppet::Type.type(:service).provide :windows do end def start + if enabled? == :false + # If disabled and not managing enable, respect disabled and fail. + if @resource[:enable].nil? + raise Puppet::Error, "Will not start disabled service #{@resource[:name]} without managing enable. Specify 'enable => false' to override." + # Otherwise start. If enable => false, we will later sync enable and + # disable the service again. + elsif @resource[:enable] == :true + enable + else + manual_start + end + end + Win32::Service.start( @resource[:name] ) rescue Win32::Service::Error => detail raise Puppet::Error.new("Cannot start #{@resource[:name]}, error was: #{detail}" ) diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb index 0667d46..2012a18 100755 --- a/spec/unit/provider/service/windows_spec.rb +++ b/spec/unit/provider/service/windows_spec.rb @@ -50,6 +50,33 @@ describe Puppet::Type.type(:service).provider(:windows), :if => Puppet.features. /Cannot start snmptrap, error was: The service cannot be started, either/ ) end + + describe "when the service is disabled" do + before :each do + @config.start_type = Win32::Service.get_start_type(Win32::Service::SERVICE_DISABLED) + Win32::Service.stubs(:start).with(@resource[:name]) + end + + it "should refuse to start if not managing enable" do + expect { @resource.provider.start }.to raise_error(Puppet::Error, /Will not start disabled service/) + end + + it "should enable if managing enable and enable is true" do + @resource[:enable] = :true + + Win32::Service.expects(:configure).with('service_name' => @resource[:name], 'start_type' => Win32::Service::SERVICE_AUTO_START).returns(Win32::Service) + + @resource.provider.start + end + + it "should manual start if managing enable and enable is false" do + @resource[:enable] = :false + + Win32::Service.expects(:configure).with('service_name' => @resource[:name], 'start_type' => Win32::Service::SERVICE_DEMAND_START).returns(Win32::Service) + + @resource.provider.start + end + end end describe "#stop" do -- 1.7.5.4 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
