chef-cli 3.1.6 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -0
  3. data/chef-cli.gemspec +1 -1
  4. data/lib/chef-cli/command/export.rb +14 -6
  5. data/lib/chef-cli/command/generate.rb +5 -3
  6. data/lib/chef-cli/command/generator_commands/cookbook.rb +32 -2
  7. data/lib/chef-cli/command/generator_commands/recipe.rb +7 -0
  8. data/lib/chef-cli/helpers.rb +4 -4
  9. data/lib/chef-cli/policyfile_services/export_repo.rb +9 -13
  10. data/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb +50 -24
  11. data/lib/chef-cli/skeletons/code_generator/recipes/recipe.rb +11 -3
  12. data/lib/chef-cli/skeletons/code_generator/templates/default/delivery-project.toml.erb +36 -0
  13. data/lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb +18 -0
  14. data/lib/chef-cli/version.rb +1 -1
  15. data/spec/unit/command/export_spec.rb +18 -1
  16. data/spec/unit/command/generate_spec.rb +7 -0
  17. data/spec/unit/command/generator_commands/cookbook_spec.rb +91 -226
  18. data/spec/unit/command/generator_commands/recipe_spec.rb +34 -0
  19. data/spec/unit/helpers_spec.rb +111 -0
  20. data/spec/unit/policyfile_services/export_repo_spec.rb +51 -2
  21. metadata +7 -14
  22. data/lib/chef-cli/command/generator_commands/build_cookbook.rb +0 -126
  23. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md +0 -146
  24. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml +0 -21
  25. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb +0 -8
  26. data/lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json +0 -17
  27. data/lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb +0 -175
  28. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb +0 -7
  29. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb +0 -10
  30. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb +0 -9
  31. data/spec/unit/command/generator_commands/build_cookbook_spec.rb +0 -377
