Merge pull request #9275 from zachflower/validate_ip_addresses

Add a clean error message for invalid IP addresses
This commit is contained in:
Brian Cain 2017-12-15 11:22:03 -08:00 committed by GitHub
commit 0d7ad9f18f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

View File

@ -448,6 +448,10 @@ module Vagrant
error_key(:collides, "vagrant.actions.vm.host_only_network") error_key(:collides, "vagrant.actions.vm.host_only_network")
end end
class NetworkAddressInvalid < VagrantError
error_key(:network_address_invalid)
end
class NetworkDHCPAlreadyAttached < VagrantError class NetworkDHCPAlreadyAttached < VagrantError
error_key(:dhcp_already_attached, "vagrant.actions.vm.network") error_key(:dhcp_already_attached, "vagrant.actions.vm.network")
end end

View File

@ -1,4 +1,5 @@
require "ipaddr" require "ipaddr"
require "resolv"
require "set" require "set"
require "log4r" require "log4r"
@ -258,7 +259,12 @@ module VagrantPlugins
# Default IP is in the 20-bit private network block for DHCP based networks # Default IP is in the 20-bit private network block for DHCP based networks
options[:ip] = "172.28.128.1" if options[:type] == :dhcp && !options[:ip] options[:ip] = "172.28.128.1" if options[:type] == :dhcp && !options[:ip]
begin
ip = IPAddr.new(options[:ip]) ip = IPAddr.new(options[:ip])
rescue IPAddr::InvalidAddressError => e
raise Vagrant::Errors::NetworkAddressInvalid, :ip => options[:ip], :error_msg => e.message
end
if ip.ipv4? if ip.ipv4?
options[:netmask] ||= "255.255.255.0" options[:netmask] ||= "255.255.255.0"

View File

@ -891,6 +891,11 @@ en:
%{message} %{message}
network_type_not_supported: |- network_type_not_supported: |-
The %{type} network type is not supported for this box or guest. The %{type} network type is not supported for this box or guest.
network_address_invalid: |-
The IP address '%{ip}' is not valid. Please review the error message
below to help resolve the issue:
%{error_msg}
network_manager_not_installed: |- network_manager_not_installed: |-
Vagrant was instructed to configure the %{device} network device to Vagrant was instructed to configure the %{device} network device to
be managed by NetworkManager. However, the configured guest VM does be managed by NetworkManager. However, the configured guest VM does

View File

@ -70,6 +70,20 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do
}]) }])
end end
it "raises the appropriate error when provided with an invalid IP address" do
guest = double("guest")
machine.config.vm.network 'private_network', { ip: '192.168.33.06' }
expect{ subject.call(env) }.to raise_error(Vagrant::Errors::NetworkAddressInvalid)
end
it "raises no invalid network error when provided with a valid IP address" do
guest = double("guest")
machine.config.vm.network 'private_network', { ip: '192.168.33.6' }
expect{ subject.call(env) }.not_to raise_error(Vagrant::Errors::NetworkAddressInvalid)
end
context "with a dhcp private network" do context "with a dhcp private network" do
let(:bridgedifs) { [] } let(:bridgedifs) { [] }
let(:hostonlyifs) { [] } let(:hostonlyifs) { [] }