diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-05-18 15:38:33 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-05-18 15:38:33 -0700 |
commit | 2882f53e6c3d7ffb1ac0c22a6200b2157cc79a15 (patch) | |
tree | 694162a154e73b8360745d67776115907968086b | |
parent | 05b07f669b9a06799e7e20608d1df3e5bbc2853a (diff) | |
parent | 75b52c7321d5461301bb942cc19162f52e7f7a58 (diff) | |
download | chef-2882f53e6c3d7ffb1ac0c22a6200b2157cc79a15.tar.gz |
Merge pull request #2312 from chef/lcg/node-attrs
fix node recipes, add run_list_expansion and cookbooks
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/chef/node.rb | 13 | ||||
-rw-r--r-- | lib/chef/run_context.rb | 1 | ||||
-rw-r--r-- | lib/chef/run_list/versioned_recipe_list.rb | 18 | ||||
-rw-r--r-- | spec/unit/client_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/policy_builder/policyfile_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/run_context_spec.rb | 31 |
7 files changed, 70 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d4254b6486..216dfe6476 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ * [pr#3344](https://github.com/chef/chef/pull/3344): Rewrite Windows user resouce code to use ffi instead of win32-api * [pr#3318](https://github.com/chef/chef/pull/3318) Modify Windows package provider to allow for url source * [pr#3381](https://github.com/chef/chef/pull/3381) warn on cookbook self-deps +* [pr#2312](https://github.com/chef/chef/pull/2312): fix `node[:recipes]` duplication, add `node[:cookbooks]` and `node[:expanded_run_list]` ## 12.3.0 diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 8c41d7e10e..d5078371c5 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -77,6 +77,15 @@ class Chef @run_state = {} end + # after the run_context has been set on the node, go through the cookbook_collection + # and setup the node[:cookbooks] attribute so that it is published in the node object + def set_cookbook_attribute + return unless run_context.cookbook_collection + run_context.cookbook_collection.each do |cookbook_name, cookbook| + automatic_attrs[:cookbooks][cookbook_name][:version] = cookbook.version + end + end + # Used by DSL def node self @@ -252,6 +261,7 @@ class Chef # saved back to the node and be searchable def loaded_recipe(cookbook, recipe) fully_qualified_recipe = "#{cookbook}::#{recipe}" + automatic_attrs[:recipes] << fully_qualified_recipe unless Array(self[:recipes]).include?(fully_qualified_recipe) end @@ -362,7 +372,8 @@ class Chef self.tags # make sure they're defined - automatic_attrs[:recipes] = expansion.recipes + automatic_attrs[:recipes] = expansion.recipes.with_fully_qualified_names_and_version_constraints + automatic_attrs[:expanded_run_list] = expansion.recipes.with_fully_qualified_names_and_version_constraints automatic_attrs[:roles] = expansion.roles apply_expansion_attributes(expansion) diff --git a/lib/chef/run_context.rb b/lib/chef/run_context.rb index 4f0215bfd4..44b05f0cc0 100644 --- a/lib/chef/run_context.rb +++ b/lib/chef/run_context.rb @@ -86,6 +86,7 @@ class Chef @reboot_info = {} @node.run_context = self + @node.set_cookbook_attribute @cookbook_compiler = nil end diff --git a/lib/chef/run_list/versioned_recipe_list.rb b/lib/chef/run_list/versioned_recipe_list.rb index 0eefded964..7cce6fa48c 100644 --- a/lib/chef/run_list/versioned_recipe_list.rb +++ b/lib/chef/run_list/versioned_recipe_list.rb @@ -63,6 +63,24 @@ class Chef end end end + + # Get an array of strings of the fully-qualified recipe names (with ::default appended) and + # with the versions in "NAME@VERSION" format. + # + # @return [Array] Array of strings with fully-qualified recipe names + def with_fully_qualified_names_and_version_constraints + self.map do |recipe_name| + ret = if recipe_name.include?('::') + recipe_name + else + "#{recipe_name}::default" + end + if @versions[recipe_name] + ret << "@#{@versions[recipe_name]}" + end + ret + end + end end end end diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index 4cfac35cb4..c1bde072f6 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -680,6 +680,7 @@ describe Chef::Client do # check pre-conditions. expect(node[:roles]).to be_nil expect(node[:recipes]).to be_nil + expect(node[:expanded_run_list]).to be_nil allow(client.policy_builder).to receive(:node).and_return(node) @@ -692,7 +693,10 @@ describe Chef::Client do expect(node[:roles]).to include("role_containing_cookbook1") expect(node[:recipes]).not_to be_nil expect(node[:recipes].length).to eq(1) - expect(node[:recipes]).to include("cookbook1") + expect(node[:recipes]).to include("cookbook1::default") + expect(node[:expanded_run_list]).not_to be_nil + expect(node[:expanded_run_list].length).to eq(1) + expect(node[:expanded_run_list]).to include("cookbook1::default") end it "should set the environment from the specified configuration value" do diff --git a/spec/unit/policy_builder/policyfile_spec.rb b/spec/unit/policy_builder/policyfile_spec.rb index e4f7388a1c..ad7c83b03b 100644 --- a/spec/unit/policy_builder/policyfile_spec.rb +++ b/spec/unit/policy_builder/policyfile_spec.rb @@ -389,8 +389,8 @@ describe Chef::PolicyBuilder::Policyfile do let(:example1_cookbook_data) { double("CookbookVersion Hash for example1 cookbook") } let(:example2_cookbook_data) { double("CookbookVersion Hash for example2 cookbook") } - let(:example1_cookbook_object) { double("Chef::CookbookVersion for example1 cookbook") } - let(:example2_cookbook_object) { double("Chef::CookbookVersion for example2 cookbook") } + let(:example1_cookbook_object) { double("Chef::CookbookVersion for example1 cookbook", version: "0.1.2") } + let(:example2_cookbook_object) { double("Chef::CookbookVersion for example2 cookbook", version: "1.2.3") } let(:expected_cookbook_hash) do { "example1" => example1_cookbook_object, "example2" => example2_cookbook_object } diff --git a/spec/unit/run_context_spec.rb b/spec/unit/run_context_spec.rb index d656111a7d..eac5028bc1 100644 --- a/spec/unit/run_context_spec.rb +++ b/spec/unit/run_context_spec.rb @@ -53,6 +53,37 @@ describe Chef::RunContext do expect(run_context.node).to eq(node) end + it "loads up node[:cookbooks]" do + expect(run_context.node[:cookbooks]).to eql( + { + "circular-dep1" => { + "version" => "0.0.0", + }, + "circular-dep2" => { + "version" => "0.0.0", + }, + "dependency1" => { + "version" => "0.0.0", + }, + "dependency2" => { + "version" => "0.0.0", + }, + "no-default-attr" => { + "version" => "0.0.0", + }, + "test" => { + "version" => "0.0.0", + }, + "test-with-circular-deps" => { + "version" => "0.0.0", + }, + "test-with-deps" => { + "version" => "0.0.0", + }, + } + ) + end + describe "loading cookbooks for a run list" do before do |