From 0a791d1c58f7e23735b93d5f184e86e20702c41a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 4 Mar 2010 21:13:17 -0800 Subject: [PATCH] Suspending moved out to an action, command takes advantage of this action now. --- lib/vagrant/actions/vm/suspend.rb | 16 +++++++++++++++ lib/vagrant/commands.rb | 6 +----- lib/vagrant/vm.rb | 9 ++++----- test/vagrant/actions/vm/suspend_test.rb | 27 +++++++++++++++++++++++++ test/vagrant/commands_test.rb | 13 +++--------- test/vagrant/vm_test.rb | 8 ++++---- 6 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 lib/vagrant/actions/vm/suspend.rb create mode 100644 test/vagrant/actions/vm/suspend_test.rb diff --git a/lib/vagrant/actions/vm/suspend.rb b/lib/vagrant/actions/vm/suspend.rb new file mode 100644 index 000000000..b1f43c507 --- /dev/null +++ b/lib/vagrant/actions/vm/suspend.rb @@ -0,0 +1,16 @@ +module Vagrant + module Actions + module VM + class Suspend < Base + def execute! + if !@runner.vm.running? + raise ActionException.new("The vagrant virtual environment you are trying to suspend must be running to be suspended.") + end + + logger.info "Saving VM state and suspending execution..." + @runner.vm.save_state(true) + end + end + end + end +end diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 4e1cd4c92..d195a57c5 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -96,11 +96,7 @@ error def suspend Env.load! Env.require_persisted_vm - error_and_exit(<<-error) if Env.persisted_vm.saved? -The vagrant virtual environment you are trying to suspend is already in a -suspended state. -error - Env.persisted_vm.save_state(true) + Env.persisted_vm.suspend end # Resume a running vagrant instance. This resumes an already suspended diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 9502fd70c..847597ed9 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -43,13 +43,12 @@ module Vagrant @vm.destroy(:destroy_image => true) end - def saved? - @vm.saved? + def suspend + execute!(Actions::VM::Suspend) end - def save_state - logger.info "Saving VM state..." - @vm.save_state(true) + def saved? + @vm.saved? end def powered_off?; @vm.powered_off? end diff --git a/test/vagrant/actions/vm/suspend_test.rb b/test/vagrant/actions/vm/suspend_test.rb new file mode 100644 index 000000000..e296b7011 --- /dev/null +++ b/test/vagrant/actions/vm/suspend_test.rb @@ -0,0 +1,27 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class SuspendActionTest < Test::Unit::TestCase + setup do + @runner, @vm, @action = mock_action(Vagrant::Actions::VM::Suspend) + mock_config + end + + context "executing" do + setup do + @vm.stubs(:running?).returns(true) + end + + should "save the state of the VM" do + @vm.expects(:save_state).with(true).once + @action.execute! + end + + should "raise an ActionException if the VM is not running" do + @vm.expects(:running?).returns(false) + @vm.expects(:save_state).never + assert_raises(Vagrant::Actions::ActionException) { + @action.execute! + } + end + end +end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 3e3dd5389..2c185ed2d 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -117,7 +117,7 @@ class CommandsTest < Test::Unit::TestCase context "suspend" do setup do - @persisted_vm.stubs(:save_state) + @persisted_vm.stubs(:suspend) @persisted_vm.stubs(:saved?).returns(false) end @@ -126,15 +126,8 @@ class CommandsTest < Test::Unit::TestCase Vagrant::Commands.suspend end - should "error and exit if the VM is already saved" do - @persisted_vm.expects(:saved?).returns(true) - Vagrant::Commands.expects(:error_and_exit).once - @persisted_vm.expects(:save_state).never - Vagrant::Commands.suspend - end - - should "save the state of the VM" do - @persisted_vm.expects(:save_state).once + should "suspend the VM" do + @persisted_vm.expects(:suspend).once Vagrant::Commands.suspend end end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index f493f30e5..066fe8ca9 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -67,15 +67,15 @@ class VMTest < Test::Unit::TestCase end end - context "saving the state" do + context "suspending" do should "check if a VM is saved" do @mock_vm.expects(:saved?).returns("foo") assert_equal "foo", @vm.saved? end - should "save state with errors raised" do - @mock_vm.expects(:save_state).with(true).once - @vm.save_state + should "execute the suspend action" do + @vm.expects(:execute!).with(Vagrant::Actions::VM::Suspend).once + @vm.suspend end end