Skip to content

Commit

Permalink
Handle cookbook artfact format differences when fetching cookbooks
Browse files Browse the repository at this point in the history
Cookbook artifacts differ in these ways:
* the name field is the cookbook name instead of name+version
* there is no "cookbook_name" field
* cookbook artifacts don't have a json_class when downloaded from the
  server
* there is an identifier field
  • Loading branch information
danielsdeleo committed Mar 25, 2015
1 parent a6fbf12 commit 93c6691
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/chef/cookbook/remote_file_vendor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RemoteFileVendor < FileVendor

def initialize(manifest, rest)
@manifest = manifest
@cookbook_name = @manifest[:cookbook_name]
@cookbook_name = @manifest[:cookbook_name] || @manifest[:name]
@rest = rest
end

Expand Down
6 changes: 5 additions & 1 deletion lib/chef/policy_builder/policyfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,18 @@ def compat_mode_manifest_for(cookbook_name, lock_data)
def artifact_manifest_for(cookbook_name, lock_data)
identifier = lock_data["identifier"]
rel_url = "cookbook_artifacts/#{cookbook_name}/#{identifier}"
http_api.get(rel_url)
inflate_cbv_object(http_api.get(rel_url))
rescue Exception => e
message = "Error loading cookbook #{cookbook_name} with identifier #{identifier} from #{rel_url}: #{e.class} - #{e.message}"
err = Chef::Exceptions::CookbookNotFound.new(message)
err.set_backtrace(e.backtrace)
raise err
end

def inflate_cbv_object(raw_manifest)
Chef::CookbookVersion.from_cb_artifact_data(raw_manifest)
end

end
end
end
Expand Down
36 changes: 28 additions & 8 deletions spec/unit/cookbook/file_vendor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

let(:file_vendor_class) { Class.new(described_class) }

# A manifest is a Hash of the format defined by Chef::CookbookVersion#manifest
let(:manifest) { {:cookbook_name => "bob"} }

context "when configured to fetch files over http" do

let(:http) { double("Chef::REST") }
Expand All @@ -40,19 +37,42 @@
expect(file_vendor_class.initialization_options).to eq(http)
end

it "creates a RemoteFileVendor for a given manifest" do
file_vendor = file_vendor_class.create_from_manifest(manifest)
expect(file_vendor).to be_a_kind_of(Chef::Cookbook::RemoteFileVendor)
expect(file_vendor.rest).to eq(http)
expect(file_vendor.cookbook_name).to eq("bob")
context "with a manifest from a cookbook version" do

# A manifest is a Hash of the format defined by Chef::CookbookVersion#manifest
let(:manifest) { {:cookbook_name => "bob", :name => "bob-1.2.3"} }

it "creates a RemoteFileVendor for a given manifest" do
file_vendor = file_vendor_class.create_from_manifest(manifest)
expect(file_vendor).to be_a_kind_of(Chef::Cookbook::RemoteFileVendor)
expect(file_vendor.rest).to eq(http)
expect(file_vendor.cookbook_name).to eq("bob")
end

end

context "with a manifest from a cookbook artifact" do

# A manifest is a Hash of the format defined by Chef::CookbookVersion#manifest
let(:manifest) { {:name => "bob"} }

it "creates a RemoteFileVendor for a given manifest" do
file_vendor = file_vendor_class.create_from_manifest(manifest)
expect(file_vendor).to be_a_kind_of(Chef::Cookbook::RemoteFileVendor)
expect(file_vendor.rest).to eq(http)
expect(file_vendor.cookbook_name).to eq("bob")
end

end
end

context "when configured to load files from disk" do

let(:cookbook_path) { %w[/var/chef/cookbooks /var/chef/other_cookbooks] }

# A manifest is a Hash of the format defined by Chef::CookbookVersion#manifest
let(:manifest) { {:cookbook_name => "bob"} }

before do
file_vendor_class.fetch_from_disk(cookbook_path)
end
Expand Down

0 comments on commit 93c6691

Please sign in to comment.