rhc 0.94.8 → 0.95.13

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 (59) hide show
  1. data/README.md +27 -1
  2. data/bin/rhc +15 -23
  3. data/bin/rhc-app +4 -1
  4. data/bin/rhc-chk +3 -0
  5. data/bin/rhc-create-app +3 -0
  6. data/bin/rhc-create-domain +3 -0
  7. data/bin/rhc-ctl-app +3 -0
  8. data/bin/rhc-ctl-domain +3 -0
  9. data/bin/rhc-domain +5 -2
  10. data/bin/rhc-domain-info +3 -0
  11. data/bin/rhc-port-forward +16 -18
  12. data/bin/rhc-snapshot +3 -0
  13. data/bin/rhc-sshkey +3 -0
  14. data/bin/rhc-tail-files +3 -0
  15. data/bin/rhc-user-info +1 -0
  16. data/features/README.md +70 -0
  17. data/features/lib/rhc_helper/app.rb +124 -0
  18. data/features/lib/rhc_helper/cartridge.rb +72 -0
  19. data/features/lib/rhc_helper/commandify.rb +154 -0
  20. data/features/lib/rhc_helper/domain.rb +50 -0
  21. data/features/lib/rhc_helper/httpify.rb +107 -0
  22. data/features/lib/rhc_helper/loggable.rb +39 -0
  23. data/features/lib/rhc_helper/persistable.rb +38 -0
  24. data/features/lib/rhc_helper/runnable.rb +41 -0
  25. data/features/lib/rhc_helper.rb +7 -0
  26. data/features/step_definitions/application_steps.rb +99 -0
  27. data/features/step_definitions/cartridge_steps.rb +42 -0
  28. data/features/step_definitions/client_steps.rb +32 -0
  29. data/features/step_definitions/domain_steps.rb +19 -0
  30. data/features/support/env.rb +99 -0
  31. data/features/verify.feature +123 -0
  32. data/lib/rhc/cli.rb +4 -1
  33. data/lib/rhc/commands/base.rb +28 -6
  34. data/lib/rhc/commands/server.rb +4 -1
  35. data/lib/rhc/commands/setup.rb +24 -0
  36. data/lib/rhc/commands.rb +10 -5
  37. data/lib/rhc/config.rb +90 -21
  38. data/lib/rhc/core_ext.rb +11 -2
  39. data/lib/rhc/coverage_helper.rb +35 -0
  40. data/lib/rhc/help_formatter.rb +30 -0
  41. data/lib/rhc/helpers.rb +41 -5
  42. data/lib/rhc/ssh_key_helpers.rb +72 -0
  43. data/lib/rhc/targz.rb +2 -8
  44. data/lib/rhc/wizard.rb +75 -58
  45. data/lib/rhc-common.rb +20 -13
  46. data/lib/rhc-rest.rb +3 -11
  47. data/spec/coverage_helper.rb +51 -0
  48. data/spec/rest_spec_helper.rb +86 -0
  49. data/spec/rhc/cli_spec.rb +19 -3
  50. data/spec/rhc/commands/server_spec.rb +2 -2
  51. data/spec/rhc/common_spec.rb +49 -0
  52. data/spec/rhc/config_spec.rb +328 -0
  53. data/spec/rhc/helpers_spec.rb +74 -1
  54. data/spec/rhc/rest_client_spec.rb +402 -0
  55. data/spec/rhc/rest_spec.rb +454 -0
  56. data/spec/rhc/targz_spec.rb +13 -0
  57. data/spec/rhc/wizard_spec.rb +305 -43
  58. data/spec/spec_helper.rb +30 -25
  59. metadata +124 -5
