rhc 1.2.7 → 1.3.8

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.
Files changed (97) hide show
  1. data/bin/rhc +6 -8
  2. data/bin/rhc-chk +23 -10
  3. data/features/domain.feature +1 -1
  4. data/features/lib/rhc_helper.rb +3 -2
  5. data/features/lib/rhc_helper/api.rb +7 -0
  6. data/features/lib/rhc_helper/app.rb +8 -10
  7. data/features/lib/rhc_helper/domain.rb +2 -1
  8. data/features/lib/rhc_helper/runnable.rb +2 -24
  9. data/features/sshkey.feature +3 -3
  10. data/features/step_definitions/cartridge_steps.rb +6 -6
  11. data/features/step_definitions/client_steps.rb +0 -1
  12. data/features/step_definitions/sshkey_steps.rb +2 -2
  13. data/features/support/before_hooks.rb +0 -1
  14. data/features/support/env.rb +5 -3
  15. data/lib/rhc-common.rb +1 -1
  16. data/lib/rhc.rb +9 -8
  17. data/lib/rhc/auth.rb +3 -0
  18. data/lib/rhc/auth/basic.rb +54 -0
  19. data/lib/rhc/cartridge_helpers.rb +11 -5
  20. data/lib/rhc/cli.rb +4 -2
  21. data/lib/rhc/command_runner.rb +35 -30
  22. data/lib/rhc/commands.rb +127 -18
  23. data/lib/rhc/commands/account.rb +24 -0
  24. data/lib/rhc/commands/alias.rb +1 -1
  25. data/lib/rhc/commands/app.rb +210 -209
  26. data/lib/rhc/commands/apps.rb +22 -0
  27. data/lib/rhc/commands/base.rb +10 -77
  28. data/lib/rhc/commands/cartridge.rb +35 -35
  29. data/lib/rhc/commands/domain.rb +20 -13
  30. data/lib/rhc/commands/git_clone.rb +30 -0
  31. data/lib/rhc/commands/{port-forward.rb → port_forward.rb} +3 -3
  32. data/lib/rhc/commands/server.rb +28 -16
  33. data/lib/rhc/commands/setup.rb +18 -1
  34. data/lib/rhc/commands/snapshot.rb +4 -4
  35. data/lib/rhc/commands/sshkey.rb +4 -18
  36. data/lib/rhc/commands/tail.rb +32 -9
  37. data/lib/rhc/config.rb +168 -99
  38. data/lib/rhc/context_helper.rb +22 -9
  39. data/lib/rhc/core_ext.rb +41 -1
  40. data/lib/rhc/exceptions.rb +21 -5
  41. data/lib/rhc/git_helpers.rb +81 -0
  42. data/lib/rhc/help_formatter.rb +21 -1
  43. data/lib/rhc/helpers.rb +222 -87
  44. data/lib/rhc/output_helpers.rb +94 -110
  45. data/lib/rhc/rest.rb +15 -198
  46. data/lib/rhc/rest/api.rb +88 -0
  47. data/lib/rhc/rest/application.rb +29 -30
  48. data/lib/rhc/rest/attributes.rb +27 -0
  49. data/lib/rhc/rest/base.rb +29 -33
  50. data/lib/rhc/rest/cartridge.rb +42 -20
  51. data/lib/rhc/rest/client.rb +351 -89
  52. data/lib/rhc/rest/domain.rb +7 -13
  53. data/lib/rhc/rest/gear_group.rb +1 -1
  54. data/lib/rhc/rest/key.rb +7 -2
  55. data/lib/rhc/rest/mock.rb +609 -0
  56. data/lib/rhc/rest/user.rb +6 -2
  57. data/lib/rhc/{ssh_key_helpers.rb → ssh_helpers.rb} +58 -28
  58. data/lib/rhc/{targz.rb → tar_gz.rb} +0 -0
  59. data/lib/rhc/usage_templates/command_help.erb +4 -1
  60. data/lib/rhc/usage_templates/help.erb +24 -11
  61. data/lib/rhc/usage_templates/options_help.erb +14 -0
  62. data/lib/rhc/wizard.rb +283 -213
  63. data/spec/keys/example.pem +23 -0
  64. data/spec/keys/example_private.pem +27 -0
  65. data/spec/keys/server.pem +19 -0
  66. data/spec/rest_spec_helper.rb +3 -371
  67. data/spec/rhc/auth_spec.rb +226 -0
  68. data/spec/rhc/cli_spec.rb +41 -14
  69. data/spec/rhc/command_spec.rb +44 -15
  70. data/spec/rhc/commands/account_spec.rb +41 -0
  71. data/spec/rhc/commands/alias_spec.rb +16 -15
  72. data/spec/rhc/commands/app_spec.rb +115 -92
  73. data/spec/rhc/commands/apps_spec.rb +39 -0
  74. data/spec/rhc/commands/cartridge_spec.rb +134 -112
  75. data/spec/rhc/commands/domain_spec.rb +31 -86
  76. data/spec/rhc/commands/git_clone_spec.rb +56 -0
  77. data/spec/rhc/commands/{port-forward_spec.rb → port_forward_spec.rb} +27 -32
  78. data/spec/rhc/commands/server_spec.rb +28 -3
  79. data/spec/rhc/commands/setup_spec.rb +29 -11
  80. data/spec/rhc/commands/snapshot_spec.rb +4 -3
  81. data/spec/rhc/commands/sshkey_spec.rb +24 -56
  82. data/spec/rhc/commands/tail_spec.rb +26 -9
  83. data/spec/rhc/commands/threaddump_spec.rb +12 -11
  84. data/spec/rhc/config_spec.rb +211 -164
  85. data/spec/rhc/context_spec.rb +2 -0
  86. data/spec/rhc/helpers_spec.rb +242 -46
  87. data/spec/rhc/rest_application_spec.rb +42 -28
  88. data/spec/rhc/rest_client_spec.rb +110 -93
  89. data/spec/rhc/rest_spec.rb +220 -131
  90. data/spec/rhc/targz_spec.rb +1 -1
  91. data/spec/rhc/wizard_spec.rb +435 -624
  92. data/spec/spec.opts +1 -1
  93. data/spec/spec_helper.rb +140 -6
  94. data/spec/wizard_spec_helper.rb +326 -0
  95. metadata +163 -143
  96. data/lib/rhc/client.rb +0 -17
  97. data/lib/rhc/git_helper.rb +0 -59
