chef-dk 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/chef-dk/builtin_commands.rb +10 -0
  4. data/lib/chef-dk/command/base.rb +2 -2
  5. data/lib/chef-dk/command/clean_policy_cookbooks.rb +116 -0
  6. data/lib/chef-dk/command/clean_policy_revisions.rb +113 -0
  7. data/lib/chef-dk/command/delete_policy.rb +122 -0
  8. data/lib/chef-dk/command/delete_policy_group.rb +122 -0
  9. data/lib/chef-dk/command/export.rb +3 -3
  10. data/lib/chef-dk/command/generate.rb +8 -0
  11. data/lib/chef-dk/command/generator_commands/app.rb +1 -1
  12. data/lib/chef-dk/command/generator_commands/cookbook.rb +1 -1
  13. data/lib/chef-dk/command/generator_commands/policyfile.rb +1 -1
  14. data/lib/chef-dk/command/generator_commands/repo.rb +1 -1
  15. data/lib/chef-dk/command/install.rb +22 -5
  16. data/lib/chef-dk/command/provision.rb +0 -4
  17. data/lib/chef-dk/command/push.rb +1 -2
  18. data/lib/chef-dk/command/shell_init.rb +65 -6
  19. data/lib/chef-dk/command/show_policy.rb +1 -2
  20. data/lib/chef-dk/command/undelete.rb +155 -0
  21. data/lib/chef-dk/command/update.rb +5 -5
  22. data/lib/chef-dk/command/verify.rb +61 -17
  23. data/lib/chef-dk/completions/bash.sh.erb +5 -0
  24. data/lib/chef-dk/completions/chef.fish.erb +10 -0
  25. data/lib/chef-dk/completions/zsh.zsh.erb +21 -0
  26. data/lib/chef-dk/exceptions.rb +12 -0
  27. data/lib/chef-dk/helpers.rb +17 -0
  28. data/lib/chef-dk/policyfile/community_cookbook_source.rb +0 -3
  29. data/lib/chef-dk/policyfile/lister.rb +3 -1
  30. data/lib/chef-dk/policyfile/undo_record.rb +142 -0
  31. data/lib/chef-dk/policyfile/undo_stack.rb +130 -0
  32. data/lib/chef-dk/policyfile_lock.rb +30 -0
  33. data/lib/chef-dk/policyfile_services/clean_policies.rb +5 -4
  34. data/lib/chef-dk/policyfile_services/clean_policy_cookbooks.rb +125 -0
  35. data/lib/chef-dk/policyfile_services/rm_policy.rb +142 -0
  36. data/lib/chef-dk/policyfile_services/rm_policy_group.rb +86 -0
  37. data/lib/chef-dk/policyfile_services/show_policy.rb +1 -1
  38. data/lib/chef-dk/policyfile_services/undelete.rb +108 -0
  39. data/lib/chef-dk/service_exceptions.rb +11 -0
  40. data/lib/chef-dk/skeletons/code_generator/files/default/chefignore +6 -2
  41. data/lib/chef-dk/skeletons/code_generator/files/default/repo/README.md +1 -1
  42. data/lib/chef-dk/skeletons/code_generator/files/default/repo/cookbooks/example/attributes/default.rb +1 -1
  43. data/lib/chef-dk/skeletons/code_generator/files/default/repo/cookbooks/example/recipes/default.rb +1 -1
  44. data/lib/chef-dk/version.rb +1 -1
  45. data/lib/kitchen/provisioner/policyfile_zero.rb +4 -1
  46. data/spec/unit/command/base_spec.rb +26 -1
  47. data/spec/unit/command/clean_policy_cookbooks_spec.rb +181 -0
  48. data/spec/unit/command/clean_policy_revisions_spec.rb +181 -0
  49. data/spec/unit/command/delete_policy_group_spec.rb +207 -0
  50. data/spec/unit/command/delete_policy_spec.rb +207 -0
  51. data/spec/unit/command/generate_spec.rb +41 -1
  52. data/spec/unit/command/generator_commands/cookbook_spec.rb +1 -1
  53. data/spec/unit/command/generator_commands/policyfile_spec.rb +1 -1
  54. data/spec/unit/command/install_spec.rb +24 -0
  55. data/spec/unit/command/shell_init_spec.rb +176 -5
  56. data/spec/unit/command/undelete_spec.rb +246 -0
  57. data/spec/unit/helpers_spec.rb +24 -0
  58. data/spec/unit/policyfile/lister_spec.rb +16 -0
  59. data/spec/unit/policyfile/undo_record_spec.rb +260 -0
  60. data/spec/unit/policyfile/undo_stack_spec.rb +266 -0
  61. data/spec/unit/policyfile_lock_serialization_spec.rb +41 -0
  62. data/spec/unit/policyfile_services/clean_policy_cookbooks_spec.rb +275 -0
  63. data/spec/unit/policyfile_services/rm_policy_group_spec.rb +241 -0
  64. data/spec/unit/policyfile_services/rm_policy_spec.rb +266 -0
  65. data/spec/unit/policyfile_services/show_policy_spec.rb +52 -2
  66. data/spec/unit/policyfile_services/undelete_spec.rb +304 -0
  67. metadata +43 -91
