summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@opscode.com>2012-05-13 18:20:48 -0700
committerBryan McLellan <btm@opscode.com>2012-05-13 18:20:48 -0700
commit1f18afa32e0aac8f999aacbad9b089d2a8945606 (patch)
tree89866a4768a0ad26861a3ea1a6faed0c91e34844
parentc74fd00d34d4d9099bb785b32e9f71326ac0b161 (diff)
parent1613d532c755be5954751c6cc0321d3e87c35123 (diff)
downloadohai-1f18afa32e0aac8f999aacbad9b089d2a8945606.tar.gz
Merge branch 'OHAI-370'
-rw-r--r--lib/ohai/plugins/linux/network.rb197
-rw-r--r--spec/ohai/plugins/linux/network_spec.rb772
2 files changed, 674 insertions, 295 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb
index c2fa390c..9f6891d4 100644
--- a/lib/ohai/plugins/linux/network.rb
+++ b/lib/ohai/plugins/linux/network.rb
@@ -40,26 +40,21 @@ IPROUTE_INT_REGEX = /^(\d+): ([0-9a-zA-Z@:\.\-_]*?)(@[0-9a-zA-Z]+|):\s/
if File.exist?("/sbin/ip")
- begin
- route_result = from("ip route show exact 0.0.0.0/0").chomp
-
- # expected results:
- # - default via 10.0.2.4 dev br0
- # - default dev br0 scope link
- if route_result_match = route_result.match(/\svia\s+([^\s+]+)\s+dev\s([^\s+]+)\b/)
- network[:default_interface] = route_result_match[2]
- network[:default_gateway] = route_result_match[1]
- elsif route_result_match = route_result.match(/\sdev\s([^\s+]+)\s+scope\s+link/)
- network[:default_interface] = route_result_match[1]
- network[:default_gateway] = "0.0.0.0" # backward compatible result
- else
- # should nodes always have a default route ? i don't think so
- # anyway, backward compatible raise ! :-)
- raise
- end
- rescue
- Ohai::Log.debug("Unable to determine default interface")
- end
+ # families to get default routes from
+ 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
@@ -181,44 +176,138 @@ if File.exist?("/sbin/ip")
end
end
- popen4("ip neighbor show") do |pid, stdin, stdout, stderr|
- stdin.close
- stdout.each do |line|
- if line =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) dev ([0-9a-zA-Z\.\:\-]+) lladdr ([a-fA-F0-9\:]+)/
- next unless iface[$2]
- iface[$2][:arp] = Mash.new unless iface[$2][:arp]
- iface[$2][:arp][$1] = $3.downcase
+ families.each do |family|
+ 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
+ end
end
end
- end
- popen4("ip route show scope link") do |pid, stdin, stdout, stderr|
- stdin.close
- stdout.each do |line|
- if line =~ /^([^\s]+)\s+dev\s+([^\s]+).*\s+src\s+([^\s]+)\b/
- tmp_route_cidr = $1
- tmp_int = $2
- tmp_source_addr = $3
- unless iface[tmp_int]
- Ohai::Log.debug("Skipping previously unseen interface from 'ip route show scope link': #{tmp_int}")
- next
+ # checking the routing tables
+ # why ?
+ # 1) to set the default gateway and default interfaces attributes
+ # 2) on some occasions, the best way to select node[:ipaddress] is to look at
+ # 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
end
- iface[tmp_int][:routes] = Mash.new unless iface[tmp_int][:routes]
- iface[tmp_int][:routes][tmp_route_cidr] = Mash.new( :scope => "Link", :src => tmp_source_addr )
- # while looping through the link level scope routes we will set ipaddress from the source address if
- # 1) there's a default route,
- # the interface is the default_interface
- # the ip source address from the routing table is really set on the node,
- # the route entry matches the default_gateway
- # macaddress is then set from this interface
- # for now the ip6address is brutaly associated with ipaddress' iface
- if (network.has_key? "default_interface") &&
- (network[:default_interface] == tmp_int) &&
- (iface[tmp_int][:addresses].has_key? tmp_source_addr) &&
- (IPAddr.new(tmp_route_cidr).include? network[:default_gateway])
- ipaddress tmp_source_addr
- macaddress iface[tmp_int][:addresses].select{|k,v| v["family"]=="lladdr"}.first.first unless iface[tmp_int][:flags].include? "NOARP"
- ip6address iface[tmp_int][:addresses].reject{|address, hash| hash['family'] != "inet6" || hash['scope'] != 'Global'}.first.first
+ end
+ end
+ # now looking at the routes to set the default attributes
+ # for information, default routes can be of this form :
+ # - default via 10.0.2.4 dev br0
+ # - default dev br0 scope link
+ # - default via 10.0.3.1 dev eth1 src 10.0.3.2 metric 10
+ # - default via 10.0.4.1 dev eth2 src 10.0.4.2 metric 20
+
+ # using a temporary var to hold routes and their interface name
+ routes = iface.collect do |i,iv|
+ iv[:routes].collect do |r|
+ r.merge(:dev=>i) if r[:family] == family[:name]
+ end.compact if iv[:routes]
+ end.compact.flatten
+
+ # using a temporary var to hold the default route
+ # in case there are more than 1 default route, sort it by its metric
+ # and return the first one
+ # (metric value when unspecified is 0)
+ default_route = routes.select do |r|
+ r[:destination] == "default"
+ end.sort do |x,y|
+ (x[:metric].nil? ? 0 : x[:metric].to_i) <=> (y[:metric].nil? ? 0 : y[:metric].to_i)
+ end.first
+
+ if default_route.nil? or default_route.empty?
+ Ohai::Log.debug("Unable to determine default #{family[:name]} interface")
+ else
+ network["#{default_prefix}_interface"] = default_route[:dev]
+ Ohai::Log.debug("#{default_prefix}_interface set to #{default_route[:dev]}")
+
+ # setting gateway to 0.0.0.0 or :: if the default route is a link level one
+ network["#{default_prefix}_gateway"] = default_route[:via] ? default_route[:via] : family[:default_route].chomp("/0")
+ Ohai::Log.debug("#{default_prefix}_gateway set to #{network["#{default_prefix}_gateway"]}")
+
+ # since we're at it, let's populate {ip,mac,ip6}address with the best values
+ # using the source field when it's specified :
+ # 1) in the default route
+ # 2) in the route entry used to reach the default gateway
+ route = routes.select do |r|
+ # selecting routes
+ r[:src] and # it has a src field
+ iface[r[:dev]] and # the iface exists
+ iface[r[:dev]][:addresses].has_key? r[:src] and # the src ip is set on the node
+ iface[r[:dev]][:addresses][r[:src]][:scope].downcase != "link" and # this isn't a link level addresse
+ ( r[:destination] == "default" or
+ ( default_route[:via] and # the default route has a gateway
+ IPAddress(r[:destination]).include? IPAddress(default_route[:via]) # the route matches the gateway
+ )
+ )
+ end.sort_by do |r|
+ # sorting the selected routes:
+ # - getting default routes first
+ # - then sort by metric
+ # - then by prefixlen
+ [
+ r[:destination] == "default" ? 0 : 1,
+ r[:metric].nil? ? 0 : r[:metric].to_i,
+ # for some reason IPAddress doesn't accept "::/0", it doesn't like prefix==0
+ # just a quick workaround: use 0 if IPAddress fails
+ begin
+ IPAddress( r[:destination] == "default" ? family[:default_route] : r[:destination] ).prefix
+ rescue
+ 0
+ end
+ ]
+ end.first
+
+ unless route.nil? or route.empty?
+ if family[:name] == "inet"
+ ipaddress route[:src]
+ macaddress iface[route[:dev]][:addresses].select{|k,v| v["family"]=="lladdr"}.first.first unless iface[route[:dev]][:flags].include? "NOARP"
+ else
+ ip6address route[:src]
end
end
end
diff --git a/spec/ohai/plugins/linux/network_spec.rb b/spec/ohai/plugins/linux/network_spec.rb
index 154086f5..b5ce675c 100644
--- a/spec/ohai/plugins/linux/network_spec.rb
+++ b/spec/ohai/plugins/linux/network_spec.rb
@@ -25,10 +25,34 @@ 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
+ @ohai.stub!(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
+ @ohai.stub!(:popen4).with("ifconfig -a").and_yield(nil, @stdin_ifconfig, @ifconfig_lines, nil)
+ @ohai.stub!(:popen4).with("arp -an").and_yield(nil, @stdin_arp, @arp_lines, nil)
+ @ohai.stub!(:popen4).with("ip -f inet neigh show").and_yield(nil, @stdin_ipneighbor, @ipneighbor_lines, nil)
+ @ohai.stub!(:popen4).with("ip -f inet6 neigh show").and_yield(nil, @stdin_ipneighbor_inet6, @ipneighbor_lines_inet6, nil)
+ @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
+ @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
+ @ohai.stub!(:popen4).with("ip -f inet route show").and_yield(nil, @stdin_ip_route, @ip_route_lines, nil)
+ @ohai.stub!(:popen4).with("ip -f inet6 route show").and_yield(nil, @stdin_ip_route_inet6, @ip_route_inet6_lines, nil)
+end
+
describe Ohai::System, "Linux Network Plugin" do
before do
- linux_ifconfig = <<-ENDIFCONFIG
+ @linux_ifconfig = <<-ENDIFCONFIG
eth0 Link encap:Ethernet HWaddr 12:31:3D:02:BE:A2
inet addr:10.116.201.76 Bcast:10.116.201.255 Mask:255.255.255.0
inet6 addr: fe80::1031:3dff:fe02:bea2/64 Scope:Link
@@ -46,6 +70,8 @@ eth0:5 Link encap:Ethernet HWaddr 00:0c:29:41:71:45
eth0.11 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee
inet addr:192.168.0.16 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::2aa:bbff:fecc:ddee/64 Scope:Link
+ inet6 addr: 1111:2222:3333:4444::2/64 Scope:Global
+ inet6 addr: 1111:2222:3333:4444::3/64 Scope:Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1208795008 errors:0 dropped:0 overruns:0 frame:0
TX packets:3269635153 errors:0 dropped:0 overruns:0 carrier:0
@@ -82,6 +108,28 @@ eth0.153 Link encap:Ethernet HWaddr 00:aa:bb:cc:dd:ee
foo:veth0@eth0 Link encap:Ethernet HWaddr ca:b3:73:8b:0c:e4
BROADCAST MULTICAST MTU:1500 Metric:1
+tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
+ inet addr:172.16.19.39 P-t-P:172.16.19.1 Mask:255.255.255.255
+ UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1
+ RX packets:57200 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:100
+ RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB)
+
+venet0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
+ UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1
+ RX packets:57200 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:100
+ RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB)
+
+venet0:0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
+ UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1418 Metric:1
+ RX packets:57200 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:13782 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:100
+ RX bytes:7377600 (7.0 MiB) TX bytes:1175481 (1.1 MiB)
+
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
@@ -94,7 +142,7 @@ ENDIFCONFIG
# Note that ifconfig shows foo:veth0@eth0 but fails to show any address information.
# This was not a mistake collecting the output and Apparently ifconfig is broken in this regard.
- linux_ip_addr = <<-IP_ADDR
+ @linux_ip_addr = <<-IP_ADDR
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
@@ -114,6 +162,9 @@ ENDIFCONFIG
link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
inet 192.168.0.16/24 brd 192.168.0.255 scope global eth0.11
inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link
+ inet6 1111:2222:3333:4444::2/64 scope global
+ valid_lft forever preferred_lft forever
+ inet6 1111:2222:3333:4444::3/64 scope global
valid_lft forever preferred_lft forever
4: eth0.151@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
@@ -134,9 +185,16 @@ ENDIFCONFIG
7: foo:veth0@eth0@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN
link/ether ca:b3:73:8b:0c:e4 brd ff:ff:ff:ff:ff:ff
inet 192.168.212.2/24 scope global foo:veth0@eth0
+8: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
+ link/none
+ inet 172.16.19.39 peer 172.16.19.1 scope global tun0
+9: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
+ link/void
+ inet 127.0.0.2/32 scope host venet0
+ inet 172.16.19.48/32 scope global venet0:0
IP_ADDR
- linux_ip_link_s_d = <<-IP_LINK_S
+ @linux_ip_link_s_d = <<-IP_LINK_S
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
@@ -156,9 +214,21 @@ IP_ADDR
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
+4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
+ link/none
+ RX: bytes packets errors dropped overrun mcast
+ 1392844460 2659966 0 0 0 0
+ TX: bytes packets errors dropped carrier collsns
+ 691785313 1919690 0 0 0 0
+5: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
+ link/void
+ RX: bytes packets errors dropped overrun mcast
+ 1392844460 2659966 0 0 0 0
+ TX: bytes packets errors dropped carrier collsns
+ 691785313 1919690 0 0 0 0
IP_LINK_S
- linux_route_n = <<-ROUTE_N
+ @linux_route_n = <<-ROUTE_N
Kernel IP routing table
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
@@ -166,24 +236,34 @@ Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.116.201.1 0.0.0.0 UG 0 0 0 eth0
ROUTE_N
- linux_arp_an = <<-ARP_AN
+ @linux_arp_an = <<-ARP_AN
? (10.116.201.1) at fe:ff:ff:ff:ff:ff [ether] on eth0
ARP_AN
- linux_ip_neighbor_show = <<-NEIGHBOR_SHOW
+ @linux_ip_neighbor_show = <<-NEIGHBOR_SHOW
10.116.201.1 dev eth0 lladdr fe:ff:ff:ff:ff:ff REACHABLE
NEIGHBOR_SHOW
- linux_ip_route_show_exact = <<-IP_ROUTE
-default via 10.116.201.1 dev eth0
-IP_ROUTE
+ @linux_ip_inet6_neighbor_show = <<-NEIGHBOR_SHOW
+1111:2222:3333:4444::1 dev eth0.11 lladdr 00:1c:0e:12:34:56 router REACHABLE
+fe80::21c:eff:fe12:3456 dev eth0.11 lladdr 00:1c:0e:30:28:00 router REACHABLE
+fe80::21c:eff:fe12:3456 dev eth0.153 lladdr 00:1c:0e:30:28:00 router REACHABLE
+NEIGHBOR_SHOW
- linux_ip_route_scope_link = <<-IP_ROUTE_SCOPE
-10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
+ @linux_ip_route = <<-IP_ROUTE_SCOPE
+10.116.201.0/24 dev eth0 proto kernel
192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+default via 10.116.201.1 dev eth0
+IP_ROUTE_SCOPE
+
+ @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
+fe80::/64 dev eth0 proto kernel metric 256
+fe80::/64 dev eth0.11 proto kernel metric 256
+1111:2222:3333:4444::/64 dev eth0.11 metric 1024 expires 86023sec
+default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
IP_ROUTE_SCOPE
@stdin_ifconfig = StringIO.new
@@ -191,17 +271,12 @@ IP_ROUTE_SCOPE
@stdin_ipaddr = StringIO.new
@stdin_iplink = StringIO.new
@stdin_ipneighbor = StringIO.new
- @stdin_ip_route_scope_link = StringIO.new
-
- @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")
- @iproute_lines = linux_ip_route_show_exact
- @ip_route_scope_link_lines = linux_ip_route_scope_link.split("\n")
+ @stdin_ipneighbor_inet6 = StringIO.new
+ @stdin_ip_route = StringIO.new
+ @stdin_ip_route_inet6 = StringIO.new
+ prepare_data
+
@ohai = Ohai::System.new
@ohai.stub!(:require_plugin).and_return(true)
@@ -214,27 +289,25 @@ IP_ROUTE_SCOPE
describe "gathering IP layer address info via #{network_method}" do
before do
File.stub!(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" )
- @ohai.stub!(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
- @ohai.stub!(:popen4).with("ifconfig -a").and_yield(nil, @stdin_ifconfig, @ifconfig_lines, nil)
- @ohai.stub!(:popen4).with("arp -an").and_yield(nil, @stdin_arp, @arp_lines, nil)
- @ohai.stub!(:popen4).with("ip neighbor show").and_yield(nil, @stdin_ipneighbor, @ipneighbor_lines, nil)
- @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
- @ohai.stub!(:popen4).with("ip route show scope link").and_yield(nil, @stdin_ip_route_scope_link, @ip_route_scope_link_lines, nil)
- @ohai._require_plugin("network")
- @ohai._require_plugin("linux::network")
+ do_stubs
end
it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network'].should_not be_nil
end
it "detects the interfaces" do
- @ohai['network']['interfaces'].keys.sort.should == ["eth0", "eth0.11", "eth0.151", "eth0.152", "eth0.153", "eth0:5", "foo:veth0@eth0", "lo"]
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network']['interfaces'].keys.sort.should == ["eth0", "eth0.11", "eth0.151", "eth0.152", "eth0.153", "eth0:5", "foo:veth0@eth0", "lo", "tun0", "venet0", "venet0:0"]
end
it "detects the ipv4 addresses of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0']['addresses'].keys.should include('10.116.201.76')
@ohai['network']['interfaces']['eth0']['addresses']['10.116.201.76']['netmask'].should == '255.255.255.0'
@ohai['network']['interfaces']['eth0']['addresses']['10.116.201.76']['broadcast'].should == '10.116.201.255'
@@ -242,6 +315,8 @@ IP_ROUTE_SCOPE
end
it "detects the ipv4 addresses of an ethernet subinterface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0.11']['addresses'].keys.should include('192.168.0.16')
@ohai['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['netmask'].should == '255.255.255.0'
@ohai['network']['interfaces']['eth0.11']['addresses']['192.168.0.16']['broadcast'].should == '192.168.0.255'
@@ -249,22 +324,41 @@ IP_ROUTE_SCOPE
end
it "detects the ipv6 addresses of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0']['addresses'].keys.should include('fe80::1031:3dff:fe02:bea2')
@ohai['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['scope'].should == 'Link'
@ohai['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['prefixlen'].should == '64'
@ohai['network']['interfaces']['eth0']['addresses']['fe80::1031:3dff:fe02:bea2']['family'].should == 'inet6'
end
+ it "detects the ipv6 addresses of an ethernet subinterface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ %w[ 1111:2222:3333:4444::2 1111:2222:3333:4444::3 ].each do |addr|
+ @ohai['network']['interfaces']['eth0.11']['addresses'].keys.should include(addr)
+ @ohai['network']['interfaces']['eth0.11']['addresses'][addr]['scope'].should == 'Global'
+ @ohai['network']['interfaces']['eth0.11']['addresses'][addr]['prefixlen'].should == '64'
+ @ohai['network']['interfaces']['eth0.11']['addresses'][addr]['family'].should == 'inet6'
+ end
+ end
+
it "detects the mac addresses of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0']['addresses'].keys.should include('12:31:3D:02:BE:A2')
@ohai['network']['interfaces']['eth0']['addresses']['12:31:3D:02:BE:A2']['family'].should == 'lladdr'
end
it "detects the encapsulation type of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0']['encapsulation'].should == 'Ethernet'
end
it "detects the flags of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
if network_method == "ifconfig"
@ohai['network']['interfaces']['eth0']['flags'].sort.should == ['BROADCAST','MULTICAST','RUNNING','UP']
else
@@ -273,20 +367,28 @@ IP_ROUTE_SCOPE
end
it "detects the number of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0']['number'].should == "0"
end
it "detects the mtu of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0']['mtu'].should == "1500"
end
it "detects the ipv4 addresses of the loopback interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['lo']['addresses'].keys.should include('127.0.0.1')
@ohai['network']['interfaces']['lo']['addresses']['127.0.0.1']['netmask'].should == '255.0.0.0'
@ohai['network']['interfaces']['lo']['addresses']['127.0.0.1']['family'].should == 'inet'
end
it "detects the ipv6 addresses of the loopback interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['lo']['addresses'].keys.should include('::1')
@ohai['network']['interfaces']['lo']['addresses']['::1']['scope'].should == 'Node'
@ohai['network']['interfaces']['lo']['addresses']['::1']['prefixlen'].should == '128'
@@ -294,10 +396,14 @@ IP_ROUTE_SCOPE
end
it "detects the encapsulation type of the loopback interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['lo']['encapsulation'].should == 'Loopback'
end
it "detects the flags of the ethernet interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
if network_method == "ifconfig"
@ohai['network']['interfaces']['lo']['flags'].sort.should == ['LOOPBACK','RUNNING','UP']
else
@@ -307,10 +413,14 @@ IP_ROUTE_SCOPE
it "detects the mtu of the loopback interface" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['lo']['mtu'].should == "16436"
end
it "detects the arp entries" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0']['arp']['10.116.201.1'].should == 'fe:ff:ff:ff:ff:ff'
end
@@ -319,14 +429,7 @@ IP_ROUTE_SCOPE
describe "gathering interface counters via #{network_method}" do
before do
File.stub!(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" )
- @ohai.stub!(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
- @ohai.stub!(:popen4).with("ifconfig -a").and_yield(nil, @stdin_ifconfig, @ifconfig_lines, nil)
- @ohai.stub!(:popen4).with("arp -an").and_yield(nil, @stdin_arp, @arp_lines, nil)
- @ohai.stub!(:popen4).with("ip neighbor show").and_yield(nil, @stdin_ipneighbor, @ipneighbor_lines, nil)
- @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
- @ohai.stub!(:popen4).with("ip route show scope link").and_yield(nil, @stdin_ip_route_scope_link, @ip_route_scope_link_lines, nil)
+ do_stubs
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
end
@@ -366,13 +469,7 @@ IP_ROUTE_SCOPE
describe "setting the node's default IP address attribute with #{network_method}" do
before do
File.stub!(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" )
- @ohai.stub!(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
- @ohai.stub!(:popen4).with("ifconfig -a").and_yield(nil, @stdin_ifconfig, @ifconfig_lines, nil)
- @ohai.stub!(:popen4).with("arp -an").and_yield(nil, @stdin_arp, @arp_lines, nil)
- @ohai.stub!(:popen4).with("ip neighbor show").and_yield(nil, @stdin_ipneighbor, @ipneighbor_lines, nil)
- @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
+ do_stubs
end
describe "without a subinterface" do
@@ -382,73 +479,68 @@ IP_ROUTE_SCOPE
end
it "finds the default interface by asking which iface has the default route" do
- @ohai['network'][:default_interface].should == 'eth0'
+ @ohai['network']['default_interface'].should == 'eth0'
end
- it "finds the default interface by asking which iface has the default route" do
- @ohai['network'][:default_gateway].should == '10.116.201.1'
+ it "finds the default gateway by asking which iface has the default route" do
+ @ohai['network']['default_gateway'].should == '10.116.201.1'
end
end
- describe "with a link level scope default route" do
+ describe "with a link level default route" do
before do
- linux_ip_route_show_exact = <<-IP_ROUTE
+ @linux_ip_route = <<-IP_ROUTE
+10.116.201.0/24 dev eth0 proto kernel
default dev eth0 scope link
IP_ROUTE
- linux_route_n = <<-ROUTE_N
+ @linux_route_n = <<-ROUTE_N
Kernel IP routing table
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
-169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 eth0
ROUTE_N
- @iproute_lines = linux_ip_route_show_exact
- @route_lines = linux_route_n.split("\n")
- @ohai.stub!(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
+ prepare_data
+ do_stubs
+
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
end
it "finds the default interface by asking which iface has the default route" do
- @ohai['network'][:default_interface].should == 'eth0'
+ @ohai['network']['default_interface'].should == 'eth0'
end
it "finds the default interface by asking which iface has the default route" do
- @ohai['network'][:default_gateway].should == '0.0.0.0'
+ @ohai['network']['default_gateway'].should == '0.0.0.0'
end
end
describe "with a subinterface" do
before do
- linux_ip_route_show_exact = <<-IP_ROUTE
+ @linux_ip_route = <<-IP_ROUTE
+192.168.0.0/24 dev eth0.11 proto kernel src 192.168.0.2
default via 192.168.0.15 dev eth0.11
IP_ROUTE
- linux_route_n = <<-ROUTE_N
+ @linux_route_n = <<-ROUTE_N
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0.11
-10.151.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0.151
-10.151.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0.151
-10.152.0.0 0.0.0.0 255.255.252.0 U 0 0 0 eth0.152
-10.153.0.0 0.0.0.0 255.255.252.0 U 0 0 0 eth0.153
0.0.0.0 192.168.0.15 0.0.0.0 UG 0 0 0 eth0.11
ROUTE_N
- @iproute_lines = linux_ip_route_show_exact
- @route_lines = linux_route_n.split("\n")
- @ohai.stub!(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
+ prepare_data
+ do_stubs
+
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
end
it "finds the default interface by asking which iface has the default route" do
- @ohai['network'][:default_interface].should == 'eth0.11'
+ @ohai['network']["default_interface"].should == 'eth0.11'
end
it "finds the default interface by asking which iface has the default route" do
- @ohai['network'][:default_gateway].should == '192.168.0.15'
+ @ohai['network']["default_gateway"].should == '192.168.0.15'
end
end
end
@@ -457,14 +549,32 @@ ROUTE_N
describe "for newer network features using iproute2 only" do
before do
File.stub!(:exist?).with("/sbin/ip").and_return(true) # iproute2 only
- @ohai.stub!(:from).with("route -n \| grep -m 1 ^0.0.0.0").and_return(@route_lines.last)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
- @ohai.stub!(:popen4).with("ifconfig -a").and_yield(nil, @stdin_ifconfig, @ifconfig_lines, nil)
- @ohai.stub!(:popen4).with("arp -an").and_yield(nil, @stdin_arp, @arp_lines, nil)
- @ohai.stub!(:popen4).with("ip neighbor show").and_yield(nil, @stdin_ipneighbor, @ipneighbor_lines, nil)
- @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
- @ohai.stub!(:popen4).with("ip route show scope link").and_yield(nil, @stdin_ip_route_scope_link, @ip_route_scope_link_lines, nil)
+ do_stubs
+ end
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
+
+ it "finds the default inet6 interface if there's a inet6 default route" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network']['default_inet6_interface'].should == 'eth0.11'
+ end
+
+ it "finds the default inet6 gateway if there's a inet6 default route" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network']['default_inet6_gateway'].should == '1111:2222:3333:4444::1'
+ end
+
+ it "finds inet6 neighbours" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network']['interfaces']['eth0.11']['neighbour_inet6']['1111:2222:3333:4444::1'].should == '00:1c:0e:12:34:56'
end
it "detects the ipv4 addresses of an ethernet interface with a crazy name" do
@@ -496,219 +606,399 @@ ROUTE_N
@ohai['network']['interfaces']['eth0.11']['state'].should == 'up'
end
- it "adds link level routes" do
- @ohai._require_plugin("network")
- @ohai._require_plugin("linux::network")
- @ohai['network']['interfaces']['eth0']['routes']['10.116.201.0/24'].should == Mash.new( :scope => "Link", :src => "10.116.201.76" )
- @ohai['network']['interfaces']['foo:veth0@eth0']['routes']['192.168.212.0/24'].should == Mash.new( :scope => "Link", :src => "192.168.212.2" )
- end
-
- describe "checking for the source address to reach the default gateway" do
- it "sets ipaddress" do
+ describe "when dealing with routes" do
+ it "adds routes" do
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
- @ohai['ipaddress'].should == "10.116.201.76"
+ @ohai['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "10.116.201.0/24", :proto => "kernel", :family =>"inet" )
+ @ohai['network']['interfaces']['foo:veth0@eth0']['routes'].should include Mash.new( :destination => "192.168.212.0/24", :proto => "kernel", :src => "192.168.212.2", :family =>"inet" )
+ @ohai['network']['interfaces']['eth0']['routes'].should include Mash.new( :destination => "fe80::/64", :metric => "256", :proto => "kernel", :family => "inet6" )
+ @ohai['network']['interfaces']['eth0.11']['routes'].should include Mash.new( :destination => "1111:2222:3333:4444::/64", :metric => "1024", :family => "inet6" )
+ @ohai['network']['interfaces']['eth0.11']['routes'].should include Mash.new( :destination => "default", :via => "1111:2222:3333:4444::1", :metric => "1024", :family => "inet6")
end
- it "sets ip6address" do
- @ohai._require_plugin("network")
- @ohai._require_plugin("linux::network")
- @ohai['ip6address'].should == "2001:44b8:4160:8f00:a00:27ff:fe13:eacd"
- end
+ describe "when there isn't a source field in route entries " do
+ it "doesn't set ipaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ipaddress'].should be nil
+ end
- describe "when about to set macaddress" do
- it "sets macaddress" do
+ it "doesn't set macaddress" do
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
- @ohai['macaddress'].should == "12:31:3D:02:BE:A2"
+ @ohai['macaddress'].should be nil
end
- describe "when then interface has the NOARP flag" do
- before do
- linux_ip_link_s_d = <<-IP_LINK_S
-1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- RX: bytes packets errors dropped overrun mcast
- 35224 524 0 0 0 0
- TX: bytes packets errors dropped carrier collsns
- 35224 524 0 0 0 0
-2: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
- link/none
- RX: bytes packets errors dropped overrun mcast
- 1392844460 2659966 0 0 0 0
- TX: bytes packets errors dropped carrier collsns
- 691785313 1919690 0 0 0 0
-IP_LINK_S
+ it "doesn't set ip6address" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ip6address'].should be nil
+ end
+ end
- linux_ip_route_show_exact = <<-IP_ROUTE
-default via 172.16.19.1 dev tun0
-IP_ROUTE
+ describe "when there's a source field in the default route entry" do
+ before do
+ @linux_ip_route = <<-IP_ROUTE_SCOPE
+10.116.201.0/24 dev eth0 proto kernel
+192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
+192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
+172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
+192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+default via 10.116.201.1 dev eth0 src 10.116.201.76
+IP_ROUTE_SCOPE
- linux_ip_addr = <<-IP_ADDR
-1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
-2: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
- link/none
- inet 172.16.19.39 peer 172.16.19.1 scope global tun0
-IP_ADDR
+ @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
+fe80::/64 dev eth0 proto kernel metric 256
+fe80::/64 dev eth0.11 proto kernel metric 256
+1111:2222:3333:4444::/64 dev eth0.11 metric 1024
+default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024 src 1111:2222:3333:4444::3
+IP_ROUTE_SCOPE
- linux_ip_route_scope_link = <<-IP_ROUTE_SCOPE
-10.118.19.1 dev tun0 proto kernel src 10.118.19.39
+ prepare_data
+ do_stubs
+ end
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
+
+ it "sets ipaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ipaddress'].should == "10.116.201.76"
+ end
+
+ it "sets ip6address" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ip6address'].should == "1111:2222:3333:4444::3"
+ end
+ end
+
+ describe "when there're several default routes" do
+ before do
+ @linux_ip_route = <<-IP_ROUTE_SCOPE
+10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
+192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
+192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
+172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
+192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+default via 10.116.201.1 dev eth0 metric 10
+default via 10.116.201.254 dev eth0 metric 9
+IP_ROUTE_SCOPE
+
+ @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
+fe80::/64 dev eth0 proto kernel metric 256
+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
+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
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
+
+ it "sets default ipv4 interface and gateway" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network']['default_interface'].should == 'eth0'
+ @ohai['network']['default_gateway'].should == '10.116.201.254'
+ end
+
+ it "sets default ipv6 interface and gateway" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network']['default_inet6_interface'].should == 'eth0.11'
+ @ohai['network']['default_inet6_gateway'].should == '1111:2222:3333:4444::ffff'
+ end
+ end
+
+ describe "when there're a mixed setup of routes that could be used to set ipaddress" do
+ before do
+ @linux_ip_route = <<-IP_ROUTE_SCOPE
+10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
+192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
+192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
+172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
+192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+default via 10.116.201.1 dev eth0 metric 10
+default via 10.116.201.254 dev eth0 metric 9 src 10.116.201.74
+IP_ROUTE_SCOPE
+
+ @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
+fe80::/64 dev eth0 proto kernel metric 256
+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
+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
- @iplink_lines = linux_ip_link_s_d.split("\n")
- @ipaddr_lines = linux_ip_addr.split("\n")
- @iproute_lines = linux_ip_route_show_exact
- @ip_route_scope_link_lines = linux_ip_route_scope_link.split("\n")
- @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
- @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
- @ohai.stub!(:popen4).with("ip route show scope link").and_yield(nil, @stdin_ip_route_scope_link, @ip_route_scope_link_lines, nil)
+ prepare_data
+ do_stubs
+ end
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
+ it "sets ipaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai["ipaddress"].should == "10.116.201.74"
+ end
+
+ it "sets ip6address" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai["ip6address"].should == "1111:2222:3333:4444::2"
+ end
+ end
+
+ describe "when there's a source field in a local route entry " do
+ before do
+ @linux_ip_route = <<-IP_ROUTE_SCOPE
+10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
+192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
+192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
+172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
+192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+default via 10.116.201.1 dev eth0
+IP_ROUTE_SCOPE
+
+ @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
+fe80::/64 dev eth0 proto kernel metric 256
+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
+default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
+IP_ROUTE_SCOPE
+
+ prepare_data
+ do_stubs
+ end
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
+
+ it "sets ipaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ipaddress'].should == "10.116.201.76"
+ end
+
+ describe "when about to set macaddress" do
+ it "sets macaddress" do
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
+ @ohai['macaddress'].should == "12:31:3D:02:BE:A2"
end
- it "macaddress shouldn't be set" do
- @ohai._require_plugin("network")
- @ohai._require_plugin("linux::network")
- @ohai['macaddress'].should be_nil
+ describe "when then interface has the NOARP flag" do
+ before do
+ @linux_ip_route = <<-IP_ROUTE
+10.118.19.1 dev tun0 proto kernel src 10.118.19.39
+default via 172.16.19.1 dev tun0
+IP_ROUTE
+
+ prepare_data
+ do_stubs
+ end
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
+
+ it "doesn't set macaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['macaddress'].should be_nil
+ end
end
end
+
+ it "sets ip6address" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ip6address'].should == "1111:2222:3333:4444::3"
+ end
end
- describe "with Linux-VServer" do
+ describe "with a link level default route" do
before do
- linux_ip_link_s_d = <<-IP_LINK_S
-1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- RX: bytes packets errors dropped overrun mcast
- 35224 524 0 0 0 0
- TX: bytes packets errors dropped carrier collsns
- 35224 524 0 0 0 0
-2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
- link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff
- RX: bytes packets errors dropped overrun mcast
- 1392844460 2659966 0 0 0 0
- TX: bytes packets errors dropped carrier collsns
- 691785313 1919690 0 0 0 0
-3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
- link/ether 12:31:3d:02:be:a3 brd ff:ff:ff:ff:ff:ff
- RX: bytes packets errors dropped overrun mcast
- 1392844460 2659966 0 0 0 0
- TX: bytes packets errors dropped carrier collsns
- 691785313 1919690 0 0 0 0
-IP_LINK_S
-
- linux_ip_route_show_exact = <<-IP_ROUTE
-default via 172.16.19.1 dev eth0
+ @linux_ip_route = <<-IP_ROUTE
+default dev venet0 scope link
IP_ROUTE
- linux_ip_addr = <<-IP_ADDR
-1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
-2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
- link/ether 00:26:2d:03:80:56 brd ff:ff:ff:ff:ff:ff
- inet 172.16.19.48/26 brd 172.16.19.63 scope global secondary eth0
-3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
- link/ether 00:26:2d:03:80:57 brd ff:ff:ff:ff:ff:ff
- inet 10.118.19.48/26 brd 10.118.19.63 scope global secondary eth1
-IP_ADDR
+ prepare_data
+ do_stubs
+ end
- linux_ip_route_scope_link = <<-IP_ROUTE_SCOPE
-172.16.19.0/26 dev eth0 proto kernel src 172.16.19.39
-172.16.19.0/26 dev if4 proto kernel src 172.16.19.45
-10.118.19.0/26 dev eth0 proto kernel src 10.118.19.39
-10.118.19.0/26 dev eth1 proto kernel src 10.118.19.11
-10.118.19.0/26 dev if5 proto kernel src 10.118.19.45
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
+
+ it "doesn't set ipaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ipaddress'].should be_nil
+ end
+ end
+
+ describe "when not having a global scope ipv6 address" do
+ before do
+ @linux_ip_route_inet6 = <<-IP_ROUTE_SCOPE
+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
- @iplink_lines = linux_ip_link_s_d.split("\n")
- @ipaddr_lines = linux_ip_addr.split("\n")
- @iproute_lines = linux_ip_route_show_exact
- @ip_route_scope_link_lines = linux_ip_route_scope_link.split("\n")
- @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
- @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
- @ohai.stub!(:popen4).with("ip route show scope link").and_yield(nil, @stdin_ip_route_scope_link, @ip_route_scope_link_lines, nil)
+ prepare_data
+ do_stubs
+ end
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
end
- it "sets ipaddress" do
- @ohai['ipaddress'].should be_nil
+
+ it "doesn't set ip6address" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ip6address'].should be_nil
end
+
end
- describe "with OpenVZ" do
+ describe "with no default route" do
before do
- linux_ip_link_s_d = <<-IP_LINK_S
-1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- RX: bytes packets errors dropped overrun mcast
- 35224 524 0 0 0 0
- TX: bytes packets errors dropped carrier collsns
- 35224 524 0 0 0 0
-2: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
- link/void
- RX: bytes packets errors dropped overrun mcast
- 1392844460 2659966 0 0 0 0
- TX: bytes packets errors dropped carrier collsns
- 691785313 1919690 0 0 0 0
-IP_LINK_S
+ @linux_ip_route = <<-IP_ROUTE
+10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76
+192.168.5.0/24 dev eth0 proto kernel src 192.168.5.1
+192.168.212.0/24 dev foo:veth0@eth0 proto kernel src 192.168.212.2
+172.16.151.0/24 dev eth0 proto kernel src 172.16.151.100
+192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2
+IP_ROUTE
- linux_ip_route_show_exact = <<-IP_ROUTE
-default dev venet0 scope link
+ @linux_ip_route_inet6 = <<-IP_ROUTE
+fe80::/64 dev eth0 proto kernel metric 256
+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
- linux_ip_addr = <<-IP_ADDR
-1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
-2: venet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
- link/void
- inet 127.0.0.2/32 scope host venet0
- inet 172.16.19.48/32 scope global venet0:0
-IP_ADDR
+ prepare_data
+ do_stubs
+ end
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
+ end
- linux_ip_route_scope_link = ""
+ it "doesn't set ipaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ipaddress'].should be_nil
+ end
- @iplink_lines = linux_ip_link_s_d.split("\n")
- @ipaddr_lines = linux_ip_addr.split("\n")
- @iproute_lines = linux_ip_route_show_exact
- @ip_route_scope_link_lines = linux_ip_route_scope_link.split("\n")
- @ohai.stub!(:popen4).with("ip -d -s link").and_yield(nil, @stdin_iplink, @iplink_lines, nil)
- @ohai.stub!(:popen4).with("ip addr").and_yield(nil, @stdin_ipaddr, @ipaddr_lines, nil)
- @ohai.stub!(:from).with("ip route show exact 0.0.0.0/0").and_return(@iproute_lines)
- @ohai.stub!(:popen4).with("ip route show scope link").and_yield(nil, @stdin_ip_route_scope_link, @ip_route_scope_link_lines, nil)
+ it "doesn't set ip6address" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ip6address'].should be_nil
+ end
+ end
+ describe "with irrelevant routes (container setups)" do
+ before do
+ @linux_ip_route = <<-IP_ROUTE
+10.116.201.0/26 dev eth0 proto kernel src 10.116.201.39
+10.116.201.0/26 dev if4 proto kernel src 10.116.201.45
+10.118.19.0/26 dev eth0 proto kernel src 10.118.19.39
+10.118.19.0/26 dev if5 proto kernel src 10.118.19.45
+default via 10.116.201.1 dev eth0 src 10.116.201.99
+IP_ROUTE
+
+ @linux_ip_route_inet6 = <<-IP_ROUTE
+fe80::/64 dev eth0 proto kernel metric 256
+fe80::/64 dev eth0.11 proto kernel metric 256
+1111:2222:3333:4444::/64 dev eth0.11 metric 1024 src 1111:2222:3333:4444::FFFF:2
+default via 1111:2222:3333:4444::1 dev eth0.11 metric 1024
+IP_ROUTE
+
+ prepare_data
+ do_stubs
+ end
+
+ it "completes the run" do
+ Ohai::Log.should_not_receive(:debug).with(/Plugin linux::network threw exception/)
@ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
+ @ohai['network'].should_not be_nil
end
- it "sets ipaddress" do
+ it "doesn't add bogus routes" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "10.116.201.0/26", :proto => "kernel", :family => "inet", :via => "10.116.201.39" )
+ @ohai['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "10.118.19.0/26", :proto => "kernel", :family => "inet", :via => "10.118.19.39" )
+ @ohai['network']['interfaces']['eth0']['routes'].should_not include Mash.new( :destination => "1111:2222:3333:4444::/64", :family => "inet6", :metric => "1024" )
+ end
+
+ it "doesn't set ipaddress" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
@ohai['ipaddress'].should be_nil
end
+
+ it "doesn't set ip6address" do
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ @ohai['ip6address'].should be_nil
+ end
end
- end
- # This should never happen in the real world.
- describe "when encountering a surprise interface" do
- before do
- linux_ip_route_scope_link = <<-IP_ROUTE_SCOPE
+ # This should never happen in the real world.
+ describe "when encountering a surprise interface" do
+ before do
+ @linux_ip_route = <<-IP_ROUTE
192.168.122.0/24 dev virbr0 proto kernel src 192.168.122.1
-IP_ROUTE_SCOPE
- @ip_route_scope_link_lines = linux_ip_route_scope_link.split("\n")
- @ohai.stub!(:popen4).with("ip route show scope link").and_yield(nil, @stdin_ip_route_scope_link, @ip_route_scope_link_lines, nil)
- end
-
- it "logs a message and skips previously unseen interfaces in 'ip route show scope link'" do
- Ohai::Log.should_receive(:debug).with("Skipping previously unseen interface from 'ip route show scope link': virbr0").once
- Ohai::Log.should_receive(:debug).any_number_of_times # Catches the 'Loading plugin network' type messages
- @ohai._require_plugin("network")
- @ohai._require_plugin("linux::network")
+IP_ROUTE
+ prepare_data
+ do_stubs
+ end
+
+ it "logs a message and skips previously unseen interfaces in 'ip route show'" do
+ Ohai::Log.should_receive(:debug).with("Skipping previously unseen interface from 'ip route show': virbr0").once
+ Ohai::Log.should_receive(:debug).any_number_of_times # Catches the 'Loading plugin network' type messages
+ @ohai._require_plugin("network")
+ @ohai._require_plugin("linux::network")
+ end
end
end
end