@@ -3,6 +3,8 @@ require 'rhc/commands/base'
3
3
  require 'rhc/exceptions'
4
4
 
5
5
  describe RHC::Commands::Base do
6
+ before{ base_config }
7
+
6
8
  context 'when statically defined with context' do
7
9
  subject do
8
10
  Kernel.module_eval do
@@ -1,7 +1,8 @@
1
1
  require 'spec_helper'
2
2
  require 'rhc/helpers'
3
- require 'rhc/ssh_key_helpers'
3
+ require 'rhc/ssh_helpers'
4
4
  require 'rhc/cartridge_helpers'
5
+ require 'rhc/git_helpers'
5
6
  require 'rhc/core_ext'
6
7
  require 'highline/import'
7
8
  require 'rhc/config'
@@ -11,23 +12,32 @@ require 'date'
11
12
  describe RHC::Helpers do
12
13
  before(:each) do
13
14
  mock_terminal
14
- RHC::Config.set_defaults
15
- @tests = HelperTests.new
15
+ user_config
16
16
  end
17
17
 
18
18
  subject do
19
19
  Class.new(Object) do
20
20
  include RHC::Helpers
21
+ include RHC::SSHHelpers
21
22
 
22
23
  def config
23
- @config ||= RHC::Config
24
+ @config ||= RHC::Config.new
25
+ end
26
+ def options
27
+ @options ||= OpenStruct.new([:server])
24
28
  end
25
29
  end.new
26
30
  end
