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
@@ -0,0 +1,207 @@
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/delete_policy_group'
21
+
22
+ describe ChefDK::Command::DeletePolicyGroup 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(:chef_config_loader) { instance_double("Chef::WorkstationConfigLoader") }
31
+
32
+ let(:chef_config) { double("Chef::Config") }
33
+
34
+ # nil means the config loader will do the default path lookup
35
+ let(:config_arg) { nil }
36
+
37
+ before do
38
+ stub_const("Chef::Config", chef_config)
39
+ allow(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
40
+ end
41
+
42
+ describe "parsing args and options" do
43
+
44
+ let(:base_params) { ["example-policy-group"] }
45
+
46
+ before do
47
+ command.apply_params!(params)
48
+ end
49
+
50
+ context "when given just the policy group name" do
51
+
52
+ let(:params) { base_params }
53
+
54
+ it "sets the policy group name" do
55
+ expect(command.policy_group).to eq("example-policy-group")
56
+ end
57
+
58
+ it "configures the rm_policy_group service" do
59
+ expect(chef_config_loader).to receive(:load)
60
+ service = command.rm_policy_group_service
61
+ expect(service).to be_a(ChefDK::PolicyfileServices::RmPolicyGroup)
62
+ expect(service.chef_config).to eq(chef_config)
63
+ expect(service.ui).to eq(command.ui)
64
+ expect(service.policy_group).to eq("example-policy-group")
65
+ end
66
+ end
67
+
68
+ context "when given a path to the config" do
69
+
70
+ let(:params) { base_params + %w[ -c ~/otherstuff/config.rb ] }
71
+
72
+ let(:config_arg) { "~/otherstuff/config.rb" }
73
+
74
+ before do
75
+ expect(chef_config_loader).to receive(:load)
76
+ end
77
+
78
+ it "reads the chef/knife config" do
79
+ expect(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
80
+ expect(command.chef_config).to eq(chef_config)
81
+ expect(command.rm_policy_group_service.chef_config).to eq(chef_config)
82
+ end
83
+
84
+ end
85
+
86
+ describe "settings that require loading chef config" do
87
+
88
+ before do
89
+ allow(chef_config_loader).to receive(:load)
90
+ end
91
+
92
+ context "with no params" do
93
+
94
+ let(:params) { base_params }
95
+
96
+ it "disables debug by default" do
97
+ expect(command.debug?).to be(false)
98
+ end
99
+
100
+ end
101
+
102
+ context "when debug mode is set" do
103
+
104
+ let(:params) { base_params + [ "-D" ] }
105
+
106
+ it "enables debug" do
107
+ expect(command.debug?).to be(true)
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "running the command" do
115
+
116
+ let(:ui) { TestHelpers::TestUI.new }
117
+
118
+ before do
119
+ allow(chef_config_loader).to receive(:load)
120
+ command.ui = ui
121
+ end
122
+
123
+ context "when given too few arguments" do
124
+
125
+ let(:params) { %w[ ] }
126
+
127
+ it "shows usage and exits" do
128
+ expect(command.run(params)).to eq(1)
129
+ end
130
+
131
+ end
132
+
133
+ context "when given too many arguments" do
134
+
135
+ let(:params) { %w[ policygroup wut-is-this ] }
136
+
137
+ it "shows usage and exits" do
138
+ expect(command.run(params)).to eq(1)
139
+ end
140
+
141
+ end
142
+
143
+ context "when the rm_policy_group service raises an exception" do
144
+
145
+ let(:backtrace) { caller[0...3] }
146
+
147
+ let(:cause) do
148
+ e = StandardError.new("some operation failed")
149
+ e.set_backtrace(backtrace)
150
+ e
151
+ end
152
+
153
+ let(:exception) do
154
+ ChefDK::DeletePolicyGroupError.new("Failed to delete policy group", cause)
155
+ end
156
+
157
+ before do
158
+ allow(command.rm_policy_group_service).to receive(:run).and_raise(exception)
159
+ end
160
+
161
+ it "prints a debugging message and exits non-zero" do
162
+ expect(command.run(%w[example-policy-group])).to eq(1)
163
+
164
+ expected_output=<<-E
165
+ Error: Failed to delete policy group
166
+ Reason: (StandardError) some operation failed
167
+
168
+ E
169
+
170
+ expect(ui.output).to eq(expected_output)
171
+ end
172
+
173
+ context "when debug is enabled" do
174
+
175
+ it "includes the backtrace in the error" do
176
+ command.run(%w[ example-policy-group -D ])
177
+
178
+ expected_output=<<-E
179
+ Error: Failed to delete policy group
180
+ Reason: (StandardError) some operation failed
181
+
182
+
183
+ E
184
+ expected_output << backtrace.join("\n") << "\n"
185
+
186
+ expect(ui.output).to eq(expected_output)
187
+ end
188
+
189
+ end
190
+
191
+ end
192
+
193
+ context "when the rm policy group service executes successfully" do
194
+
195
+ before do
196
+ expect(command.rm_policy_group_service).to receive(:run)
197
+ end
198
+
199
+ it "exits 0" do
200
+ expect(command.run(%w[example-policy-group])).to eq(0)
201
+ end
202
+
203
+ end
204
+
205
+ end
206
+ end
207
+
@@ -0,0 +1,207 @@
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/delete_policy'
21
+
22
+ describe ChefDK::Command::DeletePolicy 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(:chef_config_loader) { instance_double("Chef::WorkstationConfigLoader") }
31
+
32
+ let(:chef_config) { double("Chef::Config") }
33
+
34
+ # nil means the config loader will do the default path lookup
35
+ let(:config_arg) { nil }
36
+
37
+ before do
38
+ stub_const("Chef::Config", chef_config)
39
+ allow(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
40
+ end
41
+
42
+ describe "parsing args and options" do
43
+
44
+ let(:base_params) { ["example-policy"] }
45
+
46
+ before do
47
+ command.apply_params!(params)
48
+ end
49
+
50
+ context "when given just the policy name" do
51
+
52
+ let(:params) { base_params }
53
+
54
+ it "sets the policy name" do
55
+ expect(command.policy_name).to eq("example-policy")
56
+ end
57
+
58
+ it "configures the rm_policy service" do
59
+ expect(chef_config_loader).to receive(:load)
60
+ service = command.rm_policy_service
61
+ expect(service).to be_a(ChefDK::PolicyfileServices::RmPolicy)
62
+ expect(service.chef_config).to eq(chef_config)
63
+ expect(service.ui).to eq(command.ui)
64
+ expect(service.policy_name).to eq("example-policy")
65
+ end
66
+ end
67
+
68
+ context "when given a path to the config" do
69
+
70
+ let(:params) { base_params + %w[ -c ~/otherstuff/config.rb ] }
71
+
72
+ let(:config_arg) { "~/otherstuff/config.rb" }
73
+
74
+ before do
75
+ expect(chef_config_loader).to receive(:load)
76
+ end
77
+
78
+ it "reads the chef/knife config" do
79
+ expect(Chef::WorkstationConfigLoader).to receive(:new).with(config_arg).and_return(chef_config_loader)
80
+ expect(command.chef_config).to eq(chef_config)
81
+ expect(command.rm_policy_service.chef_config).to eq(chef_config)
82
+ end
83
+
84
+ end
85
+
86
+ describe "settings that require loading chef config" do
87
+
88
+ before do
89
+ allow(chef_config_loader).to receive(:load)
90
+ end
91
+
92
+ context "with no params" do
93
+
94
+ let(:params) { base_params }
95
+
96
+ it "disables debug by default" do
97
+ expect(command.debug?).to be(false)
98
+ end
99
+
100
+ end
101
+
102
+ context "when debug mode is set" do
103
+
104
+ let(:params) { base_params + [ "-D" ] }
105
+
106
+ it "enables debug" do
107
+ expect(command.debug?).to be(true)
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "running the command" do
115
+
116
+ let(:ui) { TestHelpers::TestUI.new }
117
+
118
+ before do
119
+ allow(chef_config_loader).to receive(:load)
120
+ command.ui = ui
121
+ end
122
+
123
+ context "when given too few arguments" do
124
+
125
+ let(:params) { %w[ ] }
126
+
127
+ it "shows usage and exits" do
128
+ expect(command.run(params)).to eq(1)
129
+ end
130
+
131
+ end
132
+
133
+ context "when given too many arguments" do
134
+
135
+ let(:params) { %w[ a-policy-name wut-is-this ] }
136
+
137
+ it "shows usage and exits" do
138
+ expect(command.run(params)).to eq(1)
139
+ end
140
+
141
+ end
142
+
143
+ context "when the rm_policy service raises an exception" do
144
+
145
+ let(:backtrace) { caller[0...3] }
146
+
147
+ let(:cause) do
148
+ e = StandardError.new("some operation failed")
149
+ e.set_backtrace(backtrace)
150
+ e
151
+ end
152
+
153
+ let(:exception) do
154
+ ChefDK::DeletePolicyError.new("Failed to delete policy.", cause)
155
+ end
156
+
157
+ before do
158
+ allow(command.rm_policy_service).to receive(:run).and_raise(exception)
159
+ end
160
+
161
+ it "prints a debugging message and exits non-zero" do
162
+ expect(command.run(%w[example-policy])).to eq(1)
163
+
164
+ expected_output=<<-E
165
+ Error: Failed to delete policy.
166
+ Reason: (StandardError) some operation failed
167
+
168
+ E
169
+
170
+ expect(ui.output).to eq(expected_output)
171
+ end
172
+
173
+ context "when debug is enabled" do
174
+
175
+ it "includes the backtrace in the error" do
176
+ command.run(%w[ example-policy -D ])
177
+
178
+ expected_output=<<-E
179
+ Error: Failed to delete policy.
180
+ Reason: (StandardError) some operation failed
181
+
182
+
183
+ E
184
+ expected_output << backtrace.join("\n") << "\n"
185
+
186
+ expect(ui.output).to eq(expected_output)
187
+ end
188
+
189
+ end
190
+
191
+ end
192
+
193
+ context "when the rm_policy service executes successfully" do
194
+
195
+ before do
196
+ expect(command.rm_policy_service).to receive(:run)
197
+ end
198
+
199
+ it "exits 0" do
200
+ expect(command.run(%w[example-policy])).to eq(0)
201
+ end
202
+
203
+ end
204
+
205
+ end
206
+ end
207
+
@@ -19,13 +19,28 @@ require 'spec_helper'
19
19
  require "stringio"
20
20
  require 'chef-dk/command/generate'
21
21
 
22
- class ChefDK::Command::GeneratorCommands::Example
22
+ class ChefDK::Command::GeneratorCommands::Example < ChefDK::Command::GeneratorCommands::Base
23
+
24
+ option :one,
25
+ long: "--option-one",
26
+ description: "one"
27
+
28
+ option :two,
29
+ long: "--option-two",
30
+ description: "two"
31
+
32
+ option :arg,
33
+ short: "-a ARG",
34
+ long: "--arg ARG",
35
+ description: "an option that takes an argument"
23
36
 
24
37
  def initialize(argv)
38
+ super # required by mixlib-cli
25
39
  @argv = argv
26
40
  end
27
41
 
28
42
  def run
43
+ parse_options(@argv)
29
44
  {:argv => @argv, :ran_cmd => "example"}
30
45
  end
31
46
  end
@@ -42,6 +57,10 @@ describe ChefDK::Command::Generate do
42
57
  stdout_io.string
43
58
  end
44
59
 
60
+ def stderr
61
+ stderr_io.string
62
+ end
63
+
45
64
  subject(:generate) do
46
65
  g = generator_class.new
47
66
  allow(g).to receive(:stdout).and_return(stdout_io)
@@ -98,5 +117,26 @@ E
98
117
  result = generate.run(%w[example argument_one argument_two --option-one --option-two])
99
118
  expect(result[:argv]).to eq(%w[argument_one argument_two --option-one --option-two])
100
119
  end
120
+
121
+ describe "when an invalid option is passed to the subcommand" do
122
+
123
+ it "prints usage and returns non-zero" do
124
+ result = generate.run(%w[example --nope])
125
+ expect(result).to eq(1)
126
+ expect(stderr).to eq("ERROR: invalid option: --nope\n\n")
127
+ end
128
+
129
+ end
130
+
131
+ describe "when an option requires an argument but none is given" do
132
+
133
+ it "prints usage and returns non-zero" do
134
+ result = generate.run(%w[example --arg])
135
+ expect(result).to eq(1)
136
+ expect(stderr).to eq("ERROR: missing argument: --arg\n\n")
137
+ end
138
+
139
+ end
140
+
101
141
  end
102
142
  end