diff --git a/lib/vagrant/actions/vm/forward_ports.rb b/lib/vagrant/actions/vm/forward_ports.rb index 314eaba2a..8b4f34b50 100644 --- a/lib/vagrant/actions/vm/forward_ports.rb +++ b/lib/vagrant/actions/vm/forward_ports.rb @@ -46,6 +46,10 @@ module Vagrant range = runner.env.config.vm.auto_port_range.to_a range -= used_ports + if range.empty? + raise ActionException.new(:vm_port_auto_empty, :vm_name => @runner.name, :name => name, :options => options) + end + # Set the port up to be the first one and add that port to # the used list. options[:hostport] = range.shift diff --git a/templates/strings.yml b/templates/strings.yml index 1e5f87c65..662391b36 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -197,6 +197,13 @@ The vagrant virtual environment you are trying to suspend must be running to be suspended. :vm_not_suspended: |- The vagrant virtual environment you are trying to resume is not in a suspended state. +:vm_port_auto_empty: |- + Vagrant found a port collision for the specified port and virtual machine. + While this port was marked to be auto-corrected, the ports in the + auto-correction range are all also used. + + VM: <%= vm_name %> + Forwarded port: <%= name %> (<%= options[:guestport] %> => <%= options[:hostport] %>) :vm_port_collision: |- Vagrant cannot forward the specified ports on this VM, since they would collide with another VirtualBox virtual machine's forwarded diff --git a/test/test_helper.rb b/test/test_helper.rb index 60f6516a0..18ee012d6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -101,6 +101,7 @@ class Test::Unit::TestCase action = action_klass.new(mock_vm, *args) stub_default_action_dependecies(action) + mock_vm.stubs(:name).returns("foo") mock_vm.stubs(:vm).returns(vm) mock_vm.stubs(:vm=) mock_vm.stubs(:invoke_callback) diff --git a/test/vagrant/actions/vm/forward_ports_test.rb b/test/vagrant/actions/vm/forward_ports_test.rb index f21d38dc0..15497a324 100644 --- a/test/vagrant/actions/vm/forward_ports_test.rb +++ b/test/vagrant/actions/vm/forward_ports_test.rb @@ -72,7 +72,6 @@ class ForwardPortsActionTest < Test::Unit::TestCase @used_ports = [1,2,3] @runner.env.config.vm.auto_port_range = (1..5) - @auto_port_range = @runner.env.config.vm.auto_port_range.to_a end should "raise an exception if auto forwarding is disabled" do @@ -94,6 +93,13 @@ class ForwardPortsActionTest < Test::Unit::TestCase @action.handle_collision(@name, @options, @used_ports) assert @used_ports.include?(4) 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) { + @action.handle_collision(@name, @options, @used_ports) + } + end end context "execution" do