diff options
author | Laurent <laurent+git@u-picardie.fr> | 2012-05-23 14:55:18 +0200 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2012-06-08 08:42:52 -0700 |
commit | 69345962431bf2dd6d74da9b501a1b39b7635a14 (patch) | |
tree | ab02faba70a91764a8368ce021582569bdd3bc63 | |
parent | 2f87cd125c7844faf465e9696309eadc3d554397 (diff) | |
download | ohai-69345962431bf2dd6d74da9b501a1b39b7635a14.tar.gz |
lots of work on the tests, plugin fixed accordingly
i'm starting to love test driven development ! :)
-rw-r--r-- | lib/ohai/plugins/network.rb | 127 | ||||
-rw-r--r-- | spec/ohai/plugins/network_spec.rb | 599 |
2 files changed, 512 insertions, 214 deletions
diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb index 3da8fa9a..c46086a9 100644 --- a/lib/ohai/plugins/network.rb +++ b/lib/ohai/plugins/network.rb @@ -28,7 +28,12 @@ counters[:network] = Mash.new unless counters[:network] require_plugin "hostname" require_plugin "#{os}::network" -def find_ip_and_iface(family = "inet", match = 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 @@ -47,42 +52,80 @@ def find_ip_and_iface(family = "inet", match = nil) end end - # return if there isn't any #{family} address ! - return [ nil, nil ] if ipaddresses.empty? - # sort ip addresses by scope, by prefixlen and then by ip address # 128 - prefixlen: longest prefixes first - r = ipaddresses.sort_by do |v| + ipaddresses.sort_by do |v| [ ( scope_prio.index(v[:scope].downcase) or 999999 ), 128 - v[:ipaddress].prefix.to_i, ( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 ) ] end - if match.nil? or match ~ /^0\.0\.0\.0/ or match ~ /^::$/ - # return the first ip address - r = r.first - else - # use the match argument to select the address +end + +def find_ip(family = "inet") + r=sorted_ips(family) + + # return if there isn't any #{family} address ! + return [ nil, nil ] if r.empty? + + # 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[:ipaddress].include? IPAddress(match) - end.first + 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 + # return the first ip address on network[int_attr] + r = r.first + end + else + r = r.first + Ohai::Log.warn("[#{family}] no default interface, picking the first ipaddress") end - return [ nil, nil ] if r.nil? + return [ nil, nil ] if r.nil? or r.empty? + [ r[:ipaddress].to_s, r[:iface] ] end def find_mac_from_iface(iface) - network["interfaces"][iface]["addresses"].select{|k,v| v["family"]=="lladdr"}.first.first + 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, network_ip, network_opts) - if network_opts[:peer] - network_opts[:peer] == address_to_match +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 @@ -94,40 +137,30 @@ end results = {} -[ - { :name => "inet", - :prefix => "default" }, - { :name => "inet6", - :prefix => "default_inet6" } -].each do |f| +FAMILIES.keys.sort.each do |family| r = {} - # 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["#{f[:prefix]}_interface"] and - network["#{f[:prefix]}_gateway"] and - network["interfaces"][network["#{f[:prefix]}_interface"]] and - network["interfaces"][network["#{f[:prefix]}_interface"]]["addresses"] - Ohai::Log.debug("Using default #{f[:name]} interface '#{network["#{f[:prefix]}_interface"]}' and default #{f[:name]} gateway '#{network["#{f[:prefix]}_gateway"]}' to set the default #{f[:name]} ip") - ( r["ip"], r["iface"] ) = find_ip_and_iface(f[:name], network["#{f[:prefix]}_gateway"]) - else - ( r["ip"], r["iface"] ) = find_ip_and_iface(f[:name]) - end - Ohai::Log.warn("conflict when looking for the default #{f[:name]} ip: network[:#{f[:prefix]}_interface] is set to '#{network["#{f[:prefix]}_interface"]}' ipaddress '#{r["ip"]}' is set on '#{r["iface"]}'") if network["#{f[:prefix]}_interface"] and network["#{f[:prefix]}_interface"] != r["iface"] + ( r["ip"], r["iface"] ) = find_ip(family) r["mac"] = find_mac_from_iface(r["iface"]) unless r["iface"].nil? - unless r["ip"].nil? - # don't overwrite attributes if they've already been set by the "#{os}::network" plugin - if f[:name] == "inet" and ipaddress.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") + Ohai::Log.warn("unable to detect macaddress") + else ipaddress r["ip"] - # macaddress is always set from the ipv4 default_route + # ATM, macaddress is always set from the ipv4 d macaddress r["mac"] - elsif f[:name] == "inet6" and ip6address.nil? + end + elsif family == "inet6" and ip6address.nil? + if r["ip"].nil? + Ohai::Log.warn("unable to detect ip6address") + else ip6address r["ip"] end - #macaddress r["mac"] unless macaddress # macaddress set from ipv4 otherwise from ipv6 - end - results[f[:name]] = r + # but we might decide to change this behavior + #macaddress r["mac"] unless macaddress # macaddress set from ipv4 otherwise from ipv6 + results[family] = r end if results["inet"]["iface"] and results["inet6"]["iface"] and diff --git a/spec/ohai/plugins/network_spec.rb b/spec/ohai/plugins/network_spec.rb index e31e5a52..480e9a35 100644 --- a/spec/ohai/plugins/network_spec.rb +++ b/spec/ohai/plugins/network_spec.rb @@ -20,16 +20,17 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') def it_does_not_fail it "doesn't fail" do - Ohai::Log.should_not_receive(:debug).with(/Plugin network threw exception/) + 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 +end describe Ohai::System, "Network Plugin" do - + basic_network_data = { "linux" => { # pp Hash[node['network']] from shef to get the network data @@ -92,258 +93,522 @@ describe Ohai::System, "Network Plugin" do "default_inet6_interface" => "eth0" } } - + %w[ linux ].each do |os| describe "with #{os}" do before(:each) do @ohai = Ohai::System.new - @ohai.stub!(:require_plugin).and_return(true) + @ohai.stub!(:require_plugin).twice.and_return(true) @ohai["network"] = basic_network_data[os] end - - describe "when the #{os}::network plugin hasn't set {ip,ip6,mac}address" do + + describe "when the #{os}::network plugin hasn't set any of {ip,ip6,mac}address attributes" do context "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._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 - + context "default ipv4 and ipv6 gateway on different interfaces" 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,mac}address" do - @ohai._require_plugin("network") + @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:3333::1" end - + it "informs about this setup" do - Ohai::Log.should_receive(:info).with(/ipaddress and ip6address are set from different interfaces/) + Ohai::Log.should_receive(:info).with(/^ipaddress and ip6address are set from different interfaces/) + @ohai._require_plugin("network") + end + end + + describe "conflicting results from the #{os}::network plugin" do + context "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 + + context "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 + + context "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 + + context "several ipaddresses matching the default route" do + context "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 + + context "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 + + context "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 + + context "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 + + context "no default route" 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(:warn).with(/^\[inet\] no default interface/).once + Ohai::Log.should_receive(:warn).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 + + context "no default route, there are global and link level addresses" 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 {ip,mac,ip6}address with global scope" do + Ohai::Log.should_receive(:warn).with(/^\[inet\] no default interface/).once + Ohai::Log.should_receive(:warn).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 - - context "conflicting results" do + + context "link level default route" 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 "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" + + 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 "warns about this conflict" do - Ohai::Log.should_receive(:warn).with(/conflict when looking for the default/).exactly(2).times + + 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 - - context "several ipaddresses matching the default route, bigger prefix wins" do + + context "point to point address" do before do @ohai["network"]["interfaces"]["eth2"] = { - "flags" =>["BROADCAST", "MULTICAST", "UP"], + "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.127", - "broadcast"=>"192.168.99.127", - "prefixlen"=>"25", + "netmask"=>"255.255.255.255", + "peer"=>"192.168.99.126", + "prefixlen"=>"32", "family"=>"inet"}, "3ffe:1111:2222:0:4444::1"=> { - "prefixlen"=> "64", + "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 "detects {ip,ip6,mac}address" do - @ohai._require_plugin("network") + + 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 - context "several ipaddresses matching the default route, smallest ip wins" do + context "ipv6 only node" 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" - } - } - } + @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 "detects {ip,ip6,mac}address" do - @ohai._require_plugin("network") - @ohai["ipaddress"].should == "192.168.66.32" - @ohai["macaddress"].should == "00:16:3E:2F:36:81" - @ohai["ip6address"].should == "3ffe:1111:2222::32" + + it "can't detect {ip,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 + end + + # ATM IT WON'T SET MACADDRESS + it "warns about not being able to set ipv4 related attributes" 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 ip6address" do + Ohai::Log.should_receive(:warn).any_number_of_times + @ohai._require_plugin("network") + @ohai["ip6address"].should == "3ffe:1111:2222::33" end end - context "no default route" do + end + + describe "the #{os}::network has already set some of the {ip,mac,ip6}address attributes" do + describe "{ip,mac}address are already set" 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"]} + @ohai["ipaddress"] = "10.11.12.13" + @ohai["macaddress"] = "00:AA:BB:CC:DD:EE" end it_does_not_fail - it "picks {ip,mac,ip6}address from the first interface" do + it "detects ip6address" 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" + @ohai["ip6address"].should == "3ffe:1111:2222::33" end - end - context "link level default route" do - pending + 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 - context "link level addresses" do - pending - end + describe "ip6address is already set" do + context "node has ipv4 and ipv6" do + before do + @ohai["ip6address"] = "3ffe:8888:9999::1" + end - end - - describe "when the #{os}::network plugin sets {ip,mac}address" do - before do - @ohai["ipaddress"] = "10.11.12.13" - @ohai["macaddress"] = "00:AA:BB:CC:DD:EE" - end + it_does_not_fail - it_does_not_fail - - it "detects ip6address" do - @ohai._require_plugin("network") - @ohai["ip6address"].should == "3ffe:1111:2222::33" - 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 + it "detects {ip,mac}address" do + @ohai._require_plugin("network") + @ohai["ipaddress"].should == "192.168.66.33" + @ohai["macaddress"].should == "00:16:3E:2F:36:79" + end - describe "when the #{os}::network plugin sets ip6address" do - before do - @ohai["ip6address"] = "3ffe:8888:9999::1" - end + it "doesn't overwrite ip6address" do + @ohai._require_plugin("network") + @ohai["ip6address"].should == "3ffe:8888:9999::1" + end + end - it_does_not_fail - - it "detects {ip,mac}address" do - @ohai._require_plugin("network") - @ohai["ipaddress"].should == "192.168.66.33" - @ohai["macaddress"].should == "00:16:3E:2F:36:79" - end - - it "doesn't overwrite ip6address" do - @ohai._require_plugin("network") - @ohai["ip6address"].should == "3ffe:8888:9999::1" - end - end - - describe "when the #{os}::network plugin sets {mac,ip6}address" do - before do - @ohai["macaddress"] = "00:AA:BB:CC:DD:EE" - @ohai["ip6address"] = "3ffe:8888:9999::1" - end + context "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 "detects {ip,mac}address" do - @ohai._require_plugin("network") - @ohai["ipaddress"].should == "192.168.66.33" - @ohai["macaddress"].should == "00:16:3E:2F:36:79" - end - - it "doesn't overwrite ip6address" do - @ohai._require_plugin("network") - @ohai["ip6address"].should == "3ffe:8888:9999::1" - end - end - - describe "when the #{os}::network plugin sets {ip,mac,ip6}address" do - before do - @ohai["ipaddress"] = "10.11.12.13" - @ohai["macaddress"] = "00:AA:BB:CC:DD:EE" - @ohai["ip6address"] = "3ffe:8888:9999::1" + it_does_not_fail + + it "can't detect {ip,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 + end + + # ATM IT WON'T SET MACADDRESS + it "warns about not being able to set ipv4 related attributes" 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 - 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" + describe "{mac,ip6}address are already set" do + before do + @ohai["macaddress"] = "00:AA:BB:CC:DD:EE" + @ohai["ip6address"] = "3ffe:8888:9999::1" + end + + it_does_not_fail + + it "detects {ip,mac}address" do + @ohai._require_plugin("network") + @ohai["ipaddress"].should == "192.168.66.33" + @ohai["macaddress"].should == "00:16:3E:2F:36:79" + end + + it "doesn't overwrite ip6address" do + @ohai._require_plugin("network") + @ohai["ip6address"].should == "3ffe:8888:9999::1" + end end - end - describe "when the #{os}::network plugin sets {ip,ip6}address" do - before do - @ohai["ipaddress"] = "10.11.12.13" - @ohai["ip6address"] = "3ffe:8888:9999::1" + 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 - - 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" + + 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 end |