diff options
author | Chris Doherty <cdoherty@getchef.com> | 2014-09-08 16:34:10 -0700 |
---|---|---|
committer | Chris Doherty <cdoherty@getchef.com> | 2014-09-10 16:34:31 -0700 |
commit | 6d3aa8327224e25d6011b46b8873cab5e7a56031 (patch) | |
tree | e51748930df65a9b87f7535a8685b38be5280b92 | |
parent | 2456f0c47df72268e1c73c9c5109771a59f10938 (diff) | |
download | chef-6d3aa8327224e25d6011b46b8873cab5e7a56031.tar.gz |
Add #reboot! to Rebooter for immediate rebooting; make Rebooter stateless;
add specs.
-rw-r--r-- | lib/chef/platform/rebooter.rb | 13 | ||||
-rw-r--r-- | spec/functional/rebooter_spec.rb | 49 |
2 files changed, 43 insertions, 19 deletions
diff --git a/lib/chef/platform/rebooter.rb b/lib/chef/platform/rebooter.rb index 75213b3fc7..08224b4d77 100644 --- a/lib/chef/platform/rebooter.rb +++ b/lib/chef/platform/rebooter.rb @@ -26,9 +26,10 @@ class Chef extend Chef::Mixin::ShellOut class << self - attr_reader :node, :reboot_info - def reboot! + def reboot!(node) + reboot_info = node.run_context.reboot_info + cmd = if Chef::Platform.windows? "shutdown /r /t #{reboot_info[:delay_mins]} /c \"#{reboot_info[:reason]}\"" else @@ -36,15 +37,13 @@ class Chef "shutdown -r +#{reboot_info[:delay_mins]} \"#{reboot_info[:reason]}\"" end + Chef::Log.warn "Rebooting server at a recipe's request. Details: #{reboot_info.inspect}" shell_out!(cmd) end - def reboot_if_needed!(this_node) - @node = this_node - @reboot_info = node.run_context.reboot_info - + def reboot_if_needed!(node) if node.run_context.reboot_requested? - reboot! + reboot!(node) end end end diff --git a/spec/functional/rebooter_spec.rb b/spec/functional/rebooter_spec.rb index 218b47b74e..8006580d5c 100644 --- a/spec/functional/rebooter_spec.rb +++ b/spec/functional/rebooter_spec.rb @@ -41,6 +41,13 @@ describe Chef::Platform::Rebooter do Chef::RunContext.new(node, {}, events) end + let(:expected) do + { + :windows => 'shutdown /r /t 5 /c "rebooter spec test"', + :linux => 'shutdown -r +5 "rebooter spec test"' + } + end + let(:rebooter) { Chef::Platform::Rebooter } describe '#reboot_if_needed!' do @@ -51,10 +58,9 @@ describe Chef::Platform::Rebooter do rebooter.reboot_if_needed!(run_context.node) end - describe 'calling #shell_out! when reboot has been requested' do + describe 'calling #shell_out! to reboot' do before(:each) do - Chef::Platform.stub(:node).and_return(run_context.node) run_context.request_reboot(reboot_info) end @@ -62,18 +68,37 @@ describe Chef::Platform::Rebooter do run_context.cancel_reboot end - it 'should produce the correct string on Windows' do - Chef::Platform.stub(:windows?).and_return(true) - expect(rebooter).to receive(:shell_out!).once.with('shutdown /r /t 5 /c "rebooter spec test"') - expect(rebooter).to receive(:reboot_if_needed!).once.and_call_original - rebooter.reboot_if_needed!(run_context.node) + shared_context 'test a reboot method' do + def test_rebooter_method(method_sym, is_windows, expected_reboot_str) + Chef::Platform.stub(:windows?).and_return(is_windows) + expect(rebooter).to receive(:shell_out!).once.with(expected_reboot_str) + expect(rebooter).to receive(method_sym).once.and_call_original + rebooter.send(method_sym, run_context.node) + end + end + + describe 'when using #reboot_if_needed!' do + include_context 'test a reboot method' + + it 'should produce the correct string on Windows' do + test_rebooter_method(:reboot_if_needed!, true, expected[:windows]) + end + + it 'should produce the correct (Linux-specific) string on non-Windows' do + test_rebooter_method(:reboot_if_needed!, false, expected[:linux]) + end end - it 'should produce the correct (Linux-specific) string on non-Windows' do - Chef::Platform.stub(:windows?).and_return(false) - expect(rebooter).to receive(:shell_out!).once.with('shutdown -r +5 "rebooter spec test"') - expect(rebooter).to receive(:reboot_if_needed!).once.and_call_original - rebooter.reboot_if_needed!(run_context.node) + describe 'when using #reboot!' do + include_context 'test a reboot method' + + it 'should produce the correct string on Windows' do + test_rebooter_method(:reboot!, true, expected[:windows]) + end + + it 'should produce the correct (Linux-specific) string on non-Windows' do + test_rebooter_method(:reboot!, false, expected[:linux]) + end end end end |