summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheodore Nordsieck <theo@opscode.com>2013-09-10 14:57:00 -0700
committerTheodore Nordsieck <theo@opscode.com>2013-09-11 16:13:25 -0700
commit67d0e9e1aa60f79a90dd3e5bbd5d49cf002e67c2 (patch)
treefe5c260aa51d0ef8eed991878824124b62deb555
parent093c22a1dc3bc4bfeb860ada3b51bed71a5dd520 (diff)
downloadohai-67d0e9e1aa60f79a90dd3e5bbd5d49cf002e67c2.tar.gz
Converted plugins/linux/network to Mixlib::ShellOut.
-rw-r--r--lib/ohai/plugins/linux/network.rb431
-rw-r--r--spec/unit/plugins/linux/network_spec.rb63
2 files changed, 219 insertions, 275 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb
index a593df04..ad701b5d 100644
--- a/lib/ohai/plugins/linux/network.rb
+++ b/lib/ohai/plugins/linux/network.rb
@@ -50,138 +50,129 @@ Ohai.plugin do
if File.exist?("/sbin/ip")
# families to get default routes from
- families = [
- {
+ families = [{
:name => "inet",
:default_route => "0.0.0.0/0",
:default_prefix => :default,
:neighbour_attribute => :arp
- },
- {
+ },{
:name => "inet6",
:default_route => "::/0",
:default_prefix => :default_inet6,
:neighbour_attribute => :neighbour_inet6
- }
- ]
-
- popen4("ip addr") do |pid, stdin, stdout, stderr|
- stdin.close
- cint = nil
- stdout.each do |line|
- if line =~ IPROUTE_INT_REGEX
- cint = $2
- iface[cint] = Mash.new
- if cint =~ /^(\w+)(\d+.*)/
- iface[cint][:type] = $1
- iface[cint][:number] = $2
- end
-
- if line =~ /mtu (\d+)/
- iface[cint][:mtu] = $1
- end
-
- flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|LOWER_UP|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)/)
- if flags.length > 1
- iface[cint][:flags] = flags.flatten.uniq
- end
+ }]
+
+ so = shell_out("ip addr")
+ cint = nil
+ so.stdout.lines do |line|
+ if line =~ IPROUTE_INT_REGEX
+ cint = $2
+ iface[cint] = Mash.new
+ if cint =~ /^(\w+)(\d+.*)/
+ iface[cint][:type] = $1
+ iface[cint][:number] = $2
end
- if line =~ /link\/(\w+) ([\da-f\:]+) /
- iface[cint][:encapsulation] = encaps_lookup($1)
- unless $2 == "00:00:00:00:00:00"
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
- iface[cint][:addresses][$2.upcase] = { "family" => "lladdr" }
- end
- end
- if line =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/
- tmp_addr, tmp_prefix = $1, $3
- tmp_prefix ||= "32"
- original_int = nil
-
- # Are we a formerly aliased interface?
- if line =~ /#{cint}:(\d+)$/
- sub_int = $1
- alias_int = "#{cint}:#{sub_int}"
- original_int = cint
- cint = alias_int
- end
-
- iface[cint] = Mash.new unless iface[cint] # Create the fake alias interface if needed
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
- iface[cint][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix }
- iface[cint][:addresses][tmp_addr][:netmask] = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s
-
- if line =~ /peer (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
- iface[cint][:addresses][tmp_addr][:peer] = $1
- end
-
- if line =~ /brd (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
- iface[cint][:addresses][tmp_addr][:broadcast] = $1
- end
- if line =~ /scope (\w+)/
- iface[cint][:addresses][tmp_addr][:scope] = ($1.eql?("host") ? "Node" : $1.capitalize)
- end
+ if line =~ /mtu (\d+)/
+ iface[cint][:mtu] = $1
+ end
- # If we found we were an an alias interface, restore cint to its original value
- cint = original_int unless original_int.nil?
+ flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|LOWER_UP|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)/)
+ if flags.length > 1
+ iface[cint][:flags] = flags.flatten.uniq
end
- if line =~ /inet6 ([a-f0-9\:]+)\/(\d+) scope (\w+)/
+ end
+ if line =~ /link\/(\w+) ([\da-f\:]+) /
+ iface[cint][:encapsulation] = encaps_lookup($1)
+ unless $2 == "00:00:00:00:00:00"
iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
- tmp_addr = $1
- iface[cint][:addresses][tmp_addr] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("host") ? "Node" : $3.capitalize) }
+ iface[cint][:addresses][$2.upcase] = { "family" => "lladdr" }
end
end
- end
-
- popen4("ip -d -s link") do |pid, stdin, stdout, stderr|
- stdin.close
- tmp_int = nil
- on_rx = true
- stdout.each do |line|
- if line =~ IPROUTE_INT_REGEX
- tmp_int = $2
- net_counters[tmp_int] = Mash.new unless net_counters[tmp_int]
+ if line =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/
+ tmp_addr, tmp_prefix = $1, $3
+ tmp_prefix ||= "32"
+ original_int = nil
+
+ # Are we a formerly aliased interface?
+ if line =~ /#{cint}:(\d+)$/
+ sub_int = $1
+ alias_int = "#{cint}:#{sub_int}"
+ original_int = cint
+ cint = alias_int
end
- if line =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
- int = on_rx ? :rx : :tx
- net_counters[tmp_int][int] = Mash.new unless net_counters[tmp_int][int]
- net_counters[tmp_int][int][:bytes] = $1
- net_counters[tmp_int][int][:packets] = $2
- net_counters[tmp_int][int][:errors] = $3
- net_counters[tmp_int][int][:drop] = $4
- if(int == :rx)
- net_counters[tmp_int][int][:overrun] = $5
- else
- net_counters[tmp_int][int][:carrier] = $5
- net_counters[tmp_int][int][:collisions] = $6
- end
+ iface[cint] = Mash.new unless iface[cint] # Create the fake alias interface if needed
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
+ iface[cint][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix }
+ iface[cint][:addresses][tmp_addr][:netmask] = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s
- on_rx = !on_rx
+ if line =~ /peer (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
+ iface[cint][:addresses][tmp_addr][:peer] = $1
end
- if line =~ /qlen (\d+)/
- net_counters[tmp_int][:tx] = Mash.new unless net_counters[tmp_int][:tx]
- net_counters[tmp_int][:tx][:queuelen] = $1
+ if line =~ /brd (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
+ iface[cint][:addresses][tmp_addr][:broadcast] = $1
end
-
- if line =~ /vlan id (\d+)/
- tmp_id = $1
- iface[tmp_int][:vlan] = Mash.new unless iface[tmp_int][:vlan]
- iface[tmp_int][:vlan][:id] = tmp_id
-
- vlan_flags = line.scan(/(REORDER_HDR|GVRP|LOOSE_BINDING)/)
- if vlan_flags.length > 0
- iface[tmp_int][:vlan][:flags] = vlan_flags.flatten.uniq
- end
+
+ if line =~ /scope (\w+)/
+ iface[cint][:addresses][tmp_addr][:scope] = ($1.eql?("host") ? "Node" : $1.capitalize)
end
- if line =~ /state (\w+)/
- iface[tmp_int]['state'] = $1.downcase
+ # If we found we were an an alias interface, restore cint to its original value
+ cint = original_int unless original_int.nil?
+ end
+ if line =~ /inet6 ([a-f0-9\:]+)\/(\d+) scope (\w+)/
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
+ tmp_addr = $1
+ iface[cint][:addresses][tmp_addr] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("host") ? "Node" : $3.capitalize) }
+ end
+ end
+
+ so = shell_out("ip -d -s link")
+ tmp_int = nil
+ on_rx = true
+ so.stdout.lines do |line|
+ if line =~ IPROUTE_INT_REGEX
+ tmp_int = $2
+ net_counters[tmp_int] = Mash.new unless net_counters[tmp_int]
+ end
+
+ if line =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/
+ int = on_rx ? :rx : :tx
+ net_counters[tmp_int][int] = Mash.new unless net_counters[tmp_int][int]
+ net_counters[tmp_int][int][:bytes] = $1
+ net_counters[tmp_int][int][:packets] = $2
+ net_counters[tmp_int][int][:errors] = $3
+ net_counters[tmp_int][int][:drop] = $4
+ if(int == :rx)
+ net_counters[tmp_int][int][:overrun] = $5
+ else
+ net_counters[tmp_int][int][:carrier] = $5
+ net_counters[tmp_int][int][:collisions] = $6
end
+ on_rx = !on_rx
+ end
+
+ if line =~ /qlen (\d+)/
+ net_counters[tmp_int][:tx] = Mash.new unless net_counters[tmp_int][:tx]
+ net_counters[tmp_int][:tx][:queuelen] = $1
+ end
+
+ if line =~ /vlan id (\d+)/
+ tmp_id = $1
+ iface[tmp_int][:vlan] = Mash.new unless iface[tmp_int][:vlan]
+ iface[tmp_int][:vlan][:id] = tmp_id
+
+ vlan_flags = line.scan(/(REORDER_HDR|GVRP|LOOSE_BINDING)/)
+ if vlan_flags.length > 0
+ iface[tmp_int][:vlan][:flags] = vlan_flags.flatten.uniq
+ end
+ end
+ if line =~ /state (\w+)/
+ iface[tmp_int]['state'] = $1.downcase
end
end
@@ -189,17 +180,15 @@ Ohai.plugin do
neigh_attr = family[:neighbour_attribute]
default_prefix = family[:default_prefix]
- popen4("ip -f #{family[:name]} neigh show") do |pid, stdin, stdout, stderr|
- stdin.close
- stdout.each do |line|
- if line =~ /^([a-f0-9\:\.]+)\s+dev\s+([^\s]+)\s+lladdr\s+([a-fA-F0-9\:]+)/
- unless iface[$2]
- Ohai::Log.warn("neighbour list has entries for unknown interface #{iface[$2]}")
- next
- end
- iface[$2][neigh_attr] = Mash.new unless iface[$2][neigh_attr]
- iface[$2][neigh_attr][$1] = $3.downcase
+ so = shell_out("ip -f #{family[:name]} neigh show")
+ so.stdout.lines do |line|
+ if line =~ /^([a-f0-9\:\.]+)\s+dev\s+([^\s]+)\s+lladdr\s+([a-fA-F0-9\:]+)/
+ unless iface[$2]
+ Ohai::Log.warn("neighbour list has entries for unknown interface #{iface[$2]}")
+ next
end
+ iface[$2][neigh_attr] = Mash.new unless iface[$2][neigh_attr]
+ iface[$2][neigh_attr][$1] = $3.downcase
end
end
@@ -210,38 +199,36 @@ Ohai.plugin do
# the routing table source field.
# 3) and since we're at it, let's populate some :routes attributes
# (going to do that for both inet and inet6 addresses)
- popen4("ip -f #{family[:name]} route show") do |pid, stdin, stdout, stderr|
- stdin.close
- stdout.each do |line|
- if line =~ /^([^\s]+)\s(.*)$/
- route_dest = $1
- route_ending = $2
- #
- if route_ending =~ /\bdev\s+([^\s]+)\b/
- route_int = $1
- else
- Ohai::Log.debug("Skipping route entry without a device: '#{line}'")
- next
- end
-
- unless iface[route_int]
- Ohai::Log.debug("Skipping previously unseen interface from 'ip route show': #{route_int}")
- next
- end
-
- route_entry = Mash.new( :destination => route_dest,
- :family => family[:name] )
- %w[via scope metric proto src].each do |k|
- route_entry[k] = $1 if route_ending =~ /\b#{k}\s+([^\s]+)\b/
- end
-
- # a sanity check, especially for Linux-VServer, OpenVZ and LXC:
- # don't report the route entry if the src address isn't set on the node
- next if route_entry[:src] and not iface[route_int][:addresses].has_key? route_entry[:src]
-
- iface[route_int][:routes] = Array.new unless iface[route_int][:routes]
- iface[route_int][:routes] << route_entry
+ so = shell_out("ip -f #{family[:name]} route show")
+ so.stdout.lines do |line|
+ if line =~ /^([^\s]+)\s(.*)$/
+ route_dest = $1
+ route_ending = $2
+ #
+ if route_ending =~ /\bdev\s+([^\s]+)\b/
+ route_int = $1
+ else
+ Ohai::Log.debug("Skipping route entry without a device: '#{line}'")
+ next
end
+
+ unless iface[route_int]
+ Ohai::Log.debug("Skipping previously unseen interface from 'ip route show': #{route_int}")
+ next
+ end
+
+ route_entry = Mash.new( :destination => route_dest,
+ :family => family[:name] )
+ %w[via scope metric proto src].each do |k|
+ route_entry[k] = $1 if route_ending =~ /\b#{k}\s+([^\s]+)\b/
+ end
+
+ # a sanity check, especially for Linux-VServer, OpenVZ and LXC:
+ # don't report the route entry if the src address isn't set on the node
+ next if route_entry[:src] and not iface[route_int][:addresses].has_key? route_entry[:src]
+
+ iface[route_int][:routes] = Array.new unless iface[route_int][:routes]
+ iface[route_int][:routes] << route_entry
end
end
# now looking at the routes to set the default attributes
@@ -325,98 +312,90 @@ Ohai.plugin do
else
begin
- route_result = from("route -n \| grep -m 1 ^0.0.0.0").split(/[ \t]+/)
+ so = shell_out("route -n")
+ route_result = so.stdout.split($/).grep( /^0.0.0.0/ )[0].split( /[ \t]+/ )
network[:default_gateway], network[:default_interface] = route_result.values_at(1,7)
rescue Ohai::Exceptions::Exec
Ohai::Log.debug("Unable to determine default interface")
end
- popen4("ifconfig -a") do |pid, stdin, stdout, stderr|
- stdin.close
- cint = nil
- stdout.each do |line|
- tmp_addr = nil
- # dev_valid_name in the kernel only excludes slashes, nulls, spaces
- # http://git.kernel.org/?p=linux/kernel/git/stable/linux-stable.git;a=blob;f=net/core/dev.c#l851
- if line =~ /^([0-9a-zA-Z@\.\:\-_]+)\s+/
- cint = $1
- iface[cint] = Mash.new
- if cint =~ /^(\w+)(\d+.*)/
- iface[cint][:type] = $1
- iface[cint][:number] = $2
- end
- end
- if line =~ /Link encap:(Local Loopback)/ || line =~ /Link encap:(.+?)\s/
- iface[cint][:encapsulation] = encaps_lookup($1)
- end
- if line =~ /HWaddr (.+?)\s/
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
- iface[cint][:addresses][$1] = { "family" => "lladdr" }
- end
- if line =~ /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
- iface[cint][:addresses][$1] = { "family" => "inet" }
- tmp_addr = $1
- end
- if line =~ /inet6 addr: ([a-f0-9\:]+)\/(\d+) Scope:(\w+)/
- iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
- iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("Host") ? "Node" : $3) }
- end
- if line =~ /Bcast:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
- iface[cint][:addresses][tmp_addr]["broadcast"] = $1
- end
- if line =~ /Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
- iface[cint][:addresses][tmp_addr]["netmask"] = $1
- end
- flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|RUNNING|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)\s/)
- if flags.length > 1
- iface[cint][:flags] = flags.flatten
- end
- if line =~ /MTU:(\d+)/
- iface[cint][:mtu] = $1
- end
- if line =~ /P-t-P:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
- iface[cint][:peer] = $1
- end
- if line =~ /RX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) frame:(\d+)/
- net_counters[cint] = Mash.new unless net_counters[cint]
- net_counters[cint][:rx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "frame" => $5 }
- end
- if line =~ /TX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) carrier:(\d+)/
- net_counters[cint][:tx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "carrier" => $5 }
- end
- if line =~ /collisions:(\d+)/
- net_counters[cint][:tx]["collisions"] = $1
- end
- if line =~ /txqueuelen:(\d+)/
- net_counters[cint][:tx]["queuelen"] = $1
- end
- if line =~ /RX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
- net_counters[cint][:rx]["bytes"] = $1
- end
- if line =~ /TX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
- net_counters[cint][:tx]["bytes"] = $1
+ so = shell_out("ifconfig -a")
+ cint = nil
+ so.stdout.lines do |line|
+ tmp_addr = nil
+ # dev_valid_name in the kernel only excludes slashes, nulls, spaces
+ # http://git.kernel.org/?p=linux/kernel/git/stable/linux-stable.git;a=blob;f=net/core/dev.c#l851
+ if line =~ /^([0-9a-zA-Z@\.\:\-_]+)\s+/
+ cint = $1
+ iface[cint] = Mash.new
+ if cint =~ /^(\w+)(\d+.*)/
+ iface[cint][:type] = $1
+ iface[cint][:number] = $2
end
end
+ if line =~ /Link encap:(Local Loopback)/ || line =~ /Link encap:(.+?)\s/
+ iface[cint][:encapsulation] = encaps_lookup($1)
+ end
+ if line =~ /HWaddr (.+?)\s/
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
+ iface[cint][:addresses][$1] = { "family" => "lladdr" }
+ end
+ if line =~ /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
+ iface[cint][:addresses][$1] = { "family" => "inet" }
+ tmp_addr = $1
+ end
+ if line =~ /inet6 addr: ([a-f0-9\:]+)\/(\d+) Scope:(\w+)/
+ iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
+ iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2, "scope" => ($3.eql?("Host") ? "Node" : $3) }
+ end
+ if line =~ /Bcast:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
+ iface[cint][:addresses][tmp_addr]["broadcast"] = $1
+ end
+ if line =~ /Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
+ iface[cint][:addresses][tmp_addr]["netmask"] = $1
+ end
+ flags = line.scan(/(UP|BROADCAST|DEBUG|LOOPBACK|POINTTOPOINT|NOTRAILERS|RUNNING|NOARP|PROMISC|ALLMULTI|SLAVE|MASTER|MULTICAST|DYNAMIC)\s/)
+ if flags.length > 1
+ iface[cint][:flags] = flags.flatten
+ end
+ if line =~ /MTU:(\d+)/
+ iface[cint][:mtu] = $1
+ end
+ if line =~ /P-t-P:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
+ iface[cint][:peer] = $1
+ end
+ if line =~ /RX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) frame:(\d+)/
+ net_counters[cint] = Mash.new unless net_counters[cint]
+ net_counters[cint][:rx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "frame" => $5 }
+ end
+ if line =~ /TX packets:(\d+) errors:(\d+) dropped:(\d+) overruns:(\d+) carrier:(\d+)/
+ net_counters[cint][:tx] = { "packets" => $1, "errors" => $2, "drop" => $3, "overrun" => $4, "carrier" => $5 }
+ end
+ if line =~ /collisions:(\d+)/
+ net_counters[cint][:tx]["collisions"] = $1
+ end
+ if line =~ /txqueuelen:(\d+)/
+ net_counters[cint][:tx]["queuelen"] = $1
+ end
+ if line =~ /RX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
+ net_counters[cint][:rx]["bytes"] = $1
+ end
+ if line =~ /TX bytes:(\d+) \((\d+?\.\d+ .+?)\)/
+ net_counters[cint][:tx]["bytes"] = $1
+ end
end
-
- popen4("arp -an") do |pid, stdin, stdout, stderr|
- stdin.close
- stdout.each do |line|
- if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) \[(\w+)\] on ([0-9a-zA-Z\.\:\-]+)/
- next unless iface[$4] # this should never happen
- iface[$4][:arp] = Mash.new unless iface[$4][:arp]
- iface[$4][:arp][$1] = $2.downcase
- end
+ so = shell_out("arp -an")
+ so.stdout.lines do |line|
+ if line =~ /^\S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) \[(\w+)\] on ([0-9a-zA-Z\.\:\-]+)/
+ next unless iface[$4] # this should never happen
+ iface[$4][:arp] = Mash.new unless iface[$4][:arp]
+ iface[$4][:arp][$1] = $2.downcase
end
end
-
end
-
-
counters[:network][:interfaces] = net_counters
-
network["interfaces"] = iface
end
end
diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb
index 378c2892..22cb6800 100644
--- a/spec/unit/plugins/linux/network_spec.rb
+++ b/spec/unit/plugins/linux/network_spec.rb
@@ -25,28 +25,16 @@ rescue LoadError => e
raise e
end
-def prepare_data
- @ifconfig_lines = @linux_ifconfig.split("\n")
- @route_lines = @linux_route_n.split("\n")
- @arp_lines = @linux_arp_an.split("\n")
- @ipaddr_lines = @linux_ip_addr.split("\n")
- @iplink_lines = @linux_ip_link_s_d.split("\n")
- @ipneighbor_lines = @linux_ip_neighbor_show.split("\n")
- @ipneighbor_lines_inet6 = @linux_ip_inet6_neighbor_show.split("\n")
- @ip_route_lines = @linux_ip_route.split("\n")
- @ip_route_inet6_lines = @linux_ip_route_inet6.split("\n")
-end
-
def do_stubs
- @plugin.stub(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
- @plugin.stub(:popen4).with("ifconfig -a").and_yield(nil, @stdin_ifconfig, @ifconfig_lines, nil)
- @plugin.stub(:popen4).with("arp -an").and_yield(nil, @stdin_arp, @arp_lines, nil)
- @plugin.stub(:popen4).with("ip -f inet neigh show").and_yield(nil, @stdin_ipneighbor, @ipneighbor_lines, nil)
- @plugin.stub(:popen4).with("ip -f inet6 neigh show").and_yield(nil, @stdin_ipneighbor_inet6, @ipneighbor_lines_inet6, nil)
- @plugin.stub(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @plugin.stub(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
- @plugin.stub(:popen4).with("ip -f inet route show").and_yield(nil, @stdin_ip_route, @ip_route_lines, nil)
- @plugin.stub(:popen4).with("ip -f inet6 route show").and_yield(nil, @stdin_ip_route_inet6, @ip_route_inet6_lines, nil)
+ @plugin.stub(:shell_out).with("ip addr").and_return(mock_shell_out(0, @linux_ip_addr, ""))
+ @plugin.stub(:shell_out).with("ip -d -s link").and_return(mock_shell_out(0, @linux_ip_link_s_d, ""))
+ @plugin.stub(:shell_out).with("ip -f inet neigh show").and_return(mock_shell_out(0, @linux_ip_neighbor_show, ""))
+ @plugin.stub(:shell_out).with("ip -f inet6 neigh show").and_return(mock_shell_out(0, @linux_ip_inet6_neighbor_show, ""))
+ @plugin.stub(:shell_out).with("ip -f inet route show").and_return(mock_shell_out(0, @linux_ip_route, ""))
+ @plugin.stub(:shell_out).with("ip -f inet6 route show").and_return(mock_shell_out(0, @linux_ip_route_inet6, ""))
+ @plugin.stub(:shell_out).with("route -n").and_return(mock_shell_out(0, @linux_route_n, ""))
+ @plugin.stub(:shell_out).with("ifconfig -a").and_return(mock_shell_out(0, @linux_ifconfig, ""))
+ @plugin.stub(:shell_out).with("arp -an").and_return(mock_shell_out(0, @linux_arp_an, ""))
end
describe Ohai::System, "Linux Network Plugin" do
@@ -266,26 +254,15 @@ fe80::/64 dev eth0.11 proto kernel metric 256
default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
IP_ROUTE_SCOPE
- @stdin_ifconfig = StringIO.new
- @stdin_arp = StringIO.new
- @stdin_ipaddr = StringIO.new
- @stdin_iplink = StringIO.new
- @stdin_ipneighbor = StringIO.new
- @stdin_ipneighbor_inet6 = StringIO.new
- @stdin_ip_route = StringIO.new
- @stdin_ip_route_inet6 = StringIO.new
-
- prepare_data
-
@plugin = get_plugin("linux/network")
- @plugin.stub(:popen4).with("ifconfig -a")
- @plugin.stub(:popen4).with("arp -an")
+ @plugin.stub(:shell_out).with("ifconfig -a").and_return([0, @linux_ifconfig, ""])
+ @plugin.stub(:shell_out).with("arp -an").and_return([0, @linux_arp_an, ""])
Ohai::Log.should_receive(:warn).with(/unable to detect/).exactly(3).times
-
+
%w{ linux/hostname hostname network }.each do |plgn|
p = get_plugin(plgn)
- p.stub(:from).with("hostname -s").and_return("katie")
- p.stub(:from).with("hostname --fqdn").and_return("katie.bethell")
+ p.stub(:shell_out).with("hostname -s").and_return(mock_shell_out(0, "katie", ""))
+ p.stub(:shell_out).with("hostname --fqdn").and_return(mock_shell_out(0, "katie.bethell", ""))
p.run
end
end
@@ -486,7 +463,6 @@ Destination Gateway Genmask Flags Metric Ref Use Iface
10.116.201.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth0
ROUTE_N
- prepare_data
do_stubs
@plugin.run
@@ -514,7 +490,6 @@ Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.15 0.0.0.0 UG 0 0 0 eth0.11
ROUTE_N
- prepare_data
do_stubs
@plugin.run
@@ -628,7 +603,6 @@ fe80::/64 dev eth0.11 proto kernel metric 256
default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
IP_ROUTE_SCOPE
- prepare_data
do_stubs
end
@@ -669,7 +643,6 @@ default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
default via 1111:2222:3333:4444::ffff dev eth0.11 metric 1023
IP_ROUTE_SCOPE
- prepare_data
do_stubs
end
@@ -712,7 +685,6 @@ default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
default via 1111:2222:3333:4444::ffff dev eth0.11 metric 1023 src 1111:2222:3333:4444::2
IP_ROUTE_SCOPE
- prepare_data
do_stubs
end
@@ -751,7 +723,6 @@ fe80::/64 dev eth0.11 proto kernel metric 256
default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
IP_ROUTE_SCOPE
- prepare_data
do_stubs
end
@@ -779,7 +750,6 @@ IP_ROUTE_SCOPE
default via 172.16.19.1 dev tun0
IP_ROUTE
- prepare_data
do_stubs
end
@@ -808,7 +778,6 @@ IP_ROUTE
default dev venet0 scope link
IP_ROUTE
- prepare_data
do_stubs
end
@@ -831,7 +800,6 @@ fe80::/64 dev eth0 proto kernel metric 256
default via fe80::21c:eff:fe12:3456 dev eth0.153 src fe80::2e0:81ff:fe2b:48e7 metric 1024
IP_ROUTE_SCOPE
- prepare_data
do_stubs
end
@@ -864,7 +832,6 @@ fe80::/64 dev eth0.11 proto kernel metric 256
1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
IP_ROUTE
- prepare_data
do_stubs
end
@@ -902,7 +869,6 @@ fe80::/64 dev eth0.11 proto kernel metric 256
default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
IP_ROUTE
- prepare_data
do_stubs
end
@@ -936,7 +902,6 @@ IP_ROUTE
@linux_ip_route = <<-IP_ROUTE
192.168.122.0/24 dev virbr0 proto kernel src 192.168.122.1
IP_ROUTE
- prepare_data
do_stubs
end