diff options
author | Tim Smith <tsmith@chef.io> | 2017-02-28 11:18:01 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-28 11:18:01 -0800 |
commit | cd241b6cd73d2d103145c27bda1ca8700a777a4d (patch) | |
tree | 0a7d42ef02292256957c17565cbddf50d5cf4732 | |
parent | 512597d0eaf5a2b83e0997d8074ec1c3a5fab118 (diff) | |
parent | bc92addc6021e217e85586351a91d1cab449a933 (diff) | |
download | ohai-cd241b6cd73d2d103145c27bda1ca8700a777a4d.tar.gz |
Merge pull request #955 from chef/lcg/fix-logger
fix logger issues
-rw-r--r-- | lib/ohai/application.rb | 1 | ||||
-rw-r--r-- | lib/ohai/log.rb | 4 | ||||
-rw-r--r-- | lib/ohai/system.rb | 9 | ||||
-rw-r--r-- | spec/unit/system_spec.rb | 34 |
4 files changed, 29 insertions, 19 deletions
diff --git a/lib/ohai/application.rb b/lib/ohai/application.rb index 9f1c400f..586a275d 100644 --- a/lib/ohai/application.rb +++ b/lib/ohai/application.rb @@ -88,6 +88,7 @@ class Ohai::Application end def run_application + config[:invoked_from_cli] = true ohai = Ohai::System.new(config) ohai.all_plugins(@attributes) diff --git a/lib/ohai/log.rb b/lib/ohai/log.rb index c63f7abe..832bf005 100644 --- a/lib/ohai/log.rb +++ b/lib/ohai/log.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,6 +22,8 @@ module Ohai class Log extend Mixlib::Log + # this class loading initalization is so that we don't lose early logger + # messages when run from the CLI? init(STDERR) level = :info diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 0e8f5b7d..e66ba90b 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -39,7 +39,12 @@ module Ohai attr_reader :provides_map attr_reader :v6_dependency_solver + # the cli flag is used to determine if we're being constructed by + # something like chef-client (which doesn't not set this flag) and + # which sets up its own loggers, or if we're coming from Ohai::Application + # and therefore need to configure Ohai's own logger. def initialize(config = {}) + @cli = config[:invoked_from_cli] @plugin_path = "" @config = config reset_system @@ -51,7 +56,7 @@ module Ohai @v6_dependency_solver = Hash.new configure_ohai - configure_logging + configure_logging if @cli @loader = Ohai::Loader.new(self) @runner = Ohai::Runner.new(self, true) diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index f184d8ee..8a7cf49b 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Claire McQuin (<claire@chef.io>) -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,7 +23,8 @@ require "ohai/mixin/os" describe "Ohai::System" do extend IntegrationSupport - let(:ohai) { Ohai::System.new } + let(:ohai_external) { } + let(:ohai) { Ohai::System.new({ invoked_from_cli: true }) } describe "#initialize" do it "returns an Ohai::System object" do @@ -43,8 +44,6 @@ describe "Ohai::System" do disabled_plugins: [ :Foo, :Baz ], directory: "/some/extra/plugins", } - allow(Ohai::Config).to receive(:merge_deprecated_config) - expect(Ohai.config).to receive(:merge!).with(config).and_call_original Ohai::System.new(config) config.each do |option, value| expect(Ohai.config[option]).to eq(value) @@ -56,33 +55,36 @@ describe "Ohai::System" do it "adds directory to plugin_path" do Ohai.config[:directory] = directory - Ohai::System.new + Ohai::System.new({ invoked_from_cli: true }) expect(Ohai.config[:plugin_path]).to include(directory) end end - context "first time configuration" do - before { allow(Ohai::Log).to receive(:configured?).and_return(false) } - + context "when testing the intializer that does way too much" do it "configures logging" do log_level = :debug Ohai.config[:log_level] = log_level expect(Ohai::Log).to receive(:level=).with(log_level) - Ohai::System.new + Ohai::System.new({ invoked_from_cli: true }) end it "resolves log_level when set to :auto" do expect(Ohai::Log).to receive(:level=).with(:info) - Ohai::System.new + Ohai::System.new({ invoked_from_cli: true }) end - end - context "after first time configuration" do - before { allow(Ohai::Log).to receive(:configured?).and_return(true) } + context "when called externally" do + it "does not configure logging" do + log_level = :debug + Ohai.config[:log_level] = log_level + expect(Ohai::Log).not_to receive(:level=).with(log_level) + Ohai::System.new() + end - it "configures logging" do - expect(Ohai::Log).not_to receive(:init).with(Ohai.config[:log_location]) - Ohai::System.new + it "does not resolve log_level when set to :auto" do + expect(Ohai::Log).not_to receive(:level=).with(:info) + Ohai::System.new() + end end end end |