summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@opscode.com>2013-09-11 14:19:52 -0700
committerClaire McQuin <claire@opscode.com>2013-09-11 16:36:46 -0700
commit15db840588d341812f6a97d3c12a02579fe79e9a (patch)
tree99e01230bd08c71e1c6ebc0a648a4047f12e6568
parent1864fe0dd017a2a590ae1678b7b48adafbce780d (diff)
downloadohai-15db840588d341812f6a97d3c12a02579fe79e9a.tar.gz
fix cycle detection
-rw-r--r--lib/ohai/runner.rb10
-rw-r--r--spec/unit/runner_spec.rb51
2 files changed, 53 insertions, 8 deletions
diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb
index 287cb0da..73e0ddce 100644
--- a/lib/ohai/runner.rb
+++ b/lib/ohai/runner.rb
@@ -41,12 +41,11 @@ module Ohai
visited = [plugin]
while !visited.empty?
p = visited.pop
- unless force
- next if p.has_run?
- end
+
+ next if p.has_run? unless force
if visited.include?(p)
- raise DependencyCycleError, "Dependency cycle detected. Please examine the following plugin files: #{cycle_sources(visited, p).join(", ")}"
+ raise DependencyCycleError, "Dependency cycle detected. Please refer to the following plugin files: #{cycle_sources(visited, p).join(", ") }"
end
dependency_providers = fetch_providers(p.dependencies)
@@ -55,8 +54,7 @@ module Ohai
if dependency_providers.empty?
@safe_run ? p.safe_run : p.run
else
- visited << p << dependency_providers
- visited.flatten!
+ visited << p << dependency_providers.first
end
end
end
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb
index c9dfe805..4cb73e62 100644
--- a/spec/unit/runner_spec.rb
+++ b/spec/unit/runner_spec.rb
@@ -120,7 +120,7 @@ describe Ohai::Runner, "run_plugin" do
@ohai.attributes[:other] = Mash.new
@ohai.attributes[:other][:providers] = [@plugin3]
- @runner.should_receive(:fetch_providers).twice.with(["thing"]).and_return([@plugin1, @plugin2])
+ @runner.should_receive(:fetch_providers).exactly(3).times.with(["thing"]).and_return([@plugin1, @plugin2])
@runner.should_receive(:fetch_providers).twice.with([]).and_return([])
@runner.run_plugin(@plugin3)
end
@@ -161,7 +161,7 @@ describe Ohai::Runner, "run_plugin" do
@ohai.attributes[:three][:providers] = [@plugin3]
@runner.should_receive(:fetch_providers).twice.with([]).and_return([])
- @runner.should_receive(:fetch_providers).twice.with(["one", "two"]).and_return([@plugin1, @plugin2])
+ @runner.should_receive(:fetch_providers).exactly(3).times.with(["one", "two"]).and_return([@plugin1, @plugin2])
@runner.run_plugin(@plugin3)
end
@@ -196,6 +196,53 @@ describe Ohai::Runner, "run_plugin" do
expect { @runner.run_plugin(@plugin1) }.to raise_error(Ohai::DependencyCycleError)
end
end
+
+ describe "when A depends on B and C, and B depends on C" do
+ before(:each) do
+ @ohai = Ohai::System.new
+ @runner = Ohai::Runner.new(@ohai, true)
+
+ klassA = Ohai.plugin { provides("A"); depends("B", "C"); collect_data { } }
+ klassB = Ohai.plugin { provides("B"); depends("C"); collect_data { } }
+ klassC = Ohai.plugin { provides("C"); collect_data { } }
+
+ @plugins = []
+ [klassA, klassB, klassC].each do |klass|
+ @plugins << klass.new(@ohai, "")
+ end
+ @pluginA, @pluginB, @pluginC = @plugins
+
+ @ohai.attributes[:A] = Mash.new
+ @ohai.attributes[:A][:providers] = [@pluginA]
+ @ohai.attributes[:B] = Mash.new
+ @ohai.attributes[:B][:providers] = [@pluginB]
+ @ohai.attributes[:C] = Mash.new
+ @ohai.attributes[:C][:providers] = [@pluginC]
+
+ @runner.stub(:fetch_providers).with(["C"]).and_return([@pluginC])
+ @runner.stub(:fetch_providers).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])
+ Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/)
+ @runner.run_plugin(@pluginA)
+
+ @plugins.each do |plugin|
+ plugin.has_run?.should be_true
+ end
+ 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])
+ Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/)
+ @runner.run_plugin(@pluginA)
+
+ @plugins.each do |plugin|
+ plugin.has_run?.should be_true
+ end
+ end
+ end
end
describe Ohai::Runner, "fetch_providers" do