diff options
author | sersut <serdar@opscode.com> | 2014-01-14 11:36:14 -0800 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2014-01-14 11:36:14 -0800 |
commit | 9502152f9044791b730c239f0a99bf4b7e817ee8 (patch) | |
tree | 57630d811fb00481beb81f4dda56d34b7bfdf105 | |
parent | 8890c7f6ac94bc50875726971a6ecc420811e8f7 (diff) | |
download | ohai-9502152f9044791b730c239f0a99bf4b7e817ee8.tar.gz |
Reset the plugin information when all_plugins is called consecutively in order to pick up any changes to the plugins that happened in the meantime.
This is needed for ohai resource of Chef which calls all_plugins during :reload action.
-rw-r--r-- | lib/ohai/system.rb | 36 | ||||
-rw-r--r-- | spec/unit/runner_spec.rb | 10 | ||||
-rw-r--r-- | spec/unit/system_spec.rb | 39 |
3 files changed, 76 insertions, 9 deletions
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 8569ba2c..f5414cf8 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -38,16 +38,23 @@ module Ohai attr_reader :v6_dependency_solver def initialize + @plugin_path = "" + reset_system + end + + def reset_system @data = Mash.new @provides_map = ProvidesMap.new @v6_dependency_solver = Hash.new - @plugin_path = "" @loader = Ohai::Loader.new(self) @runner = Ohai::Runner.new(self, true) Ohai::Hints.refresh_hints() + + # Remove the previously defined plugins + recursive_remove_constants(Ohai::NamedPlugin) end def [](key) @@ -55,6 +62,11 @@ module Ohai end def all_plugins(attribute_filter=nil) + # Reset the system when all_plugins is called since this function + # can be run multiple times in order to pick up any changes in the + # config or plugins with Chef. + reset_system + load_plugins run_plugins(true, attribute_filter) end @@ -138,10 +150,10 @@ module Ohai # of them whenever called. def refresh_plugins(attribute_filter=nil) Ohai::Hints.refresh_hints() - @provides_map.all_plugins(Array(attribute_filter)).each do |plugin| + @provides_map.all_plugins(attribute_filter).each do |plugin| plugin.reset! end - run_plugins(true, Array(attribute_filter)) + run_plugins(true, attribute_filter) end # @@ -178,5 +190,23 @@ module Ohai end end + private + def recursive_remove_constants(object) + if object.respond_to?(:constants) + object.constants.each do |const| + next unless strict_const_defined?(object, const) + recursive_remove_constants(object.const_get(const)) + object.send(:remove_const, const) + end + end + end + + def strict_const_defined?(object, const) + if object.method(:const_defined?).arity == 1 + object.const_defined?(const) + else + object.const_defined?(const, false) + end + end end end diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb index f24d6373..880f1ebb 100644 --- a/spec/unit/runner_spec.rb +++ b/spec/unit/runner_spec.rb @@ -284,8 +284,7 @@ describe Ohai::Runner, "run_plugin" do end describe "when a cycle is detected" do - let(:ohai) { Ohai::System.new } - let(:runner) { Ohai::Runner.new(ohai, true) } + let(:runner) { Ohai::Runner.new(@ohai, true) } context "when there are no edges in the cycle (A->A)" do let(:plugin_class) do @@ -297,10 +296,10 @@ describe Ohai::Runner, "run_plugin" do end end end - let(:plugin) { plugin_class.new(ohai.data) } + let(:plugin) { plugin_class.new(@ohai.data) } it "ignores the cycle" do - ohai.provides_map.set_providers_for(plugin, ["thing"]) + @ohai.provides_map.set_providers_for(plugin, ["thing"]) expected_error_string = "Dependency cycle detected. Please refer to the following plugins: Thing, Other" runner.run_plugin(plugin) # should not raise @@ -327,8 +326,9 @@ describe Ohai::Runner, "run_plugin" do @plugins = [] [klass1, klass2].each_with_index do |klass, idx| - @plugins << klass.new(ohai.data) + @plugins << klass.new(@ohai.data) end + @plugin1, @plugin2 = @plugins end diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index d40533bc..306c33b0 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -209,7 +209,9 @@ EOF ohai_system.v6_dependency_solver["v6_plugin"] = v6_plugin - ohai_system.all_plugins("primary") + # Instead of calling all plugins we call load and run directly so that the information we setup is not cleared by all_plugins + ohai_system.load_plugins + ohai_system.run_plugins(true, "primary") end # This behavior choice is somewhat arbitrary, based on what creates the @@ -286,6 +288,8 @@ EOF it "should write an error to Ohai::Log" do Ohai::Config[:plugin_path] = [ path_to(".") ] + # Make sure the stubbing of runner is not overriden with reset_system during test + @ohai.stub(:reset_system) @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) @@ -524,6 +528,39 @@ EOF end end + describe "when Chef OHAI resource executes :reload action" do + when_plugins_directory "contains a random plugin" do + with_plugin("random.rb", <<-E) + Ohai.plugin(:Random) do + provides 'random' + + collect_data do + random rand(100) + end + end + E + + before do + @ohai = Ohai::System.new + @original_config = Ohai::Config[:plugin_path] + Ohai::Config[:plugin_path] = [ path_to(".") ] + end + + after do + Ohai::Config[:plugin_path] = @original_config + end + + it "should rerun the plugin providing the desired attributes", :focus => true do + @ohai.all_plugins + initial_value = @ohai.data["random"] + @ohai.all_plugins + updated_value = @ohai.data["random"] + initial_value.should_not == updated_value + end + + end + end + describe "when refreshing plugins" do when_plugins_directory "contains v7 plugins" do with_plugin("desired.rb", <<-E) |