When handling collisions, the fixer won't set the port to something which is used by that VM later

This commit is contained in:
Mitchell Hashimoto 2010-05-27 17:41:32 -07:00
parent 61bcc761bc
commit cc0a1930d8
2 changed files with 12 additions and 2 deletions

View File

@ -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?

View File

@ -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) {