This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
/
helpers_spec.rb
577 lines (493 loc) · 18.6 KB
/
helpers_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
require 'spec_helper'
require 'rhc/helpers'
require 'rhc/ssh_helpers'
require 'rhc/cartridge_helpers'
require 'rhc/git_helpers'
require 'rhc/core_ext'
require 'highline/import'
require 'rhc/config'
require 'rhc/helpers'
require 'date'
require 'resolv'
describe RHC::Helpers do
before(:each) do
mock_terminal
user_config
end
subject do
Class.new(Object) do
include RHC::Helpers
include RHC::SSHHelpers
def config
@config ||= RHC::Config.new
end
def options
@options ||= OpenStruct.new(:server => nil)
end
end.new
end
let(:tests) { OutputTests.new }
its(:openshift_server) { should == 'openshift.redhat.com' }
its(:openshift_url) { should == 'https://openshift.redhat.com' }
it("should display slashes"){ subject.system_path('foo/bar').should == 'foo/bar' }
context "on windows" do
it("should display backslashes"){ with_constants({:ALT_SEPARATOR => '\\'}, File) { subject.system_path('foo/bar').should == 'foo\\bar' } }
it("should handle drives"){ with_constants({:ALT_SEPARATOR => '\\'}, File) { subject.system_path('C:/foo/bar').should == 'C:\\foo\\bar' } }
end
it("should pluralize many") { subject.pluralize(3, 'fish').should == '3 fishs' }
it("should not pluralize one") { subject.pluralize(1, 'fish').should == '1 fish' }
it("should decode json"){ subject.decode_json("{\"a\" : 1}").should == {'a' => 1} }
shared_examples_for "colorized output" do
it("should be colorized") do
message = "this is #{_color}"
output = capture{ subject.send(method,message) }
output.should be_colorized(message,_color)
end
it("should return true"){ subject.send(method,'anything').should be_true }
end
context "success output" do
let(:_color){ :green }
let(:method){ :success }
it_should_behave_like "colorized output"
end
context "warn output" do
let(:_color){ :yellow }
let(:method){ :warn }
it_should_behave_like "colorized output"
end
context "info output" do
let(:_color){ :cyan }
let(:method){ :info }
it_should_behave_like "colorized output"
end
context "error output" do
let(:_color){ :red }
let(:method){ :error }
it_should_behave_like "colorized output"
end
it("should output a table") do
subject.send(:display_no_info, 'test').should == ['This test has no information to show']
end
it "should parse an RFC3339 date" do
d = subject.datetime_rfc3339('2012-06-24T20:48:20-04:00')
d.day.should == 24
d.month.should == 6
d.year.should == 2012
end
context 'using the current time' do
let(:date){ Time.local(2008,1,2,1,1,0) }
let(:today){ Date.new(2008,1,2) }
before{ Date.stub(:today).and_return(today) }
let(:rfc3339){ '%Y-%m-%dT%H:%M:%S%z' }
it("should output the time for a date that is today") do
subject.date(date.strftime(rfc3339)).should =~ /^[0-9]/
end
it("should exclude the year for a date that is this year") do
subject.date(date.strftime(rfc3339)).should_not match(date.year.to_s)
end
it("should output the year for a date that is not this year") do
older = Date.today - 1*365
subject.date(older.strftime(rfc3339)).should match(older.year.to_s)
end
it("should handle invalid input") do
subject.date('Unknown date').should == 'Unknown date'
end
context 'when the year is different' do
let(:today){ Date.new(2007,1,2) }
it{ subject.date(date.strftime(rfc3339)).should match(date.year.to_s) }
end
context 'when the year of the day is different' do
let(:today){ Date.new(2008,1,1) }
it{ subject.date(date.strftime(rfc3339)).should_not match(date.year.to_s) }
end
end
context 'with LIBRA_SERVER environment variable' do
before do
ENV['LIBRA_SERVER'] = 'test.com'
user_config
end
its(:openshift_server) { should == 'test.com' }
its(:openshift_url) { should == 'https://test.com' }
after { ENV['LIBRA_SERVER'] = nil }
end
context 'with --server environment variable' do
before do
subject.options.server = "test.com"
end
its(:openshift_server) { should == 'test.com' }
its(:openshift_url) { should == 'https://test.com' }
after { ENV['LIBRA_SERVER'] = nil }
end
context "without RHC::Config" do
subject do
Class.new(Object){ include RHC::Helpers }.new
end
it("should raise on config"){ expect{ subject.config }.should raise_error }
end
context "with a bad timeout value" do
context "on the command line" do
let(:arguments){ ['help', '--timeout=string'] }
it{ expect{ run }.to exit_with_code(1) }
it{ run_output.should match("invalid argument: --timeout=string") }
end
context "via the config" do
before{ base_config{ |c, d| d.add 'timeout', 'string' } }
let(:arguments){ ['help'] }
it{ expect{ run }.to exit_with_code(1) }
it{ run_output.should match(/The configuration file.*invalid setting: invalid value for Integer/) }
end
end
context "with a valid client cert file" do
let(:arguments){ ['help', '--ssl-client-cert-file=spec/keys/example.pem'] }
it{ expect{ run }.to exit_with_code(0) }
end
context "with a missing client cert file" do
context "on the command line" do
let(:arguments){ ['help', '--ssl-client-cert-file=not_a_file'] }
it{ expect{ run }.to exit_with_code(1) }
it{ run_output.should match("The certificate 'not_a_file' cannot be loaded: No such") }
end
context "via the config" do
before{ base_config{ |c, d| d.add 'ssl_client_cert_file', 'not_a_file' } }
let(:arguments){ ['help'] }
it{ expect{ run }.to exit_with_code(1) }
it{ run_output.should match("The certificate 'not_a_file' cannot be loaded: No such") }
end
end
context 'with a valid --ssl-version' do
let(:arguments){ ['help', '--ssl-version=sslv3'] }
context 'on an older version of HTTPClient' do
before{ HTTPClient::SSLConfig.should_receive(:method_defined?).any_number_of_times.with(:ssl_version).and_return(false) }
it('should print an error') { run_output.should =~ /You are using an older version of the httpclient.*--ssl-version/ }
it('should error out') { expect{ run }.to exit_with_code(1) }
end
context 'a newer version of HTTPClient' do
before{ HTTPClient::SSLConfig.should_receive(:method_defined?).any_number_of_times.with(:ssl_version).and_return(true) }
it('should not print an error') { run_output.should_not =~ /You are using an older version of the httpclient.*--ssl-version/ }
it('should error out') { expect{ run }.to exit_with_code(0) }
end
end
context "with an invalid SSLVersion" do
context "on the command line" do
let(:arguments){ ['help', '--ssl-version=ssl'] }
it{ expect{ run }.to exit_with_code(1) }
it{ run_output.should match("The provided SSL version 'ssl' is not valid. Supported values: ") }
end
context "via the config" do
before{ base_config{ |c, d| d.add 'ssl_version', 'ssl' } }
let(:arguments){ ['help'] }
it{ expect{ run }.to exit_with_code(1) }
it{ run_output.should match("The provided SSL version 'ssl' is not valid. Supported values: ") }
end
end
context "with an valid ssl CA file" do
let(:arguments){ ['help', '--ssl-ca-file=spec/keys/example.pem'] }
it{ expect{ run }.to exit_with_code(0) }
end
context "with an invalid ssl CA file" do
let(:arguments){ ['help', '--ssl-ca-file=not_a_file'] }
it{ expect{ run }.to exit_with_code(1) }
it{ run_output.should match("The certificate 'not_a_file' cannot be loaded: No such file or directory ") }
end
context "Formatter" do
before{ tests.reset }
it "should print out a paragraph with open endline on the same line" do
tests.section_same_line
$terminal.read.should == "section 1 word\n"
end
it "should print out a section without any line breaks" do
tests.section_no_breaks
$terminal.read.should == "section 1 \n"
end
it "should print out a section with trailing line break" do
tests.section_one_break
$terminal.read.should == "section 1\n"
end
it "should print out 2 sections with matching bottom and top margins generating one space between" do
tests.sections_equal_bottom_top
$terminal.read.should == "section 1\n\nsection 2\n"
end
it "should print out 2 sections with larger bottom margin generating two spaces between" do
tests.sections_larger_bottom
$terminal.read.should == "section 1\n\n\nsection 2\n"
end
it "should print out 2 sections with larger top margin generating two spaces between" do
tests.sections_larger_top
$terminal.read.should == "section 1\n\n\nsection 2\n"
end
it "should print out 4 sections and not collapse open sections" do
tests.sections_four_on_three_lines
$terminal.read.should == "section 1\n\nsection 2 \nsection 3\n\nsection 4\n"
end
it "should show the equivilance of paragaph to section(:top => 1, :bottom => 1)" do
tests.section_1_1
section_1_1 = $terminal.read
tests.reset
tests.section_paragraph
paragraph = $terminal.read
section_1_1.should == paragraph
tests.reset
tests.section_1_1
tests.section_paragraph
$terminal.read.should == "section\n\nsection\n"
end
it "should not collapse explicit newline sections" do
tests.outside_newline
$terminal.read.should == "section 1\n\n\nsection 2\n"
end
end
context "Git Helpers" do
subject{ Class.new(Object){ include RHC::Helpers; include RHC::GitHelpers; def debug?; false; end }.new }
before{ subject.stub(:git_version){ raise "Fake Exception" } }
its(:has_git?) { should be_false }
context "git clone repo" do
let(:stdout){ 'fake git clone' }
let(:exit_status){ 0 }
let!(:spawn) do
out, err = stdout, stderr
Open4.should_receive(:spawn).and_return(exit_status) do |cmd, opts|
opts['stdout'] << out if out
opts['stderr'] << err if err
exit_status
end
true
end
it { capture{ subject.git_clone_repo("url", "repo").should be_true } }
it { capture_all{ subject.git_clone_repo("url", "repo") }.should match("fake git clone") }
context "does not succeed" do
let(:stderr){ 'fatal: error' }
let(:exit_status){ 1 }
it { capture{ expect{ subject.git_clone_repo("url", "repo") }.should raise_error(RHC::GitException) } }
it { capture_all{ subject.git_clone_repo("url", "repo") rescue nil }.should match("fake git clone") }
it { capture_all{ subject.git_clone_repo("url", "repo") rescue nil }.should match("fatal: error") }
end
context "directory is missing" do
let(:stderr){ "fatal: destination path 'foo' already exists and is not an empty directory." }
let(:exit_status){ 1 }
it { capture{ expect{ subject.git_clone_repo("url", "repo") }.should raise_error(RHC::GitDirectoryExists) } }
end
context "permission denied" do
let(:stderr){ "Permission denied (publickey,gssapi-mic)." }
let(:exit_status){ 1 }
it { capture{ expect{ subject.git_clone_repo("url", "repo") }.should raise_error(RHC::GitPermissionDenied) } }
end
end
end
context "SSH Key Helpers" do
it "should generate an ssh key then return nil when it tries to create another" do
FakeFS do
FakeFS::FileSystem.clear
subject.generate_ssh_key_ruby.should match("\.ssh/id_rsa\.pub")
subject.generate_ssh_key_ruby == nil
end
end
it "should print an error when finger print fails" do
Net::SSH::KeyFactory.should_receive(:load_public_key).with('1').and_raise(Net::SSH::Exception.new("An error"))
subject.should_receive(:error).with('An error')
subject.fingerprint_for_local_key('1').should be_nil
end
end
context "Resolv helper" do
let(:resolver) { Object.new }
let(:existent_host) { 'real_host' }
let(:nonexistent_host) { 'fake_host' }
before :all do
Resolv::Hosts.stub(:new) { resolver }
resolver.stub(:getaddress).with(existent_host) { existent_host }
resolver.stub(:getaddress).with(nonexistent_host){ Resolv::ResolvError }
end
context "when hosts file has the desired host" do
it "does not raise error" do
expect {
subject.hosts_file_contains?(existent_host)
}.to_not raise_error
end
end
context "when hosts file does not have the desired host" do
it "does not raise error" do
expect {
subject.hosts_file_contains?(nonexistent_host)
}.to_not raise_error
end
end
end
class OutputTests
include RHC::Helpers
include RHC::SSHHelpers
def initialize
@print_num = 0
@options = Commander::Command::Options.new
end
def config
@config ||= RHC::Config
end
def next_print_num
@print_num += 1
end
def output
say "section #{next_print_num}"
end
def output_no_breaks
say "section #{next_print_num} "
end
def section_same_line
section { output_no_breaks; say 'word' }
end
def section_no_breaks
section { output_no_breaks }
end
def section_one_break
section { output }
end
def sections_equal_bottom_top
section(:bottom => 1) { output }
section(:top => 1) { output }
end
def sections_larger_bottom
section(:bottom => 2) { output }
section(:top => 1) { output }
end
def sections_larger_top
section(:bottom => 1) { output }
section(:top => 2) { output }
end
def sections_four_on_three_lines
section { output }
section(:top => 1) { output_no_breaks }
section(:bottom => 1) { output }
section(:top => 1) { output }
end
def outside_newline
section(:bottom => -1) { output }
say "\n"
section(:top => 1) { output }
end
def section_1_1
section(:top => 1, :bottom => 1) { say "section" }
end
def section_paragraph
paragraph { say "section" }
end
# call section without output to reset spacing to 0
def reset
RHC::Helpers.send(:class_variable_set, :@@margin, nil)
end
end
end
describe RHC::Helpers::StringTee do
let(:other){ StringIO.new }
subject{ RHC::Helpers::StringTee.new(other) }
context "It should copy output" do
before{ subject << 'foo' }
its(:string) { should == 'foo' }
it("should tee to other") { other.string.should == 'foo' }
end
end
describe Object do
context 'present?' do
specify('nil') { nil.present?.should be_false }
specify('empty array') { [].present?.should be_false }
specify('array') { [1].present?.should be_true }
specify('string') { 'a'.present?.should be_true }
specify('empty string') { ''.present?.should be_false }
end
context 'presence' do
specify('nil') { nil.presence.should be_nil }
specify('empty array') { [].presence.should be_nil }
specify('array') { [1].presence.should == [1] }
specify('string') { 'a'.presence.should == 'a' }
specify('empty string') { ''.presence.should be_nil }
end
context 'blank?' do
specify('nil') { nil.blank?.should be_true }
specify('empty array') { [].blank?.should be_true }
specify('array') { [1].blank?.should be_false }
specify('string') { 'a'.blank?.should be_false }
specify('empty string') { ''.blank?.should be_true }
end
end
describe OpenURI do
context 'redirectable?' do
specify('http to https') { OpenURI.redirectable?(URI.parse('http://foo.com'), URI.parse('https://foo.com')).should be_true }
specify('https to http') { OpenURI.redirectable?(URI.parse('https://foo.com'), URI.parse('http://foo.com')).should be_false }
end
end
describe HighLine do
it "should wrap the terminal" do
$terminal.wrap_at = 10
say "Lorem ipsum dolor sit amet"
output = $terminal.read
output.should match "Lorem\nipsum\ndolor sit\namet"
end
it "should wrap the terminal" do
$terminal.wrap_at = 16
say "Lorem ipsum dolor sit amet"
output = $terminal.read
output.should match "Lorem ipsum\ndolor sit amet"
end
it "should not wrap the terminal" do
$terminal.wrap_at = 50
say "Lorem ipsum dolor sit amet"
output = $terminal.read
output.should match "Lorem ipsum dolor sit amet"
end
it "should wrap the terminal when using color codes" do
$terminal.wrap_at = 10
say $terminal.color("Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet", :red)
output = $terminal.read
output.should match "Lorem\nipsum\ndolor sit\namet Lorem\nipsum\ndolor sit\namet"
end
it "should wrap the terminal with other escape characters" do
$terminal.wrap_at = 10
say "Lorem ipsum dolor sit am\eet"
output = $terminal.read
output.should match "Lorem\nipsum\ndolor sit\nam\eet"
end
it "should wrap the terminal when words are smaller than wrap length" do
$terminal.wrap_at = 3
say "Antidisestablishmentarianism"
output = $terminal.read
output.should match "Ant\nidi\nses\ntab\nlis\nhme\nnta\nria\nnis\nm"
end
end
describe RHC::CartridgeHelpers do
before(:each) do
mock_terminal
end
subject do
Class.new(Object) do
include RHC::Helpers
include RHC::CartridgeHelpers
def config
@config ||= RHC::Config.new
end
end.new
end
describe '#check_cartridges' do
let(:cartridges){ [] }
let(:find_cartridges){ [] }
context "with a generic object" do
it { expect{ subject.send(:check_cartridges, 'foo', :from => cartridges) }.should raise_error(RHC::CartridgeNotFoundException, 'There are no cartridges that match \'foo\'.') }
end
end
describe '#web_carts_only' do
it { expect{ subject.send(:web_carts_only).call([]) }.to raise_error(RHC::MultipleCartridgesException, /You must select only a single web/) }
end
describe '#match_cart' do
context 'with a nil cart' do
let(:cart){ OpenStruct.new(:name => nil, :description => nil, :tags => nil) }
it{ subject.send(:match_cart, cart, 'foo').should be_false }
end
context 'with simple strings' do
let(:cart){ OpenStruct.new(:name => 'FOO-more_max any', :description => 'bar', :tags => [:baz]) }
it{ subject.send(:match_cart, cart, 'foo').should be_true }
it{ subject.send(:match_cart, cart, 'fo').should be_true }
it{ subject.send(:match_cart, cart, 'oo').should be_true }
it{ subject.send(:match_cart, cart, 'bar').should be_true }
it{ subject.send(:match_cart, cart, 'baz').should be_true }
it{ subject.send(:match_cart, cart, 'more max').should be_true }
it{ subject.send(:match_cart, cart, 'foo more max any').should be_true }
it{ subject.send(:match_cart, cart, 'foo_more max-any').should be_true }
end
end
end