From ac92fd8e1d2268c7b3f587d8f4a6622249b0f081 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 3 Nov 2021 09:32:41 -0700 Subject: [PATCH] Access provider driver through machine for version check Updates the VirtualBox version check for network range validation to access the driver via the machine instances provider within the passed env. --- .../providers/virtualbox/action/network.rb | 6 ++-- .../virtualbox/action/network_test.rb | 28 ++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/plugins/providers/virtualbox/action/network.rb b/plugins/providers/virtualbox/action/network.rb index f96b3a667..ece5ce03a 100644 --- a/plugins/providers/virtualbox/action/network.rb +++ b/plugins/providers/virtualbox/action/network.rb @@ -296,7 +296,7 @@ module VagrantPlugins error: e.message end - validate_hostonly_ip!(options[:ip]) + validate_hostonly_ip!(options[:ip], @env[:machine].provider.driver) if ip.ipv4? # Verify that a host-only network subnet would not collide @@ -515,8 +515,8 @@ module VagrantPlugins # ranges. It only validates if the network configuration file exists. # This was introduced in 6.1.28 so previous version won't have restrictions # placed on the valid ranges - def validate_hostonly_ip!(ip) - return if Gem::Version.new(Driver::Meta.version) < HOSTONLY_VALIDATE_VERSION || + def validate_hostonly_ip!(ip, driver) + return if Gem::Version.new(driver.version) < HOSTONLY_VALIDATE_VERSION || Vagrant::Util::Platform.windows? ip = IPAddr.new(ip.to_s) if !ip.is_a?(IPAddr) diff --git a/test/unit/plugins/providers/virtualbox/action/network_test.rb b/test/unit/plugins/providers/virtualbox/action/network_test.rb index 56df587c0..2e01fa527 100644 --- a/test/unit/plugins/providers/virtualbox/action/network_test.rb +++ b/test/unit/plugins/providers/virtualbox/action/network_test.rb @@ -21,7 +21,8 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do let(:env) {{ machine: machine, ui: machine.ui }} let(:app) { lambda { |*args| }} - let(:driver) { double("driver") } + let(:driver) { double("driver", version: vbox_version) } + let(:vbox_version) { "6.1.0" } let(:nics) { {} } @@ -30,8 +31,6 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do before do allow(driver).to receive(:enable_adapters) allow(driver).to receive(:read_network_interfaces) { nics } - allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:version). - and_return("6.1.0") end describe "#hostonly_config" do @@ -65,23 +64,21 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do describe "#validate_hostonly_ip!" do let(:address) { "192.168.1.2" } let(:net_conf) { [IPAddr.new(address + "/24")]} + let(:vbox_version) { "6.1.28" } before do - allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:version). - and_return("6.1.28") - allow(subject).to receive(:load_net_conf).and_return(net_conf) expect(subject).to receive(:validate_hostonly_ip!).and_call_original end it "should load net configuration" do expect(subject).to receive(:load_net_conf).and_return(net_conf) - subject.validate_hostonly_ip!(address) + subject.validate_hostonly_ip!(address, driver) end context "when address is within ranges" do it "should not error" do - subject.validate_hostonly_ip!(address) + subject.validate_hostonly_ip!(address, driver) end end @@ -90,24 +87,21 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do it "should raise an error" do expect { - subject.validate_hostonly_ip!(address) + subject.validate_hostonly_ip!(address, driver) }.to raise_error(Vagrant::Errors::VirtualBoxInvalidHostSubnet) end end context "when virtualbox version does not restrict range" do - before do - allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:version). - and_return("6.1.20") - end + let(:vbox_version) { "6.1.20" } it "should not error" do - subject.validate_hostonly_ip!(address) + subject.validate_hostonly_ip!(address, driver) end it "should not attempt to load network configuration" do expect(subject).not_to receive(:load_net_conf) - subject.validate_hostonly_ip!(address) + subject.validate_hostonly_ip!(address, driver) end end @@ -117,12 +111,12 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do end it "should not error" do - subject.validate_hostonly_ip!(address) + subject.validate_hostonly_ip!(address, driver) end it "should not attempt to load network configuration" do expect(subject).not_to receive(:load_net_conf) - subject.validate_hostonly_ip!(address) + subject.validate_hostonly_ip!(address, driver) end end end