From 5963d98dfdd10b197d2a240490dbfc1b7df66ddb Mon Sep 17 00:00:00 2001 From: Isa Farnik Date: Wed, 9 Sep 2015 15:06:40 +0100 Subject: properly detect wpar networking make tests happy --- .gitignore | 3 +- lib/ohai/plugins/aix/network.rb | 132 +++++++++++++++++----------------- spec/unit/plugins/aix/network_spec.rb | 63 ++++++++++------ 3 files changed, 113 insertions(+), 85 deletions(-) diff --git a/.gitignore b/.gitignore index 1bf14bce..f46c0cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ tmp/ coverage/ .DS_Store .bundle -*~ \ No newline at end of file +*~ +vendor diff --git a/lib/ohai/plugins/aix/network.rb b/lib/ohai/plugins/aix/network.rb index eeaf1aa5..bd4214ff 100644 --- a/lib/ohai/plugins/aix/network.rb +++ b/lib/ohai/plugins/aix/network.rb @@ -1,7 +1,8 @@ # # Author:: Kaustubh Deorukhkar () # Author:: Prabhu Das () -# Copyright:: Copyright (c) 2013 Opscode, Inc. +# Author:: Isa Farnik () +# Copyright:: Copyright (c) 2015 Chef, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -45,76 +46,78 @@ Ohai.plugin(:Network) do network Mash.new unless network network[:interfaces] = Mash.new unless network[:interfaces] - # :default_interface, :default_gateway - route -n get 0 - so = shell_out("netstat -rn |grep default") - so.stdout.lines.each do |line| - items = line.split(' ') - if items[0] == "default" - network[:default_gateway] = items[1] - network[:default_interface] = items[5] + # We unfortunately have to do things a bit different here, if ohai is running + # within a WPAR. For instance, the WPAR isn't aware of some of its own networking + # minutia such as default gateway/route. + unless shell_out("uname -W").stdout.to_i > 0 + # :default_interface, :default_gateway - route -n get 0 + so = shell_out("netstat -rn |grep default") + so.stdout.lines.each do |line| + items = line.split(' ') + if items[0] == "default" + network[:default_gateway] = items[1] + network[:default_interface] = items[5] + end end end - # List the interfaces in system. - so = shell_out("lsdev -Cc if | grep Available") - so.stdout.lines.each do |line| - if line =~ /(\S+) (\S+)\s+(.+)/ - interface = $1 - iface[interface] = Mash.new unless iface[interface] - iface[interface][:state] = ($2 == 'Available' ? 'up' : 'down') - iface[interface][:description] = $3 - - # Query the interface information - if_so = shell_out("ifconfig #{interface}") - if_so.stdout.lines.each do |line| - case line - when /^#{interface}:\sflags=\S+<(\S+)>/ - iface[interface][:flags] = $1.split(',') - iface[interface][:metric] = $1 if line =~ /metric\s(\S+)/ - else - # We have key value pairs. - if line =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/ - tmp_addr, tmp_prefix = $1, $3 - if tmp_prefix.nil? - netmask = hex_to_dec_netmask($1) if line =~ /netmask\s(\S+)\s/ - unless netmask - tmp_prefix ||= "32" - netmask = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s - end - else + # Splits the ifconfig output to 1 line per interface + if_so = shell_out("ifconfig -a") + if_so.stdout.gsub(/\n(\w+\d+)/, '___\1').split("___").each do |intraface| + splat = intraface.split(":") + interface = splat[0] + line = splat[1..-1][0] + iface[interface] = Mash.new + iface[interface][:state] = (line.include?("/ + iface[interface][:flags] = $1.split(',') + iface[interface][:metric] = $1 if lin =~ /metric\s(\S+)/ + else + # We have key value pairs. + if lin =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/ + tmp_addr, tmp_prefix = $1, $3 + if tmp_prefix.nil? + netmask = hex_to_dec_netmask($1) if lin =~ /netmask\s(\S+)\s/ + unless netmask + tmp_prefix ||= "32" netmask = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s end - - iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] - iface[interface][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix } - iface[interface][:addresses][tmp_addr][:netmask] = netmask - - if line =~ /broadcast\s(\S+)\s/ - iface[interface][:addresses][tmp_addr][:broadcast] = $1 - end - elsif line =~ /inet6 ([a-f0-9\:]+)%?([\d]*)\/?(\d*)/ - # TODO do we have more properties on inet6 in aix? broadcast - iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] - iface[interface][:addresses][$1] = { "family" => "inet6", "zone_index" => $2, "prefixlen" => $3 } else - # load all key-values, example "tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1" - properties = line.split - n = properties.length/2 - 1 - (0..n).each do |i| - iface[interface][properties[i*2]] = properties[(i*2+1)] - end + netmask = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s end - end - end #ifconfig stdout - # Query macaddress - e_so = shell_out("entstat -d #{interface} | grep \"Hardware Address\"") - iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] - e_so.stdout.lines.each do |line| - iface[interface][:addresses][$1.upcase] = { "family" => "lladdr" } if line =~ /Hardware Address: (\S+)/ + iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + iface[interface][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix } + iface[interface][:addresses][tmp_addr][:netmask] = netmask + + if lin =~ /broadcast\s(\S+)\s/ + iface[interface][:addresses][tmp_addr][:broadcast] = $1 + end + elsif lin =~ /inet6 ([a-f0-9\:]+)%?([\d]*)\/?(\d*)?/ + # TODO do we have more properties on inet6 in aix? broadcast + iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + iface[interface][:addresses][$1] = { "family" => "inet6", "zone_index" => $2, "prefixlen" => $3 } + else + # load all key-values, example "tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1" + properties = lin.split + n = properties.length/2 - 1 + (0..n).each do |i| + iface[interface][properties[i*2]] = properties[(i*2+1)] + end + end end - end #lsdev stdout - end + end + + # Query macaddress + e_so = shell_out("entstat -d #{interface} | grep \"Hardware Address\"") + iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + e_so.stdout.lines.each do |line| + iface[interface][:addresses][$1.upcase] = { "family" => "lladdr" } if line =~ /Hardware Address: (\S+)/ + end + end #ifconfig stdout # Query routes information %w{inet inet6}.each do |family| @@ -124,7 +127,7 @@ Ohai.plugin(:Network) do interface = $6 iface[interface][:routes] = Array.new unless iface[interface][:routes] iface[interface][:routes] << Mash.new( :destination => $1, :family => family, - :via => $2, :flags => $3) + :via => $2, :flags => $3) end end end @@ -142,7 +145,8 @@ Ohai.plugin(:Network) do count += 1 end end - + network["interfaces"] = iface end end + diff --git a/spec/unit/plugins/aix/network_spec.rb b/spec/unit/plugins/aix/network_spec.rb index a8be9b8d..3b66465b 100644 --- a/spec/unit/plugins/aix/network_spec.rb +++ b/spec/unit/plugins/aix/network_spec.rb @@ -24,18 +24,21 @@ describe Ohai::System, "AIX network plugin" do default 172.31.8.1 UG 2 121789 en0 - - NETSTAT_RN_GREP_DEFAULT - @lsdev_Cc_if = <<-LSDEV_CC_IF -en0 Available Standard Ethernet Network Interface -LSDEV_CC_IF - - @ifconfig_en0 = <<-IFCONFIG_EN0 + @ifconfig = <<-IFCONFIG en0: flags=1e080863,480 metric 1 inet 172.29.174.58 netmask 0xffffc000 broadcast 172.29.191.255 inet 172.29.174.59 broadcast 172.29.191.255 inet 172.29.174.60 netmask 0xffffc000 broadcast 172.29.191.255 inet6 ::1%1/0 tcp_sendspace 262144 tcp_recvspace 262144 rfc1323 1 -IFCONFIG_EN0 +en1: flags=1e084863,480 + inet 172.31.10.211 netmask 0xfffffc00 broadcast 172.31.11.255 + tcp_sendspace 262144 tcp_recvspace 262144 rfc1323 1 +lo0: flags=e08084b,c0 + inet 127.0.0.1 netmask 0xff000000 broadcast 127.255.255.255 + inet6 ::1%1/0 + tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1 +IFCONFIG @netstat_nrf_inet = <<-NETSTAT_NRF_INET Destination Gateway Flags Refs Use If Exp Groups @@ -46,6 +49,13 @@ default 172.29.128.13 UG 0 587683 en0 - - 172.29.191.255 172.29.174.58 UHSb 0 1 en0 - - NETSTAT_NRF_INET + @entstat_err = <<-ENSTAT_ERR + + +entstat: 0909-002 Unable to open device en0, errno = 13 +grep: 0652-033 Cannot open Address". +ENSTAT_ERR + @aix_arp_an = <<-ARP_AN ? (172.29.131.16) at 6e:87:70:0:40:3 [ethernet] stored in bucket 16 @@ -66,10 +76,12 @@ ARP_AN @plugin = get_plugin("aix/network") allow(@plugin).to receive(:collect_os).and_return(:aix) @plugin[:network] = Mash.new + allow(@plugin).to receive(:shell_out).with("uname -W").and_return(mock_shell_out(0, "0", nil)) allow(@plugin).to receive(:shell_out).with("netstat -rn |grep default").and_return(mock_shell_out(0, @netstat_rn_grep_default, nil)) - allow(@plugin).to receive(:shell_out).with("lsdev -Cc if | grep Available").and_return(mock_shell_out(0, @lsdev_Cc_if, nil)) - allow(@plugin).to receive(:shell_out).with("ifconfig en0").and_return(mock_shell_out(0, @ifconfig_en0, nil)) + allow(@plugin).to receive(:shell_out).with("ifconfig -a").and_return(mock_shell_out(0, @ifconfig, nil)) allow(@plugin).to receive(:shell_out).with("entstat -d en0 | grep \"Hardware Address\"").and_return(mock_shell_out(0, "Hardware Address: be:42:80:00:b0:05", nil)) + allow(@plugin).to receive(:shell_out).with("entstat -d en1 | grep \"Hardware Address\"").and_return(mock_shell_out(0, @entstat_err, nil)) + allow(@plugin).to receive(:shell_out).with("entstat -d lo0 | grep \"Hardware Address\"").and_return(mock_shell_out(0, @entstat_err, nil)) allow(@plugin).to receive(:shell_out).with("netstat -nrf inet").and_return(mock_shell_out(0, @netstat_nrf_inet, nil)) allow(@plugin).to receive(:shell_out).with("netstat -nrf inet6").and_return(mock_shell_out(0, "::1%1 ::1%1 UH 1 109392 en0 - -", nil)) allow(@plugin).to receive(:shell_out).with("arp -an").and_return(mock_shell_out(0, @aix_arp_an, nil)) @@ -85,7 +97,7 @@ ARP_AN end it "detects the interfaces" do - expect(@plugin['network']['interfaces'].keys.sort).to eq(["en0"]) + expect(@plugin['network']['interfaces'].keys.sort).to eq(["en0", "en1", "lo0"]) end it "detects the ip addresses of the interfaces" do @@ -93,17 +105,34 @@ ARP_AN end end - describe "netstat -rn |grep default" do + describe "when running on an LPAR" do + describe "netstat -rn |grep default" do + before do + @plugin.run + end + + it "returns the default gateway of the system's network" do + expect(@plugin[:network][:default_gateway]).to eq('172.31.8.1') + end + + it "returns the default interface of the system's network" do + expect(@plugin[:network][:default_interface]).to eq('en0') + end + end + end + + describe "when running on a WPAR" do before do + allow(@plugin).to receive(:shell_out).with("uname -W").and_return(mock_shell_out(0, "6", nil)) @plugin.run end - it "returns the default gateway of the system's network" do - expect(@plugin[:network][:default_gateway]).to eq('172.31.8.1') + it "avoids collecting routing information" do + expect(@plugin[:network][:default_gateway]).to be_nil end - it "returns the default interface of the system's network" do - expect(@plugin[:network][:default_interface]).to eq('en0') + it "avoids collecting default interface" do + expect(@plugin[:network][:default_gateway]).to be_nil end end @@ -113,11 +142,6 @@ ARP_AN expect(@plugin['network']['interfaces']['en0'][:state]).to eq("up") end - it "detects the description of the interfaces in the system" do - @plugin.run - expect(@plugin['network']['interfaces']['en0'][:description]).to eq("Standard Ethernet Network Interface") - end - describe "ifconfig interface" do it "detects the CHAIN network flag" do @plugin.run @@ -173,7 +197,6 @@ ARP_AN context "inet6 entries" do before do - allow(@plugin).to receive(:shell_out).with("ifconfig en0").and_return(mock_shell_out(0, "inet6 ::1%1/0", nil)) @plugin.run @inet_entry = @plugin['network']['interfaces']['en0'][:addresses]["::1"] end -- cgit v1.2.1