diff --git a/lib/vagrant/action/vm/forward_ports.rb b/lib/vagrant/action/vm/forward_ports.rb index d828cff78..05237df44 100644 --- a/lib/vagrant/action/vm/forward_ports.rb +++ b/lib/vagrant/action/vm/forward_ports.rb @@ -10,7 +10,8 @@ module Vagrant @app = app @env = env - external_collision_check + threshold_check + external_collision_check if !env.error? end #-------------------------------------------------------------- @@ -18,6 +19,14 @@ module Vagrant # executing the action #-------------------------------------------------------------- + # This method checks for any forwarded ports on the host below + # 1024, which causes the forwarded ports to fail. + def threshold_check + @env.env.config.vm.forwarded_ports.each do |name, options| + return @env.error!(:vm_port_below_threshold) if options[:hostport] <= 1024 + end + end + # This method checks for any port collisions with any VMs # which are already created (by Vagrant or otherwise). # report the collisions detected or will attempt to fix them diff --git a/templates/strings.yml b/templates/strings.yml index b538d5a26..aea33e6b0 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -272,6 +272,11 @@ VM: <%= vm_name %> Forwarded port: <%= name %> (<%= options[:guestport] %> => <%= options[:hostport] %>) +:vm_port_below_threshold: |- + The host port of all forwarded ports must be above 1024. VirtualBox + does not allow host ports to be below 1024. (Guest ports below 1024 + are fine. For example: SSH on port 22 on the guest can be forwarded + to port 2222, but not 222). :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/vagrant/action/vm/forward_ports_test.rb b/test/vagrant/action/vm/forward_ports_test.rb index 429623ac0..f6e499932 100644 --- a/test/vagrant/action/vm/forward_ports_test.rb +++ b/test/vagrant/action/vm/forward_ports_test.rb @@ -12,11 +12,29 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase context "initializing" do should "call proper methods" do + @klass.any_instance.expects(:threshold_check) @klass.any_instance.expects(:external_collision_check) @klass.new(@app, @env) end end + context "checking for threshold" do + should "error if has a port below threshold" do + @env.env.config.vm.forwarded_ports.clear + @env.env.config.vm.forward_port("foo", 22, 222) + @klass.new(@app, @env) + assert @env.error? + assert_equal :vm_port_below_threshold, @env.error.first + end + + should "not error if ports are fine" do + @env.env.config.vm.forwarded_ports.clear + @env.env.config.vm.forward_port("foo", 22, 2222) + @klass.new(@app, @env) + assert !@env.error? + end + end + context "checking for colliding external ports" do setup do @env.env.config.vm.forwarded_ports.clear @@ -43,6 +61,7 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase context "with instance" do setup do + @klass.any_instance.stubs(:threshold_check) @klass.any_instance.stubs(:external_collision_check) @instance = @klass.new(@app, @env) end