@@ -118,6 +118,17 @@ module ChefDK
118
118
  class PolicyfileCleanError < PolicyfileNestedException
119
119
  end
120
120
 
121
+ class DeletePolicyGroupError < PolicyfileNestedException
122
+ end
123
+
124
+ class DeletePolicyError < PolicyfileNestedException
125
+ end
126
+ class PolicyCookbookCleanError < PolicyfileNestedException
127
+ end
128
+
129
+ class UndeleteError < PolicyfileNestedException
130
+ end
131
+
121
132
  class ChefRunnerError < StandardError
122
133
 
123
134
  include NestedExceptionWithInspector
@@ -51,9 +51,11 @@ spec/*
51
51
  spec/fixtures/*
52
52
  test/*
53
53
  features/*
54
+ examples/*
54
55
  Guardfile
55
56
  Procfile
56
- .kitchen
57
+ .kitchen*
58
+ .rubocop.yml
57
59
 
58
60
  # SCM #
59
61
  #######
@@ -77,7 +79,9 @@ tmp
77
79
 
78
80
  # Cookbooks #
79
81
  #############
80
- CONTRIBUTING
82
+ CONTRIBUTING*
83
+ CHANGELOG*
84
+ TESTING*
81
85
 
82
86
  # Strainer #
83
87
  ############
@@ -20,7 +20,7 @@ Configuration
20
20
 
21
21
  The config file, `.chef/knife.rb` is a repository specific configuration file for knife. If you're using the Chef Platform, you can download one for your organization from the management console. If you're using the Open Source Chef Server, you can generate a new one with `knife configure`. For more information about configuring Knife, see the Knife documentation.
22
22
 
23
- http://docs.chef.io/knife.html
23
+ https://docs.chef.io/knife.html
24
24
 
25
25
  Next Steps
26
26
  ==========
@@ -4,4 +4,4 @@
4
4
  # Set a default name
5
5
  default["example"]["name"] = "Sam Doe"
6
6
 
7
- # For further information, see the Chef documentation (http://docs.getchef.com/essentials_cookbook_attribute_files.html).
7
+ # For further information, see the Chef documentation (https://docs.getchef.com/essentials_cookbook_attribute_files.html).
@@ -5,4 +5,4 @@ log "Welcome to Chef, #{node["example"]["name"]}!" do
5
5
  level :info
6
6
  end
7
7
 
8
- # For more information, see the documentation: http://docs.getchef.com/essentials_cookbook_recipes.html
8
+ # For more information, see the documentation: https://docs.getchef.com/essentials_cookbook_recipes.html
@@ -16,5 +16,5 @@
16
16
  #
17
17
 
18
18
  module ChefDK
19
- VERSION = "0.7.0"
19
+ VERSION = "0.8.0"
20
20
  end
@@ -41,6 +41,7 @@ module Kitchen
41
41
  default_config :json_attributes, true
42
42
  default_config :chef_zero_host, nil
43
43
  default_config :chef_zero_port, 8889
44
+ default_config :policyfile, "Policyfile.rb"
44
45
 
45
46
  default_config :chef_client_path do |provisioner|
46
47
  provisioner.
@@ -122,7 +123,9 @@ module Kitchen
122
123
  def policy_exporter
123
124
  # Must force this because TK by default copies the current cookbook to the sandbox
124
125
  # See ChefDK::PolicyfileServices::ExportRepo#assert_export_dir_clean!
125
- @policy_exporter ||= ChefDK::PolicyfileServices::ExportRepo.new(export_dir: sandbox_path, force: true)
126
+ @policy_exporter ||= ChefDK::PolicyfileServices::ExportRepo.new(policyfile: config[:policyfile],
127
+ export_dir: sandbox_path,
128
+ force: true)
126
129
  end
127
130
 
128
131
  # Writes a fake (but valid) validation.pem into the sandbox directory.
@@ -22,6 +22,11 @@ describe ChefDK::Command::Base do
22
22
  class TestCommand < ChefDK::Command::Base
23
23
  banner "use me please"
24
24
 
25
+ option :argue,
26
+ short: "-a ARG",
27
+ long: "--arg ARG",
28
+ description: "An option with a required argument"
29
+
25
30
  option :user,
26
31
  :short => "-u",
27
32
  :long => "--user",
@@ -96,14 +101,34 @@ describe ChefDK::Command::Base do
96
101
  it "prints the help banner and exits gracefully" do
97
102
  expect(run_command(%w[-foo])).to eq(1)
98
103
 
99
- expect(stderr).to eq("invalid option: -foo\n")
104
+ expect(stderr).to eq("ERROR: invalid option: -foo\n\n")
105
+
106
+ expected = <<-E
107
+ use me please
108
+ -a, --arg ARG An option with a required argument
109
+ -u, --user If the user exists
110
+
111
+ E
112
+ expect(stdout).to eq(expected)
113
+ end
114
+
115
+ end
116
+
117
+ describe "when given an option that requires an argument with no argument" do
118
+
119
+ it "prints the help banner and exits gracefully" do
120
+ expect(run_command(%w[-a])).to eq(1)
121
+
122
+ expect(stderr).to eq("ERROR: missing argument: -a\n\n")
100
123
 
101
124
  expected = <<-E
102
125
  use me please
126
+ -a, --arg ARG An option with a required argument
103
127
  -u, --user If the user exists
104
128
 
105
129
  E
106
130
  expect(stdout).to eq(expected)
131
+
107
132
  end
108
133
 
109
134
  end
@@ -0,0 +1,181 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2015 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/command_with_ui_object'
20
+ require 'chef-dk/command/clean_policy_cookbooks'
21
+
22
+ describe ChefDK::Command::CleanPolicyCookbooks do
23
+
24
+ it_behaves_like "a command with a UI object"
25
+
26
+ subject(:command) do
27
+ described_class.new
28
+ end
29
+
30
+ let(:clean_policy_cookbooks_service) { command.clean_policy_cookbooks_service }
31
+
32
+ let(:chef_config_loader) { instance_double("Chef::WorkstationConfigLoader") }
33
+
34
+ let(:chef_config) { double("Chef::Config") }
35
+
36
+ # nil means the config loader will do the default path lookup
37
+ let(:config_arg) { nil }
38
+
39
+ before do
40
+ stub_const("Chef::Config", chef_config)
41
+ allow(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
42
+ end
43
+
44
+ describe "parsing args and options" do
45
+ let(:params) { [] }
46
+
47
+ before do
48
+ command.apply_params!(params)
49
+ end
50
+
51
+ context "when given a path to the config" do
52
+
53
+ let(:params) { %w[ -c ~/otherstuff/config.rb ] }
54
+
55
+ let(:config_arg) { "~/otherstuff/config.rb" }
56
+
57
+ before do
58
+ expect(chef_config_loader).to receive(:load)
59
+ end
60
+
61
+ it "reads the chef/knife config" do
62
+ expect(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
63
+ expect(command.chef_config).to eq(chef_config)
64
+ expect(clean_policy_cookbooks_service.chef_config).to eq(chef_config)
65
+ end
66
+
67
+ end
68
+
69
+ describe "settings that require loading chef config" do
70
+
71
+ before do
72
+ allow(chef_config_loader).to receive(:load)
73
+ end
74
+
75
+ context "with no params" do
76
+
77
+ it "disables debug by default" do
78
+ expect(command.debug?).to be(false)
79
+ end
80
+
81
+ end
82
+
83
+ context "when debug mode is set" do
84
+
85
+ let(:params) { [ "-D" ] }
86
+
87
+ it "enables debug" do
88
+ expect(command.debug?).to be(true)
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ describe "running the command" do
98
+
99
+ let(:ui) { TestHelpers::TestUI.new }
100
+
101
+ before do
102
+ allow(chef_config_loader).to receive(:load)
103
+ command.ui = ui
104
+ end
105
+
106
+ context "when given too many arguments" do
107
+
108
+ let(:params) { %w[ wut-is-this ] }
109
+
110
+ it "shows usage and exits" do
111
+ expect(command.run(params)).to eq(1)
112
+ end
113
+
114
+ end
115
+
116
+ context "when the clean policies service raises an exception" do
117
+
118
+ let(:backtrace) { caller[0...3] }
119
+
120
+ let(:cause) do
121
+ e = StandardError.new("some operation failed")
122
+ e.set_backtrace(backtrace)
123
+ e
124
+ end
125
+
126
+ let(:exception) do
127
+ ChefDK::PolicyfileCleanError.new("Failed to delete some policy revisions.", cause)
128
+ end
129
+
130
+ before do
131
+ allow(clean_policy_cookbooks_service).to receive(:run).and_raise(exception)
132
+ end
133
+
134
+ it "prints a debugging message and exits non-zero" do
135
+ expect(command.run([])).to eq(1)
136
+
137
+ expected_output=<<-E
138
+ Error: Failed to delete some policy revisions.
139
+ Reason: (StandardError) some operation failed
140
+
141
+ E
142
+
143
+ expect(ui.output).to eq(expected_output)
144
+ end
145
+
146
+ context "when debug is enabled" do
147
+
148
+ it "includes the backtrace in the error" do
149
+
150
+ command.run(%w[ -D ])
151
+
152
+ expected_output=<<-E
153
+ Error: Failed to delete some policy revisions.
154
+ Reason: (StandardError) some operation failed
155
+
156
+
157
+ E
158
+ expected_output << backtrace.join("\n") << "\n"
159
+
160
+ expect(ui.output).to eq(expected_output)
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+
167
+ context "when the clean policies service executes successfully" do
168
+
169
+ before do
170
+ expect(clean_policy_cookbooks_service).to receive(:run)
171
+ end
172
+
173
+ it "exits 0" do
174
+ expect(command.run([])).to eq(0)
175
+ end
176
+
177
+ end
178
+
179
+ end
180
+ end
181
+
@@ -0,0 +1,181 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2015 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/command_with_ui_object'
20
+ require 'chef-dk/command/clean_policy_revisions'
21
+
22
+ describe ChefDK::Command::CleanPolicyRevisions do
23
+
24
+ it_behaves_like "a command with a UI object"
25
+
26
+ subject(:command) do
27
+ described_class.new
28
+ end
29
+
30
+ let(:clean_policies_service) { command.clean_policies_service }
31
+
32
+ let(:chef_config_loader) { instance_double("Chef::WorkstationConfigLoader") }
33
+
34
+ let(:chef_config) { double("Chef::Config") }
35
+
36
+ # nil means the config loader will do the default path lookup
37
+ let(:config_arg) { nil }
38
+
39
+ before do
40
+ stub_const("Chef::Config", chef_config)
41
+ allow(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
42
+ end
43
+
44
+ describe "parsing args and options" do
45
+ let(:params) { [] }
46
+
47
+ before do
48
+ command.apply_params!(params)
49
+ end
50
+
51
+ context "when given a path to the config" do
52
+
53
+ let(:params) { %w[ -c ~/otherstuff/config.rb ] }
54
+
55
+ let(:config_arg) { "~/otherstuff/config.rb" }
56
+
57
+ before do
58
+ expect(chef_config_loader).to receive(:load)
59
+ end
60
+
61
+ it "reads the chef/knife config" do
62
+ expect(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
63
+ expect(command.chef_config).to eq(chef_config)
64
+ expect(clean_policies_service.chef_config).to eq(chef_config)
65
+ end
66
+
67
+ end
68
+
69
+ describe "settings that require loading chef config" do
70
+
71
+ before do
72
+ allow(chef_config_loader).to receive(:load)
73
+ end
74
+
75
+ context "with no params" do
76
+
77
+ it "disables debug by default" do
78
+ expect(command.debug?).to be(false)
79
+ end
80
+
81
+ end
82
+
83
+ context "when debug mode is set" do
84
+
85
+ let(:params) { [ "-D" ] }
86
+
87
+ it "enables debug" do
88
+ expect(command.debug?).to be(true)
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ describe "running the command" do
98
+
99
+ let(:ui) { TestHelpers::TestUI.new }
100
+
101
+ before do
102
+ allow(chef_config_loader).to receive(:load)
103
+ command.ui = ui
104
+ end
105
+
106
+ context "when given too many arguments" do
107
+
108
+ let(:params) { %w[ wut-is-this ] }
109
+
110
+ it "shows usage and exits" do
111
+ expect(command.run(params)).to eq(1)
112
+ end
113
+
114
+ end
115
+
116
+ context "when the clean policies service raises an exception" do
117
+
118
+ let(:backtrace) { caller[0...3] }
119
+
120
+ let(:cause) do
121
+ e = StandardError.new("some operation failed")
122
+ e.set_backtrace(backtrace)
123
+ e
124
+ end
125
+
126
+ let(:exception) do
127
+ ChefDK::PolicyfileCleanError.new("Failed to delete some policy revisions.", cause)
128
+ end
129
+
130
+ before do
131
+ allow(clean_policies_service).to receive(:run).and_raise(exception)
132
+ end
133
+
134
+ it "prints a debugging message and exits non-zero" do
135
+ expect(command.run([])).to eq(1)
136
+
137
+ expected_output=<<-E
138
+ Error: Failed to delete some policy revisions.
139
+ Reason: (StandardError) some operation failed
140
+
141
+ E
142
+
143
+ expect(ui.output).to eq(expected_output)
144
+ end
145
+
146
+ context "when debug is enabled" do
147
+
148
+ it "includes the backtrace in the error" do
149
+
150
+ command.run(%w[ -D ])
151
+
152
+ expected_output=<<-E
153
+ Error: Failed to delete some policy revisions.
154
+ Reason: (StandardError) some operation failed
155
+
156
+
157
+ E
158
+ expected_output << backtrace.join("\n") << "\n"
159
+
160
+ expect(ui.output).to eq(expected_output)
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+
167
+ context "when the clean policies service executes successfully" do
168
+
169
+ before do
170
+ expect(clean_policies_service).to receive(:run)
171
+ end
172
+
173
+ it "exits 0" do
174
+ expect(command.run([])).to eq(0)
175
+ end
176
+
177
+ end
178
+
179
+ end
180
+ end
181
+