diff options
author | Tim Smith <tsmith@chef.io> | 2020-05-12 09:55:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-12 09:55:46 -0700 |
commit | 299923310befbcc0e68e3fbea95e39955919497d (patch) | |
tree | 662c1bcad2ae90f756ad3dbfebcbb4b64a59e4d7 | |
parent | 1d3b0b74a8a7c5244878c8b8f46946a1d2205641 (diff) | |
parent | 154c5c32cae6967693df5db4de944179300374aa (diff) | |
download | ohai-299923310befbcc0e68e3fbea95e39955919497d.tar.gz |
Merge pull request #1462 from chef/lcg/chef-utils-mash
Depend on chef-utils gem so we can use ChefUtils::Mash
-rw-r--r-- | lib/ohai/mash.rb | 222 | ||||
-rw-r--r-- | lib/ohai/plugins/cpu.rb | 2 | ||||
-rw-r--r-- | lib/ohai/plugins/virtualbox.rb | 4 | ||||
-rw-r--r-- | ohai.gemspec | 1 | ||||
-rw-r--r-- | spec/unit/plugins/abort_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/plugins/fail_spec.rb | 12 | ||||
-rw-r--r-- | spec/unit/plugins/linux/cpu_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/plugins/linux/network_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/plugins/solaris2/hostname_spec.rb | 2 |
9 files changed, 37 insertions, 216 deletions
diff --git a/lib/ohai/mash.rb b/lib/ohai/mash.rb index d5a3b4b1..d386af25 100644 --- a/lib/ohai/mash.rb +++ b/lib/ohai/mash.rb @@ -1,201 +1,21 @@ -# Copyright (c) 2009 Dan Kubb - -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: - -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -# --- -# --- - -# Some portions of blank.rb and mash.rb are verbatim copies of software -# licensed under the MIT license. That license is included below: - -# Copyright (c) 2005-2008 David Heinemeier Hansson - -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: - -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -# This class has dubious semantics and we only have it so that people can write -# params[:key] instead of params['key']. -class Mash < Hash - - # @param constructor [Object] The default value for the mash. - # If constructor is a Hash, a new mash will be created based on the keys of the hash and - # no default value will be set. - def initialize(constructor = {}) - if constructor.is_a?(Hash) - super() - update(constructor) - else - super(constructor) - end - end - - # @param key [Object] The default value for the mash. - # If key is a Symbol and it is a key in the mash, then the default value will be set to - # the value matching the key. - def default(key = nil) - if key.is_a?(Symbol) && include?(key = key.to_s) - self[key] - else - super - end - end - - alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) - alias_method :regular_update, :update unless method_defined?(:regular_update) - - # @param key [Object] The key to set. - # @param value [Object] The value to set the key to. - # - # @see Mash#convert_key - # @see Mash#convert_value - def []=(key, value) - regular_writer(convert_key(key), convert_value(value)) - end - - # @param other_hash [Hash] - # A hash to update values in the mash with. The keys and the values will be - # converted to Mash format. - # - # @return [Mash] The updated mash. - def update(other_hash) - other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } - self - end - - alias_method :merge!, :update - - # @param key [Object] The key to check for. This will be run through convert_key. - # - # @return [Boolean] True if the key exists in the mash. - def key?(key) - super(convert_key(key)) - end - - # def include? def has_key? def member? - alias_method :include?, :key? - alias_method :has_key?, :key? - alias_method :member?, :key? - - # @param key [Object] The key to fetch. This will be run through convert_key. - # @param extras [Array] Default value. - # - # @return [Object] The value at key or the default value. - def fetch(key, *extras) - super(convert_key(key), *extras) - end - - # @param indices [Array] The keys to retrieve values for. These will be run through +convert_key+. - # - # @return [Array] The values at each of the provided keys - def values_at(*indices) - indices.collect { |key| self[convert_key(key)] } - end - - # @param hash [Hash] The hash to merge with the mash. - # - # @return [Mash] A new mash with the hash values merged in. - def merge(hash) - dup.update(hash) - end - - # @param key [Object] The key to delete from the mash. - def delete(key) - super(convert_key(key)) - end - - # @param keys [Array<String, Symbol>] The mash keys to exclude. - # - # @return [Mash] A new mash without the selected keys. - # - # @example - # { :one => 1, :two => 2, :three => 3 }.except(:one) - # #=> { "two" => 2, "three" => 3 } - def except(*keys) - super(*keys.map { |k| convert_key(k) }) - end - - # Used to provide the same interface as Hash. - # - # @return [Mash] This mash unchanged. - def stringify_keys!; self end - - # @return [Hash] The mash as a Hash with symbolized keys. - def symbolize_keys - h = Hash.new(default) - each { |key, val| h[key.to_sym] = val } - h - end - - # @return [Hash] The mash as a Hash with string keys. - def to_hash - Hash.new(default).merge(self) - end - - # Convert a Hash into a Mash. The input Hash's default value is maintained - # @return [Mash] - def self.from_hash(hash) - mash = Mash.new(hash) - mash.default = hash.default - mash - end - - protected - - # @param key [Object] The key to convert. - # @return [Object] The converted key. If the key was a symbol, it will be converted to a string. - # - # @api private - def convert_key(key) - key.is_a?(Symbol) ? key.to_s : key - end - - # @param value [Object] The value to convert. - # - # @return [Object] - # The converted value. A Hash or an Array of hashes, will be converted to - # their Mash equivalents. - # - # @api private - def convert_value(value) - if value.class == Hash - Mash.from_hash(value) - elsif value.is_a?(Array) - value.collect { |e| convert_value(e) } - else - value - end - end -end +# +# Copyright:: Copyright (c) Chef Software Inc. +# 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 "chef-utils/mash" unless defined?(ChefUtils::Mash) + +# For historical reasons we inject Mash directly into the top level class namespace +Mash = ChefUtils::Mash unless defined?(Mash) diff --git a/lib/ohai/plugins/cpu.rb b/lib/ohai/plugins/cpu.rb index 5375c726..eb7ea232 100644 --- a/lib/ohai/plugins/cpu.rb +++ b/lib/ohai/plugins/cpu.rb @@ -364,7 +364,7 @@ Ohai.plugin(:CPU) do cpu["cpustates"][value] += 1 when /core_id/ cpu[instance]["core_id"] = value - # Detect hyperthreading/multithreading + # Detect hyperthreading/multithreading cpucores.push(value) if cpucores.index(value).nil? when /family|fpu_type|model|stepping|vendor_id/ cpu[instance][key] = value diff --git a/lib/ohai/plugins/virtualbox.rb b/lib/ohai/plugins/virtualbox.rb index b2549f99..c3277d4c 100644 --- a/lib/ohai/plugins/virtualbox.rb +++ b/lib/ohai/plugins/virtualbox.rb @@ -93,8 +93,8 @@ Ohai.plugin(:Virtualbox) do so_cmd = "VBoxManage list --sorted #{query_type}" logger.trace(so_cmd) so = shell_out(so_cmd) - # raise an exception if the command fails - # so.error! + # raise an exception if the command fails + # so.error! if so.exitstatus == 0 # break the result into paragraph blocks, on successive newlines diff --git a/ohai.gemspec b/ohai.gemspec index c242fd9c..2d6a9a5c 100644 --- a/ohai.gemspec +++ b/ohai.gemspec @@ -25,6 +25,7 @@ Gem::Specification.new do |s| s.add_dependency "wmi-lite", "~> 1.0" s.add_dependency "ffi", "~> 1.9" s.add_dependency "chef-config", ">= 12.8", "< 17" + s.add_dependency "chef-utils", ">= 16.0", "< 17" # Note for ohai developers: If chef-config causes you grief, try: # bundle install --with development # this should work as long as chef is a development dependency in Gemfile. diff --git a/spec/unit/plugins/abort_spec.rb b/spec/unit/plugins/abort_spec.rb index 68531eb1..447427ed 100644 --- a/spec/unit/plugins/abort_spec.rb +++ b/spec/unit/plugins/abort_spec.rb @@ -34,7 +34,7 @@ describe "a plug-in that aborts execution" do Dir.mkdir("#{tmp}/plugins") rescue Errno::EEXIST - # ignore + # ignore end @@ -52,7 +52,7 @@ describe "a plug-in that aborts execution" do Dir.delete("#{tmp}/plugins") rescue - # ignore + # ignore end diff --git a/spec/unit/plugins/fail_spec.rb b/spec/unit/plugins/fail_spec.rb index 35a0643a..1fa252d1 100644 --- a/spec/unit/plugins/fail_spec.rb +++ b/spec/unit/plugins/fail_spec.rb @@ -26,7 +26,7 @@ shared_examples "a v7 loading failure" do Dir.mkdir("#{tmp}/plugins") rescue Errno::EEXIST - # ignore + # ignore end @@ -44,7 +44,7 @@ shared_examples "a v7 loading failure" do Dir.delete("#{tmp}/plugins") rescue - # ignore + # ignore end @@ -70,7 +70,7 @@ shared_examples "a v7 loading success" do Dir.mkdir("#{tmp}/plugins") rescue Errno::EEXIST - # ignore + # ignore end @@ -88,7 +88,7 @@ shared_examples "a v7 loading success" do Dir.delete("#{tmp}/plugins") rescue - # ignore + # ignore end @@ -113,7 +113,7 @@ shared_examples "a v7 run failure" do Dir.mkdir("#{tmp}/plugins") rescue Errno::EEXIST - # ignore + # ignore end @@ -131,7 +131,7 @@ shared_examples "a v7 run failure" do Dir.delete("#{tmp}/plugins") rescue - # ignore + # ignore end diff --git a/spec/unit/plugins/linux/cpu_spec.rb b/spec/unit/plugins/linux/cpu_spec.rb index 38de38a3..479c9083 100644 --- a/spec/unit/plugins/linux/cpu_spec.rb +++ b/spec/unit/plugins/linux/cpu_spec.rb @@ -96,7 +96,7 @@ describe Ohai::System, "General Linux cpu plugin" do tempfile.close tempfile.unlink rescue - # really do not care + # really do not care end diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index 4e6974e4..8aa11336 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -142,8 +142,8 @@ describe Ohai::System, "Linux Network Plugin" do collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:140 (140.0 B) EOM -# Note that ifconfig shows foo:veth0@eth0 but fails to show any address information. -# This was not a mistake collecting the output and Apparently ifconfig is broken in this regard. + # Note that ifconfig shows foo:veth0@eth0 but fails to show any address information. + # This was not a mistake collecting the output and Apparently ifconfig is broken in this regard. end let(:linux_ip_route) do diff --git a/spec/unit/plugins/solaris2/hostname_spec.rb b/spec/unit/plugins/solaris2/hostname_spec.rb index a8db9478..390b5e45 100644 --- a/spec/unit/plugins/solaris2/hostname_spec.rb +++ b/spec/unit/plugins/solaris2/hostname_spec.rb @@ -24,7 +24,7 @@ describe Ohai::System, "Solaris2.X hostname plugin" do allow(@plugin).to receive(:collect_os).and_return(:solaris2) allow(@plugin).to receive(:resolve_fqdn).and_return("kitteh.inurfridge.eatinurfoodz") allow(@plugin).to receive(:shell_out).with("hostname").and_return(mock_shell_out(0, "kitteh\n", "")) -# Socket.stub(:getaddrinfo).and_return( [["AF_INET", 0, "kitteh.inurfridge.eatinurfoodz", "10.1.2.3", 2, 0, 0]] ); + # Socket.stub(:getaddrinfo).and_return( [["AF_INET", 0, "kitteh.inurfridge.eatinurfoodz", "10.1.2.3", 2, 0, 0]] ); end it_should_check_from("solaris2::hostname", "hostname", "hostname", "kitteh") |