31
+ let(:tests) { OutputTests.new }
27
32
 
28
33
  its(:openshift_server) { should == 'openshift.redhat.com' }
29
34
  its(:openshift_url) { should == 'https://openshift.redhat.com' }
30
35
 
36
+ it("should display slashes"){ subject.system_path('foo/bar').should == 'foo/bar' }
37
+ context "on windows" do
38
+ it("should display backslashes"){ with_constants({:ALT_SEPARATOR => '\\'}, File) { subject.system_path('foo/bar').should == 'foo\\bar' } }
39
+ it("should handle drives"){ with_constants({:ALT_SEPARATOR => '\\'}, File) { subject.system_path('C:/foo/bar').should == 'C:\\foo\\bar' } }
40
+ end
31
41
 
32
42
  it("should pluralize many") { subject.pluralize(3, 'fish').should == '3 fishs' }
33
43
  it("should not pluralize one") { subject.pluralize(1, 'fish').should == '1 fish' }
@@ -35,10 +45,10 @@ describe RHC::Helpers do
35
45
  it("should decode json"){ subject.decode_json("{\"a\" : 1}").should == {'a' => 1} }
36
46
 
37
47
  it("should output green on success") do
38
- capture{ subject.success 'this is green' }.should == "\e[32mthis is green\e[0m"
48
+ capture{ subject.success 'this is green' }.should == "\e[32mthis is green\e[0m\n"
39
49
  end
40
50
  it("should output yellow on warn") do
41
- capture{ subject.success 'this is yellow' }.should == "\e[32mthis is yellow\e[0m"
51
+ capture{ subject.success 'this is yellow' }.should == "\e[32mthis is yellow\e[0m\n"
42
52
  end
43
53
  it("should return true on success"){ subject.success('anything').should be_true }
44
54
  it("should return true on success"){ subject.warn('anything').should be_true }
@@ -49,8 +59,6 @@ describe RHC::Helpers do
49
59
  end.should == ['10 2','3 40']
50
60
  end
51
61
 
52
- it("should generate table rows"){ subject.send(:make_table, [1,2]).should == [1,2] }
53
- it("should generate a table"){ subject.send(:make_table, 1).should == [1] }
54
62
  it("should output a table") do
55
63
  subject.send(:display_no_info, 'test').should == ['This test has no information to show']
56
64
  end
@@ -63,22 +71,48 @@ describe RHC::Helpers do
63
71
  end
64
72
 
65
73
  context 'using the current time' do
66
- let(:now){ Time.now }
74
+ let(:date){ Time.local(2008,1,2,1,1,0) }
75
+ let(:today){ Date.new(2008,1,2) }
76
+ before{ Date.stub(:today).and_return(today) }
77
+
67
78
  let(:rfc3339){ '%Y-%m-%dT%H:%M:%S%z' }
68
79
  it("should output the time for a date that is today") do
69
- subject.date(now.strftime(rfc3339)).should =~ /^[0-9]/
80
+ subject.date(date.strftime(rfc3339)).should =~ /^[0-9]/
81
+ end
82
+ it("should exclude the year for a date that is this year") do
83
+ subject.date(date.strftime(rfc3339)).should_not match(date.year.to_s)
70
84
  end
71
85
  it("should output the year for a date that is not this year") do
72
- older = now - 1*365*24*60
73
- subject.date(older.strftime(rfc3339)).should =~ /^[A-Z]/
86
+ older = Date.today - 1*365
87
+ subject.date(older.strftime(rfc3339)).should match(older.year.to_s)
88
+ end
89
+ it("should handle invalid input") do
90
+ subject.date('Unknown date').should == 'Unknown date'
91
+ end
92
+
93
+ context 'when the year is different' do
94
+ let(:today){ Date.new(2007,1,2) }
95
+ it{ subject.date(date.strftime(rfc3339)).should match(date.year.to_s) }
96
+ end
97
+
98
+ context 'when the year of the day is different' do
99
+ let(:today){ Date.new(2008,1,1) }
100
+ it{ subject.date(date.strftime(rfc3339)).should_not match(date.year.to_s) }
74
101
  end
