summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@opscode.com>2013-10-17 17:25:18 -0700
committerClaire McQuin <claire@opscode.com>2013-10-17 17:25:18 -0700
commit71c4778d272db32a2db4948de734079a0e483e7f (patch)
treeb761057f539cfa769895785681a7afc4c74e7798
parent7a87dcdb0ee61f66ef70e1ad0d943d774f360b6d (diff)
parent1c5852515b76a7a260deb91afabba87fe067a739 (diff)
downloadohai-71c4778d272db32a2db4948de734079a0e483e7f.tar.gz
Merge pull request #214 from opscode/OC-10249
Rename providers -> plugins && seperate v6 methods from v7 methods in system.rb
-rw-r--r--lib/ohai/dsl/plugin.rb4
-rw-r--r--lib/ohai/loader.rb4
-rw-r--r--lib/ohai/runner.rb16
-rw-r--r--lib/ohai/system.rb96
-rw-r--r--spec/ohai/dsl/plugin_spec.rb17
-rw-r--r--spec/unit/loader_spec.rb2
-rw-r--r--spec/unit/runner_spec.rb84
-rw-r--r--spec/unit/system_spec.rb24
8 files changed, 135 insertions, 112 deletions
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb
index 34178986..c79b170a 100644
--- a/lib/ohai/dsl/plugin.rb
+++ b/lib/ohai/dsl/plugin.rb
@@ -236,8 +236,8 @@ module Ohai
a = a[part]
end
end
- a[:_providers] ||= []
- a[:_providers] << self
+ a[:_plugins] ||= []
+ a[:_plugins] << self
end
end
diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb
index c1ec7941..f0ff6232 100644
--- a/lib/ohai/loader.rb
+++ b/lib/ohai/loader.rb
@@ -78,8 +78,8 @@ module Ohai
end
end
- a[:_providers] ||= []
- a[:_providers] << plugin
+ a[:_plugins] ||= []
+ a[:_plugins] << plugin
end
end
diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb
index 15334e75..67930c1c 100644
--- a/lib/ohai/runner.rb
+++ b/lib/ohai/runner.rb
@@ -48,8 +48,8 @@ module Ohai
raise DependencyCycleError, "Dependency cycle detected. Please refer to the following plugins: #{get_cycle(visited, p).join(", ") }"
end
- dependency_providers = fetch_providers(p.dependencies)
- dependency_providers.delete_if { |provider| (!force && provider.has_run?) || provider.eql?(p) }
+ dependency_providers = fetch_plugins(p.dependencies)
+ dependency_providers.delete_if { |plugin| (!force && plugin.has_run?) || plugin.eql?(p) }
if dependency_providers.empty?
@safe_run ? p.safe_run : p.run
@@ -60,21 +60,19 @@ module Ohai
end
# returns a list of plugins which provide the given attributes
- def fetch_providers(attributes)
- providers = []
+ def fetch_plugins(attributes)
+ plugins = []
attributes.each do |attribute|
attrs = @attributes
parts = attribute.split('/')
parts.each do |part|
- next if part == Ohai::Mixin::OS.collect_os
raise NoAttributeError, "Cannot find plugin providing attribute \'#{attribute}\'" unless attrs[part]
attrs = attrs[part]
end
- providers << attrs[:_providers]
- providers.flatten!
+ plugins << attrs[:_plugins]
+ plugins.flatten!
end
- providers.uniq!
- providers
+ plugins.uniq
end
# given a list of plugins and the first plugin in the cycle,
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index 37d7a87d..d842cd0b 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -51,6 +51,14 @@ module Ohai
@data[key]
end
+ #=============================================
+ # Version 7 system commands
+ #=============================================
+ def all_plugins
+ load_plugins
+ run_plugins(true)
+ end
+
def load_plugins
Ohai::Config[:plugin_path].each do |path|
[
@@ -84,7 +92,7 @@ module Ohai
end
# collect and run version 7 plugins
- plugins = collect_providers(@attributes)
+ plugins = collect_plugins(@attributes)
begin
plugins.each { |plugin| @runner.run_plugin(plugin, force) }
rescue DependencyCycleError, NoAttributeError => e
@@ -94,55 +102,25 @@ module Ohai
true
end
- def all_plugins
- load_plugins
- run_plugins(true)
- end
-
- def collect_providers(providers)
- plugins = []
- if providers.is_a?(Mash)
- providers.keys.each do |provider|
- if provider.eql?("_providers")
- plugins << providers[provider]
+ def collect_plugins(plugins)
+ collected = []
+ if plugins.is_a?(Mash)
+ plugins.keys.each do |plugin|
+ if plugin.eql?("_plugins")
+ collected << plugins[plugin]
else
- plugins << collect_providers(providers[provider])
+ collected << collect_plugins(plugins[plugin])
end
end
else
- plugins << providers
- end
- plugins.flatten.uniq
- end
-
- # todo: fixup for running w/ new internals
- def refresh_plugins(path = '/')
- parts = path.split('/')
- if parts.length == 0
- h = @metadata
- else
- parts.shift if parts[0].length == 0
- h = @metadata
- parts.each do |part|
- break unless h.has_key?(part)
- h = h[part]
- end
- end
-
- refreshments = collect_providers(h)
- Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
-
- # remove the hints cache
- @hints = Hash.new
-
- refreshments.each do |r|
- @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
- end
- refreshments.each do |r|
- require_plugin(r) unless @seen_plugins.has_key?(r)
+ collected << plugins
end
+ collected.flatten.uniq
end
+ #=============================================
+ # Version 6 system commands
+ #=============================================
def require_plugin(plugin_name, force=false)
unless force
plugin = @v6_dependency_solver[plugin_name]
@@ -188,6 +166,38 @@ module Ohai
plugin
end
+ # todo: fix for running w/new internals
+ # add updated function to v7?
+ def refresh_plugins(path = '/')
+ parts = path.split('/')
+ if parts.length == 0
+ h = @metadata
+ else
+ parts.shift if parts[0].length == 0
+ h = @metadata
+ parts.each do |part|
+ break unless h.has_key?(part)
+ h = h[part]
+ end
+ end
+
+ refreshments = collect_plugins(h)
+ Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
+
+ # remove the hints cache
+ @hints = Hash.new
+
+ refreshments.each do |r|
+ @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
+ end
+ refreshments.each do |r|
+ require_plugin(r) unless @seen_plugins.has_key?(r)
+ end
+ end
+
+ #=============================================
+ # For outputting an Ohai::System object
+ #=============================================
# Serialize this object as a hash
def to_json
Yajl::Encoder.new.encode(@data)
diff --git a/spec/ohai/dsl/plugin_spec.rb b/spec/ohai/dsl/plugin_spec.rb
index 4bdda824..c02be172 100644
--- a/spec/ohai/dsl/plugin_spec.rb
+++ b/spec/ohai/dsl/plugin_spec.rb
@@ -1,5 +1,20 @@
#
+# Author:: Claire McQuin (<claire@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, 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 expressed or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License
#
require File.expand_path("../../../spec_helper.rb", __FILE__)
@@ -281,7 +296,7 @@ describe Ohai::DSL::Plugin::VersionVI do
plugins << p
end
- @ohai.attributes[:attribute][:_providers].should eql(plugins)
+ @ohai.attributes[:attribute][:_plugins].should eql(plugins)
end
end
diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb
index 12bc55e3..02bf41c7 100644
--- a/spec/unit/loader_spec.rb
+++ b/spec/unit/loader_spec.rb
@@ -112,7 +112,7 @@ EOF
end
plugins.each { |plugin| @loader.collect_provides(plugin) }
- @ohai.attributes[:attr][:_providers].should eql(plugins)
+ @ohai.attributes[:attr][:_plugins].should eql(plugins)
end
end
end
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb
index efa3985c..fa621cc3 100644
--- a/spec/unit/runner_spec.rb
+++ b/spec/unit/runner_spec.rb
@@ -35,7 +35,7 @@ describe Ohai::Runner, "run_plugin" do
end
it "should not find dependencies" do
- @runner.should_receive(:fetch_providers).with([]).and_return([])
+ @runner.should_receive(:fetch_plugins).with([]).and_return([])
@runner.run_plugin(@plugin)
end
@@ -104,18 +104,18 @@ describe Ohai::Runner, "run_plugin" do
it "should locate the provider" do
@ohai.attributes[:thing] = Mash.new
- @ohai.attributes[:thing][:_providers] = [@plugin1]
+ @ohai.attributes[:thing][:_plugins] = [@plugin1]
@ohai.attributes[:other] = Mash.new
- @ohai.attributes[:other][:_providers] = [@plugin2]
+ @ohai.attributes[:other][:_plugins] = [@plugin2]
- @runner.should_receive(:fetch_providers).twice.with(["thing"]).and_return([@plugin1])
- @runner.should_receive(:fetch_providers).with([]).and_return([])
+ @runner.should_receive(:fetch_plugins).twice.with(["thing"]).and_return([@plugin1])
+ @runner.should_receive(:fetch_plugins).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.stub(:fetch_plugins).with(["thing"]).and_return([@plugin1])
+ @runner.stub(:fetch_plugins).with([]).and_return([])
@runner.run_plugin(@plugin2)
@plugins.each do |plugin|
@@ -149,18 +149,18 @@ describe Ohai::Runner, "run_plugin" do
it "should locate each provider" do
@ohai.attributes[:thing] = Mash.new
- @ohai.attributes[:thing][:_providers] = [@plugin1, @plugin2]
+ @ohai.attributes[:thing][:_plugins] = [@plugin1, @plugin2]
@ohai.attributes[:other] = Mash.new
- @ohai.attributes[:other][:_providers] = [@plugin3]
+ @ohai.attributes[:other][:_plugins] = [@plugin3]
- @runner.should_receive(:fetch_providers).exactly(3).times.with(["thing"]).and_return([@plugin1, @plugin2])
- @runner.should_receive(:fetch_providers).twice.with([]).and_return([])
+ @runner.should_receive(:fetch_plugins).exactly(3).times.with(["thing"]).and_return([@plugin1, @plugin2])
+ @runner.should_receive(:fetch_plugins).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.stub(:fetch_plugins).with([]).and_return([])
+ @runner.stub(:fetch_plugins).with(["thing"]).and_return([@plugin1, @plugin2])
@runner.run_plugin(@plugin3)
@plugins.each do |plugin|
plugin.has_run?.should be_true
@@ -203,20 +203,20 @@ describe Ohai::Runner, "run_plugin" do
it "should locate each provider" do
@ohai.attributes[:one] = Mash.new
- @ohai.attributes[:one][:_providers] = [@plugin1]
+ @ohai.attributes[:one][:_plugins] = [@plugin1]
@ohai.attributes[:two] = Mash.new
- @ohai.attributes[:two][:_providers] = [@plugin2]
+ @ohai.attributes[:two][:_plugins] = [@plugin2]
@ohai.attributes[:three] = Mash.new
- @ohai.attributes[:three][:_providers] = [@plugin3]
+ @ohai.attributes[:three][:_plugins] = [@plugin3]
- @runner.should_receive(:fetch_providers).twice.with([]).and_return([])
- @runner.should_receive(:fetch_providers).exactly(3).times.with(["one", "two"]).and_return([@plugin1, @plugin2])
+ @runner.should_receive(:fetch_plugins).twice.with([]).and_return([])
+ @runner.should_receive(:fetch_plugins).exactly(3).times.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.stub(:fetch_plugins).with([]).and_return([])
+ @runner.stub(:fetch_plugins).with(["one", "two"]).and_return([@plugin1, @plugin2])
@runner.run_plugin(@plugin3)
@plugins.each do |plugin|
plugin.has_run?.should be_true
@@ -252,8 +252,8 @@ describe Ohai::Runner, "run_plugin" do
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])
+ @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)
end
end
@@ -285,18 +285,18 @@ describe Ohai::Runner, "run_plugin" do
@pluginA, @pluginB, @pluginC = @plugins
@ohai.attributes[:A] = Mash.new
- @ohai.attributes[:A][:_providers] = [@pluginA]
+ @ohai.attributes[:A][:_plugins] = [@pluginA]
@ohai.attributes[:B] = Mash.new
- @ohai.attributes[:B][:_providers] = [@pluginB]
+ @ohai.attributes[:B][:_plugins] = [@pluginB]
@ohai.attributes[:C] = Mash.new
- @ohai.attributes[:C][:_providers] = [@pluginC]
+ @ohai.attributes[:C][:_plugins] = [@pluginC]
- @runner.stub(:fetch_providers).with(["C"]).and_return([@pluginC])
- @runner.stub(:fetch_providers).with([]).and_return([])
+ @runner.stub(:fetch_plugins).with(["C"]).and_return([@pluginC])
+ @runner.stub(:fetch_plugins).with([]).and_return([])
end
it "should not detect a cycle when B is the first provider returned" do
- @runner.stub(:fetch_providers).with(["B", "C"]).and_return([@pluginB, @pluginC])
+ @runner.stub(:fetch_plugins).with(["B", "C"]).and_return([@pluginB, @pluginC])
Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/)
@runner.run_plugin(@pluginA)
@@ -306,7 +306,7 @@ describe Ohai::Runner, "run_plugin" do
end
it "should not detect a cycle when C is the first provider returned" do
- @runner.stub(:fetch_providers).with(["B", "C"]).and_return([@pluginC, @pluginB])
+ @runner.stub(:fetch_plugins).with(["B", "C"]).and_return([@pluginC, @pluginB])
Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/)
@runner.run_plugin(@pluginA)
@@ -317,7 +317,7 @@ describe Ohai::Runner, "run_plugin" do
end
end
-describe Ohai::Runner, "fetch_providers" do
+describe Ohai::Runner, "fetch_plugins" do
before(:each) do
@ohai = Ohai::System.new
@runner = Ohai::Runner.new(@ohai, true)
@@ -328,9 +328,9 @@ describe Ohai::Runner, "fetch_providers" do
it "should return the provider" do
plugin = Ohai::DSL::Plugin.new(@ohai, "")
@ohai.attributes[:single] = Mash.new
- @ohai.attributes[:single][:_providers] = [plugin]
+ @ohai.attributes[:single][:_plugins] = [plugin]
- dependency_providers = @runner.fetch_providers(["single"])
+ dependency_providers = @runner.fetch_plugins(["single"])
dependency_providers.should eql([plugin])
end
end
@@ -340,9 +340,9 @@ describe Ohai::Runner, "fetch_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]
+ @ohai.attributes[:single][:_plugins] = [plugin1, plugin2]
- dependency_providers = @runner.fetch_providers(["single"])
+ dependency_providers = @runner.fetch_plugins(["single"])
dependency_providers.should eql([plugin1, plugin2])
end
end
@@ -354,11 +354,11 @@ describe Ohai::Runner, "fetch_providers" 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[:one][:_plugins] = [plugin1]
@ohai.attributes[:two] = Mash.new
- @ohai.attributes[:two][:_providers] = [plugin2]
+ @ohai.attributes[:two][:_plugins] = [plugin2]
- dependency_providers = @runner.fetch_providers(["one", "two"])
+ dependency_providers = @runner.fetch_plugins(["one", "two"])
dependency_providers.should eql([plugin1, plugin2])
end
end
@@ -367,11 +367,11 @@ describe Ohai::Runner, "fetch_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[:one][:_plugins] = [plugin]
@ohai.attributes[:two] = Mash.new
- @ohai.attributes[:two][:_providers] = [plugin]
+ @ohai.attributes[:two][:_plugins] = [plugin]
- dependency_providers = @runner.fetch_providers(["one", "two"])
+ dependency_providers = @runner.fetch_plugins(["one", "two"])
dependency_providers.should eql([plugin])
end
end
@@ -383,9 +383,9 @@ describe Ohai::Runner, "fetch_providers" do
@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]
+ @ohai.attributes[:top][:middle][:bottom][:_plugins] = [plugin]
- dependency_providers = @runner.fetch_providers(["top/middle/bottom"])
+ dependency_providers = @runner.fetch_plugins(["top/middle/bottom"])
dependency_providers.should eql([plugin])
end
end
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index b6607624..dd93a1e9 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -95,7 +95,7 @@ describe "Ohai::System" do
@plugins << p
end
- @ohai.stub(:collect_providers).and_return([])
+ @ohai.stub(:collect_plugins).and_return([])
end
after(:each) do
@@ -128,7 +128,7 @@ describe "Ohai::System" do
@ohai = Ohai::System.new
klass = Ohai.plugin(:Empty) { }
plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
- @ohai.stub(:collect_providers).and_return([plugin])
+ @ohai.stub(:collect_plugins).and_return([plugin])
end
describe "when a NoAttributeError is received" do
@@ -165,7 +165,7 @@ describe "Ohai::System" do
@plugins << klass.new(@ohai, "")
end
- @ohai.stub(:collect_providers).and_return(@plugins)
+ @ohai.stub(:collect_plugins).and_return(@plugins)
end
it "should run each plugin once from Ohai::System" do
@@ -241,7 +241,7 @@ EOF
end
end
- describe "#collect_providers" do
+ describe "#collect_plugins" do
before(:each) do
@ohai = Ohai::System.new
@@ -256,16 +256,16 @@ EOF
it "should find all the plugins providing attributes" do
a = @ohai.attributes
a[:zero] = Mash.new
- a[:zero][:_providers] = [@plugins[0]]
+ a[:zero][:_plugins] = [@plugins[0]]
a[:one] = Mash.new
- a[:one][:_providers] = [@plugins[1]]
+ a[:one][:_plugins] = [@plugins[1]]
a[:one][:two] = Mash.new
- a[:one][:two][:_providers] = [@plugins[2]]
+ a[:one][:two][:_plugins] = [@plugins[2]]
a[:stub] = Mash.new
a[:stub][:three] = Mash.new
- a[:stub][:three][:_providers] = [@plugins[3]]
+ a[:stub][:three][:_plugins] = [@plugins[3]]
- providers = @ohai.collect_providers(@ohai.attributes)
+ providers = @ohai.collect_plugins(@ohai.attributes)
providers.size.should eql(@plugins.size)
@plugins.each do |plugin|
providers.include?(plugin).should be_true
@@ -355,7 +355,7 @@ EOF
@ohai.v6_dependency_solver['v6plugin'] = @v6plugin
@ohai.v6_dependency_solver['v7plugin'] = @v7plugin
@ohai.attributes[:message] = Mash.new
- @ohai.attributes[:message][:_providers] = [@v7plugin]
+ @ohai.attributes[:message][:_plugins] = [@v7plugin]
end
it "should run the plugin it requires" do
@@ -402,9 +402,9 @@ EOF
a = @ohai.attributes
a[:message] = Mash.new
- a[:message][:_providers] = [@v7plugin]
+ a[:message][:_plugins] = [@v7plugin]
a[:other] = Mash.new
- a[:other][:_providers] = [@other]
+ a[:other][:_plugins] = [@other]
end
it "should resolve the v7 plugin dependencies" do