This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 208
/
server_spec.rb
572 lines (526 loc) · 22.7 KB
/
server_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
require 'spec_helper'
require 'rest_spec_helper'
require 'wizard_spec_helper'
require 'rhc/commands/server'
require 'rhc/config'
require 'rhc/servers'
describe RHC::Commands::Server do
let(:rest_client) { MockRestClient.new }
let(:default_options){}
let(:options){ Commander::Command::Options.new(default_options) }
let(:servers){ RHC::Servers.new.tap{|c| c.stub(:home_dir).and_return('/home/mock_user') } }
let(:config){ RHC::Config.new.tap{ |c| c.stub(:home_dir).and_return('/home/mock_user') } }
before do
FakeFS.activate!
FakeFS::FileSystem.clear
end
after do
FakeFS.deactivate!
end
describe "server status" do
before(:each){ user_config }
describe 'run against a different server' do
let(:arguments) { ['server', 'status', '--server', 'foo.com', '-l', 'person', '-p', ''] }
context 'when server refuses connection' do
before { stub_request(:get, 'https://foo.com/broker/rest/api').with(&user_agent_header).to_raise(SocketError) }
it('should output an error') { run_output.should =~ /Connected to foo.com.*Unable to connect to the server/m }
it { expect { run }.to exit_with_code(1) }
end
context 'when API is missing' do
before { stub_request(:get, 'https://foo.com/broker/rest/api').with(&user_agent_header).to_return(:status => 404) }
it('should output an error') { run_output.should =~ /Connected to foo.com.*server is not responding correctly/m }
it { expect { run }.to exit_with_code(1) }
end
context 'when API is at version 1.2' do
before do
rest_client.stub(:api_version_negotiated).and_return('1.2')
end
it('should output an error') { run_output.should =~ /Connected to foo.com.*Using API version 1.2/m }
it { expect { run }.to exit_with_code(0) }
end
end
describe 'run against an invalid server url' do
let(:arguments) { ['server', 'status', '--server', 'invalid_uri', '-l', 'person', '-p', ''] }
it('should output an invalid URI error') { run_output.should match('Invalid URI specified: invalid_uri') }
end
describe 'run' do
let(:arguments) { ['server', 'status'] }
before{ rest_client.stub(:auth).and_return(nil) }
context 'when no issues' do
before { stub_request(:get, 'https://openshift.redhat.com/app/status/status.json').with(&user_agent_header).to_return(:body => {'issues' => []}.to_json) }
it('should output success') { run_output.should =~ /All systems running fine/ }
it { expect { run }.to exit_with_code(0) }
end
context 'when 1 issue' do
before do
stub_request(:get, 'https://openshift.redhat.com/app/status/status.json').with(&user_agent_header).to_return(:body =>
{'open' => [
{'issue' => {
'created_at' => '2011-05-22T17:31:32-04:00',
'id' => 11,
'title' => 'Root cause',
'updates' => [{
'created_at' => '2012-05-22T13:48:20-04:00',
'description' => 'Working on update'
}]
}}]}.to_json)
end
it { expect { run }.to exit_with_code(1) }
it('should output message') { run_output.should =~ /1 open issue/ }
it('should output title') { run_output.should =~ /Root cause/ }
it('should contain update') { run_output.should =~ /Working on update/ }
end
end
end
describe "server list" do
context "without express.conf or servers.yml" do
let(:arguments) { ['servers'] }
it 'should output correctly' do
run_output.should =~ /You don't have any servers configured\. Use 'rhc setup' to configure your OpenShift server/
end
it { expect { run }.to exit_with_code(0) }
end
context "with one entry on servers.yml and no express.conf file" do
before do
stub_servers_yml
end
let(:arguments) { ['servers'] }
it 'should output correctly' do
run_output.should =~ /Server 'server1'/
run_output.should =~ /Hostname:\s+openshift1.server.com/
run_output.should =~ /Login:\s+user1/
run_output.should =~ /Use Auth Tokens:\s+true/
run_output.should =~ /Insecure:\s+false/
run_output.should =~ /You have 1 server configured/
end
it { expect { run }.to exit_with_code(0) }
end
context "from express.conf and servers.yml" do
let(:local_config_server){ 'openshift.redhat.com' }
before do
local_config
stub_servers_yml
end
let(:arguments) { ['servers'] }
it 'should output correctly' do
run_output.should =~ /Server 'online' \(not configured, run 'rhc setup'\)/
run_output.should =~ /Hostname:\s+#{local_config_server}/
run_output.should =~ /Server 'server1'/
run_output.should =~ /Hostname:\s+openshift1.server.com/
run_output.should =~ /Login:\s+user1/
run_output.should =~ /Use Auth Tokens:\s+true/
run_output.should =~ /Insecure:\s+false/
run_output.should =~ /You have 2 servers configured/
end
it { expect { run }.to exit_with_code(0) }
end
context "from express.conf and several entries on servers.yml" do
let(:local_config_server){ 'openshift.redhat.com' }
let(:entries){ 3 }
before do
local_config
stub_servers_yml(entries)
end
let(:arguments) { ['servers'] }
it 'should output correctly' do
run_output.should =~ /Server 'online' \(not configured, run 'rhc setup'\)/
run_output.should =~ /Hostname:\s+#{local_config_server}/
Array(1..entries).each do |i|
run_output.should =~ /Server 'server#{i}'/
run_output.should =~ /Hostname:\s+openshift#{i}.server.com/
end
end
it { expect { run }.to exit_with_code(0) }
end
context "from express.conf and several entries on servers.yml, matching servers" do
let(:local_config_server){ 'openshift1.server.com' }
let(:local_config_username){ 'local_config_user' }
let(:local_config_password){ 'password' }
let(:entries){ 3 }
before do
local_config
stub_servers_yml(entries)
end
let(:arguments) { ['servers'] }
it 'should output correctly' do
run_output.should =~ /Server 'server1' \(in use\)/
run_output.should =~ /Hostname:\s+#{local_config_server}/
run_output.should =~ /Login:\s+#{local_config_username}/
Array(1..entries).each do |i|
run_output.should =~ /Server 'server#{i}'/
run_output.should =~ /Hostname:\s+openshift#{i}.server.com/
end
end
it { expect { run }.to exit_with_code(0) }
end
context "when express.conf server is not present in servers.yml" do
let(:local_config_server){ 'some.openshift.mycompany.com' }
let(:entries){ 3 }
before do
local_config
stub_servers_yml(entries)
end
let(:arguments) { ['servers'] }
it 'should output correctly' do
run_output.should =~ /Server '#{local_config_server}' \(not configured, run 'rhc setup'\)/
run_output.should =~ /Hostname:\s+#{local_config_server}/
Array(1..entries).each do |i|
run_output.should =~ /Server 'server#{i}'/
run_output.should =~ /Hostname:\s+openshift#{i}.server.com/
end
end
it { expect { run }.to exit_with_code(0) }
end
end
describe "server show" do
context "from express.conf" do
let(:local_config_username){ 'local_config_user' }
let(:local_config_password){ 'password' }
let(:local_config_server){ 'openshift.redhat.com' }
before do
local_config
end
let(:arguments) { ['server', 'show', 'online'] }
it 'should output correctly' do
run_output.should =~ /Server 'online' \(in use\)/
run_output.should =~ /Hostname:\s+openshift.redhat.com/
run_output.should =~ /Login:\s+local_config_user/
end
it { expect { run }.to exit_with_code(0) }
end
context "from express.conf and servers.yml" do
let(:arguments) { ['server', 'show', 'openshift1.server.com'] }
before do
local_config
stub_servers_yml do |s|
s.each do |i|
i.nickname = nil
i.use_authorization_tokens = false
i.insecure = true
end
end
end
it 'should output correctly' do
run_output.should =~ /Server 'openshift1.server.com'/
run_output.should =~ /Hostname:\s+openshift1.server.com/
run_output.should =~ /Login:\s+user1/
run_output.should =~ /Use Auth Tokens:\s+false/
run_output.should =~ /Insecure:\s+true/
end
it { expect { run }.to exit_with_code(0) }
end
context "when trying to show server not configured" do
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
before(:each) do
stub_servers_yml
local_config
end
let(:arguments) { ['server', 'show', 'zee'] }
it 'should output correctly' do
run_output.should =~ /You don't have any server configured with the hostname or nickname 'zee'/
end
it { expect { run }.to exit_with_code(166) }
end
end
describe "server add" do
context "with no existing config" do
let(:new_server){ 'openshift1.server.com' }
let(:username){ 'user1' }
let(:token){ 'an_existing_token' }
let(:arguments) { ['server', 'add', new_server, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
before(:each) {
stub_wizard
}
it 'should create servers.yml' do
servers.present?.should be_false
servers.list.should == []
servers.hostname_exists?(new_server).should be_false
run_output.should =~ /Saving server configuration to.*servers\.yml.*done/
servers.reload
servers.present?.should be_true
servers.list.should_not == []
servers.hostname_exists?(new_server).should be_true
end
it { expect { run }.to exit_with_code(0) }
end
context "with existing express.conf and successfully adding server" do
let(:server){ 'openshift1.server.com' }
let(:username){ 'user1' }
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
let(:token){ 'an_existing_token' }
let(:arguments) { ['server', 'add', server, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
subject{ RHC::ServerWizard.new(config, options, servers) }
before(:each) do
RHC::Servers.any_instance.stub(:save!)
stub_wizard
local_config
end
it 'should output correctly' do
run_output.should =~ /Using an existing token for #{username} to login to #{server}/
run_output.should =~ /Saving configuration to.*express\.conf.*done/
run_output.should =~ /Saving server configuration to.*servers\.yml.*done/
end
it { expect { run }.to exit_with_code(0) }
end
context "with existing express.conf trying to add an existing server" do
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
let(:arguments) { ['server', 'add', local_config_server, 'server1', '-l', local_config_username, '--use-authorization-tokens', '--no-insecure'] }
before(:each) do
local_config
end
it 'should output correctly' do
run_output.should =~ /You already have a server configured with the hostname '#{local_config_server}'/
end
it { expect { run }.to exit_with_code(165) }
end
context "with existing express.conf and servers.yml and adding a new server" do
let(:server){ 'openshift.server.com' }
let(:username){ 'user3' }
let(:server_name){ 'server3' }
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
let(:token){ 'an_existing_token' }
let(:arguments) { ['server', 'add', server, server_name, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
subject{ RHC::ServerWizard.new(config, options, servers) }
before do
stub_wizard
local_config
stub_servers_yml(2)
end
it { run_output.should =~ /Using an existing token for #{username} to login to #{server}/ }
it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
it { expect { run }.to exit_with_code(0) }
end
context "with existing express.conf and servers.yml and adding a new server with port and http scheme" do
let(:server){ 'http://my.server.com:4000' }
let(:username){ 'user3' }
let(:server_name){ 'server3' }
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
let(:token){ 'an_existing_token' }
let(:arguments) { ['server', 'add', server, server_name, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
subject{ RHC::ServerWizard.new(config, options, servers) }
before do
stub_wizard
local_config
stub_servers_yml(2)
end
it { run_output.should =~ /Using an existing token for #{username} to login to #{server}/ }
it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
it { expect { run }.to exit_with_code(0) }
end
context "with existing express.conf and servers.yml and adding a new mock server" do
let(:server){ 'openshift.server.com' }
let(:username){ 'user3' }
let(:server_name){ 'server3' }
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
let(:arguments) { ['server', 'add', server, server_name, '-l', username, '--skip-wizard'] }
before do
local_config
stub_servers_yml(2)
end
it { run_output.should_not =~ /Using an existing token for #{username} to login to #{server}/ }
it { run_output.should_not =~ /Saving configuration to.*express\.conf.*done/ }
it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
it { expect { run }.to exit_with_code(0) }
end
context "with existing express.conf and servers.yml and trying to add an existing server" do
let(:local_config_username){ 'local_config_user' }
let(:local_config_password){ 'password' }
let(:local_config_server){ 'openshift.redhat.com' }
before do
local_config
stub_servers_yml(2)
end
let(:arguments) { ['server', 'add', 'foo.com', 'server1', '-l', local_config_username, '--use-authorization-tokens', '--no-insecure'] }
it 'should output correctly' do
run_output.should =~ /You already have a server configured with the nickname 'server1'/
end
it { expect { run }.to exit_with_code(164) }
end
context "when providing ssl information and missing a required option" do
let(:arguments) { ['server', 'add', 'testing.server.com', '--ssl-ca-file', '~/ca.crt', '--ssl-client-cert-file', '~/server.crt'] }
before do
RHC::HelpFormatter.any_instance.stub(:render_command_syntax).and_return('foo')
RHC::Helpers.stub(:certificate_file).and_return('foo')
end
it 'should output correctly' do
run_output.should =~ /You must use the --ssl-ca-file, --ssl-client-cert-file, and --ssl-client-key-file commands together./
end
it{ expect{run}.to exit_with_code(1) }
end
context "with wizard failure" do
let(:token){ 'an_existing_token' }
let(:arguments) { ['server', 'add', 'failure.server.com', 'failed', '-l', 'failer'] }
before do
RHC::ServerWizard.any_instance.stub(:run).and_return(false)
stub_servers_yml
end
it { expect { run }.to exit_with_code(1) }
end
end
describe "server remove" do
context "when trying to remove the server in use" do
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
before(:each) do
stub_servers_yml
local_config
end
let(:arguments) { ['server', 'remove', local_config_server] }
it 'should output correctly' do
run_output.should =~ /The '#{local_config_server}' server is in use/
end
it { expect { run }.to exit_with_code(167) }
end
context "when removing successfully" do
let(:server){ 'openshift5.server.com' }
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
before(:each) do
stub_servers_yml(5)
local_config
end
let(:arguments) { ['server', 'remove', server] }
it 'should output correctly' do
run_output.should =~ /Removing '#{server}'.*done/
end
it { expect { run }.to exit_with_code(0) }
end
context "when trying to remove server not configured" do
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
before(:each) do
stub_servers_yml
local_config
end
let(:arguments) { ['server', 'remove', 'zee'] }
it 'should output correctly' do
run_output.should =~ /You don't have any server configured with the hostname or nickname 'zee'/
end
it { expect { run }.to exit_with_code(166) }
end
end
describe "server configure" do
context "when configuring existing server" do
let(:server){ 'local.server.com' }
let(:username){ 'local_username' }
let(:local_config_server){ server }
let(:local_config_username){ username }
let(:local_config_server_new_username){ 'new_username' }
let(:local_config_server_new_name){ 'new_name' }
let(:token){ 'an_existing_token' }
let(:arguments) { ['server', 'configure', local_config_server, '--nickname', local_config_server_new_name, '-l', local_config_server_new_username, '--insecure', '--token', token, '--use'] }
subject{ RHC::ServerWizard.new(config, options, servers) }
before do
stub_wizard
local_config
stub_servers_yml
end
it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
it { run_output.should =~ /Using an existing token for #{local_config_server_new_username} to login to #{server}/ }
it { run_output.should =~ /Server '#{local_config_server_new_name}' \(in use\)/ }
it { run_output.should =~ /Hostname:\s+#{server}/ }
it { run_output.should =~ /Login:\s+#{local_config_server_new_username}/ }
it { run_output.should =~ /Insecure:\s+true/ }
it { run_output.should =~ /Now using '#{server}'/ }
it { expect { run }.to exit_with_code(0) }
end
context "with existing express.conf and servers.yml and skipping wizard" do
let(:server){ 'local.server.com' }
let(:username){ 'local_username' }
let(:local_config_server){ server }
let(:local_config_username){ username }
let(:local_config_server_new_username){ 'new_username' }
let(:local_config_server_new_name){ 'new_name' }
let(:arguments) { ['server', 'configure', local_config_server, '--nickname', local_config_server_new_name, '-l', local_config_server_new_username, '--insecure',
'--skip-wizard'] }
before do
local_config
stub_servers_yml
end
it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
it { run_output.should_not =~ /Saving configuration to.*express\.conf.*done/ }
it { run_output.should_not =~ /Using an existing token/ }
it { run_output.should =~ /Server '#{local_config_server_new_name}' \(in use\)/ }
it { run_output.should_not =~ /Now using '#{server}'/ }
it { run_output.should =~ /Login:\s+#{local_config_server_new_username}/ }
it { expect { run }.to exit_with_code(0) }
end
context "when trying to remove server not found" do
let(:local_config_server){ 'local.server.com' }
let(:local_config_username){ 'local_username' }
before(:each) do
stub_servers_yml
local_config
end
let(:arguments) { ['server', 'configure', 'zee', '--insecure'] }
it 'should output correctly' do
run_output.should =~ /You don't have any server configured with the hostname or nickname 'zee'/
end
it { expect { run }.to exit_with_code(166) }
end
end
describe "server use" do
context "when using an existing server" do
let(:server){ 'local.server.com' }
let(:username){ 'local_user' }
let(:local_config_server){ server }
let(:local_config_username){ username }
let(:token){ 'an_existing_token' }
subject{ RHC::ServerWizard.new(config, options, servers) }
before do
stub_wizard
local_config
stub_servers_yml(3)
end
let(:arguments) { ['server', 'use', 'server3', '--token', token] }
it { run_output.should =~ /Using an existing token for .* to login to openshift3\.server\.com/ }
it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
it { run_output.should =~ /Now using 'openshift3\.server\.com'/ }
it { expect { run }.to exit_with_code(0) }
end
context "with wizard failure" do
let(:server){ 'local.server.com' }
let(:username){ 'local_user' }
let(:local_config_server){ server }
let(:local_config_username){ username }
subject{ RHC::ServerWizard.new(config, options, servers) }
before do
RHC::ServerWizard.any_instance.stub(:run).and_return(false)
local_config
stub_servers_yml
end
let(:arguments) { ['server', 'use', 'local.server.com'] }
it { expect { run }.to exit_with_code(1) }
end
end
protected
def stub_servers_yml(entries=1, &block)
RHC::Servers.any_instance.stub(:present?).and_return(true)
RHC::Servers.any_instance.stub(:load).and_return(
Array(1..entries).collect do |i|
RHC::Server.new("openshift#{i}.server.com",
:nickname => "server#{i}",
:login => "user#{i}",
:use_authorization_tokens => true,
:insecure => false,
:persisted => true)
end.tap{|i| yield i if block_given?})
end
def stub_wizard
rest_client.stub(:api)
rest_client.stub(:user).and_return(double(:login => username))
rest_client.stub(:supports_sessions?).and_return(true)
rest_client.stub(:new_session)
subject.stub(:new_client_for_options).and_return(rest_client)
end
end