Raise an error if host IP is not found

This commit is contained in:
Jeff Bonhag 2020-03-19 17:16:21 -04:00
parent 705baaad46
commit 6d228becf9
No known key found for this signature in database
GPG Key ID: 32966F3FB5AC1129
4 changed files with 24 additions and 7 deletions

View File

@ -271,7 +271,11 @@ module Vagrant
end
else
# Do a regular check
is_port_open?(host_ip, host_port)
if test_host_ip == "0.0.0.0" || ipv4_addresses.include?(test_host_ip)
is_port_open?(test_host_ip, host_port)
else
raise Errors::ForwardPortHostIPNotFound
end
end
end

View File

@ -400,6 +400,10 @@ module Vagrant
error_key(:auto_empty, "vagrant.actions.vm.forward_ports")
end
class ForwardPortHostIPNotFound < VagrantError
error_key(:host_ip_not_found, "vagrant.actions.vm.forward_ports")
end
class ForwardPortCollision < VagrantError
error_key(:collision_error, "vagrant.actions.vm.forward_ports")
end

View File

@ -2321,6 +2321,10 @@ en:
forwarding: Forwarding ports...
forwarding_entry: |-
%{guest_port} (guest) => %{host_port} (host) (adapter %{adapter})
host_ip_not_found:
You are trying to forward a host IP that does not exist. Please set `host_ip`
to the address of an existing IPv4 network interface, or remove the option
from your port forward configuration.
non_nat: |-
VirtualBox adapter #%{adapter} not configured as "NAT". Skipping port
forwards on this adapter.

View File

@ -177,9 +177,11 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do
describe "#port_check" do
let(:host_ip){ "127.0.0.1" }
let(:host_port){ 8080 }
let(:test_ips) { [ "127.0.0.1", "192.168.1.7" ] }
before do
instance.instance_variable_set(:@machine, machine)
allow(instance).to receive(:ipv4_addresses).and_return(test_ips)
end
it "should check if the port is open" do
@ -187,13 +189,8 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do
instance.send(:port_check, host_ip, host_port)
end
context "when host_ip is 0.0.0.0" do
context "when host ip is 0.0.0.0" do
let(:host_ip) { "0.0.0.0" }
let(:test_ips) { [ "127.0.0.1", "192.168.1.7" ] }
before do
allow(instance).to receive(:ipv4_addresses).and_return(test_ips)
end
context "on windows" do
let(:guest) { :windows }
@ -221,5 +218,13 @@ describe Vagrant::Action::Builtin::HandleForwardedPortCollisions do
end
end
end
context "when host ip does not exist" do
let(:host_ip) { "127.0.0.2" }
it "should raise an error" do
expect{ instance.send(:port_check, host_ip, host_port) }.to raise_error(Vagrant::Errors::ForwardPortHostIPNotFound)
end
end
end
end