1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright 2014-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
describe Chef::Cookbook::CookbookVersionLoader do
before do
allow(ChefConfig).to receive(:windows?) { false }
end
describe "loading a cookbook" do
let(:chefignore) { nil }
let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "cookbooks/openldap") }
let(:cookbook_loader) { Chef::Cookbook::CookbookVersionLoader.new(cookbook_path, chefignore) }
let(:loaded_cookbook) do
cookbook_loader.load!
cookbook_loader.cookbook_version
end
def full_path(cookbook_relative_path)
File.join(cookbook_path, cookbook_relative_path)
end
it "loads attribute files of the cookbook" do
expect(loaded_cookbook.attribute_filenames).to include(full_path("/attributes/default.rb"))
expect(loaded_cookbook.attribute_filenames).to include(full_path("/attributes/smokey.rb"))
end
it "loads definition files" do
expect(loaded_cookbook.definition_filenames).to include(full_path("/definitions/client.rb"))
expect(loaded_cookbook.definition_filenames).to include(full_path("/definitions/server.rb"))
end
it "loads recipes" do
expect(loaded_cookbook.recipe_filenames).to include(full_path("/recipes/default.rb"))
expect(loaded_cookbook.recipe_filenames).to include(full_path("/recipes/gigantor.rb"))
expect(loaded_cookbook.recipe_filenames).to include(full_path("/recipes/one.rb"))
expect(loaded_cookbook.recipe_filenames).to include(full_path("/recipes/return.rb"))
end
it "loads libraries" do
expect(loaded_cookbook.library_filenames).to include(full_path("/libraries/openldap.rb"))
expect(loaded_cookbook.library_filenames).to include(full_path("/libraries/openldap/version.rb"))
end
it "loads static files in the files/ dir" do
expect(loaded_cookbook.file_filenames).to include(full_path("/files/default/remotedir/remotesubdir/remote_subdir_file1.txt"))
expect(loaded_cookbook.file_filenames).to include(full_path("/files/default/remotedir/remotesubdir/remote_subdir_file2.txt"))
end
it "loads files that start with a ." do
expect(loaded_cookbook.file_filenames).to include(full_path("/files/default/.dotfile"))
expect(loaded_cookbook.file_filenames).to include(full_path("/files/default/.ssh/id_rsa"))
expect(loaded_cookbook.file_filenames).to include(full_path("/files/default/remotedir/.a_dotdir/.a_dotfile_in_a_dotdir"))
end
it "loads all unignored files, even if they don't match a segment type" do
expect(loaded_cookbook.all_unignored_files).to include(full_path("/spec/spec_helper.rb"))
end
it "should load the metadata for the cookbook" do
expect(loaded_cookbook.metadata.name.to_s).to eq("openldap")
expect(loaded_cookbook.metadata).to be_a_kind_of(Chef::Cookbook::Metadata)
end
context "when a cookbook has ignored files" do
let(:chefignore) { Chef::Cookbook::Chefignore.new(File.join(CHEF_SPEC_DATA, "cookbooks")) }
let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "kitchen/openldap") }
it "skips ignored files" do
expect(loaded_cookbook.recipe_filenames).to include(full_path("recipes/gigantor.rb"))
expect(loaded_cookbook.recipe_filenames).to include(full_path("recipes/woot.rb"))
expect(loaded_cookbook.recipe_filenames).to_not include(full_path("recipes/ignoreme.rb"))
end
end
context "when the given path is not actually a cookbook" do
let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "cookbooks/NOTHING_HERE_FOLKS") }
it "raises an error when loading with #load!" do
expect { cookbook_loader.load! }.to raise_error(Chef::Exceptions::CookbookNotFoundInRepo)
end
it "skips the cookbook when called with #load" do
expect { cookbook_loader.load }.to_not raise_error
end
end
context "when a cookbook has a metadata name different than directory basename" do
let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "cookbooks/name-mismatch-versionnumber") }
it "prefers the metadata name to the directory basename" do
expect(loaded_cookbook.name).to eq(:"name-mismatch")
end
end
context "when a cookbook has a metadata file with a ruby error [CHEF-2923]" do
let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "invalid-metadata-chef-repo/invalid-metadata") }
it "raises an error when loading with #load!" do
expect { cookbook_loader.load! }.to raise_error("THIS METADATA HAS A BUG")
end
it "raises an error when called with #load" do
expect { cookbook_loader.load }.to raise_error("THIS METADATA HAS A BUG")
end
it "doesn't raise an error when determining the cookbook name" do
expect { cookbook_loader.cookbook_name }.to_not raise_error
end
it "doesn't raise an error when metadata is first generated" do
expect { cookbook_loader.metadata }.to_not raise_error
end
it "sets an error flag containing error information" do
cookbook_loader.metadata
expect(cookbook_loader.metadata_error).to be_a(StandardError)
expect(cookbook_loader.metadata_error.message).to eq("THIS METADATA HAS A BUG")
end
end
context "when a cookbook has a metadata file with invalid metadata" do
let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "incomplete-metadata-chef-repo/incomplete-metadata") }
let(:error_message) do
"Cookbook loaded at path(s) [#{cookbook_path}] has invalid metadata: The `name' attribute is required in cookbook metadata"
end
it "raises an error when loading with #load!" do
expect { cookbook_loader.load! }.to raise_error(Chef::Exceptions::MetadataNotValid, error_message)
end
it "raises an error when called with #load" do
expect { cookbook_loader.load }.to raise_error(Chef::Exceptions::MetadataNotValid, error_message)
end
it "uses the inferred cookbook name [CHEF-2923]" do
# This behavior is intended to support the CHEF-2923 feature where
# invalid metadata doesn't prevent you from uploading other cookbooks.
#
# The metadata is the definitive source of the cookbook name, but if
# the metadata is incomplete/invalid, we can't read the name from it.
#
# The CookbookLoader stores CookbookVersionLoaders in a Hash with
# cookbook names as the keys, and finds the loader in this Hash to call
# #load on it when the user runs a command like `knife cookbook upload specific-cookbook`
#
# Most of the time this will work, but if the user tries to upload a
# specific cookbook by name, has customized that cookbook's name (so it
# doesn't match the inferred name), and that metadata file has a syntax
# error, we might report a "cookbook not found" error instead of the
# metadata syntax error that is the actual cause.
expect(cookbook_loader.cookbook_name).to eq(:"incomplete-metadata")
end
end
end
end
|