summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-06-10 14:33:54 -0700
committerTim Smith <tsmith@chef.io>2017-06-10 15:00:57 -0700
commit4af278603214300eb44e7929cff406bc7aace051 (patch)
tree095b2d148e92e672e5de3bbf8798889ad223976c
parent72079a1ff60b9707850f7b178a2b1f9f026f4bb0 (diff)
downloadohai-network_which.tar.gz
Avoid extra file stats and simplify things a bitnetwork_which
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/ohai/plugins/linux/network.rb18
-rw-r--r--spec/unit/plugins/linux/network_spec.rb14
2 files changed, 15 insertions, 17 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..c727ec49 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
@@ -692,7 +693,8 @@ EOM
describe "setting the node's default IP address attribute with iproute2" do
before(:each) do
- allow(plugin).to receive(:iproute2_binary_path).and_return( "/sbin/ip" )
+ allow(plugin).to receive(:which).with("ip").and_return("/sbin/ip")
+ 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