From b9e2d317c82e5ef646723041baf81aa55052bae5 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Tue, 10 Feb 2015 10:24:05 -0800 Subject: Switch save URL based on policy mode flag --- lib/chef/cookbook_manifest.rb | 15 ++++++++++--- spec/unit/cookbook_manifest_spec.rb | 42 +++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/chef/cookbook_manifest.rb b/lib/chef/cookbook_manifest.rb index 417afbd82c..92f20664bd 100644 --- a/lib/chef/cookbook_manifest.rb +++ b/lib/chef/cookbook_manifest.rb @@ -39,8 +39,9 @@ class Chef def_delegator :@cookbook_version, :version def_delegator :@cookbook_version, :frozen_version? - def initialize(cookbook_version) + def initialize(cookbook_version, policy_mode: false) @cookbook_version = cookbook_version + @policy_mode = !!policy_mode reset! end @@ -99,6 +100,10 @@ class Chef @manifest_records_by_path end + def policy_mode? + @policy_mode + end + def to_hash result = manifest.dup result['frozen?'] = frozen_version? @@ -116,14 +121,14 @@ class Chef # REST api. If there is an existing document on the server and it # is marked frozen, a PUT will result in a 409 Conflict. def save_url - "cookbooks/#{name}/#{version}" + "#{cookbook_url_path}/#{name}/#{version}" end # Adds the `force=true` parameter to the upload URL. This allows # the user to overwrite a frozen cookbook (a PUT against the # normal #save_url raises a 409 Conflict in this case). def force_save_url - "cookbooks/#{name}/#{version}?force=true" + "#{cookbook_url_path}/#{name}/#{version}?force=true" end # TODO: This is kind of terrible. investigate removing it @@ -144,6 +149,10 @@ class Chef private + def cookbook_url_path + policy_mode? ? "cookbook_artifacts" : "cookbooks" + end + # See #manifest for a description of the manifest return value. # See #preferred_manifest_record for a description an individual manifest record. def generate_manifest diff --git a/spec/unit/cookbook_manifest_spec.rb b/spec/unit/cookbook_manifest_spec.rb index 8f9c9810d4..938f72c743 100644 --- a/spec/unit/cookbook_manifest_spec.rb +++ b/spec/unit/cookbook_manifest_spec.rb @@ -38,7 +38,19 @@ describe Chef::CookbookManifest do end end - subject(:cookbook_manifest) { Chef::CookbookManifest.new(cookbook_version) } + let(:policy_mode) { false } + + subject(:cookbook_manifest) { Chef::CookbookManifest.new(cookbook_version, policy_mode: policy_mode) } + + context "when policy mode is not specified" do + + subject(:cookbook_manifest) { Chef::CookbookManifest.new(cookbook_version) } + + it "defaults to policies disabled" do + expect(cookbook_manifest.policy_mode?).to be(false) + end + + end describe "collecting cookbook data from the cookbook version object" do @@ -157,6 +169,7 @@ describe Chef::CookbookManifest do "root_files" => map_to_file_specs(root_filenames), } end + before do cookbook_version.attribute_filenames = attribute_filenames cookbook_version.definition_filenames = definition_filenames @@ -183,14 +196,31 @@ describe Chef::CookbookManifest do describe "providing upstream URLs for save" do - it "gives the save URL" do - expect(cookbook_manifest.save_url).to eq("cookbooks/tatft/1.2.3") - end + context "and policy mode is disabled" do + + it "gives the save URL" do + expect(cookbook_manifest.save_url).to eq("cookbooks/tatft/1.2.3") + end + + it "gives the force save URL" do + expect(cookbook_manifest.force_save_url).to eq("cookbooks/tatft/1.2.3?force=true") + end - it "gives the force save URL" do - expect(cookbook_manifest.force_save_url).to eq("cookbooks/tatft/1.2.3?force=true") end + context "and policy mode is enabled" do + + let(:policy_mode) { true } + + it "gives the save URL" do + expect(cookbook_manifest.save_url).to eq("cookbook_artifacts/tatft/1.2.3") + end + + it "gives the force save URL" do + expect(cookbook_manifest.force_save_url).to eq("cookbook_artifacts/tatft/1.2.3?force=true") + end + + end end end -- cgit v1.2.1