From e825847dac1d07b0985a56672b5ded589818db66 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 16 Feb 2010 13:21:27 -0800 Subject: [PATCH] VM instances can execute a single command now (VM#execute!(foo)) and added "stop" action to force immediate shutdown of VM. --- lib/vagrant/actions/stop.rb | 10 ++++++++++ lib/vagrant/vm.rb | 12 +++++++----- test/vagrant/actions/stop_test.rb | 12 ++++++++++++ test/vagrant/vm_test.rb | 18 +++++++++++++++++- 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 lib/vagrant/actions/stop.rb create mode 100644 test/vagrant/actions/stop_test.rb diff --git a/lib/vagrant/actions/stop.rb b/lib/vagrant/actions/stop.rb new file mode 100644 index 000000000..e628f01f7 --- /dev/null +++ b/lib/vagrant/actions/stop.rb @@ -0,0 +1,10 @@ +module Vagrant + module Actions + class Stop < Base + def execute! + logger.info "Forcing shutdown of VM..." + @vm.vm.stop(true) + end + end + end +end \ No newline at end of file diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 733862569..3836c6343 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -88,7 +88,12 @@ error @actions << action_klass.new(self, *args) end - def execute! + def execute!(single_action=nil, *args) + if single_action + @actions.clear + add_action(single_action, *args) + end + # Call the prepare method on each once its # initialized, then call the execute! method [:prepare, :execute!].each do |method| @@ -118,10 +123,7 @@ error end def destroy - if @vm.running? - logger.info "VM is running. Forcing immediate shutdown..." - @vm.stop(true) - end + execute!(Actions::Stop) if @vm.running? logger.info "Destroying VM and associated drives..." @vm.destroy(:destroy_image => true) diff --git a/test/vagrant/actions/stop_test.rb b/test/vagrant/actions/stop_test.rb new file mode 100644 index 000000000..1fa8e7a24 --- /dev/null +++ b/test/vagrant/actions/stop_test.rb @@ -0,0 +1,12 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') + +class StopActionTest < Test::Unit::TestCase + setup do + @mock_vm, @vm, @action = mock_action(Vagrant::Actions::Stop) + end + + should "force the VM to stop" do + @vm.expects(:stop).with(true).once + @action.execute! + end +end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index e5467383c..4b875515f 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -92,6 +92,21 @@ class VMTest < Test::Unit::TestCase @vm.add_action(action_klass, "foo", "bar") end + should "clear the actions and run a single action if given to execute!" do + action = mock("action") + run_action = mock("action_run") + run_class = mock("run_class") + run_class.expects(:new).once.returns(run_action) + @vm.actions << action + + [:prepare, :execute!].each do |method| + action.expects(method).never + run_action.expects(method).once + end + + @vm.execute!(run_class) + end + should "run #prepare on all actions, then #execute!" do action_seq = sequence("action_seq") actions = [] @@ -154,6 +169,7 @@ class VMTest < Test::Unit::TestCase context "destroying" do setup do @mock_vm.stubs(:running?).returns(false) + @vm.stubs(:execute!) end should "destoy the VM along with images" do @@ -163,8 +179,8 @@ class VMTest < Test::Unit::TestCase should "stop the VM if its running" do @mock_vm.expects(:running?).returns(true) - @mock_vm.expects(:stop).with(true) @mock_vm.expects(:destroy).with(:destroy_image => true).once + @vm.expects(:execute!).with(Vagrant::Actions::Stop).once @vm.destroy end end