diff options
author | danielsdeleo <dan@chef.io> | 2015-09-16 16:17:00 -0700 |
---|---|---|
committer | danielsdeleo <dan@chef.io> | 2015-09-21 09:07:28 -0700 |
commit | 3bffbc968d4dc76cd6992c603f849cf46fd759dc (patch) | |
tree | 5b3e67923977c8dcf9b0951bd83b62dcd8291f62 | |
parent | 3da8700c1c88a44488732bae47bcc5b1cb545ca2 (diff) | |
download | chef-3bffbc968d4dc76cd6992c603f849cf46fd759dc.tar.gz |
Apply a named_run_list in policy builder via configuration
-rw-r--r-- | lib/chef/policy_builder/policyfile.rb | 36 | ||||
-rw-r--r-- | spec/unit/policy_builder/policyfile_spec.rb | 106 |
2 files changed, 120 insertions, 22 deletions
diff --git a/lib/chef/policy_builder/policyfile.rb b/lib/chef/policy_builder/policyfile.rb index 69ef485e14..bb9593eb36 100644 --- a/lib/chef/policy_builder/policyfile.rb +++ b/lib/chef/policy_builder/policyfile.rb @@ -170,6 +170,8 @@ class Chef # # @return [RunListExpansionIsh] A RunListExpansion duck-type. def expand_run_list + CookbookCacheCleaner.instance.skip_removal = true if named_run_list_requested? + node.run_list(run_list) node.automatic_attrs[:roles] = [] node.automatic_attrs[:recipes] = run_list_expansion_ish.recipes @@ -253,7 +255,14 @@ class Chef # @api private def run_list - policy["run_list"] + if named_run_list_requested? + named_run_list or + raise ConfigurationError, + "Policy '#{retrieved_policy_name}' revision '#{revision_id}' does not have named_run_list '#{named_run_list_name}'" + + "(available named_run_lists: [#{available_named_run_lists.join(', ')}])" + else + policy["run_list"] + end end # @api private @@ -441,6 +450,11 @@ class Chef end # @api private + def revision_id + policy["revision_id"] + end + + # @api private def http_api @api_service ||= Chef::REST.new(config[:chef_server_url]) end @@ -462,6 +476,26 @@ class Chef Chef.set_run_context(run_context) end + def retrieved_policy_name + policy["name"] + end + + def named_run_list + policy["named_run_lists"] && policy["named_run_lists"][named_run_list_name] + end + + def available_named_run_lists + (policy["named_run_lists"] || {}).keys + end + + def named_run_list_requested? + !!Chef::Config[:named_run_list] + end + + def named_run_list_name + Chef::Config[:named_run_list] + end + def compat_mode_manifest_for(cookbook_name, lock_data) xyz_version = lock_data["dotted_decimal_identifier"] rel_url = "cookbooks/#{cookbook_name}/#{xyz_version}" diff --git a/spec/unit/policy_builder/policyfile_spec.rb b/spec/unit/policy_builder/policyfile_spec.rb index 9a39161648..b656a66ec3 100644 --- a/spec/unit/policy_builder/policyfile_spec.rb +++ b/spec/unit/policy_builder/policyfile_spec.rb @@ -76,8 +76,11 @@ describe Chef::PolicyBuilder::Policyfile do let(:policyfile_run_list) { ["recipe[example1::default]", "recipe[example2::server]"] } - let(:parsed_policyfile_json) do + let(:basic_valid_policy_data) do { + "name" => "example-policy", + "revision_id" => "123abc", + "run_list" => policyfile_run_list, "cookbook_locks" => { @@ -90,6 +93,8 @@ describe Chef::PolicyBuilder::Policyfile do } end + let(:parsed_policyfile_json) { basic_valid_policy_data } + let(:err_namespace) { Chef::PolicyBuilder::Policyfile } it "configures a Chef HTTP API client" do @@ -498,34 +503,93 @@ describe Chef::PolicyBuilder::Policyfile do expect(node["override_key"]).to be_nil end - it "applies ohai data" do - expect(ohai_data).to_not be_empty # ensure test is testing something - ohai_data.each do |key, value| - expect(node.automatic_attrs[key]).to eq(value) + describe "setting attribute values" do + + before do + policy_builder.build_node end - end - it "applies attributes from json file" do - expect(node["custom_attr"]).to eq("custom_attr_value") - end + it "resets default and override data" do + expect(node["default_key"]).to be_nil + expect(node["override_key"]).to be_nil + end - it "applies attributes from the policyfile" do - expect(node["policyfile_default_attr"]).to eq("policyfile_default_value") - expect(node["policyfile_override_attr"]).to eq("policyfile_override_value") - end + it "applies ohai data" do + expect(ohai_data).to_not be_empty # ensure test is testing something + ohai_data.each do |key, value| + expect(node.automatic_attrs[key]).to eq(value) + end + end - it "sets the policyfile's run_list on the node object" do - expect(node.run_list).to eq(policyfile_run_list) - end + it "applies attributes from json file" do + expect(node["custom_attr"]).to eq("custom_attr_value") + end - it "creates node.automatic_attrs[:roles]" do - expect(node.automatic_attrs[:roles]).to eq([]) - end + it "applies attributes from the policyfile" do + expect(node["policyfile_default_attr"]).to eq("policyfile_default_value") + expect(node["policyfile_override_attr"]).to eq("policyfile_override_value") + end + + it "sets the policyfile's run_list on the node object" do + expect(node.run_list).to eq(policyfile_run_list) + end - it "create node.automatic_attrs[:recipes]" do - expect(node.automatic_attrs[:recipes]).to eq(["example1::default", "example2::server"]) + it "creates node.automatic_attrs[:roles]" do + expect(node.automatic_attrs[:roles]).to eq([]) + end + + it "create node.automatic_attrs[:recipes]" do + expect(node.automatic_attrs[:recipes]).to eq(["example1::default", "example2::server"]) + end end + context "when a named run_list is given" do + + before do + Chef::Config[:named_run_list] = "deploy-app" + end + + context "and the named run_list is not present in the policy" do + + it "raises a ConfigurationError" do + err_class = Chef::PolicyBuilder::Policyfile::ConfigurationError + err_text = "Policy 'example-policy' revision '123abc' does not have named_run_list 'deploy-app'(available named_run_lists: [])" + expect { policy_builder.build_node }.to raise_error(err_class, err_text) + end + + end + + context "and the named run_list is present in the policy" do + + let(:parsed_policyfile_json) do + basic_valid_policy_data.dup.tap do |p| + p["named_run_lists"] = { + "deploy-app" => [ "recipe[example1::default]" ] + } + end + end + + before do + policy_builder.build_node + end + + it "sets the run list to the desired named run list" do + expect(policy_builder.run_list).to eq([ "recipe[example1::default]" ]) + expected_expansion = Chef::PolicyBuilder::Policyfile::RunListExpansionIsh.new([ "example1::default" ], []) + expect(policy_builder.run_list_expansion).to eq(expected_expansion) + expect(policy_builder.run_list_with_versions_for_display).to eq(["example1::default@2.3.5 (168d210)"]) + expect(node.run_list).to eq([ Chef::RunList::RunListItem.new("recipe[example1::default]") ]) + expect(node[:roles]).to eq( [] ) + expect(node[:recipes]).to eq( ["example1::default"] ) + end + + it "disables the cookbook cache cleaner" do + expect(Chef::CookbookCacheCleaner.instance.skip_removal).to be(true) + end + + end + + end end |