75
102
  end
76
103
 
77
104
  context 'with LIBRA_SERVER environment variable' do
78
105
  before do
79
106
  ENV['LIBRA_SERVER'] = 'test.com'
80
- # need to reinit config to pick up env var
81
- RHC::Config.initialize
107
+ user_config
108
+ end
109
+ its(:openshift_server) { should == 'test.com' }
110
+ its(:openshift_url) { should == 'https://test.com' }
111
+ after { ENV['LIBRA_SERVER'] = nil }
112
+ end
113
+ context 'with --server environment variable' do
114
+ before do
115
+ subject.options.server = "test.com"
82
116
  end
83
117
  its(:openshift_server) { should == 'test.com' }
84
118
  its(:openshift_url) { should == 'https://test.com' }
@@ -86,63 +120,188 @@ describe RHC::Helpers do
86
120
  end
87
121
 
88
122
  context "without RHC::Config" do
89
- subject do
123
+ subject do
90
124
  Class.new(Object){ include RHC::Helpers }.new
91
125
  end
92
126
 
93
127
  it("should raise on config"){ expect{ subject.config }.should raise_error }
94
128
  end
95
129
 
130
+ context "with a bad timeout value" do
131
+ context "on the command line" do
132
+ let(:arguments){ ['help', '--timeout=string'] }
133
+ it{ expect{ run }.to exit_with_code(1) }
134
+ it{ run_output.should match("invalid argument: --timeout=string") }
135
+ end
136
+ context "via the config" do
137
+ before{ base_config{ |c, d| d.add 'timeout', 'string' } }
138
+ let(:arguments){ ['help'] }
139
+ it{ expect{ run }.to exit_with_code(1) }
140
+ it{ run_output.should match(/The configuration file.*invalid setting: invalid value for Integer/) }
141
+ end
142
+ end
143
+ context "with a valid client cert file" do
144
+ let(:arguments){ ['help', '--ssl-client-cert-file=spec/keys/example.pem'] }
145
+ it{ expect{ run }.to exit_with_code(0) }
146
+ end
147
+
148
+ context "with a missing client cert file" do
149
+ context "on the command line" do
150
+ let(:arguments){ ['help', '--ssl-client-cert-file=not_a_file'] }
151
+ it{ expect{ run }.to exit_with_code(1) }
152
+ it{ run_output.should match("The certificate 'not_a_file' cannot be loaded: No such") }
153
+ end
154
+ context "via the config" do
155
+ before{ base_config{ |c, d| d.add 'ssl_client_cert_file', 'not_a_file' } }
156
+ let(:arguments){ ['help'] }
157
+ it{ expect{ run }.to exit_with_code(1) }
158
+ it{ run_output.should match("The certificate 'not_a_file' cannot be loaded: No such") }
159
+ end
160
+ end
161
+
162
+ context 'with a valid --ssl-version' do
163
+ let(:arguments){ ['help', '--ssl-version=sslv3'] }
164
+
165
+ context 'on an older version of HTTPClient' do
166
+ before{ HTTPClient::SSLConfig.should_receive(:method_defined?).any_number_of_times.with(:ssl_version).and_return(false) }
167
+ it('should print an error') { run_output.should =~ /You are using an older version of the httpclient.*--ssl-version/ }
168
+ it('should error out') { expect{ run }.to exit_with_code(1) }
169
+ end
170
+ context 'a newer version of HTTPClient' do
171
+ before{ HTTPClient::SSLConfig.should_receive(:method_defined?).any_number_of_times.with(:ssl_version).and_return(true) }
172
+ it('should not print an error') { run_output.should_not =~ /You are using an older version of the httpclient.*--ssl-version/ }
173
+ it('should error out') { expect{ run }.to exit_with_code(0) }
174
+ end
175
+ end
176
+
177
+ context "with an invalid SSLVersion" do
178
+ context "on the command line" do
179
+ let(:arguments){ ['help', '--ssl-version=ssl'] }
180
+ it{ expect{ run }.to exit_with_code(1) }
181
+ it{ run_output.should match("The provided SSL version 'ssl' is not valid. Supported values: ") }
182
+ end
183
+ context "via the config" do
184
+ before{ base_config{ |c, d| d.add 'ssl_version', 'ssl' } }
185
+ let(:arguments){ ['help'] }
186
+ it{ expect{ run }.to exit_with_code(1) }
187
+ it{ run_output.should match("The provided SSL version 'ssl' is not valid. Supported values: ") }
188
+ end
189
+ end
190
+
191
+ context "with an valid ssl CA file" do
192
+ let(:arguments){ ['help', '--ssl-ca-file=spec/keys/example.pem'] }
193
+ it{ expect{ run }.to exit_with_code(0) }
194
+ end
195
+
196
+ context "with an invalid ssl CA file" do
197
+ let(:arguments){ ['help', '--ssl-ca-file=not_a_file'] }
198
+ it{ expect{ run }.to exit_with_code(1) }
199
+ it{ run_output.should match("The certificate 'not_a_file' cannot be loaded: No such file or directory ") }
200
+ end
201
+
96
202
  context "Formatter" do
