diff options
author | Laurent <laurent+git@u-picardie.fr> | 2012-05-23 18:37:51 +0200 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2012-06-08 08:42:52 -0700 |
commit | 428699f275ca5aaa01ea8e709daa4b999b5909ce (patch) | |
tree | 42f18d3d363e615ddd9d04fae073cd7d30c2ffcf | |
parent | 70e5abab0ee6d54e1a66beae4eb8d92649c8e4a7 (diff) | |
download | ohai-428699f275ca5aaa01ea8e709daa4b999b5909ce.tar.gz |
2 mores test + a fix
one of the test illustrates the bad ipaddress selection as stated in http://tickets.opscode.com/browse/OHAI-343?focusedCommentId=23032&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-23032
-rw-r--r-- | lib/ohai/plugins/network.rb | 4 | ||||
-rw-r--r-- | spec/ohai/plugins/network_spec.rb | 143 |
2 files changed, 90 insertions, 57 deletions
diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb index 5a18e930..7c7f91be 100644 --- a/lib/ohai/plugins/network.rb +++ b/lib/ohai/plugins/network.rb @@ -46,7 +46,7 @@ def sorted_ips(family = "inet") 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"], + :scope => addr_v["scope"].nil? ? nil : addr_v["scope"].downcase, :iface => iface } end @@ -55,7 +55,7 @@ def sorted_ips(family = "inet") # 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].downcase) or 999999 ), + [ ( scope_prio.index(v[:scope]) or 999999 ), 128 - v[:ipaddress].prefix.to_i, ( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 ) ] diff --git a/spec/ohai/plugins/network_spec.rb b/spec/ohai/plugins/network_spec.rb index bdb50bf1..a84ad425 100644 --- a/spec/ohai/plugins/network_spec.rb +++ b/spec/ohai/plugins/network_spec.rb @@ -412,71 +412,106 @@ describe Ohai::System, "Network Plugin" do end describe "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 + 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_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" + 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 - end - describe "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 + 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_does_not_fail - it "prefers global scope addressses to set {ip,mac,ip6}address" 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" + it "prefers global scope addressses to set {ip,mac,ip6}address" 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 end describe "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 + 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_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") + 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 - 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" + 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 @@ -491,7 +526,7 @@ describe Ohai::System, "Network Plugin" do "prefixlen" => "64", "family" => "inet6" }, - "00:16:3E:2F:36:81" =>{"family" => "lladdr"}, + "00:16:3E:2F:36:81" => {"family" => "lladdr"}, "192.168.66.99" => { "scope" => "Global", "netmask" => "255.255.255.255", @@ -558,12 +593,11 @@ describe Ohai::System, "Network Plugin" do 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_network_data.keys.sort.each do |os| - 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 @@ -728,6 +762,5 @@ describe Ohai::System, "Network Plugin" do end end - end end |