diff options
author | Tim Smith <tsmith@chef.io> | 2019-11-22 19:38:44 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-22 19:38:44 -0800 |
commit | 48c85ea6c84bb51d20be8ed0d67c1353fad16f28 (patch) | |
tree | fe75236dc7e9fc39fae67fb520848817e5d878c6 | |
parent | 4c24064f3531f9cee85ee61757cc3bd753fc272d (diff) | |
parent | 6c6de0511039f1c64e83802e98de4eae3550ebad (diff) | |
download | ohai-48c85ea6c84bb51d20be8ed0d67c1353fad16f28.tar.gz |
Merge pull request #1412 from AmazeCom/fixes-for-ruby27
Fix failures under Ruby 2.7
-rw-r--r-- | lib/ohai/mixin/network_helper.rb (renamed from lib/ohai/mixin/network_constants.rb) | 9 | ||||
-rw-r--r-- | lib/ohai/plugins/aix/network.rb | 11 | ||||
-rw-r--r-- | lib/ohai/plugins/darwin/network.rb | 10 | ||||
-rw-r--r-- | lib/ohai/plugins/network.rb | 10 | ||||
-rw-r--r-- | lib/ohai/plugins/solaris2/network.rb | 10 | ||||
-rw-r--r-- | spec/unit/mixin/network_helper_spec.rb | 30 | ||||
-rw-r--r-- | spec/unit/plugins/aix/network_spec.rb | 10 |
7 files changed, 58 insertions, 32 deletions
diff --git a/lib/ohai/mixin/network_constants.rb b/lib/ohai/mixin/network_helper.rb index a10d78e4..be783722 100644 --- a/lib/ohai/mixin/network_constants.rb +++ b/lib/ohai/mixin/network_helper.rb @@ -19,11 +19,18 @@ module Ohai module Mixin - module NetworkConstants + module NetworkHelper FAMILIES = { "inet" => "default", "inet6" => "default_inet6", }.freeze + + def hex_to_dec_netmask(netmask) + # example 'ffff0000' -> '255.255.0.0' + dec = netmask[0..1].to_i(16).to_s(10) + [2, 4, 6].each { |n| dec = dec + "." + netmask[n..n + 1].to_i(16).to_s(10) } + dec + end end end end diff --git a/lib/ohai/plugins/aix/network.rb b/lib/ohai/plugins/aix/network.rb index b7915d7e..c0d5fb77 100644 --- a/lib/ohai/plugins/aix/network.rb +++ b/lib/ohai/plugins/aix/network.rb @@ -20,16 +20,11 @@ Ohai.plugin(:Network) do require "ipaddr" + require_relative "../../mixin/network_helper" provides "network", "counters/network", "macaddress" - # Helpers - def hex_to_dec_netmask(netmask) - # example '0xffff0000' -> '255.255.0.0' - dec = netmask[2..3].to_i(16).to_s(10) - [4, 6, 8].each { |n| dec = dec + "." + netmask[n..n + 1].to_i(16).to_s(10) } - dec - end + include Ohai::Mixin::NetworkHelper collect_data(:aix) do # Loads following information. @@ -80,7 +75,7 @@ Ohai.plugin(:Network) do if lin =~ %r{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/ + netmask = hex_to_dec_netmask($1) if lin =~ /netmask\s0x(\S+)\s/ unless netmask tmp_prefix ||= "32" netmask = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s diff --git a/lib/ohai/plugins/darwin/network.rb b/lib/ohai/plugins/darwin/network.rb index 24c6f22d..9736987d 100644 --- a/lib/ohai/plugins/darwin/network.rb +++ b/lib/ohai/plugins/darwin/network.rb @@ -17,9 +17,13 @@ # Ohai.plugin(:Network) do + require_relative "../../mixin/network_helper" + provides "network", "network/interfaces" provides "counters/network", "counters/network/interfaces" + include Ohai::Mixin::NetworkHelper + def parse_media(media_string) media = {} line_array = media_string.split(" ") @@ -87,8 +91,6 @@ Ohai.plugin(:Network) do end collect_data(:darwin) do - require "scanf" - network Mash.new unless network network[:interfaces] ||= Mash.new counters Mash.new unless counters @@ -138,11 +140,11 @@ Ohai.plugin(:Network) do end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask 0x(([0-9a-f]){1,8})\s*$/ iface[cint][:addresses] ||= Mash.new - iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * "." } + iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => hex_to_dec_netmask($2) } end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask 0x(([0-9a-f]){1,8}) broadcast (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ iface[cint][:addresses] ||= Mash.new - iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * ".", "broadcast" => $4 } + iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => hex_to_dec_netmask($2) , "broadcast" => $4 } end if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*) prefixlen (\d+)\s*/ iface[cint][:addresses] ||= Mash.new diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb index af50d6d7..03003eb2 100644 --- a/lib/ohai/plugins/network.rb +++ b/lib/ohai/plugins/network.rb @@ -18,8 +18,8 @@ Ohai.plugin(:NetworkAddresses) do require "ipaddress" - require_relative "../mixin/network_constants" - include Ohai::Mixin::NetworkConstants + require_relative "../mixin/network_helper" + include Ohai::Mixin::NetworkHelper provides "ipaddress", "ip6address", "macaddress" @@ -69,8 +69,8 @@ Ohai.plugin(:NetworkAddresses) do return [ nil, nil ] if ips.empty? # shortcuts to access default #{family} interface and gateway - int_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] + "_interface" - gw_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] + "_gateway" + int_attr = Ohai::Mixin::NetworkHelper::FAMILIES[family] + "_interface" + gw_attr = Ohai::Mixin::NetworkHelper::FAMILIES[family] + "_gateway" if network[int_attr] # working with the address(es) of the default network interface @@ -142,7 +142,7 @@ Ohai.plugin(:NetworkAddresses) do counters[:network] ||= Mash.new # inet family is processed before inet6 to give ipv4 precedence - Ohai::Mixin::NetworkConstants::FAMILIES.keys.sort.each do |family| + Ohai::Mixin::NetworkHelper::FAMILIES.keys.sort.each do |family| r = {} # find the ip/interface with the default route for this family (r["ip"], r["iface"]) = find_ip(family) diff --git a/lib/ohai/plugins/solaris2/network.rb b/lib/ohai/plugins/solaris2/network.rb index 5e890f2b..aa60b21e 100644 --- a/lib/ohai/plugins/solaris2/network.rb +++ b/lib/ohai/plugins/solaris2/network.rb @@ -63,9 +63,13 @@ unless defined?(ETHERNET_ENCAPS) end Ohai.plugin(:Network) do + require_relative "../../mixin/network_helper" + provides "network", "network/interfaces" provides "counters/network", "counters/network/interfaces" + include Ohai::Mixin::NetworkHelper + def solaris_encaps_lookup(ifname) return "Ethernet" if ETHERNET_ENCAPS.include?(ifname) return "Ethernet" if ifname.eql?("net") @@ -92,8 +96,6 @@ Ohai.plugin(:Network) do end collect_data(:solaris2) do - require "scanf" - iface = Mash.new network Mash.new unless network network[:interfaces] ||= Mash.new @@ -124,11 +126,11 @@ Ohai.plugin(:Network) do end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8})\s*$/ iface[cint][:addresses] ||= Mash.new - iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * "." } + iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => hex_to_dec_netmask($2) } end if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8}) broadcast (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ iface[cint][:addresses] ||= Mash.new - iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf("%2x" * 4) * ".", "broadcast" => $4 } + iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => hex_to_dec_netmask($2) , "broadcast" => $4 } end if line =~ %r{\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*)/(\d+)\s*$} iface[cint][:addresses] ||= Mash.new diff --git a/spec/unit/mixin/network_helper_spec.rb b/spec/unit/mixin/network_helper_spec.rb new file mode 100644 index 00000000..ec0b2472 --- /dev/null +++ b/spec/unit/mixin/network_helper_spec.rb @@ -0,0 +1,30 @@ +# +# Author:: Kris Shannon <k.shannon@amaze.com.au> +# Copyright:: Copyright (c) 2019 Amaze Communication. +# 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 "spec_helper" +require "ohai/mixin/network_helper" + +describe Ohai::Mixin::NetworkHelper, "Network Helper Mixin" do + let(:mixin) { Object.new.extend(Ohai::Mixin::NetworkHelper) } + + describe "hex_to_dec_netmask method" do + it "converts a netmask from hexadecimal form to decimal form" do + expect(mixin.hex_to_dec_netmask("ffff0000")).to eq("255.255.0.0") + end + end +end diff --git a/spec/unit/plugins/aix/network_spec.rb b/spec/unit/plugins/aix/network_spec.rb index a290e48a..e96db800 100644 --- a/spec/unit/plugins/aix/network_spec.rb +++ b/spec/unit/plugins/aix/network_spec.rb @@ -300,14 +300,4 @@ describe Ohai::System, "AIX network plugin" do expect(@plugin["network"]["arp"][0][:remote_mac]).to eq("6e:87:70:0:40:3") end end - - describe "hex_to_dec_netmask method" do - before do - @plugin.run - end - - it "converts a netmask from hexadecimal form to decimal form" do - expect(@plugin.hex_to_dec_netmask("0xffff0000")).to eq("255.255.0.0") - end - end end |