summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@opscode.com>2012-06-08 08:43:15 -0700
committerBryan McLellan <btm@opscode.com>2012-06-08 08:43:15 -0700
commitda8397940162dc1ef10b189ac561a26a101f6580 (patch)
tree09fce2c6b16ab98ab810e439083dfc2f55f10a0a
parent256ba7db6227d70dc3eb97f0feb09e2133320789 (diff)
parent482e3f10474606788ac446745db8769cb805c46f (diff)
downloadohai-da8397940162dc1ef10b189ac561a26a101f6580.tar.gz
Merge branch 'OHAI-355'
-rw-r--r--lib/ohai/plugins/network.rb174
-rw-r--r--spec/ohai/plugins/darwin/network_spec.rb6
-rw-r--r--spec/ohai/plugins/linux/network_spec.rb63
-rw-r--r--spec/ohai/plugins/network_spec.rb829
-rw-r--r--spec/ohai/plugins/sigar/network_route_spec.rb1
5 files changed, 967 insertions, 106 deletions
diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb
index 3087f293..68771221 100644
--- a/lib/ohai/plugins/network.rb
+++ b/lib/ohai/plugins/network.rb
@@ -25,63 +25,149 @@ network[:interfaces] = Mash.new unless network[:interfaces]
counters Mash.new unless counters
counters[:network] = Mash.new unless counters[:network]
-ipaddress nil
-ip6address
-macaddress nil
-
require_plugin "hostname"
require_plugin "#{os}::network"
-# ipaddress and macaddress can be set from the #{os}::network plugin
-return unless ipaddress.nil?
+FAMILIES = {
+ "inet" => "default",
+ "inet6" => "default_inet6"
+}
+
+def sorted_ips(family = "inet")
+ raise "bad family #{family}" unless [ "inet", "inet6" ].include? family
+
+ # going to use that later to sort by scope
+ scope_prio = [ "global", "site", "link", "host", "node", nil ]
+
+ ipaddresses = []
+ # ipaddresses going to hold #{family} ipaddresses and their scope
+ Mash[network['interfaces']].each do |iface, iface_v|
+ iface_v['addresses'].each do |addr, addr_v|
+ next if addr_v.nil? or not addr_v.has_key? "family" or addr_v['family'] != family
+ ipaddresses << {
+ :ipaddress => addr_v["prefixlen"] ? IPAddress("#{addr}/#{addr_v["prefixlen"]}") : IPAddress("#{addr}/#{addr_v["netmask"]}"),
+ :scope => addr_v["scope"].nil? ? nil : addr_v["scope"].downcase,
+ :iface => iface
+ }
+ end
+ end
+
+ # sort ip addresses by scope, by prefixlen and then by ip address
+ # 128 - prefixlen: longest prefixes first
+ ipaddresses.sort_by do |v|
+ [ ( scope_prio.index(v[:scope]) or 999999 ),
+ 128 - v[:ipaddress].prefix.to_i,
+ ( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 )
+ ]
+ end
+end
+
+def find_ip(family = "inet")
+ r=sorted_ips(family)
+
+ # return if there isn't any #{family} address !
+ return [ nil, nil ] if r.empty?
-def find_ip_and_mac(addresses, match = nil)
- ip = nil; mac = nil; ip6 = nil
- addresses.keys.each do |addr|
- if match.nil?
- ip = addr if addresses[addr]["family"].eql?("inet")
+ # shortcuts to access default #{family} interface and gateway
+ int_attr = FAMILIES[family] +"_interface"
+ gw_attr = FAMILIES[family] + "_gateway"
+
+ # If we have a default interface that has addresses,
+ # populate the short-cut attributes
+ if network[int_attr]
+
+ # network[int_attr] exists, the choosen ip must be exist on this interface
+ r = r.select do |v|
+ v[:iface] == network[int_attr]
+ end
+ if r.empty?
+ Ohai::Log.warn("[#{family}] no ip on #{network[int_attr]}")
+ elsif network[gw_attr] and
+ network["interfaces"][network[int_attr]] and
+ network["interfaces"][network[int_attr]]["addresses"]
+ if [ "0.0.0.0", "::" ].include? network[gw_attr]
+ # link level default route
+ Ohai::Log.debug("link level default #{family} route, picking ip from #{network[gw_attr]}")
+ r = r.first
+ else
+ r = r.select do |v|
+ network_contains_address(network[gw_attr], v[:ipaddress], v[:iface])
+ end.first
+ if r.nil?
+ Ohai::Log.warn("[#{family}] no ipaddress/mask on #{network[int_attr]} matching the gateway #{network[gw_attr]}")
+ else
+ Ohai::Log.debug("[#{family}] Using default interface #{network[int_attr]} and default gateway #{network[gw_attr]} to set the default ip to #{r[:ipaddress]}")
+ end
+ end
else
- ip = addr if addresses[addr]["family"].eql?("inet") && network_contains_address(match, addr, addresses[addr])
+ # return the first ip address on network[int_attr]
+ r = r.first
end
- ip6 = addr if addresses[addr]["family"].eql?("inet6") && addresses[addr]["scope"].eql?("Global")
- mac = addr if addresses[addr]["family"].eql?("lladdr")
- break if (ip and mac)
+ else
+ r = r.first
+ Ohai::Log.info("[#{family}] no default interface, picking the first ipaddress")
end
- Ohai::Log.debug("Found IPv4 address #{ip} with MAC #{mac} #{match.nil? ? '' : 'matching address ' + match}")
- Ohai::Log.debug("Found IPv6 address #{ip6}") if ip6
- [ip, mac, ip6]
+
+ return [ nil, nil ] if r.nil? or r.empty?
+
+ [ r[:ipaddress].to_s, r[:iface] ]
end
-def network_contains_address(address_to_match, network_ip, network_opts)
- if network_opts[:peer]
- network_opts[:peer] == address_to_match
+def find_mac_from_iface(iface)
+ r = network["interfaces"][iface]["addresses"].select{|k,v| v["family"]=="lladdr"}
+ r.nil? or r.first.nil? ? nil : r.first.first
+end
+
+def network_contains_address(address_to_match, ipaddress, iface)
+ # address_to_match: String
+ # ipaddress: IPAddress
+ # iface: String
+ if peer = network["interfaces"][iface]["addresses"][ipaddress.to_s][:peer]
+ IPAddress(peer) == IPAddress(address_to_match)
else
- network = IPAddress "#{network_ip}/#{network_opts[:netmask]}"
- host = IPAddress address_to_match
- network.include?(host)
+ ipaddress.include? IPAddress(address_to_match)
end
end
-# If we have a default interface that has addresses, populate the short-cut attributes
-# 0.0.0.0 is not a valid gateway address in this case
-if network[:default_interface] and
- network[:default_gateway] and
- network[:default_gateway] != "0.0.0.0" and
- network["interfaces"][network[:default_interface]] and
- network["interfaces"][network[:default_interface]]["addresses"]
- Ohai::Log.debug("Using default interface for default ip and mac address")
- im = find_ip_and_mac(network["interfaces"][network[:default_interface]]["addresses"], network[:default_gateway])
- ipaddress im.shift
- macaddress im.shift
- ip6address im.shift
-else
- network["interfaces"].keys.sort.each do |iface|
- if network["interfaces"][iface]["encapsulation"].eql?("Ethernet")
- Ohai::Log.debug("Picking ip and mac address from first Ethernet interface")
- im = find_ip_and_mac(network["interfaces"][iface]["addresses"])
- ipaddress im.shift
- macaddress im.shift
- return if (ipaddress and macaddress)
+# ipaddress, ip6address and macaddress can be set by the #{os}::network plugin
+# atm it is expected macaddress is set at the same time than ipaddress
+# if ipaddress is set and macaddress is nil, that means the interface
+# ipaddress is bound to has the NOARP flag
+
+
+results = {}
+
+# inet family is treated before inet6
+FAMILIES.keys.sort.each do |family|
+ r = {}
+ ( r["ip"], r["iface"] ) = find_ip(family)
+ r["mac"] = find_mac_from_iface(r["iface"]) unless r["iface"].nil?
+ # don't overwrite attributes if they've already been set by the "#{os}::network" plugin
+ if family == "inet" and ipaddress.nil?
+ if r["ip"].nil?
+ Ohai::Log.warn("unable to detect ipaddress")
+ # i don't issue this warning if r["ip"] exists and r["mac"].nil?
+ # as it could be a valid setup with a NOARP default_interface
+ Ohai::Log.warn("unable to detect macaddress")
+ else
+ ipaddress r["ip"]
+ macaddress r["mac"]
+ end
+ elsif family == "inet6" and ip6address.nil?
+ if r["ip"].nil?
+ Ohai::Log.warn("unable to detect ip6address")
+ else
+ ip6address r["ip"]
+ if r["mac"] and macaddress.nil? and ipaddress.nil?
+ Ohai::Log.info("macaddress set to #{r["mac"]} from the ipv6 setup")
+ macaddress r["mac"]
+ end
end
end
+ results[family] = r
+end
+
+if results["inet"]["iface"] and results["inet6"]["iface"] and
+ results["inet"]["iface"] != results["inet6"]["iface"]
+ Ohai::Log.info("ipaddress and ip6address are set from different interfaces (#{results["inet"]["iface"]} & #{results["inet6"]["iface"]}), macaddress has been set using the ipaddress interface")
end
diff --git a/spec/ohai/plugins/darwin/network_spec.rb b/spec/ohai/plugins/darwin/network_spec.rb
index 214feeed..52ab355f 100644
--- a/spec/ohai/plugins/darwin/network_spec.rb
+++ b/spec/ohai/plugins/darwin/network_spec.rb
@@ -427,6 +427,9 @@ net.smb.fs.tcprcvbuf: 261120
@ohai.stub(:from).with("route -n get default").and_return(darwin_route)
@ohai.stub(:popen4).with("netstat -i -d -l -b -n")
+
+ Ohai::Log.should_receive(:warn).with(/unable to detect/).exactly(3).times
+ @ohai._require_plugin("network")
end
describe "gathering IP layer address info" do
@@ -435,7 +438,6 @@ net.smb.fs.tcprcvbuf: 261120
@ohai.stub!(:popen4).with("ifconfig -a").and_yield(nil, @stdin_ifconfig, @ifconfig_lines, nil)
@ohai.stub(:popen4).with("netstat -i -d -l -b -n").and_yield(nil, @stdin_netstat, @netstat_lines, nil)
@ohai.stub(:popen4).with("sysctl net").and_yield(nil, @stdin_sysctl, @sysctl_lines, nil)
- @ohai._require_plugin("network")
@ohai._require_plugin("darwin::network")
end
@@ -848,4 +850,4 @@ net.smb.fs.tcprcvbuf: 261120
@ohai["network"]["settings"]['net.smb.fs.tcprcvbuf'].should == '261120'
end
end
-end \ No newline at end of file
+end
diff --git a/spec/ohai/plugins/linux/network_spec.rb b/spec/ohai/plugins/linux/network_spec.rb
index b5ce675c..caf7ebf1 100644
--- a/spec/ohai/plugins/linux/network_spec.rb
+++ b/spec/ohai/plugins/linux/network_spec.rb
@@ -282,6 +282,9 @@ IP_ROUTE_SCOPE
@ohai.stub(:popen4).with("ifconfig -a")
@ohai.stub(:popen4).with("arp -an")
+
+ Ohai::Log.should_receive(:warn).with(/unable to detect/).exactly(3).times
+ @ohai._require_plugin("network")
end
["ifconfig","iproute2"].each do |network_method|
@@ -294,19 +297,16 @@ IP_ROUTE_SCOPE
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._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'
@@ -315,7 +315,6 @@ 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'
@@ -324,7 +323,6 @@ 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'
@@ -333,7 +331,6 @@ IP_ROUTE_SCOPE
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)
@@ -344,20 +341,17 @@ IP_ROUTE_SCOPE
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']
@@ -367,19 +361,16 @@ 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'
@@ -387,7 +378,6 @@ IP_ROUTE_SCOPE
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'
@@ -396,13 +386,11 @@ 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']
@@ -413,13 +401,11 @@ 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
@@ -430,7 +416,6 @@ IP_ROUTE_SCOPE
before do
File.stub!(:exist?).with("/sbin/ip").and_return( network_method == "iproute2" )
do_stubs
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
end
@@ -474,7 +459,6 @@ IP_ROUTE_SCOPE
describe "without a subinterface" do
before do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
end
@@ -502,7 +486,6 @@ ROUTE_N
prepare_data
do_stubs
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
end
@@ -531,7 +514,6 @@ ROUTE_N
prepare_data
do_stubs
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
end
@@ -554,31 +536,26 @@ ROUTE_N
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
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['foo:veth0@eth0']['addresses'].keys.should include('192.168.212.2')
@ohai['network']['interfaces']['foo:veth0@eth0']['addresses']['192.168.212.2']['netmask'].should == '255.255.255.0'
@@ -586,7 +563,6 @@ ROUTE_N
end
it "generates a fake interface for ip aliases for backward compatibility" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0:5']['addresses'].keys.should include('192.168.5.1')
@ohai['network']['interfaces']['eth0:5']['addresses']['192.168.5.1']['netmask'].should == '255.255.255.0'
@@ -594,21 +570,18 @@ ROUTE_N
end
it "adds the vlan information of an interface" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0.11']['vlan']['id'].should == '11'
@ohai['network']['interfaces']['eth0.11']['vlan']['flags'].should == [ 'REORDER_HDR' ]
end
it "adds the state of an interface" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['network']['interfaces']['eth0.11']['state'].should == 'up'
end
describe "when dealing with routes" do
it "adds routes" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@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" )
@@ -619,19 +592,16 @@ ROUTE_N
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
it "doesn't set macaddress" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['macaddress'].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
@@ -661,19 +631,16 @@ IP_ROUTE_SCOPE
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
@@ -705,20 +672,17 @@ IP_ROUTE_SCOPE
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'
@@ -751,19 +715,16 @@ IP_ROUTE_SCOPE
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
@@ -793,20 +754,17 @@ IP_ROUTE_SCOPE
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
@@ -824,13 +782,11 @@ IP_ROUTE
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
@@ -838,7 +794,6 @@ IP_ROUTE
end
it "sets ip6address" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['ip6address'].should == "1111:2222:3333:4444::3"
end
@@ -856,13 +811,11 @@ IP_ROUTE
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
@@ -881,13 +834,11 @@ IP_ROUTE_SCOPE
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 ip6address" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['ip6address'].should be_nil
end
@@ -916,19 +867,16 @@ IP_ROUTE
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
it "doesn't set ip6address" do
- @ohai._require_plugin("network")
@ohai._require_plugin("linux::network")
@ohai['ip6address'].should be_nil
end
@@ -957,13 +905,11 @@ IP_ROUTE
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 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" )
@@ -971,13 +917,11 @@ IP_ROUTE
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
@@ -996,7 +940,6 @@ IP_ROUTE
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
diff --git a/spec/ohai/plugins/network_spec.rb b/spec/ohai/plugins/network_spec.rb
new file mode 100644
index 00000000..0ec9dc0f
--- /dev/null
+++ b/spec/ohai/plugins/network_spec.rb
@@ -0,0 +1,829 @@
+#
+# Author:: Laurent Desarmes <laurent.desarmes@u-picardie.fr>
+# Copyright:: Copyright (c) 2012 Laurent Desarmes
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+def it_does_not_fail
+ it "doesn't fail" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ Ohai::Log.should_not_receive(:debug).with(/^Plugin network threw exception/)
+ @ohai._require_plugin("network")
+ %w[ ipaddress, macaddress, ip6address ].each do |attribute|
+ @ohai.should have_key(attribute)
+ end
+ end
+end
+
+describe Ohai::System, "Network Plugin" do
+
+ basic_data = {
+ "linux" => {
+ "network" => {
+ # pp Hash[node['network']] from shef to get the network data
+ # have just removed the neighbour and route entries by hand
+ "interfaces" => {
+ "lo" => {
+ "flags" => ["LOOPBACK", "UP"],
+ "addresses" => {
+ "::1" => {
+ "scope" => "Node",
+ "prefixlen" => "128",
+ "family" => "inet6"
+ },
+ "127.0.0.1" => {
+ "scope" => "Node",
+ "netmask" => "255.0.0.0",
+ "prefixlen" => "8",
+ "family" => "inet"
+ }
+ },
+ "mtu" => "16436",
+ "encapsulation" => "Loopback"
+ },
+ "eth0" => {
+ "flags" => ["BROADCAST", "MULTICAST", "UP"],
+ "number" => "0",
+ "addresses" => {
+ "fe80::216:3eff:fe2f:3679" => {
+ "scope" => "Link",
+ "prefixlen" => "64",
+ "family" => "inet6"
+ },
+ "00:16:3E:2F:36:79" => {"family" => "lladdr"},
+ "192.168.66.33" => {
+ "scope" => "Global",
+ "netmask" => "255.255.255.0",
+ "broadcast" => "192.168.66.255",
+ "prefixlen" => "24",
+ "family" => "inet"
+ },
+ "3ffe:1111:2222::33" => {
+ "prefixlen" => "48",
+ "family" => "inet6",
+ "scope" => "Global"
+ }
+ },
+ "mtu" => "1500",
+ "type" => "eth",
+ "encapsulation" => "Ethernet"
+ },
+ "eth1" => {
+ "flags" => ["BROADCAST", "MULTICAST", "UP"],
+ "number" => "1",
+ "addresses" => {
+ "fe80::216:3eff:fe2f:3680" => {
+ "scope" => "Link",
+ "prefixlen" => "64",
+ "family" => "inet6"
+ },
+ "00:16:3E:2F:36:80" => {"family" => "lladdr"},
+ "192.168.99.11" => {
+ "scope" => "Global",
+ "netmask" => "255.255.255.0",
+ "broadcast" => "192.168.99.255",
+ "prefixlen" => "24",
+ "family" => "inet"
+ },
+ "3ffe:1111:3333::1" => {
+ "prefixlen" => "48",
+ "family" => "inet6",
+ "scope" => "Global"
+ }
+ },
+ "mtu" => "1500",
+ "type" => "eth",
+ "encapsulation" => "Ethernet"
+ }
+ },
+ "default_gateway" => "192.168.66.15",
+ "default_interface" => "eth0",
+ "default_inet6_gateway" => "3ffe:1111:2222::",
+ "default_inet6_interface" => "eth0"
+ }
+ },
+ "windows" => {
+ "network" => {
+ "interfaces" => {
+ "0xb" => {
+ "addresses" => {
+ "172.19.0.130" => {
+ "prefixlen" => "24",
+ "netmask" => "255.255.255.0",
+ "broadcast" => "172.19.0.255",
+ "family" => "inet"
+ },
+ "fe80::698d:3e37:7950:b28c" => {
+ "prefixlen" => "64",
+ "family" => "inet6",
+ "scope" => "Link"
+ },
+ "52:54:44:66:66:02" => {
+ "family" => "lladdr"
+ }
+ },
+ "mtu" => nil,
+ "type" => "Ethernet 802.3",
+ "encapsulation" => "Ethernet"
+ }
+ },
+ "default_gateway" => "172.19.0.1",
+ "default_interface" => "0xb"
+ }
+ }
+ }
+
+ describe "with linux" do
+ before(:each) do
+ @ohai = Ohai::System.new
+ @ohai.stub!(:require_plugin).twice.and_return(true)
+ @ohai["network"] = basic_data["linux"]["network"]
+ end
+
+ describe "when the linux::network plugin hasn't set any of {ip,ip6,mac}address attributes" do
+ describe "simple setup" do
+ it_does_not_fail
+
+ it "logs 2 debug messages" do
+ Ohai::Log.should_receive(:debug).with(/^Loading plugin network/).once
+ Ohai::Log.should_receive(:debug).with(/^\[inet\] Using default/).once
+ Ohai::Log.should_receive(:debug).with(/^\[inet6\] Using default/).once
+ @ohai._require_plugin("network")
+ end
+
+ it "detects {ip,ip6,mac}address" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.33"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:79"
+ @ohai["ip6address"].should == "3ffe:1111:2222::33"
+ end
+ end
+
+ describe "default ipv4 and ipv6 gateway on different interfaces" do
+ describe "both interfaces have an ARP" do
+ before do
+ @ohai["network"]["default_inet6_gateway"] = "3ffe:1111:3333::"
+ @ohai["network"]["default_inet6_interface"] = "eth1"
+ end
+
+ it_does_not_fail
+
+ it "detects {ip,ip6}address" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.33"
+ @ohai["ip6address"].should == "3ffe:1111:3333::1"
+ end
+
+ it "set macaddress from the ipv4 setup" do
+ @ohai._require_plugin("network")
+ @ohai["macaddress"].should == "00:16:3E:2F:36:79"
+ end
+
+ it "informs about this setup" do
+ Ohai::Log.should_receive(:info).with(/^ipaddress and ip6address are set from different interfaces/)
+ @ohai._require_plugin("network")
+ end
+ end
+
+ describe "ipv4 interface has no ARP" do
+ before do
+ @ohai["network"]["interfaces"]["eth0"]["addresses"].delete_if{|k,kv| kv["family"] == "lladdr" }
+ # not really checked by this pluging
+ @ohai["network"]["interfaces"]["eth0"]["flags"] << "NOARP"
+ @ohai["network"]["default_inet6_gateway"] = "3ffe:1111:3333::"
+ @ohai["network"]["default_inet6_interface"] = "eth1"
+ end
+
+ it_does_not_fail
+
+ it "detects {ip,ip6}address" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.33"
+ @ohai["ip6address"].should == "3ffe:1111:3333::1"
+ end
+
+ it "doesn't set macaddress, ipv4 setup is valid and has precedence over ipv6" do
+ Ohai::Log.should_not_receive(:warn).with(/^unable to detect macaddress/)
+ @ohai._require_plugin("network")
+ @ohai["macaddress"].should be_nil
+ end
+
+ it "informs about this setup" do
+ Ohai::Log.should_receive(:info).with(/^ipaddress and ip6address are set from different interfaces/)
+ @ohai._require_plugin("network")
+ end
+ end
+ end
+
+ describe "conflicting results from the linux::network plugin" do
+ describe "default interface doesn't match the default_gateway" do
+ before do
+ @ohai["network"]["default_interface"] = "eth1"
+ @ohai["network"]["default_inet6_interface"] = "eth1"
+ end
+
+ it_does_not_fail
+
+ it "doesn't detect {ip,ip6,mac}address" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should be_nil
+ @ohai["macaddress"].should be_nil
+ @ohai["ip6address"].should be_nil
+ end
+
+ it "warns about this conflict" do
+ Ohai::Log.should_receive(:warn).with(/^\[inet\] no ipaddress\/mask on eth1/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^\[inet6\] no ipaddress\/mask on eth1/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ip6address/).once
+ @ohai._require_plugin("network")
+ end
+ end
+
+ describe "no ip address for the given default interface/gateway" do
+ before do
+ @ohai["network"]["interfaces"]["eth0"]["addresses"].delete_if{|k,v| %w[inet inet6].include? v["family"]}
+ end
+
+ it_does_not_fail
+
+ it "doesn't detect {ip,ip6,mac}address" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should be_nil
+ @ohai["macaddress"].should be_nil
+ @ohai["ip6address"].should be_nil
+ end
+
+ it "warns about this conflict" do
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^\[inet\] no ip on eth0/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ip6address/).once
+ Ohai::Log.should_receive(:warn).with(/^\[inet6\] no ip on eth0/).once
+ @ohai._require_plugin("network")
+ end
+ end
+
+ describe "no ip at all" do
+ before do
+ @ohai["network"]["default_gateway"] = nil
+ @ohai["network"]["default_interface"] = nil
+ @ohai["network"]["default_inet6_gateway"] = nil
+ @ohai["network"]["default_inet6_interface"] = nil
+ @ohai["network"]["interfaces"].each do |i,iv|
+ iv["addresses"].delete_if{|k,kv| %w[inet inet6].include? kv["family"]}
+ end
+ end
+
+ it_does_not_fail
+
+ it "doesn't detect {ip,ip6,mac}address" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should be_nil
+ @ohai["macaddress"].should be_nil
+ @ohai["ip6address"].should be_nil
+ end
+
+ it "should warn about it" do
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ip6address/).once
+ @ohai._require_plugin("network")
+ end
+ end
+ end
+
+ describe "several ipaddresses matching the default route" do
+ describe "bigger prefix not set on the default interface" do
+ before do
+ @ohai["network"]["interfaces"]["eth2"] = {
+ "flags" => ["BROADCAST", "MULTICAST", "UP"],
+ "number" => "2",
+ "addresses" => {
+ "fe80::216:3eff:fe2f:3681" => {
+ "scope" => "Link",
+ "prefixlen" => "64",
+ "family" => "inet6"
+ },
+ "00:16:3E:2F:36:81" => {"family" => "lladdr"},
+ "192.168.66.99" => {
+ "scope" => "Global",
+ "netmask" => "255.255.255.128",
+ "broadcast" => "192.168.99.127",
+ "prefixlen" => "25",
+ "family" => "inet"
+ },
+ "3ffe:1111:2222:0:4444::1" => {
+ "prefixlen" => "64",
+ "family" => "inet6",
+ "scope" => "Global"
+ }
+ }
+ }
+ end
+
+ it_does_not_fail
+
+ it "sets {ip,ip6,mac}address correctly" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.33"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:79"
+ @ohai["ip6address"].should == "3ffe:1111:2222::33"
+ end
+ end
+
+ describe "bigger prefix set on the default interface" do
+ before do
+ @ohai["network"]["interfaces"]["eth0"]["addresses"]["192.168.66.99"] = {
+ "scope" => "Global",
+ "netmask" => "255.255.255.128",
+ "broadcast" => "192.168.66.127",
+ "prefixlen" => "25",
+ "family" => "inet"
+ }
+ @ohai["network"]["interfaces"]["eth0"]["addresses"]["3ffe:1111:2222:0:4444::1"] = {
+ "prefixlen" => "64",
+ "family" => "inet6",
+ "scope" => "Global"
+ }
+ end
+
+ it_does_not_fail
+
+ it "sets {ip,ip6,mac}address correctly" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.99"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:79"
+ @ohai["ip6address"].should == "3ffe:1111:2222:0:4444::1"
+ end
+ end
+
+ describe "smallest ip not set on the default_interface" do
+ before do
+ @ohai["network"]["interfaces"]["eth2"] = {
+ "flags" => ["BROADCAST", "MULTICAST", "UP"],
+ "number" => "2",
+ "addresses" => {
+ "fe80::216:3eff:fe2f:3681" => {
+ "scope" => "Link",
+ "prefixlen" => "64",
+ "family" => "inet6"
+ },
+ "00:16:3E:2F:36:81" => {"family" => "lladdr"},
+ "192.168.66.32" => {
+ "scope" => "Global",
+ "netmask" => "255.255.255.0",
+ "broadcast" => "192.168.66.255",
+ "prefixlen" => "24",
+ "family" => "inet"
+ },
+ "3ffe:1111:2222::32" => {
+ "prefixlen" => "48",
+ "family" => "inet6",
+ "scope" => "Global"
+ }
+ }
+ }
+ end
+
+ it_does_not_fail
+
+ it "sets {ip,ip6,mac}address correctly" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.33"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:79"
+ @ohai["ip6address"].should == "3ffe:1111:2222::33"
+ end
+ end
+
+ describe "smallest ip set on the default_interface" do
+ before do
+ @ohai["network"]["interfaces"]["eth0"]["addresses"]["192.168.66.32"] = {
+ "scope" => "Global",
+ "netmask" => "255.255.255.0",
+ "broadcast" => "192.168.66.255",
+ "prefixlen" => "24",
+ "family" => "inet"
+ }
+ @ohai["network"]["interfaces"]["eth0"]["addresses"]["3ffe:1111:2222::32"] = {
+ "prefixlen" => "48",
+ "family" => "inet6",
+ "scope" => "Global"
+ }
+ end
+
+ it_does_not_fail
+
+ it "sets {ip,ip6,mac}address correctly" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.32"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:79"
+ @ohai["ip6address"].should == "3ffe:1111:2222::32"
+ end
+ end
+ end
+
+ describe "no default route" do
+ describe "first interface is not the best choice" do
+ before do
+ @ohai["network"]["default_gateway"] = nil
+ @ohai["network"]["default_interface"] = nil
+ @ohai["network"]["default_inet6_gateway"] = nil
+ @ohai["network"]["default_inet6_interface"] = nil
+ # removing inet* addresses from eth0, to complicate things a bit
+ @ohai["network"]["interfaces"]["eth0"]["addresses"].delete_if{|k,v| %w[inet inet6].include? v["family"]}
+ end
+
+ it_does_not_fail
+
+ it "picks {ip,mac,ip6}address from the first interface" do
+ Ohai::Log.should_receive(:info).with(/^\[inet\] no default interface/).once
+ Ohai::Log.should_receive(:info).with(/^\[inet6\] no default interface/).once
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.99.11"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:80"
+ @ohai["ip6address"].should == "3ffe:1111:3333::1"
+ end
+ end
+
+ describe "can choose from addresses with different scopes" do
+ before do
+ @ohai["network"]["default_gateway"] = nil
+ @ohai["network"]["default_interface"] = nil
+ @ohai["network"]["default_inet6_gateway"] = nil
+ @ohai["network"]["default_inet6_interface"] = nil
+ # just changing scopes to lInK for eth0 addresses
+ @ohai["network"]["interfaces"]["eth0"]["addresses"].each{|k,v| v[:scope]="lInK" if %w[inet inet6].include? v["family"]}
+ end
+
+ it_does_not_fail
+
+ it "prefers global scope addressses to set {ip,mac,ip6}address" do
+ Ohai::Log.should_receive(:info).with(/^\[inet\] no default interface/).once
+ Ohai::Log.should_receive(:info).with(/^\[inet6\] no default interface/).once
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.99.11"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:80"
+ @ohai["ip6address"].should == "3ffe:1111:3333::1"
+ end
+ end
+ end
+
+ describe "link level default route" do
+ describe "simple setup" do
+ before do
+ @ohai["network"]["default_gateway"] = "0.0.0.0"
+ @ohai["network"]["default_interface"] = "eth1"
+ @ohai["network"]["default_inet6_gateway"] = "::"
+ @ohai["network"]["default_inet6_interface"] = "eth1"
+ end
+
+ it_does_not_fail
+
+ it "displays debug messages" do
+ Ohai::Log.should_receive(:debug).with(/^Loading plugin network/).once
+ Ohai::Log.should_receive(:debug).with(/^link level default inet /).once
+ Ohai::Log.should_receive(:debug).with(/^link level default inet6 /).once
+ @ohai._require_plugin("network")
+ end
+
+ it "picks {ip,mac,ip6}address from the default interface" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.99.11"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:80"
+ @ohai["ip6address"].should == "3ffe:1111:3333::1"
+ end
+ end
+
+ describe "can choose from addresses with different scopes" do
+ before do
+ @ohai["network"]["default_gateway"] = "0.0.0.0"
+ @ohai["network"]["default_interface"] = "eth1"
+ @ohai["network"]["default_inet6_gateway"] = "::"
+ @ohai["network"]["default_inet6_interface"] = "eth1"
+ @ohai["network"]["interfaces"]["eth1"]["addresses"]["127.0.0.2"] = {
+ "scope" => "host",
+ "netmask" => "255.255.255.255",
+ "prefixlen" => "32",
+ "family" => "inet"
+ }
+ end
+
+ it_does_not_fail
+
+ it "displays debug messages" do
+ Ohai::Log.should_receive(:debug).with(/^Loading plugin network/).once
+ Ohai::Log.should_receive(:debug).with(/^link level default inet /).once
+ Ohai::Log.should_receive(:debug).with(/^link level default inet6 /).once
+ @ohai._require_plugin("network")
+ end
+
+ it "picks {ip,mac,ip6}address from the default interface" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.99.11"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:80"
+ @ohai["ip6address"].should == "3ffe:1111:3333::1"
+ end
+ end
+ end
+
+ describe "point to point address" do
+ before do
+ @ohai["network"]["interfaces"]["eth2"] = {
+ "flags" => ["POINTOPOINT", "BROADCAST", "MULTICAST", "UP"],
+ "number" => "2",
+ "addresses" => {
+ "fe80::216:3eff:fe2f:3681" => {
+ "scope" => "Link",
+ "prefixlen" => "64",
+ "family" => "inet6"
+ },
+ "00:16:3E:2F:36:81" => {"family" => "lladdr"},
+ "192.168.66.99" => {
+ "scope" => "Global",
+ "netmask" => "255.255.255.255",
+ "peer" => "192.168.99.126",
+ "prefixlen" => "32",
+ "family" => "inet"
+ },
+ "3ffe:1111:2222:0:4444::1" => {
+ "prefixlen" => "128",
+ "peer" => "3ffe:1111:2222:0:4444::2",
+ "family" => "inet6",
+ "scope" => "Global"
+ }
+ }
+ }
+ @ohai["network"]["default_gateway"] = "192.168.99.126"
+ @ohai["network"]["default_interface"] = "eth2"
+ @ohai["network"]["default_inet6_gateway"] = "3ffe:1111:2222:0:4444::2"
+ @ohai["network"]["default_inet6_interface"] = "eth2"
+ end
+
+ it_does_not_fail
+
+ it "picks {ip,mac,ip6}address from the default interface" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "192.168.66.99"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:81"
+ @ohai["ip6address"].should == "3ffe:1111:2222:0:4444::1"
+ end
+ end
+
+ describe "ipv6 only node" do
+ before do
+ @ohai["network"]["default_gateway"] = nil
+ @ohai["network"]["default_interface"] = nil
+ @ohai["network"]["interfaces"].each do |i,iv|
+ iv["addresses"].delete_if{|k,kv| kv["family"] == "inet" }
+ end
+ end
+
+ it_does_not_fail
+
+ it "can't detect ipaddress" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should be_nil
+ end
+
+ it "warns about not being able to set {ip,mac}address (ipv4)" do
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
+ @ohai._require_plugin("network")
+ end
+
+ it "sets {ip6,mac}address" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ip6address"].should == "3ffe:1111:2222::33"
+ @ohai["macaddress"].should == "00:16:3E:2F:36:79"
+ end
+
+ it "informs about macaddress being set using the ipv6 setup" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ Ohai::Log.should_receive(:info).with(/^macaddress set to 00:16:3E:2F:36:79 from the ipv6 setup/).once
+ @ohai._require_plugin("network")
+ end
+ end
+
+ end
+
+ basic_data.keys.sort.each do |os|
+ describe "the #{os}::network has already set some of the {ip,mac,ip6}address attributes" do
+ before(:each) do
+ @ohai = Ohai::System.new
+ @ohai.stub!(:require_plugin).twice.and_return(true)
+ @ohai["network"] = basic_data[os]["network"]
+ end
+
+ describe "{ip,mac}address are already set" do
+ before do
+ @ohai["ipaddress"] = "10.11.12.13"
+ @ohai["macaddress"] = "00:AA:BB:CC:DD:EE"
+ @expected_results = {
+ "linux" => {
+ "ip6address" => "3ffe:1111:2222::33"
+ },
+ "windows" => {
+ "ip6address" => "fe80::698d:3e37:7950:b28c"
+ }
+ }
+ end
+
+ it_does_not_fail
+
+ it "detects ip6address" do
+ @ohai._require_plugin("network")
+ @ohai["ip6address"].should == @expected_results[os]["ip6address"]
+ end
+
+ it "doesn't overwrite {ip,mac}address" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "10.11.12.13"
+ @ohai["macaddress"].should == "00:AA:BB:CC:DD:EE"
+ end
+ end
+
+ describe "ip6address is already set" do
+ describe "node has ipv4 and ipv6" do
+ before do
+ @ohai["ip6address"] = "3ffe:8888:9999::1"
+ @expected_results = {
+ "linux" => {
+ "ipaddress" => "192.168.66.33",
+ "macaddress" => "00:16:3E:2F:36:79"
+ },
+ "windows" => {
+ "ipaddress" => "172.19.0.130",
+ "macaddress" => "52:54:44:66:66:02"
+ }
+ }
+ end
+
+ it_does_not_fail
+
+ it "detects {ip,mac}address" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == @expected_results[os]["ipaddress"]
+ @ohai["macaddress"].should == @expected_results[os]["macaddress"]
+ end
+
+ it "doesn't overwrite ip6address" do
+ @ohai._require_plugin("network")
+ @ohai["ip6address"].should == "3ffe:8888:9999::1"
+ end
+ end
+
+ describe "ipv6 only node" do
+ before do
+ @ohai["network"]["default_gateway"] = nil
+ @ohai["network"]["default_interface"] = nil
+ @ohai["network"]["interfaces"].each do |i,iv|
+ iv["addresses"].delete_if{|k,kv| kv["family"] == "inet" }
+ end
+ @ohai["ip6address"] = "3ffe:8888:9999::1"
+ end
+
+ it_does_not_fail
+
+ it "can't detect ipaddress (ipv4)" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should be_nil
+ end
+
+ it "can't detect macaddress either" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["macaddress"].should be_nil
+ end
+
+ it "warns about not being able to set {ip,mac}address" do
+ Ohai::Log.should_receive(:warn).with(/^unable to detect ipaddress/).once
+ Ohai::Log.should_receive(:warn).with(/^unable to detect macaddress/).once
+ @ohai._require_plugin("network")
+ end
+
+ it "doesn't overwrite ip6address" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ip6address"].should == "3ffe:8888:9999::1"
+ end
+ end
+ end
+
+ describe "{mac,ip6}address are already set" do
+ describe "valid ipv4 setup" do
+ before do
+ @ohai["macaddress"] = "00:AA:BB:CC:DD:EE"
+ @ohai["ip6address"] = "3ffe:8888:9999::1"
+ @expected_results = {
+ "linux" => {
+ "ipaddress" => "192.168.66.33",
+ "macaddress" => "00:16:3E:2F:36:79"
+ },
+ "windows" => {
+ "ipaddress" => "172.19.0.130",
+ "macaddress" => "52:54:44:66:66:02"
+ }
+ }
+ end
+
+ it_does_not_fail
+
+ it "detects ipaddress and overwrite macaddress" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == @expected_results[os]["ipaddress"]
+ @ohai["macaddress"].should == @expected_results[os]["macaddress"]
+ end
+
+ it "doesn't overwrite ip6address" do
+ @ohai._require_plugin("network")
+ @ohai["ip6address"].should == "3ffe:8888:9999::1"
+ end
+ end
+
+ describe "ipv6 only node" do
+ before do
+ @ohai["network"]["default_gateway"] = nil
+ @ohai["network"]["default_interface"] = nil
+ @ohai["network"]["interfaces"].each do |i,iv|
+ iv["addresses"].delete_if{|k,kv| kv["family"] == "inet" }
+ end
+ @ohai["macaddress"] = "00:AA:BB:CC:DD:EE"
+ @ohai["ip6address"] = "3ffe:8888:9999::1"
+ end
+
+ it_does_not_fail
+
+ it "can't set ipaddress" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should be_nil
+ end
+
+ it "doesn't overwrite {ip6,mac}address" do
+ Ohai::Log.should_receive(:warn).any_number_of_times
+ @ohai._require_plugin("network")
+ @ohai["ip6address"].should == "3ffe:8888:9999::1"
+ @ohai["macaddress"].should == "00:AA:BB:CC:DD:EE"
+ end
+ end
+ end
+
+ describe "{ip,mac,ip6}address are already set" do
+ before do
+ @ohai["ipaddress"] = "10.11.12.13"
+ @ohai["macaddress"] = "00:AA:BB:CC:DD:EE"
+ @ohai["ip6address"] = "3ffe:8888:9999::1"
+ end
+
+ it_does_not_fail
+
+ it "doesn't overwrite {ip,mac,ip6}address" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "10.11.12.13"
+ @ohai["macaddress"].should == "00:AA:BB:CC:DD:EE"
+ @ohai["ip6address"].should == "3ffe:8888:9999::1"
+ end
+ end
+
+ describe "{ip,ip6}address are already set" do
+ before do
+ @ohai["ipaddress"] = "10.11.12.13"
+ @ohai["ip6address"] = "3ffe:8888:9999::1"
+ end
+
+ it_does_not_fail
+
+ it "doesn't overwrite {ip,mac,ip6}address" do
+ @ohai._require_plugin("network")
+ @ohai["ipaddress"].should == "10.11.12.13"
+ @ohai["macaddress"].should == nil
+ @ohai["ip6address"].should == "3ffe:8888:9999::1"
+ end
+ end
+
+ end
+ end
+ end
+end
diff --git a/spec/ohai/plugins/sigar/network_route_spec.rb b/spec/ohai/plugins/sigar/network_route_spec.rb
index bc2bb6ee..7005c70d 100644
--- a/spec/ohai/plugins/sigar/network_route_spec.rb
+++ b/spec/ohai/plugins/sigar/network_route_spec.rb
@@ -122,6 +122,7 @@ describe Ohai::System, "Sigar network route plugin" do
Sigar.should_receive(:new).at_least(2).times.and_return(@sigar)
@ohai.require_plugin("os")
@ohai[:os]="sigar"
+ Ohai::Log.should_receive(:warn).with(/unable to detect ip6address/).once
@ohai.require_plugin("network")
@ohai.require_plugin("sigar::network_route")
end