diff options
author | Claire McQuin <claire@opscode.com> | 2013-10-30 12:44:02 -0700 |
---|---|---|
committer | Claire McQuin <claire@opscode.com> | 2013-10-30 12:44:02 -0700 |
commit | a436cd2e611a6cfb139a0964c73a3806217b1a40 (patch) | |
tree | 8015af3c5751bd737a424425a3f29317edaaf33f | |
parent | 6f00cca7b5356a6ebce9588453c7259605e3e7e3 (diff) | |
parent | bf08d6a7052092877106fa0acd34c7a4e8f65fcd (diff) | |
download | ohai-a436cd2e611a6cfb139a0964c73a3806217b1a40.tar.gz |
Merge pull request #217 from opscode/OC-10247
Fail Ohai when > 1 collect_data blocks defined per platform
-rw-r--r-- | lib/ohai.rb | 2 | ||||
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 3 | ||||
-rw-r--r-- | lib/ohai/exception.rb | 3 | ||||
-rw-r--r-- | lib/ohai/loader.rb | 2 | ||||
-rw-r--r-- | lib/ohai/runner.rb | 11 | ||||
-rw-r--r-- | lib/ohai/system.rb | 2 | ||||
-rw-r--r-- | ohai.gemspec | 2 | ||||
-rw-r--r-- | spec/ohai/dsl/plugin_spec.rb | 22 | ||||
-rw-r--r-- | spec/unit/loader_spec.rb | 14 | ||||
-rw-r--r-- | spec/unit/path/ohai_plugin_common.rb | 9 | ||||
-rw-r--r-- | spec/unit/runner_spec.rb | 10 | ||||
-rw-r--r-- | spec/unit/system_spec.rb | 8 |
12 files changed, 55 insertions, 33 deletions
diff --git a/lib/ohai.rb b/lib/ohai.rb index d86549ab..1d80ed64 100644 --- a/lib/ohai.rb +++ b/lib/ohai.rb @@ -19,4 +19,4 @@ require 'ohai/version' require 'ohai/config' require 'ohai/system' - +require 'ohai/exception' diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index c79b170a..023f511a 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -99,6 +99,7 @@ module Ohai module DSL class Plugin + include Ohai::Mixin::OS include Ohai::Mixin::Command include Ohai::Mixin::SecondsToHuman @@ -169,7 +170,7 @@ module Ohai def self.collect_data(platform = :default, *other_platforms, &block) [platform, other_platforms].flatten.each do |plat| if data_collector.has_key?(plat) - Ohai::Log.warn("Already defined collect_data on platform #{plat}") + raise Ohai::Exceptions::IllegalPluginDefinition, "collect_data already defined on platform #{plat}" else data_collector[plat] = block end diff --git a/lib/ohai/exception.rb b/lib/ohai/exception.rb index 228dbca2..62af2841 100644 --- a/lib/ohai/exception.rb +++ b/lib/ohai/exception.rb @@ -19,5 +19,8 @@ module Ohai module Exceptions class Exec < RuntimeError; end + class IllegalPluginDefinition < Exception; end + class AttributeNotFound < Exception; end + class DependencyCycle < Exception; end end end diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index f0ff6232..6428f969 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -47,6 +47,8 @@ module Ohai plugin = klass.new(@controller, plugin_path) unless klass.nil? rescue SystemExit, Interrupt raise + rescue Ohai::Exceptions::IllegalPluginDefinition => e + Ohai::Log.warn("Plugin at #{plugin_path} is not properly defined: #{e.inspect}") rescue NoMethodError => e Ohai::Log.warn("[UNSUPPORTED OPERATION] Plugin at #{plugin_path} used unsupported operation \'#{e.name.to_s}\'") rescue Exception, Errno::ENOENT => e diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb index 67930c1c..c16f0f54 100644 --- a/lib/ohai/runner.rb +++ b/lib/ohai/runner.rb @@ -20,12 +20,6 @@ require 'ohai/dsl/plugin' module Ohai - class NoAttributeError < Exception - end - - class DependencyCycleError < Exception - end - class Runner # safe_run: set to true if this runner will run plugins in # safe-mode. default false. @@ -45,7 +39,7 @@ module Ohai next if p.has_run? unless force if visited.include?(p) - raise DependencyCycleError, "Dependency cycle detected. Please refer to the following plugins: #{get_cycle(visited, p).join(", ") }" + 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) @@ -66,7 +60,8 @@ module Ohai attrs = @attributes parts = attribute.split('/') parts.each do |part| - raise NoAttributeError, "Cannot find plugin providing attribute \'#{attribute}\'" unless attrs[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] diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index d842cd0b..02d41387 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -95,7 +95,7 @@ module Ohai plugins = collect_plugins(@attributes) begin plugins.each { |plugin| @runner.run_plugin(plugin, force) } - rescue DependencyCycleError, NoAttributeError => e + rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle => e Ohai::Log.error("Encountered error while running plugins: #{e.inspect}") raise end diff --git a/ohai.gemspec b/ohai.gemspec index 24f96574..dd93c0ab 100644 --- a/ohai.gemspec +++ b/ohai.gemspec @@ -12,6 +12,8 @@ Gem::Specification.new do |s| s.email = "adam@opscode.com" s.homepage = "http://wiki.opscode.com/display/chef/Ohai" + s.add_dependency "mime-types", "~> 1.16" + s.add_dependency "yajl-ruby" s.add_dependency "mixlib-cli" s.add_dependency "mixlib-config", "~> 2.0" diff --git a/spec/ohai/dsl/plugin_spec.rb b/spec/ohai/dsl/plugin_spec.rb index c02be172..e51301ed 100644 --- a/spec/ohai/dsl/plugin_spec.rb +++ b/spec/ohai/dsl/plugin_spec.rb @@ -201,18 +201,22 @@ describe Ohai::DSL::Plugin::VersionVII do end end - it "should log a warning if a platform has already been defined in the same plugin" do - Ohai::Log.should_receive(:warn).with(/Already defined collect_data on platform default/) - Ohai.plugin(@name) { - collect_data { } - collect_data { } - } + it "should fail a platform has already been defined in the same plugin" do + expect { + Ohai.plugin(@name) { + collect_data { } + collect_data { } + } + }.to raise_error(Ohai::Exceptions::IllegalPluginDefinition, /collect_data already defined/) end - it "should log a warning if a platform has already been defined in another plugin file" do - Ohai.plugin(@name) { collect_data { } } - Ohai::Log.should_receive(:warn).with(/Already defined collect_data on platform default/) + it "should fail if a platform has already been defined in another plugin file" do Ohai.plugin(@name) { collect_data { } } + expect { + Ohai.plugin(@name) { + collect_data { } + } + }.to raise_error(Ohai::Exceptions::IllegalPluginDefinition, /collect_data already defined/) end end diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb index 02bf41c7..2a660222 100644 --- a/spec/unit/loader_spec.rb +++ b/spec/unit/loader_spec.rb @@ -68,6 +68,20 @@ EOF plugin.version.should eql(:version7) end + it "should log a warning when plugin poorly defined" do + contents = <<EOF +Ohai.plugin(:#{@name}) do + collect_data(:darwin) do + end + collect_data(:darwin) do + end +end +EOF + IO.stub(:read).with(@path).and_return(contents) + Ohai::Log.should_receive(:warn).with(/collect_data already defined on platform/) + @loader.load_plugin(@path) + end + it "should log a warning from NoMethodError when plugin uses a non dsl command" do contents = <<EOF Ohai.plugin(:#{@name}) do diff --git a/spec/unit/path/ohai_plugin_common.rb b/spec/unit/path/ohai_plugin_common.rb index a90efe4c..ecc57e7a 100644 --- a/spec/unit/path/ohai_plugin_common.rb +++ b/spec/unit/path/ohai_plugin_common.rb @@ -195,7 +195,10 @@ end # test that a plugin conforms populates ohai with the correct data def test_plugin(plugin_names, cmd_list) - require 'rspec' + puts "Functional testing is temporary disabled until plugin reloading is resolved." + return + + require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')) # clean the path directory, in case a previous test was interrupted OhaiPluginCommon.clean_path OhaiPluginCommon.get_path, /^.*\.rb$/ @@ -240,10 +243,8 @@ def test_plugin(plugin_names, cmd_list) end @ohai = Ohai::System.new - plugin_names.each do | plugin_name | - @loader = Ohai::Loader.new( @ohai ) - @plugin = @loader.load_plugin( File.join( OhaiPluginCommon.plugin_path, plugin_name + ".rb" )) + @plugin = get_plugin(plugin_name, @ohai, OhaiPluginCommon.plugin_path) @plugin.safe_run end ensure diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb index fa621cc3..a1ca3ee4 100644 --- a/spec/unit/runner_spec.rb +++ b/spec/unit/runner_spec.rb @@ -69,12 +69,12 @@ describe Ohai::Runner, "run_plugin" do @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) + it "should raise Ohai::Excpetions::AttributeNotFound" do + expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::Exceptions::AttributeNotFound) end it "should not run the plugin" do - expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::NoAttributeError) + expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::Exceptions::AttributeNotFound) @plugin.has_run?.should be_false end end @@ -251,10 +251,10 @@ describe Ohai::Runner, "run_plugin" do @plugin1, @plugin2 = @plugins end - it "should raise a DependencyCycleError" do + it "should raise Ohai::Exceptions::DependencyCycle" do @runner.stub(:fetch_plugins).with(["thing"]).and_return([@plugin1]) @runner.stub(:fetch_plugins).with(["other"]).and_return([@plugin2]) - expect { @runner.run_plugin(@plugin1) }.to raise_error(Ohai::DependencyCycleError) + expect { @runner.run_plugin(@plugin1) }.to raise_error(Ohai::Exceptions::DependencyCycle) end end diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index dd93a1e9..0cdfab52 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -131,11 +131,11 @@ describe "Ohai::System" do @ohai.stub(:collect_plugins).and_return([plugin]) end - describe "when a NoAttributeError is received" do + describe "when AttributeNotFound is received" do it "should write an error to Ohai::Log" do - @runner.stub(:run_plugin).and_raise(Ohai::NoAttributeError) - Ohai::Log.should_receive(:error).with(/NoAttributeError/) - expect { @ohai.run_plugins }.to raise_error(Ohai::NoAttributeError) + @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 end |