Suspend middleware

This commit is contained in:
Mitchell Hashimoto 2010-07-06 21:22:55 -07:00
parent d313a106a3
commit 3ed9222ea5
5 changed files with 64 additions and 2 deletions

View File

@ -33,6 +33,13 @@ module Vagrant
register :halt, halt
# suspend - Suspends the VM
suspend = Builder.new do
use VM::Suspend
end
register :suspend, suspend
# reload - Halts then restarts the VM
reload = Builder.new do
use Action[:halt]

View File

@ -0,0 +1,20 @@
module Vagrant
class Action
module VM
class Suspend
def initialize(app, env)
@app = app
end
def call(env)
if env["vm"].vm.running?
env.logger.info "Saving VM state and suspending execution..."
env["vm"].vm.save_state
end
@app.call(env)
end
end
end
end
end

View File

@ -126,7 +126,7 @@ module Vagrant
end
def suspend
execute!(Actions::VM::Suspend)
env.actions.run(:suspend)
end
def resume

View File

@ -0,0 +1,35 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class SuspendVMActionTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Action::VM::Suspend
@app, @env = mock_action_data
@vm = mock("vm")
@env["vm"] = @vm
@internal_vm = mock("internal")
@vm.stubs(:vm).returns(@internal_vm)
@instance = @klass.new(@app, @env)
end
context "calling" do
should "run the proper methods when running" do
@internal_vm.expects(:running?).returns(true)
seq = sequence("seq")
@internal_vm.expects(:save_state).once.in_sequence(seq)
@app.expects(:call).with(@env).once.in_sequence(seq)
@instance.call(@env)
end
should "do nothing if VM is not running" do
@internal_vm.expects(:running?).returns(false)
@internal_vm.expects(:save_state).never
@app.expects(:call).with(@env).once
@instance.call(@env)
end
end
end

View File

@ -198,7 +198,7 @@ class VMTest < Test::Unit::TestCase
context "suspending" do
should "execute the suspend action" do
@vm.expects(:execute!).with(Vagrant::Actions::VM::Suspend).once
@vm.env.actions.expects(:run).with(:suspend).once
@vm.suspend
end
end