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.
This commit is contained in:
Jeff Bonhag 2020-03-18 17:39:33 -04:00
parent b8ecab1201
commit 705baaad46
No known key found for this signature in database
GPG Key ID: 32966F3FB5AC1129
2 changed files with 32 additions and 20 deletions

View File

@ -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}")

View File

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