From f593bb54d2fad54f2d79607679bc08b8859dc91f Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Mon, 20 Apr 2020 13:28:37 -0400 Subject: [PATCH] Fix #8704: Raise an error if the guest IP ends with .1 (#11500) If the VirtualBox guest property /VirtualBox/GuestInfo/Net/1/V4/IP returns an IP address ending in .1, raise an error. This addresses an issue that was revealed as an NFS error, where Vagrant was creating an exports file with the wrong IP address. This was thought to be caused by the presence of a docker0 interface, but it manifested itself even without Docker installed. This issue is difficult to reproduce, but hopefully this PR will get us closer to the root cause. --- .../virtualbox/driver/version_5_0.rb | 9 +++-- .../virtualbox_driver_version_5_x_examples.rb | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/plugins/providers/virtualbox/driver/version_5_0.rb b/plugins/providers/virtualbox/driver/version_5_0.rb index 7220bb5d9..b08576a8f 100644 --- a/plugins/providers/virtualbox/driver/version_5_0.rb +++ b/plugins/providers/virtualbox/driver/version_5_0.rb @@ -481,7 +481,7 @@ module VagrantPlugins end end - return get_machine_id specified_name + return get_machine_id(specified_name) end def max_network_adapters @@ -587,6 +587,11 @@ module VagrantPlugins def read_guest_ip(adapter_number) ip = read_guest_property("/VirtualBox/GuestInfo/Net/#{adapter_number}/V4/IP") + if ip.end_with?(".1") + @logger.warn("VBoxManage guest property returned: #{ip}. Result resembles IP of DHCP server and is being ignored.") + ip = nil + end + if !valid_ip_address?(ip) raise Vagrant::Errors::VirtualBoxGuestPropertyNotFound, guest_property: "/VirtualBox/GuestInfo/Net/#{adapter_number}/V4/IP" @@ -923,7 +928,7 @@ module VagrantPlugins def valid_ip_address?(ip) # Filter out invalid IP addresses # GH-4658 VirtualBox can report an IP address of 0.0.0.0 for FreeBSD guests. - if ip == "0.0.0.0" + if ip == "0.0.0.0" || ip.nil? return false else return true diff --git a/test/unit/plugins/providers/virtualbox/support/shared/virtualbox_driver_version_5_x_examples.rb b/test/unit/plugins/providers/virtualbox/support/shared/virtualbox_driver_version_5_x_examples.rb index 2c8b142fd..ac5cdc56a 100644 --- a/test/unit/plugins/providers/virtualbox/support/shared/virtualbox_driver_version_5_x_examples.rb +++ b/test/unit/plugins/providers/virtualbox/support/shared/virtualbox_driver_version_5_x_examples.rb @@ -125,4 +125,40 @@ shared_examples "a version 5.x virtualbox driver" do |options| end end end + + describe "#read_guest_ip" do + context "when guest ip ends in .1" do + before do + key = "/VirtualBox/GuestInfo/Net/1/V4/IP" + + expect(subprocess).to receive(:execute). + with("VBoxManage", "guestproperty", "get", uuid, key, an_instance_of(Hash)). + and_return(subprocess_result(stdout: "Value: 172.28.128.1")) + end + + it "should raise an error" do + expect { subject.read_guest_ip(1) }.to raise_error(Vagrant::Errors::VirtualBoxGuestPropertyNotFound) + end + end + end + + describe "#valid_ip_address?" do + context "when ip is 0.0.0.0" do + let(:ip) { "0.0.0.0" } + + it "should be false" do + result = subject.send(:valid_ip_address?, ip) + expect(result).to be(false) + end + end + + context "when ip address is nil" do + let(:ip) { nil } + + it "should be false" do + result = subject.send(:valid_ip_address?, ip) + expect(result).to be(false) + end + end + end end