`vagrant-down` now uses reusable actions (a Down action)

This commit is contained in:
Mitchell Hashimoto 2010-03-05 16:47:47 -08:00
parent 0cdc6b5eaa
commit 0a54ea1464
6 changed files with 85 additions and 18 deletions

View File

@ -0,0 +1,14 @@
module Vagrant
module Actions
module VM
class Destroy < Base
def execute!
@runner.invoke_around_callback(:destroy) do
logger.info "Destroying VM and associated drives..."
@runner.vm.destroy(:destroy_image => true)
end
end
end
end
end
end

View File

@ -0,0 +1,12 @@
module Vagrant
module Actions
module VM
class Down < Base
def prepare
@runner.add_action(Halt) if @runner.vm.running?
@runner.add_action(Destroy)
end
end
end
end
end

View File

@ -37,10 +37,7 @@ module Vagrant
end
def destroy
execute!(Actions::VM::Halt) if @vm.running?
logger.info "Destroying VM and associated drives..."
@vm.destroy(:destroy_image => true)
execute!(Actions::VM::Down)
end
def suspend

View File

@ -0,0 +1,24 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class DestroyActionTest < Test::Unit::TestCase
setup do
@mock_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Destroy)
mock_config
end
context "executing" do
setup do
@vm.stubs(:destroy)
end
should "invoke an around callback around the destroy" do
@mock_vm.expects(:invoke_around_callback).with(:destroy).once
@action.execute!
end
should "destroy VM and attached images" do
@vm.expects(:destroy).with(:destroy_image => true).once
@action.execute!
end
end
end

View File

@ -0,0 +1,32 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class DownActionTest < Test::Unit::TestCase
setup do
@mock_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Down)
mock_config
end
context "preparing" do
setup do
@vm.stubs(:running?).returns(false)
end
def setup_action_expectations(order)
default_seq = sequence("default_seq")
order.each do |action|
@mock_vm.expects(:add_action).with(action).once.in_sequence(default_seq)
end
end
should "add the destroy action alone if VM is not running" do
setup_action_expectations([Vagrant::Actions::VM::Destroy])
@action.prepare
end
should "add the halt action if the VM is running" do
@vm.expects(:running?).returns(true)
setup_action_expectations([Vagrant::Actions::VM::Halt, Vagrant::Actions::VM::Destroy])
@action.prepare
end
end
end

View File

@ -49,20 +49,8 @@ class VMTest < Test::Unit::TestCase
end
context "destroying" do
setup do
@mock_vm.stubs(:running?).returns(false)
@vm.stubs(:execute!)
end
should "destoy the VM along with images" do
@mock_vm.expects(:destroy).with(:destroy_image => true).once
@vm.destroy
end
should "stop the VM if its running" do
@mock_vm.expects(:running?).returns(true)
@mock_vm.expects(:destroy).with(:destroy_image => true).once
@vm.expects(:execute!).with(Vagrant::Actions::VM::Halt).once
should "execute the down action" do
@vm.expects(:execute!).with(Vagrant::Actions::VM::Down).once
@vm.destroy
end
end