summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@getchef.com>2015-07-20 13:24:51 -0700
committerClaire McQuin <claire@getchef.com>2015-07-24 09:57:14 -0700
commit68630cdda403271384f6af38bf3af91e2a53a862 (patch)
treef3baca79fbd8314bc6b3b25e22b3b66906870073
parent6819498a6cec34f48611b92aa1a1998c5587b12c (diff)
downloadohai-mcquin/ohai-config/load-config.tar.gz
Allow system to accept configuration options and configure ohai on initializemcquin/ohai-config/load-config
-rw-r--r--lib/ohai/application.rb13
-rw-r--r--lib/ohai/system.rb23
-rw-r--r--spec/functional/application_spec.rb11
-rw-r--r--spec/unit/application_spec.rb69
-rw-r--r--spec/unit/system_spec.rb69
5 files changed, 92 insertions, 93 deletions
diff --git a/lib/ohai/application.rb b/lib/ohai/application.rb
index 113b9c82..a4b34699 100644
--- a/lib/ohai/application.rb
+++ b/lib/ohai/application.rb
@@ -73,7 +73,6 @@ class Ohai::Application
def run
configure_ohai
- configure_logging
run_application
end
@@ -82,20 +81,10 @@ class Ohai::Application
@attributes = nil if @attributes.empty?
load_workstation_config
- Ohai::Config.merge_deprecated_config
- Ohai.config.merge!(config)
- if Ohai.config[:directory]
- Ohai.config[:plugin_path] << Ohai.config[:directory]
- end
- end
-
- def configure_logging
- Ohai::Log.init(Ohai.config[:log_location])
- Ohai::Log.level = Ohai.config[:log_level]
end
def run_application
- ohai = Ohai::System.new
+ ohai = Ohai::System.new(config)
ohai.all_plugins(@attributes)
if @attributes
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index c98ac1af..d733191f 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -34,23 +34,22 @@ module Ohai
include Ohai::Mixin::ConstantHelper
attr_accessor :data
+ attr_reader :config
attr_reader :provides_map
attr_reader :v6_dependency_solver
- def initialize
+ def initialize(config = {})
@plugin_path = ""
+ @config = config
reset_system
end
def reset_system
@data = Mash.new
@provides_map = ProvidesMap.new
-
@v6_dependency_solver = Hash.new
- # configure logging
- Ohai::Log.init(Ohai.config[:log_location])
- Ohai::Log.level = Ohai.config[:log_level]
+ configure_ohai
@loader = Ohai::Loader.new(self)
@runner = Ohai::Runner.new(self, true)
@@ -204,5 +203,19 @@ module Ohai
raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{data.class}!"
end
end
+
+ private
+ def configure_ohai
+ Ohai::Config.merge_deprecated_config
+ Ohai.config.merge!(@config)
+
+ if Ohai.config[:directory] &&
+ !Ohai.config[:plugin_path].include?(Ohai::Config[:directory])
+ Ohai.config[:plugin_path] << Ohai.config[:directory]
+ end
+
+ Ohai::Log.init(Ohai.config[:log_location])
+ Ohai::Log.level = Ohai.config[:log_level]
+ end
end
end
diff --git a/spec/functional/application_spec.rb b/spec/functional/application_spec.rb
index 85b938dc..33992f5d 100644
--- a/spec/functional/application_spec.rb
+++ b/spec/functional/application_spec.rb
@@ -112,23 +112,16 @@ CONFIG
let(:plugin_path) { '/path/to/plugins' }
- it 'logs warnings for deprecated options and merges with Ohai.config' do
+ it 'logs warnings for deprecated top-level options' do
# deprecation warnings for options need to be stubbed in the order
# they are received, in this case it's the order they appear in the
# configuration file.
- options = [ :log_location, :log_level, :disabled_plugins, :plugin_path ]
+ options = [ :log_location, :log_level, :disabled_plugins ]
options.each do |option|
expect(Ohai::Log).to receive(:warn).
with(/Ohai::Config\[:#{option}\] is deprecated/)
end
app.configure_ohai
- options.each do |option|
- if option == :plugin_path
- expect(Ohai.config[option]).to include(self.send(option))
- else
- expect(Ohai.config[option]).to eq(self.send(option))
- end
- end
end
end
diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb
index 4dfdc24d..3fbb5a2e 100644
--- a/spec/unit/application_spec.rb
+++ b/spec/unit/application_spec.rb
@@ -34,17 +34,7 @@ RSpec.describe 'Ohai::Application' do
ARGV.replace(@original_argv)
end
- def stub_fatal!(expected_message)
- expect(STDERR).to receive(:puts).with(expected_message)
- expect(Ohai::Log).to receive(:fatal).with(expected_message)
- end
-
describe '#configure_ohai' do
- it 'merges deprecated config settings into the ohai config context' do
- expect(Ohai::Config).to receive(:merge_deprecated_config)
- app.configure_ohai
- end
-
describe 'loading configuration from a file' do
let(:config_file) { '/local/workstation/config' }
let(:config_loader) { instance_double('ChefConfig::WorkstationConfigLoader') }
@@ -65,28 +55,15 @@ RSpec.describe 'Ohai::Application' do
end
it 'loads the configuration file' do
- expect(config_loader).to receive(:path_exists?).
- with(config_file).
- and_return(true)
- expect(config_loader).to receive(:config_location).
- and_return(config_file)
- expect(Ohai::Config).to receive(:from_file).
- with(config_file)
+ expect(config_loader).to receive(:load)
app.configure_ohai
end
context 'when the configuration file does not exist' do
- let(:expected_message) { Regexp.new("#{config_file} does not exist") }
-
it 'terminates the application' do
- expect(config_loader).to receive(:path_exists?).
- with(config_file).
- and_return(false)
- expect(Ohai::Application).to receive(:fatal!).
- with(expected_message).
- and_call_original
- stub_fatal!(expected_message)
- expect { app.configure_ohai }.to raise_error(SystemExit)
+ expect(config_loader).to receive(:load).and_raise(ChefConfig::ConfigurationError)
+ expect(Ohai::Application).to receive(:fatal!)
+ app.configure_ohai
end
end
end
@@ -99,10 +76,7 @@ RSpec.describe 'Ohai::Application' do
end
it 'loads the configuration file' do
- expect(config_loader).to receive(:config_location).
- and_return(config_file)
- expect(Ohai::Config).to receive(:from_file).
- with(config_file)
+ expect(config_loader).to receive(:load)
app.configure_ohai
end
end
@@ -117,40 +91,7 @@ RSpec.describe 'Ohai::Application' do
with(/Ohai::Config\[:directory\] is deprecated/)
app.configure_ohai
end
-
- it 'merges CLI options into the ohai config context' do
- app.configure_ohai
- expect(Ohai.config[:directory]).to eq(directory)
- end
- end
-
- context 'when directory is configured' do
- let(:directory) { '/some/fantastic/plugins' }
-
- shared_examples_for 'directory' do
- it 'adds directory to plugin_path' do
- app.configure_ohai
- expect(Ohai.config[:plugin_path]).to include(directory)
- end
- end
-
- context 'in a configuration file' do
- before do
- allow(Ohai::Log).to receive(:warn).
- with(/Ohai::Config\[:directory\] is deprecated/)
- Ohai::Config[:directory] = directory
- end
-
- include_examples 'directory'
- end
-
- context 'as a command line option' do
- let(:argv) { ['-d', directory] }
-
- include_examples 'directory'
- end
end
end
-
end
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index 0eb7f439..1d6d65f5 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -26,17 +26,80 @@ describe "Ohai::System" do
let(:ohai) { Ohai::System.new }
describe "#initialize" do
- it "should return an Ohai::System object" do
+ it "returns an Ohai::System object" do
expect(ohai).to be_a_kind_of(Ohai::System)
end
- it "should set @attributes to a ProvidesMap" do
+ it "sets @attributes to a ProvidesMap" do
expect(ohai.provides_map).to be_a_kind_of(Ohai::ProvidesMap)
end
- it "should set @v6_dependency_solver to a Hash" do
+ it "sets @v6_dependency_solver to a Hash" do
expect(ohai.v6_dependency_solver).to be_a_kind_of(Hash)
end
+
+ it 'merges deprecated config settings into the ohai config context' do
+ expect(Ohai::Log).to receive(:warn).
+ with(/Ohai::Config\[:disabled_plugins\] is deprecated/)
+ Ohai::Config[:disabled_plugins] = [ :Foo, :Baz ]
+ expect(Ohai::Config).to receive(:merge_deprecated_config).
+ and_call_original
+ Ohai::System.new
+ expect(Ohai.config[:disabled_plugins]).to eq([ :Foo, :Baz ])
+ end
+
+ it 'merges provided configuration options into the ohai config context' do
+ config = {
+ 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)
+ end
+ end
+
+ context 'when directory is configured' do
+ let(:directory) { '/some/fantastic/plugins' }
+
+ it 'adds directory to plugin_path' do
+ Ohai.config[:directory] = directory
+ Ohai::System.new
+ expect(Ohai.config[:plugin_path]).to include(directory)
+ end
+ end
+
+ shared_examples_for 'appendable deprecated configuration option' do
+ it 'logs a warning and configures the option on the ohai config context' do
+ Ohai::Config[option] << value
+ expect(Ohai::Log).to receive(:warn).
+ with(/Ohai::Config\[:#{option}\] is deprecated/)
+ Ohai::System.new
+ expect(Ohai.config[option]).to include(value)
+ end
+ end
+
+ context 'when a top-level hints_path is configured' do
+ include_examples 'appendable deprecated configuration option' do
+ let(:option) { :hints_path }
+ let(:value) { '/path/to/hints' }
+ end
+ end
+
+ context 'when a top-level plugin_path is configured' do
+ include_examples 'appendable deprecated configuration option' do
+ let(:option) { :plugin_path }
+ let(:value) { '/path/to/plugins' }
+ end
+ end
+
+ it 'configures logging' do
+ expect(Ohai::Log).to receive(:init).with(Ohai.config[:log_location])
+ expect(Ohai::Log).to receive(:level=).with(Ohai.config[:log_level])
+ Ohai::System.new
+ end
end
when_plugins_directory "contains v6 and v7 plugins" do