Suspending moved out to an action, command takes advantage of this action now.

This commit is contained in:
Mitchell Hashimoto 2010-03-04 21:13:17 -08:00
parent b4e25143dc
commit 0a791d1c58
6 changed files with 55 additions and 24 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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