diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a59cf368..28bfc926c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ - VirtualBox 4.0 support. Support for VirtualBox 3.2 is _dropped_, since the API is so different. Stay with the 0.6.x series if you have VirtualBox 3.2.x. + - Changed the unused host only network destroy mechanism to check for + uselessness after the VM is destroyed. This should result in more accurate + checks. ## 0.6.9 (December 21, 2010) diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index f1aef3892..f63ab9171 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -62,9 +62,9 @@ module Vagrant register(:destroy, Builder.new do use Action[:halt], :force => true use VM::ClearNFSExports - use VM::DestroyUnusedNetworkInterfaces use VM::Destroy use VM::CleanMachineFolder + use VM::DestroyUnusedNetworkInterfaces end) # package - Export and package the VM diff --git a/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb b/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb index 66b903146..0730d1ab6 100644 --- a/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb +++ b/lib/vagrant/action/vm/destroy_unused_network_interfaces.rb @@ -9,20 +9,15 @@ module Vagrant end def call(env) - # We need to check if the host only network specified by any - # of the adapters would not have any more clients if it was - # destroyed. And if so, then destroy the host only network - # itself. - interfaces = env["vm"].vm.network_adapters.collect do |adapter| - adapter.host_interface_object - end + # Destroy all the host only network adapters which are empty. + VirtualBox::Global.global(true).host.network_interfaces.each do |iface| + # We only care about host only interfaces + next if iface.interface_type != :host_only - interfaces.compact.uniq.each do |interface| - # Destroy the network interface if there is only one - # attached VM (which must be this VM) - if interface.attached_vms.length == 1 + # Destroy it if there is nothing attached + if iface.attached_vms.empty? env.ui.info I18n.t("vagrant.actions.vm.destroy_network.destroying") - interface.destroy + iface.destroy end end diff --git a/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb b/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb index 95cc12f8d..7e4a9c7a0 100644 --- a/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb +++ b/test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb @@ -16,24 +16,27 @@ class DestroyUnusedNetworkInterfacesVMActionTest < Test::Unit::TestCase context "calling" do setup do - @network_adapters = [] - @internal_vm.stubs(:network_adapters).returns(@network_adapters) + @interfaces = [] + global = mock("global") + host = mock("host") + VirtualBox::Global.stubs(:global).returns(global) + global.stubs(:host).returns(host) + host.stubs(:network_interfaces).returns(@interfaces) end - def stub_interface(length=5) + def stub_interface(length=5, type=:host_only) interface = mock("interface") - adapter = mock("adapter") - adapter.stubs(:host_interface_object).returns(interface) + interface.stubs(:interface_type).returns(type) interface.stubs(:attached_vms).returns(Array.new(length)) - @network_adapters << adapter + @interfaces << interface interface end should "destroy only the unused network interfaces" do stub_interface(5) stub_interface(7) - results = [stub_interface(1), stub_interface(1)] + results = [stub_interface(0), stub_interface(0)] results.each do |result| result.expects(:destroy).once