summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@opscode.com>2013-09-27 10:04:12 -0700
committerClaire McQuin <claire@opscode.com>2013-10-17 12:07:30 -0700
commit1f285b875fc9f6d8a8afd5873a2c7230743425d0 (patch)
tree327989a4f3af7a4d4dccc9c4f36b1fdab383200f
parente4499f2e85936877f1fc418941cfe28ac806c710 (diff)
downloadohai-1f285b875fc9f6d8a8afd5873a2c7230743425d0.tar.gz
update system spec for plugin namespacing
-rw-r--r--spec/unit/system_spec.rb178
1 files changed, 147 insertions, 31 deletions
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index c29ca2b2..48669b6f 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -46,9 +46,13 @@ describe "Ohai::System" do
Ohai::Loader.stub(:new) { loader }
@ohai = Ohai::System.new
- klass = Ohai.plugin { }
+ klass = Ohai.plugin(:Test) { }
plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
- loader.stub(:load_plugin).with("/tmp/plugins/empty.rb").and_return(plugin)
+ loader.stub(:load_plugin).with("/tmp/plugins/empty.rb", anything()).and_return(plugin)
+ end
+
+ after(:each) do
+ Ohai::NamedPlugin.send(:remove_const, :Test)
end
it "should load plugins when plugin_path has a trailing slash" do
@@ -83,21 +87,28 @@ describe "Ohai::System" do
describe "with v6 plugins only" do
before(:each) do
@ohai = Ohai::System.new
- @klass = Ohai.v6plugin { collect_contents("") }
@plugins = []
- 5.times do |x|
- @plugins << @klass.new(@ohai, "/tmp/plugins/plugin#{x}.rb")
- end
-
- ['one', 'two', 'three', 'four', 'five'].each_with_index do |plugin_name, idx|
- @ohai.v6_dependency_solver[plugin_name] = @plugins[idx]
+ @names = ['one', 'two', 'three', 'four', 'five']
+ @names.each do |name|
+ k = Ohai.v6plugin(name) {
+ collect_contents("")
+ }
+ p = k.new(@ohai, "/tmp/plugins/#{name}.rb")
+ @ohai.v6_dependency_solver[name] = p
+ @plugins << p
end
@ohai.stub(:collect_providers).and_return([])
end
after(:each) do
+ @names.each do |name|
+ name_sym = name.capitalize.to_sym
+ if Ohai::NamedPlugin.send(:const_defined?, name_sym)
+ Ohai::NamedPlugin.send(:remove_const, name_sym)
+ end
+ end
@ohai.v6_dependency_solver.clear
end
@@ -125,11 +136,17 @@ describe "Ohai::System" do
Ohai::Runner.stub(:new) { @runner }
@ohai = Ohai::System.new
- klass = Ohai.plugin { }
+ klass = Ohai.plugin(:Empty) { }
plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
@ohai.stub(:collect_providers).and_return([plugin])
end
+ after(:each) do
+ if Ohai::NamedPlugin.send(:const_defined?, :Empty)
+ Ohai::NamedPlugin.send(:remove_const, :Empty)
+ end
+ end
+
describe "when a NoAttributeError is received" do
it "should write an error to Ohai::Log" do
@runner.stub(:run_plugin).and_raise(Ohai::NoAttributeError)
@@ -147,15 +164,34 @@ describe "Ohai::System" do
@ohai = Ohai::System.new
- klass = Ohai.plugin { provides("itself"); collect_data { itself("me") } }
+ @names = [:One, :Two, :Three, :Four, :Five]
+
+ klasses = []
+ @names.each do |name|
+ klasses << Ohai.plugin(name) {
+ provides("itself")
+ collect_data {
+ itself("me")
+ }
+ }
+ end
+
@plugins = []
- 5.times do |x|
- @plugins << klass.new(@ohai, "/tmp/plugins/plugin#{x}.rb")
+ klasses.each do |klass|
+ @plugins << klass.new(@ohai, "")
end
@ohai.stub(:collect_providers).and_return(@plugins)
end
+ after(:each) do
+ @names.each do |name|
+ if Ohai::NamedPlugin.send(:const_defined?, name)
+ Ohai::NamedPlugin.send(:remove_const, name)
+ end
+ end
+ end
+
it "should run each plugin once from Ohai::System" do
@plugins.each do |plugin|
@runner.should_receive(:run_plugin).with(plugin, false)
@@ -167,17 +203,53 @@ describe "Ohai::System" do
describe "with v6 plugins that depend on v7 plugins" do
before(:each) do
@ohai = Ohai::System.new
+ loader = Ohai::Loader.new(@ohai)
+
+ messages = <<EOF
+require_plugin 'v6message'
+require_plugin 'v7message'
- @plugin_path = Ohai::Config[:plugin_path]
- Ohai::Config[:plugin_path] = [File.expand_path(File.dirname(__FILE__) + '/../data/plugins')]
- @ohai.load_plugins
+provides 'messages'
+messages Mash.new
+messages[:v6message] = v6message
+messages[:v7message] = v7message
+EOF
+ v6message = <<EOF
+provides 'v6message'
+v6message "update me!"
+EOF
+ v7message = <<EOF
+Ohai.plugin(:V7message) do
+ provides 'v7message'
+
+ collect_data(:default) do
+ v7message "v7 plugins are awesome!"
+ end
+end
+EOF
+ @names = [:Messages, :V6message, :V7message]
@plugins = []
- @ohai.v6_dependency_solver.each { |plugin_name, plugin| @plugins << plugin }
+ [
+ [messages, :Messages],
+ [v6message, :V6message],
+ [v7message, :V7message]
+ ].each do |contents, name|
+ IO.stub(:read).with("tmp/#{name.to_s.downcase}.rb").and_return(contents)
+ @plugins << loader.load_plugin("tmp/#{name.to_s.downcase}.rb", name)
+ end
+
+ @plugins.each do |plugin|
+ @ohai.v6_dependency_solver[File.basename(plugin.source, '.rb')] = plugin
+ end
end
after(:each) do
- Ohai::Config[:plugin_path] = @plugin_path
+ @names.each do |name|
+ if Ohai::NamedPlugin.send(:const_defined?, name)
+ Ohai::NamedPlugin.send(:remove_const, name)
+ end
+ end
end
it "should run each plugin" do
@@ -205,10 +277,19 @@ describe "Ohai::System" do
before(:each) do
@ohai = Ohai::System.new
- klass = Ohai.plugin { }
+ @names = [:Zero, :One, :Two, :Three]
@plugins = []
- 4.times do
- @plugins << klass.new(@ohai, "")
+ @names.each do |name|
+ k = Ohai.plugin(name) { }
+ @plugins << k.new(@ohai, "")
+ end
+ end
+
+ after(:each) do
+ @names.each do |name|
+ if Ohai::NamedPlugin.send(:const_defined?, name)
+ Ohai::NamedPlugin.send(:remove_const, name)
+ end
end
end
@@ -238,13 +319,16 @@ describe "Ohai::System" do
Ohai::Config[:plugin_path] = ["/tmp/plugins"]
@ohai = Ohai::System.new
- klass = Ohai.v6plugin { }
+ klass = Ohai.v6plugin("empty") { }
@plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
@ohai.stub(:plugin_for).with("empty").and_return(@plugin)
end
after(:each) do
+ if Ohai::NamedPlugin.send(:const_defined?, :Empty)
+ Ohai::NamedPlugin.send(:remove_const, :Empty)
+ end
Ohai::Config[:plugin_path] = @plugin_path
end
@@ -307,8 +391,11 @@ provides 'v6attr'
require_plugin 'v7plugin'
v6attr message
EOF
- v6klass = Ohai.v6plugin { collect_contents(v6string) }
- v7klass = Ohai.plugin { provides("message"); collect_data { message("hey.") } }
+ v6klass = Ohai.v6plugin('v6plugin') { collect_contents(v6string) }
+ v7klass = Ohai.plugin(:V7plugin) {
+ provides("message")
+ collect_data { message("hey.") }
+ }
@v6plugin = v6klass.new(@ohai, "/tmp/plugins/v6plugin.rb")
@v7plugin = v7klass.new(@ohai, "/tmp/plugins/v7plugin.rb")
@@ -318,6 +405,14 @@ EOF
@ohai.attributes[:message][:providers] = [@v7plugin]
end
+ after(:each) do
+ [:V6plugin, :V7plugin].each do |name|
+ if Ohai::NamedPlugin.send(:const_defined?, name)
+ Ohai::NamedPlugin.send(:remove_const, name)
+ end
+ end
+ end
+
it "should run the plugin it requires" do
@ohai.require_plugin('v6plugin')
@v7plugin.has_run?.should be_true
@@ -340,9 +435,16 @@ provides 'v6attr'
require_plugin 'v7plugin'
v6attr message
EOF
- v6klass = Ohai.v6plugin { collect_contents(v6string) }
- v7klass = Ohai.plugin { provides("message"); depends("other"); collect_data{ message(other) } }
- otherklass = Ohai.plugin { provides("other"); collect_data{ other("o hai") } }
+ v6klass = Ohai.v6plugin('v6plugin') { 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, "/tmp/plugin/v6plugin.rb")
@v7plugin = v7klass.new(@ohai, "/tmp/plugins/v7plugin.rb")
@@ -360,6 +462,14 @@ EOF
a[:other][:providers] = [@other]
end
+ after(:each) do
+ [:V6plugin, :V7plugin, :Other].each do |name|
+ if Ohai::NamedPlugin.send(:const_defined?, name)
+ Ohai::NamedPlugin.send(:remove_const, name)
+ end
+ end
+ end
+
it "should resolve the v7 plugin dependencies" do
@ohai.require_plugin('v6plugin')
[@v6plugin, @v7plugin, @other].each do |plugin|
@@ -388,7 +498,13 @@ EOF
Ohai::Loader.stub(:new) { @loader }
@ohai = Ohai::System.new
- @klass = Ohai.v6plugin { }
+ @klass = Ohai.v6plugin('empty') { }
+ end
+
+ after(:each) do
+ if Ohai::NamedPlugin.send(:const_defined?, :Empty)
+ Ohai::NamedPlugin.send(:remove_const, :Empty)
+ end
end
it "should find a plugin with a simple name" do
@@ -396,7 +512,7 @@ EOF
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)
+ @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb", "empty").and_return(plugin)
found_plugin = @ohai.plugin_for("empty")
found_plugin.should eql(plugin)
@@ -407,7 +523,7 @@ EOF
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)
+ @loader.stub(:load_plugin).with("/tmp/plugins/ubuntu/empty.rb", "ubuntu::empty").and_return(plugin)
found_plugin = @ohai.plugin_for("ubuntu::empty")
found_plugin.should eql(plugin)
@@ -426,7 +542,7 @@ EOF
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)
+ @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb", "empty").and_return(plugin)
@ohai.plugin_for("empty")
@ohai.v6_dependency_solver.should have_key('empty')