summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@opscode.com>2013-12-16 10:46:29 -0800
committerClaire McQuin <claire@opscode.com>2013-12-17 14:32:23 -0800
commitb8caf44c183c4f0d3ca3e6b66ea2bd5ed8810d89 (patch)
treec717c0e81be94dfa0fe53615cd95f991468e389b
parent723b746c6d1fe868afc26312b6db91de34db5b3e (diff)
downloadohai-b8caf44c183c4f0d3ca3e6b66ea2bd5ed8810d89.tar.gz
move lookup of parent providers for attributes from runner to provides_map
-rw-r--r--lib/ohai/provides_map.rb17
-rw-r--r--lib/ohai/runner.rb39
-rw-r--r--spec/unit/provides_map_spec.rb52
-rw-r--r--spec/unit/runner_spec.rb31
4 files changed, 65 insertions, 74 deletions
diff --git a/lib/ohai/provides_map.rb b/lib/ohai/provides_map.rb
index 2981e071..15fa939a 100644
--- a/lib/ohai/provides_map.rb
+++ b/lib/ohai/provides_map.rb
@@ -51,14 +51,26 @@ module Ohai
end
end
- def find_providers_for(attributes)
+ # inherit = false => only look up providers for a listed
+ # attribute, don't look for parent providers
+ # inherit = true => looks up parent attribute providers if no
+ # providers for a listed attribute are found first
+ def find_providers_for(attributes, inherit = false)
plugins = []
attributes.each do |attribute|
attrs = map
parts = attribute.split('/')
parts.each do |part|
+ # TODO: pretty sure we can remove this line, below
next if part == Ohai::Mixin::OS.collect_os
- raise Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute \'#{attribute}\'" unless attrs[part]
+ unless attrs[part]
+ # this attribute does not exist in the map.
+ # raise an error if inherit == false (we aren't looking up
+ # parent providers) or attrs[parts[0]] doesn't exist (in
+ # that case, no parents to look for)
+ raise Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute \'#{attribute}\'" unless inherit && attrs != map
+ break # otherwise (inherit == true and we have the deepest parent of this attribute)
+ end
attrs = attrs[part]
end
plugins += collect_plugins_in(attrs, [])
@@ -66,7 +78,6 @@ module Ohai
plugins.uniq
end
-
def all_plugins(attribute_filter=nil)
if attribute_filter.nil?
collected = []
diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb
index f1726c02..8c7eca1a 100644
--- a/lib/ohai/runner.rb
+++ b/lib/ohai/runner.rb
@@ -85,45 +85,8 @@ module Ohai
end
end
- # returns a list of plugins which provide the given attributes
def fetch_plugins(attributes)
- plugins = []
- # subattribute_regex matches the lowest subattribute of any
- # given attribute. if the attribute is 'attr/sub1/sub2', then
- # subattribute_regex matches '/sub2'. if the attribute is 'attr'
- # then subattribute_regex matches nothing.
- subattribute_regex = Regexp.new("/[^/]+$")
- attributes.each do |attribute|
- partial_attribute = attribute
- # look for providers until 1) we find some, or 2) we've
- # exhaused our search (the highest-level of the attribute
- # doesn't return any results
- while (found_providers = safe_find_providers_for(partial_attribute)).empty?
- md = subattribute_regex.match(partial_attribute)
- # since md doesn't match the highest-level attribute, if md
- # is nil, then we can't look any higher. this attribute
- # really does not exist.
- raise Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing #{attribute}" unless md
- # remove the lowest-level subattribute and look again
- # don't use chomp! affects attribute, too.
- partial_attribute = partial_attribute.chomp(md[0])
- end
- plugins << found_providers
- plugins.flatten!
- end
- plugins.uniq
- end
-
- # "safely" finds providers for a single attribute. by "safe", if
- # there are no plugins providing the attribute, return an empty
- # array to indicate this (don't raise
- # Ohai::Exceptions::AttributeNotFound error)
- def safe_find_providers_for(attribute)
- begin
- @provides_map.find_providers_for([attribute])
- rescue Ohai::Exceptions::AttributeNotFound
- return []
- end
+ @provides_map.find_providers_for(attributes, true)
end
# Given a list of plugins and the first plugin in the cycle,
diff --git a/spec/unit/provides_map_spec.rb b/spec/unit/provides_map_spec.rb
index 26552772..fbc026fe 100644
--- a/spec/unit/provides_map_spec.rb
+++ b/spec/unit/provides_map_spec.rb
@@ -29,6 +29,16 @@ describe Ohai::ProvidesMap do
let(:plugin_4) { Ohai::DSL::Plugin.new(ohai_system.data) }
describe "when looking up providing plugins for a single attribute" do
+ describe "when no plugin provides the attribute" do
+ it "should raise Ohai::Exceptions::AttributeNotFound error, with inherit = false" do
+ expect{ provides_map.find_providers_for(["single"]) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute 'single'")
+ end
+
+ it "should raise Ohai::Exceptions::AttributeNotFound error, with inherit = true" do
+ expect{ provides_map.find_providers_for(["single"], true) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute 'single'")
+ end
+ end
+
describe "when only one plugin provides the attribute" do
before do
provides_map.set_providers_for(plugin_1, ["single"])
@@ -78,12 +88,45 @@ describe Ohai::ProvidesMap do
end
describe "when looking up providers for multi-level attributes" do
- before do
- provides_map.set_providers_for(plugin_1, ["top/middle/bottom"])
+ describe "when the full attribute exists in the map" do
+ before do
+ provides_map.set_providers_for(plugin_1, ["top/middle/bottom"])
+ end
+
+ it "should collect the provider" do
+ expect(provides_map.find_providers_for(["top/middle/bottom"])).to eq([plugin_1])
+ end
end
- it "should collect the provider" do
- expect(provides_map.find_providers_for(["top/middle/bottom"])).to eq([plugin_1])
+ describe "when the full attribute doesn't exist in the map" do
+ context "and inherit = false" do
+ before do
+ provides_map.set_providers_for(plugin_1, ["top/middle"])
+ end
+
+ it "should raise Ohai::Exceptions::AttributeNotFound error" do
+ expect{ provides_map.find_providers_for(["top/middle/bottom"]) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute 'top/middle/bottom'")
+ end
+ end
+
+ context "and inherit = true" do
+ before do
+ provides_map.set_providers_for(plugin_1, ["top"])
+ provides_map.set_providers_for(plugin_2, ["top/middle"])
+ end
+
+ it "should not raise error" do
+ expect{ provides_map.find_providers_for(["top/middle/bottom"], true) }.not_to raise_error
+ end
+
+ it "should return the most specific parent provider" do
+ expect(provides_map.find_providers_for(["top/middle/bottom"], true)).to eq([plugin_2])
+ end
+
+ it "should raise Ohai::Exceptions::AttributeNotFound error if no parent exists" do
+ expect{ provides_map.find_providers_for(["one/two/three"], true) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute 'one/two/three'")
+ end
+ end
end
it "should collect the provider" do
@@ -100,7 +143,6 @@ describe Ohai::ProvidesMap do
end
it "should find all the plugins providing attributes" do
-
all_plugins = provides_map.all_plugins
expect(all_plugins).to have(4).plugins
expect(all_plugins).to include(plugin_1)
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb
index 0fb65283..36c0c0d8 100644
--- a/spec/unit/runner_spec.rb
+++ b/spec/unit/runner_spec.rb
@@ -381,31 +381,6 @@ describe Ohai::Runner, "run_plugin" do
end
end
-describe Ohai::Runner, "safe_find_providers_for" do
- before(:each) do
- @provides_map = Ohai::ProvidesMap.new
- @data = Mash.new
- @ohai = double('Ohai::System', :data => @data, :provides_map => @provides_map)
- @runner = Ohai::Runner.new(@ohai, true)
- end
-
- it "should return an empty array if there are no providers for an attribute" do
- # nothing in the provides map
- @runner.safe_find_providers_for("false/attribute").should be_empty
- end
-
- it "should not raise an error if there are no providers for an attribute" do
- # nothing in the provides map
- expect{ @runner.safe_find_providers_for("false/attribute") }.not_to raise_error
- end
-
- it "should return the providers for an attribute that exists" do
- plugin = Ohai::DSL::Plugin.new(@ohai, "tmp/plugins/real.rb")
- @provides_map.set_providers_for(plugin, ["real/attribute"])
- @runner.safe_find_providers_for("real/attribute").should eql([plugin])
- end
-end
-
describe Ohai::Runner, "fetch_plugins" do
before(:each) do
@provides_map = Ohai::ProvidesMap.new
@@ -425,7 +400,7 @@ describe Ohai::Runner, "fetch_plugins" do
describe "when the attribute is not provided by any plugin" do
describe "and some parent attribute has providers" do
it "should return the providers for the parent" do
- plugin = Ohai::DSL::Plugin.new(@ohai, "tmp/plugins/test.rb")
+ plugin = Ohai::DSL::Plugin.new(@ohai.data)
@provides_map.set_providers_for(plugin, ["test/attribute"])
@runner.fetch_plugins(["test/attribute/too_far"]).should eql([plugin])
end
@@ -434,13 +409,13 @@ describe Ohai::Runner, "fetch_plugins" do
describe "and no parent attribute has providers" do
it "should raise Ohai::Exceptions::AttributeNotFound exception" do
# provides map is empty
- expect{ @runner.fetch_plugins(["false/attribute"]) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing false/attribute")
+ expect{ @runner.fetch_plugins(["false/attribute"]) }.to raise_error(Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute 'false/attribute'")
end
end
end
it "should return unique providers" do
- plugin = Ohai::DSL::Plugin.new(@ohai, "tmp/plugins/test.rb")
+ plugin = Ohai::DSL::Plugin.new(@ohai.data)
@provides_map.set_providers_for(plugin, ["test", "test/too_far/way_too_far"])
@runner.fetch_plugins(["test", "test/too_far/way_too_far"]).should eql([plugin])
end