Skip to content

Commit

Permalink
[CHEF-2373] Handle no versions of a cookbook available in environment
Browse files Browse the repository at this point in the history
cdb_load_filtered_recipe_list was failing if there was a cookbook that
had no available versions for a given environment.

also improved tests.
  • Loading branch information
Seth Falcon committed May 24, 2011
1 parent 72e642c commit 765c9fb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
18 changes: 12 additions & 6 deletions chef/lib/chef/environment.rb
@@ -1,6 +1,7 @@
#
# Author:: Stephen Delano (<stephen@opscode.com>)
# Author:: Seth Falcon (<seth@opscode.com>)
# Author:: John Keiser (<jkeiser@ospcode.com>)
# Copyright:: Copyright 2010-2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
Expand Down Expand Up @@ -398,12 +399,17 @@ def self.cdb_minimal_filtered_versions(name, couchdb=nil)

def self.cdb_load_filtered_recipe_list(name, couchdb=nil)
cdb_load_filtered_cookbook_versions(name, couchdb).map do |cb_name, cb|
cb.first.recipe_filenames_by_name.keys.map do |recipe|
case recipe
when DEFAULT
cb_name
else
"#{cb_name}::#{recipe}"
if cb.empty? # no available versions
[] # empty list elided with flatten
else
latest_version = cb.first
latest_version.recipe_filenames_by_name.keys.map do |recipe|
case recipe
when DEFAULT
cb_name
else
"#{cb_name}::#{recipe}"
end
end
end
end.flatten
Expand Down
31 changes: 30 additions & 1 deletion chef/spec/unit/environment_spec.rb
@@ -1,7 +1,8 @@
#
# Author:: Stephen Delano (<stephen@ospcode.com>)
# Author:: Seth Falcon (<seth@ospcode.com>)
# Copyright:: Copyright 2010 Opscode, Inc.
# Author:: John Keiser (<jkeiser@ospcode.com>)
# Copyright:: Copyright 2010-2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -247,21 +248,25 @@
@all_cookbooks << begin
cv = Chef::CookbookVersion.new("apt")
cv.version = "1.0.0"
cv.recipe_filenames = ["default.rb", "only-in-1-0.rb"]
cv
end
@all_cookbooks << begin
cv = Chef::CookbookVersion.new("apt")
cv.version = "1.1.0"
cv.recipe_filenames = ["default.rb", "only-in-1-1.rb"]
cv
end
@all_cookbooks << begin
cv = Chef::CookbookVersion.new("apache2")
cv.version = "2.0.0"
cv.recipe_filenames = ["default.rb", "mod_ssl.rb"]
cv
end
@all_cookbooks << begin
cv = Chef::CookbookVersion.new("god")
cv.version = "4.2.0"
cv.recipe_filenames = ["default.rb"]
cv
end
Chef::CookbookVersion.stub!(:cdb_list).and_return @all_cookbooks
Expand All @@ -272,9 +277,33 @@
Chef::Environment.cdb_load_filtered_cookbook_versions("prod")
end

it "should handle cookbooks with no available version" do
@environment.cookbook_versions({
"apt" => "> 999.0.0",
"apache2" => "= 2.0.0"
})
Chef::Environment.should_receive(:cdb_load).with("prod", nil)
recipes = Chef::Environment.cdb_load_filtered_recipe_list("prod")
# order doesn't matter
recipes.should =~ ["god", "apache2", "apache2::mod_ssl"]
end


it "should load all the cookbook versions" do
Chef::CookbookVersion.should_receive(:cdb_list)
Chef::Environment.cdb_load_filtered_cookbook_versions("prod")
recipes = Chef::Environment.cdb_load_filtered_recipe_list("prod")
recipes.should =~ ["apache2", "apache2::mod_ssl", "apt",
"apt::only-in-1-0", "god"]
end

it "should load all the cookbook versions with no policy" do
@environment.cookbook_versions({})
Chef::CookbookVersion.should_receive(:cdb_list)
Chef::Environment.cdb_load_filtered_cookbook_versions("prod")
recipes = Chef::Environment.cdb_load_filtered_recipe_list("prod")
recipes.should =~ ["apache2", "apache2::mod_ssl", "apt",
"apt::only-in-1-1", "god"]
end

it "should restrict the cookbook versions, as specified in the environment" do
Expand Down

0 comments on commit 765c9fb

Please sign in to comment.