summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Kantrowitz <noah@coderanger.net>2017-04-04 22:24:29 -0700
committerNoah Kantrowitz <noah@coderanger.net>2017-04-04 22:24:29 -0700
commit2fa4ec2392ba002c6881a2283ca65320471e4c7b (patch)
treeec78d5ee53ecda07d8732e26704fa5b3da30e177
parentf51d16101dd244046bece8721861c1aad332bca0 (diff)
downloadchef-2fa4ec2392ba002c6881a2283ca65320471e4c7b.tar.gz
Make root alias attributes work with the normal CookbookCompiler load behavior.
Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r--lib/chef/cookbook_version.rb5
-rw-r--r--lib/chef/run_context/cookbook_compiler.rb17
-rw-r--r--spec/functional/root_alias_spec.rb7
3 files changed, 23 insertions, 6 deletions
diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb
index a3c38e4e26..602388d0b7 100644
--- a/lib/chef/cookbook_version.rb
+++ b/lib/chef/cookbook_version.rb
@@ -146,7 +146,10 @@ class Chef
@recipe_filenames_by_name ||= begin
name_map = filenames_by_name(files_for("recipes"))
root_alias = cookbook_manifest.root_files.find {|record| record[:name] == "recipe.rb" }
- name_map["default"] = root_alias[:full_path] if root_alias
+ if root_alias
+ Chef::Log.error("Cookbook #{name} contains both recipe.rb and and recipes/default.rb, ignoring recipes/default.rb") if name_map["default"]
+ name_map["default"] = root_alias[:full_path]
+ end
name_map
end
end
diff --git a/lib/chef/run_context/cookbook_compiler.rb b/lib/chef/run_context/cookbook_compiler.rb
index 94635be03d..ed0a1ad9fe 100644
--- a/lib/chef/run_context/cookbook_compiler.rb
+++ b/lib/chef/run_context/cookbook_compiler.rb
@@ -105,7 +105,7 @@ class Chef
# according to #cookbook_order; within a cookbook, +default.rb+ is loaded
# first, then the remaining attributes files in lexical sort order.
def compile_attributes
- @events.attribute_load_start(count_files_by_segment(:attributes))
+ @events.attribute_load_start(count_files_by_segment(:attributes, "attributes.rb"))
cookbook_order.each do |cookbook|
load_attributes_from_cookbook(cookbook)
end
@@ -166,7 +166,16 @@ class Chef
def load_attributes_from_cookbook(cookbook_name)
list_of_attr_files = files_in_cookbook_by_segment(cookbook_name, :attributes).dup
- if default_file = list_of_attr_files.find { |path| File.basename(path) == "default.rb" }
+ root_alias = cookbook_collection[cookbook_name].files_for(:root_files).find {|record| record[:name] == "attributes.rb" }
+ default_file = list_of_attr_files.find { |path| File.basename(path) == "default.rb" }
+ if root_alias
+ if default_file
+ Chef::Log.error("Cookbook #{cookbook_name} contains both attributes.rb and and attributes/default.rb, ignoring attributes/default.rb")
+ list_of_attr_files.delete(default_file)
+ end
+ # The actual root_alias path decoding is handled in CookbookVersion#attribute_filenames_by_short_filename
+ load_attribute_file(cookbook_name.to_s, "default")
+ elsif default_file
list_of_attr_files.delete(default_file)
load_attribute_file(cookbook_name.to_s, default_file)
end
@@ -259,9 +268,9 @@ class Chef
ordered_cookbooks << cookbook
end
- def count_files_by_segment(segment)
+ def count_files_by_segment(segment, root_alias=nil)
cookbook_collection.inject(0) do |count, cookbook_by_name|
- count + cookbook_by_name[1].segment_filenames(segment).size
+ count + cookbook_by_name[1].segment_filenames(segment).size + (root_alias ? cookbook_by_name[1].files_for(:root_files).select {|record| record[:name] == root_alias }.size : 0)
end
end
diff --git a/spec/functional/root_alias_spec.rb b/spec/functional/root_alias_spec.rb
index 6c08877be4..e26f41ff67 100644
--- a/spec/functional/root_alias_spec.rb
+++ b/spec/functional/root_alias_spec.rb
@@ -37,10 +37,15 @@ describe "root aliases" do
end
describe "attributes root aliases" do
- it "should load attributes.rb" do
+ it "should load attributes.rb when included directly" do
node.include_attribute("aliased")
expect(node["aliased"]["attr"]).to eq "value"
end
+
+ it "should load attributes.rb when loading a cookbook" do
+ run_context.load(node.run_list.expand("_default"))
+ expect(node["aliased"]["attr"]).to eq "value"
+ end
end
describe "recipe root aliased" do