summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsersut <serdar@opscode.com>2013-12-16 11:10:47 -0800
committersersut <serdar@opscode.com>2013-12-17 14:09:47 -0800
commit9915bf8d23ccb83f2944e41c88163f49b7fb7965 (patch)
tree256227d85486d423a326dd02a14a049c7d8c094e
parent4043527c700c503de317d4221f516392064eb720 (diff)
downloadohai-9915bf8d23ccb83f2944e41c88163f49b7fb7965.tar.gz
Rewrite system specs with OHAI integration testing infra.
Refactor the runner class. Make sure require_plugin() checks v6 plugins first. Use partial paths as keys in the v6 dependency solver.
-rw-r--r--lib/ohai/exception.rb1
-rw-r--r--lib/ohai/runner.rb42
-rw-r--r--lib/ohai/system.rb131
-rw-r--r--spec/unit/runner_spec.rb143
-rw-r--r--spec/unit/system_spec.rb404
5 files changed, 320 insertions, 401 deletions
diff --git a/lib/ohai/exception.rb b/lib/ohai/exception.rb
index 62af2841..0496573f 100644
--- a/lib/ohai/exception.rb
+++ b/lib/ohai/exception.rb
@@ -22,5 +22,6 @@ module Ohai
class IllegalPluginDefinition < Exception; end
class AttributeNotFound < Exception; end
class DependencyCycle < Exception; end
+ class DependencyNotFound < Exception; end
end
end
diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb
index e4650319..817518d4 100644
--- a/lib/ohai/runner.rb
+++ b/lib/ohai/runner.rb
@@ -29,14 +29,37 @@ module Ohai
@safe_run = safe_run
end
- # runs this plugin and any un-run dependencies. if force is set to
- # true, then this plugin and its dependencies will be run even if
- # they have been run before.
+ # Runs plugins and any un-run dependencies.
+ # If force is set to 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]
+
+ if Ohai::Config[:disabled_plugins].include?(plugin.name)
+ Ohai::Log.debug("Skipping disabled plugin #{plugin.name}")
+ return false
+ end
+
+ case plugin.version
+ when :version7
+ run_v7_plugin(plugin, force)
+ when :version6
+ run_v6_plugin(plugin, force)
+ else
+ raise ArgumentError, "Invalid plugin version #{plugin.version} for plugin #{plugin}"
+ end
+ end
+
+ def run_v6_plugin(plugin, force)
+ return true if plugin.has_run? && !force
+
+ @safe_run ? plugin.safe_run : plugin.run
+ end
+
+ def run_v7_plugin(plugin, force)
+ visited = [ plugin ]
while !visited.empty?
next_plugin = visited.pop
@@ -47,7 +70,12 @@ module Ohai
end
dependency_providers = fetch_plugins(next_plugin.dependencies)
- dependency_providers.delete_if { |dep_plugin| (!force && dep_plugin.has_run?) || dep_plugin.eql?(next_plugin) }
+
+ # Remove the already ran plugins from dependencies if force is not set
+ # Also remove the plugin that we are about to run from dependencies as well.
+ dependency_providers.delete_if { |dep_plugin|
+ (!force && dep_plugin.has_run?) || dep_plugin.eql?(next_plugin)
+ }
if dependency_providers.empty?
@safe_run ? next_plugin.safe_run : next_plugin.run
@@ -62,9 +90,9 @@ module Ohai
@provides_map.find_providers_for(attributes)
end
- # given a list of plugins and the first plugin in the cycle,
+ # Given a list of plugins and the first plugin in the cycle,
# returns the list of plugin source files responsible for the
- # cycle. does not include plugins that aren't a part of the cycle
+ # cycle. Does not include plugins that aren't a part of the cycle
def get_cycle(plugins, cycle_start)
cycle = plugins.drop_while { |plugin| !plugin.eql?(cycle_start) }
names = []
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index 81664b44..c1467784 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -55,9 +55,6 @@ module Ohai
@data[key]
end
- #=============================================
- # Version 7 system commands
- #=============================================
def all_plugins(attribute_filter=nil)
load_plugins
run_plugins(true, false, attribute_filter)
@@ -72,11 +69,11 @@ module Ohai
if plugin && plugin.version == :version6
# Capture the plugin in @v6_dependency_solver if it is a V6 plugin
# to be able to resolve V6 dependencies later on.
+ # We are using the partial path in the dep solver as a key.
partial_path = Pathname.new(plugin_file_path).relative_path_from(Pathname.new(path)).to_s
- dep_solver_key = nameify_v6_plugin(partial_path)
- unless @v6_dependency_solver.has_key?(dep_solver_key)
- @v6_dependency_solver[dep_solver_key] = plugin
+ unless @v6_dependency_solver.has_key?(partial_path)
+ @v6_dependency_solver[partial_path] = plugin
else
Ohai::Log.debug("Plugin '#{plugin_file_path}' is already loaded.")
end
@@ -86,93 +83,65 @@ module Ohai
end
def run_plugins(safe = false, force = false, attribute_filter = nil)
- # collect and run version 6 plugins
- v6plugins = []
- @v6_dependency_solver.each { |plugin_name, plugin| v6plugins << plugin if plugin.version.eql?(:version6) }
- v6plugins.each do |v6plugin|
- if !v6plugin.has_run? || force
- safe ? v6plugin.safe_run : v6plugin.run
- end
+ # First run all the version 6 plugins
+ @v6_dependency_solver.values.each do |v6plugin|
+ @runner.run_plugin(v6plugin, force)
end
- # collect and run version 7 plugins
- plugins = @provides_map.all_plugins(attribute_filter)
-
+ # Then run all the version 7 plugins
begin
- plugins.each { |plugin| @runner.run_plugin(plugin, force) }
+ @provides_map.all_plugins(attribute_filter).each { |plugin|
+ @runner.run_plugin(plugin, force)
+ }
rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle => e
Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
raise
end
- true
end
- 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]
- else
- collected << collect_plugins(plugins[plugin])
- end
- end
- else
- collected << plugins
- end
- collected.flatten.uniq
+ def pathify_v6_plugin(plugin_name)
+ path_components = plugin_name.split("::")
+ File.join(path_components) + ".rb"
end
- #=============================================
- # Version 6 system commands
- #=============================================
- def require_plugin(plugin_name, force=false)
- unless force
- plugin = @v6_dependency_solver[plugin_name]
- return true if plugin && plugin.has_run?
- end
-
- if Ohai::Config[:disabled_plugins].include?(plugin_name)
- Ohai::Log.debug("Skipping disabled plugin #{plugin_name}")
- return false
- end
-
- if plugin = @v6_dependency_solver[plugin_name] or plugin = plugin_for(plugin_name)
- begin
- plugin.version.eql?(:version7) ? @runner.run_plugin(plugin, force) : plugin.safe_run
- true
- rescue SystemExit, Interrupt
- raise
- rescue DependencyCycleError, NoAttributeError => e
- Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
- raise
- rescue Exception,Errno::ENOENT => e
- Ohai::Log.debug("Plugin #{plugin_name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
- end
+ #
+ # Below APIs are from V6.
+ # Make sure that you are not breaking backwards compatibility
+ # if you are changing any of the APIs below.
+ #
+ def require_plugin(plugin_ref, force=false)
+ plugins = [ ]
+ # This method is only callable by version 6 plugins.
+ # First we check if there exists a v6 plugin that fulfills the dependency.
+ if @v6_dependency_solver.has_key? pathify_v6_plugin(plugin_ref)
+ # Note that: partial_path looks like Plugin::Name
+ # keys for @v6_dependency_solver are in form 'plugin/name.rb'
+ plugins << @v6_dependency_solver[pathify_v6_plugin(plugin_ref)]
else
- Ohai::Log.debug("No #{plugin_name} found in #{Ohai::Config[:plugin_path]}")
+ # While looking up V7 plugins we need to convert the plugin_ref to an attribute.
+ attribute = plugin_ref.gsub("::", "/")
+ plugins = @provides_map.find_providers_for([attribute])
end
- end
-
- def plugin_for(plugin_name)
- filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
- plugin = nil
- Ohai::Config[:plugin_path].each do |path|
- check_path = File.expand_path(File.join(path, filename))
- if File.exist?(check_path)
- plugin = @loader.load_plugin(check_path)
- @v6_dependency_solver[plugin_name] = plugin
- break
- else
- next
+ if plugins.empty?
+ raise DependencyNotFound, "Can not find a plugin for dependency #{plugin_ref}"
+ else
+ plugins.each do |plugin|
+ begin
+ @runner.run_plugin(plugin, force)
+ rescue SystemExit, Interrupt
+ raise
+ rescue Ohai::Exceptions::DependencyCycle, Ohai::Exceptions::AttributeNotFound => e
+ Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
+ raise
+ rescue Exception,Errno::ENOENT => e
+ Ohai::Log.debug("Plugin #{plugin.name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
+ end
end
end
- plugin
end
- # todo: fix for running w/new internals
+ # TODO: fix for running w/new internals
# add updated function to v7?
def refresh_plugins(path = '/')
Ohai::Hints.refresh_hints()
@@ -200,15 +169,16 @@ module Ohai
end
end
- #=============================================
- # For outputting an Ohai::System object
- #=============================================
+ #
# Serialize this object as a hash
+ #
def to_json
Yajl::Encoder.new.encode(@data)
end
+ #
# Pretty Print this object as JSON
+ #
def json_pretty_print(item=nil)
Yajl::Encoder.new(:pretty => true).encode(item || @data)
end
@@ -233,10 +203,5 @@ module Ohai
end
end
- def nameify_v6_plugin(partial_path)
- md = Regexp.new("(.+).rb$").match(partial_path)
- md[1].gsub(File::SEPARATOR, "::")
- end
-
end
end
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb
index b2695604..393cbed2 100644
--- a/spec/unit/runner_spec.rb
+++ b/spec/unit/runner_spec.rb
@@ -20,43 +20,146 @@
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)
+ let(:safe_run) { true }
+
+ before(:each) do
+ @ohai = Ohai::System.new
+ @runner = Ohai::Runner.new(@ohai, safe_run)
+ end
+
+ describe "when running an invalid plugin" do
+ it "should raise error" do
+ lambda { @runner.run_plugin(double("Ohai::NotPlugin")) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "when running a plugin" do
+ let(:plugin) { double("Ohai::DSL::Plugin", :kind_of? => true, :version => version, :name => "Test", :has_run? => has_run, :dependencies => [ ]) }
+ let(:version) { :version7 }
+ let(:has_run) { false }
+
+ describe "version 7" do
+ it "should call run_v7_plugin" do
+ @runner.should_receive(:run_v7_plugin)
+ @runner.run_plugin(plugin)
+ end
+
+ describe "if the plugin has run before" do
+ let(:has_run) { true }
+
+ it "plugin should not run when run_plugin is not forced" do
+ plugin.should_not_receive(:safe_run)
+ @runner.run_plugin(plugin)
+ end
+
+ it "plugin should run when run_plugin is not forced" do
+ plugin.should_receive(:safe_run)
+ @runner.run_plugin(plugin, true)
+ end
+
+ describe "if the plugin is disabled" do
+ it "should not run the plugin" do
+ Ohai::Config.should_receive(:[]).with(:disabled_plugins).and_return(["Test"])
+ @runner.should_not_receive(:run_v7_plugin)
+ @runner.run_plugin(plugin)
+ end
+ end
+ end
+ end
+
+ describe "version 6" do
+ let(:version) { :version6 }
+
+ it "should call run_v6_plugin" do
+ @runner.should_receive(:run_v6_plugin)
+ @runner.run_plugin(plugin)
+ end
+
+ describe "if the plugin has not run before" do
+ describe "if safe_run is not set" do
+ it "safe_run should be called" do
+ plugin.should_receive(:safe_run)
+ @runner.run_plugin(plugin)
+ end
+ end
+
+ describe "if safe_run is set" do
+ let(:safe_run) { false }
+
+ it "run should be called" do
+ plugin.should_receive(:run)
+ @runner.run_plugin(plugin)
+ end
+ end
+ end
+ describe "if the plugin has run before" do
+ let(:has_run) { true }
+
+ it "plugin should not run when run_plugin is not forced" do
+ plugin.should_not_receive(:safe_run)
+ @runner.run_plugin(plugin)
+ end
+
+ it "plugin should run when run_plugin is not forced" do
+ plugin.should_receive(:safe_run)
+ @runner.run_plugin(plugin, true)
+ end
+ end
+
+ describe "if the plugin is disabled" do
+ it "should not run the plugin" do
+ Ohai::Config.should_receive(:[]).with(:disabled_plugins).and_return(["Test"])
+ @runner.should_not_receive(:run_v7_plugin)
+ @runner.run_plugin(plugin)
+ end
+ end
+ end
+
+ describe "invalid version" do
+ let(:version) { :versionBla }
+
+ it "should raise error" do
+ lambda { @runner.run_plugin(plugin) }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "when plugin is disabled" do
+ before do
+ Ohai::Config.should_receive(:[]).with(:disabled_plugins).and_return(["Test"])
+ end
+
+ it "should not run the plugin" do
+ @runner.should_not_receive(:run_v7_plugin)
+ @runner.run_plugin(plugin)
+ end
+ end
+ end
+
+ describe "when running a plugin with no dependencies, Ohai::Runner" do
+ let(:plugin) {
klass = Ohai.plugin(:Test) {
provides("thing")
collect_data {
thing(Mash.new)
}
}
- @plugin = klass.new(@ohai.data)
- end
-
- it "should not find dependencies" do
- @runner.should_receive(:fetch_plugins).with([]).and_return([])
- @runner.run_plugin(@plugin)
- end
+ klass.new(@ohai.data)
+ }
it "should run the plugin" do
- @runner.run_plugin(@plugin)
- @plugin.has_run?.should be_true
+ @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)
+ @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(:Test) {
@@ -146,7 +249,7 @@ describe Ohai::Runner, "run_plugin" do
end
end
end
-
+
describe "when running a plugin with many dependencies" do
before(:each) do
@ohai = Ohai::System.new
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index b0fd01e7..c07ed836 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -60,7 +60,7 @@ EOF
it "load_plugins() should load all the plugins" do
@ohai.load_plugins
@ohai.provides_map.map.keys.should include("seals")
- @ohai.v6_dependency_solver.keys.should include("lake")
+ @ohai.v6_dependency_solver.keys.should include("lake.rb")
Ohai::NamedPlugin.const_get(:Zoo).should == Ohai::NamedPlugin::Zoo
end
end
@@ -100,72 +100,42 @@ EOF
@ohai.load_plugins
@ohai.provides_map.map.keys.should include("seals")
@ohai.provides_map.map.keys.should include("crabs")
- @ohai.v6_dependency_solver.keys.should include("lake")
- @ohai.v6_dependency_solver.keys.should include("mountain")
+ @ohai.v6_dependency_solver.keys.should include("lake.rb")
+ @ohai.v6_dependency_solver.keys.should include("mountain.rb")
Ohai::NamedPlugin.const_get(:Zoo).should == Ohai::NamedPlugin::Zoo
Ohai::NamedPlugin.const_get(:Nature).should == Ohai::NamedPlugin::Nature
end
end
- describe "#run_plugins" do
- describe "with v6 plugins only" do
- before(:each) do
- @ohai = Ohai::System.new
-
- @plugins = []
- @names = ['one', 'two', 'three', 'four', 'five']
- @names.each do |name|
- k = Class.new(Ohai::DSL::Plugin::VersionVI) {
- collect_contents("")
- }
- p = k.new(@ohai, "some/plugin/path.rb")
- @ohai.v6_dependency_solver[name] = p
- @plugins << p
- end
-
- @ohai.stub(:collect_plugins).and_return([])
- end
-
- after(:each) do
- @ohai.v6_dependency_solver.clear
- end
-
- it "should run all version 6 plugins" do
- @ohai.run_plugins(true)
- @plugins.each do |plugin|
- plugin.has_run?.should be_true
- end
- end
+ describe "when running plugins" do
+ before do
+ @original_config = Ohai::Config[:plugin_path]
+ end
- it "should force plugins to run again if force is set to true" do
- @plugins.each do |plugin|
- plugin.stub(:has_run?).and_return(:true)
- plugin.should_receive(:safe_run)
- end
+ after do
+ Ohai::Config[:plugin_path] = @original_config
+ end
- @ohai.run_plugins(true, true)
- end
+ before(:each) do
+ @ohai = Ohai::System.new
end
- describe "with v7 plugins only" do
- describe "when handling an error" do
- before(:each) do
- @runner = double('@runner')
- Ohai::Runner.stub(:new) { @runner }
+ when_plugins_directory "contains v6 plugins only" do
+ with_plugin("zoo.rb", <<EOF)
+provides 'zoo'
+zoo("animals")
+EOF
- @ohai = Ohai::System.new
- klass = Ohai.plugin(:Empty) { }
- plugin = klass.new(@ohai.data)
- @ohai.provides_map.should_receive(:all_plugins).and_return([plugin])
- end
+ with_plugin("park.rb", <<EOF)
+provides 'park'
+park("plants")
+EOF
- describe "when AttributeNotFound is received" do
- it "should write an error to Ohai::Log" do
- @runner.stub(:run_plugin).and_raise(Ohai::Exceptions::AttributeNotFound)
- Ohai::Log.should_receive(:error).with(/Ohai::Exceptions::AttributeNotFound/)
- expect { @ohai.run_plugins }.to raise_error(Ohai::Exceptions::AttributeNotFound)
- end
- end
+ it "should collect data from all the plugins" do
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
+ @ohai.all_plugins
+ @ohai.data[:zoo].should == "animals"
+ @ohai.data[:park].should == "plants"
end
describe "when running in whitelist mode" do
@@ -246,38 +216,38 @@ EOF
end
end
- describe "when running all loaded plugins" do
- before(:each) do
- @runner = double('@runner')
- Ohai::Runner.stub(:new) { @runner }
-
- @ohai = Ohai::System.new
-
- @names = [:One, :Two, :Three, :Four, :Five]
+ when_plugins_directory "contains v7 plugins only" do
+ with_plugin("zoo.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ provides 'zoo'
- klasses = []
- @names.each do |name|
- klasses << Ohai.plugin(name) {
- provides("itself")
- collect_data {
- itself("me")
- }
- }
- end
+ collect_data(:default) do
+ zoo("animals")
+ end
+end
+EOF
- @plugins = []
- klasses.each do |klass|
- @plugins << klass.new(@ohai.data)
- end
+ with_plugin("park.rb", <<EOF)
+Ohai.plugin(:Park) do
+ provides 'park'
+ collect_data(:default) do
+ park("plants")
+ end
+end
+EOF
- @ohai.provides_map.should_receive(:all_plugins).and_return(@plugins)
+ it "should collect data from all the plugins" do
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
+ @ohai.all_plugins
+ @ohai.data[:zoo].should == "animals"
+ @ohai.data[:park].should == "plants"
end
- it "should run each plugin once from Ohai::System" do
- @plugins.each do |plugin|
- @runner.should_receive(:run_plugin).with(plugin, false)
- end
- @ohai.run_plugins
+ it "should write an error to Ohai::Log" do
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
+ @ohai.instance_variable_get("@runner").stub(:run_plugin).and_raise(Ohai::Exceptions::AttributeNotFound)
+ Ohai::Log.should_receive(:error).with(/Encountered error while running plugins/)
+ expect { @ohai.all_plugins }.to raise_error(Ohai::Exceptions::AttributeNotFound)
end
end
@@ -303,7 +273,6 @@ Ohai.plugin(:V7message) do
provides 'v7message'
collect_data(:default) do
- puts "I'm running now."
v7message "v7 plugins are awesome!"
end
end
@@ -320,7 +289,6 @@ EOF
end
it "should collect all data" do
- pending("Requires some changes to require_plugin() which will be changed in a seperate PR as a next step.")
@ohai.all_plugins
[:v6message, :v7message, :messages].each do |attribute|
@ohai.data.should have_key(attribute)
@@ -336,237 +304,91 @@ EOF
end
end
- describe "#collect_plugins" do
- before(:each) do
- @ohai = Ohai::System.new
-
- @names = [:Zero, :One, :Two, :Three]
- @plugins = []
- @names.each do |name|
- k = Ohai.plugin(name) { }
- @plugins << k.new(@ohai.data)
- end
- end
+ describe "require_plugin()" do
+ when_plugins_directory "contains v6 and v7 plugin with the same name" do
+ with_plugin("message.rb", <<EOF)
+provides 'message'
- it "should find all the plugins providing attributes" do
- 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
- end
- end
- end
-
- describe "#require_plugin" do
- before(:each) do
- @plugin_path = Ohai::Config[:plugin_path]
- Ohai::Config[:plugin_path] = ["/tmp/plugins"]
-
- @ohai = Ohai::System.new
- klass = Class.new(Ohai::DSL::Plugin::VersionVI) { }
- @plugin = klass.new(@ohai, "some/plugin/path.rb")
-
- @ohai.stub(:plugin_for).with("empty").and_return(@plugin)
- end
+message "From Version 6"
+EOF
- it "should immediately return if force is false and the plugin has already run" do
- @ohai.v6_dependency_solver['empty'] = @plugin
- @plugin.stub(:has_run?).and_return(true)
+ with_plugin("v7/message.rb", <<EOF)
+Ohai.plugin(:Message) do
+ provides 'message'
- @ohai.should_not_receive(:plugin_for).with("empty")
- @ohai.require_plugin("empty", true).should be_true
- end
+ collect_data(:default) do
+ message "From Version 7"
+ end
+end
+EOF
- context "when a plugin is disabled" do
- before(:each) do
- Ohai::Config[:disabled_plugins] = ["empty"]
+ before do
+ @ohai = Ohai::System.new
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
end
- it "should not run the plugin" do
- Ohai::Log.should_receive(:debug).with(/Skipping disabled plugin/)
- @ohai.should_not_receive(:plugin_for).with("empty")
- @ohai.require_plugin("empty").should be_false
+ after do
+ Ohai::Config[:plugin_path] = @original_config
end
- it "should not run the plugin even if force is true" do
- Ohai::Log.should_receive(:debug).with(/Skipping disabled plugin/)
- @ohai.should_not_receive(:plugin_for).with("empty")
- @ohai.require_plugin("empty", true).should be_false
+ it "version 6 should run" do
+ @ohai.load_plugins
+ @ohai.require_plugin("message")
+ @ohai.data[:message].should eql("From Version 6")
end
end
- it "should check for the plugin in v6_dependency_solver first" do
- @ohai.v6_dependency_solver['empty'] = @plugin
- @ohai.should_not_receive(:plugin_for).with("empty")
- @plugin.stub(:safe_run).and_return(true)
- @ohai.require_plugin("empty").should be_true
- end
+ when_plugins_directory "a v6 plugin that requires a v7 plugin with dependencies" do
+ with_plugin("message.rb", <<EOF)
+provides 'message'
- it "should load the plugin if not already loaded" do
- @ohai.stub(:plugin_for).with("empty").and_return(@plugin)
- @ohai.should_receive(:plugin_for).with("empty")
- @plugin.stub(:safe_run).and_return(true)
- @ohai.require_plugin("empty").should be_true
- end
+require_plugin 'v7message'
- it "should run the plugin if the plugin has not yet run" do
- @ohai.stub(:plugin_for).with("empty").and_return(@plugin)
- @plugin.stub(:safe_run).and_return(true)
- @ohai.require_plugin("empty").should be_true
- end
+message Mash.new
+message[:v6message] = "Hellos from 6"
+message[:copy_message] = v7message
+EOF
- it "should log a message to debug if a plugin cannot be found" do
- @ohai.stub(:plugin_for).with("fake").and_return(nil)
- Ohai::Log.should_receive(:debug).with(/No fake found in/)
- @ohai.require_plugin("fake")
- end
+ with_plugin("v7message.rb", <<EOF)
+Ohai.plugin(:V7message) do
+ provides 'v7message'
+ depends 'zoo'
- context "when a v6 plugin requires a v7 plugin" do
- before(:each) do
- v6string = <<EOF
-provides 'v6attr'
-require_plugin 'v7plugin'
-v6attr message
+ collect_data(:default) do
+ v7message ("Hellos from 7: " + zoo)
+ end
+end
EOF
- v6klass = Class.new(Ohai::DSL::Plugin::VersionVI) { collect_contents(v6string) }
- v7klass = Ohai.plugin(:V7plugin) {
- provides("message")
- collect_data { message("hey.") }
- }
- @v6plugin = v6klass.new(@ohai, "some/plugin/path.rb")
- @v7plugin = v7klass.new(@ohai.data)
-
- @ohai.v6_dependency_solver['v6plugin'] = @v6plugin
- @ohai.v6_dependency_solver['v7plugin'] = @v7plugin
- @ohai.provides_map.set_providers_for(@v7plugin, ["message"])
- end
-
- it "should run the plugin it requires" do
- @ohai.require_plugin('v6plugin')
- @v7plugin.has_run?.should be_true
- @v6plugin.has_run?.should be_true
- end
- it "should be able to access the data set by the v7 plugin" do
- @ohai.require_plugin('v6plugin')
- @ohai.data.should have_key(:message)
- @ohai.data[:message].should eql("hey.")
- @ohai.data.should have_key(:v6attr)
- @ohai.data[:v6attr].should eql("hey.")
- end
- end
+ with_plugin("zoo.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ provides 'zoo'
- context "when a v6 plugin requires a v7 plugin with dependencies" do
- before(:each) do
- v6string = <<EOF
-provides 'v6attr'
-require_plugin 'v7plugin'
-v6attr message
+ collect_data(:default) do
+ zoo "animals"
+ end
+end
EOF
- v6klass = Class.new(Ohai::DSL::Plugin::VersionVI) { collect_contents(v6string) }
- v7klass = Ohai.plugin(:V7plugin) {
- provides("message")
- depends("other")
- collect_data{ message(other) }
- }
- otherklass = Ohai.plugin(:Other) {
- provides("other")
- collect_data{ other("o hai") }
- }
-
- @v6plugin = v6klass.new(@ohai, "some/plugin/path.rb")
- @v7plugin = v7klass.new(@ohai.data)
- @other = otherklass.new(@ohai.data)
-
- vds = @ohai.v6_dependency_solver
- vds['v6plugin'] = @v6plugin
- vds['v7plugin'] = @v7plugin
- vds['other'] = @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
- @ohai.require_plugin('v6plugin')
- [@v6plugin, @v7plugin, @other].each do |plugin|
- plugin.has_run?.should be_true
- end
+ before do
+ @ohai = Ohai::System.new
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
end
- it "should set all collected data properly" do
- @ohai.require_plugin('v6plugin')
- d = @ohai.data
- d.should have_key(:other)
- d.should have_key(:message)
- d.should have_key(:v6attr)
- [:other, :message, :v6attr].each do |attr|
- d[attr].should eql("o hai")
- end
+ after do
+ Ohai::Config[:plugin_path] = @original_config
end
- end
- end
-
- describe "#plugin_for" do
- before(:each) do
- Ohai::Config[:plugin_path] = ["/tmp/plugins"]
- @loader = double('@loader')
- Ohai::Loader.stub(:new) { @loader }
-
- @ohai = Ohai::System.new
- @klass = Class.new(Ohai::DSL::Plugin::VersionVI) { }
- end
-
- it "should find a plugin with a simple name" do
- plugin = @klass.new(@ohai, "/tmp/plugins/empty.rb")
- File.stub(:join).with("/tmp/plugins", "empty.rb").and_return("/tmp/plugins/empty.rb")
- File.stub(:expand_path).with("/tmp/plugins/empty.rb").and_return("/tmp/plugins/empty.rb")
- File.stub(:exist?).with("/tmp/plugins/empty.rb").and_return(true)
- @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb").and_return(plugin)
-
- found_plugin = @ohai.plugin_for("empty")
- found_plugin.should eql(plugin)
- end
-
- it "should find a plugin with a complex name" do
- plugin = @klass.new(@ohai, "/tmp/plugins/empty.rb")
- File.stub(:join).with("/tmp/plugins", "ubuntu/empty.rb").and_return("/tmp/plugins/ubuntu/empty.rb")
- File.stub(:expand_path).with("/tmp/plugins/ubuntu/empty.rb").and_return("/tmp/plugins/ubuntu/empty.rb")
- File.stub(:exist?).with("/tmp/plugins/ubuntu/empty.rb").and_return(true)
- @loader.stub(:load_plugin).with("/tmp/plugins/ubuntu/empty.rb").and_return(plugin)
-
- found_plugin = @ohai.plugin_for("ubuntu::empty")
- found_plugin.should eql(plugin)
- end
-
- it "should return nil if a plugin is not found" do
- File.stub(:join).with("/tmp/plugins", "fake.rb").and_return("/tmp/plugins/fake.rb")
- File.stub(:expand_path).with("/tmp/plugins/fake.rb").and_return("/tmp/plugins/fake.rb")
- File.should_receive(:exist?).with("/tmp/plugins/fake.rb").and_return(false)
-
- @ohai.plugin_for("fake").should be_nil
+ it "should collect all the data properly" do
+ @ohai.all_plugins
+ @ohai.data[:v7message].should == "Hellos from 7: animals"
+ @ohai.data[:zoo].should == "animals"
+ @ohai.data[:message][:v6message].should == "Hellos from 6"
+ @ohai.data[:message][:copy_message].should == "Hellos from 7: animals"
+ end
end
- it "should add the plugin to the v6_dependency_solver" do
- plugin = @klass.new(@ohai, "/tmp/plugins/empty.rb")
- File.stub(:join).with("/tmp/plugins", "empty.rb").and_return("/tmp/plugins/empty.rb")
- File.stub(:expand_path).with("/tmp/plugins/empty.rb").and_return("/tmp/plugins/empty.rb")
- File.stub(:exist?).with("/tmp/plugins/empty.rb").and_return(true)
- @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb").and_return(plugin)
-
- @ohai.plugin_for("empty")
- @ohai.v6_dependency_solver.should have_key('empty')
- @ohai.v6_dependency_solver['empty'].should eql(plugin)
- end
end
end