summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Kantrowitz <noah@coderanger.net>2016-09-02 18:14:40 -0700
committerNoah Kantrowitz <noah@coderanger.net>2016-09-02 18:38:59 -0700
commit1c990a11ebe360f5e85ac13626ce1e09e295f919 (patch)
treeba08b180ba24e1154e21eb3c4e0a12df642b4f46
parentcc96f2fc1e907c95778bb91e9ff2168a05e43656 (diff)
downloadchef-1c990a11ebe360f5e85ac13626ce1e09e295f919.tar.gz
Hook up the recipe_file_loaded event which was defined but not actually called.
This also extends two of the recipe events to get the recipe name in addition to the path because that's usually useful for display. These arguments are both added at the end so the auto-magic argument trimmer will keep backwards compat. Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r--lib/chef/event_dispatch/base.rb4
-rw-r--r--lib/chef/formatters/base.rb4
-rw-r--r--lib/chef/run_context/cookbook_compiler.rb5
-rw-r--r--spec/unit/run_context/cookbook_compiler_spec.rb42
4 files changed, 48 insertions, 7 deletions
diff --git a/lib/chef/event_dispatch/base.rb b/lib/chef/event_dispatch/base.rb
index 6c6b2fa3bb..04c960c7af 100644
--- a/lib/chef/event_dispatch/base.rb
+++ b/lib/chef/event_dispatch/base.rb
@@ -232,11 +232,11 @@ class Chef
end
# Called after the recipe has been loaded
- def recipe_file_loaded(path)
+ def recipe_file_loaded(path, recipe)
end
# Called after a recipe file fails to load
- def recipe_file_load_failed(path, exception)
+ def recipe_file_load_failed(path, exception, recipe)
end
# Called when a recipe cannot be resolved
diff --git a/lib/chef/formatters/base.rb b/lib/chef/formatters/base.rb
index 3641e619e9..536bf72e02 100644
--- a/lib/chef/formatters/base.rb
+++ b/lib/chef/formatters/base.rb
@@ -203,12 +203,12 @@ class Chef
end
# Delegates to #file_loaded
- def recipe_file_loaded(path)
+ def recipe_file_loaded(path, recipe)
file_loaded(path)
end
# Delegates to #file_load_failed
- def recipe_file_load_failed(path, exception)
+ def recipe_file_load_failed(path, exception, recipe)
file_load_failed(path, exception)
end
diff --git a/lib/chef/run_context/cookbook_compiler.rb b/lib/chef/run_context/cookbook_compiler.rb
index bdf3a1251c..b2a8d236a3 100644
--- a/lib/chef/run_context/cookbook_compiler.rb
+++ b/lib/chef/run_context/cookbook_compiler.rb
@@ -137,13 +137,14 @@ class Chef
@events.recipe_load_start(run_list_expansion.recipes.size)
run_list_expansion.recipes.each do |recipe|
begin
+ path = resolve_recipe(recipe)
@run_context.load_recipe(recipe)
+ @events.recipe_file_loaded(path, recipe)
rescue Chef::Exceptions::RecipeNotFound => e
@events.recipe_not_found(e)
raise
rescue Exception => e
- path = resolve_recipe(recipe)
- @events.recipe_file_load_failed(path, e)
+ @events.recipe_file_load_failed(path, e, recipe)
raise
end
end
diff --git a/spec/unit/run_context/cookbook_compiler_spec.rb b/spec/unit/run_context/cookbook_compiler_spec.rb
index 868bed4bfd..e93088cd5f 100644
--- a/spec/unit/run_context/cookbook_compiler_spec.rb
+++ b/spec/unit/run_context/cookbook_compiler_spec.rb
@@ -158,7 +158,47 @@ describe Chef::RunContext::CookbookCompiler do
end
describe "loading recipes" do
- # Tests for this behavior are in RunContext's tests
+ # Additional tests for this behavior are in RunContext's tests
+
+ describe "event dispatch" do
+ let(:recipe) { "dependency1::default" }
+ let(:recipe_path) do
+ File.expand_path("../../../data/run_context/cookbooks/dependency1/recipes/default.rb", __FILE__)
+ end
+ before do
+ node.run_list(recipe)
+ end
+ subject { compiler.compile_recipes }
+
+ it "dispatches normally" do
+ allow(run_context).to receive(:load_recipe)
+ expect(events).to receive(:recipe_load_start).with(1)
+ expect(events).to receive(:recipe_file_loaded).with(recipe_path, "dependency1::default")
+ expect(events).to receive(:recipe_load_complete).with(no_args)
+ subject
+ end
+
+ it "dispatches when a recipe is not found" do
+ exc = Chef::Exceptions::RecipeNotFound.new
+ allow(run_context).to receive(:load_recipe).and_raise(exc)
+ expect(events).to receive(:recipe_load_start).with(1)
+ expect(events).to_not receive(:recipe_file_loaded)
+ expect(events).to receive(:recipe_not_found).with(exc)
+ expect(events).to_not receive(:recipe_load_complete)
+ expect { subject }.to raise_error(exc)
+ end
+
+ it "dispatches when a recipe has an error" do
+ exc = ArgumentError.new
+ allow(run_context).to receive(:load_recipe).and_raise(exc)
+ expect(events).to receive(:recipe_load_start).with(1)
+ expect(events).to_not receive(:recipe_file_loaded)
+ expect(events).to receive(:recipe_file_load_failed).with(recipe_path, exc, "dependency1::default")
+ expect(events).to_not receive(:recipe_load_complete)
+ expect { subject }.to raise_error(exc)
+ end
+ end
+
end
describe "listing cookbook order" do