From 0a54ea146494dcfebaf104093c98b97a3494b596 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Mar 2010 16:47:47 -0800 Subject: [PATCH] `vagrant-down` now uses reusable actions (a Down action) --- lib/vagrant/actions/vm/destroy.rb | 14 +++++++++++ lib/vagrant/actions/vm/down.rb | 12 ++++++++++ lib/vagrant/vm.rb | 5 +--- test/vagrant/actions/vm/destroy_test.rb | 24 +++++++++++++++++++ test/vagrant/actions/vm/down_test.rb | 32 +++++++++++++++++++++++++ test/vagrant/vm_test.rb | 16 ++----------- 6 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 lib/vagrant/actions/vm/destroy.rb create mode 100644 lib/vagrant/actions/vm/down.rb create mode 100644 test/vagrant/actions/vm/destroy_test.rb create mode 100644 test/vagrant/actions/vm/down_test.rb diff --git a/lib/vagrant/actions/vm/destroy.rb b/lib/vagrant/actions/vm/destroy.rb new file mode 100644 index 000000000..8c72d1302 --- /dev/null +++ b/lib/vagrant/actions/vm/destroy.rb @@ -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 diff --git a/lib/vagrant/actions/vm/down.rb b/lib/vagrant/actions/vm/down.rb new file mode 100644 index 000000000..2e9a7be1e --- /dev/null +++ b/lib/vagrant/actions/vm/down.rb @@ -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 diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 2d85d2f90..660bdc44c 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -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 diff --git a/test/vagrant/actions/vm/destroy_test.rb b/test/vagrant/actions/vm/destroy_test.rb new file mode 100644 index 000000000..e851748d4 --- /dev/null +++ b/test/vagrant/actions/vm/destroy_test.rb @@ -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 diff --git a/test/vagrant/actions/vm/down_test.rb b/test/vagrant/actions/vm/down_test.rb new file mode 100644 index 000000000..0d2b7b444 --- /dev/null +++ b/test/vagrant/actions/vm/down_test.rb @@ -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 diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index eb476f71b..a420d70ec 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -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