From 0c6ebdc02cf3892e5ad395879cf80e9f18290af9 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Wed, 23 Apr 2014 14:28:08 -0700 Subject: Make root_dir required for CookbookVersion to generate manifest --- spec/unit/cookbook_version_spec.rb | 346 +++++++++++++++++++----------- spec/unit/knife/cookbook_metadata_spec.rb | 4 +- spec/unit/knife/cookbook_upload_spec.rb | 20 +- spec/unit/version_constraint_spec.rb | 2 +- 4 files changed, 237 insertions(+), 135 deletions(-) (limited to 'spec') diff --git a/spec/unit/cookbook_version_spec.rb b/spec/unit/cookbook_version_spec.rb index d186ef01be..f20bc3766a 100644 --- a/spec/unit/cookbook_version_spec.rb +++ b/spec/unit/cookbook_version_spec.rb @@ -20,7 +20,7 @@ require 'spec_helper' describe Chef::CookbookVersion do describe "when first created" do before do - @cookbook_version = Chef::CookbookVersion.new("tatft") + @cookbook_version = Chef::CookbookVersion.new("tatft", '/tmp/blah') end it "has a name" do @@ -96,156 +96,258 @@ describe Chef::CookbookVersion do end end - describe "after the cookbook has been loaded" do + describe "with a cookbook directory named tatft" do MD5 = /[0-9a-f]{32}/ before do - # Currently the cookbook loader finds all the files then tells CookbookVersion - # where they are. - @cookbook_version = Chef::CookbookVersion.new("tatft") - @cookbook = Hash.new { |hash, key| hash[key] = [] } - cookbook_root = File.join(CHEF_SPEC_DATA, 'cb_version_cookbooks', 'tatft') + @cookbook_root = File.join(CHEF_SPEC_DATA, 'cb_version_cookbooks', 'tatft') # Dunno if the paths here are representitive of what is set by CookbookLoader... - @cookbook[:attribute_filenames] = Dir[File.join(cookbook_root, 'attributes', '**', '*.rb')] - @cookbook[:definition_filenames] = Dir[File.join(cookbook_root, 'definitions', '**', '*.rb')] - @cookbook[:file_filenames] = Dir[File.join(cookbook_root, 'files', '**', '*.tgz')] - @cookbook[:recipe_filenames] = Dir[File.join(cookbook_root, 'recipes', '**', '*.rb')] - @cookbook[:template_filenames] = Dir[File.join(cookbook_root, 'templates', '**', '*.erb')] - @cookbook[:library_filenames] = Dir[File.join(cookbook_root, 'libraries', '**', '*.rb')] - @cookbook[:resource_filenames] = Dir[File.join(cookbook_root, 'resources', '**', '*.rb')] - @cookbook[:provider_filenames] = Dir[File.join(cookbook_root, 'providers', '**', '*.rb')] - @cookbook[:root_filenames] = Array(File.join(cookbook_root, 'README.rdoc')) - @cookbook[:metadata_filenames] = Array(File.join(cookbook_root, 'metadata.json')) - - @cookbook_version.attribute_filenames = @cookbook[:attribute_filenames] - @cookbook_version.definition_filenames = @cookbook[:definition_filenames] - @cookbook_version.recipe_filenames = @cookbook[:recipe_filenames] - @cookbook_version.template_filenames = @cookbook[:template_filenames] - @cookbook_version.file_filenames = @cookbook[:file_filenames] - @cookbook_version.library_filenames = @cookbook[:library_filenames] - @cookbook_version.resource_filenames = @cookbook[:resource_filenames] - @cookbook_version.provider_filenames = @cookbook[:provider_filenames] - @cookbook_version.root_filenames = @cookbook[:root_filenames] - @cookbook_version.metadata_filenames = @cookbook[:metadata_filenames] - - # Used to test file-specificity related file lookups - @node = Chef::Node.new - @node.set[:platform] = "ubuntu" - @node.set[:platform_version] = "13.04" - @node.name("testing") + @cookbook[:attribute_filenames] = Dir[File.join(@cookbook_root, 'attributes', '**', '*.rb')] + @cookbook[:definition_filenames] = Dir[File.join(@cookbook_root, 'definitions', '**', '*.rb')] + @cookbook[:file_filenames] = Dir[File.join(@cookbook_root, 'files', '**', '*.tgz')] + @cookbook[:recipe_filenames] = Dir[File.join(@cookbook_root, 'recipes', '**', '*.rb')] + @cookbook[:template_filenames] = Dir[File.join(@cookbook_root, 'templates', '**', '*.erb')] + @cookbook[:library_filenames] = Dir[File.join(@cookbook_root, 'libraries', '**', '*.rb')] + @cookbook[:resource_filenames] = Dir[File.join(@cookbook_root, 'resources', '**', '*.rb')] + @cookbook[:provider_filenames] = Dir[File.join(@cookbook_root, 'providers', '**', '*.rb')] + @cookbook[:root_filenames] = Array(File.join(@cookbook_root, 'README.rdoc')) + @cookbook[:metadata_filenames] = Array(File.join(@cookbook_root, 'metadata.json')) + end - it "generates a manifest containing the cookbook's files" do - manifest = @cookbook_version.manifest + describe "and a cookbook with the same name" do + before do + # Currently the cookbook loader finds all the files then tells CookbookVersion + # where they are. + @cookbook_version = Chef::CookbookVersion.new("tatft", @cookbook_root) + + @cookbook_version.attribute_filenames = @cookbook[:attribute_filenames] + @cookbook_version.definition_filenames = @cookbook[:definition_filenames] + @cookbook_version.recipe_filenames = @cookbook[:recipe_filenames] + @cookbook_version.template_filenames = @cookbook[:template_filenames] + @cookbook_version.file_filenames = @cookbook[:file_filenames] + @cookbook_version.library_filenames = @cookbook[:library_filenames] + @cookbook_version.resource_filenames = @cookbook[:resource_filenames] + @cookbook_version.provider_filenames = @cookbook[:provider_filenames] + @cookbook_version.root_filenames = @cookbook[:root_filenames] + @cookbook_version.metadata_filenames = @cookbook[:metadata_filenames] + + # Used to test file-specificity related file lookups + @node = Chef::Node.new + @node.set[:platform] = "ubuntu" + @node.set[:platform_version] = "13.04" + @node.name("testing") + end - manifest["metadata"].should == Chef::Cookbook::Metadata.new - manifest["cookbook_name"].should == "tatft" + it "generates a manifest containing the cookbook's files" do + manifest = @cookbook_version.manifest - manifest["recipes"].should have(1).recipe_file + manifest["metadata"].should == Chef::Cookbook::Metadata.new + manifest["cookbook_name"].should == "tatft" - recipe = manifest["recipes"].first - recipe["name"].should == "default.rb" - recipe["path"].should == "recipes/default.rb" - recipe["checksum"].should match(MD5) - recipe["specificity"].should == "default" + manifest["recipes"].should have(1).recipe_file - manifest["definitions"].should have(1).definition_file + recipe = manifest["recipes"].first + recipe["name"].should == "default.rb" + recipe["path"].should == "recipes/default.rb" + recipe["checksum"].should match(MD5) + recipe["specificity"].should == "default" - definition = manifest["definitions"].first - definition["name"].should == "runit_service.rb" - definition["path"].should == "definitions/runit_service.rb" - definition["checksum"].should match(MD5) - definition["specificity"].should == "default" + manifest["definitions"].should have(1).definition_file - manifest["libraries"].should have(1).library_file + definition = manifest["definitions"].first + definition["name"].should == "runit_service.rb" + definition["path"].should == "definitions/runit_service.rb" + definition["checksum"].should match(MD5) + definition["specificity"].should == "default" - library = manifest["libraries"].first - library["name"].should == "ownage.rb" - library["path"].should == "libraries/ownage.rb" - library["checksum"].should match(MD5) - library["specificity"].should == "default" + manifest["libraries"].should have(1).library_file - manifest["attributes"].should have(1).attribute_file + library = manifest["libraries"].first + library["name"].should == "ownage.rb" + library["path"].should == "libraries/ownage.rb" + library["checksum"].should match(MD5) + library["specificity"].should == "default" - attribute_file = manifest["attributes"].first - attribute_file["name"].should == "default.rb" - attribute_file["path"].should == "attributes/default.rb" - attribute_file["checksum"].should match(MD5) - attribute_file["specificity"].should == "default" + manifest["attributes"].should have(1).attribute_file - manifest["files"].should have(1).cookbook_file + attribute_file = manifest["attributes"].first + attribute_file["name"].should == "default.rb" + attribute_file["path"].should == "attributes/default.rb" + attribute_file["checksum"].should match(MD5) + attribute_file["specificity"].should == "default" - cookbook_file = manifest["files"].first - cookbook_file["name"].should == "giant_blob.tgz" - cookbook_file["path"].should == "files/default/giant_blob.tgz" - cookbook_file["checksum"].should match(MD5) - cookbook_file["specificity"].should == "default" + manifest["files"].should have(1).cookbook_file - manifest["templates"].should have(1).template + cookbook_file = manifest["files"].first + cookbook_file["name"].should == "giant_blob.tgz" + cookbook_file["path"].should == "files/default/giant_blob.tgz" + cookbook_file["checksum"].should match(MD5) + cookbook_file["specificity"].should == "default" - template = manifest["templates"].first - template["name"].should == "configuration.erb" - template["path"].should == "templates/default/configuration.erb" - template["checksum"].should match(MD5) - template["specificity"].should == "default" + manifest["templates"].should have(1).template - manifest["resources"].should have(1).lwr + template = manifest["templates"].first + template["name"].should == "configuration.erb" + template["path"].should == "templates/default/configuration.erb" + template["checksum"].should match(MD5) + template["specificity"].should == "default" - lwr = manifest["resources"].first - lwr["name"].should == "lwr.rb" - lwr["path"].should == "resources/lwr.rb" - lwr["checksum"].should match(MD5) - lwr["specificity"].should == "default" + manifest["resources"].should have(1).lwr - manifest["providers"].should have(1).lwp + lwr = manifest["resources"].first + lwr["name"].should == "lwr.rb" + lwr["path"].should == "resources/lwr.rb" + lwr["checksum"].should match(MD5) + lwr["specificity"].should == "default" - lwp = manifest["providers"].first - lwp["name"].should == "lwp.rb" - lwp["path"].should == "providers/lwp.rb" - lwp["checksum"].should match(MD5) - lwp["specificity"].should == "default" + manifest["providers"].should have(1).lwp - manifest["root_files"].should have(1).file_in_the_cookbook_root + lwp = manifest["providers"].first + lwp["name"].should == "lwp.rb" + lwp["path"].should == "providers/lwp.rb" + lwp["checksum"].should match(MD5) + lwp["specificity"].should == "default" - readme = manifest["root_files"].first - readme["name"].should == "README.rdoc" - readme["path"].should == "README.rdoc" - readme["checksum"].should match(MD5) - readme["specificity"].should == "default" - end + manifest["root_files"].should have(1).file_in_the_cookbook_root - it "determines whether a template is available for a given node" do - @cookbook_version.should have_template_for_node(@node, "configuration.erb") - @cookbook_version.should_not have_template_for_node(@node, "missing.erb") - end + readme = manifest["root_files"].first + readme["name"].should == "README.rdoc" + readme["path"].should == "README.rdoc" + readme["checksum"].should match(MD5) + readme["specificity"].should == "default" + end - it "determines whether a cookbook_file is available for a given node" do - @cookbook_version.should have_cookbook_file_for_node(@node, "giant_blob.tgz") - @cookbook_version.should_not have_cookbook_file_for_node(@node, "missing.txt") - end + it "determines whether a template is available for a given node" do + @cookbook_version.should have_template_for_node(@node, "configuration.erb") + @cookbook_version.should_not have_template_for_node(@node, "missing.erb") + end - describe "raises an error when attempting to load a missing cookbook_file and" do - before do - node = Chef::Node.new.tap do |n| - n.name("sample.node") - n.automatic_attrs[:fqdn] = "sample.example.com" - n.automatic_attrs[:platform] = "ubuntu" - n.automatic_attrs[:platform_version] = "10.04" + it "determines whether a cookbook_file is available for a given node" do + @cookbook_version.should have_cookbook_file_for_node(@node, "giant_blob.tgz") + @cookbook_version.should_not have_cookbook_file_for_node(@node, "missing.txt") + end + + describe "raises an error when attempting to load a missing cookbook_file and" do + before do + node = Chef::Node.new.tap do |n| + n.name("sample.node") + n.automatic_attrs[:fqdn] = "sample.example.com" + n.automatic_attrs[:platform] = "ubuntu" + n.automatic_attrs[:platform_version] = "10.04" + end + @attempt_to_load_file = lambda { @cookbook_version.preferred_manifest_record(node, :files, "no-such-thing.txt") } + end + + it "describes the cookbook and version" do + useful_explanation = Regexp.new(Regexp.escape("Cookbook 'tatft' (0.0.0) does not contain")) + @attempt_to_load_file.should raise_error(Chef::Exceptions::FileNotFound, useful_explanation) + end + + it "lists suggested places to look" do + useful_explanation = Regexp.new(Regexp.escape("files/default/no-such-thing.txt")) + @attempt_to_load_file.should raise_error(Chef::Exceptions::FileNotFound, useful_explanation) end - @attempt_to_load_file = lambda { @cookbook_version.preferred_manifest_record(node, :files, "no-such-thing.txt") } end + end - it "describes the cookbook and version" do - useful_explanation = Regexp.new(Regexp.escape("Cookbook 'tatft' (0.0.0) does not contain")) - @attempt_to_load_file.should raise_error(Chef::Exceptions::FileNotFound, useful_explanation) + describe "and a cookbook_version with a different name" do + before do + # Currently the cookbook loader finds all the files then tells CookbookVersion + # where they are. + @cookbook_version = Chef::CookbookVersion.new("blarghle", @cookbook_root) + @cookbook_version.attribute_filenames = @cookbook[:attribute_filenames] + @cookbook_version.definition_filenames = @cookbook[:definition_filenames] + @cookbook_version.recipe_filenames = @cookbook[:recipe_filenames] + @cookbook_version.template_filenames = @cookbook[:template_filenames] + @cookbook_version.file_filenames = @cookbook[:file_filenames] + @cookbook_version.library_filenames = @cookbook[:library_filenames] + @cookbook_version.resource_filenames = @cookbook[:resource_filenames] + @cookbook_version.provider_filenames = @cookbook[:provider_filenames] + @cookbook_version.root_filenames = @cookbook[:root_filenames] + @cookbook_version.metadata_filenames = @cookbook[:metadata_filenames] end - it "lists suggested places to look" do - useful_explanation = Regexp.new(Regexp.escape("files/default/no-such-thing.txt")) - @attempt_to_load_file.should raise_error(Chef::Exceptions::FileNotFound, useful_explanation) + it "generates a manifest containing the cookbook's files" do + manifest = @cookbook_version.manifest + + manifest["metadata"].should == Chef::Cookbook::Metadata.new + manifest["cookbook_name"].should == "blarghle" + + manifest["recipes"].should have(1).recipe_file + + recipe = manifest["recipes"].first + recipe["name"].should == "default.rb" + recipe["path"].should == "recipes/default.rb" + recipe["checksum"].should match(MD5) + recipe["specificity"].should == "default" + + manifest["definitions"].should have(1).definition_file + + definition = manifest["definitions"].first + definition["name"].should == "runit_service.rb" + definition["path"].should == "definitions/runit_service.rb" + definition["checksum"].should match(MD5) + definition["specificity"].should == "default" + + manifest["libraries"].should have(1).library_file + + library = manifest["libraries"].first + library["name"].should == "ownage.rb" + library["path"].should == "libraries/ownage.rb" + library["checksum"].should match(MD5) + library["specificity"].should == "default" + + manifest["attributes"].should have(1).attribute_file + + attribute_file = manifest["attributes"].first + attribute_file["name"].should == "default.rb" + attribute_file["path"].should == "attributes/default.rb" + attribute_file["checksum"].should match(MD5) + attribute_file["specificity"].should == "default" + + manifest["files"].should have(1).cookbook_file + + cookbook_file = manifest["files"].first + cookbook_file["name"].should == "giant_blob.tgz" + cookbook_file["path"].should == "files/default/giant_blob.tgz" + cookbook_file["checksum"].should match(MD5) + cookbook_file["specificity"].should == "default" + + manifest["templates"].should have(1).template + + template = manifest["templates"].first + template["name"].should == "configuration.erb" + template["path"].should == "templates/default/configuration.erb" + template["checksum"].should match(MD5) + template["specificity"].should == "default" + + manifest["resources"].should have(1).lwr + + lwr = manifest["resources"].first + lwr["name"].should == "lwr.rb" + lwr["path"].should == "resources/lwr.rb" + lwr["checksum"].should match(MD5) + lwr["specificity"].should == "default" + + manifest["providers"].should have(1).lwp + + lwp = manifest["providers"].first + lwp["name"].should == "lwp.rb" + lwp["path"].should == "providers/lwp.rb" + lwp["checksum"].should match(MD5) + lwp["specificity"].should == "default" + + manifest["root_files"].should have(1).file_in_the_cookbook_root + + readme = manifest["root_files"].first + readme["name"].should == "README.rdoc" + readme["path"].should == "README.rdoc" + readme["checksum"].should match(MD5) + readme["specificity"].should == "default" end end @@ -270,8 +372,8 @@ describe Chef::CookbookVersion do ["1.2", "2.1"] ] examples.each do |smaller, larger| - sm = Chef::CookbookVersion.new("foo") - lg = Chef::CookbookVersion.new("foo") + sm = Chef::CookbookVersion.new("foo", '/tmp/blah') + lg = Chef::CookbookVersion.new("foo", '/tmp/blah') sm.version = smaller lg.version = larger sm.should be < lg @@ -281,8 +383,8 @@ describe Chef::CookbookVersion do end it "should equate versions 1.2 and 1.2.0" do - a = Chef::CookbookVersion.new("foo") - b = Chef::CookbookVersion.new("foo") + a = Chef::CookbookVersion.new("foo", '/tmp/blah') + b = Chef::CookbookVersion.new("foo", '/tmp/blah') a.version = "1.2" b.version = "1.2.0" a.should == b @@ -290,9 +392,9 @@ describe Chef::CookbookVersion do it "should not allow you to sort cookbooks with different names" do - apt = Chef::CookbookVersion.new "apt" + apt = Chef::CookbookVersion.new "apt", '/tmp/blah' apt.version = "1.0" - god = Chef::CookbookVersion.new "god" + god = Chef::CookbookVersion.new "god", '/tmp/blah' god.version = "2.0" lambda {apt <=> god}.should raise_error(Chef::Exceptions::CookbookVersionNameMismatch) end @@ -300,7 +402,7 @@ describe Chef::CookbookVersion do describe "when you set a version" do before do - @cbv = Chef::CookbookVersion.new("version validation") + @cbv = Chef::CookbookVersion.new("version validation", '/tmp/blah') end it "should accept valid cookbook versions" do good_versions = %w(1.2 1.2.3 1000.80.50000 0.300.25) diff --git a/spec/unit/knife/cookbook_metadata_spec.rb b/spec/unit/knife/cookbook_metadata_spec.rb index 92db650435..26ff43829c 100644 --- a/spec/unit/knife/cookbook_metadata_spec.rb +++ b/spec/unit/knife/cookbook_metadata_spec.rb @@ -51,9 +51,9 @@ describe Chef::Knife::CookbookMetadata do describe 'with -a or --all' do before(:each) do @knife.config[:all] = true - @foo = Chef::CookbookVersion.new('foo') + @foo = Chef::CookbookVersion.new('foo', '/tmp/blah') @foo.version = '1.0.0' - @bar = Chef::CookbookVersion.new('bar') + @bar = Chef::CookbookVersion.new('bar', '/tmp/blah') @bar.version = '2.0.0' @cookbook_loader = { "foo" => @foo, diff --git a/spec/unit/knife/cookbook_upload_spec.rb b/spec/unit/knife/cookbook_upload_spec.rb index 5c7a4c1125..6a99a80459 100644 --- a/spec/unit/knife/cookbook_upload_spec.rb +++ b/spec/unit/knife/cookbook_upload_spec.rb @@ -23,7 +23,7 @@ require 'chef/cookbook_uploader' require 'timeout' describe Chef::Knife::CookbookUpload do - let(:cookbook) { Chef::CookbookVersion.new('test_cookbook') } + let(:cookbook) { Chef::CookbookVersion.new('test_cookbook', '/tmp/blah.txt') } let(:cookbooks_by_name) do {cookbook.name => cookbook} @@ -39,7 +39,7 @@ describe Chef::Knife::CookbookUpload do let(:cookbook_uploader) { double(:upload_cookbooks => nil) } let(:output) { StringIO.new } - + let(:name_args) { ['test_cookbook'] } let(:knife) do @@ -58,7 +58,7 @@ describe Chef::Knife::CookbookUpload do it 'should upload cookbooks with predefined concurrency' do Chef::CookbookVersion.stub(:list_all_versions).and_return({}) knife.config[:concurrency] = 3 - test_cookbook = Chef::CookbookVersion.new('test_cookbook') + test_cookbook = Chef::CookbookVersion.new('test_cookbook', '/tmp/blah') cookbook_loader.stub(:each).and_yield("test_cookbook", test_cookbook) cookbook_loader.stub(:cookbook_names).and_return(["test_cookbook"]) Chef::CookbookUploader.should_receive(:new).with( kind_of(Array), kind_of(Array), @@ -131,9 +131,9 @@ E let(:cookbooks_by_name) do { - 'test_cookbook1' => Chef::CookbookVersion.new('test_cookbook1'), - 'test_cookbook2' => Chef::CookbookVersion.new('test_cookbook2'), - 'test_cookbook3' => Chef::CookbookVersion.new('test_cookbook3') + 'test_cookbook1' => Chef::CookbookVersion.new('test_cookbook1', '/tmp/blah'), + 'test_cookbook2' => Chef::CookbookVersion.new('test_cookbook2', '/tmp/blah'), + 'test_cookbook3' => Chef::CookbookVersion.new('test_cookbook3', '/tmp/blah') } end @@ -163,7 +163,7 @@ E "test_cookbook3" => test_cookbook3 } end - let(:test_cookbook1) { Chef::CookbookVersion.new('test_cookbook1') } + let(:test_cookbook1) { Chef::CookbookVersion.new('test_cookbook1', '/tmp/blah') } let(:test_cookbook2) do c = Chef::CookbookVersion.new('test_cookbook2') @@ -191,7 +191,7 @@ E end describe 'when specifying a cookbook name with missing dependencies' do - let(:cookbook_dependency) { Chef::CookbookVersion.new('dependency') } + let(:cookbook_dependency) { Chef::CookbookVersion.new('dependency', '/tmp/blah') } before(:each) do cookbook.metadata.depends("dependency") @@ -245,8 +245,8 @@ E describe 'with -a or --all' do before(:each) do knife.config[:all] = true - @test_cookbook1 = Chef::CookbookVersion.new('test_cookbook1') - @test_cookbook2 = Chef::CookbookVersion.new('test_cookbook2') + @test_cookbook1 = Chef::CookbookVersion.new('test_cookbook1', '/tmp/blah') + @test_cookbook2 = Chef::CookbookVersion.new('test_cookbook2', '/tmp/blah') cookbook_loader.stub(:each).and_yield("test_cookbook1", @test_cookbook1).and_yield("test_cookbook2", @test_cookbook2) cookbook_loader.stub(:cookbook_names).and_return(["test_cookbook1", "test_cookbook2"]) end diff --git a/spec/unit/version_constraint_spec.rb b/spec/unit/version_constraint_spec.rb index 2c1246b776..a49b082d9d 100644 --- a/spec/unit/version_constraint_spec.rb +++ b/spec/unit/version_constraint_spec.rb @@ -76,7 +76,7 @@ describe Chef::VersionConstraint do @vc.should include Chef::Version.new("1.4") end it "Chef::CookbookVersion" do - cv = Chef::CookbookVersion.new("alice") + cv = Chef::CookbookVersion.new("alice", '/tmp/blah.txt') cv.version = "1.4" @vc.should include cv end -- cgit v1.2.1