diff options
author | Claire McQuin <claire@opscode.com> | 2013-09-03 14:24:12 -0700 |
---|---|---|
committer | Claire McQuin <claire@opscode.com> | 2013-09-03 14:53:08 -0700 |
commit | bf6f552c3c70ef836151ae832efde121c3222944 (patch) | |
tree | e4f8912b80605c52192d30745d060ccb650d29d4 /spec/unit/runner_spec.rb | |
parent | ff149f9a1ea45f63bf785bd3ccc6d8e9e7d87739 (diff) | |
download | ohai-bf6f552c3c70ef836151ae832efde121c3222944.tar.gz |
Ohai::Runner class for running plugins, add run_plugins to Ohai::System to run loaded plugins
Diffstat (limited to 'spec/unit/runner_spec.rb')
-rw-r--r-- | spec/unit/runner_spec.rb | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb index 3ad237d2..51fca2b3 100644 --- a/spec/unit/runner_spec.rb +++ b/spec/unit/runner_spec.rb @@ -16,3 +16,260 @@ # 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::Runner, "run_plugin" do + describe "when running a plugin with no dependencies, Ohai::Runner" do + before(:each) do + @ohai = Ohai::System.new + @runner = Ohai::Runner.new(@ohai, true) + + klass = Ohai.plugin { provides("thing"); collect_data { thing(Mash.new) } } + @plugin = klass.new(@ohai, "/tmp/plugins/thing.rb") + end + + it "should not find dependencies" do + @runner.should_receive(:fetch_providers).with([]).and_return([]) + @runner.run_plugin(@plugin) + end + + it "should run the plugin" do + @runner.run_plugin(@plugin) + @plugin.has_run?.should be_true + end + + it "should add plugin data to Ohai::System.data" do + @runner.run_plugin(@plugin) + @ohai.data.should have_key(:thing) + @ohai.data[:thing].should eql({}) + end + end + + describe "when running a plugin with one dependency" do + before(:each) do + @ohai = Ohai::System.new + @runner = Ohai::Runner.new(@ohai, true) + end + + describe "when the dependency does not exist" do + before(:each) do + klass = Ohai.plugin { provides("thing"); depends("other_thing"); collect_data { thing(other_thing) } } + @plugin = klass.new(@ohai, "/tmp/plugins/thing.rb") + end + + it "should raise a NoAttributeError" do + expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::NoAttributeError) + end + + it "should not run the plugin" do + expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::NoAttributeError) + @plugin.has_run?.should be_false + end + end + + describe "when the dependency has a single provider" do + before(:each) do + klass1 = Ohai.plugin { provides("thing"); collect_data { thing("thang") } } + klass2 = Ohai.plugin { provides("other"); depends("thing"); collect_data { other(thing) } } + + @plugins = [] + [klass1, klass2].each do |klass| + @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][:providers] = [@plugin1] + @ohai.attributes[:other] = Mash.new + @ohai.attributes[:other][:providers] = [@plugin2] + + @runner.should_receive(:fetch_providers).twice.with(["thing"]).and_return([@plugin1]) + @runner.should_receive(:fetch_providers).with([]).and_return([]) + @runner.run_plugin(@plugin2) + end + + it "should run the plugins" do + @runner.stub(:fetch_providers).with(["thing"]).and_return([@plugin1]) + @runner.stub(:fetch_providers).with([]).and_return([]) + + @runner.run_plugin(@plugin2) + @plugins.each do |plugin| + plugin.has_run?.should be_true + end + end + end + + describe "when the dependency has multiple providers" do + before(:each) do + klass1 = Ohai.plugin { provides("thing"); collect_data { thing(Mash.new) } } + klass2 = Ohai.plugin { provides("other"); depends("thing"); collect_data { other(thing) } } + + @plugins = [] + [klass1, klass1, klass2].each do |klass| + @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][:providers] = [@plugin1, @plugin2] + @ohai.attributes[:other] = Mash.new + @ohai.attributes[:other][:providers] = [@plugin3] + + @runner.should_receive(:fetch_providers).twice.with(["thing"]).and_return([@plugin1, @plugin2]) + @runner.should_receive(:fetch_providers).twice.with([]).and_return([]) + @runner.run_plugin(@plugin3) + end + + it "should run the plugins" do + @runner.stub(:fetch_providers).with([]).and_return([]) + @runner.stub(:fetch_providers).with(["thing"]).and_return([@plugin1, @plugin2]) + @runner.run_plugin(@plugin3) + @plugins.each do |plugin| + plugin.has_run?.should be_true + end + end + end + end + + describe "when running a plugin with many dependencies" do + before(:each) do + @ohai = Ohai::System.new + @runner = Ohai::Runner.new(@ohai, true) + + klass1 = Ohai.plugin { provides("one"); collect_data { one(1) } } + klass2 = Ohai.plugin { provides("two"); collect_data { two(2) } } + klass3 = Ohai.plugin { provides("three"); depends("one", "two"); collect_data { three(3) } } + + @plugins = [] + [klass1, klass2, klass3].each do |klass| + @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][:providers] = [@plugin1] + @ohai.attributes[:two] = Mash.new + @ohai.attributes[:two][:providers] = [@plugin2] + @ohai.attributes[:three] = Mash.new + @ohai.attributes[:three][:providers] = [@plugin3] + + @runner.should_receive(:fetch_providers).twice.with([]).and_return([]) + @runner.should_receive(:fetch_providers).twice.with(["one", "two"]).and_return([@plugin1, @plugin2]) + @runner.run_plugin(@plugin3) + end + + it "should run the plugins" do + @runner.stub(:fetch_providers).with([]).and_return([]) + @runner.stub(:fetch_providers).with(["one", "two"]).and_return([@plugin1, @plugin2]) + @runner.run_plugin(@plugin3) + @plugins.each do |plugin| + plugin.has_run?.should be_true + end + end + end + + describe "when a cycle is detected" do + before(:each) do + @ohai = Ohai::System.new + @runner = Ohai::Runner.new(@ohai, true) + + klass1 = Ohai.plugin { provides("thing"); depends("other"); collect_data { thing(other) } } + klass2 = Ohai.plugin { provides("other"); depends("thing"); collect_data { other(thing) } } + + @plugins = [] + [klass1, klass2].each_with_index do |klass, idx| + @plugins << klass.new(@ohai, "/tmp/plugins/plugin#{idx}.rb") + end + @plugin1, @plugin2 = @plugins + end + + it "should raise a DependencyCycleError" do + @runner.stub(:fetch_providers).with(["thing"]).and_return([@plugin1]) + @runner.stub(:fetch_providers).with(["other"]).and_return([@plugin2]) + expect { @runner.run_plugin(@plugin1) }.to raise_error(Ohai::DependencyCycleError) + end + end +end + +describe Ohai::Runner, "fetch_providers" do + before(:each) do + @ohai = Ohai::System.new + @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][:providers] = [plugin] + + dependency_providers = @runner.fetch_providers(["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][:providers] = [plugin1, plugin2] + + dependency_providers = @runner.fetch_providers(["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][:providers] = [plugin1] + @ohai.attributes[:two] = Mash.new + @ohai.attributes[:two][:providers] = [plugin2] + + dependency_providers = @runner.fetch_providers(["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][:providers] = [plugin] + @ohai.attributes[:two] = Mash.new + @ohai.attributes[:two][:providers] = [plugin] + + dependency_providers = @runner.fetch_providers(["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][:providers] = [plugin] + + dependency_providers = @runner.fetch_providers(["top/middle/bottom"]) + dependency_providers.should eql([plugin]) + end + end +end + + |