From 705baaad46edd1be1f1507aa0fa429decf1f6655 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Wed, 18 Mar 2020 17:39:33 -0400 Subject: [PATCH] Only do special 0.0.0.0 check on Windows hosts If host_ip is nil or 0.0.0.0, only do the special port check on Windows hosts, because non-Windows hosts can test 0.0.0.0 directly. --- .../handle_forwarded_port_collisions.rb | 3 +- .../handle_forwarded_port_collisions_test.rb | 49 ++++++++++++------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index db8e8dd45..186636c05 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -255,7 +255,8 @@ module Vagrant def port_check(host_ip, host_port) # If no host_ip is specified, intention taken to be listen on all interfaces. - if host_ip.nil? || host_ip == "0.0.0.0" + test_host_ip = host_ip || "0.0.0.0" + if @machine.config.vm.guest == :windows && test_host_ip == "0.0.0.0" @logger.debug("Checking port #{host_port} on all IPv4 addresses...") available_ips = ipv4_addresses.select do |test_host_ip| @logger.debug("Host IP: #{test_host_ip}, port: #{host_port}") 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 c4ea11837..8a5eca4c4 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 @@ -39,10 +39,13 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do end end + let(:guest) { } + let(:vm_config) do double("machine_vm_config").tap do |config| allow(config).to receive(:usable_port_range).and_return(1000..2000) allow(config).to receive(:networks).and_return([]) + allow(config).to receive(:guest).and_return(guest) end end @@ -161,7 +164,7 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do expect(instance.send(:ipv4_addresses)).to eq([ "127.0.0.1" ]) end - context "on windows" do + context "with nil interface address" do let(:nil_ifaddr) { double("nil_ifaddr", addr: nil ) } let(:ifaddrs) { [ ipv4_ifaddr, ipv6_ifaddr, nil_ifaddr ] } @@ -175,6 +178,10 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do let(:host_ip){ "127.0.0.1" } let(:host_port){ 8080 } + before do + instance.instance_variable_set(:@machine, machine) + end + it "should check if the port is open" do expect(instance).to receive(:is_port_open?).with(host_ip, host_port).and_return(true) instance.send(:port_check, host_ip, host_port) @@ -188,26 +195,30 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do allow(instance).to receive(:ipv4_addresses).and_return(test_ips) end - it "should check the port on every IPv4 interface" do - expect(instance).to receive(:is_port_open?).with(test_ips.first, host_port) - expect(instance).to receive(:is_port_open?).with(test_ips.last, host_port) - instance.send(:port_check, host_ip, host_port) - end + context "on windows" do + let(:guest) { :windows } - it "should return false if the port is closed on any IPv4 interfaces" do - expect(instance).to receive(:is_port_open?).with(test_ips.first, host_port). - and_return(true) - expect(instance).to receive(:is_port_open?).with(test_ips.last, host_port). - and_return(false) - expect(instance.send(:port_check, host_ip, host_port)).to be(false) - end + it "should check the port on every IPv4 interface" do + expect(instance).to receive(:is_port_open?).with(test_ips.first, host_port) + expect(instance).to receive(:is_port_open?).with(test_ips.last, host_port) + instance.send(:port_check, host_ip, host_port) + end - it "should return true if the port is open on all IPv4 interfaces" do - expect(instance).to receive(:is_port_open?).with(test_ips.first, host_port). - and_return(true) - expect(instance).to receive(:is_port_open?).with(test_ips.last, host_port). - and_return(true) - expect(instance.send(:port_check, host_ip, host_port)).to be(true) + it "should return false if the port is closed on any IPv4 interfaces" do + expect(instance).to receive(:is_port_open?).with(test_ips.first, host_port). + and_return(true) + expect(instance).to receive(:is_port_open?).with(test_ips.last, host_port). + and_return(false) + expect(instance.send(:port_check, host_ip, host_port)).to be(false) + end + + it "should return true if the port is open on all IPv4 interfaces" do + expect(instance).to receive(:is_port_open?).with(test_ips.first, host_port). + and_return(true) + expect(instance).to receive(:is_port_open?).with(test_ips.last, host_port). + and_return(true) + expect(instance.send(:port_check, host_ip, host_port)).to be(true) + end end end end