diff options
author | Tim Smith <tsmith@chef.io> | 2017-06-10 14:33:54 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2017-06-10 19:14:59 -0700 |
commit | c509235734d49c44f9e23ea08f941be7972c848f (patch) | |
tree | 6fa596606b5ec197cf38c1fac71e3a0f38a5d4a7 | |
parent | 72079a1ff60b9707850f7b178a2b1f9f026f4bb0 (diff) | |
download | ohai-c509235734d49c44f9e23ea08f941be7972c848f.tar.gz |
Avoid extra file stats and simplify things a bit
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/ohai/plugins/linux/network.rb | 18 | ||||
-rw-r--r-- | spec/unit/plugins/linux/network_spec.rb | 16 |
2 files changed, 16 insertions, 18 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 87364499..32a89133 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -37,16 +37,12 @@ Ohai.plugin(:Network) do File.exist? "/proc/net/if_inet6" end - def iproute2_binary_path - which("ip") - end - def ethtool_binary_path - which("ethtool") + @ethtool ||= which("ethtool") end def is_openvz? - ::File.directory?("/proc/vz") + @openvz ||= ::File.directory?("/proc/vz") end def is_openvz_host? @@ -151,11 +147,11 @@ Ohai.plugin(:Network) do # determine layer 1 details for the interface using ethtool def ethernet_layer_one(iface) - return iface unless ethtool_binary = ethtool_binary_path + return iface unless ethtool_binary_path keys = %w{ Speed Duplex Port Transceiver Auto-negotiation MDI-X } iface.each_key do |tmp_int| next unless iface[tmp_int][:encapsulation] == "Ethernet" - so = shell_out("#{ethtool_binary} #{tmp_int}") + so = shell_out("#{ethtool_binary_path} #{tmp_int}") so.stdout.lines do |line| line.chomp! Ohai::Log.debug("Plugin Network: Parsing ethtool output: #{line}") @@ -175,10 +171,10 @@ Ohai.plugin(:Network) do # determine ring parameters for the interface using ethtool def ethernet_ring_parameters(iface) - return iface unless ethtool_binary = ethtool_binary_path + return iface unless ethtool_binary_path iface.each_key do |tmp_int| next unless iface[tmp_int][:encapsulation] == "Ethernet" - so = shell_out("#{ethtool_binary} -g #{tmp_int}") + so = shell_out("#{ethtool_binary_path} -g #{tmp_int}") Ohai::Log.debug("Plugin Network: Parsing ethtool output: #{so.stdout}") type = nil iface[tmp_int]["ring_params"] = {} @@ -471,7 +467,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 iproute2_binary_path + if which("ip") # 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 3691d974..b0c5ad06 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -535,8 +535,8 @@ EOM %w{ifconfig iproute2}.each do |network_method| describe "gathering IP layer address info via #{network_method}" do before(:each) do - allow(plugin).to receive(:iproute2_binary_path).and_return( network_method == "ifconfig" ? false : "/sbin/ip" ) - allow(plugin).to receive(:ethtool_binary_path).and_return( "/sbin/ethtool" ) + allow(plugin).to receive(:which).with("ip").and_return( network_method == "iproute2" ? "/sbin/ip" : false ) + allow(plugin).to receive(:which).with("ethtool").and_return( "/sbin/ethtool" ) plugin.run end @@ -654,7 +654,8 @@ EOM describe "gathering interface counters via #{network_method}" do before(:each) do - allow(plugin).to receive(:iproute2_binary_path).and_return( "/sbin/ip" ) + allow(plugin).to receive(:which).with("ip").and_return(network_method == "iproute2" ? "/sbin/ip" : false) + allow(plugin).to receive(:which).with("ethtool").and_return("/sbin/ethtool") plugin.run end @@ -690,9 +691,10 @@ EOM end end - describe "setting the node's default IP address attribute with iproute2" do + describe "setting the node's default IP address attribute with #{network_method}" do before(:each) do - allow(plugin).to receive(:iproute2_binary_path).and_return( "/sbin/ip" ) + allow(plugin).to receive(:which).with("ip").and_return(network_method == "iproute2" ? "/sbin/ip" : false) + allow(plugin).to receive(:which).with("ethtool").and_return("/sbin/ethtool") plugin.run end @@ -770,8 +772,8 @@ EOM describe "for newer network features using iproute2 only" do before(:each) do - allow(plugin).to receive(:iproute2_binary_path).and_return( "/sbin/ip" ) - allow(plugin).to receive(:ethtool_binary_path).and_return( "/sbin/ethtool" ) + allow(plugin).to receive(:which).with("ip").and_return("/sbin/ip") + allow(plugin).to receive(:which).with("ethtool").and_return( "/sbin/ethtool" ) allow(File).to receive(:exist?).with("/proc/net/if_inet6").and_return(true) # ipv6 is enabled plugin.run end |