@@ -0,0 +1,86 @@
1
+ require 'webmock/rspec'
2
+
3
+ Spec::Matchers.define :have_same_attributes_as do |expected|
4
+ match do |actual|
5
+ (actual.instance_variables == expected.instance_variables) &&
6
+ (actual.instance_variables.map { |i| instance_variable_get(i) } ==
7
+ expected.instance_variables.map { |i| instance_variable_get(i) })
8
+ end
9
+ end
10
+
11
+ module RestSpecHelper
12
+ def mock_user
13
+ "test_user"
14
+ end
15
+
16
+ def mock_pass
17
+ "test pass"
18
+ end
19
+
20
+ def mock_uri
21
+ "test.domain.com"
22
+ end
23
+
24
+ # Creates consistent hrefs for testing
25
+ def mock_href(relative="", with_auth=false)
26
+ uri_string = mock_uri
27
+ if (with_auth == true)
28
+ uri_string = mock_user + ":" + mock_pass + "@" + mock_uri
29
+ end
30
+ "https://#{uri_string}/#{relative}"
31
+ end
32
+
33
+ # This formats link lists for JSONification
34
+ def mock_response_links(links)
35
+ link_set = {}
36
+ links.each do |link|
37
+ operation = link[0]
38
+ href = link[1]
39
+ method = link[2]
40
+ # Note that the 'relative' key/value pair below is a convenience for testing;
41
+ # this is not used by the API classes.
42
+ link_set[operation] = { 'href' => mock_href(href), 'method' => method, 'relative' => href }
43
+ end
44
+ return link_set
45
+ end
46
+
47
+ def mock_app_links(domain_id='test_domain',app_id='test_app')
48
+ [['ADD_CARTRIDGE', "domains/#{domain_id}/apps/#{app_id}/carts/add", 'post'],
49
+ ['LIST_CARTRIDGES', "domains/#{domain_id}/apps/#{app_id}/carts/", 'get' ],
50
+ ['START', "domains/#{domain_id}/apps/#{app_id}/start", 'post'],
51
+ ['STOP', "domains/#{domain_id}/apps/#{app_id}/stop", 'post'],
52
+ ['RESTART', "domains/#{domain_id}/apps/#{app_id}/restart", 'post'],
53
+ ['DELETE', "domains/#{domain_id}/apps/#{app_id}/delete", 'post']]
54
+ end
55
+
56
+ def mock_cart_links(domain_id='test_domain',app_id='test_app',cart_id='test_cart')
57
+ [['START', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/start", 'post'],
58
+ ['STOP', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/stop", 'post'],
59
+ ['RESTART', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/restart", 'post'],
60
+ ['DELETE', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/delete", 'post']]
61
+ end
62
+
63
+ def mock_client_links
64
+ [['GET_USER', 'user/', 'get' ],
65
+ ['ADD_DOMAIN', 'domains/add', 'post'],
66
+ ['LIST_DOMAINS', 'domains/', 'get' ],
67
+ ['LIST_CARTRIDGES', 'cartridges/', 'get' ]]
68
+ end
69
+
70
+ def mock_domain_links(domain_id='test_domain')
71
+ [['ADD_APPLICATION', "domains/#{domain_id}/apps/add", 'post'],
72
+ ['LIST_APPLICATIONS', "domains/#{domain_id}/apps/", 'get' ],
73
+ ['UPDATE', "domains/#{domain_id}/update", 'post'],
74
+ ['DELETE', "domains/#{domain_id}/delete", 'post']]
75
+ end
76
+
77
+ def mock_key_links(key_id='test_key')
78
+ [['UPDATE', "user/keys/#{key_id}/update", 'post'],
79
+ ['DELETE', "user/keys/#{key_id}/delete", 'post']]
80
+ end
81
+
82
+ def mock_user_links
83
+ [['ADD_KEY', 'user/keys/add', 'post'],
84
+ ['LIST_KEYS', 'user/keys/', 'get' ]]
85
+ end
86
+ end
data/spec/rhc/cli_spec.rb CHANGED
@@ -2,6 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  describe RHC::CLI do
4
4
 
5
+ shared_examples_for 'a help page' do
6
+ let(:arguments) { @arguments or raise "no arguments" }
7
+ it('should contain the program description') { run_output.should =~ /Command line interface for OpenShift/ }
8
+ it('should contain the global options') { run_output.should =~ /Global Options:/ }
9
+ it('should provide a --config switch') { run_output.should =~ /\-\-config FILE/ }
10
+ end
11
+
5
12
  describe '#start' do
6
13
  context 'with no arguments' do
7
14
  let(:arguments) { [] }
@@ -10,9 +17,18 @@ describe RHC::CLI do
10
17
  end
11
18
 
12
19
  context 'with --help' do
13
- let(:arguments) { ['--help'] }
14
- it { expect { run }.should exit_with_code(0) }
15
- it('should contain the program description') { run_output.should =~ /Command line interface for OpenShift/ }
20
+ before(:each){ @arguments = ['--help'] }
21
+ it_should_behave_like 'a help page'
22
+ end
23
+
24
+ context 'with -h' do
25
+ before(:each){ @arguments = ['-h'] }
26
+ it_should_behave_like 'a help page'
27
+ end
28
+
29
+ context 'with help' do
30
+ before(:each){ @arguments = ['help'] }
31
+ it_should_behave_like 'a help page'
16
32
  end
17
33
  end
18
34
 
@@ -4,7 +4,7 @@ require 'rhc/config'
4
4
 
5
5
  describe RHC::Commands::Server do
6
6
  before(:each) do
7
- RHC::Config.initialize
7
+ RHC::Config.set_defaults
8
8
  end
9
9
 
10
10
  describe 'run' do
@@ -12,8 +12,8 @@ describe RHC::Commands::Server do
12
12
 
13
13
  context 'when no issues' do
14
14
  before { stub_request(:get, 'https://openshift.redhat.com/app/status/status.json').to_return(:body => {'issues' => []}.to_json) }
15
- it { expect { run }.should exit_with_code(0) }
16
15
  it('should output success') { run_output.should =~ /All systems running fine/ }
16
+ it { expect { run }.should exit_with_code(0) }
17
17
  end
18
18
 
19
19
  context 'when 1 issue' do
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'rest_spec_helper'
3
+ require 'rhc-common'
4
+
5
+ # The existence of this file is a stopgap to provide test coverage
6
+ # for a specific bug fix (BZ836882). This test should be migrated
7
+ # to a more appropriate location when rhc-common is refactored out
8
+ # of existence.
9
+
10
+ describe RHC do
11
+ let(:client_links) { mock_response_links(mock_client_links) }
12
+ let(:domain_links) { mock_response_links(mock_domain_links('mock_domain')) }
13
+ let(:user_info) {
14
+ { 'user_info' => { 'domains' => [ { 'namespace' => 'mock_domain' } ] } }
15
+ }
16
+ context "#create_app" do
17
+ context " creating a scaling app" do
18
+ before do
19
+ stub_request(:get, mock_href('broker/rest/api', true)).
20
+ to_return({ :body => { :data => client_links }.to_json,
21
+ :status => 200
22
+ })
23
+ stub_request(:any, mock_href(client_links['LIST_DOMAINS']['relative'], true)).
24
+ to_return({ :body => {
25
+ :type => 'domains',
26
+ :data =>
27
+ [{ :id => 'mock_domain',
28
+ :links => domain_links,
29
+ }]
30
+ }.to_json,
31
+ :status => 200
32
+ })
33
+ stub_request(:any, mock_href(domain_links['ADD_APPLICATION']['relative'], true)).
34
+ to_raise(Rhc::Rest::ServerErrorException.new("Mock server error"))
35
+ RHC.stub!(:print_response_err) { |output| @test_output = output; exit 1 }
36
+ end
37
+ it "posts an error message if the Rest API encounters a server error" do
38
+ lambda{ RHC.create_app( mock_uri, Net::HTTP, user_info,
39
+ 'mock_app', 'mock_type', mock_user, mock_pass,
40
+ nil, false, false, false, 'small', true) }.
41
+ should raise_error(SystemExit)
42
+ @test_output.body.should match(/Mock server error/)
43
+ end
44
+ after do
45
+ RHC.unstub!(:print_response_err)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,328 @@
1
+ require 'spec_helper'
2
+ require 'rhc/config'
3
+ require 'net/http'
4
+
5
+ describe RHC::Config do
6
+ before(:all) do
7
+ ENV['LIBRA_SERVER'] = nil
8
+ ENV['http_proxy'] = nil
9
+ mock_terminal
10
+ FakeFS.activate!
11
+ end
12
+
13
+ after(:all) do
14
+ FakeFS::FileSystem.clear
15
+ FakeFS.deactivate!
16
+ end
17
+
18
+ context "Config default values with no files" do
19
+ before(:each) do
20
+ RHC::Config.initialize
21
+ end
22
+
23
+ it "should not have any configs" do
24
+ RHC::Config.has_global_config?.should be_false
25
+ RHC::Config.has_local_config?.should be_false
26
+ RHC::Config.has_opts_config?.should be_false
27
+ end
28
+
29
+ it "should suggest I run the wizard" do
30
+ RHC::Config.should_run_wizard?.should be_true
31
+ end
32
+
33
+ it "should return openshift.redhat.com for the server" do
34
+ RHC::Config['libra_server'].should == "openshift.redhat.com"
35
+ RHC::Config.default_rhlogin.should be_nil
36
+ RHC::Config.config_user("default@redhat.com")
37
+ RHC::Config.default_rhlogin.should == "default@redhat.com"
38
+ end
39
+ end
40
+
41
+ context "Config values with /etc/openshift/express.conf" do
42
+ it "should have only a global config" do
43
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, "global.openshift.redhat.com", "global@redhat.com")
44
+ RHC::Config.initialize
45
+ RHC::Config.has_global_config?.should be_true
46
+ RHC::Config.has_local_config?.should be_false
47
+ RHC::Config.has_opts_config?.should be_false
48
+ end
49
+
50
+ it "should get values from the global config" do
51
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, "global.openshift.redhat.com",
52
+ "global@redhat.com",
53
+ {"random_value" => 12})
54
+ RHC::Config.initialize
55
+
56
+ RHC::Config['libra_server'].should == "global.openshift.redhat.com"
57
+ RHC::Config.default_rhlogin.should == "global@redhat.com"
58
+ RHC::Config['random_value'].should == "12"
59
+ RHC::Config['non_value'].should be_nil
60
+
61
+ end
62
+
63
+ it "should have libra_server fallback to the default if not set in config" do
64
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, nil,
65
+ "global@redhat.com")
66
+ RHC::Config.initialize
67
+
68
+ RHC::Config['libra_server'].should == "openshift.redhat.com"
69
+ RHC::Config.default_rhlogin.should == "global@redhat.com"
70
+ end
71
+ end
72
+
73
+ context "Config values with ~/.openshift/express.conf" do
74
+ it "should have global and local config" do
75
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, "global.openshift.redhat.com",
76
+ "global@redhat.com")
77
+ ConfigHelper.write_out_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'),
78
+ "local.openshift.redhat.com","local@redhat.com")
79
+ RHC::Config.initialize
80
+ RHC::Config.home_dir = ConfigHelper.home_dir
81
+
82
+ RHC::Config.home_conf_path.should == File.join(ConfigHelper.home_dir, '.openshift')
83
+ RHC::Config.local_config_path.should == File.join(ConfigHelper.home_dir, '.openshift', 'express.conf')
84
+ RHC::Config.has_global_config?.should be_true
85
+ RHC::Config.has_local_config?.should be_true
86
+ RHC::Config.has_opts_config?.should be_false
87
+ end
88
+
89
+ it "should get values from local config" do
90
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, "global.openshift.redhat.com",
91
+ "global@redhat.com",
92
+ {"random_value" => "12"})
93
+ ConfigHelper.write_out_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'),
94
+ "local.openshift.redhat.com",
95
+ "local@redhat.com",
96
+ {"random_value" => 11})
97
+ RHC::Config.initialize
98
+ RHC::Config.home_dir = ConfigHelper.home_dir
99
+
100
+ RHC::Config['libra_server'].should == "local.openshift.redhat.com"
101
+ RHC::Config.default_rhlogin.should == "local@redhat.com"
102
+ RHC::Config['random_value'].should == "11"
103
+ RHC::Config['non_value'].should be_nil
104
+ end
105
+
106
+ it "should fallback to the default or global if not set in config" do
107
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, nil,
108
+ "global@redhat.com")
109
+ ConfigHelper.write_out_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'),
110
+ nil,
111
+ nil,
112
+ {"random_value" => 11})
113
+ RHC::Config.initialize
114
+ RHC::Config.home_dir = ConfigHelper.home_dir
115
+
116
+ RHC::Config['libra_server'].should == "openshift.redhat.com"
117
+ RHC::Config.default_rhlogin.should == "global@redhat.com"
118
+ RHC::Config['random_value'].should == "11"
119
+ end
120
+
121
+ end
122
+
123
+ context "Config values with LIBRA_SERVER ENV set" do
124
+ it "should get values from local config" do
125
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, "global.openshift.redhat.com",
126
+ "global@redhat.com",
127
+ {"random_value" => "12"})
128
+ ConfigHelper.write_out_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'),
129
+ "local.openshift.redhat.com",
130
+ "local@redhat.com",
131
+ {"random_value" => 11})
132
+ ENV['LIBRA_SERVER'] = "env.openshift.redhat.com"
133
+ RHC::Config.initialize
134
+ RHC::Config.set_local_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'))
135
+
136
+ RHC::Config['libra_server'].should == "env.openshift.redhat.com"
137
+ RHC::Config.default_rhlogin.should == "local@redhat.com"
138
+ RHC::Config['random_value'].should == "11"
139
+ RHC::Config['non_value'].should be_nil
140
+ end
141
+ end
142
+
143
+ context "Config values with options set" do
144
+ it "should have global and local config" do
145
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, "global.openshift.redhat.com",
146
+ "global@redhat.com")
147
+ ConfigHelper.write_out_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'),
148
+ "local.openshift.redhat.com","local@redhat.com")
149
+ ConfigHelper.write_out_config(ConfigHelper.opts_config_path,
150
+ "opts.openshift.redhat.com",
151
+ "opts@redhat.com")
152
+ RHC::Config.initialize
153
+ RHC::Config.home_dir = ConfigHelper.home_dir
154
+ RHC::Config.check_cpath({"config" => ConfigHelper.opts_config_path,
155
+ "random_val" => "ok"})
156
+
157
+ RHC::Config.has_global_config?.should be_true
158
+ RHC::Config.has_local_config?.should be_true
159
+ RHC::Config.has_opts_config?.should be_true
160
+ end
161
+
162
+ it "should get values from local config" do
163
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, "global.openshift.redhat.com",
164
+ "global@redhat.com",
165
+ {"random_value" => "12"})
166
+ ConfigHelper.write_out_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'),
167
+ "local.openshift.redhat.com",
168
+ "local@redhat.com",
169
+ {"random_value" => 11})
170
+ ConfigHelper.write_out_config(ConfigHelper.opts_config_path,
171
+ "opts.openshift.redhat.com",
172
+ "opts@redhat.com",
173
+ {"random_value" => 10})
174
+ RHC::Config.initialize
175
+ RHC::Config.home_dir = ConfigHelper.home_dir
176
+ RHC::Config.check_cpath({"config" => ConfigHelper.opts_config_path,
177
+ "random_val" => "ok"})
178
+
179
+ RHC::Config['libra_server'].should == "opts.openshift.redhat.com"
180
+ RHC::Config.default_rhlogin.should == "opts@redhat.com"
181
+ RHC::Config['random_value'].should == "10"
182
+ RHC::Config['non_value'].should be_nil
183
+ end
184
+
185
+ it "should fallback to the default or global or local if not set in config" do
186
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path, nil,
187
+ "global@redhat.com")
188
+ ConfigHelper.write_out_config(File.join(ConfigHelper.home_dir,'.openshift', 'express.conf'),
189
+ nil,
190
+ nil,
191
+ {"random_value" => 11,
192
+ "local_value" => "local"})
193
+ ConfigHelper.write_out_config(ConfigHelper.opts_config_path,
194
+ nil,
195
+ nil,
196
+ {"random_value" => 10})
197
+ RHC::Config.initialize
198
+ RHC::Config.home_dir = ConfigHelper.home_dir
199
+ RHC::Config.check_cpath({"config" => ConfigHelper.opts_config_path,
200
+ "random_val" => "ok"})
201
+
202
+ RHC::Config['libra_server'].should == "openshift.redhat.com"
203
+ RHC::Config.default_rhlogin.should == "global@redhat.com"
204
+ RHC::Config['random_value'].should == "10"
205
+ RHC::Config['local_value'].should == "local"
206
+ end
207
+ end
208
+
209
+ context "Debug options" do
210
+ after(:all) do
211
+ FakeFS::FileSystem.clear
212
+ end
213
+
214
+ it "should show debug as false because nothing is set" do
215
+ ConfigHelper.check_legacy_debug({}).should be_false
216
+ end
217
+
218
+ it "should show debug as 'true' because config is set" do
219
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path,
220
+ nil,
221
+ nil,
222
+ {"debug" => "true"})
223
+ RHC::Config.initialize
224
+ ConfigHelper.check_legacy_debug({}).should == "true"
225
+ end
226
+
227
+ it "should show debug as false because config is set" do
228
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path,
229
+ nil,
230
+ nil,
231
+ {"debug" => "false"})
232
+ RHC::Config.initialize
233
+ ConfigHelper.check_legacy_debug({}).should be_false
234
+ end
235
+
236
+ it "should show debug as true because config is set" do
237
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path,
238
+ nil,
239
+ nil,
240
+ {"debug" => "true"})
241
+ RHC::Config.initialize
242
+ ConfigHelper.check_legacy_debug({"debug" => false}).should be_true
243
+ end
244
+
245
+ it "should show debug as true because opt is set" do
246
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path,
247
+ nil,
248
+ nil,
249
+ {"debug" => "false"})
250
+ RHC::Config.initialize
251
+ ConfigHelper.check_legacy_debug({"debug" => true}).should be_true
252
+ end
253
+ end
254
+
255
+ context "Odds and ends including error conditions" do
256
+ it "should return a direct http connection" do
257
+ proxy = RHC::Config.default_proxy
258
+ proxy.should == Net::HTTP
259
+ end
260
+
261
+ it "should retrun a proxy http connection" do
262
+ RHC::Config.initialize
263
+ ENV['http_proxy'] = "fakeproxy.foo:8080"
264
+ proxy = RHC::Config.default_proxy
265
+ # returns a generic class so we check to make sure it is not a
266
+ # Net::HTTP class and rely on simplecov to make sure the proxy
267
+ # code path was run
268
+ proxy.should_not == Net::HTTP
269
+ end
270
+
271
+ it "should exit if config file can't be read" do
272
+ ConfigHelper.write_out_config(ConfigHelper.global_config_path,
273
+ "global.openshift.redhat.com",
274
+ "global@redhat.com")
275
+ RHC::Config.initialize
276
+ RHC::Vendor::ParseConfig.stub(:new) { raise Errno::EACCES.new("Fake can't read file") }
277
+ RHC::Config.stub(:exit) { |code| code }
278
+
279
+ RHC::Config.check_cpath({"config" => "fake.conf"}).should == 253
280
+
281
+ # write out config file so it exists but is not readable
282
+ ConfigHelper.write_out_config("fake.conf",
283
+ "global.openshift.redhat.com",
284
+ "global@redhat.com")
285
+
286
+ RHC::Config.read_config_files.should == 253
287
+ RHC::Config.set_local_config("fake.conf").should == 253
288
+ RHC::Config.set_opts_config("fake.conf").should == 253
289
+ end
290
+ end
291
+ end
292
+
293
+ class ConfigHelper
294
+ @@global_config_path = '/etc/openshift/express.conf'
295
+ @@home_dir = '/home/mock_user'
296
+ @@opts_config_path = File.join(@@home_dir, "my.conf")
297
+
298
+ def self.global_config_path
299
+ @@global_config_path
300
+ end
301
+
302
+ def self.home_dir
303
+ @@home_dir
304
+ end
305
+
306
+ def self.opts_config_path
307
+ @@opts_config_path
308
+ end
309
+
310
+ def self.check_legacy_debug(opts)
311
+ # this simulates how the old rhc code checked for debug
312
+ # in the future this should all be filtered through the Config module
313
+ # and an app should just have to use RHC::Config.debug?
314
+ debug = RHC::Config['debug'] == 'false' ? nil : RHC::Config['debug']
315
+ debug = true if opts.has_key? 'debug'
316
+ debug
317
+ end
318
+
319
+ def self.write_out_config(config_path, server, login, other={})
320
+ FileUtils.mkdir_p File.dirname(config_path)
321
+ File.open(config_path, "w") do |f|
322
+ f.write "# This is a test file\n\n"
323
+ f.write("libra_server = #{server}\n") unless server.nil?
324
+ f.write("default_rhlogin = #{login}\n\n") unless login.nil?
325
+ other.each { |key, value| f.write("#{key}=#{value}\n") }
326
+ end
327
+ end
328
+ end
@@ -1,23 +1,70 @@
1
1
  require 'spec_helper'