203
+ before{ tests.reset }
204
+
205
+ it "should print out a paragraph with open endline on the same line" do
206
+ tests.section_same_line
207
+ $terminal.read.should == "section 1 word\n"
208
+ end
209
+
97
210
  it "should print out a section without any line breaks" do
98
- @tests.section_no_breaks
99
- $terminal.read.should == "section 1 "
211
+ tests.section_no_breaks
212
+ $terminal.read.should == "section 1 \n"
100
213
  end
101
214
 
102
215
  it "should print out a section with trailing line break" do
103
- @tests.section_one_break
216
+ tests.section_one_break
104
217
  $terminal.read.should == "section 1\n"
105
218
  end
106
219
 
107
220
  it "should print out 2 sections with matching bottom and top margins generating one space between" do
108
- @tests.sections_equal_bottom_top
221
+ tests.sections_equal_bottom_top
109
222
  $terminal.read.should == "section 1\n\nsection 2\n"
110
223
  end
111
224
 
112
225
  it "should print out 2 sections with larger bottom margin generating two spaces between" do
113
- @tests.sections_larger_bottom
226
+ tests.sections_larger_bottom
114
227
  $terminal.read.should == "section 1\n\n\nsection 2\n"
115
228
  end
116
229
 
117
230
  it "should print out 2 sections with larger top margin generating two spaces between" do
118
- @tests.sections_larger_top
231
+ tests.sections_larger_top
119
232
  $terminal.read.should == "section 1\n\n\nsection 2\n"
120
233
  end
121
234
 
122
- it "should print out 4 sections with the middle two on the same line and a space between the lines" do
123
- @tests.sections_four_on_three_lines
124
- $terminal.read.should == "section 1\n\nsection 2 section 3\n\nsection 4\n"
235
+ it "should print out 4 sections and not collapse open sections" do
236
+ tests.sections_four_on_three_lines
237
+ $terminal.read.should == "section 1\n\nsection 2 \nsection 3\n\nsection 4\n"
125
238
  end
126
239
 
127
240
  it "should show the equivilance of paragaph to section(:top => 1, :bottom => 1)" do
128
- @tests.section_1_1
241
+ tests.section_1_1
129
242
  section_1_1 = $terminal.read
130
- @tests.reset
131
- @tests.section_paragraph
243
+ tests.reset
244
+ tests.section_paragraph
132
245
  paragraph = $terminal.read
133
246
 
134
247
  section_1_1.should == paragraph
135
248
 
136
- @tests.reset
137
- @tests.section_1_1
138
- @tests.section_paragraph
249
+ tests.reset
250
+ tests.section_1_1
251
+ tests.section_paragraph
139
252
 