@@ -1,146 +0,0 @@
1
- # build_cookbook
2
-
3
- A build cookbook for running the parent project through Chef Workflow
4
-
5
- This build cookbook should be customized to suit the needs of the parent project. Using this cookbook can be done outside of Chef Workflow, too. If the parent project is a Chef cookbook, we've detected that and "wrapped" [delivery-truck](https://github.com/chef-cookbooks/delivery-truck). That means it is a dependency, and each of its pipeline phase recipes is included in the appropriate phase recipes in this cookbook. If the parent project is not a cookbook, it's left as an exercise to the reader to customize the recipes as needed for each phase in the pipeline.
6
-
7
- ## .delivery/config.json
8
-
9
- In the parent directory to this build_cookbook, the `config.json` can be modified as necessary. For example, phases can be skipped, publishing information can be added, and so on. Refer to customer support or the Chef Workflow documentation for assistance on what options are available for this configuration.
10
-
11
- ## Test Kitchen - Local Verify Testing
12
-
13
- This cookbook also has a `kitchen.yml` which can be used to create local build nodes with Test Kitchen to perform the verification phases, `unit`, `syntax`, and `lint`. When running `kitchen converge`, the instances will be set up like Chef Workflow "build nodes" with the [delivery_build cookbook](https://github.com/chef-cookbooks/delivery_build). The reason for this is to make sure that the same exact kind of nodes are used by this build cookbook are run on the local workstation as would run Workflow. It will run `delivery job verify PHASE` for the parent project.
14
-
15
- Modify the `kitchen.yml` if necessary to change the platforms or other configuration to run the verify phases. After making changes in the parent project, `cd` into this directory (`.delivery/build_cookbook`), and run:
16
-
17
- ```
18
- kitchen test
19
- ```
20
-
21
- ## Recipes
22
-
23
- Each of the recipes in this build_cookbook are run in the named phase during the Chef Workflow pipeline. The `unit`, `syntax`, and `lint` recipes are additionally run when using Test Kitchen for local testing as noted in the above section.
24
-
25
- ## Making Changes - Cookbook Example
26
-
27
- When making changes in the parent project (that which lives in `../..` from this directory), or in the recipes in this build cookbook, there is a bespoke workflow for Chef Workflow. As an example, we'll discuss a Chef Cookbook as the parent.
28
-
29
- First, create a new branch for the changes.
30
-
31
- ```
32
- git checkout -b testing-build-cookbook
33
- ```
34
-
35
- Next, increment the version in the metadata.rb. This should be in the _parent_, not in this, the build_cookbook. If this is not done, the verify phase will fail.
36
-
37
- ```
38
- % git diff
39
- <SNIP>
40
- -version '0.1.0'
41
- +version '0.1.1'
42
- ```
43
-
44
- The change we'll use for an example is to install the `zsh` package. Write a failing ChefSpec in the cookbook project's `spec/unit/recipes/default_spec.rb`.
45
-
46
- ```ruby
47
- require 'spec_helper'
48
-
49
- describe 'godzilla::default' do
50
- context 'When all attributes are default, on Ubuntu 18.04' do
51
- let(:chef_run) do
52
- runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '18.04')
53
- runner.converge(described_recipe)
54
- end
55
-
56
- it 'installs zsh' do
57
- expect(chef_run).to install_package('zsh')
58
- end
59
- end
60
- end
61
- ```
62
-
63
- Commit the local changes as work in progress. The `delivery job` expects to use a clean git repository.
64
-
65
- ```
66
- git add ../..
67
- git commit -m 'WIP: Testing changes'
68
- ```
69
-
70
- From _this_ directory (`.delivery/build_cookbook`, relative to the parent cookbook project), run
71
-
72
- ```
73
- cd .delivery/build_cookbook
74
- kitchen converge
75
- ```
76
-
77
- This will take some time at first, because the VMs need to be created, Chef Infra Client installed, the Delivery CLI installed, etc. Later runs will be faster until they are destroyed. It will also fail on the first VM, as expected, because we wrote the test first. Now edit the parent cookbook project's default recipe to install `zsh`.
78
-
79
- ```
80
- cd ../../
81
- $EDITOR/recipes/default.rb
82
- ```
83
-
84
- It should look like this:
85
-
86
- ```
87
- package 'zsh'
88
- ```
89
-
90
- Create another commit.
91
-
92
- ```
93
- git add .
94
- git commit -m 'WIP: Install zsh in default recipe'
95
- ```
96
-
97
- Now rerun kitchen from the build_cookbook.
98
-
99
- ```
100
- cd .delivery/build_cookbook
101
- kitchen converge
102
- ```
103
-
104
- This will take a while because it will now pass on the first VM and then create the second VM. We should have warned you this was a good time for a coffee break.
105
-
106
- ```
107
- Recipe: test::default
108
-
109
- - execute HOME=/home/vagrant delivery job verify unit --server localhost --ent test --org kitchen
110
- * execute[HOME=/home/vagrant delivery job verify lint --server localhost --ent test --org kitchen] action run
111
- - execute HOME=/home/vagrant delivery job verify lint --server localhost --ent test --org kitchen
112
-
113
- - execute HOME=/home/vagrant delivery job verify syntax --server localhost --ent test --org kitchen
114
-
115
- Running handlers:
116
- Running handlers complete
117
- Chef Infra Client finished, 3/32 resources updated in 54.665445968 seconds
118
- Finished converging <default-centos-8> (1m26.83s).
119
- ```
120
-
121
- Victory is ours! Our verify phase passed on the build nodes.
122
-
123
- We are ready to run this through our Workflow pipeline. Simply run `delivery review` on the local system from the parent project, and it will open a browser window up to the change we just added.
124
-
125
- ```
126
- cd ../..
127
- delivery review
128
- ```
129
-
130
- ## FAQ
131
-
132
- ### Why don't I just run rspec and cookstyle on my local system?
133
-
134
- An objection to the Test Kitchen approach is that it is much faster to run the unit, lint, and syntax commands for the project on the local system. That is totally true, and also totally valid. Do that for the really fast feedback loop. However, the dance we do with Test Kitchen brings a much higher degree of confidence in the changes we're making, that everything will run on the build nodes in Chef Workflow. We strongly encourage this approach before actually pushing the changes to Workflow.
135
-
136
- ### Why do I have to make a commit every time?
137
-
138
- When running `delivery job`, it expects to merge the commit for the changeset against the clean master branch. If we don't save our progress by making a commit, our local changes aren't run through `delivery job` in the Test Kitchen build instances. We can always perform an interactive rebase, and modify the original changeset message in Workflow with `delivery review --edit`. The latter won't modify the git commits, only the changeset in Workflow.
139
-
140
- ### What do I do next?
141
-
142
- Make changes in the cookbook project as required for organizational goals and needs. Modify the `build_cookbook` as necessary for the pipeline phases that the cookbook should go through.
143
-
144
- ### What if I get stuck?
145
-
146
- Contact Chef Support, or your Chef Customer Success team and they will help you get unstuck.
@@ -1,21 +0,0 @@
1
- ---
2
- driver:
3
- name: vagrant
4
- synced_folders:
5
- - [<%= File.join(ENV['PWD'], '..', '..')%>, '/tmp/repo-data']
6
-
7
- provisioner:
8
- name: chef_zero
9
- encrypted_data_bag_secret_key_path: 'secrets/fakey-mcfakerton'
10
- data_bags_path: './data_bags'
11
- product_name: chefdk
12
-
13
- platforms:
14
- - name: ubuntu-20.04
15
- - name: centos-8
16
-
17
- suites:
18
- - name: default
19
- run_list:
20
- - recipe[test]
21
- attributes:
@@ -1,8 +0,0 @@
1
- %w(unit lint syntax).each do |phase|
2
- # TODO: This works on Linux/Unix. Not Windows.
3
- execute "HOME=/home/vagrant delivery job verify #{phase} --server localhost --ent test --org kitchen" do
4
- cwd '/tmp/repo-data'
5
- user 'vagrant'
6
- environment('GIT_DISCOVERY_ACROSS_FILESYSTEM' => '1')
7
- end
8
- end
@@ -1,17 +0,0 @@
1
- {
2
- "version": "2",
3
- "build_cookbook": {
4
- "name": "build_cookbook",
5
- "path": ".delivery/build_cookbook"
6
- },
7
- "delivery-truck": {
8
- "lint": {
9
- "enable_cookstyle": true
10
- }
11
- },
12
- "skip_phases": [],
13
- "job_dispatch": {
14
- "version": "v2"
15
- },
16
- "dependencies": []
17
- }
@@ -1,175 +0,0 @@
1
- context = ChefCLI::Generator.context
2
- workflow_project_dir = context.workflow_project_dir
3
- pipeline = context.pipeline
4
- dot_delivery_dir = File.join(workflow_project_dir, '.delivery')
5
- config_json = File.join(dot_delivery_dir, 'config.json')
6
- project_toml = File.join(dot_delivery_dir, 'project.toml')
7
-
8
- generator_desc('Ensuring delivery CLI configuration')
9
-
10
- directory dot_delivery_dir
11
-
12
- cookbook_file config_json do
13
- source 'delivery-config.json'
14
- not_if { ::File.exist?(config_json) }
15
- end
16
-
17
- # Adding the delivery local-mode config
18
- cookbook_file project_toml do
19
- source 'delivery-project.toml'
20
- not_if { ::File.exist?(project_toml) }
21
- end
22
-
23
- generator_desc('Ensuring correct Workflow (Delivery) build cookbook content')
24
-
25
- build_cookbook_dir = File.join(dot_delivery_dir, 'build_cookbook')
26
-
27
- # cookbook root dir
28
- directory build_cookbook_dir
29
-
30
- # metadata.rb
31
- template "#{build_cookbook_dir}/metadata.rb" do
32
- source 'build_cookbook/metadata.rb.erb'
33
- helpers(ChefCLI::Generator::TemplateHelper)
34
- action :create_if_missing
35
- end
36
-
37
- # README
38
- cookbook_file "#{build_cookbook_dir}/README.md" do
39
- source 'build_cookbook/README.md'
40
- action :create_if_missing
41
- end
42
-
43
- # LICENSE
44
- template "#{build_cookbook_dir}/LICENSE" do
45
- source "LICENSE.#{context.license}.erb"
46
- helpers(ChefCLI::Generator::TemplateHelper)
47
- action :create_if_missing
48
- end
49
-
50
- # chefignore
51
- cookbook_file "#{build_cookbook_dir}/chefignore"
52
-
53
- # Berksfile
54
- template "#{build_cookbook_dir}/Berksfile" do
55
- source 'build_cookbook/Berksfile.erb'
56
- helpers(ChefCLI::Generator::TemplateHelper)
57
- action :create_if_missing
58
- end
59
-
60
- # Recipes
61
- directory "#{build_cookbook_dir}/recipes"
62
-
63
- %w(default deploy functional lint provision publish quality security smoke syntax unit).each do |phase|
64
- template "#{build_cookbook_dir}/recipes/#{phase}.rb" do
65
- source 'build_cookbook/recipe.rb.erb'
66
- helpers(ChefCLI::Generator::TemplateHelper)
67
- variables phase: phase
68
- action :create_if_missing
69
- end
70
- end
71
-
72
- # Test Kitchen build node
73
- cookbook_file "#{build_cookbook_dir}/kitchen.yml" do
74
- source 'build_cookbook/kitchen.yml'
75
- end
76
-
77
- directory "#{build_cookbook_dir}/data_bags/keys" do
78
- recursive true
79
- end
80
-
81
- file "#{build_cookbook_dir}/data_bags/keys/delivery_builder_keys.json" do
82
- content '{"id": "delivery_builder_keys"}'
83
- end
84
-
85
- directory "#{build_cookbook_dir}/secrets"
86
-
87
- file "#{build_cookbook_dir}/secrets/fakey-mcfakerton"
88
-
89
- directory "#{build_cookbook_dir}/test/fixtures/cookbooks/test/recipes" do
90
- recursive true
91
- end
92
-
93
- file "#{build_cookbook_dir}/test/fixtures/cookbooks/test/metadata.rb" do
94
- content %(name 'test'
95
- version '0.1.0')
96
- end
97
-
98
- cookbook_file "#{build_cookbook_dir}/test/fixtures/cookbooks/test/recipes/default.rb" do
99
- source 'build_cookbook/test-fixture-recipe.rb'
100
- end
101
-
102
- # Construct git history as if we did all the work in a feature branch which we
103
- # merged into master at the end, which looks like this:
104
- #
105
- # ```
106
- # git log --graph --oneline
107
- # * 5fec5bd Merge branch 'add-delivery-configuration'
108
- # |\
109
- # | * 967bb9f Add generated delivery build cookbook
110
- # | * 1558e0a Add generated delivery configuration
111
- # |/
112
- # * db22790 Add generated cookbook content
113
- # ```
114
- #
115
- if context.have_git && context.workflow_project_git_initialized && !context.skip_git_init
116
-
117
- generator_desc('Adding delivery configuration to feature branch')
118
-
119
- execute('git-create-feature-branch') do
120
- command('git checkout -t -b add-delivery-configuration')
121
- cwd workflow_project_dir
122
- not_if { shell_out('git branch', cwd: workflow_project_dir).stdout.match(/add-delivery-configuration/) }
123
- end
124
-
125
- execute('git-add-delivery-config-json') do
126
- command('git add .delivery/config.json')
127
- cwd workflow_project_dir
128
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(%r{.delivery/config.json}) }
129
- end
130
-
131
- # Adding the new prototype file to the feature branch
132
- # so it gets checked in with the delivery config commit
133
- execute('git-add-delivery-project-toml') do
134
- command('git add .delivery/project.toml')
135
- cwd workflow_project_dir
136
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(%r{.delivery/project.toml}) }
137
- end
138
-
139
- execute('git-commit-delivery-config') do
140
- command('git commit -m "Add generated delivery configuration"')
141
- cwd workflow_project_dir
142
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(/config\.json|project\.toml/) }
143
- end
144
-
145
- generator_desc('Adding build cookbook to feature branch')
146
-
147
- execute('git-add-delivery-build-cookbook-files') do
148
- command('git add .delivery')
149
- cwd workflow_project_dir
150
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(/\.delivery/) }
151
- end
152
-
153
- execute('git-commit-delivery-build-cookbook') do
154
- command('git commit -m "Add generated delivery build cookbook"')
155
- cwd workflow_project_dir
156
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(/\.delivery/) }
157
- end
158
-
159
- execute("git-return-to-#{pipeline}-branch") do
160
- command("git checkout #{pipeline}")
161
- cwd workflow_project_dir
162
- end
163
-
164
- generator_desc('Merging delivery content feature branch to master')
165
-
166
- execute('git-merge-delivery-config-branch') do
167
- command('git merge --no-ff add-delivery-configuration')
168
- cwd workflow_project_dir
169
- end
170
-
171
- execute('git-remove-delivery-config-branch') do
172
- command('git branch -D add-delivery-configuration')
173
- cwd workflow_project_dir
174
- end
175
- end
@@ -1,7 +0,0 @@
1
- source 'https://supermarket.chef.io'
2
-
3
- metadata
4
-
5
- group :workflow do
6
- cookbook 'test', path: './test/fixtures/cookbooks/test'
7
- end
@@ -1,10 +0,0 @@
1
- name 'build_cookbook'
2
- maintainer '<%= copyright_holder %>'
3
- maintainer_email '<%= email %>'
4
- license '<%= license %>'
5
- version '0.1.0'
6
- chef_version '>= 15.0'
7
- <% if build_cookbook_parent_is_cookbook -%>
8
-
9
- depends 'delivery-truck'
10
- <% end -%>
@@ -1,9 +0,0 @@
1
- #
2
- # Cookbook:: build_cookbook
3
- # Recipe:: <%= @phase %>
4
- #
5
- <%= license_description('#') %>
6
-
7
- <% if build_cookbook_parent_is_cookbook -%>
8
- include_recipe 'delivery-truck::<%= @phase %>'
9
- <% end -%>
@@ -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