From a11fb717b03d1f5eaa4b11187cfec565c7c27d0d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 6 Jul 2010 21:27:20 -0700 Subject: [PATCH] Resume middleware --- lib/vagrant/action/builtin.rb | 7 ++++++ lib/vagrant/action/vm/resume.rb | 20 +++++++++++++++ lib/vagrant/vm.rb | 2 +- test/vagrant/action/vm/resume_test.rb | 35 +++++++++++++++++++++++++++ test/vagrant/vm_test.rb | 2 +- 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 lib/vagrant/action/vm/resume.rb create mode 100644 test/vagrant/action/vm/resume_test.rb diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index c78edad3d..48204e3fa 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -40,6 +40,13 @@ module Vagrant register :suspend, suspend + # resume - Resume a VM + resume = Builder.new do + use VM::Resume + end + + register :resume, resume + # reload - Halts then restarts the VM reload = Builder.new do use Action[:halt] diff --git a/lib/vagrant/action/vm/resume.rb b/lib/vagrant/action/vm/resume.rb new file mode 100644 index 000000000..e22cbdb40 --- /dev/null +++ b/lib/vagrant/action/vm/resume.rb @@ -0,0 +1,20 @@ +module Vagrant + class Action + module VM + class Resume + def initialize(app, env) + @app = app + end + + def call(env) + if env["vm"].vm.saved? + env.logger.info "Resuming suspended VM..." + env["vm"].start + end + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 316e65995..7f14213d0 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -130,7 +130,7 @@ module Vagrant end def resume - execute!(Actions::VM::Resume) + env.actions.run(:resume) end def saved? diff --git a/test/vagrant/action/vm/resume_test.rb b/test/vagrant/action/vm/resume_test.rb new file mode 100644 index 000000000..e4dfa4e27 --- /dev/null +++ b/test/vagrant/action/vm/resume_test.rb @@ -0,0 +1,35 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class ResumeVMActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::VM::Resume + @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 saved" do + @internal_vm.expects(:saved?).returns(true) + + seq = sequence("seq") + @vm.expects(:start).once.in_sequence(seq) + @app.expects(:call).with(@env).once.in_sequence(seq) + @instance.call(@env) + end + + should "do nothing if VM is not saved" do + @internal_vm.expects(:saved?).returns(false) + + @vm.expects(:start).never + @app.expects(:call).with(@env).once + @instance.call(@env) + end + end +end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 1144d5094..54f5e82b4 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -205,7 +205,7 @@ class VMTest < Test::Unit::TestCase context "resuming" do should "execute the resume action" do - @vm.expects(:execute!).with(Vagrant::Actions::VM::Resume).once + @vm.env.actions.expects(:run).with(:resume).once @vm.resume end end