diff options
author | sersut <serdar@opscode.com> | 2014-06-18 13:35:42 -0700 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2014-06-18 13:35:42 -0700 |
commit | 26ef3a3a1ef6f1b02578a4636ed89c00e21c3c71 (patch) | |
tree | e14b3578da493721d71d8310cbc4e5da8562b8f8 | |
parent | 8b94d87ac6e33b08bbfd1ff46e9db3fd2464e337 (diff) | |
parent | 5439694dcdfceb12add4a5409477e2e14296c457 (diff) | |
download | chef-26ef3a3a1ef6f1b02578a4636ed89c00e21c3c71.tar.gz |
Merge branch 'CHEF-4791' of github.com:deployable/chef
-rw-r--r-- | lib/chef/provider/service/windows.rb | 36 | ||||
-rw-r--r-- | spec/unit/provider/service/windows_spec.rb | 61 |
2 files changed, 91 insertions, 6 deletions
diff --git a/lib/chef/provider/service/windows.rb b/lib/chef/provider/service/windows.rb index ba51e53bed..ae4f29b1b3 100644 --- a/lib/chef/provider/service/windows.rb +++ b/lib/chef/provider/service/windows.rb @@ -28,11 +28,19 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service include Chef::Mixin::ShellOut - RUNNING = 'running' - STOPPED = 'stopped' + #Win32::Service.get_start_type AUTO_START = 'auto start' DISABLED = 'disabled' + #Win32::Service.get_current_state + RUNNING = 'running' + STOPPED = 'stopped' + CONTINUE_PENDING = 'continue pending' + PAUSE_PENDING = 'pause pending' + PAUSED = 'paused' + START_PENDING = 'start pending' + STOP_PENDING = 'stop pending' + def whyrun_supported? false end @@ -49,9 +57,15 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service def start_service if Win32::Service.exists?(@new_resource.service_name) - if current_state == RUNNING + state = current_state + if state == RUNNING Chef::Log.debug "#{@new_resource} already started - nothing to do" - else + elsif state == START_PENDING + Chef::Log.debug "#{@new_resource} already sent start signal - waiting for start" + spawn_command_thread do + wait_for_state(RUNNING) + end + elsif state == STOPPED if @new_resource.start_command Chef::Log.debug "#{@new_resource} starting service using the given start_command" shell_out!(@new_resource.start_command) @@ -62,6 +76,8 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service end end @new_resource.updated_by_last_action(true) + else + raise Chef::Exceptions::Service, "Service #{@new_resource} can't be started from state [#{state}]" end else Chef::Log.debug "#{@new_resource} does not exist - nothing to do" @@ -70,7 +86,8 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service def stop_service if Win32::Service.exists?(@new_resource.service_name) - if current_state == RUNNING + state = current_state + if state == RUNNING if @new_resource.stop_command Chef::Log.debug "#{@new_resource} stopping service using the given stop_command" shell_out!(@new_resource.stop_command) @@ -81,8 +98,15 @@ class Chef::Provider::Service::Windows < Chef::Provider::Service end end @new_resource.updated_by_last_action(true) - else + elsif state == STOPPED Chef::Log.debug "#{@new_resource} already stopped - nothing to do" + elsif state == STOP_PENDING + Chef::Log.debug "#{@new_resource} already sent stop signal - waiting for stop" + spawn_command_thread do + wait_for_state(STOPPED) + end + else + raise Chef::Exceptions::Service, "Service #{@new_resource} can't be stopped from state [#{state}]" end else Chef::Log.debug "#{@new_resource} does not exist - nothing to do" diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb index 08f5a81a9d..a007e7b984 100644 --- a/spec/unit/provider/service/windows_spec.rb +++ b/spec/unit/provider/service/windows_spec.rb @@ -92,8 +92,39 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do @provider.start_service @new_resource.updated_by_last_action?.should be_false end + + it "should raise an error if the service is paused" do + Win32::Service.stub(:status).with(@new_resource.service_name).and_return( + double("StatusStruct", :current_state => "paused")) + @provider.load_current_resource + Win32::Service.should_not_receive(:start).with(@new_resource.service_name) + expect { @provider.start_service }.to raise_error( Chef::Exceptions::Service ) + @new_resource.updated_by_last_action?.should be_false + end + + it "should wait and continue if the service is in start_pending" do + Win32::Service.stub(:status).with(@new_resource.service_name).and_return( + double("StatusStruct", :current_state => "start pending"), + double("StatusStruct", :current_state => "start pending"), + double("StatusStruct", :current_state => "running")) + @provider.load_current_resource + Win32::Service.should_not_receive(:start).with(@new_resource.service_name) + @provider.start_service + @new_resource.updated_by_last_action?.should be_false + end + + it "should fail if the service is in stop_pending" do + Win32::Service.stub(:status).with(@new_resource.service_name).and_return( + double("StatusStruct", :current_state => "stop pending")) + @provider.load_current_resource + Win32::Service.should_not_receive(:start).with(@new_resource.service_name) + expect { @provider.start_service }.to raise_error( Chef::Exceptions::Service ) + @new_resource.updated_by_last_action?.should be_false + end + end + describe Chef::Provider::Service::Windows, "stop_service" do before(:each) do @@ -130,6 +161,36 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do @provider.stop_service @new_resource.updated_by_last_action?.should be_false end + + it "should raise an error if the service is paused" do + Win32::Service.stub(:status).with(@new_resource.service_name).and_return( + double("StatusStruct", :current_state => "paused")) + @provider.load_current_resource + Win32::Service.should_not_receive(:start).with(@new_resource.service_name) + expect { @provider.stop_service }.to raise_error( Chef::Exceptions::Service ) + @new_resource.updated_by_last_action?.should be_false + end + + it "should wait and continue if the service is in stop_pending" do + Win32::Service.stub(:status).with(@new_resource.service_name).and_return( + double("StatusStruct", :current_state => "stop pending"), + double("StatusStruct", :current_state => "stop pending"), + double("StatusStruct", :current_state => "stopped")) + @provider.load_current_resource + Win32::Service.should_not_receive(:stop).with(@new_resource.service_name) + @provider.stop_service + @new_resource.updated_by_last_action?.should be_false + end + + it "should fail if the service is in start_pending" do + Win32::Service.stub(:status).with(@new_resource.service_name).and_return( + double("StatusStruct", :current_state => "start pending")) + @provider.load_current_resource + Win32::Service.should_not_receive(:stop).with(@new_resource.service_name) + expect { @provider.stop_service }.to raise_error( Chef::Exceptions::Service ) + @new_resource.updated_by_last_action?.should be_false + end + end describe Chef::Provider::Service::Windows, "restart_service" do |