From f11b854cd189e8ad311592d507a0849cfbc0da52 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Fri, 21 Aug 2020 16:30:54 -0700 Subject: Migrate to the chef-utils helpers This replaces the which/shell_out helpers with ones from chef-utils. Signed-off-by: Lamont Granquist --- Rakefile | 2 +- lib/ohai/dsl/plugin.rb | 10 +-- lib/ohai/mixin/chef_utils_wiring.rb | 41 +++++++++ lib/ohai/mixin/command.rb | 56 +----------- lib/ohai/mixin/shell_out.rb | 50 +++++++++++ lib/ohai/mixin/which.rb | 38 ++++++++ lib/ohai/system.rb | 5 +- lib/ohai/util/file_helper.rb | 40 +-------- spec/unit/mixin/command_spec.rb | 150 ------------------------------- spec/unit/mixin/shell_out_spec.rb | 172 ++++++++++++++++++++++++++++++++++++ 10 files changed, 314 insertions(+), 250 deletions(-) create mode 100644 lib/ohai/mixin/chef_utils_wiring.rb create mode 100644 lib/ohai/mixin/shell_out.rb create mode 100644 lib/ohai/mixin/which.rb delete mode 100644 spec/unit/mixin/command_spec.rb create mode 100644 spec/unit/mixin/shell_out_spec.rb diff --git a/Rakefile b/Rakefile index 93aff295..00d67eda 100644 --- a/Rakefile +++ b/Rakefile @@ -40,4 +40,4 @@ task :console do IRB.start end -task default: %i{style spec} +task default: %i{spec style} diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index c1722370..165ecd39 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob () # Author:: Claire McQuin () -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. +# Copyright:: Copyright (c) Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,10 +19,10 @@ # require_relative "../mixin/os" -require_relative "../mixin/command" +require_relative "../mixin/shell_out" require_relative "../mixin/seconds_to_human" +require_relative "../mixin/which" require_relative "../hints" -require_relative "../util/file_helper" module Ohai @@ -83,9 +83,9 @@ module Ohai class Plugin include Ohai::Mixin::OS - include Ohai::Mixin::Command + include Ohai::Mixin::ShellOut include Ohai::Mixin::SecondsToHuman - include Ohai::Util::FileHelper + include Ohai::Mixin::Which attr_reader :data attr_reader :failed diff --git a/lib/ohai/mixin/chef_utils_wiring.rb b/lib/ohai/mixin/chef_utils_wiring.rb new file mode 100644 index 00000000..527ac497 --- /dev/null +++ b/lib/ohai/mixin/chef_utils_wiring.rb @@ -0,0 +1,41 @@ +# +# 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_relative "../log" +# require_relative "../config" +# require_relative "../chef_class" + +module Ohai + module Mixin + # Common Dependency Injection wiring for ChefUtils-related modules + module ChefUtilsWiring + private + + def __config + Ohai::Config + end + + def __log + logger + end + + def __transport_connection + # Chef.run_context&.transport_connection + end + end + end +end diff --git a/lib/ohai/mixin/command.rb b/lib/ohai/mixin/command.rb index 503b70d3..f335db93 100644 --- a/lib/ohai/mixin/command.rb +++ b/lib/ohai/mixin/command.rb @@ -1,54 +1,2 @@ -# -# Author:: Adam Jacob () -# Author:: Tim Smith () -# Copyright:: Copyright (c) 2008-2016 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_relative "../exception" -require_relative "../log" -require "mixlib/shellout" unless defined?(Mixlib::ShellOut::DEFAULT_READ_TIMEOUT) - -module Ohai - module Mixin - module Command - # DISCLAIMER: Logging only works in the context of a plugin!! - # accept a command and any of the mixlib-shellout options - def shell_out(cmd, **options) - options = options.dup - # unless specified by the caller timeout after configured timeout (default 30 seconds) - options[:timeout] ||= Ohai::Config.ohai[:shellout_timeout] - unless RUBY_PLATFORM.match?(/mswin|mingw32|windows/) - options[:env] = options.key?(:env) ? options[:env].dup : {} - options[:env]["PATH"] ||= ((ENV["PATH"] || "").split(":") + %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}).join(":") - end - so = Mixlib::ShellOut.new(cmd, options) - begin - so.run_command - logger.trace("Plugin #{name}: ran '#{cmd}' and returned #{so.exitstatus}") - so - rescue Errno::ENOENT => e - logger.trace("Plugin #{name}: ran '#{cmd}' and failed #{e.inspect}") - raise Ohai::Exceptions::Exec, e - rescue Mixlib::ShellOut::CommandTimeout => e - logger.trace("Plugin #{name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds") - raise Ohai::Exceptions::Exec, e - end - end - - module_function :shell_out - end - end -end +require_relative "shell_out" +Ohai::Mixin::Command = Ohai::Mixin::ShellOut unless defined?(Ohai::Mixin::Command) diff --git a/lib/ohai/mixin/shell_out.rb b/lib/ohai/mixin/shell_out.rb new file mode 100644 index 00000000..18c2ee38 --- /dev/null +++ b/lib/ohai/mixin/shell_out.rb @@ -0,0 +1,50 @@ +# +# Author:: Adam Jacob () +# Author:: Tim Smith () +# 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_relative "../exception" +require_relative "../log" + +require "mixlib/shellout/helper" unless defined?(Mixlib::ShellOut::Helper) +require_relative "chef_utils_wiring" unless defined?(Ohai::Mixin::ChefUtilsWiring) + +module Ohai + module Mixin + module ShellOut + include Mixlib::ShellOut::Helper + include Ohai::Mixin::ChefUtilsWiring + + def shell_out(cmd, **options) + options = options.dup + # unless specified by the caller timeout after configured timeout (default 30 seconds) + options[:timeout] ||= Ohai::Config.ohai[:shellout_timeout] + begin + so = super(cmd, **options) + logger.trace("Plugin #{name}: ran '#{cmd}' and returned #{so.exitstatus}") + so + rescue Errno::ENOENT => e + logger.trace("Plugin #{name}: ran '#{cmd}' and failed #{e.inspect}") + raise Ohai::Exceptions::Exec, e + rescue Mixlib::ShellOut::CommandTimeout => e + logger.trace("Plugin #{name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds") + raise Ohai::Exceptions::Exec, e + end + end + end + end +end diff --git a/lib/ohai/mixin/which.rb b/lib/ohai/mixin/which.rb new file mode 100644 index 00000000..32c83f0f --- /dev/null +++ b/lib/ohai/mixin/which.rb @@ -0,0 +1,38 @@ +#-- +# 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/dsl/which" unless defined?(ChefUtils::DSL::Which) +require "chef-utils/dsl/default_paths" unless defined?(ChefUtils::DSL::DefaultPaths) +require_relative "chef_utils_wiring" unless defined?(Ohai::Mixin::ChefUtilsWiring) + +module Ohai + module Mixin + module Which + include ChefUtils::DSL::Which + include ChefUtils::DSL::DefaultPaths + include ChefUtilsWiring + + private + + # we dep-inject default paths into this API for historical reasons + # + # @api private + def __extra_path + __default_paths + end + end + end +end diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 6cddf774..ee196414 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob () -# Copyright:: Copyright (c) 2008-2019, Chef Software Inc. +# Copyright:: Copyright (c) Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,13 +22,12 @@ require_relative "log" require_relative "mash" require_relative "runner" require_relative "dsl" -require_relative "mixin/command" +require_relative "mixin/shell_out" require_relative "mixin/os" require_relative "mixin/string" require_relative "mixin/constant_helper" require_relative "provides_map" require_relative "hints" -require "mixlib/shellout" unless defined?(Mixlib::ShellOut::DEFAULT_READ_TIMEOUT) require_relative "config" require "ffi_yajl" unless defined?(FFI_Yajl) diff --git a/lib/ohai/util/file_helper.rb b/lib/ohai/util/file_helper.rb index 87358d95..6a52be15 100644 --- a/lib/ohai/util/file_helper.rb +++ b/lib/ohai/util/file_helper.rb @@ -1,38 +1,4 @@ -# Author:: Lamont Granquist () -# -# Copyright:: Copyright (c) 2013-14 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. - -# Copied from chef/lib/chef/util/selinux.rb - -module Ohai - module Util - module FileHelper - def which(cmd) - paths = ENV["PATH"].split(File::PATH_SEPARATOR) + [ "/bin", "/usr/bin", "/sbin", "/usr/sbin" ] - paths.each do |path| - filename = File.join(path, cmd) - if File.executable?(filename) - logger.trace("Plugin #{name}: found #{cmd} at #{filename}") - return filename - end - end - logger.trace("Plugin #{name}: did not find #{cmd}") - false - end - end - end +require_relative "../mixin/which" +module Ohai::Util + FileHelper = Ohai::Mixin::Which end diff --git a/spec/unit/mixin/command_spec.rb b/spec/unit/mixin/command_spec.rb deleted file mode 100644 index d6df3b0d..00000000 --- a/spec/unit/mixin/command_spec.rb +++ /dev/null @@ -1,150 +0,0 @@ -# -# Author:: Diego Algorta (diego@oboxodo.com) -# Copyright:: Copyright (c) 2009 Diego Algorta -# 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" - -describe Ohai::Mixin::Command, "shell_out" do - let(:cmd) { "sparkle-dream --version" } - - let(:shell_out) { double("Mixlib::ShellOut") } - - let(:plugin_name) { :OSSparkleDream } - - let(:options) { windows? ? { timeout: 30 } : { timeout: 30, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } } - - let(:logger) { instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil) } - - before do - allow(described_class).to receive(:logger).and_return(logger) - allow(described_class).to receive(:name).and_return(plugin_name) - @original_env = ENV.to_hash - ENV.clear - end - - after do - ENV.clear - ENV.update(@original_env) - end - - describe "when the command runs" do - it "logs the command and exitstatus" do - expect(Mixlib::ShellOut) - .to receive(:new) - .with(cmd, options) - .and_return(shell_out) - - expect(shell_out) - .to receive(:run_command) - - expect(shell_out) - .to receive(:exitstatus) - .and_return(256) - - expect(logger).to receive(:trace) - .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and returned 256") - - described_class.shell_out(cmd) - end - end - - describe "when the command does not exist" do - it "logs the command and error message" do - expect(Mixlib::ShellOut) - .to receive(:new) - .with(cmd, options) - .and_return(shell_out) - - expect(shell_out) - .to receive(:run_command) - .and_raise(Errno::ENOENT, "sparkle-dream") - - expect(logger) - .to receive(:trace) - .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and failed " \ - "#") - - expect { described_class.shell_out(cmd) } - .to raise_error(Ohai::Exceptions::Exec) - end - end - - describe "when the command times out" do - it "logs the command an timeout error message" do - expect(Mixlib::ShellOut) - .to receive(:new) - .with(cmd, options) - .and_return(shell_out) - - expect(shell_out) - .to receive(:run_command) - .and_raise(Mixlib::ShellOut::CommandTimeout) - - expect(logger) - .to receive(:trace) - .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ - "out after 30 seconds") - - expect { described_class.shell_out(cmd) } - .to raise_error(Ohai::Exceptions::Exec) - end - end - - describe "when a timeout option is provided" do - let(:options) { windows? ? { timeout: 10 } : { timeout: 10, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } } - - it "runs the command with the provided timeout" do - expect(Mixlib::ShellOut) - .to receive(:new) - .with(cmd, options) - .and_return(shell_out) - - expect(shell_out) - .to receive(:run_command) - - expect(shell_out) - .to receive(:exitstatus) - .and_return(256) - - expect(logger).to receive(:trace) - .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and returned 256") - - described_class.shell_out(cmd, options) - end - - describe "when the command times out" do - it "logs the command an timeout error message" do - expect(Mixlib::ShellOut) - .to receive(:new) - .with(cmd, options) - .and_return(shell_out) - - expect(shell_out) - .to receive(:run_command) - .and_raise(Mixlib::ShellOut::CommandTimeout) - - expect(logger) - .to receive(:trace) - .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ - "out after 10 seconds") - - expect { described_class.shell_out(cmd, options) } - .to raise_error(Ohai::Exceptions::Exec) - end - end - end -end diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb new file mode 100644 index 00000000..6c54b166 --- /dev/null +++ b/spec/unit/mixin/shell_out_spec.rb @@ -0,0 +1,172 @@ +# +# Author:: Diego Algorta (diego@oboxodo.com) +# Copyright:: Copyright (c) 2009 Diego Algorta +# 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" + +describe Ohai::Mixin::ShellOut, "shell_out" do + let(:cmd) { "sparkle-dream --version" } + + let(:shell_out) { double("Mixlib::ShellOut", live_stream: nil, :live_stream= => nil) } + + let(:plugin_name) { :OSSparkleDream } + + let(:timeout) { 30 } + + let(:options) do + if windows? + { timeout: timeout } + else + { + timeout: timeout, + environment: { + "LANG" => "en_US.UTF-8", + "LANGUAGE" => "en_US.UTF-8", + "LC_ALL" => "en_US.UTF-8", + "PATH" => "/Users/lamont/.asdf/installs/ruby/2.7.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + }, + } + end + end + + let(:logger) { instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil, debug?: false) } + + class DummyPlugin + include Ohai::Mixin::ShellOut + end + + let(:instance) { DummyPlugin.new } + + before do + allow(instance).to receive(:logger).and_return(logger) + allow(instance).to receive(:name).and_return(plugin_name) + @original_env = ENV.to_hash + ENV.clear + end + + after do + ENV.clear + ENV.update(@original_env) + end + + describe "when the command runs" do + it "logs the command and exitstatus" do + expect(Mixlib::ShellOut) + .to receive(:new) + .with(cmd, options) + .and_return(shell_out) + + expect(shell_out) + .to receive(:run_command) + + expect(shell_out) + .to receive(:exitstatus) + .and_return(256) + + expect(logger).to receive(:trace) + .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and returned 256") + + instance.shell_out(cmd) + end + end + + describe "when the command does not exist" do + it "logs the command and error message" do + expect(Mixlib::ShellOut) + .to receive(:new) + .with(cmd, options) + .and_return(shell_out) + + expect(shell_out) + .to receive(:run_command) + .and_raise(Errno::ENOENT, "sparkle-dream") + + expect(logger) + .to receive(:trace) + .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and failed " \ + "#") + + expect { instance.shell_out(cmd) } + .to raise_error(Ohai::Exceptions::Exec) + end + end + + describe "when the command times out" do + it "logs the command an timeout error message" do + expect(Mixlib::ShellOut) + .to receive(:new) + .with(cmd, options) + .and_return(shell_out) + + expect(shell_out) + .to receive(:run_command) + .and_raise(Mixlib::ShellOut::CommandTimeout) + + expect(logger) + .to receive(:trace) + .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ + "out after 30 seconds") + + expect { instance.shell_out(cmd) } + .to raise_error(Ohai::Exceptions::Exec) + end + end + + describe "when a timeout option is provided" do + let(:timeout) { 10 } + + it "runs the command with the provided timeout" do + expect(Mixlib::ShellOut) + .to receive(:new) + .with(cmd, options) + .and_return(shell_out) + + expect(shell_out) + .to receive(:run_command) + + expect(shell_out) + .to receive(:exitstatus) + .and_return(256) + + expect(logger).to receive(:trace) + .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and returned 256") + + instance.shell_out(cmd, timeout: 10) + end + + describe "when the command times out" do + it "logs the command an timeout error message" do + expect(Mixlib::ShellOut) + .to receive(:new) + .with(cmd, options) + .and_return(shell_out) + + expect(shell_out) + .to receive(:run_command) + .and_raise(Mixlib::ShellOut::CommandTimeout) + + expect(logger) + .to receive(:trace) + .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ + "out after 10 seconds") + + expect { instance.shell_out(cmd, timeout: 10) } + .to raise_error(Ohai::Exceptions::Exec) + end + end + end +end -- cgit v1.2.1 From 35a7946499362de10c82660698a2af7a85c06135 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Fri, 21 Aug 2020 16:39:50 -0700 Subject: remove hardcoded path from my machine Signed-off-by: Lamont Granquist --- spec/unit/mixin/shell_out_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index 6c54b166..b70b41b3 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -37,7 +37,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do "LANG" => "en_US.UTF-8", "LANGUAGE" => "en_US.UTF-8", "LC_ALL" => "en_US.UTF-8", - "PATH" => "/Users/lamont/.asdf/installs/ruby/2.7.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "PATH" => "#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", }, } end -- cgit v1.2.1 From 52208ef602e335d6b3b4c24513a5ace018d5e163 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Fri, 21 Aug 2020 17:09:23 -0700 Subject: Fix the PATH for the way it gets de-duplicated The default paths algorithm is a little bit weird Signed-off-by: Lamont Granquist --- spec/unit/mixin/shell_out_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index b70b41b3..b6284a3b 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -37,7 +37,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do "LANG" => "en_US.UTF-8", "LANGUAGE" => "en_US.UTF-8", "LC_ALL" => "en_US.UTF-8", - "PATH" => "#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "PATH" => [ "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin", RbConfig::CONFIG["bindir"] ].uniq.reverse.join(':') }, } end -- cgit v1.2.1 From 3f55e30b887a7f953b512c830680b36ca9b970b6 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Fri, 21 Aug 2020 17:11:15 -0700 Subject: single quotes again Signed-off-by: Lamont Granquist --- spec/unit/mixin/shell_out_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index b6284a3b..3a49745c 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -37,7 +37,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do "LANG" => "en_US.UTF-8", "LANGUAGE" => "en_US.UTF-8", "LC_ALL" => "en_US.UTF-8", - "PATH" => [ "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin", RbConfig::CONFIG["bindir"] ].uniq.reverse.join(':') + "PATH" => [ "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin", RbConfig::CONFIG["bindir"] ].uniq.reverse.join(":"), }, } end -- cgit v1.2.1 From a8abf9260b74ab6f316a717380eb727aaa758184 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 24 Aug 2020 22:36:50 -0700 Subject: code review Signed-off-by: Lamont Granquist --- lib/ohai/mixin/chef_utils_wiring.rb | 4 +--- lib/ohai/mixin/command.rb | 1 + lib/ohai/mixin/which.rb | 2 +- lib/ohai/util/file_helper.rb | 1 + 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ohai/mixin/chef_utils_wiring.rb b/lib/ohai/mixin/chef_utils_wiring.rb index 527ac497..3ca61c25 100644 --- a/lib/ohai/mixin/chef_utils_wiring.rb +++ b/lib/ohai/mixin/chef_utils_wiring.rb @@ -15,9 +15,7 @@ # limitations under the License. # -# require_relative "../log" -# require_relative "../config" -# require_relative "../chef_class" +require_relative "../config" module Ohai module Mixin diff --git a/lib/ohai/mixin/command.rb b/lib/ohai/mixin/command.rb index f335db93..030844a7 100644 --- a/lib/ohai/mixin/command.rb +++ b/lib/ohai/mixin/command.rb @@ -1,2 +1,3 @@ +$stderr.puts "WARN: Ohai::Mixin::Command is deprecated, please use Ohai::Mixin::ShellOut or remove if the reference is unnecessary" require_relative "shell_out" Ohai::Mixin::Command = Ohai::Mixin::ShellOut unless defined?(Ohai::Mixin::Command) diff --git a/lib/ohai/mixin/which.rb b/lib/ohai/mixin/which.rb index 32c83f0f..b21ee640 100644 --- a/lib/ohai/mixin/which.rb +++ b/lib/ohai/mixin/which.rb @@ -1,4 +1,4 @@ -#-- +# # Copyright:: Copyright (c) Chef Software Inc. # License:: Apache License, Version 2.0 # diff --git a/lib/ohai/util/file_helper.rb b/lib/ohai/util/file_helper.rb index 6a52be15..019d39d7 100644 --- a/lib/ohai/util/file_helper.rb +++ b/lib/ohai/util/file_helper.rb @@ -1,3 +1,4 @@ +$stderr.puts "WARN: Ohai::Util::FileHelper is deprecated, please use Ohai::Mixin::Which or remove if the reference is unnecessary" require_relative "../mixin/which" module Ohai::Util FileHelper = Ohai::Mixin::Which -- cgit v1.2.1 From 483a5295873a411a36357a6d02ad725e1e15008d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 25 Aug 2020 12:37:18 -0700 Subject: fix specs Signed-off-by: Lamont Granquist --- spec/unit/mixin/shell_out_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index 3a49745c..1b3dac51 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -31,13 +31,15 @@ describe Ohai::Mixin::ShellOut, "shell_out" do if windows? { timeout: timeout } else + # this just replicates the behavior of default_paths in chef-utils + default_paths = ( [ ENV['PATH'] ? ENV['PATH'].split(':').reverse : nil, RbConfig::CONFIG["bindir"] ].uniq.reverse + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") { timeout: timeout, environment: { "LANG" => "en_US.UTF-8", "LANGUAGE" => "en_US.UTF-8", "LC_ALL" => "en_US.UTF-8", - "PATH" => [ "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin", RbConfig::CONFIG["bindir"] ].uniq.reverse.join(":"), + "PATH" => default_paths, }, } end -- cgit v1.2.1 From b93bb1ec4abc31e7babc2b437e9ee6c0b03ccfbb Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 25 Aug 2020 15:57:59 -0700 Subject: fix the deafult locale we need to call out to the ChefConfig helper to set this Signed-off-by: Lamont Granquist --- spec/unit/mixin/shell_out_spec.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index 1b3dac51..f02c9c13 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -33,12 +33,13 @@ describe Ohai::Mixin::ShellOut, "shell_out" do else # this just replicates the behavior of default_paths in chef-utils default_paths = ( [ ENV['PATH'] ? ENV['PATH'].split(':').reverse : nil, RbConfig::CONFIG["bindir"] ].uniq.reverse + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") + default_locale = ChefConfig::Config.guess_internal_locale { timeout: timeout, environment: { - "LANG" => "en_US.UTF-8", - "LANGUAGE" => "en_US.UTF-8", - "LC_ALL" => "en_US.UTF-8", + "LANG" => default_locale, + "LANGUAGE" => default_locale, + "LC_ALL" => default_locale, "PATH" => default_paths, }, } -- cgit v1.2.1 From 5e4fc4ab25185696c7ad84aa34f17a3eed6ab13c Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 25 Aug 2020 16:01:58 -0700 Subject: chefstyle Signed-off-by: Lamont Granquist --- lib/ohai/plugins/ec2.rb | 2 +- lib/ohai/plugins/shard.rb | 2 +- spec/unit/mixin/shell_out_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index b4fb735e..c526a37b 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -114,7 +114,7 @@ Ohai.plugin(:EC2) do end collect_data do - require "base64" + require "base64" unless defined?(Base64) if looks_like_ec2? logger.trace("Plugin EC2: looks_like_ec2? == true") diff --git a/lib/ohai/plugins/shard.rb b/lib/ohai/plugins/shard.rb index cf7641b0..bfd07212 100644 --- a/lib/ohai/plugins/shard.rb +++ b/lib/ohai/plugins/shard.rb @@ -52,7 +52,7 @@ Ohai.plugin(:ShardSeed) do def digest_algorithm case Ohai.config[:plugin][:shard_seed][:digest_algorithm] || default_digest_algorithm when "md5" - require "digest/md5" + require "digest/md5" unless defined?(Digest::MD5) Digest::MD5 when "sha256" require "openssl/digest" diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index f02c9c13..93ccb2c3 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -32,7 +32,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do { timeout: timeout } else # this just replicates the behavior of default_paths in chef-utils - default_paths = ( [ ENV['PATH'] ? ENV['PATH'].split(':').reverse : nil, RbConfig::CONFIG["bindir"] ].uniq.reverse + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") + default_paths = ( [ ENV["PATH"] ? ENV["PATH"].split(":").reverse : nil, RbConfig::CONFIG["bindir"] ].uniq.reverse + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") default_locale = ChefConfig::Config.guess_internal_locale { timeout: timeout, -- cgit v1.2.1 From c2cfa71a400ff49493adc0882373bd55d6141e8a Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 25 Aug 2020 16:27:40 -0700 Subject: trying to fix windows tests Signed-off-by: Lamont Granquist --- spec/unit/mixin/shell_out_spec.rb | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index 93ccb2c3..a7ff5776 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -28,22 +28,25 @@ describe Ohai::Mixin::ShellOut, "shell_out" do let(:timeout) { 30 } let(:options) do + # this just replicates the behavior of default_paths in chef-utils + default_paths = [ Gem.bindir, RbConfig::CONFIG["bindir"] ].compact.uniq + if windows? - { timeout: timeout } + default_paths = default_paths.join(";") else - # this just replicates the behavior of default_paths in chef-utils - default_paths = ( [ ENV["PATH"] ? ENV["PATH"].split(":").reverse : nil, RbConfig::CONFIG["bindir"] ].uniq.reverse + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") - default_locale = ChefConfig::Config.guess_internal_locale - { - timeout: timeout, - environment: { - "LANG" => default_locale, - "LANGUAGE" => default_locale, - "LC_ALL" => default_locale, - "PATH" => default_paths, - }, - } + default_paths = ( default_paths + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") end + + default_locale = ChefConfig::Config.guess_internal_locale + { + timeout: timeout, + environment: { + "LANG" => default_locale, + "LANGUAGE" => default_locale, + "LC_ALL" => default_locale, + "PATH" => default_paths, + }, + } end let(:logger) { instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil, debug?: false) } @@ -101,7 +104,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do expect(logger) .to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and failed " \ - "#") + "#") expect { instance.shell_out(cmd) } .to raise_error(Ohai::Exceptions::Exec) @@ -122,7 +125,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do expect(logger) .to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ - "out after 30 seconds") + "out after 30 seconds") expect { instance.shell_out(cmd) } .to raise_error(Ohai::Exceptions::Exec) @@ -165,7 +168,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do expect(logger) .to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ - "out after 10 seconds") + "out after 10 seconds") expect { instance.shell_out(cmd, timeout: 10) } .to raise_error(Ohai::Exceptions::Exec) -- cgit v1.2.1 From cf74fad25b978b400226c616d4cc70cd21374a34 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 25 Aug 2020 16:32:09 -0700 Subject: fix case sensitive windows path Signed-off-by: Lamont Granquist --- spec/unit/mixin/shell_out_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index a7ff5776..e7e194c7 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -37,6 +37,8 @@ describe Ohai::Mixin::ShellOut, "shell_out" do default_paths = ( default_paths + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") end + path_var = windows? ? "Path" : "PATH" + default_locale = ChefConfig::Config.guess_internal_locale { timeout: timeout, @@ -44,7 +46,7 @@ describe Ohai::Mixin::ShellOut, "shell_out" do "LANG" => default_locale, "LANGUAGE" => default_locale, "LC_ALL" => default_locale, - "PATH" => default_paths, + path_var => default_paths, }, } end -- cgit v1.2.1 From 238a4cb9baf9728ee9076ceefcfb6324960a0262 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 25 Aug 2020 17:51:42 -0700 Subject: Try to fix windows units This throws warnings, but I'm a bit out of ideas today. This lies like crazy to make it work on windows. Signed-off-by: Lamont Granquist --- spec/unit/mixin/which_spec.rb | 48 ++++++++++++++++++++++++++++++++++++++ spec/unit/util/file_helper_spec.rb | 47 ------------------------------------- 2 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 spec/unit/mixin/which_spec.rb delete mode 100644 spec/unit/util/file_helper_spec.rb diff --git a/spec/unit/mixin/which_spec.rb b/spec/unit/mixin/which_spec.rb new file mode 100644 index 00000000..d6236757 --- /dev/null +++ b/spec/unit/mixin/which_spec.rb @@ -0,0 +1,48 @@ +# Author:: Bryan McLellan +# +# 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 "spec_helper" + +class FileHelperMock + include Ohai::Mixin::Which +end + +describe "Ohai::Mixin::Which" do + let(:file_helper) { FileHelperMock.new } + + before do + old_env = ENV + ENV["Path"] = ENV["PATH"] = "/usr/bin" + allow(file_helper).to receive(:name).and_return("Fakeclass") + logger = instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil) + allow(file_helper).to receive(:logger).and_return(logger) + allow(File).to receive(:executable?).and_return(false) + ENV = old_env + end + + describe "which" do + it "returns the path to an executable that is in the path" do + allow(File).to receive(:executable?).with("/usr/bin/skyhawk").and_return(true) + expect(file_helper.which("skyhawk")).to eql "/usr/bin/skyhawk" + end + + it "returns false if the executable is not in the path" do + expect(file_helper.which("the_cake")).to be false + end + end +end diff --git a/spec/unit/util/file_helper_spec.rb b/spec/unit/util/file_helper_spec.rb deleted file mode 100644 index 8fc542f0..00000000 --- a/spec/unit/util/file_helper_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -# Author:: Bryan McLellan -# -# Copyright:: Copyright (c) 2014-2016 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 "spec_helper" -require "ohai/util/file_helper" - -class FileHelperMock - include Ohai::Util::FileHelper -end - -describe "Ohai::Util::FileHelper" do - let(:file_helper) { FileHelperMock.new } - - before do - allow(file_helper).to receive(:name).and_return("Fakeclass") - logger = instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil) - allow(file_helper).to receive(:logger).and_return(logger) - allow(File).to receive(:executable?).and_return(false) - end - - describe "which" do - it "returns the path to an executable that is in the path" do - allow(File).to receive(:executable?).with("/usr/bin/skyhawk").and_return(true) - - expect(file_helper.which("skyhawk")).to eql "/usr/bin/skyhawk" - end - - it "returns false if the executable is not in the path" do - expect(file_helper.which("the_cake")).to be false - end - end -end -- cgit v1.2.1