140
- $terminal.read.should == "\nsection\n\nsection\n\n"
253
+ $terminal.read.should == "section\n\nsection\n"
141
254
  end
142
255
 
143
- it "should show two line with one space between even though an outside newline was printed" do
144
- @tests.outside_newline
145
- $terminal.read.should == "section 1\n\nsection 2\n"
256
+ it "should not collapse explicit newline sections" do
257
+ tests.outside_newline
258
+ $terminal.read.should == "section 1\n\n\nsection 2\n"
259
+ end
260
+ end
261
+
262
+ context "Git Helpers" do
263
+ subject{ Class.new(Object){ include RHC::Helpers; include RHC::GitHelpers; def debug?; false; end }.new }
264
+ before{ subject.stub(:git_version){ raise "Fake Exception" } }
265
+ its(:has_git?) { should be_false }
266
+
267
+ context "git clone repo" do
268
+ let(:stdout){ 'fake git clone' }
269
+ let(:exit_status){ 0 }
270
+ let!(:spawn) do
271
+ out, err = stdout, stderr
272
+ Open4.should_receive(:spawn).and_return(exit_status) do |cmd, opts|
273
+ opts['stdout'] << out if out
274
+ opts['stderr'] << err if err
275
+ exit_status
276
+ end
277
+ true
278
+ end
279
+
280
+ it { capture{ subject.git_clone_repo("url", "repo").should be_true } }
281
+ it { capture_all{ subject.git_clone_repo("url", "repo") }.should match("fake git clone") }
282
+
283
+ context "does not succeed" do
284
+ let(:stderr){ 'fatal: error' }
285
+ let(:exit_status){ 1 }
286
+
287
+ it { capture{ expect{ subject.git_clone_repo("url", "repo") }.should raise_error(RHC::GitException) } }
288
+ it { capture_all{ subject.git_clone_repo("url", "repo") rescue nil }.should match("fake git clone") }
289
+ it { capture_all{ subject.git_clone_repo("url", "repo") rescue nil }.should match("fatal: error") }
290
+ end
291
+
292
+ context "directory is missing" do
293
+ let(:stderr){ "fatal: destination path 'foo' already exists and is not an empty directory." }
294
+ let(:exit_status){ 1 }
295
+
296
+ it { capture{ expect{ subject.git_clone_repo("url", "repo") }.should raise_error(RHC::GitDirectoryExists) } }
297
+ end
298
+
299
+ context "permission denied" do
300
+ let(:stderr){ "Permission denied (publickey,gssapi-mic)." }
301
+ let(:exit_status){ 1 }
302
+
303
+ it { capture{ expect{ subject.git_clone_repo("url", "repo") }.should raise_error(RHC::GitPermissionDenied) } }
304
+ end
146
305
  end
147
306
  end
148
307
 
@@ -150,15 +309,21 @@ describe RHC::Helpers do
150
309
  it "should generate an ssh key then return nil when it tries to create another" do
151
310
  FakeFS do
152
311
  FakeFS::FileSystem.clear
153
- @tests.generate_ssh_key_ruby.should match("\.ssh/id_rsa\.pub")
154
- @tests.generate_ssh_key_ruby == nil
312
+ subject.generate_ssh_key_ruby.should match("\.ssh/id_rsa\.pub")
313
+ subject.generate_ssh_key_ruby == nil
155
314
  end
156
315
  end
316
+
317
+ it "should print an error when finger print fails" do
318
+ Net::SSH::KeyFactory.should_receive(:load_public_key).with('1').and_raise(Net::SSH::Exception.new("An error"))
319
+ subject.should_receive(:error).with('An error')
320
+ subject.fingerprint_for_local_key('1').should be_nil
321
+ end
157
322
  end
158
323
 
159
- class HelperTests
324
+ class OutputTests
160
325
  include RHC::Helpers
161
- include RHC::SSHKeyHelpers
326
+ include RHC::SSHHelpers
162
327
 
163
328
  def initialize
