diff options
-rw-r--r-- | lib/ohai/plugins/linux/network.rb | 7 | ||||
-rw-r--r-- | spec/unit/plugins/linux/network_spec.rb | 16 |
2 files changed, 18 insertions, 5 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index d550a9da..42251bd7 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -33,6 +33,10 @@ Ohai.plugin(:Network) do encap end + def iproute2_binary_available? + ["/sbin/ip", "/usr/bin/ip"].any? { |path| File.exist?(path) } + end + collect_data(:linux) do require 'ipaddr' @@ -49,8 +53,7 @@ Ohai.plugin(:Network) do # The '@eth0:' portion doesn't exist on primary interfaces and thus is optional in the regex IPROUTE_INT_REGEX = /^(\d+): ([0-9a-zA-Z@:\.\-_]*?)(@[0-9a-zA-Z]+|):\s/ unless defined? IPROUTE_INT_REGEX - if File.exist?("/sbin/ip") - + if iproute2_binary_available? # families to get default routes from families = [{ :name => "inet", diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index dbb6643e..1369b600 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -313,11 +313,21 @@ fe80::21c:eff:fe12:3456 dev eth0.153 lladdr 00:1c:0e:30:28:00 router REACHABLE allow(plugin).to receive(:shell_out).with("arp -an").and_return(mock_shell_out(0, linux_arp_an, "")) end + describe "#iproute2_binary_available?" do + ["/sbin/ip", "/usr/bin/ip"].each do |path| + it "accepts #{path}" do + allow(File).to receive(:exist?).and_return(false) + allow(File).to receive(:exist?).with(path).and_return(true) + expect(plugin.iproute2_binary_available?).to be_truthy + end + end + end + ["ifconfig","iproute2"].each do |network_method| describe "gathering IP layer address info via #{network_method}" do before(:each) do - allow(File).to receive(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) + allow(plugin).to receive(:iproute2_binary_available?).and_return( network_method == "iproute2" ) plugin.run end @@ -423,7 +433,7 @@ fe80::21c:eff:fe12:3456 dev eth0.153 lladdr 00:1c:0e:30:28:00 router REACHABLE describe "gathering interface counters via #{network_method}" do before(:each) do - allow(File).to receive(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) + allow(plugin).to receive(:iproute2_binary_available?).and_return( network_method == "iproute2" ) plugin.run end @@ -461,7 +471,7 @@ fe80::21c:eff:fe12:3456 dev eth0.153 lladdr 00:1c:0e:30:28:00 router REACHABLE describe "setting the node's default IP address attribute with #{network_method}" do before(:each) do - allow(File).to receive(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" ) + allow(plugin).to receive(:iproute2_binary_available?).and_return( network_method == "iproute2" ) plugin.run end |