From cc0a1930d870003a01a63bd8b9c530f774a754dc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 27 May 2010 17:41:32 -0700 Subject: [PATCH] When handling collisions, the fixer won't set the port to something which is used by that VM later --- lib/vagrant/actions/vm/forward_ports.rb | 6 ++++-- test/vagrant/actions/vm/forward_ports_test.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/actions/vm/forward_ports.rb b/lib/vagrant/actions/vm/forward_ports.rb index 8b4f34b50..78840cf22 100644 --- a/lib/vagrant/actions/vm/forward_ports.rb +++ b/lib/vagrant/actions/vm/forward_ports.rb @@ -41,9 +41,11 @@ module Vagrant raise ActionException.new(:vm_port_collision, :name => name, :hostport => options[:hostport].to_s, :guestport => options[:guestport].to_s, :adapter => options[:adapter]) end - # Get the auto port range and get rid of the used ports so - # all we're left with is available ports + # Get the auto port range and get rid of the used ports and + # ports which are being used in other forwards so we're just + # left with available ports. range = runner.env.config.vm.auto_port_range.to_a + range -= runner.env.config.vm.forwarded_ports.collect { |n, o| o[:hostport].to_i } range -= used_ports if range.empty? diff --git a/test/vagrant/actions/vm/forward_ports_test.rb b/test/vagrant/actions/vm/forward_ports_test.rb index 15497a324..f8d478b17 100644 --- a/test/vagrant/actions/vm/forward_ports_test.rb +++ b/test/vagrant/actions/vm/forward_ports_test.rb @@ -94,6 +94,14 @@ class ForwardPortsActionTest < Test::Unit::TestCase assert @used_ports.include?(4) end + should "not use a host port which is being forwarded later" do + @runner.env.config.vm.forward_port("http", 80, 4) + + assert_equal 0, @options[:hostport] + @action.handle_collision(@name, @options, @used_ports) + assert_equal 5, @options[:hostport] + end + should "raise an exception if there are no auto ports available" do @runner.env.config.vm.auto_port_range = (1..3) assert_raises(Vagrant::Actions::ActionException) {