diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index 023f511a0..2b15cae3a 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -109,12 +109,15 @@ class Plugin def initialize(controller, source) @controller = controller - @attributes = controller.attributes @data = controller.data @source = source @has_run = false end + def provides_map + @controller.provides_map + end + def run @has_run = true run_plugin @@ -227,19 +230,7 @@ def self.collect_contents(contents) end def provides(*paths) - paths.each do |path| - parts = path.split("/") - a = @attributes - unless parts.length == 0 - parts.shift if parts[0].length == 0 - parts.each do |part| - a[part] ||= Mash.new - a = a[part] - end - end - a[:_plugins] ||= [] - a[:_plugins] << self - end + provides_map.set_providers_for(self, paths) end def require_plugin(*args) diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index 6428f9699..bd698fcc7 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -25,7 +25,10 @@ class Loader def initialize(controller) @controller = controller - @attributes = controller.attributes + end + + def provides_map + @controller.provides_map end # @note: plugin_name is used only by version 6 plugins and is the @@ -68,21 +71,7 @@ def load_plugin(plugin_path, plugin_name=nil) def collect_provides(plugin) plugin_provides = plugin.class.provides_attrs - - plugin_provides.each do |attr| - parts = attr.split('/') - a = @attributes - unless parts.length == 0 - parts.shift if parts[0].length == 0 - parts.each do |part| - a[part] ||= Mash.new - a = a[part] - end - end - - a[:_plugins] ||= [] - a[:_plugins] << plugin - end + provides_map.set_providers_for(plugin, plugin_provides) end end diff --git a/lib/ohai/provides_map.rb b/lib/ohai/provides_map.rb new file mode 100644 index 000000000..2f61f550e --- /dev/null +++ b/lib/ohai/provides_map.rb @@ -0,0 +1,90 @@ +# +# Author:: Adam Jacob () +# Author:: Daniel DeLeo () +# Copyright:: Copyright (c) 2008, 2013 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'ohai/mash' +require 'ohai/exception' +require 'ohai/mixin/os' +require 'ohai/dsl/plugin' + +module Ohai + class ProvidesMap + + attr_reader :map + + def initialize + @map = Mash.new + end + + def set_providers_for(plugin, provided_attributes) + unless plugin.kind_of?(Ohai::DSL::Plugin) + raise ArgumentError, "set_providers_for only accepts Ohai Plugin classes (got: #{plugin})" + end + provided_attributes.each do |attr| + parts = attr.split('/') + a = map + unless parts.length == 0 + parts.shift if parts[0].length == 0 + parts.each do |part| + a[part] ||= Mash.new + a = a[part] + end + end + + a[:_plugins] ||= [] + a[:_plugins] << plugin + end + end + + def find_providers_for(attributes) + plugins = [] + attributes.each do |attribute| + attrs = map + parts = attribute.split('/') + parts.each do |part| + next if part == Ohai::Mixin::OS.collect_os + raise Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute \'#{attribute}\'" unless attrs[part] + attrs = attrs[part] + end + plugins << attrs[:_plugins] + plugins.flatten! + end + plugins.uniq + end + + + def all_plugins + collected = [] + collect_plugins_in(map, collected).uniq + end + + private + + def collect_plugins_in(provides_map, collected) + provides_map.keys.each do |plugin| + if plugin.eql?("_plugins") + collected.concat(provides_map[plugin]) + else + collect_plugins_in(provides_map[plugin], collected) + end + end + collected + end + end +end + diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb index c16f0f54f..0ead9718d 100644 --- a/lib/ohai/runner.rb +++ b/lib/ohai/runner.rb @@ -21,10 +21,11 @@ module Ohai class Runner + # safe_run: set to true if this runner will run plugins in # safe-mode. default false. def initialize(controller, safe_run = false) - @attributes = controller.attributes + @provides_map = controller.provides_map @safe_run = safe_run end @@ -32,42 +33,33 @@ def initialize(controller, safe_run = false) # true, then this plugin and its dependencies will be run even if # they have been run before. def run_plugin(plugin, force = false) + unless plugin.kind_of?(Ohai::DSL::Plugin) + raise ArgumentError, "Invalid plugin #{plugin} (must be an Ohai::DSL::Plugin or subclass)" + end visited = [plugin] while !visited.empty? - p = visited.pop + next_plugin = visited.pop - next if p.has_run? unless force + next if next_plugin.has_run? unless force - if visited.include?(p) + if visited.include?(next_plugin) raise Ohai::Exceptions::DependencyCycle, "Dependency cycle detected. Please refer to the following plugins: #{get_cycle(visited, p).join(", ") }" end - dependency_providers = fetch_plugins(p.dependencies) - dependency_providers.delete_if { |plugin| (!force && plugin.has_run?) || plugin.eql?(p) } + dependency_providers = fetch_plugins(next_plugin.dependencies) + dependency_providers.delete_if { |dep_plugin| (!force && dep_plugin.has_run?) || dep_plugin.eql?(next_plugin) } if dependency_providers.empty? - @safe_run ? p.safe_run : p.run + @safe_run ? next_plugin.safe_run : next_plugin.run else - visited << p << dependency_providers.first + visited << next_plugin << dependency_providers.first end end end # returns a list of plugins which provide the given attributes def fetch_plugins(attributes) - plugins = [] - attributes.each do |attribute| - attrs = @attributes - parts = attribute.split('/') - parts.each do |part| - next if part == Ohai::Mixin::OS.collect_os - raise Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute \'#{attribute}\'" unless attrs[part] - attrs = attrs[part] - end - plugins << attrs[:_plugins] - plugins.flatten! - end - plugins.uniq + @provides_map.find_providers_for(attributes) end # given a list of plugins and the first plugin in the cycle, diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 02d413879..a5029c29c 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -25,20 +25,23 @@ require 'ohai/mixin/command' require 'ohai/mixin/os' require 'ohai/mixin/string' +require 'ohai/provides_map' require 'mixlib/shellout' require 'yajl' module Ohai + class System attr_accessor :data - attr_reader :attributes + attr_reader :provides_map attr_reader :hints attr_reader :v6_dependency_solver def initialize @data = Mash.new - @attributes = Mash.new + @provides_map = ProvidesMap.new + @hints = Hash.new @v6_dependency_solver = Hash.new @plugin_path = "" @@ -92,7 +95,8 @@ def run_plugins(safe = false, force = false) end # collect and run version 7 plugins - plugins = collect_plugins(@attributes) + plugins = @provides_map.all_plugins + begin plugins.each { |plugin| @runner.run_plugin(plugin, force) } rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle => e @@ -105,6 +109,7 @@ def run_plugins(safe = false, force = false) def collect_plugins(plugins) collected = [] if plugins.is_a?(Mash) + # TODO: remove this branch plugins.keys.each do |plugin| if plugin.eql?("_plugins") collected << plugins[plugin] diff --git a/spec/ohai/dsl/plugin_spec.rb b/spec/ohai/dsl/plugin_spec.rb index e51301edb..072e293a2 100644 --- a/spec/ohai/dsl/plugin_spec.rb +++ b/spec/ohai/dsl/plugin_spec.rb @@ -43,8 +43,7 @@ end context "when accessing data via method_missing" do - it "should take a missing method and store the method name as a key, with its arguments as value\ -s" do + it "should take a missing method and store the method name as a key, with its arguments as values" do plugin.guns_n_roses("chinese democracy") plugin.data["guns_n_roses"].should eql("chinese democracy") end @@ -223,7 +222,7 @@ describe "#provides (deprecated)" do it "should log a warning" do plugin = Ohai::DSL::Plugin::VersionVII.new(Ohai::System.new, "") - Ohai::Log.should_receive(:warn).with(/[UNSUPPORTED OPERATION]/) + Ohai::Log.should_receive(:warn).with(/\[UNSUPPORTED OPERATION\]/) plugin.provides("attribute") end end @@ -231,16 +230,16 @@ describe "#require_plugin (deprecated)" do it "should log a warning" do plugin = Ohai::DSL::Plugin::VersionVII.new(Ohai::System.new, "") - Ohai::Log.should_receive(:warn).with(/[UNSUPPORTED OPERATION]/) + Ohai::Log.should_receive(:warn).with(/\[UNSUPPORTED OPERATION\]/) plugin.require_plugin("plugin") end end it_behaves_like "Ohai::DSL::Plugin" do - let (:ohai) { Ohai::System.new } - let (:source) { "path/plugin.rb" } - let (:plugin) { Ohai::DSL::Plugin::VersionVII.new(ohai, source) } - let (:version) { :version7 } + let(:ohai) { Ohai::System.new } + let(:source) { "path/plugin.rb" } + let(:plugin) { Ohai::DSL::Plugin::VersionVII.new(ohai, source) } + let(:version) { :version7 } end end @@ -272,15 +271,15 @@ plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "") plugin.provides("attribute") - @ohai.attributes.should have_key(:attribute) + @ohai.provides_map.find_providers_for(["attribute"]).should eq([plugin]) end it "should collect a list of attributes" do plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "") plugin.provides("attr1", "attr2", "attr3") - [:attr1, :attr2, :attr3].each do |attr| - @ohai.attributes.should have_key(attr) + %w[attr1 attr2 attr3].each do |attr| + @ohai.provides_map.find_providers_for([attr]).should eq([plugin]) end end @@ -288,8 +287,7 @@ plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "") plugin.provides("attr/subattr") - @ohai.attributes.should have_key(:attr) - @ohai.attributes[:attr].should have_key(:subattr) + @ohai.provides_map.find_providers_for(["attr/subattr"]).should eq([plugin]) end it "should collect all unique providers for an attribute" do @@ -300,14 +298,14 @@ plugins << p end - @ohai.attributes[:attribute][:_plugins].should eql(plugins) + @ohai.provides_map.find_providers_for(["attribute"]).should =~ plugins end end it_behaves_like "Ohai::DSL::Plugin" do - let (:ohai) { Ohai::System.new } - let (:source) { "path/plugin.rb" } - let (:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, source) } - let (:version) { :version6 } + let(:ohai) { Ohai::System.new } + let(:source) { "path/plugin.rb" } + let(:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, source) } + let(:version) { :version6 } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 580ca9afb..1b245a89f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -117,6 +117,8 @@ def from_file(filename) RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true + config.filter_run :focus => true + config.filter_run_excluding :windows_only => true unless windows? config.filter_run_excluding :unix_only => true unless unix? config.filter_run_excluding :ruby_18_only => true unless ruby_18? @@ -124,6 +126,8 @@ def from_file(filename) config.filter_run_excluding :requires_root => true unless ENV['USER'] == 'root' config.filter_run_excluding :requires_unprivileged_user => true if ENV['USER'] == 'root' + config.run_all_when_everything_filtered = true + config.before :each do Ohai::Config.reset end diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb index 2a6602227..8587d721e 100644 --- a/spec/unit/loader_spec.rb +++ b/spec/unit/loader_spec.rb @@ -53,7 +53,7 @@ test Mash.new EOF IO.stub(:read).with(@path).and_return(contents) - Ohai::Log.should_receive(:warn).with(/[DEPRECATION]/) + Ohai::Log.should_receive(:warn).with(/\[DEPRECATION\]/) plugin = @loader.load_plugin(@path, @v6name) plugin.version.should eql(:version6) end @@ -89,7 +89,7 @@ end EOF IO.stub(:read).with(@path).and_return(contents) - Ohai::Log.should_receive(:warn).with(/[UNSUPPORTED OPERATION]/) + Ohai::Log.should_receive(:warn).with(/\[UNSUPPORTED OPERATION\]/) @loader.load_plugin(@path) end end @@ -105,15 +105,14 @@ klass = Ohai.plugin(@name) { provides("attr") } plugin = klass.new(@ohai, @path) @loader.collect_provides(plugin) - @ohai.attributes.should have_key(:attr) + @ohai.provides_map.find_providers_for(["attr"]).should eq([plugin]) end it "should add provided subattributes to Ohai" do klass = Ohai.plugin(@name) { provides("attr/sub") } plugin = klass.new(@ohai, @plath) @loader.collect_provides(plugin) - @ohai.attributes.should have_key(:attr) - @ohai.attributes[:attr].should have_key(:sub) + @ohai.provides_map.find_providers_for([ "attr/sub" ]).should include(plugin) end it "should collect the unique providers for an attribute" do @@ -126,7 +125,7 @@ end plugins.each { |plugin| @loader.collect_provides(plugin) } - @ohai.attributes[:attr][:_plugins].should eql(plugins) + @ohai.provides_map.find_providers_for(["attr"]).should =~ plugins end end end diff --git a/spec/unit/plugins/fail_spec.rb b/spec/unit/plugins/fail_spec.rb index 6603bcd37..de77c6f23 100644 --- a/spec/unit/plugins/fail_spec.rb +++ b/spec/unit/plugins/fail_spec.rb @@ -55,7 +55,8 @@ it "should not have attribute keys" do @loader.load_plugin("#{tmp}/plugins/fail.rb") - @ohai.attributes.should_not have_key("fail") + #@ohai.attributes.should_not have_key("fail") + lambda { @ohai.provides_map.find_providers_for(["fail"]) }.should raise_error(Ohai::Exceptions::AttributeNotFound) end it "should not have source key" do @@ -103,7 +104,7 @@ it "should have attribute keys" do @loader.load_plugin("#{tmp}/plugins/fail.rb") - @ohai.attributes.should have_key("fail") + @ohai.provides_map.should have_key("fail") end it "should have source key" do @@ -151,7 +152,7 @@ it "should not have new attribute keys" do @loader.load_plugin("#{tmp}/plugins/fail.rb").new(@ohai).run - @ohai.attributes.should_not have_key("other") + @ohai.provides_map.should_not have_key("other") end it "should write to Ohai::Log" do diff --git a/spec/unit/provides_map_spec.rb b/spec/unit/provides_map_spec.rb new file mode 100644 index 000000000..5675335d5 --- /dev/null +++ b/spec/unit/provides_map_spec.rb @@ -0,0 +1,110 @@ +# +# Author:: Daniel DeLeo () +# Copyright:: Copyright (c) 2013 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); you +# may not use this file except in compliance with the License. You may +# obtain a copy of the license at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License +# + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') + +describe Ohai::ProvidesMap do + + let(:ohai_system) { Ohai::System.new } + let(:provides_map) { Ohai::ProvidesMap.new } + let(:plugin_1) { Ohai::DSL::Plugin.new(ohai_system, "") } + let(:plugin_2) { Ohai::DSL::Plugin.new(ohai_system, "") } + let(:plugin_3) { Ohai::DSL::Plugin.new(ohai_system, "") } + let(:plugin_4) { Ohai::DSL::Plugin.new(ohai_system, "") } + + describe "when looking up providing plugins for a single attribute" do + describe "when only one plugin provides the attribute" do + before do + provides_map.set_providers_for(plugin_1, ["single"]) + end + + it "should return the provider" do + expect(provides_map.find_providers_for(["single"])).to eq([plugin_1]) + end + end + + describe "when multiple plugins provide the attribute" do + before do + provides_map.set_providers_for(plugin_1, ["single"]) + provides_map.set_providers_for(plugin_2, ["single"]) + end + + it "should return all providers" do + expect(provides_map.find_providers_for(["single"])).to eq([plugin_1, plugin_2]) + end + end + end + + describe "when looking up providing plugins for multiple attributes" do + describe "when a different plugin provides each attribute" do + + before do + provides_map.set_providers_for(plugin_1, ["one"]) + provides_map.set_providers_for(plugin_2, ["two"]) + end + + it "should return each provider" do + expect(provides_map.find_providers_for(["one", "two"])).to eq([plugin_1, plugin_2]) + end + end + + describe "when one plugin provides both requested attributes" do + + before do + provides_map.set_providers_for(plugin_1, ["one"]) + provides_map.set_providers_for(plugin_1, ["one_again"]) + end + + it "should return unique providers" do + expect(provides_map.find_providers_for(["one", "one_again"])).to eq([plugin_1]) + end + end + end + + describe "when looking up providers for multi-level attributes" 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 + + describe "when listing all plugins" do + before(:each) do + provides_map.set_providers_for(plugin_1, ["one"]) + provides_map.set_providers_for(plugin_2, ["two"]) + provides_map.set_providers_for(plugin_3, ["stub/three"]) + provides_map.set_providers_for(plugin_4, ["foo/bar/four", "also/this/four"]) + 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) + expect(all_plugins).to include(plugin_2) + expect(all_plugins).to include(plugin_3) + expect(all_plugins).to include(plugin_4) + end + end + +end + diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb index a1ca3ee44..02ac92eb3 100644 --- a/spec/unit/runner_spec.rb +++ b/spec/unit/runner_spec.rb @@ -100,23 +100,11 @@ @plugins << klass.new(@ohai, "/tmp/plugins/source_dont_matter.rb") end @plugin1, @plugin2 = @plugins - end - - it "should locate the provider" do - @ohai.attributes[:thing] = Mash.new - @ohai.attributes[:thing][:_plugins] = [@plugin1] - @ohai.attributes[:other] = Mash.new - @ohai.attributes[:other][:_plugins] = [@plugin2] - @runner.should_receive(:fetch_plugins).twice.with(["thing"]).and_return([@plugin1]) - @runner.should_receive(:fetch_plugins).with([]).and_return([]) - @runner.run_plugin(@plugin2) + @ohai.provides_map.set_providers_for(@plugin1, ["thing"]) end it "should run the plugins" do - @runner.stub(:fetch_plugins).with(["thing"]).and_return([@plugin1]) - @runner.stub(:fetch_plugins).with([]).and_return([]) - @runner.run_plugin(@plugin2) @plugins.each do |plugin| plugin.has_run?.should be_true @@ -145,22 +133,12 @@ @plugins << klass.new(@ohai, "/tmp/plugins/whateva.rb") end @plugin1, @plugin2, @plugin3 = @plugins - end - - it "should locate each provider" do - @ohai.attributes[:thing] = Mash.new - @ohai.attributes[:thing][:_plugins] = [@plugin1, @plugin2] - @ohai.attributes[:other] = Mash.new - @ohai.attributes[:other][:_plugins] = [@plugin3] - @runner.should_receive(:fetch_plugins).exactly(3).times.with(["thing"]).and_return([@plugin1, @plugin2]) - @runner.should_receive(:fetch_plugins).twice.with([]).and_return([]) - @runner.run_plugin(@plugin3) + @ohai.provides_map.set_providers_for(@plugin1, ["thing"]) + @ohai.provides_map.set_providers_for(@plugin2, ["thing"]) end it "should run the plugins" do - @runner.stub(:fetch_plugins).with([]).and_return([]) - @runner.stub(:fetch_plugins).with(["thing"]).and_return([@plugin1, @plugin2]) @runner.run_plugin(@plugin3) @plugins.each do |plugin| plugin.has_run?.should be_true @@ -199,24 +177,11 @@ @plugins << klass.new(@ohai, "/tmp/plugins/number.rb") end @plugin1, @plugin2, @plugin3 = @plugins - end - - it "should locate each provider" do - @ohai.attributes[:one] = Mash.new - @ohai.attributes[:one][:_plugins] = [@plugin1] - @ohai.attributes[:two] = Mash.new - @ohai.attributes[:two][:_plugins] = [@plugin2] - @ohai.attributes[:three] = Mash.new - @ohai.attributes[:three][:_plugins] = [@plugin3] - - @runner.should_receive(:fetch_plugins).twice.with([]).and_return([]) - @runner.should_receive(:fetch_plugins).exactly(3).times.with(["one", "two"]).and_return([@plugin1, @plugin2]) - @runner.run_plugin(@plugin3) + @ohai.provides_map.set_providers_for(@plugin1, ["one", "two"]) + @ohai.provides_map.set_providers_for(@plugin2, ["one", "two"]) end it "should run the plugins" do - @runner.stub(:fetch_plugins).with([]).and_return([]) - @runner.stub(:fetch_plugins).with(["one", "two"]).and_return([@plugin1, @plugin2]) @runner.run_plugin(@plugin3) @plugins.each do |plugin| plugin.has_run?.should be_true @@ -283,20 +248,13 @@ @plugins << klass.new(@ohai, "") end @pluginA, @pluginB, @pluginC = @plugins - - @ohai.attributes[:A] = Mash.new - @ohai.attributes[:A][:_plugins] = [@pluginA] - @ohai.attributes[:B] = Mash.new - @ohai.attributes[:B][:_plugins] = [@pluginB] - @ohai.attributes[:C] = Mash.new - @ohai.attributes[:C][:_plugins] = [@pluginC] - - @runner.stub(:fetch_plugins).with(["C"]).and_return([@pluginC]) - @runner.stub(:fetch_plugins).with([]).and_return([]) end it "should not detect a cycle when B is the first provider returned" do - @runner.stub(:fetch_plugins).with(["B", "C"]).and_return([@pluginB, @pluginC]) + @ohai.provides_map.set_providers_for(@pluginA, ["A"]) + @ohai.provides_map.set_providers_for(@pluginB, ["B"]) + @ohai.provides_map.set_providers_for(@pluginC, ["C"]) + Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/) @runner.run_plugin(@pluginA) @@ -306,7 +264,10 @@ end it "should not detect a cycle when C is the first provider returned" do - @runner.stub(:fetch_plugins).with(["B", "C"]).and_return([@pluginC, @pluginB]) + @ohai.provides_map.set_providers_for(@pluginA, ["A"]) + @ohai.provides_map.set_providers_for(@pluginC, ["C"]) + @ohai.provides_map.set_providers_for(@pluginB, ["B"]) + Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/) @runner.run_plugin(@pluginA) @@ -323,71 +284,12 @@ @runner = Ohai::Runner.new(@ohai, true) end - describe "for a single attribute" do - describe "with one provider" do - it "should return the provider" do - plugin = Ohai::DSL::Plugin.new(@ohai, "") - @ohai.attributes[:single] = Mash.new - @ohai.attributes[:single][:_plugins] = [plugin] - - dependency_providers = @runner.fetch_plugins(["single"]) - dependency_providers.should eql([plugin]) - end - end - - describe "with multiple providers" do - it "should return all providers" do - plugin1 = Ohai::DSL::Plugin.new(@ohai, "") - plugin2 = Ohai::DSL::Plugin.new(@ohai, "") - @ohai.attributes[:single] = Mash.new - @ohai.attributes[:single][:_plugins] = [plugin1, plugin2] - - dependency_providers = @runner.fetch_plugins(["single"]) - dependency_providers.should eql([plugin1, plugin2]) - end - end - end - - describe "for multiple attributes" do - describe "with unique providers" do - it "should return each provider" do - plugin1 = Ohai::DSL::Plugin.new(@ohai, "") - plugin2 = Ohai::DSL::Plugin.new(@ohai, "") - @ohai.attributes[:one] = Mash.new - @ohai.attributes[:one][:_plugins] = [plugin1] - @ohai.attributes[:two] = Mash.new - @ohai.attributes[:two][:_plugins] = [plugin2] - - dependency_providers = @runner.fetch_plugins(["one", "two"]) - dependency_providers.should eql([plugin1, plugin2]) - end - end - - describe "with shared providers" do - it "should return unique providers" do - plugin = Ohai::DSL::Plugin.new(@ohai, "") - @ohai.attributes[:one] = Mash.new - @ohai.attributes[:one][:_plugins] = [plugin] - @ohai.attributes[:two] = Mash.new - @ohai.attributes[:two][:_plugins] = [plugin] - - dependency_providers = @runner.fetch_plugins(["one", "two"]) - dependency_providers.should eql([plugin]) - end - end - end - - describe "for multi-level attributes" do - it "should collect the provider" do - plugin = Ohai::DSL::Plugin.new(@ohai, "") - @ohai.attributes[:top] = Mash.new - @ohai.attributes[:top][:middle] = Mash.new - @ohai.attributes[:top][:middle][:bottom] = Mash.new - @ohai.attributes[:top][:middle][:bottom][:_plugins] = [plugin] + it "should collect the provider" do + plugin = Ohai::DSL::Plugin.new(@ohai, "") + @ohai.provides_map.set_providers_for(plugin, ["top/middle/bottom"]) - dependency_providers = @runner.fetch_plugins(["top/middle/bottom"]) - dependency_providers.should eql([plugin]) - end + dependency_providers = @runner.fetch_plugins(["top/middle/bottom"]) + dependency_providers.should eql([plugin]) end end diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index 0cdfab527..c4b640b89 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -29,8 +29,8 @@ @ohai.should be_a_kind_of(Ohai::System) end - it "should set @attributes to a Mash" do - @ohai.attributes.should be_a_kind_of(Mash) + it "should set @attributes to a ProvidesMap" do + @ohai.provides_map.should be_a_kind_of(Ohai::ProvidesMap) end it "should set @v6_dependency_solver to a Hash" do @@ -128,7 +128,7 @@ @ohai = Ohai::System.new klass = Ohai.plugin(:Empty) { } plugin = klass.new(@ohai, "/tmp/plugins/empty.rb") - @ohai.stub(:collect_plugins).and_return([plugin]) + @ohai.provides_map.should_receive(:all_plugins).and_return([plugin]) end describe "when AttributeNotFound is received" do @@ -165,7 +165,7 @@ @plugins << klass.new(@ohai, "") end - @ohai.stub(:collect_plugins).and_return(@plugins) + @ohai.provides_map.should_receive(:all_plugins).and_return(@plugins) end it "should run each plugin once from Ohai::System" do @@ -254,18 +254,13 @@ end it "should find all the plugins providing attributes" do - a = @ohai.attributes - a[:zero] = Mash.new - a[:zero][:_plugins] = [@plugins[0]] - a[:one] = Mash.new - a[:one][:_plugins] = [@plugins[1]] - a[:one][:two] = Mash.new - a[:one][:two][:_plugins] = [@plugins[2]] - a[:stub] = Mash.new - a[:stub][:three] = Mash.new - a[:stub][:three][:_plugins] = [@plugins[3]] - - providers = @ohai.collect_plugins(@ohai.attributes) + provides_map = @ohai.provides_map + provides_map.set_providers_for(@plugins[0], ["zero"]) + provides_map.set_providers_for(@plugins[1], ["one"]) + provides_map.set_providers_for(@plugins[2], ["two"]) + provides_map.set_providers_for(@plugins[3], ["stub/three"]) + + providers = provides_map.all_plugins providers.size.should eql(@plugins.size) @plugins.each do |plugin| providers.include?(plugin).should be_true @@ -354,8 +349,7 @@ @ohai.v6_dependency_solver['v6plugin'] = @v6plugin @ohai.v6_dependency_solver['v7plugin'] = @v7plugin - @ohai.attributes[:message] = Mash.new - @ohai.attributes[:message][:_plugins] = [@v7plugin] + @ohai.provides_map.set_providers_for(@v7plugin, ["message"]) end it "should run the plugin it requires" do @@ -400,11 +394,11 @@ vds['v7plugin'] = @v7plugin vds['other'] = @other - a = @ohai.attributes - a[:message] = Mash.new - a[:message][:_plugins] = [@v7plugin] - a[:other] = Mash.new - a[:other][:_plugins] = [@other] + dependency_map = @ohai.provides_map + #dependency_map[:message][:_plugins] = [@v7plugin] + dependency_map.set_providers_for(@v7plugin, ["message"]) + #dependency_map[:other][:_plugins] = [@other] + dependency_map.set_providers_for(@other, ["other"]) end it "should resolve the v7 plugin dependencies" do