Fixes #11236: Windows port detection (#11244)

This reverts commit 81553263ab.

This fixes a regression with Windows port detection which led to port
collisions not being fixed on `vagrant up`.

PR #8517 changed `IsPortOpen#is_port_open?` to rescue
Errno::EADDRNOTAVAIL, but when we merged it into master, there was code
in `HandleForwardedPortCollisions#port_check` that depended on that
error bubbling up.
This commit is contained in:
Jeff Bonhag 2019-12-19 14:46:13 -05:00 committed by GitHub
parent 85065c3746
commit ade076cabb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 7 deletions

View File

@ -30,8 +30,7 @@ module Vagrant
return true
end
rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, \
Errno::ENETUNREACH, Errno::EACCES, Errno::ENOTCONN, \
Errno::EADDRNOTAVAIL
Errno::ENETUNREACH, Errno::EACCES, Errno::ENOTCONN
# Any of the above exceptions signal that the port is closed.
return false
end

View File

@ -75,7 +75,8 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do
end
it "should check if host port is in use" do
expect(instance).to receive(:is_forwarded_already).and_return false
expect(instance).to receive(:is_forwarded_already).and_return(false)
expect(instance).to receive(:is_port_open?).and_return(false)
instance.call(env)
end
@ -148,7 +149,7 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do
let(:host_port){ 8080 }
it "should check if the port is open" do
expect(instance).to receive(:is_port_open?).with(host_ip, host_port).and_return true
expect(instance).to receive(:is_port_open?).with(host_ip, host_port).and_return(true)
instance.send(:port_check, host_ip, host_port)
end
@ -156,13 +157,13 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do
let(:host_ip){ nil }
it "should set host_ip to 0.0.0.0 when unset" do
expect(instance).to receive(:is_port_open?).with("0.0.0.0", host_port).and_return true
expect(instance).to receive(:is_port_open?).with("0.0.0.0", host_port).and_return(true)
instance.send(:port_check, host_ip, host_port)
end
it "should set host_ip to 127.0.0.1 when 0.0.0.0 is not available" do
expect(instance).to receive(:is_port_open?).with("0.0.0.0", host_port).and_raise Errno::EADDRNOTAVAIL
expect(instance).to receive(:is_port_open?).with("127.0.0.1", host_port).and_return true
expect(instance).to receive(:is_port_open?).with("0.0.0.0", host_port).and_raise(Errno::EADDRNOTAVAIL)
expect(instance).to receive(:is_port_open?).with("127.0.0.1", host_port).and_return(true)
instance.send(:port_check, host_ip, host_port)
end
end

View File

@ -49,5 +49,15 @@ describe Vagrant::Util::IsPortOpen do
# best, really.
expect(klass.is_port_open?("127.0.0.1", closed_port)).not_to be
end
it "should handle connection refused" do
expect(TCPSocket).to receive(:new).with("0.0.0.0", closed_port).and_raise(Errno::ECONNREFUSED)
expect(klass.is_port_open?("0.0.0.0", closed_port)).to be(false)
end
it "should raise an error if cannot assign requested address" do
expect(TCPSocket).to receive(:new).with("0.0.0.0", open_port).and_raise(Errno::EADDRNOTAVAIL)
expect { klass.is_port_open?("0.0.0.0", open_port) }.to raise_error(Errno::EADDRNOTAVAIL)
end
end