2
2
  require 'rhc/helpers'
3
+ require 'rhc/ssh_key_helpers'
3
4
  require 'rhc/core_ext'
4
5
  require 'highline/import'
5
6
  require 'rhc/config'
7
+ require 'date'
6
8
 
7
9
  describe RHC::Helpers do
8
10
  before(:each) do
9
11
  mock_terminal
10
- RHC::Config.initialize
12
+ RHC::Config.set_defaults
11
13
  @tests = HelperTests.new()
12
14
  end
13
15
 
14
16
  subject do
15
17
  Class.new(Object) do
16
18
  include RHC::Helpers
19
+
20
+ def config
21
+ @config ||= RHC::Config
22
+ end
17
23
  end.new
18
24
  end
19
25
 
20
26
  its(:openshift_server) { should == 'openshift.redhat.com' }
27
+ its(:openshift_url) { should == 'https://openshift.redhat.com' }
28
+
29
+
30
+ it("should pluralize many") { subject.pluralize(3, 'fish').should == '3 fishs' }
31
+ it("should not pluralize one") { subject.pluralize(1, 'fish').should == '1 fish' }
32
+
33
+ it("should decode json"){ subject.decode_json("{\"a\" : 1}").should == {'a' => 1} }
34
+
35
+ it("should output green on success") do
36
+ capture{ subject.success 'this is green' }.should == "\e[32mthis is green\e[0m"
37
+ end
38
+ it("should output yellow on warn") do
39
+ capture{ subject.success 'this is yellow' }.should == "\e[32mthis is yellow\e[0m"
40
+ end
41
+ it("should return true on success"){ subject.success('anything').should be_true }
42
+ it("should return true on success"){ subject.warn('anything').should be_true }
43
+
44
+ it("should draw a table") do
45
+ subject.table([[10,2], [3,40]]) do |i|
46
+ i.map(&:to_s)
47
+ end.should == ['10 2','3 40']
48
+ end
49
+
50
+ it "should parse an RFC3339 date" do
51
+ d = subject.datetime_rfc3339('2012-06-24T20:48:20-04:00')
52
+ d.day.should == 24
53
+ d.month.should == 6
54
+ d.year.should == 2012
55
+ end
56
+
57
+ context 'using the current time' do
58
+ let(:now){ Time.now }
59
+ let(:rfc3339){ '%Y-%m-%dT%H:%M:%S%z' }
60
+ it("should output the time for a date that is today") do
61
+ subject.date(now.strftime(rfc3339)).should =~ /^[0-9]/
62
+ end
63
+ it("should output the year for a date that is not this year") do
64
+ older = now - 1*365*24*60
65
+ subject.date(older.strftime(rfc3339)).should =~ /^[A-Z]/
66
+ end
67
+ end
21
68
 
