From 1f7f5577cc5cc89d210d6b3578132e2c391d1aca Mon Sep 17 00:00:00 2001 From: John Keiser Date: Wed, 10 Jun 2015 12:25:25 -0700 Subject: Make load/include recipe run in child context --- lib/chef/run_context.rb | 6 ++--- .../cookbooks/include/recipes/default.rb | 24 +++++++++++++++++++ .../cookbooks/include/recipes/includee.rb | 3 +++ spec/unit/run_context/child_run_context_spec.rb | 27 ++++++++++++++++++++++ spec/unit/run_context_spec.rb | 3 +++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 spec/data/run_context/cookbooks/include/recipes/default.rb create mode 100644 spec/data/run_context/cookbooks/include/recipes/includee.rb diff --git a/lib/chef/run_context.rb b/lib/chef/run_context.rb index 6aa511f30f..f55d1740b1 100644 --- a/lib/chef/run_context.rb +++ b/lib/chef/run_context.rb @@ -564,10 +564,7 @@ ERROR_MESSAGE events has_cookbook_file_in_cookbook? has_template_in_cookbook? - include_recipe load - load_recipe - load_recipe_file loaded_attribute loaded_attributes loaded_attributes_hash @@ -606,7 +603,10 @@ ERROR_MESSAGE immediate_notification_collection immediate_notification_collection= immediate_notifications + include_recipe initialize_child_state + load_recipe + load_recipe_file notifies_immediately notifies_delayed parent_run_context diff --git a/spec/data/run_context/cookbooks/include/recipes/default.rb b/spec/data/run_context/cookbooks/include/recipes/default.rb new file mode 100644 index 0000000000..8d22994252 --- /dev/null +++ b/spec/data/run_context/cookbooks/include/recipes/default.rb @@ -0,0 +1,24 @@ +module ::RanResources + def self.resources + @resources ||= [] + end +end +class RunContextCustomResource < Chef::Resource + action :create do + ruby_block '4' do + block { RanResources.resources << 4 } + end + recipe_eval do + ruby_block '1' do + block { RanResources.resources << 1 } + end + include_recipe 'include::includee' + ruby_block '3' do + block { RanResources.resources << 3 } + end + end + ruby_block '5' do + block { RanResources.resources << 5 } + end + end +end diff --git a/spec/data/run_context/cookbooks/include/recipes/includee.rb b/spec/data/run_context/cookbooks/include/recipes/includee.rb new file mode 100644 index 0000000000..87bb7f114e --- /dev/null +++ b/spec/data/run_context/cookbooks/include/recipes/includee.rb @@ -0,0 +1,3 @@ +ruby_block '2' do + block { RanResources.resources << 2 } +end diff --git a/spec/unit/run_context/child_run_context_spec.rb b/spec/unit/run_context/child_run_context_spec.rb index 4651bd2737..63586e459c 100644 --- a/spec/unit/run_context/child_run_context_spec.rb +++ b/spec/unit/run_context/child_run_context_spec.rb @@ -101,6 +101,33 @@ describe Chef::RunContext::ChildRunContext do c = child.create_child expect(c.parent_run_context).to eq child end + + context "after load('include::default')" do + before do + run_list = Chef::RunList.new('include::default').expand('_default') + # TODO not sure why we had to do this to get everything to work ... + node.automatic_attrs[:recipes] = [] + child.load(run_list) + end + + it "load_recipe loads into the child" do + expect(child.resource_collection).to be_empty + child.load_recipe("include::includee") + expect(child.resource_collection).not_to be_empty + end + + it "include_recipe loads into the child" do + expect(child.resource_collection).to be_empty + child.include_recipe("include::includee") + expect(child.resource_collection).not_to be_empty + end + + it "load_recipe_file loads into the child" do + expect(child.resource_collection).to be_empty + child.load_recipe_file(File.expand_path("include/recipes/includee.rb", chef_repo_path)) + expect(child.resource_collection).not_to be_empty + end + end end end end diff --git a/spec/unit/run_context_spec.rb b/spec/unit/run_context_spec.rb index 857bc6b90e..99801575ef 100644 --- a/spec/unit/run_context_spec.rb +++ b/spec/unit/run_context_spec.rb @@ -68,6 +68,9 @@ describe Chef::RunContext do "dependency2" => { "version" => "0.0.0", }, + "include" => { + "version" => "0.0.0", + }, "no-default-attr" => { "version" => "0.0.0", }, -- cgit v1.2.1