diff options
author | Thom May <thom@may.lt> | 2018-02-16 09:55:31 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-16 09:55:31 +0000 |
commit | 380cf7eba190b3c26f69f29462ed42add6ebd56e (patch) | |
tree | ada8eee5d95adb6d4c6310c5366973e361641939 | |
parent | 39200b29290b1cc4c42d62a48bc689092d5d5d97 (diff) | |
parent | 5a662a4463cce6cd479e8042f92fd6bfb9c85269 (diff) | |
download | ohai-380cf7eba190b3c26f69f29462ed42add6ebd56e.tar.gz |
Merge pull request #1127 from chef/remove_v6_plugins
Initial removal of support for Ohai V6 plugins
-rw-r--r-- | lib/ohai/dsl.rb | 1 | ||||
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 1 | ||||
-rw-r--r-- | lib/ohai/dsl/plugin/versionvi.rb | 61 | ||||
-rw-r--r-- | lib/ohai/loader.rb | 56 | ||||
-rw-r--r-- | lib/ohai/runner.rb | 11 | ||||
-rw-r--r-- | lib/ohai/system.rb | 66 | ||||
-rw-r--r-- | spec/data/plugins/messages.rb | 8 | ||||
-rw-r--r-- | spec/data/plugins/v6message.rb | 2 | ||||
-rw-r--r-- | spec/unit/dsl/plugin_spec.rb | 33 | ||||
-rw-r--r-- | spec/unit/loader_spec.rb | 62 | ||||
-rw-r--r-- | spec/unit/plugins/fail_spec.rb | 55 | ||||
-rw-r--r-- | spec/unit/runner_spec.rb | 37 | ||||
-rw-r--r-- | spec/unit/system_spec.rb | 353 |
13 files changed, 7 insertions, 739 deletions
diff --git a/lib/ohai/dsl.rb b/lib/ohai/dsl.rb index 025f6be5..7ee6645a 100644 --- a/lib/ohai/dsl.rb +++ b/lib/ohai/dsl.rb @@ -18,5 +18,4 @@ # require "ohai/dsl/plugin" -require "ohai/dsl/plugin/versionvi" require "ohai/dsl/plugin/versionvii" diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index f187aeaf..a0d8c150 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -49,6 +49,7 @@ module Ohai plugin = nil + # avoid already initialized constant warnings if already defined if NamedPlugin.strict_const_defined?(name) plugin = NamedPlugin.const_get(name) plugin.class_eval(&block) diff --git a/lib/ohai/dsl/plugin/versionvi.rb b/lib/ohai/dsl/plugin/versionvi.rb deleted file mode 100644 index 2a96c0fb..00000000 --- a/lib/ohai/dsl/plugin/versionvi.rb +++ /dev/null @@ -1,61 +0,0 @@ -# -# Author:: Serdar Sutay (<serdar@chef.io>) -# Copyright:: Copyright (c) 2013-2016 Chef Software, 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. -# - -module Ohai - module DSL - class Plugin - class VersionVI < Plugin - attr_reader :version - attr_reader :source - - def initialize(controller, plugin_path, plugin_dir_path) - super(controller.data) - @controller = controller - @version = :version6 - @source = plugin_path - @plugin_dir_path = plugin_dir_path - end - - def name - # Ohai V6 doesn't have any name specification for plugins. - # So we are using the partial path to infer the name of the plugin - partial_path = Pathname.new(@source).relative_path_from(Pathname.new(@plugin_dir_path)).to_s - partial_path.chomp(".rb").gsub("/", "::") - end - - def self.version - :version6 - end - - def self.collect_contents(contents) - define_method(:run_plugin) { instance_eval(contents) } - end - - def provides(*paths) - Ohai::Log.debug("Skipping provides '#{paths.join(",")}' for plugin '#{name}'") - end - - def require_plugin(plugin_ref) - @controller.require_plugin(plugin_ref) - end - - end - end - end -end diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index f91425fc..d4d78811 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -53,13 +53,8 @@ module Ohai end end - # Simple struct to track a v6 plugin's class, file path, and the root of - # the plugin dir from which it was loaded. - V6PluginClass = Struct.new(:plugin_class, :plugin_path, :plugin_dir_path) - def initialize(controller) @controller = controller - @v6_plugin_classes = [] @v7_plugin_classes = [] end @@ -76,7 +71,6 @@ module Ohai load_plugin_class(plugin_file.path, plugin_file.plugin_root) end - collect_v6_plugins collect_v7_plugins end @@ -92,14 +86,10 @@ module Ohai # Load a specified file as an ohai plugin and creates an instance of it. # Not used by ohai itself, but can be used to load a plugin for testing # purposes. - # plugin_dir_path is required when loading a v6 plugin. def load_plugin(plugin_path, plugin_dir_path = nil) plugin_class = load_plugin_class(plugin_path, plugin_dir_path) return nil unless plugin_class.kind_of?(Class) - case - when plugin_class < Ohai::DSL::Plugin::VersionVI - load_v6_plugin(plugin_class, plugin_path, plugin_dir_path) - when plugin_class < Ohai::DSL::Plugin::VersionVII + if plugin_class < Ohai::DSL::Plugin::VersionVII load_v7_plugin(plugin_class) else raise Exceptions::IllegalPluginDefinition, "cannot create plugin of type #{plugin_class}" @@ -127,12 +117,10 @@ module Ohai if contents.include?("Ohai.plugin") load_v7_plugin_class(contents, plugin_path) else - Ohai::Log.warn("[DEPRECATION] Plugin at #{plugin_path} is a version 6 plugin. \ -Version 6 plugins will not be supported in Chef/Ohai 14. \ -Please upgrade your plugin to version 7 plugin format. \ -For more information visit here: docs.chef.io/ohai_custom.html") - - load_v6_plugin_class(contents, plugin_path, plugin_dir_path) + raise Exceptions::IllegalPluginDefinition, "[DEPRECATION] Plugin at #{plugin_path}"\ + " is a version 6 plugin. Version 6 plugins are no longer supported by Ohai. This"\ + " plugin will need to be updated to the v7 Ohai plugin format. See"\ + " https://docs.chef.io/ohai_custom.html for v7 syntax." end end @@ -143,46 +131,12 @@ For more information visit here: docs.chef.io/ohai_custom.html") @controller.provides_map.set_providers_for(plugin, plugin_provides) end - def v6_dependency_solver - @controller.v6_dependency_solver - end - - def collect_v6_plugins - @v6_plugin_classes.each do |plugin_spec| - plugin = load_v6_plugin(plugin_spec.plugin_class, plugin_spec.plugin_path, plugin_spec.plugin_dir_path) - loaded_v6_plugin(plugin, plugin_spec.plugin_path, plugin_spec.plugin_dir_path) - end - end - def collect_v7_plugins @v7_plugin_classes.each do |plugin_class| load_v7_plugin(plugin_class) end end - def load_v6_plugin_class(contents, plugin_path, plugin_dir_path) - plugin_class = Class.new(Ohai::DSL::Plugin::VersionVI) { collect_contents(contents) } - @v6_plugin_classes << V6PluginClass.new(plugin_class, plugin_path, plugin_dir_path) - plugin_class - end - - def load_v6_plugin(plugin_class, plugin_path, plugin_dir_path) - plugin_class.new(@controller, plugin_path, plugin_dir_path) - end - - # 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. - def loaded_v6_plugin(plugin, plugin_file_path, plugin_dir_path) - partial_path = Pathname.new(plugin_file_path).relative_path_from(Pathname.new(plugin_dir_path)).to_s - - 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 - end - def load_v7_plugin_class(contents, plugin_path) plugin_class = eval(contents, TOPLEVEL_BINDING, plugin_path) unless plugin_class.kind_of?(Class) && plugin_class < Ohai::DSL::Plugin diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb index 05fd7cfc..ba3985dc 100644 --- a/lib/ohai/runner.rb +++ b/lib/ohai/runner.rb @@ -42,11 +42,8 @@ module Ohai end begin - case plugin.version - when :version7 + if plugin.version == :version7 run_v7_plugin(plugin) - when :version6 - run_v6_plugin(plugin) else raise Ohai::Exceptions::InvalidPlugin, "Invalid plugin version #{plugin.version} for plugin #{plugin}" end @@ -61,12 +58,6 @@ module Ohai Ohai::Log.debug("Plugin #{plugin.name} took #{elapsed.total} seconds to run.") end - def run_v6_plugin(plugin) - return true if plugin.has_run? - - @safe_run ? plugin.safe_run : plugin.run - end - def run_v7_plugin(plugin) visited = [ plugin ] until visited.empty? diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 13d60bf0..78da59b4 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -37,7 +37,6 @@ module Ohai attr_accessor :data attr_reader :config attr_reader :provides_map - attr_reader :v6_dependency_solver # the cli flag is used to determine if we're being constructed by # something like chef-client (which doesn't not set this flag) and @@ -54,7 +53,6 @@ module Ohai def reset_system @data = Mash.new @provides_map = ProvidesMap.new - @v6_dependency_solver = Hash.new configure_ohai configure_logging if @cli @@ -87,19 +85,6 @@ module Ohai end def run_plugins(safe = false, attribute_filter = nil) - # First run all the version 6 plugins - @v6_dependency_solver.values.each do |v6plugin| - @runner.run_plugin(v6plugin) - end - - # Users who are migrating from ohai 6 may give one or more Ohai 6 plugin - # names as the +attribute_filter+. In this case we return early because - # the v7 plugin provides map will not have an entry for this plugin. - if attribute_filter && Array(attribute_filter).all? { |filter_item| have_v6_plugin?(filter_item) } - return true - end - - # Then run all the version 7 plugins begin @provides_map.all_plugins(attribute_filter).each do |plugin| @runner.run_plugin(plugin) @@ -132,57 +117,6 @@ module Ohai freeze_strings! end - def have_v6_plugin?(name) - @v6_dependency_solver.values.any? { |v6plugin| v6plugin.name == name } - end - - def pathify_v6_plugin(plugin_name) - path_components = plugin_name.split("::") - File.join(path_components) + ".rb" - 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 - # While looking up V7 plugins we need to convert the plugin_ref to an attribute. - attribute = plugin_ref.gsub("::", "/") - begin - plugins = @provides_map.find_providers_for([attribute]) - rescue Ohai::Exceptions::AttributeNotFound - Ohai::Log.debug("Can not find any v7 plugin that provides #{attribute}") - plugins = [ ] - end - end - - if plugins.empty? - raise Ohai::Exceptions::DependencyNotFound, "Can not find a plugin for dependency #{plugin_ref}" - else - plugins.each do |plugin| - begin - @runner.run_plugin(plugin) - 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 - end - # Re-runs plugins that provide the attributes specified by # +attribute_filter+. If +attribute_filter+ is not given, re-runs all # plugins. diff --git a/spec/data/plugins/messages.rb b/spec/data/plugins/messages.rb deleted file mode 100644 index 98a621d4..00000000 --- a/spec/data/plugins/messages.rb +++ /dev/null @@ -1,8 +0,0 @@ -require_plugin 'v6message' -require_plugin 'v7message' - -provides 'messages' - -messages Mash.new -messages[:v6message] = v6message -messages[:v7message] = v7message diff --git a/spec/data/plugins/v6message.rb b/spec/data/plugins/v6message.rb deleted file mode 100644 index c21982fe..00000000 --- a/spec/data/plugins/v6message.rb +++ /dev/null @@ -1,2 +0,0 @@ -provides 'v6message' -v6message "update me!" diff --git a/spec/unit/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb index c1ed623c..5000b112 100644 --- a/spec/unit/dsl/plugin_spec.rb +++ b/spec/unit/dsl/plugin_spec.rb @@ -631,36 +631,3 @@ describe Ohai::DSL::Plugin::VersionVII do let(:version) { :version7 } end end - -describe Ohai::DSL::Plugin::VersionVI do - describe "#version" do - it "saves the plugin version as :version6" do - plugin = Class.new(Ohai::DSL::Plugin::VersionVI) {} - expect(plugin.version).to eql(:version6) - end - end - - describe "#provides" do - let(:ohai) { Ohai::System.new } - - it "logs a debug message when provides is used" do - allow(Ohai::Log).to receive(:debug) - expect(Ohai::Log).to receive(:debug).with(/Skipping provides/) - plugin = Ohai::DSL::Plugin::VersionVI.new(ohai, "/some/plugin/path.rb", "/some/plugin") - plugin.provides("attribute") - end - - it "does not update the provides map for version 6 plugins." do - plugin = Ohai::DSL::Plugin::VersionVI.new(ohai, "/some/plugin/path.rb", "/some/plugin") - plugin.provides("attribute") - expect(ohai.provides_map.map).to be_empty - end - - end - - it_behaves_like "Ohai::DSL::Plugin" do - let(:ohai) { Ohai::System.new } - let(:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, "/some/plugin/path.rb", "/some/plugin") } - let(:version) { :version6 } - end -end diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb index 8331ae65..07384c6d 100644 --- a/spec/unit/loader_spec.rb +++ b/spec/unit/loader_spec.rb @@ -33,68 +33,6 @@ describe Ohai::Loader do end end - when_plugins_directory "contains both V6 & V7 plugins" do - with_plugin("zoo.rb", <<EOF) -Ohai.plugin(:Zoo) do - provides 'seals' -end -EOF - - with_plugin("zoo_too.rb", <<EOF) -Ohai.plugin(:Zoo) do - provides 'elephants' -end -EOF - - with_plugin("lake.rb", <<EOF) -provides 'fish' -EOF - - describe "load_plugin() method" do - describe "when loading a v7 plugin" do - let(:plugin) { loader.load_plugin(path_to("zoo.rb")) } - - it "saves the plugin according to its attribute" do - plugin - expect(provides_map.map.keys).to include("seals") - end - - it "saves a single plugin source" do - expect(plugin.source).to eql([path_to("zoo.rb")]) - end - - it "saves all plugin sources" do - plugin - loader.load_plugin(path_to("zoo_too.rb")) - expect(plugin.source).to eql([path_to("zoo.rb"), path_to("zoo_too.rb")]) - end - end - - describe "when loading a v6 plugin" do - let(:plugin) { loader.load_plugin(path_to("lake.rb"), path_to(".")) } - - before(:each) do - expect(Ohai::Log).to receive(:warn).with(/\[DEPRECATION\]/) - end - - it "does not add this plugin's provided attributes to the provides map" do - plugin - expect(provides_map.map).to be_empty - end - - it "saves the plugin's source" do - expect(plugin.source).to eql(path_to("lake.rb")) - end - end - - it "logs a warning if a plugin doesn't exist" do - expect(Ohai::Log).to receive(:warn).with(/Unable to open or read plugin/) - loader.load_plugin(path_to("rainier.rb"), path_to(".")) - expect(provides_map.map).to be_empty - end - end - end - when_plugins_directory "is an additional plugin path" do with_plugin("cookbook_a/alpha.rb", <<EOF) Ohai.plugin(:Alpha) do diff --git a/spec/unit/plugins/fail_spec.rb b/spec/unit/plugins/fail_spec.rb index 859011c7..ba629601 100644 --- a/spec/unit/plugins/fail_spec.rb +++ b/spec/unit/plugins/fail_spec.rb @@ -59,11 +59,6 @@ shared_examples "a v7 loading failure" do expect { @ohai.provides_map.find_providers_for(["fail"]) }.to raise_error(Ohai::Exceptions::AttributeNotFound) end - it "should not have source key" do - @loader.load_plugin("#{tmp}/plugins/fail.rb") - expect(@ohai.v6_dependency_solver).not_to have_key("#{tmp}/plugins/fail.rb") - end - it "should write to Ohai::Log" do expect(Ohai::Log).to receive(:warn).once @loader.load_plugin("#{tmp}/plugins/fail.rb") @@ -107,11 +102,6 @@ shared_examples "a v7 loading success" do expect(@ohai.provides_map).to have_key("fail") end - it "should have source key" do - @loader.load_plugin("#{tmp}/plugins/fail.rb") - expect(@ohai.v6_dependency_solver).to have_key("#{tmp}/plugins/fail.rb") - end - it "should not write to Ohai::Log" do expect(Ohai::Log).not_to receive(:warn) @loader.load_plugin("#{tmp}/plugins/fail.rb") @@ -161,51 +151,6 @@ shared_examples "a v7 run failure" do end end -=begin -shared_examples "a v6 run failure" do - before(:all) do - begin - Dir.mkdir("#{tmp}/plugins") - rescue Errno::EEXIST - # ignore - end - end - - before(:each) do - fail_file = File.open("#{tmp}/plugins/fail.rb", "w+") - fail_file.write(failstr) - fail_file.close - end - - after(:each) do - File.delete("#{tmp}/plugins/fail.rb") - end - - after(:all) do - begin - Dir.delete("#{tmp}/plugins") - rescue - # ignore - end - end - - before(:each) do - @ohai = Ohai::System.new - @loader = Ohai::Loader.new(@ohai) - end - - it "should not add data keys" do - @loader.load_plugin("#{tmp}/plugins/fail.rb") - @ohai.data.should_not have_key("fail") - end - - it "should write to Ohai::Log" do - Ohai::Log.should_receive(:warn).once - @loader.load_plugin("#{tmp}/plugins/fail.rb").new(@ohai).run - end -end -=end - describe "when using DSL commands outside Ohai.plugin block" do failstr1 = <<EOF provides "fail" diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb index a811c954..64baf51c 100644 --- a/spec/unit/runner_spec.rb +++ b/spec/unit/runner_spec.rb @@ -54,43 +54,6 @@ describe Ohai::Runner, "run_plugin" do end end - describe "version 6" do - let(:version) { :version6 } - - it "should call run_v6_plugin" do - expect(@runner).to 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 - expect(plugin).to 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 - expect(plugin).to receive(:run) - @runner.run_plugin(plugin) - end - end - end - - describe "if the plugin has run before" do - let(:has_run) { true } - - it "should not run" do - expect(plugin).not_to receive(:safe_run) - @runner.run_plugin(plugin) - end - - end - end - describe "invalid version" do let(:version) { :versionBla } diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb index 72aa9b0b..edaa202f 100644 --- a/spec/unit/system_spec.rb +++ b/spec/unit/system_spec.rb @@ -35,10 +35,6 @@ describe "Ohai::System" do expect(ohai.provides_map).to be_a_kind_of(Ohai::ProvidesMap) end - it "sets @v6_dependency_solver to a Hash" do - expect(ohai.v6_dependency_solver).to be_a_kind_of(Hash) - end - it "merges provided configuration options into the ohai config context" do config = { disabled_plugins: [ :Foo, :Baz ], @@ -90,29 +86,6 @@ describe "Ohai::System" do end end - when_plugins_directory "contains v6 and v7 plugins" do - with_plugin("zoo.rb", <<EOF) -Ohai.plugin(:Zoo) do - provides 'seals' -end -EOF - - with_plugin("lake.rb", <<EOF) -provides 'fish' -EOF - - before do - Ohai.config[:plugin_path] = [ path_to(".") ] - end - - it "load_plugins() should load all the plugins" do - ohai.load_plugins - expect(ohai.provides_map.map.keys).to include("seals") - expect(ohai.v6_dependency_solver.keys).to include("lake.rb") - expect(Ohai::NamedPlugin.const_get(:Zoo)).to eq(Ohai::NamedPlugin::Zoo) - end - end - when_plugins_directory "contains directories inside" do with_plugin("repo1/zoo.rb", <<EOF) Ohai.plugin(:Zoo) do @@ -142,8 +115,6 @@ EOF ohai.load_plugins expect(ohai.provides_map.map.keys).to include("seals") expect(ohai.provides_map.map.keys).to include("crabs") - expect(ohai.v6_dependency_solver.keys).to include("lake.rb") - expect(ohai.v6_dependency_solver.keys).to include("mountain.rb") expect(Ohai::NamedPlugin.const_get(:Zoo)).to eq(Ohai::NamedPlugin::Zoo) expect(Ohai::NamedPlugin.const_get(:Nature)).to eq(Ohai::NamedPlugin::Nature) end @@ -151,123 +122,6 @@ EOF end describe "when running plugins" do - when_plugins_directory "contains v6 plugins only" do - with_plugin("zoo.rb", <<EOF) -provides 'zoo' -zoo("animals") -EOF - - with_plugin("park.rb", <<EOF) -provides 'park' -park("plants") -EOF - - it "should collect data from all the plugins" do - Ohai.config[:plugin_path] = [ path_to(".") ] - ohai.all_plugins - expect(ohai.data[:zoo]).to eq("animals") - expect(ohai.data[:park]).to eq("plants") - expect(ohai.data[:zoo]).to be_frozen - expect(ohai.data[:park]).to be_frozen - end - - describe "when using :disabled_plugins" do - before do - Ohai.config[:disabled_plugins] = [ "zoo" ] - end - - after do - Ohai.config[:disabled_plugins] = [ ] - end - - it "shouldn't run disabled version 6 plugins" do - Ohai.config[:plugin_path] = [ path_to(".") ] - ohai.all_plugins - expect(ohai.data[:zoo]).to be_nil - expect(ohai.data[:park]).to eq("plants") - end - end - - describe "when running in whitelist mode" do - let(:ohai_system) { Ohai::System.new } - - let(:primary_plugin_class) do - Ohai.plugin(:Primary) do - provides "primary" - depends "dependency/one" - depends "dependency/two" - collect_data {} - end - end - - let(:dependency_plugin_one_class) do - Ohai.plugin(:DependencyOne) do - provides "dependency/one" - collect_data {} - end - end - - let(:dependency_plugin_two_class) do - Ohai.plugin(:DependencyTwo) do - provides "dependency/two" - collect_data {} - end - end - - let(:unrelated_plugin_class) do - Ohai.plugin(:Unrelated) do - provides "whatever" - collect_data {} - end - end - - let(:v6_plugin_class) do - Class.new(Ohai::DSL::Plugin::VersionVI) { collect_contents("v6_key('v6_data')") } - end - - let(:primary_plugin) { primary_plugin_class.new(ohai_system) } - let(:dependency_plugin_one) { dependency_plugin_one_class.new(ohai_system) } - let(:dependency_plugin_two) { dependency_plugin_two_class.new(ohai_system) } - let(:unrelated_plugin) { unrelated_plugin_class.new(ohai_system) } - let(:v6_plugin) { v6_plugin_class.new(ohai_system, "/v6_plugin.rb", "/") } - - before do - allow(ohai_system).to receive(:load_plugins) # TODO: temporary hack - don't run unrelated plugins... - [ primary_plugin, dependency_plugin_one, dependency_plugin_two, unrelated_plugin].each do |plugin| - plugin_provides = plugin.class.provides_attrs - ohai_system.provides_map.set_providers_for(plugin, plugin_provides) - end - - ohai_system.v6_dependency_solver["v6_plugin"] = v6_plugin - - # 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 - # least code complexity in legacy v6 plugin format support. Once we - # ship 7.0, though, we need to stick to the same behavior. - it "runs v6 plugins" do - expect(v6_plugin.has_run?).to be true - end - - it "runs plugins that provide the requested attributes" do - expect(primary_plugin.has_run?).to be true - end - - it "runs dependencies of plugins that provide requested attributes" do - expect(dependency_plugin_one.has_run?).to be true - expect(dependency_plugin_two.has_run?).to be true - end - - it "does not run plugins that are irrelevant to the requested attributes" do - expect(unrelated_plugin.has_run?).to be false - end - - end - end - when_plugins_directory "contains a v7 plugins with :default and platform specific blocks" do with_plugin("message.rb", <<EOF) Ohai.plugin(:Message) do @@ -370,217 +224,10 @@ EOF end end - - when_plugins_directory "contains v6 & v7 plugins in different directories" do - with_plugin("my_plugins/zoo.rb", <<EOF) -Ohai.plugin(:Zoo) do - provides 'zoo' - - collect_data(:default) do - zoo("animals") - end -end -EOF - - with_plugin("my_plugins/nature.rb", <<EOF) -Ohai.plugin(:Nature) do - provides 'nature' - - collect_data(:default) do - nature("cougars") - end -end -EOF - - with_plugin("my_plugins/park.rb", <<EOF) -provides 'park' -park("plants") -EOF - - with_plugin("my_plugins/home.rb", <<EOF) -provides 'home' -home("dog") -EOF - - describe "when using :disabled_plugins" do - before do - Ohai.config[:disabled_plugins] = [ :Zoo, "my_plugins::park" ] - end - - after do - Ohai.config[:disabled_plugins] = [ ] - end - - it "shouldn't run disabled plugins" do - Ohai.config[:plugin_path] = [ path_to(".") ] - ohai.all_plugins - expect(ohai.data[:zoo]).to be_nil - expect(ohai.data[:nature]).to eq("cougars") - expect(ohai.data[:park]).to be_nil - expect(ohai.data[:home]).to eq("dog") - end - end - end - - when_plugins_directory "contains v6 plugins that depend on v7 plugins" do - with_plugin("messages.rb", <<EOF) -require_plugin 'v6message' -require_plugin 'v7message' - -provides 'messages' - -messages Mash.new -messages[:v6message] = v6message -messages[:v7message] = v7message -EOF - - with_plugin("v6message.rb", <<EOF) -provides 'v6message' -v6message "update me!" -EOF - - with_plugin("v7message.rb", <<EOF) -Ohai.plugin(:V7message) do - provides 'v7message' - - collect_data(:default) do - v7message "v7 plugins are awesome!" - end -end -EOF - - before do - Ohai.config[:plugin_path] = [ path_to(".") ] - end - - it "should collect all data" do - ohai.all_plugins - [:v6message, :v7message, :messages].each do |attribute| - expect(ohai.data).to have_key(attribute) - end - - expect(ohai.data[:v6message]).to eql("update me!") - expect(ohai.data[:v7message]).to eql("v7 plugins are awesome!") - [:v6message, :v7message].each do |subattr| - expect(ohai.data[:messages]).to have_key(subattr) - expect(ohai.data[:messages][subattr]).to eql(ohai.data[subattr]) - end - end - 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' - -message "From Version 6" -EOF - - with_plugin("v7/message.rb", <<EOF) -Ohai.plugin(:Message) do - provides 'message' - - collect_data(:default) do - message "From Version 7" - end -end -EOF - - before do - Ohai.config[:plugin_path] = [ path_to(".") ] - end - - it "version 6 should run" do - ohai.load_plugins - ohai.require_plugin("message") - expect(ohai.data[:message]).to eql("From Version 6") - end - end - - when_plugins_directory "a v6 plugin that requires a v7 plugin with dependencies" do - with_plugin("message.rb", <<EOF) -provides 'message' - -require_plugin 'v7message' - -message Mash.new -message[:v6message] = "Hellos from 6" -message[:copy_message] = v7message -EOF - - with_plugin("v7message.rb", <<EOF) -Ohai.plugin(:V7message) do - provides 'v7message' - depends 'zoo' - - collect_data(:default) do - v7message ("Hellos from 7: " + zoo) - end -end -EOF - - with_plugin("zoo.rb", <<EOF) -Ohai.plugin(:Zoo) do - provides 'zoo' - - collect_data(:default) do - zoo "animals" - end -end -EOF - - before do - Ohai.config[:plugin_path] = [ path_to(".") ] - end - - it "should collect all the data properly" do - ohai.all_plugins - expect(ohai.data[:v7message]).to eq("Hellos from 7: animals") - expect(ohai.data[:zoo]).to eq("animals") - expect(ohai.data[:message][:v6message]).to eq("Hellos from 6") - expect(ohai.data[:message][:copy_message]).to eq("Hellos from 7: animals") - end - end - - when_plugins_directory "a v6 plugin that requires non-existing v7 plugin" do - with_plugin("message.rb", <<EOF) -provides 'message' - -require_plugin 'v7message' - -message v7message -EOF - - before do - Ohai.config[:plugin_path] = [ path_to(".") ] - end - - it "should raise DependencyNotFound" do - expect { ohai.all_plugins }.to raise_error(Ohai::Exceptions::DependencyNotFound) - end - end end describe "when Chef OHAI resource executes :reload action" do - when_plugins_directory "contains a v6 plugin" do - with_plugin("a_v6plugin.rb", <<-E) - plugin_data Mash.new - plugin_data[:foo] = :bar - E - - before do - Ohai.config[:plugin_path] = [ path_to(".") ] - end - - it "reloads only the v6 plugin when given a specific plugin to load" do - ohai.all_plugins - expect { ohai.all_plugins("a_v6plugin") }.not_to raise_error - end - - end - when_plugins_directory "contains a random plugin" do with_plugin("random.rb", <<-E) Ohai.plugin(:Random) do |