From 3ed9222ea59878c103a5df3b8174cd675e5a0cb5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 6 Jul 2010 21:22:55 -0700 Subject: [PATCH] Suspend middleware --- lib/vagrant/action/builtin.rb | 7 ++++++ lib/vagrant/action/vm/suspend.rb | 20 +++++++++++++++ lib/vagrant/vm.rb | 2 +- test/vagrant/action/vm/suspend_test.rb | 35 ++++++++++++++++++++++++++ test/vagrant/vm_test.rb | 2 +- 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 lib/vagrant/action/vm/suspend.rb create mode 100644 test/vagrant/action/vm/suspend_test.rb diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 65a77eb03..c78edad3d 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -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] diff --git a/lib/vagrant/action/vm/suspend.rb b/lib/vagrant/action/vm/suspend.rb new file mode 100644 index 000000000..abda51e63 --- /dev/null +++ b/lib/vagrant/action/vm/suspend.rb @@ -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 diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index bad10f711..316e65995 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -126,7 +126,7 @@ module Vagrant end def suspend - execute!(Actions::VM::Suspend) + env.actions.run(:suspend) end def resume diff --git a/test/vagrant/action/vm/suspend_test.rb b/test/vagrant/action/vm/suspend_test.rb new file mode 100644 index 000000000..0084b9db9 --- /dev/null +++ b/test/vagrant/action/vm/suspend_test.rb @@ -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 diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 656bee4f1..1144d5094 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -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