Change the mechanism by which useless host only networks are detected and destroyed

This commit is contained in:
Mitchell Hashimoto 2010-12-24 18:57:44 -08:00
parent 4d8e3dc1bf
commit c32ab0442c
4 changed files with 21 additions and 20 deletions

View File

@ -3,6 +3,9 @@
- VirtualBox 4.0 support. Support for VirtualBox 3.2 is _dropped_, since - 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 the API is so different. Stay with the 0.6.x series if you have VirtualBox
3.2.x. 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) ## 0.6.9 (December 21, 2010)

View File

@ -62,9 +62,9 @@ module Vagrant
register(:destroy, Builder.new do register(:destroy, Builder.new do
use Action[:halt], :force => true use Action[:halt], :force => true
use VM::ClearNFSExports use VM::ClearNFSExports
use VM::DestroyUnusedNetworkInterfaces
use VM::Destroy use VM::Destroy
use VM::CleanMachineFolder use VM::CleanMachineFolder
use VM::DestroyUnusedNetworkInterfaces
end) end)
# package - Export and package the VM # package - Export and package the VM

View File

@ -9,20 +9,15 @@ module Vagrant
end end
def call(env) def call(env)
# We need to check if the host only network specified by any # Destroy all the host only network adapters which are empty.
# of the adapters would not have any more clients if it was VirtualBox::Global.global(true).host.network_interfaces.each do |iface|
# destroyed. And if so, then destroy the host only network # We only care about host only interfaces
# itself. next if iface.interface_type != :host_only
interfaces = env["vm"].vm.network_adapters.collect do |adapter|
adapter.host_interface_object
end
interfaces.compact.uniq.each do |interface| # Destroy it if there is nothing attached
# Destroy the network interface if there is only one if iface.attached_vms.empty?
# attached VM (which must be this VM)
if interface.attached_vms.length == 1
env.ui.info I18n.t("vagrant.actions.vm.destroy_network.destroying") env.ui.info I18n.t("vagrant.actions.vm.destroy_network.destroying")
interface.destroy iface.destroy
end end
end end

View File

@ -16,24 +16,27 @@ class DestroyUnusedNetworkInterfacesVMActionTest < Test::Unit::TestCase
context "calling" do context "calling" do
setup do setup do
@network_adapters = [] @interfaces = []
@internal_vm.stubs(:network_adapters).returns(@network_adapters) global = mock("global")
host = mock("host")
VirtualBox::Global.stubs(:global).returns(global)
global.stubs(:host).returns(host)
host.stubs(:network_interfaces).returns(@interfaces)
end end
def stub_interface(length=5) def stub_interface(length=5, type=:host_only)
interface = mock("interface") interface = mock("interface")
adapter = mock("adapter") interface.stubs(:interface_type).returns(type)
adapter.stubs(:host_interface_object).returns(interface)
interface.stubs(:attached_vms).returns(Array.new(length)) interface.stubs(:attached_vms).returns(Array.new(length))
@network_adapters << adapter @interfaces << interface
interface interface
end end
should "destroy only the unused network interfaces" do should "destroy only the unused network interfaces" do
stub_interface(5) stub_interface(5)
stub_interface(7) stub_interface(7)
results = [stub_interface(1), stub_interface(1)] results = [stub_interface(0), stub_interface(0)]
results.each do |result| results.each do |result|
result.expects(:destroy).once result.expects(:destroy).once