164
329
  @print_num = 0
@@ -180,6 +345,10 @@ describe RHC::Helpers do
180
345
  def output_no_breaks
181
346
  say "section #{next_print_num} "
182
347
  end
348
+
349
+ def section_same_line
350
+ section { output_no_breaks; say 'word' }
351
+ end
183
352
 
184
353
  def section_no_breaks
185
354
  section { output_no_breaks }
@@ -227,11 +396,21 @@ describe RHC::Helpers do
227
396
 
228
397
  # call section without output to reset spacing to 0
229
398
  def reset
230
- section {}
399
+ RHC::Helpers.send(:class_variable_set, :@@margin, nil)
231
400
  end
232
401
  end
233
402
  end
234
403
 
404
+ describe RHC::Helpers::StringTee do
405
+ let(:other){ StringIO.new }
406
+ subject{ RHC::Helpers::StringTee.new(other) }
407
+ context "It should copy output" do
408
+ before{ subject << 'foo' }
409
+ its(:string) { should == 'foo' }
410
+ it("should tee to other") { other.string.should == 'foo' }
411
+ end
412
+ end
413
+
235
414
  describe Object do
236
415
  context 'present?' do
237
416
  specify('nil') { nil.present?.should be_false }
@@ -241,6 +420,14 @@ describe Object do
241
420
  specify('empty string') { ''.present?.should be_false }
242
421
  end
243
422
 
423
+ context 'presence' do
424
+ specify('nil') { nil.presence.should be_nil }
425
+ specify('empty array') { [].presence.should be_nil }
426
+ specify('array') { [1].presence.should == [1] }
427
+ specify('string') { 'a'.presence.should == 'a' }
428
+ specify('empty string') { ''.presence.should be_nil }
429
+ end
430
+
244
431
  context 'blank?' do
245
432
  specify('nil') { nil.blank?.should be_true }
246
433
  specify('empty array') { [].blank?.should be_true }
@@ -299,7 +486,6 @@ end
299
486
  describe RHC::CartridgeHelpers do
300
487
  before(:each) do
301
488
  mock_terminal
302
- @tests = HelperTests.new
303
489
  end
304
490
 
305
491
  subject do
@@ -315,13 +501,23 @@ describe RHC::CartridgeHelpers do
315
501
  describe '#find_cartridge' do
316
502
  let(:cartridges){ [] }
317
503
  let(:find_cartridges){ [] }
318
- let(:rest_obj) do
319
- Object.new.tap do |o|
320
- o.stub(:find_cartridges).and_return(find_cartridges)
321
- o.stub(:cartridges).and_return(cartridges)
504
+ context "with a generic object" do
505
+ let(:rest_obj) do
506
+ Object.new.tap do |o|
507
+ o.stub(:find_cartridges).and_return(find_cartridges)
508
+ o.stub(:cartridges).and_return(cartridges)
509
+ end
322
510
  end
511
+ it { expect{ subject.find_cartridge(rest_obj, 'foo') }.should raise_error(RHC::CartridgeNotFoundException, 'Cartridge \'foo\' is not a valid cartridge name.') }
512
+ end
513
+ context "with an Application" do
514
+ let(:rest_obj) do
515
+ RHC::Rest::Application.new('name' => 'my_app').tap do |o|
516
+ o.stub(:find_cartridges).and_return(find_cartridges)
517
+ o.stub(:cartridges).and_return(cartridges)
518
+ end
519
+ end
520
+ it { expect{ subject.find_cartridge(rest_obj, 'foo') }.should raise_error(RHC::CartridgeNotFoundException, 'Cartridge \'foo\' cannot be found in application \'my_app\'.') }
323
521
  end
324
-
325
- it { expect{ subject.find_cartridge(rest_obj, 'foo') }.should raise_error(RHC::CartridgeNotFoundException, 'Invalid cartridge specified: \'foo\'. No cartridges have been added to this app.') }
326
522
  end
327
523
  end
