From c08937d8998d35a5b665903591c55a7b4a6e8686 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 14 Mar 2010 16:57:29 -0700 Subject: [PATCH] Dotfile and active list are now updated when environment is torn down --- lib/vagrant/actions/vm/destroy.rb | 13 ++++++++-- lib/vagrant/env.rb | 8 ++++++ test/vagrant/actions/vm/destroy_test.rb | 29 ++++++++++++++++------ test/vagrant/env_test.rb | 33 +++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/lib/vagrant/actions/vm/destroy.rb b/lib/vagrant/actions/vm/destroy.rb index 8c72d1302..d30d78f41 100644 --- a/lib/vagrant/actions/vm/destroy.rb +++ b/lib/vagrant/actions/vm/destroy.rb @@ -4,10 +4,19 @@ module Vagrant class Destroy < Base def execute! @runner.invoke_around_callback(:destroy) do - logger.info "Destroying VM and associated drives..." - @runner.vm.destroy(:destroy_image => true) + destroy_vm + depersist end end + + def destroy_vm + logger.info "Destroying VM and associated drives..." + @runner.vm.destroy(:destroy_image => true) + end + + def depersist + Env.depersist_vm(@runner) + end end end end diff --git a/lib/vagrant/env.rb b/lib/vagrant/env.rb index da43196f2..0142d402f 100644 --- a/lib/vagrant/env.rb +++ b/lib/vagrant/env.rb @@ -116,6 +116,14 @@ msg ActiveList.add(vm) end + def depersist_vm(vm) + # Delete the dotfile if it exists + File.delete(dotfile_path) if File.exist?(dotfile_path) + + # Remove from the global store + ActiveList.remove(vm) + end + def load_root_path!(path=nil) path = Pathname.new(File.expand_path(path || Dir.pwd)) diff --git a/test/vagrant/actions/vm/destroy_test.rb b/test/vagrant/actions/vm/destroy_test.rb index e851748d4..4ce5aa0d6 100644 --- a/test/vagrant/actions/vm/destroy_test.rb +++ b/test/vagrant/actions/vm/destroy_test.rb @@ -2,23 +2,36 @@ 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) + @runner, @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 + @runner.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 + should "destroy VM and clear persist" do + @runner.stubs(:invoke_around_callback).yields + clear_seq = sequence("clear") + @action.expects(:destroy_vm).in_sequence(clear_seq) + @action.expects(:depersist).in_sequence(clear_seq) @action.execute! end end + + context "destroying the VM" do + should "destroy VM and attached images" do + @vm.expects(:destroy).with(:destroy_image => true).once + @action.destroy_vm + end + end + + context "depersisting" do + should "call depersist_vm on Env" do + Vagrant::Env.expects(:depersist_vm).with(@runner).once + @action.depersist + end + end end diff --git a/test/vagrant/env_test.rb b/test/vagrant/env_test.rb index 65eebfc16..1275927fb 100644 --- a/test/vagrant/env_test.rb +++ b/test/vagrant/env_test.rb @@ -176,8 +176,6 @@ class EnvTest < Test::Unit::TestCase context "persisting the VM into a file" do setup do - mock_config - @vm = mock("vm") @vm.stubs(:uuid).returns("foo") @@ -198,6 +196,37 @@ class EnvTest < Test::Unit::TestCase end end + context "depersisting the VM" do + setup do + File.stubs(:exist?).returns(false) + File.stubs(:delete) + + Vagrant::ActiveList.stubs(:remove) + + @dotfile_path = "foo" + Vagrant::Env.stubs(:dotfile_path).returns(@dotfile_path) + + @vm = mock("vm") + end + + should "remove the dotfile if it exists" do + File.expects(:exist?).with(Vagrant::Env.dotfile_path).returns(true) + File.expects(:delete).with(Vagrant::Env.dotfile_path).once + Vagrant::Env.depersist_vm(@vm) + end + + should "not remove the dotfile if it doesn't exist" do + File.expects(:exist?).returns(false) + File.expects(:delete).never + Vagrant::Env.depersist_vm(@vm) + end + + should "remove from the active list" do + Vagrant::ActiveList.expects(:remove).with(@vm) + Vagrant::Env.depersist_vm(@vm) + end + end + context "loading the UUID out from the persisted file" do setup do File.stubs(:file?).returns(true)