diff options
author | Bryan McLellan <btm@opscode.com> | 2013-06-20 15:17:21 -0700 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2013-06-20 15:17:21 -0700 |
commit | 7247abbb00dfccaf29ec50712852882d6b347557 (patch) | |
tree | bb5905b26cc8ce0a9166d4f8ba144f992d238010 | |
parent | b06446c805da606529a199597ddae2b49f880a10 (diff) | |
parent | 1cdf0bb8e3e99e09a39c004f18285051cdc9eb24 (diff) | |
download | ohai-7247abbb00dfccaf29ec50712852882d6b347557.tar.gz |
Merge branch 'OHAI-426'
-rw-r--r-- | lib/ohai/plugins/network.rb | 26 | ||||
-rw-r--r-- | spec/unit/plugins/network_spec.rb | 36 |
2 files changed, 41 insertions, 21 deletions
diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb index db71c566..cf572567 100644 --- a/lib/ohai/plugins/network.rb +++ b/lib/ohai/plugins/network.rb @@ -63,48 +63,50 @@ def sorted_ips(family = "inet") end def find_ip(family = "inet") - r=sorted_ips(family) + ips=sorted_ips(family) # return if there isn't any #{family} address ! - return [ nil, nil ] if r.empty? + return [ nil, nil ] if ips.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 + # populate the short-cut attributes ipaddress, ip6address and macaddress if network[int_attr] - # network[int_attr] exists, the choosen ip must be exist on this interface - r = r.select do |v| + # working with the address(es) of the default network interface + gw_if_ips = ips.select do |v| v[:iface] == network[int_attr] end - if r.empty? - Ohai::Log.warn("[#{family}] no ip on #{network[int_attr]}") + if gw_if_ips.empty? + Ohai::Log.warn("[#{family}] no ip address 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 + r = gw_if_ips.first else - r = r.select do |v| + # checking network masks + r = gw_if_ips.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]}") + r = gw_if_ips.first + Ohai::Log.warn("[#{family}] no ipaddress/mask on #{network[int_attr]} matching the gateway #{network[gw_attr]}, picking one anyway") 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 + r = gw_if_ips.first end else - r = r.first + r = ips.first Ohai::Log.debug("[#{family}] no default interface, picking the first ipaddress") end diff --git a/spec/unit/plugins/network_spec.rb b/spec/unit/plugins/network_spec.rb index eec0d70c..945bab81 100644 --- a/spec/unit/plugins/network_spec.rb +++ b/spec/unit/plugins/network_spec.rb @@ -240,20 +240,38 @@ describe Ohai::System, "Network Plugin" do it_does_not_fail - it "doesn't detect {ip,ip6,mac}address" do + it "picks {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 + @ohai["ipaddress"].should == "192.168.99.11" + @ohai["macaddress"].should == "00:16:3E:2F:36:80" + @ohai["ip6address"].should == "3ffe:1111:3333::1" 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 "there's a default gateway, none of the configured ip/mask theorically allows to reach it" do + before do + @ohai["network"]["default_gateway"] = "172.16.12.42" + @ohai["network"]["default_inet6_gateway"] = "3ffe:12:42::7070" + end + + it "picks {ip,ip6,mac}address" do + Ohai::Log.should_receive(:warn).any_number_of_times + @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 + + it "warns about this conflict" do + Ohai::Log.should_receive(:warn).with(/^\[inet\] no ipaddress\/mask on eth0/).once + Ohai::Log.should_receive(:warn).with(/^\[inet6\] no ipaddress\/mask on eth0/).once @ohai._require_plugin("network") end end @@ -276,9 +294,9 @@ describe Ohai::System, "Network Plugin" do 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(/^\[inet\] no ip address 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::Log.should_receive(:warn).with(/^\[inet6\] no ip address on eth0/).once @ohai._require_plugin("network") end end |