Disable host only networks on halt [closes GH-116]

This commit is contained in:
Mitchell Hashimoto 2010-07-21 21:01:38 -07:00
parent f2242662f8
commit 4e8b6f32b4
5 changed files with 73 additions and 0 deletions

View File

@ -33,6 +33,7 @@ module Vagrant
# a restart if fails.
halt = Builder.new do
use VM::Halt
use VM::DisableNetworks
end
register :halt, halt

View File

@ -0,0 +1,26 @@
module Vagrant
class Action
module VM
# Middleware to disable all host only networks on the
# VM
class DisableNetworks
def initialize(app, env)
@app = app
@env = env
end
def call(env)
env.logger.info "Disabling host only networks..."
env["vm"].vm.network_adapters.each do |adapter|
next if adapter.attachment_type != :host_only
adapter.enabled = false
adapter.save
end
@app.call(env)
end
end
end
end
end

View File

@ -20,6 +20,10 @@ module Vagrant
env.logger.info "Forcing shutdown of VM..."
env["vm"].vm.stop
end
# Sleep for a second to verify that the VM properly
# cleans itself up
sleep 1
end
@app.call(env)

View File

@ -0,0 +1,39 @@
require "test_helper"
class DisableNetworksVMActionTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Action::VM::DisableNetworks
@app, @env = mock_action_data
@vm = mock("vm")
@env.env.stubs(:vm).returns(@vm)
@internal_vm = mock("internal")
@vm.stubs(:vm).returns(@internal_vm)
@internal_vm.stubs(:network_adapters).returns([])
@instance = @klass.new(@app, @env)
end
def mock_adapter(type)
adapter = mock("adapter")
adapter.stubs(:attachment_type).returns(type)
if type == :host_only
adapter.expects(:enabled=).with(false)
adapter.expects(:save)
end
@internal_vm.network_adapters << adapter
end
should "remove all network adapters and continue chain" do
mock_adapter(:bridged)
mock_adapter(:host_only)
mock_adapter(:host_only)
@app.expects(:call).with(@env).once
@instance.call(@env)
end
end

View File

@ -54,6 +54,7 @@ Gem::Specification.new do |s|
"lib/vagrant/action/vm/customize.rb",
"lib/vagrant/action/vm/destroy.rb",
"lib/vagrant/action/vm/destroy_unused_network_interfaces.rb",
"lib/vagrant/action/vm/disable_networks.rb",
"lib/vagrant/action/vm/export.rb",
"lib/vagrant/action/vm/forward_ports.rb",
"lib/vagrant/action/vm/forward_ports_helpers.rb",
@ -144,6 +145,7 @@ Gem::Specification.new do |s|
"test/vagrant/action/vm/customize_test.rb",
"test/vagrant/action/vm/destroy_test.rb",
"test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb",
"test/vagrant/action/vm/disable_networks_test.rb",
"test/vagrant/action/vm/export_test.rb",
"test/vagrant/action/vm/forward_ports_helpers_test.rb",
"test/vagrant/action/vm/forward_ports_test.rb",
@ -230,6 +232,7 @@ Gem::Specification.new do |s|
"test/vagrant/action/vm/customize_test.rb",
"test/vagrant/action/vm/destroy_test.rb",
"test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb",
"test/vagrant/action/vm/disable_networks_test.rb",
"test/vagrant/action/vm/export_test.rb",
"test/vagrant/action/vm/forward_ports_helpers_test.rb",
"test/vagrant/action/vm/forward_ports_test.rb",