diff options
author | Claire McQuin <claire@getchef.com> | 2015-08-24 13:06:54 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2015-08-24 13:52:49 -0700 |
commit | 4cbe4c84886e8b21c2afd44c6cc30a084d2d81eb (patch) | |
tree | 3bc4c3a75c6ca711697740cb3d610200ebde2fb4 | |
parent | 3fe8850e66d5a95e47f2f14471eb22648b43bbef (diff) | |
download | ohai-4cbe4c84886e8b21c2afd44c6cc30a084d2d81eb.tar.gz |
configuration plugin dsl
-rw-r--r-- | lib/ohai/dsl/plugin/versionvii.rb | 41 | ||||
-rw-r--r-- | spec/unit/dsl/plugin_spec.rb | 73 |
2 files changed, 114 insertions, 0 deletions
diff --git a/lib/ohai/dsl/plugin/versionvii.rb b/lib/ohai/dsl/plugin/versionvii.rb index fdf7fd7c..2e24322b 100644 --- a/lib/ohai/dsl/plugin/versionvii.rb +++ b/lib/ohai/dsl/plugin/versionvii.rb @@ -100,6 +100,47 @@ module Ohai def require_plugin(*args) Ohai::Log.warn("[UNSUPPORTED OPERATION] \'require_plugin\' is no longer supported. Please use \'depends\' instead.\nIgnoring plugin(s) #{args.join(", ")}") end + + def configuration(option, *options) + return nil if plugin_config.nil? || !plugin_config.key?(option) + value = plugin_config[option] + options.each do |opt| + return nil unless value.key?(opt) + value = value[opt] + end + value + end + + private + def plugin_config + @plugin_config ||= fetch_plugin_config + end + + def fetch_plugin_config + # DMI => ["DMI"] + # Memory => ["", "Memory"] + # NetworkListeners => ["", "Network", "", "Listeners"] + # SSHHostKey => ["SSH", "Host", "", "Key"] + parts = self.name.to_s.split(/([A-Z][a-z]+)/) + # ["DMI"] => ["DMI"] + # ["", "Memory"] => ["Memory"] + # ["", "Network", "", "Listeners"] => ["Network", "Listeners"] + # ["SSH", "Host", "", "Key"] => ["SSH", "Host", "Key"] + parts.delete_if { |part| part.empty? } + # ["DMI"] => :dmi + # ["Memory"] => :memory + # ["Network", "Listeners"] => :network_listeners + # ["SSH", "Host", "Key"] => :ssh_host_key + snake_case_name = parts.map { |part| part.downcase }.join("_").to_sym + + # Plugin names in config hashes are auto-vivified, so we check with + # key? to avoid falsely instantiating a configuration hash. + if Ohai.config[:plugin].key?(snake_case_name) + Ohai.config[:plugin][snake_case_name] + else + nil + end + end end end end diff --git a/spec/unit/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb index dc4c1681..fa2a7379 100644 --- a/spec/unit/dsl/plugin_spec.rb +++ b/spec/unit/dsl/plugin_spec.rb @@ -293,6 +293,79 @@ describe Ohai::DSL::Plugin::VersionVII do end end + describe "#configuration" do + let(:plugin) do + klass = Ohai.plugin(camel_name) { } + klass.new({}) + end + + shared_examples_for "plugin config lookup" do + it "returns the configuration option value" do + Ohai.config[:plugin][snake_name][:foo] = true + expect(plugin.configuration(:foo)).to eq(true) + end + end + + describe "a plugin named Abc" do + let(:camel_name) { :Abc } + let(:snake_name) { :abc } + + it "returns nil when the plugin is not configured" do + expect(plugin.configuration(:foo)).to eq(nil) + end + + it "does not auto-vivify an un-configured plugin" do + plugin.configuration(:foo) + expect(Ohai.config[:plugin]).to_not have_key(:test) + end + + it "returns nil when the option is not configured" do + Ohai.config[:plugin][snake_name][:foo] = true + expect(plugin.configuration(:bar)).to eq(nil) + end + + it "returns nil when the suboption is not configured" do + Ohai.config[:plugin][snake_name][:foo] = { } + expect(plugin.configuration(:foo, :bar)).to eq(nil) + end + + include_examples "plugin config lookup" + + it "returns the configuration sub-option value" do + Ohai.config[:plugin][snake_name][:foo] = { :bar => true } + expect(plugin.configuration(:foo, :bar)).to eq(true) + end + end + + describe "a plugin named ABC" do + let(:camel_name) { :ABC } + let(:snake_name) { :abc } + + include_examples "plugin config lookup" + end + + describe "a plugin named Abc2" do + let(:camel_name) { :Abc2 } + let(:snake_name) { :abc_2 } + + include_examples "plugin config lookup" + end + + describe "a plugin named AbcAbc" do + let(:camel_name) { :AbcXyz } + let(:snake_name) { :abc_xyz } + + include_examples "plugin config lookup" + end + + describe "a plugin named ABCLmnoXyz" do + let(:camel_name) { :ABCLmnoXyz } + let(:snake_name) { :abc_lmno_xyz } + + include_examples "plugin config lookup" + end + end + it_behaves_like "Ohai::DSL::Plugin" do let(:ohai) { Ohai::System.new } let(:plugin) { Ohai::DSL::Plugin::VersionVII.new(ohai.data) } |