diff options
author | Claire McQuin <claire@getchef.com> | 2015-08-19 11:49:23 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2015-08-19 11:49:23 -0700 |
commit | 5721e0e88d97f5f1aa54a94aecd58976b7e0fc1c (patch) | |
tree | 3b6c753af4fc5aa4e5baeba3c48d1b64562f8b74 | |
parent | 5440b503ebb0cfb560883a03fbb3277d41a64ea1 (diff) | |
download | ohai-5721e0e88d97f5f1aa54a94aecd58976b7e0fc1c.tar.gz |
Add configuration DSL method to v7 plugins.mcquin/ohai-config/dot-plugin
-rw-r--r-- | lib/ohai/dsl/plugin/versionvii.rb | 20 | ||||
-rw-r--r-- | spec/unit/dsl/plugin_spec.rb | 63 |
2 files changed, 83 insertions, 0 deletions
diff --git a/lib/ohai/dsl/plugin/versionvii.rb b/lib/ohai/dsl/plugin/versionvii.rb index fdf7fd7c..60a065fc 100644 --- a/lib/ohai/dsl/plugin/versionvii.rb +++ b/lib/ohai/dsl/plugin/versionvii.rb @@ -93,6 +93,15 @@ module Ohai end end + def configuration(option) + config_name = snake_case_name + if Ohai.config[:plugin].key?(config_name) + Ohai.config[:plugin][config_name][option] + else + nil + end + end + def provides(*paths) Ohai::Log.warn("[UNSUPPORTED OPERATION] \'provides\' is no longer supported in a \'collect_data\' context. Please specify \'provides\' before collecting plugin data. Ignoring command \'provides #{paths.join(", ")}") end @@ -100,6 +109,17 @@ 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 + + private + def snake_case_name + # DMI => ["DMI"] + # Memory => ["", "Memory"] + # NetworkListeners => ["", "Network", "", "Listeners"] + # SSHHostKey => ["SSH", "Host", "", "Key"] + parts = self.name.to_s.split(/([A-Z][a-z]+)/) + parts.delete_if { |part| part.empty? } + parts.map { |part| part.downcase }.join("_").to_sym + end end end end diff --git a/spec/unit/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb index dc4c1681..f5150bc4 100644 --- a/spec/unit/dsl/plugin_spec.rb +++ b/spec/unit/dsl/plugin_spec.rb @@ -277,6 +277,69 @@ describe Ohai::DSL::Plugin::VersionVII do end end + describe "#configuration" do + shared_examples "plugin config lookup" do + let(:plugin) do + klass = Ohai.plugin(camel_name.to_sym) { } + klass.new({}) + end + + context "when the plugin is configured" do + before do + Ohai.config[:plugin][snake_name] = {} + end + + it "returns the configuration value when set" do + Ohai.config[:plugin][snake_name][:foo] = true + expect(plugin.configuration(:foo)).to be true + end + + it "returns nil when unset" do + expect(plugin.configuration(:foo)).to be_nil + end + end + + context "when the plugin is not configured" do + it "returns nil" do + expect(plugin.configuration(:foo)).to be_nil + end + + it "does not add plugin key to configuration" do + plugin.configuration(:foo) + expect(Ohai.config[:plugin]).to_not have_key(snake_name) + end + end + end + + describe "plugins named like Memory" do + include_examples "plugin config lookup" do + let(:camel_name) { "Memory" } + let(:snake_name) { :memory } + end + end + + describe "plugins named like DMI" do + include_examples "plugin config lookup" do + let(:camel_name) { "DMI" } + let(:snake_name) { :dmi } + end + end + + describe "plugins named like NetworkListeners" do + include_examples "plugin config lookup" do + let(:camel_name) { "NetworkListeners" } + let(:snake_name) { :network_listeners } + end + end + + describe "plugins named SSHHostKey" do + include_examples "plugin config lookup" do + let(:camel_name) { "SSHHostKey" } + let(:snake_name) { :ssh_host_key } + end + end + end + describe "#provides (deprecated)" do it "should log a warning" do plugin = Ohai::DSL::Plugin::VersionVII.new(Mash.new) |