From 51232f9a032913466b0cc713ca4d7506adbe459c Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 9 Mar 2018 14:15:31 -0800 Subject: [PATCH] When matching hostonly adapter name, force common types Force a string type for comparison to prevent false positives when the name is unset (resulting in a nil value). --- .../providers/virtualbox/action/network.rb | 4 +- .../virtualbox/action/network_test.rb | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/plugins/providers/virtualbox/action/network.rb b/plugins/providers/virtualbox/action/network.rb index 90634cf3f..4081c11a3 100644 --- a/plugins/providers/virtualbox/action/network.rb +++ b/plugins/providers/virtualbox/action/network.rb @@ -480,8 +480,8 @@ module VagrantPlugins return interface if config[:name] && config[:name] == interface[:name] #if a config name is specified, we should only look for that. - if config[:name] != "" - next + if config[:name].to_s != "" + next end if interface[:ip] != "" diff --git a/test/unit/plugins/providers/virtualbox/action/network_test.rb b/test/unit/plugins/providers/virtualbox/action/network_test.rb index 5fc09575e..5c1eae212 100644 --- a/test/unit/plugins/providers/virtualbox/action/network_test.rb +++ b/test/unit/plugins/providers/virtualbox/action/network_test.rb @@ -181,4 +181,45 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do end end end + + describe "#hostonly_find_matching_network" do + let(:ip){ "192.168.55.2" } + let(:config){ {ip: ip, netmask: "255.255.255.0"} } + let(:interfaces){ [] } + + before do + allow(driver).to receive(:read_host_only_interfaces).and_return(interfaces) + subject.instance_variable_set(:@env, env) + end + + context "with no defined host interfaces" do + it "should return nil" do + expect(subject.hostonly_find_matching_network(config)).to be_nil + end + end + + context "with matching host interface" do + let(:interfaces){ [{ip: "192.168.55.1", netmask: "255.255.255.0", name: "vnet"}] } + + it "should return matching interface" do + expect(subject.hostonly_find_matching_network(config)).to eq(interfaces.first) + end + + context "with matching name" do + let(:config){ {ip: ip, netmask: "255.255.255.0", name: "vnet"} } + + it "should return matching interface" do + expect(subject.hostonly_find_matching_network(config)).to eq(interfaces.first) + end + end + + context "with non-matching name" do + let(:config){ {ip: ip, netmask: "255.255.255.0", name: "unknown"} } + + it "should return nil" do + expect(subject.hostonly_find_matching_network(config)).to be_nil + end + end + end + end end