hammer_cli_foreman 0.0.10 → 0.0.11

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.

Potentially problematic release.


This version of hammer_cli_foreman might be problematic. Click here for more details.

Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -0
  3. data/doc/host_create.md +190 -0
  4. data/lib/hammer_cli_foreman.rb +1 -0
  5. data/lib/hammer_cli_foreman/commands.rb +21 -7
  6. data/lib/hammer_cli_foreman/common_parameter.rb +12 -18
  7. data/lib/hammer_cli_foreman/domain.rb +8 -6
  8. data/lib/hammer_cli_foreman/environment.rb +10 -0
  9. data/lib/hammer_cli_foreman/exception_handler.rb +11 -1
  10. data/lib/hammer_cli_foreman/exceptions.rb +9 -0
  11. data/lib/hammer_cli_foreman/host.rb +107 -16
  12. data/lib/hammer_cli_foreman/hostgroup.rb +10 -0
  13. data/lib/hammer_cli_foreman/puppet_class.rb +10 -1
  14. data/lib/hammer_cli_foreman/resource_supported_test.rb +3 -3
  15. data/lib/hammer_cli_foreman/smart_class_parameter.rb +110 -0
  16. data/lib/hammer_cli_foreman/version.rb +1 -1
  17. data/test/unit/apipie_resource_mock.rb +88 -0
  18. data/test/unit/architecture_test.rb +91 -0
  19. data/test/unit/common_parameter_test.rb +77 -0
  20. data/test/unit/compute_resource_test.rb +94 -0
  21. data/test/unit/domain_test.rb +130 -0
  22. data/test/unit/environment_test.rb +110 -0
  23. data/test/unit/exception_handler_test.rb +48 -0
  24. data/test/unit/fact_test.rb +31 -0
  25. data/test/unit/helpers/command.rb +69 -0
  26. data/test/unit/helpers/resource_disabled.rb +24 -0
  27. data/test/unit/host_test.rb +315 -0
  28. data/test/unit/hostgroup_test.rb +145 -0
  29. data/test/unit/image_test.rb +136 -0
  30. data/test/unit/location_test.rb +109 -0
  31. data/test/unit/media_test.rb +106 -0
  32. data/test/unit/model_test.rb +98 -0
  33. data/test/unit/operating_system_test.rb +151 -0
  34. data/test/unit/organization_test.rb +109 -0
  35. data/test/unit/output/formatters_test.rb +19 -0
  36. data/test/unit/partition_table_test.rb +143 -0
  37. data/test/unit/puppet_class_test.rb +71 -0
  38. data/test/unit/report_test.rb +64 -0
  39. data/test/unit/smart_class_parameter_test.rb +77 -0
  40. data/test/unit/smart_proxy_test.rb +102 -0
  41. data/test/unit/subnet_test.rb +95 -0
  42. data/test/unit/template_test.rb +141 -0
  43. data/test/unit/test_helper.rb +27 -0
  44. data/test/unit/test_output_adapter.rb +19 -0
  45. data/test/unit/user_test.rb +92 -0
  46. metadata +107 -29
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require 'hammer_cli_foreman/exception_handler'
3
+
4
+ describe HammerCLIForeman::ExceptionHandler do
5
+
6
+ let(:output) { HammerCLI::Output::Output.new }
7
+ let(:handler) { HammerCLIForeman::ExceptionHandler.new(:output => output) }
8
+ let(:heading) { "Something went wrong" }
9
+
10
+ it "should print resource errors on unprocessable entity exception" do
11
+ response = <<-RESPONSE
12
+ {"subnet":{"id":null,"errors":{"network":["can't be blank","is invalid"],"name":["can't be blank"]},"full_messages":["Network address can't be blank","Network address is invalid","Name can't be blank"]}}
13
+ RESPONSE
14
+
15
+ ex = RestClient::UnprocessableEntity.new(response)
16
+ output.expects(:print_error).with(heading, "Network address can't be blank\nNetwork address is invalid\nName can't be blank")
17
+ err_code = handler.handle_exception(ex, :heading => heading)
18
+ err_code.must_equal HammerCLI::EX_DATAERR
19
+ end
20
+
21
+ it "should handle argument error" do
22
+ ex = ArgumentError.new
23
+ output.expects(:print_error).with(heading, ex.message)
24
+ err_code = handler.handle_exception(ex, :heading => heading)
25
+ err_code.must_equal HammerCLI::EX_USAGE
26
+ end
27
+
28
+ it "should handle forbidden error" do
29
+ ex = RestClient::Forbidden.new
30
+ output.expects(:print_error).with('Forbidden - server refused to process the request', nil)
31
+ err_code = handler.handle_exception(ex)
32
+ err_code.must_equal HammerCLI::EX_NOPERM
33
+ end
34
+
35
+ it "should handle unknown exception" do
36
+ output.expects(:print_error).with(heading, "Error: message")
37
+ MyException = Class.new(Exception)
38
+ err_code = handler.handle_exception(MyException.new('message'), :heading => heading)
39
+ err_code.must_equal HammerCLI::EX_SOFTWARE
40
+ end
41
+
42
+ it "should handle unsupported operation error" do
43
+ output.expects(:print_error).with(heading, "message")
44
+ err_code = handler.handle_exception(HammerCLIForeman::OperationNotSupportedError.new('message'), :heading => heading)
45
+ err_code.must_equal HammerCLI::EX_UNAVAILABLE
46
+ end
47
+ end
48
+
@@ -0,0 +1,31 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require File.join(File.dirname(__FILE__), 'apipie_resource_mock')
3
+
4
+
5
+ describe HammerCLIForeman::Fact do
6
+
7
+ extend CommandTestHelper
8
+
9
+ before :each do
10
+ cmd.class.resource ApipieResourceMock.new(cmd.class.resource.resource_class)
11
+ end
12
+
13
+ context "ListCommand" do
14
+
15
+ let(:cmd) { HammerCLIForeman::Fact::ListCommand.new("", ctx) }
16
+
17
+ context "parameters" do
18
+ it_should_accept "no arguments"
19
+ it_should_accept_search_params
20
+ end
21
+
22
+ context "output" do
23
+ it_should_print_n_records 2
24
+ it_should_print_column "Host"
25
+ it_should_print_column "Fact"
26
+ it_should_print_column "Value"
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__), '../test_output_adapter')
2
+
3
+
4
+ module CommandTestHelper
5
+
6
+ def with_params(params, &block)
7
+ context "with params "+params.to_s do
8
+ let(:with_params) { params }
9
+ self.instance_eval &block
10
+ end
11
+ end
12
+
13
+ def it_should_call_action(action, params)
14
+ it "should call action "+action.to_s do
15
+ arguments ||= respond_to?(:with_params) ? with_params : []
16
+ cmd.resource.resource_class.expects_with(action, params)
17
+ cmd.run(arguments)
18
+ end
19
+ end
20
+
21
+ def it_should_fail_with(message, arguments=[])
22
+ it "should fail with " + message.to_s do
23
+ proc { cmd.run(arguments) }.must_raise Clamp::UsageError
24
+ end
25
+ end
26
+
27
+ def it_should_accept(message, arguments=[])
28
+ it "should accept " + message.to_s do
29
+ cmd.run(arguments).must_equal HammerCLI::EX_OK
30
+ end
31
+ end
32
+
33
+ def it_should_print_column(column_name, arguments=nil)
34
+ it "should print column " + column_name do
35
+ arguments ||= respond_to?(:with_params) ? with_params : []
36
+
37
+ cmd.stubs(:context).returns({ :adapter => :test })
38
+ proc { cmd.run(arguments) }.must_output /.*##{column_name}#.*/
39
+ end
40
+ end
41
+
42
+ def it_should_print_columns(column_names, arguments=nil)
43
+ column_names.each do |name|
44
+ it_should_print_column name, arguments
45
+ end
46
+ end
47
+
48
+ def it_should_print_n_records(count=nil, arguments=nil)
49
+ it "should print correct count of records" do
50
+ arguments ||= respond_to?(:with_params) ? with_params : []
51
+
52
+ cmd.stubs(:context).returns({ :adapter => :test })
53
+ count ||= expected_record_count rescue 0
54
+ out, err = capture_io do
55
+ cmd.run(arguments)
56
+ end
57
+
58
+ out.split(/\n/).length.must_equal count+1 # plus 1 for line with column headers
59
+ end
60
+ end
61
+
62
+ def it_should_accept_search_params
63
+ it_should_accept "search", ["--search=some_search"]
64
+ it_should_accept "per page", ["--per-page=1"]
65
+ it_should_accept "page", ["--page=2"]
66
+ it_should_accept "order", ["--order=order"]
67
+ end
68
+
69
+ end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), '../apipie_resource_mock')
2
+
3
+ module ResourceDisabled
4
+
5
+ def it_should_fail_when_disabled
6
+ arguments = @with_params ? @with_params.dup : []
7
+ context "resource disabled" do
8
+
9
+ it "should return error" do
10
+ cmd.class.resource ApipieDisabledResourceMock.new(cmd.class.resource.resource_class)
11
+ arguments = respond_to?(:with_params) ? with_params : []
12
+ cmd.run(arguments).must_equal HammerCLI::EX_UNAVAILABLE
13
+ end
14
+
15
+ it "should print error message" do
16
+ cmd.class.resource ApipieDisabledResourceMock.new(cmd.class.resource.resource_class)
17
+ cmd.stubs(:context).returns({ :adapter => :test })
18
+
19
+ arguments = respond_to?(:with_params) ? with_params : []
20
+ lambda { cmd.run(arguments) }.must_output "", /.*not support.*/
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,315 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require File.join(File.dirname(__FILE__), 'apipie_resource_mock')
3
+
4
+
5
+ describe HammerCLIForeman::Host do
6
+
7
+ extend CommandTestHelper
8
+
9
+ before :each do
10
+ cmd.class.resource ApipieResourceMock.new(cmd.class.resource.resource_class)
11
+ end
12
+
13
+ context "ListCommand" do
14
+
15
+ let(:cmd) { HammerCLIForeman::Host::ListCommand.new("", ctx) }
16
+
17
+ context "parameters" do
18
+ it_should_accept "no arguments"
19
+ it_should_accept_search_params
20
+ end
21
+
22
+ context "output" do
23
+ let(:expected_record_count) { cmd.resource.call(:index)[0].length }
24
+
25
+ it_should_print_n_records
26
+ it_should_print_columns ["Id", "Name", "Operating System Id", "Host Group Id", "IP", "MAC"]
27
+ end
28
+
29
+ end
30
+
31
+ context "InfoCommand" do
32
+
33
+ let(:cmd) { HammerCLIForeman::Host::InfoCommand.new("", ctx) }
34
+
35
+ before :each do
36
+ HammerCLIForeman::Parameter.stubs(:get_parameters).returns([])
37
+ end
38
+
39
+ context "parameters" do
40
+ it_should_accept "id", ["--id=1"]
41
+ it_should_accept "name", ["--name=host"]
42
+ it_should_fail_with "no arguments"
43
+ end
44
+
45
+ context "output" do
46
+ with_params ["--id=1"] do
47
+ it_should_print_n_records 1
48
+ it_should_print_columns ["Id", "Name", "Operating System Id", "Host Group Id", "IP", "MAC"]
49
+
50
+ it_should_print_columns ["UUID", "Cert name"]
51
+ it_should_print_columns ["Environment", "Environment Id"]
52
+ it_should_print_columns ["Managed", "Enabled", "Build"]
53
+ it_should_print_columns ["Use image", "Disk", "Image file"]
54
+ it_should_print_columns ["SP Name", "SP IP", "SP MAC", "SP Subnet", "SP Subnet Id"]
55
+ it_should_print_columns ["Created at", "Updated at", "Installed at", "Last report"]
56
+ it_should_print_columns ["Puppet CA Proxy Id", "Medium Id", "Model Id", "Owner Id", "Subnet Id", "Domain Id"]
57
+ it_should_print_columns ["Puppet Proxy Id", "Owner Type", "Partition Table Id", "Architecture Id", "Image Id", "Compute Resource Id"]
58
+ it_should_print_columns ["Comment"]
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ context "StatusCommand" do
65
+
66
+ let(:cmd) { HammerCLIForeman::Host::StatusCommand.new("", ctx) }
67
+
68
+ before :each do
69
+ resource_mock = ApipieResourceMock.new(cmd.class.resource.resource_class)
70
+ resource_mock.stub_method(:power, {'power' => 'running'})
71
+ cmd.class.resource resource_mock
72
+ end
73
+
74
+ context "parameters" do
75
+ it_should_accept "name", ["--name=host"]
76
+ it_should_accept "id", ["--id=1"]
77
+ it_should_fail_with "no arguments"
78
+ end
79
+
80
+ context "output" do
81
+ with_params ["--id=1"] do
82
+ it_should_print_columns ["Status", "Power"]
83
+
84
+ it "should output status" do
85
+ cmd.stubs(:context).returns({ :adapter => :test })
86
+ proc { cmd.run(with_params) }.must_output "#Status#Power#\n#missing#running#\n"
87
+ end
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+ context "FactsCommand" do
94
+
95
+ let(:cmd) { HammerCLIForeman::Host::FactsCommand.new("", ctx) }
96
+
97
+ context "parameters" do
98
+ it_should_accept "name", ["--name=host"]
99
+ it_should_accept "id", ["--id=1"]
100
+ it_should_fail_with "no arguments"
101
+ end
102
+
103
+ context "output" do
104
+ with_params ["--name=my5name.mydomain.net"] do
105
+ it_should_print_n_records 2
106
+ it_should_print_column "Fact"
107
+ it_should_print_column "Value"
108
+ end
109
+ end
110
+ end
111
+
112
+
113
+ context "PuppetClassesCommand" do
114
+
115
+ let(:cmd) { HammerCLIForeman::Host::PuppetClassesCommand.new("", ctx) }
116
+
117
+ context "parameters" do
118
+ it_should_accept "name", ["--name=host"]
119
+ it_should_accept "id", ["--id=1"]
120
+ it_should_fail_with "name or id missing", []
121
+ end
122
+
123
+ context "output" do
124
+ with_params ["--name=my5name.mydomain.net"] do
125
+ it_should_print_column "Id"
126
+ it_should_print_column "Name"
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+
133
+ context "PuppetRunCommand" do
134
+
135
+ let(:cmd) { HammerCLIForeman::Host::PuppetRunCommand.new("", ctx) }
136
+
137
+ context "parameters" do
138
+ it_should_accept "name", ["--name=host"]
139
+ it_should_accept "id", ["--id=1"]
140
+ it_should_fail_with "no arguments"
141
+ end
142
+
143
+ context "output" do
144
+ with_params ["--id=1"] do
145
+ it "should inform that puppet was triggered" do
146
+ cmd.stubs(:context).returns({ :adapter => :test })
147
+ proc { cmd.run(with_params) }.must_output "Puppet run triggered\n"
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+
154
+ context "ReportsCommand" do
155
+
156
+ let(:cmd) { HammerCLIForeman::Host::ReportsCommand.new("", ctx) }
157
+
158
+ context "parameters" do
159
+ it_should_accept "id", ["--id=1"]
160
+ it_should_accept "name", ["--name=my.test.host.org"]
161
+ end
162
+
163
+ context "output" do
164
+ with_params ["--id=1"] do
165
+ let(:expected_record_count) { cmd.resource.call(:index)[0].length }
166
+
167
+ it_should_print_n_records
168
+ it_should_print_column "Id"
169
+ it_should_print_column "Host"
170
+ it_should_print_column "Last report"
171
+ it_should_print_column "Applied"
172
+ it_should_print_column "Restarted"
173
+ it_should_print_column "Failed"
174
+ it_should_print_column "Restart Failures"
175
+ it_should_print_column "Skipped"
176
+ it_should_print_column "Pending"
177
+ end
178
+ end
179
+
180
+ end
181
+
182
+ context "DeleteCommand" do
183
+
184
+ let(:cmd) { HammerCLIForeman::Host::DeleteCommand.new("", ctx) }
185
+
186
+ context "parameters" do
187
+ it_should_accept "name", ["--name=host"]
188
+ it_should_accept "id", ["--id=1"]
189
+ it_should_fail_with "name or id missing", []
190
+ end
191
+
192
+ end
193
+
194
+ context "CreateCommand" do
195
+
196
+ let(:cmd) { HammerCLIForeman::Host::CreateCommand.new("", ctx) }
197
+
198
+ context "parameters" do
199
+ it_should_accept "name, environment_id, architecture_id, domain_id, puppet_proxy_id, operatingsystem_id and more",
200
+ ["--name=host", "--environment-id=1", "--architecture-id=1", "--domain-id=1", "--puppet-proxy-id=1", "--operatingsystem-id=1",
201
+ "--ip=1.2.3.4", "--mac=11:22:33:44:55:66", "--medium-id=1", "--partition-table-id=1", "--subnet-id=1",
202
+ "--sp-subnet-id=1", "--model-id=1", "--hostgroup-id=1", "--owner-id=1", '--puppet-ca-proxy-id=1']
203
+ it_should_fail_with "name or id missing",
204
+ ["--environment-id=1", "--architecture-id=1", "--domain-id=1", "--puppet-proxy-id=1", "--operatingsystem-id=1"]
205
+ it_should_fail_with "environment_id missing",
206
+ ["--name=host", "--architecture-id=1", "--domain-id=1", "--puppet-proxy-id=1", "--operatingsystem-id=1"]
207
+ it_should_fail_with "architecture_id missing",
208
+ ["--name=host", "--environment-id=1", "--domain-id=1", "--puppet-proxy-id=1", "--operatingsystem-id=1"]
209
+ it_should_fail_with "domain_id missing",
210
+ ["--name=host", "--environment-id=1", "--architecture-id=1", "--puppet-proxy-id=1", "--operatingsystem-id=1"]
211
+ it_should_fail_with "puppet_proxy_id missing",
212
+ ["--name=host", "--environment-id=1", "--architecture-id=1", "--domain-id=1", "--operatingsystem-id=1"]
213
+ it_should_fail_with "operatingsystem_id missing",
214
+ ["--name=host", "--environment-id=1", "--architecture-id=1", "--domain-id=1", "--puppet-proxy-id=1"]
215
+ end
216
+ end
217
+
218
+ context "UpdateCommand" do
219
+
220
+ let(:cmd) { HammerCLIForeman::Host::UpdateCommand.new("", ctx) }
221
+
222
+ context "parameters" do
223
+ it_should_accept "name", ["--name=host", "--new-name=host2"]
224
+ it_should_accept "id and more", ["--id=1", "--new-name=host2", "--environment-id=1", "--architecture-id=1",
225
+ "--domain-id=1", "--puppet-proxy-id=1", "--operatingsystem-id=1",
226
+ "--ip=1.2.3.4", "--mac=11:22:33:44:55:66", "--medium-id=1", "--partition-table-id=1", "--subnet-id=1",
227
+ "--sp-subnet-id=1", "--model-id=1", "--hostgroup-id=1", "--owner-id=1", '--puppet-ca-proxy-id=1']
228
+ it_should_fail_with "no params", []
229
+ it_should_fail_with "name or id missing", ["--new-name=host2"]
230
+ end
231
+
232
+ end
233
+
234
+
235
+ context "SetParameterCommand" do
236
+
237
+ before :each do
238
+ resource_mock = ApipieResourceMock.new(cmd.class.resource.resource_class)
239
+ resource_mock.stub_method(:index, [])
240
+ cmd.class.resource resource_mock
241
+ end
242
+
243
+ let(:cmd) { HammerCLIForeman::Host::SetParameterCommand.new("", ctx) }
244
+
245
+ context "parameters" do
246
+ it_should_accept "name, value and host name", ["--name=name", "--value=val", "--host-name=name"]
247
+ it_should_accept "name, value and host id", ["--name=name", "--value=val", "--host-id=id"]
248
+ it_should_fail_with "name missing", ["--value=val", "--host-name=name"]
249
+ it_should_fail_with "value missing", ["--name=name", "--host-name=name"]
250
+ it_should_fail_with "host name or id missing", ["--name=name", "--value=val"]
251
+ end
252
+
253
+ end
254
+
255
+
256
+ context "DeleteParameterCommand" do
257
+
258
+ let(:cmd) { HammerCLIForeman::Host::DeleteParameterCommand.new("", ctx) }
259
+
260
+ context "parameters" do
261
+ it_should_accept "name and host name", ["--name=name", "--host-name=name"]
262
+ it_should_accept "name and host id", ["--name=name", "--host-id=id"]
263
+ it_should_fail_with "name missing", ["--host-name=name"]
264
+ it_should_fail_with "host name or id missing", ["--name=name"]
265
+ end
266
+
267
+ end
268
+
269
+ context "StartCommand" do
270
+ let(:cmd) { HammerCLIForeman::Host::StartCommand.new("", ctx) }
271
+ context "parameters" do
272
+ it_should_accept "name", ["--name=host"]
273
+ it_should_accept "id", ["--id=1"]
274
+ it_should_fail_with "empty params", []
275
+ end
276
+ end
277
+
278
+ context "StopCommand" do
279
+ let(:cmd) { HammerCLIForeman::Host::StopCommand.new("", ctx) }
280
+ context "parameters" do
281
+ it_should_accept "name", ["--name=host"]
282
+ it_should_accept "id", ["--id=1"]
283
+ it_should_accept "id and force", ["--id=1", "--force"]
284
+ it_should_fail_with "empty params", []
285
+ end
286
+ end
287
+
288
+ context "RebootCommand" do
289
+ let(:cmd) { HammerCLIForeman::Host::RebootCommand.new("", ctx) }
290
+ context "parameters" do
291
+ it_should_accept "name", ["--name=host"]
292
+ it_should_accept "id", ["--id=1"]
293
+ it_should_fail_with "empty params", []
294
+ end
295
+ end
296
+
297
+ context "SCParamsCommand" do
298
+
299
+ before :each do
300
+ cmd.class.resource ResourceMocks.smart_class_parameter
301
+ end
302
+
303
+ let(:cmd) { HammerCLIForeman::Host::SCParamsCommand.new("", ctx) }
304
+
305
+ context "parameters" do
306
+ it_should_accept "name", ["--name=env"]
307
+ it_should_accept "id", ["--id=1"]
308
+ it_should_fail_with "name or id missing", []
309
+ end
310
+
311
+ end
312
+
313
+ end
314
+
315
+