VM instances can execute a single command now (VM#execute!(foo)) and added "stop" action to force immediate shutdown of VM.

This commit is contained in:
Mitchell Hashimoto 2010-02-16 13:21:27 -08:00
parent 7c61792b19
commit e825847dac
4 changed files with 46 additions and 6 deletions

View File

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

View File

@ -88,7 +88,12 @@ error
@actions << action_klass.new(self, *args) @actions << action_klass.new(self, *args)
end 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 # Call the prepare method on each once its
# initialized, then call the execute! method # initialized, then call the execute! method
[:prepare, :execute!].each do |method| [:prepare, :execute!].each do |method|
@ -118,10 +123,7 @@ error
end end
def destroy def destroy
if @vm.running? execute!(Actions::Stop) if @vm.running?
logger.info "VM is running. Forcing immediate shutdown..."
@vm.stop(true)
end
logger.info "Destroying VM and associated drives..." logger.info "Destroying VM and associated drives..."
@vm.destroy(:destroy_image => true) @vm.destroy(:destroy_image => true)

View File

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

View File

@ -92,6 +92,21 @@ class VMTest < Test::Unit::TestCase
@vm.add_action(action_klass, "foo", "bar") @vm.add_action(action_klass, "foo", "bar")
end 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 should "run #prepare on all actions, then #execute!" do
action_seq = sequence("action_seq") action_seq = sequence("action_seq")
actions = [] actions = []
@ -154,6 +169,7 @@ class VMTest < Test::Unit::TestCase
context "destroying" do context "destroying" do
setup do setup do
@mock_vm.stubs(:running?).returns(false) @mock_vm.stubs(:running?).returns(false)
@vm.stubs(:execute!)
end end
should "destoy the VM along with images" do 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 should "stop the VM if its running" do
@mock_vm.expects(:running?).returns(true) @mock_vm.expects(:running?).returns(true)
@mock_vm.expects(:stop).with(true)
@mock_vm.expects(:destroy).with(:destroy_image => true).once @mock_vm.expects(:destroy).with(:destroy_image => true).once
@vm.expects(:execute!).with(Vagrant::Actions::Stop).once
@vm.destroy @vm.destroy
end end
end end