rhc 1.25.3 → 1.26.9

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.
@@ -41,7 +41,7 @@ Options
41
41
  Global Options
42
42
  <%= table(
43
43
  @global_options.select do |opt|
44
- not (opt[:hide] || @command.options.any?{ |o| (o[:switches] & opt[:switches]).present? })
44
+ not (opt[:hide] || @command.options.any?{ |o| (o[:switches].map(&:split).map(&:first) & opt[:switches].map(&:split).map(&:first)).present? })
45
45
  end.map do |opt|
46
46
  [opt[:switches].join(', '), opt[:description]]
47
47
  end,
@@ -1,6 +1,7 @@
1
1
  require 'rhc'
2
2
  require 'fileutils'
3
3
  require 'socket'
4
+ require 'rhc/server_helpers'
4
5
 
5
6
  module RHC
6
7
  class Wizard
@@ -12,6 +13,9 @@ module RHC
12
13
 
13
14
  DEFAULT_MAX_LENGTH = 16
14
15
 
16
+ SERVER_STAGES = [
17
+ :server_stage,
18
+ ]
15
19
  CONFIG_STAGES = [
16
20
  :login_stage,
17
21
  :create_config_stage,
@@ -30,7 +34,7 @@ module RHC
30
34
  APP_STAGES = [
31
35
  :show_app_info_stage,
32
36
  ]
33
- STAGES = [:greeting_stage] + CONFIG_STAGES + KEY_STAGES + TEST_STAGES + NAMESPACE_STAGES + APP_STAGES + [:finalize_stage]
37
+ STAGES = [:greeting_stage] + SERVER_STAGES + CONFIG_STAGES + KEY_STAGES + TEST_STAGES + NAMESPACE_STAGES + APP_STAGES + [:finalize_stage]
34
38
 
35
39
  def stages
36
40
  STAGES
@@ -42,9 +46,11 @@ module RHC
42
46
  # Running the setup wizard may change the contents of opts and config if
43
47
  # the create_config_stage completes successfully.
44
48
  #
45
- def initialize(config=RHC::Config.new, opts=Commander::Command::Options.new)
49
+ def initialize(config=RHC::Config.new, opts=Commander::Command::Options.new, servers=RHC::Servers.new)
46
50
  @config = config
47
51
  @options = opts
52
+ @servers = servers
53
+ @servers.sync_from_config(@config)
48
54
  end
49
55
 
50
56
  # Public: Runs the setup wizard to make sure ~/.openshift and ~/.ssh are correct
@@ -71,7 +77,8 @@ module RHC
71
77
  include RHC::SSHHelpers
72
78
  include RHC::GitHelpers
73
79
  include RHC::CartridgeHelpers
74
- attr_reader :config, :options
80
+ include RHC::ServerHelpers
81
+ attr_reader :config, :options, :servers
75
82
  attr_accessor :auth, :user
76
83
  attr_writer :rest_client
77
84
 
@@ -80,7 +87,7 @@ module RHC
80
87
  end
81
88
 
82
89
  def openshift_server
83
- options.server || config['libra_server'] || "openshift.redhat.com"
90
+ options.server || config['libra_server'] || openshift_online_server
84
91
  end
85
92
 
86
93
  def new_client_for_options
@@ -164,6 +171,20 @@ module RHC
164
171
  true
165
172
  end
166
173
 
174
+ def server_stage
175
+ paragraph do
176
+ unless options.__explicit__[:server]
177
+ say "If you have your own OpenShift server, you can specify it now. Just hit enter to use#{openshift_online_server? ? ' the server for OpenShift Online' : ''}: #{openshift_server}."
178
+ options.server = ask "Enter the server hostname: " do |q|
179
+ q.default = openshift_server
180
+ q.responses[:ask_on_error] = ''
181
+ end
182
+ paragraph{ say "You can add more servers later using 'rhc server'." }
183
+ end
184
+ end
185
+ true
186
+ end
187
+
167
188
  def login_stage
168
189
  if token_for_user
169
190
  options.token = token_for_user
@@ -220,24 +241,52 @@ module RHC
220
241
 
221
242
  def create_config_stage
222
243
  paragraph do
223
- if File.exists? config.path
224
- backup = "#{@config.path}.bak"
225
- FileUtils.cp(config.path, backup)
226
- FileUtils.rm(config.path)
227
- end
228
-
229
- say "Saving configuration to #{system_path(config.path)} ... "
244
+ FileUtils.mkdir_p File.dirname(config.path)
230
245
 
231
246
  changed = Commander::Command::Options.new(options)
232
247
  changed.rhlogin = username
233
248
  changed.password = nil
234
249
  changed.use_authorization_tokens = options.create_token != false && !changed.token.nil?
235
-
236
- FileUtils.mkdir_p File.dirname(config.path)
237
- config.save!(changed)
250
+ changed.insecure = options.insecure == true
238
251
  options.__replace__(changed)
239
252
 
240
- success "done"
253
+ # Save servers.yml if:
254
+ # 1. we've been explicitly told to (typically when running the "rhc server" command)
255
+ # 2. if the servers.yml file exists
256
+ # 3. if we're configuring a second server
257
+ write_servers_yml = @save_servers || servers.present? || (servers.list.present? && !servers.hostname_exists?(options.server))
258
+
259
+ # Decide which fields to save to express.conf
260
+ # 1. If we've already been told explicitly, use that
261
+ # 2. If we're writing servers.yml, only save server to express.conf
262
+ # 3. If we're not writing servers.yml, save everything to express.conf
263
+ config_fields_to_save = @config_fields_to_save || (write_servers_yml ? [:server] : nil)
264
+
265
+ # Save config unless we've been explicitly told not to save any fields to express.conf
266
+ if config_fields_to_save != []
267
+ say "Saving configuration to #{system_path(config.path)} ... "
268
+ config.backup
269
+ FileUtils.rm(config.path, :force => true)
270
+
271
+ config.save!(changed, config_fields_to_save)
272
+ success "done"
273
+ end
274
+
275
+ if write_servers_yml
276
+ say "Saving server configuration to #{system_path(servers.path)} ... "
277
+ servers.backup
278
+ servers.add_or_update(options.server,
279
+ :login => options.rhlogin,
280
+ :use_authorization_tokens => options.use_authorization_tokens,
281
+ :insecure => options.insecure,
282
+ :timeout => options.timeout,
283
+ :ssl_version => options.ssl_version,
284
+ :ssl_client_cert_file => options.ssl_client_cert_file,
285
+ :ssl_ca_file => options.ssl_ca_file)
286
+ servers.save!
287
+ success "done"
288
+ end
289
+
241
290
  end
242
291
 
243
292
  true
@@ -631,4 +680,16 @@ EOF
631
680
  super config, options
632
681
  end
633
682
  end
683
+
684
+ class ServerWizard < Wizard
685
+ def initialize(config, options, server_configs, set_as_default=false)
686
+ @save_servers = true
687
+ @config_fields_to_save = set_as_default ? [:server] : []
688
+ super config, options, server_configs
689
+ end
690
+
691
+ def stages
692
+ CONFIG_STAGES
693
+ end
694
+ end
634
695
  end
@@ -201,6 +201,7 @@ module RhcExecutionHelper
201
201
  def setup_args(opts={})
202
202
  c = opts[:client] || client
203
203
  args = []
204
+ args << (opts[:server] || ENV['RHC_SERVER'] || 'localhost')
204
205
  args << 'yes' if (ENV['TEST_INSECURE'] == '1' || false)
205
206
  args << (opts[:login] || ENV['TEST_USERNAME'])
206
207
  args << (opts[:password] || ENV['TEST_PASSWORD'])
@@ -2,12 +2,14 @@ require 'spec_helper'
2
2
  require 'rest_spec_helper'
3
3
  require 'rhc/commands/app'
4
4
  require 'rhc/config'
5
+ require 'rhc/servers'
5
6
  require 'resolv'
6
7
 
7
8
  describe RHC::Commands::App do
8
9
  let!(:rest_client){ MockRestClient.new }
9
10
  let!(:config){ user_config }
10
11
  before{ RHC::Config.stub(:home_dir).and_return('/home/mock_user') }
12
+ before{ RHC::Servers.stub(:home_dir).and_return('/home/mock_user') }
11
13
  before do
12
14
  FakeFS.activate!
13
15
  FakeFS::FileSystem.clear
@@ -1,69 +1,524 @@
1
1
  require 'spec_helper'
2
2
  require 'rest_spec_helper'
3
+ require 'wizard_spec_helper'
3
4
  require 'rhc/commands/server'
4
5
  require 'rhc/config'
6
+ require 'rhc/servers'
5
7
 
6
8
  describe RHC::Commands::Server do
7
- before(:each){ user_config }
8
9
  let(:rest_client) { MockRestClient.new }
10
+ let(:default_options){}
11
+ let(:options){ Commander::Command::Options.new(default_options) }
12
+ let(:config){ RHC::Config.new.tap{ |c| c.stub(:home_dir).and_return('/home/mock_user') } }
13
+ let(:servers){ RHC::Servers.new.tap{|c| c.stub(:home_dir).and_return('/home/mock_user') } }
14
+ before do
15
+ FakeFS.activate!
16
+ FakeFS::FileSystem.clear
17
+ end
18
+ after do
19
+ FakeFS.deactivate!
20
+ end
9
21
 
10
- describe 'run against a different server' do
11
- let(:arguments) { ['server', '--server', 'foo.com', '-l', 'person', '-p', ''] }
22
+ describe "server status" do
23
+ before(:each){ user_config }
24
+ describe 'run against a different server' do
25
+ let(:arguments) { ['server', 'status', '--server', 'foo.com', '-l', 'person', '-p', ''] }
12
26
 
13
- context 'when server refuses connection' do
14
- before { stub_request(:get, 'https://foo.com/broker/rest/api').with(&user_agent_header).to_raise(SocketError) }
15
- it('should output an error') { run_output.should =~ /Connected to foo.com.*Unable to connect to the server/m }
16
- it { expect { run }.to exit_with_code(1) }
27
+ context 'when server refuses connection' do
28
+ before { stub_request(:get, 'https://foo.com/broker/rest/api').with(&user_agent_header).to_raise(SocketError) }
29
+ it('should output an error') { run_output.should =~ /Connected to foo.com.*Unable to connect to the server/m }
30
+ it { expect { run }.to exit_with_code(1) }
31
+ end
32
+
33
+ context 'when API is missing' do
34
+ before { stub_request(:get, 'https://foo.com/broker/rest/api').with(&user_agent_header).to_return(:status => 404) }
35
+ it('should output an error') { run_output.should =~ /Connected to foo.com.*server is not responding correctly/m }
36
+ it { expect { run }.to exit_with_code(1) }
37
+ end
38
+
39
+ context 'when API is at version 1.2' do
40
+ before do
41
+ rest_client.stub(:api_version_negotiated).and_return('1.2')
42
+ end
43
+ it('should output an error') { run_output.should =~ /Connected to foo.com.*Using API version 1.2/m }
44
+ it { expect { run }.to exit_with_code(0) }
45
+ end
17
46
  end
18
47
 
19
- context 'when API is missing' do
20
- before { stub_request(:get, 'https://foo.com/broker/rest/api').with(&user_agent_header).to_return(:status => 404) }
21
- it('should output an error') { run_output.should =~ /Connected to foo.com.*server is not responding correctly/m }
22
- it { expect { run }.to exit_with_code(1) }
48
+ describe 'run against an invalid server url' do
49
+ let(:arguments) { ['server', 'status', '--server', 'invalid_uri', '-l', 'person', '-p', ''] }
50
+ it('should output an invalid URI error') { run_output.should match('Invalid URI specified: invalid_uri') }
51
+ end
52
+
53
+ describe 'run' do
54
+ let(:arguments) { ['server', 'status'] }
55
+ before{ rest_client.stub(:auth).and_return(nil) }
56
+
57
+ context 'when no issues' do
58
+ before { stub_request(:get, 'https://openshift.redhat.com/app/status/status.json').with(&user_agent_header).to_return(:body => {'issues' => []}.to_json) }
59
+ it('should output success') { run_output.should =~ /All systems running fine/ }
60
+ it { expect { run }.to exit_with_code(0) }
61
+ end
62
+
63
+ context 'when 1 issue' do
64
+ before do
65
+ stub_request(:get, 'https://openshift.redhat.com/app/status/status.json').with(&user_agent_header).to_return(:body =>
66
+ {'open' => [
67
+ {'issue' => {
68
+ 'created_at' => '2011-05-22T17:31:32-04:00',
69
+ 'id' => 11,
70
+ 'title' => 'Root cause',
71
+ 'updates' => [{
72
+ 'created_at' => '2012-05-22T13:48:20-04:00',
73
+ 'description' => 'Working on update'
74
+ }]
75
+ }}]}.to_json)
76
+ end
77
+ it { expect { run }.to exit_with_code(1) }
78
+ it('should output message') { run_output.should =~ /1 open issue/ }
79
+ it('should output title') { run_output.should =~ /Root cause/ }
80
+ it('should contain update') { run_output.should =~ /Working on update/ }
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "server list" do
86
+ context "without express.conf or servers.yml" do
87
+ let(:arguments) { ['servers'] }
88
+ it 'should output correctly' do
89
+ run_output.should =~ /You don't have any servers configured\. Use 'rhc setup' to configure your OpenShift server/
90
+ end
91
+ it { expect { run }.to exit_with_code(0) }
92
+ end
93
+
94
+ context "with one entry on servers.yml and no express.conf file" do
95
+ before do
96
+ stub_servers_yml
97
+ end
98
+ let(:arguments) { ['servers'] }
99
+ it 'should output correctly' do
100
+ run_output.should =~ /Server 'server1'/
101
+ run_output.should =~ /Hostname:\s+openshift1.server.com/
102
+ run_output.should =~ /Login:\s+user1/
103
+ run_output.should =~ /Use Auth Tokens:\s+true/
104
+ run_output.should =~ /Insecure:\s+false/
105
+ run_output.should =~ /You have 1 server configured/
106
+ end
107
+ it { expect { run }.to exit_with_code(0) }
108
+ end
109
+
110
+ context "from express.conf and servers.yml" do
111
+ let(:local_config_username){ 'local_config_user' }
112
+ let(:local_config_password){ 'password' }
113
+ let(:local_config_server){ 'openshift.redhat.com' }
114
+ before do
115
+ local_config
116
+ stub_servers_yml
117
+ end
118
+ let(:arguments) { ['servers'] }
119
+ it 'should output correctly' do
120
+ run_output.should =~ /Server 'online' \(in use\)/
121
+ run_output.should =~ /Hostname:\s+#{local_config_server}/
122
+ run_output.should =~ /Login:\s+#{local_config_username}/
123
+ run_output.should =~ /Server 'server1'/
124
+ run_output.should =~ /Hostname:\s+openshift1.server.com/
125
+ run_output.should =~ /Login:\s+user1/
126
+ run_output.should =~ /Use Auth Tokens:\s+true/
127
+ run_output.should =~ /Insecure:\s+false/
128
+ run_output.should =~ /You have 2 servers configured/
129
+ end
130
+ it { expect { run }.to exit_with_code(0) }
23
131
  end
24
132
 
25
- context 'when API is at version 1.2' do
133
+ context "from express.conf and several entries on servers.yml" do
134
+ let(:local_config_username){ 'local_config_user' }
135
+ let(:local_config_password){ 'password' }
136
+ let(:local_config_server){ 'openshift.redhat.com' }
137
+ let(:entries){ 3 }
26
138
  before do
27
- rest_client.stub(:api_version_negotiated).and_return('1.2')
139
+ local_config
140
+ stub_servers_yml(entries)
141
+ end
142
+ let(:arguments) { ['servers'] }
143
+ it 'should output correctly' do
144
+ run_output.should =~ /Server 'online' \(in use\)/
145
+ run_output.should =~ /Hostname:\s+#{local_config_server}/
146
+ Array(1..entries).each do |i|
147
+ run_output.should =~ /Server 'server#{i}'/
148
+ run_output.should =~ /Hostname:\s+openshift#{i}.server.com/
149
+ end
28
150
  end
29
- it('should output an error') { run_output.should =~ /Connected to foo.com.*Using API version 1.2/m }
30
151
  it { expect { run }.to exit_with_code(0) }
31
152
  end
32
153
  end
33
154
 
34
- describe 'run against an invalid server url' do
35
- let(:arguments) { ['server', '--server', 'invalid_uri', '-l', 'person', '-p', ''] }
36
- it('should output an invalid URI error') { run_output.should match('Invalid URI specified: invalid_uri') }
155
+ describe "server show" do
156
+ context "from express.conf" do
157
+ let(:local_config_username){ 'local_config_user' }
158
+ let(:local_config_password){ 'password' }
159
+ let(:local_config_server){ 'openshift.redhat.com' }
160
+ before do
161
+ local_config
162
+ end
163
+ let(:arguments) { ['server', 'show', 'online'] }
164
+ it 'should output correctly' do
165
+ run_output.should =~ /Server 'online' \(in use\)/
166
+ run_output.should =~ /Hostname:\s+openshift.redhat.com/
167
+ run_output.should =~ /Login:\s+local_config_user/
168
+ end
169
+ it { expect { run }.to exit_with_code(0) }
170
+ end
171
+
172
+ context "from express.conf and servers.yml" do
173
+ let(:arguments) { ['server', 'show', 'openshift1.server.com'] }
174
+ before do
175
+ local_config
176
+ stub_servers_yml do |s|
177
+ s.each do |i|
178
+ i.nickname = nil
179
+ i.use_authorization_tokens = false
180
+ i.insecure = true
181
+ end
182
+ end
183
+ end
184
+ it 'should output correctly' do
185
+ run_output.should =~ /Server 'openshift1.server.com'/
186
+ run_output.should =~ /Hostname:\s+openshift1.server.com/
187
+ run_output.should =~ /Login:\s+user1/
188
+ run_output.should =~ /Use Auth Tokens:\s+false/
189
+ run_output.should =~ /Insecure:\s+true/
190
+ end
191
+ it { expect { run }.to exit_with_code(0) }
192
+ end
193
+
194
+ context "when trying to show server not configured" do
195
+ let(:local_config_server){ 'local.server.com' }
196
+ let(:local_config_username){ 'local_username' }
197
+ before(:each) do
198
+ stub_servers_yml
199
+ local_config
200
+ end
201
+ let(:arguments) { ['server', 'show', 'zee'] }
202
+ it 'should output correctly' do
203
+ run_output.should =~ /You don't have any server configured with the hostname or nickname 'zee'/
204
+ end
205
+ it { expect { run }.to exit_with_code(166) }
206
+ end
207
+ end
208
+
209
+ describe "server add" do
210
+ context "with no existing config" do
211
+ let(:new_server){ 'openshift1.server.com' }
212
+ let(:username){ 'user1' }
213
+ let(:token){ 'an_existing_token' }
214
+ let(:arguments) { ['server', 'add', new_server, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
215
+ before(:each) {
216
+ stub_wizard
217
+ }
218
+ it 'should create servers.yml' do
219
+ servers.present?.should be_false
220
+ servers.list.should == []
221
+ servers.hostname_exists?(new_server).should be_false
222
+ run_output.should =~ /Saving server configuration to.*servers\.yml.*done/
223
+ servers.reload
224
+ servers.present?.should be_true
225
+ servers.list.should_not == []
226
+ servers.hostname_exists?(new_server).should be_true
227
+ end
228
+ it { expect { run }.to exit_with_code(0) }
229
+ end
230
+
231
+ context "with existing express.conf and successfully adding server" do
232
+ let(:server){ 'openshift1.server.com' }
233
+ let(:username){ 'user1' }
234
+ let(:local_config_server){ 'local.server.com' }
235
+ let(:local_config_username){ 'local_username' }
236
+ let(:token){ 'an_existing_token' }
237
+ let(:arguments) { ['server', 'add', server, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
238
+ subject{ RHC::ServerWizard.new(config, options, servers) }
239
+ before(:each) do
240
+ RHC::Servers.any_instance.stub(:save!)
241
+ stub_wizard
242
+ local_config
243
+ end
244
+ it 'should output correctly' do
245
+ run_output.should =~ /Using an existing token for #{username} to login to #{server}/
246
+ run_output.should =~ /Saving configuration to.*express\.conf.*done/
247
+ run_output.should =~ /Saving server configuration to.*servers\.yml.*done/
248
+ end
249
+ it { expect { run }.to exit_with_code(0) }
250
+ end
251
+
252
+ context "with existing express.conf trying to add an existing server" do
253
+ let(:local_config_server){ 'local.server.com' }
254
+ let(:local_config_username){ 'local_username' }
255
+ let(:arguments) { ['server', 'add', local_config_server, 'server1', '-l', local_config_username, '--use-authorization-tokens', '--no-insecure'] }
256
+ before(:each) do
257
+ local_config
258
+ end
259
+ it 'should output correctly' do
260
+ run_output.should =~ /You already have a server configured with the hostname '#{local_config_server}'/
261
+ end
262
+ it { expect { run }.to exit_with_code(165) }
263
+ end
264
+
265
+ context "with existing express.conf and servers.yml and adding a new server" do
266
+ let(:server){ 'openshift.server.com' }
267
+ let(:username){ 'user3' }
268
+ let(:server_name){ 'server3' }
269
+ let(:local_config_server){ 'local.server.com' }
270
+ let(:local_config_username){ 'local_username' }
271
+ let(:token){ 'an_existing_token' }
272
+ let(:arguments) { ['server', 'add', server, server_name, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
273
+ subject{ RHC::ServerWizard.new(config, options, servers) }
274
+ before do
275
+ stub_wizard
276
+ local_config
277
+ stub_servers_yml(2)
278
+ end
279
+ it { run_output.should =~ /Using an existing token for #{username} to login to #{server}/ }
280
+ it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
281
+ it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
282
+ it { expect { run }.to exit_with_code(0) }
283
+ end
284
+
285
+ context "with existing express.conf and servers.yml and adding a new server with port and http scheme" do
286
+ let(:server){ 'http://my.server.com:4000' }
287
+ let(:username){ 'user3' }
288
+ let(:server_name){ 'server3' }
289
+ let(:local_config_server){ 'local.server.com' }
290
+ let(:local_config_username){ 'local_username' }
291
+ let(:token){ 'an_existing_token' }
292
+ let(:arguments) { ['server', 'add', server, server_name, '-l', username, '--use-authorization-tokens', '--no-insecure', '--token', token, '--use'] }
293
+ subject{ RHC::ServerWizard.new(config, options, servers) }
294
+ before do
295
+ stub_wizard
296
+ local_config
297
+ stub_servers_yml(2)
298
+ end
299
+ it { run_output.should =~ /Using an existing token for #{username} to login to #{server}/ }
300
+ it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
301
+ it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
302
+ it { expect { run }.to exit_with_code(0) }
303
+ end
304
+
305
+ context "with existing express.conf and servers.yml and adding a new mock server" do
306
+ let(:server){ 'openshift.server.com' }
307
+ let(:username){ 'user3' }
308
+ let(:server_name){ 'server3' }
309
+ let(:local_config_server){ 'local.server.com' }
310
+ let(:local_config_username){ 'local_username' }
311
+ let(:arguments) { ['server', 'add', server, server_name, '-l', username, '--skip-wizard'] }
312
+ before do
313
+ local_config
314
+ stub_servers_yml(2)
315
+ end
316
+ it { run_output.should_not =~ /Using an existing token for #{username} to login to #{server}/ }
317
+ it { run_output.should_not =~ /Saving configuration to.*express\.conf.*done/ }
318
+ it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
319
+ it { expect { run }.to exit_with_code(0) }
320
+ end
321
+
322
+ context "with existing express.conf and servers.yml and trying to add an existing server" do
323
+ let(:local_config_username){ 'local_config_user' }
324
+ let(:local_config_password){ 'password' }
325
+ let(:local_config_server){ 'openshift.redhat.com' }
326
+ before do
327
+ local_config
328
+ stub_servers_yml(2)
329
+ end
330
+ let(:arguments) { ['server', 'add', 'foo.com', 'server1', '-l', local_config_username, '--use-authorization-tokens', '--no-insecure'] }
331
+ it 'should output correctly' do
332
+ run_output.should =~ /You already have a server configured with the nickname 'server1'/
333
+ end
334
+ it { expect { run }.to exit_with_code(164) }
335
+ end
336
+
337
+ context "with wizard failure" do
338
+ let(:token){ 'an_existing_token' }
339
+ let(:arguments) { ['server', 'add', 'failure.server.com', 'failed', '-l', 'failer'] }
340
+ before do
341
+ RHC::ServerWizard.any_instance.stub(:run).and_return(false)
342
+ stub_servers_yml
343
+ end
344
+ it { expect { run }.to exit_with_code(1) }
345
+ end
37
346
  end
38
347
 
39
- describe 'run' do
40
- let(:arguments) { ['server'] }
41
- before{ rest_client.stub(:auth).and_return(nil) }
348
+ describe "server remove" do
349
+ context "when trying to remove the server in use" do
350
+ let(:local_config_server){ 'local.server.com' }
351
+ let(:local_config_username){ 'local_username' }
352
+ before(:each) do
353
+ stub_servers_yml
354
+ local_config
355
+ end
356
+ let(:arguments) { ['server', 'remove', local_config_server] }
357
+ it 'should output correctly' do
358
+ run_output.should =~ /The '#{local_config_server}' server is in use/
359
+ end
360
+ it { expect { run }.to exit_with_code(167) }
361
+ end
42
362
 
43
- context 'when no issues' do
44
- before { stub_request(:get, 'https://openshift.redhat.com/app/status/status.json').with(&user_agent_header).to_return(:body => {'issues' => []}.to_json) }
45
- it('should output success') { run_output.should =~ /All systems running fine/ }
363
+ context "when removing successfully" do
364
+ let(:server){ 'openshift5.server.com' }
365
+ let(:local_config_server){ 'local.server.com' }
366
+ let(:local_config_username){ 'local_username' }
367
+ before(:each) do
368
+ stub_servers_yml(5)
369
+ local_config
370
+ end
371
+ let(:arguments) { ['server', 'remove', server] }
372
+ it 'should output correctly' do
373
+ run_output.should =~ /Removing '#{server}'.*done/
374
+ end
46
375
  it { expect { run }.to exit_with_code(0) }
47
376
  end
48
377
 
49
- context 'when 1 issue' do
378
+ context "when trying to remove server not configured" do
379
+ let(:local_config_server){ 'local.server.com' }
380
+ let(:local_config_username){ 'local_username' }
381
+ before(:each) do
382
+ stub_servers_yml
383
+ local_config
384
+ end
385
+ let(:arguments) { ['server', 'remove', 'zee'] }
386
+ it 'should output correctly' do
387
+ run_output.should =~ /You don't have any server configured with the hostname or nickname 'zee'/
388
+ end
389
+ it { expect { run }.to exit_with_code(166) }
390
+ end
391
+ end
392
+
393
+ describe "server configure" do
394
+ context "when configuring existing server" do
395
+ let(:server){ 'local.server.com' }
396
+ let(:username){ 'local_username' }
397
+
398
+ let(:local_config_server){ server }
399
+ let(:local_config_username){ username }
400
+
401
+ let(:local_config_server_new_username){ 'new_username' }
402
+ let(:local_config_server_new_name){ 'new_name' }
403
+ let(:token){ 'an_existing_token' }
404
+ let(:arguments) { ['server', 'configure', local_config_server, '--nickname', local_config_server_new_name, '-l', local_config_server_new_username, '--insecure', '--token', token, '--use'] }
405
+ subject{ RHC::ServerWizard.new(config, options, servers) }
406
+ before do
407
+ stub_wizard
408
+ local_config
409
+ stub_servers_yml
410
+ end
411
+ it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
412
+ it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
413
+ it { run_output.should =~ /Using an existing token for #{local_config_server_new_username} to login to #{server}/ }
414
+ it { run_output.should =~ /Server '#{local_config_server_new_name}' \(in use\)/ }
415
+ it { run_output.should =~ /Hostname:\s+#{server}/ }
416
+ it { run_output.should =~ /Login:\s+#{local_config_server_new_username}/ }
417
+ it { run_output.should =~ /Insecure:\s+true/ }
418
+ it { run_output.should =~ /Now using '#{server}'/ }
419
+ it { expect { run }.to exit_with_code(0) }
420
+ end
421
+
422
+ context "with existing express.conf and servers.yml and skipping wizard" do
423
+ let(:server){ 'local.server.com' }
424
+ let(:username){ 'local_username' }
425
+
426
+ let(:local_config_server){ server }
427
+ let(:local_config_username){ username }
428
+
429
+ let(:local_config_server_new_username){ 'new_username' }
430
+ let(:local_config_server_new_name){ 'new_name' }
431
+ let(:arguments) { ['server', 'configure', local_config_server, '--nickname', local_config_server_new_name, '-l', local_config_server_new_username, '--insecure',
432
+ '--skip-wizard'] }
50
433
  before do
51
- stub_request(:get, 'https://openshift.redhat.com/app/status/status.json').with(&user_agent_header).to_return(:body =>
52
- {'open' => [
53
- {'issue' => {
54
- 'created_at' => '2011-05-22T17:31:32-04:00',
55
- 'id' => 11,
56
- 'title' => 'Root cause',
57
- 'updates' => [{
58
- 'created_at' => '2012-05-22T13:48:20-04:00',
59
- 'description' => 'Working on update'
60
- }]
61
- }}]}.to_json)
434
+ local_config
435
+ stub_servers_yml
62
436
  end
437
+ it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
438
+ it { run_output.should_not =~ /Saving configuration to.*express\.conf.*done/ }
439
+ it { run_output.should_not =~ /Using an existing token/ }
440
+ it { run_output.should =~ /Server '#{local_config_server_new_name}' \(in use\)/ }
441
+ it { run_output.should_not =~ /Now using '#{server}'/ }
442
+ it { run_output.should =~ /Login:\s+#{local_config_server_new_username}/ }
443
+ it { expect { run }.to exit_with_code(0) }
444
+ end
445
+
446
+ context "when trying to remove server not found" do
447
+ let(:local_config_server){ 'local.server.com' }
448
+ let(:local_config_username){ 'local_username' }
449
+ before(:each) do
450
+ stub_servers_yml
451
+ local_config
452
+ end
453
+ let(:arguments) { ['server', 'configure', 'zee', '--insecure'] }
454
+ it 'should output correctly' do
455
+ run_output.should =~ /You don't have any server configured with the hostname or nickname 'zee'/
456
+ end
457
+ it { expect { run }.to exit_with_code(166) }
458
+ end
459
+ end
460
+
461
+ describe "server use" do
462
+ context "when using an existing server" do
463
+ let(:server){ 'local.server.com' }
464
+ let(:username){ 'local_user' }
465
+
466
+ let(:local_config_server){ server }
467
+ let(:local_config_username){ username }
468
+
469
+ let(:token){ 'an_existing_token' }
470
+ subject{ RHC::ServerWizard.new(config, options, servers) }
471
+ before do
472
+ stub_wizard
473
+ local_config
474
+ stub_servers_yml(3)
475
+ end
476
+ let(:arguments) { ['server', 'use', 'server3', '--token', token] }
477
+ it { run_output.should =~ /Using an existing token for .* to login to openshift3\.server\.com/ }
478
+ it { run_output.should =~ /Saving server configuration to.*servers\.yml.*done/ }
479
+ it { run_output.should =~ /Saving configuration to.*express\.conf.*done/ }
480
+ it { run_output.should =~ /Now using 'openshift3\.server\.com'/ }
481
+ it { expect { run }.to exit_with_code(0) }
482
+ end
483
+
484
+ context "with wizard failure" do
485
+ let(:server){ 'local.server.com' }
486
+ let(:username){ 'local_user' }
487
+
488
+ let(:local_config_server){ server }
489
+ let(:local_config_username){ username }
490
+
491
+ subject{ RHC::ServerWizard.new(config, options, servers) }
492
+ before do
493
+ RHC::ServerWizard.any_instance.stub(:run).and_return(false)
494
+ local_config
495
+ stub_servers_yml
496
+ end
497
+ let(:arguments) { ['server', 'use', 'local.server.com'] }
63
498
  it { expect { run }.to exit_with_code(1) }
64
- it('should output message') { run_output.should =~ /1 open issue/ }
65
- it('should output title') { run_output.should =~ /Root cause/ }
66
- it('should contain update') { run_output.should =~ /Working on update/ }
67
499
  end
68
500
  end
501
+
502
+ protected
503
+ def stub_servers_yml(entries=1, &block)
504
+ RHC::Servers.any_instance.stub(:save!)
505
+ RHC::Servers.any_instance.stub(:present?).and_return(true)
506
+ RHC::Servers.any_instance.stub(:load).and_return(
507
+ Array(1..entries).collect do |i|
508
+ RHC::Server.new("openshift#{i}.server.com",
509
+ :nickname => "server#{i}",
510
+ :login => "user#{i}",
511
+ :use_authorization_tokens => true,
512
+ :insecure => false)
513
+ end.tap{|i| yield i if block_given?})
514
+ end
515
+
516
+ def stub_wizard
517
+ rest_client.stub(:api)
518
+ rest_client.stub(:user).and_return(double(:login => username))
519
+ rest_client.stub(:supports_sessions?).and_return(true)
520
+ rest_client.stub(:new_session)
521
+ subject.stub(:new_client_for_options).and_return(rest_client)
522
+ end
523
+
69
524
  end