diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index 917cbde8d..aafb1e396 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -269,7 +269,8 @@ module Vagrant end else # Do a regular check - if test_host_ip == "0.0.0.0" || Vagrant::Util::IPv4Interfaces.ipv4_interfaces.detect { |iface| iface[1] == test_host_ip } + if test_host_ip == "0.0.0.0" || Addrinfo.ip(test_host_ip).ipv4_loopback? || + Vagrant::Util::IPv4Interfaces.ipv4_interfaces.detect { |iface| iface[1] == test_host_ip } Vagrant::Util::IsPortOpen.is_port_open?(test_host_ip, host_port) else raise Errors::ForwardPortHostIPNotFound, name: machine.name, host_ip: host_ip diff --git a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb index 8f16954af..151c27202 100644 --- a/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb +++ b/test/unit/vagrant/action/builtin/handle_forwarded_port_collisions_test.rb @@ -195,12 +195,22 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do end context "when host ip does not exist" do - let(:host_ip) { "127.0.0.2" } + let(:host_ip) { "192.168.99.100" } let(:name) { "default" } it "should raise an error including the machine name" do allow(machine).to receive(:name).and_return(name) - expect{ instance.send(:port_check, host_ip, host_port) }.to raise_error(Vagrant::Errors::ForwardPortHostIPNotFound, /#{name}/) + expect{ instance.send(:port_check, host_ip, host_port) }. + to raise_error(Vagrant::Errors::ForwardPortHostIPNotFound, /#{name}/) + end + end + + context "with loopback address" do + let (:host_ip) { "127.1.2.40" } + + it "should check if the port is open" do + expect(Vagrant::Util::IsPortOpen).to receive(:is_port_open?).with(host_ip, host_port).and_return(true) + instance.send(:port_check, host_ip, host_port) end end end