22
69
  context 'with LIBRA_SERVER environment variable' do
23
70
  before do
@@ -26,9 +73,18 @@ describe RHC::Helpers do
26
73
  RHC::Config.initialize
27
74
  end
28
75
  its(:openshift_server) { should == 'test.com' }
76
+ its(:openshift_url) { should == 'https://test.com' }
29
77
  after { ENV['LIBRA_SERVER'] = nil }
30
78
  end
31
79
 
80
+ context "without RHC::Config" do
81
+ subject do
82
+ Class.new(Object){ include RHC::Helpers }.new
83
+ end
84
+
85
+ it("should raise on config"){ expect{ subject.config }.should raise_error }
86
+ end
87
+
32
88
  context "Formatter" do
33
89
  it "should print out a section without any line breaks" do
34
90
  @tests.section_no_breaks
@@ -82,8 +138,25 @@ describe RHC::Helpers do
82
138
  end
83
139
  end
84
140
 
141
+ context "SSH Key Helpers" do
142
+ before do
143
+ FakeFS.activate!
144
+ end
145
+
146
+ it "should generate an ssh key then return nil when it tries to create another" do
147
+ @tests.generate_ssh_key_ruby.should match("\.ssh/id_rsa\.pub")
148
+ @tests.generate_ssh_key_ruby == nil
149
+ end
150
+
151
+ after do
152
+ FakeFS::FileSystem.clear
153
+ FakeFS.deactivate!
154
+ end
155
+ end
156
+
85
157
  class HelperTests
86
158
  include RHC::Helpers
159
+ include RHC::SSHKeyHelpers
87
160
 
88
161
  def initialize
89
162
  @print_num = 0