summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerdar Sutay <serdar@opscode.com>2014-01-16 12:55:35 -0800
committerSerdar Sutay <serdar@opscode.com>2014-01-16 12:55:35 -0800
commitfc72e6616111b5e9662e0c5cec30de6f051d06f4 (patch)
treee9c3fdc1f1645a0d9035e91e43bd2500a8480005
parentf53bda7e7ef7df46eb3bf82c1cbc89d6c53a6061 (diff)
parentef7be56ba815223699cd2bc60e91e86dd65c2531 (diff)
downloadohai-fc72e6616111b5e9662e0c5cec30de6f051d06f4.tar.gz
Merge pull request #278 from opscode/OHAI-546
OHAI-546: cannot print specified attributes on the command line
-rw-r--r--lib/ohai/provides_map.rb24
-rw-r--r--spec/unit/provides_map_spec.rb9
-rw-r--r--spec/unit/system_spec.rb69
3 files changed, 96 insertions, 6 deletions
diff --git a/lib/ohai/provides_map.rb b/lib/ohai/provides_map.rb
index c2da2124..18a76d25 100644
--- a/lib/ohai/provides_map.rb
+++ b/lib/ohai/provides_map.rb
@@ -60,19 +60,35 @@ module Ohai
plugins.uniq
end
- # gather plugins providing each of the attributes listed along
- # with providers of subattributes
+ # This function is used to fetch the plugins for the attributes specified
+ # in the CLI options to Ohai.
+ # It first attempts to find the plugins for the attributes
+ # or the sub attributes given.
+ # If it can't find any, it looks for plugins that might
+ # provide the parents of a given attribute and returns the
+ # first parent found.
def deep_find_providers_for(attributes)
plugins = []
attributes.each do |attribute|
attrs = select_subtree(@map, attribute)
- raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'" unless attrs
+
+ unless attrs
+ attrs = select_closest_subtree(@map, attribute)
+
+ unless attrs
+ raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'"
+ end
+ end
+
collect_plugins_in(attrs, plugins)
end
+
plugins.uniq
end
- # gather plugins providing each of the attributes listed, or the
+ # This function is used to fetch the plugins from
+ # 'depends "languages"' statements in plugins.
+ # It gathers plugins providing each of the attributes listed, or the
# plugins providing the closest parent attribute
def find_closest_providers_for(attributes)
plugins = []
diff --git a/spec/unit/provides_map_spec.rb b/spec/unit/provides_map_spec.rb
index 3db88d21..9fa9a8f5 100644
--- a/spec/unit/provides_map_spec.rb
+++ b/spec/unit/provides_map_spec.rb
@@ -116,20 +116,25 @@ describe Ohai::ProvidesMap do
end
end
- describe "when looking for attribute and subattribute providers" do
+ describe "when looking for providers of attributes specified in CLI" do
before do
provides_map.set_providers_for(plugin_1, ["cat/whiskers"])
provides_map.set_providers_for(plugin_2, ["cat/paws", "cat/paws/toes"])
provides_map.set_providers_for(plugin_3, ["cat/mouth/teeth"])
end
- it "should find providers for subattributes even when the attribute doesn't have a provider" do
+ it "should find providers for subattributes if any exists when the attribute doesn't have a provider" do
providers = provides_map.deep_find_providers_for(["cat"])
expect(providers).to have(3).plugins
expect(providers).to include(plugin_1)
expect(providers).to include(plugin_2)
expect(providers).to include(plugin_3)
end
+
+ it "should find providers for the first parent attribute when the attribute or any subattributes doesn't have a provider" do
+ providers = provides_map.deep_find_providers_for(["cat/paws/front"])
+ expect(providers).to eq([plugin_2])
+ end
end
describe "when looking for the closest providers" do
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index ff5dcbbf..0ab66ab4 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -619,4 +619,73 @@ EOF
end
end
+
+ describe "when running ohai for specific attributes" do
+ when_plugins_directory "contains v7 plugins" do
+ with_plugin("languages.rb", <<-E)
+ Ohai.plugin(:Languages) do
+ provides 'languages'
+
+ collect_data do
+ languages Mash.new
+ end
+ end
+ E
+
+ with_plugin("english.rb", <<-E)
+ Ohai.plugin(:English) do
+ provides 'languages/english'
+
+ depends 'languages'
+
+ collect_data do
+ languages[:english] = Mash.new
+ languages[:english][:version] = 2014
+ end
+ end
+ E
+
+ with_plugin("french.rb", <<-E)
+ Ohai.plugin(:French) do
+ provides 'languages/french'
+
+ depends 'languages'
+
+ collect_data do
+ languages[:french] = Mash.new
+ languages[:french][:version] = 2012
+ end
+ end
+ E
+
+ before do
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
+ end
+
+ after do
+ Ohai::Config[:plugin_path] = @original_config
+ end
+
+ it "should run all the plugins when a top level attribute is specified" do
+ ohai.all_plugins("languages")
+ ohai.data[:languages][:english][:version].should == 2014
+ ohai.data[:languages][:french][:version].should == 2012
+ end
+
+ it "should run the first parent when a non-existent child is specified" do
+ ohai.all_plugins("languages/english/version")
+ ohai.data[:languages][:english][:version].should == 2014
+ ohai.data[:languages][:french].should be_nil
+ end
+
+ it "should be able to run multiple plugins" do
+ ohai.all_plugins(["languages/english", "languages/french"])
+ ohai.data[:languages][:english][:version].should == 2014
+ ohai.data[:languages][:french][:version].should == 2012
+ end
+
+ end
+ end
+
end