chef-cli 4.0.0 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/chef-cli.gemspec +1 -1
- data/lib/chef-cli/command/export.rb +14 -6
- data/lib/chef-cli/command/generate.rb +5 -3
- data/lib/chef-cli/command/generator_commands/cookbook.rb +15 -2
- data/lib/chef-cli/command/generator_commands/recipe.rb +7 -0
- data/lib/chef-cli/cookbook_metadata.rb +1 -1
- data/lib/chef-cli/exceptions.rb +13 -1
- data/lib/chef-cli/helpers.rb +4 -4
- data/lib/chef-cli/policyfile/dsl.rb +16 -1
- data/lib/chef-cli/policyfile_services/export_repo.rb +9 -13
- data/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb +26 -5
- data/lib/chef-cli/skeletons/code_generator/recipes/recipe.rb +11 -3
- data/lib/chef-cli/skeletons/code_generator/templates/default/delivery-project.toml.erb +1 -1
- data/lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb +18 -0
- data/lib/chef-cli/version.rb +1 -1
- data/spec/unit/command/export_spec.rb +18 -1
- data/spec/unit/command/generate_spec.rb +7 -0
- data/spec/unit/command/generator_commands/cookbook_spec.rb +49 -235
- data/spec/unit/command/generator_commands/recipe_spec.rb +34 -0
- data/spec/unit/cookbook_metadata_spec.rb +20 -1
- data/spec/unit/fixtures/example_cookbook_both_metadata/.gitignore +17 -0
- data/spec/unit/fixtures/example_cookbook_both_metadata/.kitchen.yml +16 -0
- data/spec/unit/fixtures/example_cookbook_both_metadata/Berksfile +3 -0
- data/spec/unit/fixtures/example_cookbook_both_metadata/README.md +4 -0
- data/spec/unit/fixtures/example_cookbook_both_metadata/chefignore +96 -0
- data/spec/unit/fixtures/example_cookbook_both_metadata/metadata.json +5 -0
- data/spec/unit/fixtures/example_cookbook_both_metadata/metadata.rb +9 -0
- data/spec/unit/fixtures/example_cookbook_both_metadata/recipes/default.rb +8 -0
- data/spec/unit/helpers_spec.rb +111 -0
- data/spec/unit/policyfile_evaluation_spec.rb +67 -1
- data/spec/unit/policyfile_services/export_repo_spec.rb +51 -2
- metadata +22 -14
- data/lib/chef-cli/command/generator_commands/build_cookbook.rb +0 -126
- data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md +0 -146
- data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml +0 -21
- data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb +0 -8
- data/lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json +0 -17
- data/lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb +0 -175
- data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb +0 -7
- data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb +0 -10
- data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb +0 -9
- data/spec/unit/command/generator_commands/build_cookbook_spec.rb +0 -377
@@ -1,377 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright:: Copyright (c) 2014-2018 Chef Software Inc.
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
require "spec_helper"
|
19
|
-
require "shared/custom_generator_cookbook"
|
20
|
-
require "shared/setup_git_committer_config"
|
21
|
-
require "chef-cli/command/generator_commands/build_cookbook"
|
22
|
-
require "mixlib/shellout"
|
23
|
-
|
24
|
-
describe ChefCLI::Command::GeneratorCommands::BuildCookbook do
|
25
|
-
|
26
|
-
include_context("setup_git_committer_config")
|
27
|
-
|
28
|
-
let(:argv) { %w{workflow_project} }
|
29
|
-
|
30
|
-
let(:stdout_io) { StringIO.new }
|
31
|
-
let(:stderr_io) { StringIO.new }
|
32
|
-
|
33
|
-
let(:expected_cookbook_file_relpaths) do
|
34
|
-
%w{
|
35
|
-
kitchen.yml
|
36
|
-
data_bags
|
37
|
-
data_bags/keys
|
38
|
-
data_bags/keys/delivery_builder_keys.json
|
39
|
-
test
|
40
|
-
test/fixtures
|
41
|
-
test/fixtures/cookbooks
|
42
|
-
test/fixtures/cookbooks/test
|
43
|
-
test/fixtures/cookbooks/test/metadata.rb
|
44
|
-
test/fixtures/cookbooks/test/recipes
|
45
|
-
test/fixtures/cookbooks/test/recipes/default.rb
|
46
|
-
Berksfile
|
47
|
-
chefignore
|
48
|
-
metadata.rb
|
49
|
-
README.md
|
50
|
-
LICENSE
|
51
|
-
recipes
|
52
|
-
recipes/default.rb
|
53
|
-
recipes/deploy.rb
|
54
|
-
recipes/functional.rb
|
55
|
-
recipes/lint.rb
|
56
|
-
recipes/provision.rb
|
57
|
-
recipes/publish.rb
|
58
|
-
recipes/quality.rb
|
59
|
-
recipes/security.rb
|
60
|
-
recipes/smoke.rb
|
61
|
-
recipes/syntax.rb
|
62
|
-
recipes/unit.rb
|
63
|
-
secrets
|
64
|
-
secrets/fakey-mcfakerton
|
65
|
-
}
|
66
|
-
end
|
67
|
-
|
68
|
-
let(:expected_cookbook_files) do
|
69
|
-
expected_cookbook_file_relpaths.map do |relpath|
|
70
|
-
File.join(tempdir, "workflow_project", ".delivery", "build_cookbook", relpath)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
subject(:cookbook_generator) do
|
75
|
-
described_class.new(argv)
|
76
|
-
end
|
77
|
-
|
78
|
-
def generator_context
|
79
|
-
ChefCLI::Generator.context
|
80
|
-
end
|
81
|
-
|
82
|
-
before do
|
83
|
-
ChefCLI::Generator.reset
|
84
|
-
end
|
85
|
-
|
86
|
-
it "configures the chef runner" do
|
87
|
-
expect(cookbook_generator.chef_runner).to be_a(ChefCLI::ChefRunner)
|
88
|
-
expect(cookbook_generator.chef_runner.cookbook_path).to eq(File.expand_path("lib/chef-cli/skeletons", project_root))
|
89
|
-
end
|
90
|
-
|
91
|
-
context "when given invalid/incomplete arguments" do
|
92
|
-
|
93
|
-
let(:expected_help_message) do
|
94
|
-
"Usage: chef generate build-cookbook NAME [options]\n"
|
95
|
-
end
|
96
|
-
|
97
|
-
def with_argv(argv)
|
98
|
-
generator = described_class.new(argv)
|
99
|
-
allow(generator).to receive(:stdout).and_return(stdout_io)
|
100
|
-
allow(generator).to receive(:stderr).and_return(stderr_io)
|
101
|
-
generator
|
102
|
-
end
|
103
|
-
|
104
|
-
it "prints usage when args are empty" do
|
105
|
-
with_argv([]).run
|
106
|
-
expect(stderr_io.string).to include(expected_help_message)
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
context "when given the name of the delivery project" do
|
112
|
-
|
113
|
-
let(:argv) { %w{workflow_project} }
|
114
|
-
|
115
|
-
let(:project_dir) { File.join(tempdir, "workflow_project") }
|
116
|
-
|
117
|
-
before do
|
118
|
-
reset_tempdir
|
119
|
-
Dir.mkdir(project_dir)
|
120
|
-
end
|
121
|
-
|
122
|
-
it "configures the generator context" do
|
123
|
-
cookbook_generator.read_and_validate_params
|
124
|
-
cookbook_generator.setup_context
|
125
|
-
expect(generator_context.workflow_project_dir).to eq(File.join(Dir.pwd, "workflow_project"))
|
126
|
-
end
|
127
|
-
|
128
|
-
it "creates a build cookbook" do
|
129
|
-
Dir.chdir(tempdir) do
|
130
|
-
allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
131
|
-
expect(cookbook_generator.run).to eq(0)
|
132
|
-
end
|
133
|
-
generated_files = Dir.glob("#{tempdir}/workflow_project/**/*", File::FNM_DOTMATCH)
|
134
|
-
expected_cookbook_files.each do |expected_file|
|
135
|
-
expect(generated_files).to include(expected_file)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
shared_examples_for "a generated file" do |context_var|
|
140
|
-
before do
|
141
|
-
Dir.chdir(tempdir) do
|
142
|
-
allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
143
|
-
expect(cookbook_generator.run).to eq(0)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
it "should contain #{context_var} from the generator context" do
|
148
|
-
expect(File.read(file)).to include(line)
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
# This shared example group requires a let binding for
|
153
|
-
# `expected_kitchen_yml_content`
|
154
|
-
shared_examples_for "kitchen_yml_and_integration_tests" do
|
155
|
-
|
156
|
-
describe "Generating Test Kitchen and integration testing files" do
|
157
|
-
|
158
|
-
before do
|
159
|
-
Dir.chdir(tempdir) do
|
160
|
-
allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
161
|
-
expect(cookbook_generator.run).to eq(0)
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
let(:file) { File.join(tempdir, "workflow_project", ".delivery", "build_cookbook", "kitchen.yml") }
|
166
|
-
|
167
|
-
it "creates a kitchen.yml with the expected content" do
|
168
|
-
expect(IO.read(file)).to eq(expected_kitchen_yml_content)
|
169
|
-
end
|
170
|
-
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
context "when the delivery project is a cookbook" do
|
175
|
-
|
176
|
-
let(:parent_metadata_rb) { File.join(tempdir, "workflow_project", "metadata.rb") }
|
177
|
-
|
178
|
-
before do
|
179
|
-
FileUtils.touch(parent_metadata_rb)
|
180
|
-
end
|
181
|
-
|
182
|
-
it "detects that the parent project is a cookbook" do
|
183
|
-
Dir.chdir(tempdir) do
|
184
|
-
cookbook_generator.read_and_validate_params
|
185
|
-
cookbook_generator.setup_context
|
186
|
-
expect(generator_context.build_cookbook_parent_is_cookbook).to eq(true)
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
describe "metadata.rb" do
|
191
|
-
let(:file) { File.join(tempdir, "workflow_project", ".delivery", "build_cookbook", "metadata.rb") }
|
192
|
-
|
193
|
-
include_examples "a generated file", :cookbook_name do
|
194
|
-
let(:line) do
|
195
|
-
<<~METADATA
|
196
|
-
name 'build_cookbook'
|
197
|
-
maintainer 'The Authors'
|
198
|
-
maintainer_email 'you@example.com'
|
199
|
-
license 'all_rights'
|
200
|
-
version '0.1.0'
|
201
|
-
chef_version '>= 15.0'
|
202
|
-
|
203
|
-
depends 'delivery-truck'
|
204
|
-
METADATA
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
describe "delivery phase recipes" do
|
210
|
-
|
211
|
-
before do
|
212
|
-
Dir.chdir(tempdir) do
|
213
|
-
allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
214
|
-
expect(cookbook_generator.run).to eq(0)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
it "generates phase recipes which include the corresponding delivery truck recipe" do
|
219
|
-
%w{
|
220
|
-
deploy.rb
|
221
|
-
functional.rb
|
222
|
-
lint.rb
|
223
|
-
provision.rb
|
224
|
-
publish.rb
|
225
|
-
quality.rb
|
226
|
-
security.rb
|
227
|
-
smoke.rb
|
228
|
-
syntax.rb
|
229
|
-
unit.rb
|
230
|
-
}.each do |phase_recipe|
|
231
|
-
recipe_file = File.join(tempdir, "workflow_project", ".delivery", "build_cookbook", "recipes", phase_recipe)
|
232
|
-
phase = File.basename(phase_recipe, ".rb")
|
233
|
-
expected_content = %Q{include_recipe 'delivery-truck::#{phase}'}
|
234
|
-
expect(IO.read(recipe_file)).to include(expected_content)
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
end
|
239
|
-
|
240
|
-
end
|
241
|
-
|
242
|
-
context "when the delivery project is not a cookbook" do
|
243
|
-
|
244
|
-
it "detects that the parent project is not a cookbook" do
|
245
|
-
cookbook_generator.read_and_validate_params
|
246
|
-
cookbook_generator.setup_context
|
247
|
-
expect(generator_context.build_cookbook_parent_is_cookbook).to eq(false)
|
248
|
-
end
|
249
|
-
|
250
|
-
describe "metadata.rb" do
|
251
|
-
let(:file) { File.join(tempdir, "workflow_project", ".delivery", "build_cookbook", "metadata.rb") }
|
252
|
-
|
253
|
-
include_examples "a generated file", :cookbook_name do
|
254
|
-
let(:line) do
|
255
|
-
<<~METADATA
|
256
|
-
name 'build_cookbook'
|
257
|
-
maintainer 'The Authors'
|
258
|
-
maintainer_email 'you@example.com'
|
259
|
-
license 'all_rights'
|
260
|
-
version '0.1.0'
|
261
|
-
METADATA
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
describe "delivery phase recipes" do
|
267
|
-
|
268
|
-
before do
|
269
|
-
Dir.chdir(tempdir) do
|
270
|
-
allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
271
|
-
expect(cookbook_generator.run).to eq(0)
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
it "generates phase recipes that are empty" do
|
276
|
-
%w{
|
277
|
-
deploy.rb
|
278
|
-
functional.rb
|
279
|
-
lint.rb
|
280
|
-
provision.rb
|
281
|
-
publish.rb
|
282
|
-
quality.rb
|
283
|
-
security.rb
|
284
|
-
smoke.rb
|
285
|
-
syntax.rb
|
286
|
-
unit.rb
|
287
|
-
}.each do |phase_recipe|
|
288
|
-
recipe_file = File.join(tempdir, "workflow_project", ".delivery", "build_cookbook", "recipes", phase_recipe)
|
289
|
-
expect(IO.read(recipe_file)).to_not include("include_recipe")
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
end
|
294
|
-
|
295
|
-
end
|
296
|
-
|
297
|
-
context "when the delivery project is a git repo" do
|
298
|
-
|
299
|
-
let(:readme) { File.join(project_dir, "README.md") }
|
300
|
-
|
301
|
-
def git!(cmd)
|
302
|
-
Mixlib::ShellOut.new("git #{cmd}", cwd: project_dir).tap do |c|
|
303
|
-
c.run_command
|
304
|
-
c.error!
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
before do
|
309
|
-
FileUtils.touch(readme)
|
310
|
-
|
311
|
-
git!("init .")
|
312
|
-
git!("add .")
|
313
|
-
git!("commit --no-gpg-sign -m \"initial commit\"")
|
314
|
-
|
315
|
-
Dir.chdir(tempdir) do
|
316
|
-
allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
317
|
-
expect(cookbook_generator.run).to eq(0)
|
318
|
-
end
|
319
|
-
end
|
320
|
-
|
321
|
-
it "creates delivery config in a feature branch and merges it" do
|
322
|
-
expect(git!("log").stdout).to include("Merge branch 'add-delivery-configuration'")
|
323
|
-
end
|
324
|
-
|
325
|
-
end
|
326
|
-
|
327
|
-
context "when the delivery project has already a config.json and project.toml" do
|
328
|
-
|
329
|
-
let(:dot_delivery) { File.join(project_dir, ".delivery") }
|
330
|
-
let(:config_json) { File.join(dot_delivery, "config.json") }
|
331
|
-
let(:project_toml) { File.join(dot_delivery, "project.toml") }
|
332
|
-
|
333
|
-
def git!(cmd)
|
334
|
-
Mixlib::ShellOut.new("git #{cmd}", cwd: project_dir).tap do |c|
|
335
|
-
c.run_command
|
336
|
-
c.error!
|
337
|
-
end
|
338
|
-
end
|
339
|
-
|
340
|
-
before do
|
341
|
-
FileUtils.mkdir_p(dot_delivery)
|
342
|
-
FileUtils.touch(config_json)
|
343
|
-
FileUtils.touch(project_toml)
|
344
|
-
|
345
|
-
git!("init .")
|
346
|
-
git!("add .")
|
347
|
-
git!("commit --no-gpg-sign -m \"initial commit\"")
|
348
|
-
|
349
|
-
Dir.chdir(tempdir) do
|
350
|
-
allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
351
|
-
expect(cookbook_generator.run).to eq(0)
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
it "does not overwrite the delivery config" do
|
356
|
-
expect(git!("log").stdout).to_not include("Add generated delivery configuration")
|
357
|
-
end
|
358
|
-
|
359
|
-
end
|
360
|
-
end
|
361
|
-
|
362
|
-
context "when given a path including the .delivery directory" do
|
363
|
-
let(:argv) { [ File.join(tempdir, "workflow_project", ".delivery", "build_cookbook") ] }
|
364
|
-
|
365
|
-
before do
|
366
|
-
reset_tempdir
|
367
|
-
end
|
368
|
-
|
369
|
-
it "correctly sets the delivery project dir to the parent of the .delivery dir" do
|
370
|
-
cookbook_generator.read_and_validate_params
|
371
|
-
cookbook_generator.setup_context
|
372
|
-
expect(generator_context.workflow_project_dir).to eq(File.join(tempdir, "workflow_project"))
|
373
|
-
end
|
374
|
-
|
375
|
-
end
|
376
|
-
|
377
|
-
end
|