Improved lock-reducing of forwarded ports action

This commit is contained in:
Mitchell Hashimoto 2011-07-08 00:03:33 -07:00
parent 2492f479d5
commit 268f7f7bc2
2 changed files with 13 additions and 15 deletions

View File

@ -83,14 +83,15 @@ module Vagrant
def call(env)
@env = env
forward_ports
@env["config"].vm.customize do |vm|
@env.ui.info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
forward_ports(vm)
end
@app.call(env)
end
def forward_ports
@env.ui.info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
def forward_ports(vm)
@env.env.config.vm.forwarded_ports.each do |name, options|
adapter = options[:adapter]
message_attributes = {
@ -103,13 +104,11 @@ module Vagrant
# Assuming the only reason to establish port forwarding is because the VM is using Virtualbox NAT networking.
# Host-only or Bridged networking don't require port-forwarding and establishing forwarded ports on these
# attachment types has uncertain behaviour.
@env["config"].vm.customize do |vm|
if @env["vm"].vm.network_adapters[adapter].attachment_type == :nat
@env.ui.info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry", message_attributes))
forward_port(vm, name, options)
else
@env.ui.info(I18n.t("vagrant.actions.vm.forward_ports.non_nat", message_attributes))
end
if vm.network_adapters[adapter].attachment_type == :nat
@env.ui.info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry", message_attributes))
forward_port(vm, name, options)
else
@env.ui.info(I18n.t("vagrant.actions.vm.forward_ports.non_nat", message_attributes))
end
end
end

View File

@ -123,6 +123,7 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
context "calling" do
should "clear all previous ports and forward new ports" do
exec_seq = sequence("exec_seq")
@env["config"].vm.expects(:customize).yields(@internal_vm).in_sequence(exec_seq)
@instance.expects(:forward_ports).once.in_sequence(exec_seq)
@app.expects(:call).once.with(@env).in_sequence(exec_seq)
@instance.call(@env)
@ -144,8 +145,7 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
@instance.expects(:forward_port).once
@env["config"].vm.stubs(:customize).yields(@internal_vm)
@instance.forward_ports
@instance.forward_ports(@internal_vm)
end
should "not port forward for non NAT interfaces" do
@ -155,8 +155,7 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
@internal_vm.expects(:network_adapters).returns([network_adapter])
network_adapter.expects(:attachment_type).returns(:host_only)
@env["config"].vm.stubs(:customize).yields(@internal_vm)
@instance.forward_ports
@instance.forward_ports(@internal_vm)
end
end