@@ -5,22 +5,20 @@ require 'base64'
5
5
  module RHC
6
6
  module Rest
7
7
  describe Application do
8
- # make sure auth is set up for the Application object since we are not
9
- # calling it from RHC::Rest::Client
10
- credentials = Base64.strict_encode64("#{mock_user}:#{mock_pass}")
11
- @@headers["Authorization"] = "Basic #{credentials}"
12
- @@headers["User-Agent"] = RHC::Helpers.user_agent
13
-
8
+ let (:client) { RHC::Rest::Client.new('test.domain.com', 'test_user', 'test pass') }
14
9
  let (:app_links) { mock_response_links(mock_app_links('mock_domain','mock_app')) }
15
10
  let (:app_obj) {
16
- RHC::Rest::Application.new({ 'domain_id' => 'mock_domain',
17
- 'name' => 'mock_app',
18
- 'creation_time' => Time.now.to_s,
19
- 'uuid' => 1234,
20
- 'aliases' => ['alias1','alias2'],
21
- 'server_identity' => mock_uri,
22
- 'links' => app_links
23
- })
11
+ args = {
12
+ 'domain_id' => 'mock_domain',
13
+ 'name' => 'mock_app',
14
+ 'creation_time' => Time.now.to_s,
15
+ 'uuid' => 1234,
16
+ 'aliases' => ['alias1','alias2'],
17
+ 'server_identity' => mock_uri,
18
+ 'links' => app_links
19
+ }
20
+ args.merge!(attributes) if defined?(attributes)
21
+ RHC::Rest::Application.new(args, client)
24
22
  }
25
23
  context "#new" do
26
24
  it "returns an application object" do
@@ -44,25 +42,41 @@ module RHC
44
42
  end
45
43
 
46
44
  context "#cartridges" do
45
+ let(:num_carts){ 0 }
47
46
  before(:each) do
48
47
  stub_api_request(:any, app_links['LIST_CARTRIDGES']['relative']).
49
- to_return(mock_cartridge_response(2)).
50
- to_return(mock_cartridge_response(0))
48
+ to_return(mock_cartridge_response(num_carts))
51
49
  end
52
- it "returns a list of all cartridges in the current application" do
53
- app = app_obj
54
- carts = app.cartridges
55
- carts.length.should equal(2)
56
- (0..1).each do |idx|
57
- carts[idx].should be_an_instance_of RHC::Rest::Cartridge
58
- carts[idx].name.should == "mock_cart_#{idx}"
50
+ context "with carts" do
51
+ let(:num_carts){ 2 }
52
+ it "returns a list of all cartridges in the current application" do
53
+ app = app_obj
54
+ carts = app.cartridges
55
+ carts.length.should == 2
56
+ (0..1).each do |idx|
57
+ carts[idx].should be_an_instance_of RHC::Rest::Cartridge
58
+ carts[idx].name.should == "mock_cart_#{idx}"
59
+ end
59
60
  end
60
61
  end
61
- it "returns an empty list if the current app has no cartridges" do
62
- app = app_obj
63
- carts = app.cartridges # Disregard the first request;
64
- carts = app.cartridges # 2nd request simulates empty response.
65
- carts.length.should equal(0)
62
+ context "without carts" do
63
+ it "returns an empty list" do
64
+ app = app_obj
65
+ carts = app.cartridges
66
+ carts.length.should == 0
67
+ end
68
+ end
69
+ context "with carts included in initial reponse" do
70
+ let(:attributes){ {:cartridges => RHC::Json.decode(mock_cartridge_response(2)[:body])['data'] }}
71
+ it "returns a list of all cartridges in the current application" do
72
+ app = app_obj
73
+ carts = app.cartridges
74
+ carts.length.should == 2
75
+ (0..1).each do |idx|
76
+ carts[idx].should be_an_instance_of RHC::Rest::Cartridge
77
+ carts[idx].name.should == "mock_cart_#{idx}"
78
+ end
79
+ end
66
80
  end
67
81
  end
68
82