diff options
-rw-r--r-- | lib/chef/cookbook/metadata.rb | 6 | ||||
-rw-r--r-- | lib/chef/exceptions.rb | 12 | ||||
-rw-r--r-- | spec/integration/knife/upload_spec.rb | 29 | ||||
-rw-r--r-- | spec/integration/solo/solo_spec.rb | 34 | ||||
-rw-r--r-- | spec/unit/cookbook/metadata_spec.rb | 52 |
5 files changed, 123 insertions, 10 deletions
diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb index 93c62862a1..6c5663ae40 100644 --- a/lib/chef/cookbook/metadata.rb +++ b/lib/chef/cookbook/metadata.rb @@ -2,7 +2,7 @@ # Author:: Adam Jacob (<adam@opscode.com>) # Author:: AJ Christensen (<aj@opscode.com>) # Author:: Seth Falcon (<seth@opscode.com>) -# Copyright:: Copyright 2008-2010 Opscode, Inc. +# Copyright:: Copyright 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -652,13 +652,13 @@ class Chef def validate_ohai_version! unless gem_dep_matches?("ohai", Gem::Version.new(Ohai::VERSION), *ohai_versions) - raise Exceptions::CookbookOhaiVersionMismatch.new(Ohai::VERSION, *ohai_versions) + raise Exceptions::CookbookOhaiVersionMismatch.new(Ohai::VERSION, name, version, *ohai_versions) end end def validate_chef_version! unless gem_dep_matches?("chef", Gem::Version.new(Chef::VERSION), *chef_versions) - raise Exceptions::CookbookChefVersionMismatch.new(Chef::VERSION, *chef_versions) + raise Exceptions::CookbookChefVersionMismatch.new(Chef::VERSION, name, version, *chef_versions) end end diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index ef73caa276..8172311dd6 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -2,7 +2,7 @@ # Author:: Adam Jacob (<adam@opscode.com>) # Author:: Seth Falcon (<seth@opscode.com>) # Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>) -# Copyright:: Copyright 2008-2010 Opscode, Inc. +# Copyright:: Copyright 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -483,14 +483,16 @@ class Chef end class CookbookChefVersionMismatch < RuntimeError - def initialize(chef_version, *constraints) - super "chef version #{chef_version} is badness" + def initialize(chef_version, cookbook_name, cookbook_version, *constraints) + constraint_str = constraints.map { |c| c.requirement.as_list.to_s }.join(', ') + super "Cookbook '#{cookbook_name}' version '#{cookbook_version}' depends on chef version #{constraint_str}, but the running chef version is #{chef_version}" end end class CookbookOhaiVersionMismatch < RuntimeError - def initialize(ohai_version, *constraints) - super "ohai version #{ohai_version} is badness" + def initialize(ohai_version, cookbook_name, cookbook_version, *constraints) + constraint_str = constraints.map { |c| c.requirement.as_list.to_s }.join(', ') + super "Cookbook '#{cookbook_name}' version '#{cookbook_version}' depends on ohai version #{constraint_str}, but the running ohai version is #{ohai_version}" end end diff --git a/spec/integration/knife/upload_spec.rb b/spec/integration/knife/upload_spec.rb index 6ca8c3d8ce..27736349cc 100644 --- a/spec/integration/knife/upload_spec.rb +++ b/spec/integration/knife/upload_spec.rb @@ -1,6 +1,6 @@ # # Author:: John Keiser (<jkeiser@opscode.com>) -# Copyright:: Copyright (c) 2013 Opscode, Inc. +# Copyright:: Copyright (c) 2013-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -699,6 +699,19 @@ EOH end end end + when_the_chef_server "is empty" do + when_the_repository 'has a cookbook with an invalid chef_version constraint in it' do + before do + file 'cookbooks/x/metadata.rb', cb_metadata('x', '1.0.0', "\nchef_version '~> 999.0'") + end + it 'knife upload succeeds' do + knife('upload /cookbooks/x').should_succeed <<EOM +Updated /cookbooks/x +EOM + knife('diff --name-status /cookbooks').should_succeed '' + end + end + end end # without versioned cookbooks with_versioned_cookbooks do @@ -1219,6 +1232,20 @@ EOM end end end + + when_the_chef_server "is empty" do + when_the_repository 'has a cookbook with an invalid chef_version constraint in it' do + before do + file 'cookbooks/x-1.0.0/metadata.rb', cb_metadata('x', '1.0.0', "\nchef_version '~> 999.0'") + end + it 'knife upload succeeds' do + knife('upload /cookbooks/x').should_succeed <<EOM +Updated /cookbooks/x +EOM + knife('diff --name-status /cookbooks').should_succeed '' + end + end + end end # with versioned cookbooks when_the_chef_server 'has a user' do diff --git a/spec/integration/solo/solo_spec.rb b/spec/integration/solo/solo_spec.rb index f45933c799..6240a38144 100644 --- a/spec/integration/solo/solo_spec.rb +++ b/spec/integration/solo/solo_spec.rb @@ -70,6 +70,40 @@ EOM end end + when_the_repository "has a cookbook with an incompatible chef_version" do + before do + file 'cookbooks/x/metadata.rb', cb_metadata('x', '1.0.0', "\nchef_version '~> 999.0'") + file 'cookbooks/x/recipes/default.rb', 'puts "ITWORKS"' + file 'config/solo.rb', <<EOM +cookbook_path "#{path_to('cookbooks')}" +file_cache_path "#{path_to('config/cache')}" +EOM + end + + it "should exit with an error" do + result = shell_out("#{chef_solo} -c \"#{path_to('config/solo.rb')}\" -o 'x::default' -l debug", :cwd => chef_dir) + expect(result.exitstatus).to eq(1) + expect(result.stdout).to include("Chef::Exceptions::CookbookChefVersionMismatch") + end + end + + when_the_repository "has a cookbook with an incompatible ohai_version" do + before do + file 'cookbooks/x/metadata.rb', cb_metadata('x', '1.0.0', "\nohai_version '~> 999.0'") + file 'cookbooks/x/recipes/default.rb', 'puts "ITWORKS"' + file 'config/solo.rb', <<EOM +cookbook_path "#{path_to('cookbooks')}" +file_cache_path "#{path_to('config/cache')}" +EOM + end + + it "should exit with an error" do + result = shell_out("#{chef_solo} -c \"#{path_to('config/solo.rb')}\" -o 'x::default' -l debug", :cwd => chef_dir) + expect(result.exitstatus).to eq(1) + expect(result.stdout).to include("Chef::Exceptions::CookbookOhaiVersionMismatch") + end + end + when_the_repository "has a cookbook with a recipe with sleep" do before do diff --git a/spec/unit/cookbook/metadata_spec.rb b/spec/unit/cookbook/metadata_spec.rb index ce8fb1ff26..0215858180 100644 --- a/spec/unit/cookbook/metadata_spec.rb +++ b/spec/unit/cookbook/metadata_spec.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@opscode.com>) # Author:: Seth Falcon (<seth@opscode.com>) -# Copyright:: Copyright 2008-2010 Opscode, Inc. +# Copyright:: Copyright 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -351,6 +351,31 @@ describe Chef::Cookbook::Metadata do it "should work with multiple complex constraints" do expect_chef_version_works([">= 11.14.2", "< 11.18.10"],[">= 12.2.1", "< 12.5.1"]) end + + it "should fail validation on a simple pessimistic constraint" do + expect_chef_version_works(["~> 999.0"]) + expect { metadata.validate_chef_version! }.not_to raise_error(Chef::Exceptions::CookbookChefVersionMismatch) + end + + it "should fail validation when that valid chef versions are too big" do + expect_chef_version_works([">= 999.0", "< 999.9"]) + expect { metadata.validate_chef_version! }.to raise_error(Chef::Exceptions::CookbookChefVersionMismatch) + end + + it "should fail validation when that valid chef versions are too small" do + expect_chef_version_works([">= 0.0.1", "< 0.0.9"]) + expect { metadata.validate_chef_version! }.to raise_error(Chef::Exceptions::CookbookChefVersionMismatch) + end + + it "should fail validation when all ranges fail" do + expect_chef_version_works([">= 999.0", "< 999.9"],[">= 0.0.1", "< 0.0.9"]) + expect { metadata.validate_chef_version! }.to raise_error(Chef::Exceptions::CookbookChefVersionMismatch) + end + + it "should pass validation when one constraint passes" do + expect_chef_version_works([">= 999.0", "< 999.9"],["= #{Chef::VERSION}"]) + expect { metadata.validate_chef_version! }.not_to raise_error + end end describe "ohai_version" do @@ -378,6 +403,31 @@ describe Chef::Cookbook::Metadata do it "should work with multiple complex constraints" do expect_ohai_version_works([">= 11.14.2", "< 11.18.10"],[">= 12.2.1", "< 12.5.1"]) end + + it "should fail validation on a simple pessimistic constraint" do + expect_ohai_version_works(["~> 999.0"]) + expect { metadata.validate_ohai_version! }.to raise_error(Chef::Exceptions::CookbookOhaiVersionMismatch) + end + + it "should fail validation when that valid chef versions are too big" do + expect_ohai_version_works([">= 999.0", "< 999.9"]) + expect { metadata.validate_ohai_version! }.to raise_error(Chef::Exceptions::CookbookOhaiVersionMismatch) + end + + it "should fail validation when that valid chef versions are too small" do + expect_ohai_version_works([">= 0.0.1", "< 0.0.9"]) + expect { metadata.validate_ohai_version! }.to raise_error(Chef::Exceptions::CookbookOhaiVersionMismatch) + end + + it "should fail validation when all ranges fail" do + expect_ohai_version_works([">= 999.0", "< 999.9"],[">= 0.0.1", "< 0.0.9"]) + expect { metadata.validate_ohai_version! }.to raise_error(Chef::Exceptions::CookbookOhaiVersionMismatch) + end + + it "should pass validation when one constraint passes" do + expect_ohai_version_works([">= 999.0", "< 999.9"],["= #{Ohai::VERSION}"]) + expect { metadata.validate_ohai_version! }.not_to raise_error + end end describe "attribute groupings" do |