startapp 0.1.6

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 (156) hide show
  1. checksums.yaml +7 -0
  2. data/COPYRIGHT +1 -0
  3. data/LICENSE +11 -0
  4. data/README.md +95 -0
  5. data/Rakefile +6 -0
  6. data/autocomplete/rhc_bash +1672 -0
  7. data/bin/app +37 -0
  8. data/conf/express.conf +8 -0
  9. data/features/assets/deploy.tar.gz +0 -0
  10. data/features/core_feature.rb +191 -0
  11. data/features/deployments_feature.rb +129 -0
  12. data/features/domains_feature.rb +58 -0
  13. data/features/keys_feature.rb +37 -0
  14. data/features/members_feature.rb +166 -0
  15. data/lib/rhc/auth/basic.rb +64 -0
  16. data/lib/rhc/auth/token.rb +102 -0
  17. data/lib/rhc/auth/token_store.rb +53 -0
  18. data/lib/rhc/auth.rb +5 -0
  19. data/lib/rhc/autocomplete.rb +66 -0
  20. data/lib/rhc/autocomplete_templates/bash.erb +39 -0
  21. data/lib/rhc/cartridge_helpers.rb +118 -0
  22. data/lib/rhc/cli.rb +40 -0
  23. data/lib/rhc/command_runner.rb +185 -0
  24. data/lib/rhc/commands/account.rb +25 -0
  25. data/lib/rhc/commands/alias.rb +124 -0
  26. data/lib/rhc/commands/app.rb +726 -0
  27. data/lib/rhc/commands/apps.rb +20 -0
  28. data/lib/rhc/commands/authorization.rb +115 -0
  29. data/lib/rhc/commands/base.rb +174 -0
  30. data/lib/rhc/commands/cartridge.rb +329 -0
  31. data/lib/rhc/commands/clone.rb +66 -0
  32. data/lib/rhc/commands/configure.rb +20 -0
  33. data/lib/rhc/commands/create.rb +100 -0
  34. data/lib/rhc/commands/delete.rb +19 -0
  35. data/lib/rhc/commands/deploy.rb +32 -0
  36. data/lib/rhc/commands/deployment.rb +82 -0
  37. data/lib/rhc/commands/domain.rb +172 -0
  38. data/lib/rhc/commands/env.rb +142 -0
  39. data/lib/rhc/commands/force_stop.rb +17 -0
  40. data/lib/rhc/commands/git_clone.rb +34 -0
  41. data/lib/rhc/commands/logout.rb +51 -0
  42. data/lib/rhc/commands/logs.rb +21 -0
  43. data/lib/rhc/commands/member.rb +148 -0
  44. data/lib/rhc/commands/port_forward.rb +197 -0
  45. data/lib/rhc/commands/reload.rb +17 -0
  46. data/lib/rhc/commands/restart.rb +17 -0
  47. data/lib/rhc/commands/scp.rb +54 -0
  48. data/lib/rhc/commands/server.rb +40 -0
  49. data/lib/rhc/commands/setup.rb +60 -0
  50. data/lib/rhc/commands/show.rb +43 -0
  51. data/lib/rhc/commands/snapshot.rb +137 -0
  52. data/lib/rhc/commands/ssh.rb +51 -0
  53. data/lib/rhc/commands/sshkey.rb +97 -0
  54. data/lib/rhc/commands/start.rb +17 -0
  55. data/lib/rhc/commands/stop.rb +17 -0
  56. data/lib/rhc/commands/tail.rb +47 -0
  57. data/lib/rhc/commands/threaddump.rb +14 -0
  58. data/lib/rhc/commands/tidy.rb +17 -0
  59. data/lib/rhc/commands.rb +396 -0
  60. data/lib/rhc/config.rb +321 -0
  61. data/lib/rhc/context_helper.rb +121 -0
  62. data/lib/rhc/core_ext.rb +202 -0
  63. data/lib/rhc/coverage_helper.rb +33 -0
  64. data/lib/rhc/deployment_helpers.rb +111 -0
  65. data/lib/rhc/exceptions.rb +256 -0
  66. data/lib/rhc/git_helpers.rb +106 -0
  67. data/lib/rhc/help_formatter.rb +55 -0
  68. data/lib/rhc/helpers.rb +481 -0
  69. data/lib/rhc/highline_extensions.rb +479 -0
  70. data/lib/rhc/json.rb +51 -0
  71. data/lib/rhc/output_helpers.rb +260 -0
  72. data/lib/rhc/rest/activation.rb +11 -0
  73. data/lib/rhc/rest/alias.rb +42 -0
  74. data/lib/rhc/rest/api.rb +87 -0
  75. data/lib/rhc/rest/application.rb +348 -0
  76. data/lib/rhc/rest/attributes.rb +36 -0
  77. data/lib/rhc/rest/authorization.rb +8 -0
  78. data/lib/rhc/rest/base.rb +79 -0
  79. data/lib/rhc/rest/cartridge.rb +162 -0
  80. data/lib/rhc/rest/client.rb +650 -0
  81. data/lib/rhc/rest/deployment.rb +18 -0
  82. data/lib/rhc/rest/domain.rb +98 -0
  83. data/lib/rhc/rest/environment_variable.rb +15 -0
  84. data/lib/rhc/rest/gear_group.rb +16 -0
  85. data/lib/rhc/rest/httpclient.rb +145 -0
  86. data/lib/rhc/rest/key.rb +44 -0
  87. data/lib/rhc/rest/membership.rb +105 -0
  88. data/lib/rhc/rest/mock.rb +1042 -0
  89. data/lib/rhc/rest/user.rb +32 -0
  90. data/lib/rhc/rest.rb +148 -0
  91. data/lib/rhc/scp_helpers.rb +27 -0
  92. data/lib/rhc/ssh_helpers.rb +380 -0
  93. data/lib/rhc/tar_gz.rb +51 -0
  94. data/lib/rhc/usage_templates/command_help.erb +51 -0
  95. data/lib/rhc/usage_templates/command_syntax_help.erb +11 -0
  96. data/lib/rhc/usage_templates/help.erb +61 -0
  97. data/lib/rhc/usage_templates/missing_help.erb +1 -0
  98. data/lib/rhc/usage_templates/options_help.erb +12 -0
  99. data/lib/rhc/vendor/okjson.rb +600 -0
  100. data/lib/rhc/vendor/parseconfig.rb +178 -0
  101. data/lib/rhc/vendor/sshkey.rb +253 -0
  102. data/lib/rhc/vendor/zliby.rb +628 -0
  103. data/lib/rhc/version.rb +5 -0
  104. data/lib/rhc/wizard.rb +637 -0
  105. data/lib/rhc.rb +34 -0
  106. data/spec/coverage_helper.rb +82 -0
  107. data/spec/direct_execution_helper.rb +339 -0
  108. data/spec/keys/example.pem +23 -0
  109. data/spec/keys/example_private.pem +27 -0
  110. data/spec/keys/server.pem +19 -0
  111. data/spec/rest_spec_helper.rb +31 -0
  112. data/spec/rhc/assets/cert.crt +22 -0
  113. data/spec/rhc/assets/cert_key_rsa +27 -0
  114. data/spec/rhc/assets/empty.txt +0 -0
  115. data/spec/rhc/assets/env_vars.txt +7 -0
  116. data/spec/rhc/assets/env_vars_2.txt +1 -0
  117. data/spec/rhc/assets/foo.txt +1 -0
  118. data/spec/rhc/assets/targz_corrupted.tar.gz +1 -0
  119. data/spec/rhc/assets/targz_sample.tar.gz +0 -0
  120. data/spec/rhc/auth_spec.rb +442 -0
  121. data/spec/rhc/cli_spec.rb +186 -0
  122. data/spec/rhc/command_spec.rb +435 -0
  123. data/spec/rhc/commands/account_spec.rb +42 -0
  124. data/spec/rhc/commands/alias_spec.rb +333 -0
  125. data/spec/rhc/commands/app_spec.rb +777 -0
  126. data/spec/rhc/commands/apps_spec.rb +39 -0
  127. data/spec/rhc/commands/authorization_spec.rb +157 -0
  128. data/spec/rhc/commands/cartridge_spec.rb +665 -0
  129. data/spec/rhc/commands/clone_spec.rb +41 -0
  130. data/spec/rhc/commands/deployment_spec.rb +327 -0
  131. data/spec/rhc/commands/domain_spec.rb +401 -0
  132. data/spec/rhc/commands/env_spec.rb +493 -0
  133. data/spec/rhc/commands/git_clone_spec.rb +102 -0
  134. data/spec/rhc/commands/logout_spec.rb +86 -0
  135. data/spec/rhc/commands/member_spec.rb +247 -0
  136. data/spec/rhc/commands/port_forward_spec.rb +217 -0
  137. data/spec/rhc/commands/scp_spec.rb +77 -0
  138. data/spec/rhc/commands/server_spec.rb +69 -0
  139. data/spec/rhc/commands/setup_spec.rb +118 -0
  140. data/spec/rhc/commands/snapshot_spec.rb +179 -0
  141. data/spec/rhc/commands/ssh_spec.rb +163 -0
  142. data/spec/rhc/commands/sshkey_spec.rb +188 -0
  143. data/spec/rhc/commands/tail_spec.rb +81 -0
  144. data/spec/rhc/commands/threaddump_spec.rb +84 -0
  145. data/spec/rhc/config_spec.rb +407 -0
  146. data/spec/rhc/helpers_spec.rb +531 -0
  147. data/spec/rhc/highline_extensions_spec.rb +314 -0
  148. data/spec/rhc/json_spec.rb +30 -0
  149. data/spec/rhc/rest_application_spec.rb +258 -0
  150. data/spec/rhc/rest_client_spec.rb +752 -0
  151. data/spec/rhc/rest_spec.rb +740 -0
  152. data/spec/rhc/targz_spec.rb +55 -0
  153. data/spec/rhc/wizard_spec.rb +756 -0
  154. data/spec/spec_helper.rb +575 -0
  155. data/spec/wizard_spec_helper.rb +330 -0
  156. metadata +469 -0
@@ -0,0 +1,1042 @@
1
+ module RHC::Rest::Mock
2
+
3
+ def self.start
4
+ RHC::Helpers.warn "Running in mock mode"
5
+ require 'webmock'
6
+ WebMock.disable_net_connect!
7
+ MockRestClient.class_eval do
8
+ include WebMock::API
9
+ include Helpers
10
+ def user_agent_header
11
+ end
12
+ def user_auth
13
+ {:user => nil, :password => nil}
14
+ end
15
+ end
16
+ MockRestUser.class_eval do
17
+ def add_key(*args)
18
+ attributes['links'] ||= {}
19
+ links['ADD_KEY'] = {'href' => 'https://test.domain.com/broker/rest/user/keys', 'method' => 'POST'}
20
+ super
21
+ end
22
+ end
23
+ MockRestClient.new.tap do |c|
24
+ d = c.add_domain("test1")
25
+ app = d.add_application('app1', 'carttype1')
26
+ app.cartridges[0].display_name = "A display name"
27
+ app.add_cartridge('mockcart2')
28
+ app2 = d.add_application('app2', 'carttype2', true)
29
+ c.stub_add_key_error('test', 'this failed')
30
+ end
31
+ end
32
+
33
+ module Helpers
34
+
35
+ def mock_date_1
36
+ '2013-02-21T01:00:01Z'
37
+ end
38
+
39
+ def mock_user
40
+ "test_user"
41
+ end
42
+
43
+ def mock_user_auth
44
+ respond_to?(:user_auth) ? self.user_auth : {:user => username, :password => password}
45
+ end
46
+
47
+ def credentials_for(with_auth)
48
+ if with_auth == true
49
+ [respond_to?(:username) ? self.username : mock_user, respond_to?(:password) ? self.password : mock_pass]
50
+ elsif with_auth
51
+ with_auth.values_at(:user, :password)
52
+ end
53
+ end
54
+
55
+ def example_allows_members?
56
+ respond_to?(:supports_members?) && supports_members?
57
+ end
58
+ def example_allows_gear_sizes?
59
+ respond_to?(:supports_allowed_gear_sizes?) && supports_allowed_gear_sizes?
60
+ end
61
+
62
+ def expect_authorization(with_auth)
63
+ username, password = credentials_for(with_auth)
64
+ lambda{ |r|
65
+ !username || (r.headers['Authorization'] == "Basic #{["#{username}:#{password}"].pack('m').tr("\n", '')}")
66
+ }
67
+ end
68
+
69
+ def stub_api_request(method, uri, with_auth=true)
70
+ api = stub_request(method, mock_href(uri, with_auth))
71
+ api.with(&lambda{ |r| request.headers['Authorization'] == "Bearer #{with_auth[:token]}" }) if with_auth.respond_to?(:[]) && with_auth[:token]
72
+ api.with(&expect_authorization(with_auth))
73
+ api.with(&user_agent_header)
74
+ if @challenge
75
+ stub_request(method, mock_href(uri, false)).to_return(:status => 401, :headers => {'www-authenticate' => 'basic realm="openshift broker"'})
76
+ end
77
+ api
78
+ end
79
+
80
+ def challenge(&block)
81
+ @challenge = true
82
+ yield
83
+ ensure
84
+ @challenge = false
85
+ end
86
+
87
+ def stub_api(auth=false, authorizations=false)
88
+ stub_api_request(:get, 'broker/rest/api', auth).
89
+ to_return({
90
+ :body => {
91
+ :data => mock_response_links(authorizations ? mock_api_with_authorizations : mock_real_client_links),
92
+ :supported_api_versions => [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6],
93
+ }.to_json
94
+ })
95
+ end
96
+ def stub_api_v12(auth=false)
97
+ stub_api_request(:get, 'broker/rest/api', auth).
98
+ to_return({
99
+ :body => {
100
+ :data => mock_response_links(mock_real_client_links),
101
+ :supported_api_versions => [1.0, 1.1, 1.2],
102
+ }.to_json
103
+ })
104
+ end
105
+ def stub_user(auth=mock_user_auth)
106
+ stub_api_request(:get, 'broker/rest/user', auth).to_return(simple_user(username))
107
+ end
108
+ def stub_add_key(name='default')
109
+ stub_api_request(:post, 'broker/rest/user/keys', mock_user_auth).
110
+ with(:body => hash_including({:name => name, :type => 'ssh-rsa'})).
111
+ to_return({:status => 201, :body => {}.to_json})
112
+ end
113
+ def stub_update_key(name)
114
+ stub_api_request(:put, "broker/rest/user/keys/#{name}", mock_user_auth).
115
+ with(:body => hash_including({:type => 'ssh-rsa'})).
116
+ to_return({:status => 200, :body => {}.to_json})
117
+ end
118
+ def stub_add_key_error(name, message, code=422)
119
+ stub_api_request(:post, "broker/rest/user/keys", mock_user_auth).
120
+ with(:body => hash_including({:name => name, :type => 'ssh-rsa'})).
121
+ to_return({:status => code, :body => {:messages => [
122
+ {:text => message, :field => 'name', :severity => 'error'},
123
+ {:text => "A warning from the server", :field => nil, :severity => 'warning'},
124
+ ]
125
+ }.to_json})
126
+ end
127
+ def stub_create_domain(name)
128
+ stub_api_request(:post, 'broker/rest/domains', mock_user_auth).
129
+ with(:body => hash_including({:id => name})).
130
+ to_return(new_domain(name))
131
+ end
132
+ def stub_authorizations
133
+ stub_api_request(:get, 'broker/rest/user/authorizations', mock_user_auth).
134
+ to_return({
135
+ :status => 200,
136
+ :body => {
137
+ :type => 'authorizations',
138
+ :data => [
139
+ {
140
+ :note => 'an_authorization',
141
+ :token => 'a_token_value',
142
+ :created_at => mock_date_1,
143
+ :expires_in_seconds => 60,
144
+ :scopes => 'session read'
145
+ }
146
+ ]
147
+ }.to_json
148
+ })
149
+ end
150
+ def stub_delete_authorizations
151
+ stub_api_request(:delete, 'broker/rest/user/authorizations', mock_user_auth).
152
+ to_return(:status => 204)
153
+ end
154
+ def stub_delete_authorization(token)
155
+ stub_api_request(:delete, "broker/rest/user/authorizations/#{token}", mock_user_auth).
156
+ to_return(:status => 204)
157
+ end
158
+ def stub_add_authorization(params)
159
+
160
+ stub_request(:get, "https://broker.startapp.bg/broker/rest/api").
161
+ with(:headers => {'Accept'=>'application/json', 'Authorization'=>'Bearer 05bad717f5c73d38d0e9cd9839c3445132b1a9df61dcbc4992e72e2cc6faf06f'}).
162
+ to_return(:status => 200, :body => "", :headers => {})
163
+
164
+ stub_api_request(:post, 'broker/rest/user/authorizations', mock_user_auth).
165
+ with(:body => hash_including(params)).
166
+ to_return(new_authorization(params))
167
+ end
168
+ def stub_no_keys
169
+ stub_api_request(:get, 'broker/rest/user/keys', mock_user_auth).to_return(empty_keys)
170
+ end
171
+ def stub_mock_ssh_keys(name='test')
172
+ stub_api_request(:get, 'broker/rest/user/keys', mock_user_auth).
173
+ to_return({
174
+ :body => {
175
+ :type => 'keys',
176
+ :data => [
177
+ {
178
+ :name => name,
179
+ :type => pub_key.split[0],
180
+ :content => pub_key.split[1],
181
+ # :links => mock_response_links([
182
+ # ['UPDATE', "broker/rest/user/keys/#{name}", 'put']
183
+ # ]),
184
+ }
185
+ ],
186
+ }.to_json
187
+ })
188
+ end
189
+ def stub_one_key(name)
190
+ stub_api_request(:get, 'broker/rest/user/keys', mock_user_auth).
191
+ to_return({
192
+ :body => {
193
+ :type => 'keys',
194
+ :data => [
195
+ {
196
+ :name => name,
197
+ :type => 'ssh-rsa',
198
+ :content => rsa_key_content_public,
199
+ :links => mock_response_links([
200
+ ['UPDATE', "broker/rest/user/keys/#{name}", 'put']
201
+ ]),
202
+ }
203
+ ],
204
+ }.to_json
205
+ })
206
+ end
207
+ def stub_no_domains(with_auth=mock_user_auth)
208
+ stub_api_request(:get, 'broker/rest/domains', with_auth).to_return(empty_domains)
209
+ end
210
+ def stub_one_domain(name, optional_params=nil, with_auth=mock_user_auth)
211
+ stub_api_request(:get, 'broker/rest/domains', with_auth).
212
+ to_return({
213
+ :body => {
214
+ :type => 'domains',
215
+ :data => [{:id => name, :links => mock_response_links(mock_domain_links(name).concat([
216
+ ['LIST_APPLICATIONS', "broker/rest/domains/#{name}/applications", 'get'],
217
+ ['ADD_APPLICATION', "broker/rest/domains/#{name}/applications", 'post', ({:optional_params => optional_params} if optional_params)],
218
+ (['LIST_MEMBERS', "broker/rest/domains/#{name}/members", 'get'] if example_allows_members?),
219
+ (['UPDATE_MEMBERS', "broker/rest/domains/#{name}/members", 'patch'] if example_allows_members?),
220
+ ].compact))}],
221
+ }.to_json
222
+ })
223
+ end
224
+ def stub_one_application(domain_name, name, *args)
225
+ stub_api_request(:get, "broker/rest/domains/#{domain_name}/applications", mock_user_auth).
226
+ to_return({
227
+ :body => {
228
+ :type => 'applications',
229
+ :data => [{
230
+ :domain_id => domain_name,
231
+ :id => 1,
232
+ :name => name,
233
+ :ssh_url => "ssh://12345@#{name}-#{domain_name}.rhcloud.com",
234
+ :app_url => "http://#{name}-#{domain_name}.rhcloud.com",
235
+ :links => mock_response_links([
236
+ ]),
237
+ }],
238
+ }.to_json
239
+ })
240
+ stub_relative_application(domain_name,name, *args)
241
+ end
242
+
243
+ def stub_relative_application(domain_name, app_name, body = {}, status = 200)
244
+ url = client_links['LIST_DOMAINS']['relative'] rescue "broker/rest/domains"
245
+ stub_api_request(:any, "#{url}/#{domain_name}/applications/#{app_name}").
246
+ to_return({
247
+ :body => {
248
+ :type => 'application',
249
+ :data => {
250
+ :domain_id => domain_name,
251
+ :name => app_name,
252
+ :id => 1,
253
+ :links => mock_response_links(mock_app_links(domain_name,app_name)),
254
+ }
255
+ }.merge(body).to_json,
256
+ :status => status
257
+ })
258
+ end
259
+
260
+ def stub_application_cartridges(domain_name, app_name, cartridges, status = 200)
261
+ url = client_links['LIST_DOMAINS']['relative'] rescue "broker/rest/domains"
262
+ stub_api_request(:any, "#{url}/#{domain_name}/applications/#{app_name}/cartridges").
263
+ to_return({
264
+ :body => {
265
+ :type => 'cartridges',
266
+ :data => cartridges.map{ |c| c.is_a?(String) ? {:name => c} : c }.map do |cart|
267
+ cart[:links] ||= mock_response_links(mock_cart_links(domain_name,app_name, cart[:name]))
268
+ cart
269
+ end
270
+ }.to_json,
271
+ :status => status
272
+ })
273
+ end
274
+
275
+ def stub_simple_carts(with_auth=mock_user_auth)
276
+ stub_api_request(:get, 'broker/rest/cartridges', with_auth).to_return(simple_carts)
277
+ end
278
+
279
+ def define_exceptional_test_on_wizard
280
+ RHC::Wizard.module_eval <<-EOM
281
+ private
282
+ def test_and_raise
283
+ raise
284
+ end
285
+ EOM
286
+ end
287
+
288
+ def empty_keys
289
+ empty_response_list('keys')
290
+ end
291
+ def empty_domains
292
+ empty_response_list('domains')
293
+ end
294
+
295
+ def empty_response_list(type)
296
+ {
297
+ :body => {
298
+ :type => type,
299
+ :data => [],
300
+ }.to_json
301
+ }
302
+ end
303
+
304
+ def new_domain(name)
305
+ {
306
+ :status => 201,
307
+ :body => {
308
+ :type => 'domain',
309
+ :data => {
310
+ :id => name,
311
+ :links => mock_response_links([
312
+ ])
313
+ },
314
+ }.to_json
315
+ }
316
+ end
317
+ def simple_carts
318
+ {
319
+ :body => {
320
+ :type => 'cartridges',
321
+ :data => [
322
+ {:name => 'mock_standalone_cart-1', :type => 'standalone', :tags => ['cartridge'], :display_name => 'Mock1 Cart'},
323
+ {:name => 'mock_standalone_cart-2', :type => 'standalone', :description => 'Mock2 description'},
324
+ {:name => 'mock_embedded_cart-1', :type => 'embedded', :tags => ['scheduled'], :display_name => 'Mock1 Embedded Cart'},
325
+ {:name => 'premium_cart-1', :type => 'standalone', :tags => ['premium'], :display_name => 'Premium Cart', :usage_rate_usd => '0.02'},
326
+ ],
327
+ }.to_json
328
+ }
329
+ end
330
+ def simple_user(login)
331
+ {
332
+ :body => {
333
+ :type => 'user',
334
+ :data => {
335
+ :login => login,
336
+ :plan_id => respond_to?(:user_plan_id) ? self.user_plan_id : nil,
337
+ :consumed_gears => respond_to?(:user_consumed_gears) ? self.user_consumed_gears : 0,
338
+ :max_gears => respond_to?(:user_max_gears) ? self.user_max_gears : 3,
339
+ :capabilities => respond_to?(:user_capabilities) ? self.user_capabilities : {:gear_sizes => ['small', 'medium']},
340
+ :links => mock_response_links([
341
+ ['ADD_KEY', "broker/rest/user/keys", 'POST'],
342
+ ['LIST_KEYS', "broker/rest/user/keys", 'GET'],
343
+ ])
344
+ },
345
+ }.to_json
346
+ }
347
+ end
348
+ def new_authorization(params)
349
+ {
350
+ :status => 201,
351
+ :body => {
352
+ :type => 'authorization',
353
+ :data => {
354
+ :note => params[:note],
355
+ :token => 'a_token_value',
356
+ :scopes => (params[:scope] || "userinfo").gsub(/,/, ' '),
357
+ :expires_in => (params[:expires_in] || 60).to_i,
358
+ :expires_in_seconds => (params[:expires_in] || 60).to_i,
359
+ :created_at => mock_date_1,
360
+ },
361
+ }.to_json
362
+ }
363
+ end
364
+
365
+ def mock_pass
366
+ "test pass"
367
+ end
368
+
369
+ def mock_uri
370
+ "test.domain.com"
371
+ end
372
+
373
+ # Creates consistent hrefs for testing
374
+ def mock_href(relative="", with_auth=false)
375
+ server = respond_to?(:server) ? self.server : mock_uri
376
+ user, pass = credentials_for(with_auth)
377
+ uri_string =
378
+ if user
379
+ "#{user}:#{pass}@#{server}"
380
+ else
381
+ server
382
+ end
383
+
384
+ "https://#{uri_string}/#{relative}"
385
+ end
386
+
387
+ def mock_response_links(links)
388
+ link_set = {}
389
+ links.each do |link|
390
+ options = link[3] || {}
391
+ link_set[link[0]] = { 'href' => mock_href(link[1]), 'method' => link[2], 'relative' => link[1]}.merge(options)
392
+ end
393
+ link_set
394
+ end
395
+
396
+ def mock_app_links(domain_id='test_domain',app_id='test_app')
397
+ [['ADD_CARTRIDGE', "domains/#{domain_id}/apps/#{app_id}/carts/add", 'post', {'optional_params' => [{'name' => 'environment_variables'}]} ],
398
+ ['LIST_CARTRIDGES', "broker/rest/domains/#{domain_id}/applications/#{app_id}/cartridges", 'get' ],
399
+ ['GET_GEAR_GROUPS', "domains/#{domain_id}/apps/#{app_id}/gear_groups", 'get' ],
400
+ ['START', "domains/#{domain_id}/apps/#{app_id}/start", 'post'],
401
+ ['STOP', "domains/#{domain_id}/apps/#{app_id}/stop", 'post'],
402
+ ['RESTART', "domains/#{domain_id}/apps/#{app_id}/restart", 'post'],
403
+ ['SCALE_UP', "broker/rest/application/#{app_id}/events", 'scale-up'],
404
+ ['SCALE_DOWN', "broker/rest/application/#{app_id}/events", 'scale-down'],
405
+ ['THREAD_DUMP', "domains/#{domain_id}/apps/#{app_id}/event", 'post'],
406
+ ['ADD_ALIAS', "domains/#{domain_id}/apps/#{app_id}/event", 'post'],
407
+ ['REMOVE_ALIAS', "domains/#{domain_id}/apps/#{app_id}/event", 'post'],
408
+ ['LIST_ALIASES', "domains/#{domain_id}/apps/#{app_id}/aliases", 'get'],
409
+ ['LIST_ENVIRONMENT_VARIABLES', "domains/#{domain_id}/apps/#{app_id}/event", 'post'],
410
+ ['SET_UNSET_ENVIRONMENT_VARIABLES', "domains/#{domain_id}/apps/#{app_id}/event", 'post'],
411
+ ['DELETE', "broker/rest/domains/#{domain_id}/applications/#{app_id}", 'delete'],
412
+ (['LIST_MEMBERS', "domains/#{domain_id}/apps/#{app_id}/members", 'get'] if example_allows_members?),
413
+ ['UPDATE', "broker/rest/domain/#{domain_id}/application/#{app_id}", 'put'],
414
+ ['LIST_DEPLOYMENTS', "broker/rest/domain/#{domain_id}/application/#{app_id}/deployments", 'get' ],
415
+ ['UPDATE_DEPLOYMENTS', "broker/rest/domain/#{domain_id}/application/#{app_id}/deployments", 'post' ],
416
+ ['ACTIVATE', "broker/rest/domain/#{domain_id}/application/#{app_id}/events", 'post'],
417
+ ['DEPLOY', "broker/rest/domain/#{domain_id}/application/#{app_id}/deployments", 'post']
418
+ ].compact
419
+ end
420
+
421
+ def mock_cart_links(domain_id='test_domain',app_id='test_app',cart_id='test_cart')
422
+ [['START', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/start", 'post'],
423
+ ['STOP', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/stop", 'post'],
424
+ ['RESTART', "domains/#{domain_id}/apps/#{app_id}/carts/#{cart_id}/restart", 'post'],
425
+ ['DELETE', "broker/rest/domains/#{domain_id}/applications/#{app_id}/cartridges/#{cart_id}", 'DELETE']]
426
+ end
427
+
428
+ def mock_client_links
429
+ [['GET_USER', 'user/', 'get' ],
430
+ ['ADD_DOMAIN', 'domains/add', 'post'],
431
+ ['LIST_DOMAINS', 'domains/', 'get' ],
432
+ ['LIST_CARTRIDGES', 'cartridges/', 'get' ]]
433
+ end
434
+
435
+ def mock_real_client_links
436
+ [['GET_USER', "broker/rest/user", 'GET'],
437
+ ['LIST_DOMAINS', "broker/rest/domains", 'GET'],
438
+ ['ADD_DOMAIN', "broker/rest/domains", 'POST', ({'optional_params' => [{'name' => 'allowed_gear_sizes'}]} if example_allows_gear_sizes?)].compact,
439
+ ['LIST_CARTRIDGES', "broker/rest/cartridges", 'GET'],
440
+ ]
441
+ end
442
+
443
+ def mock_api_with_authorizations
444
+ mock_real_client_links.concat([
445
+ ['LIST_AUTHORIZATIONS', "broker/rest/user/authorizations", 'GET'],
446
+ ['ADD_AUTHORIZATION', "broker/rest/user/authorizations", 'POST'],
447
+ ['SHOW_AUTHORIZATION', "broker/rest/user/authorizations/:id", 'GET'],
448
+ ])
449
+ end
450
+
451
+ def mock_domain_links(domain_id='test_domain')
452
+ [['ADD_APPLICATION', "domains/#{domain_id}/apps/add", 'post', {'optional_params' => [{'name' => 'environment_variables'}]} ],
453
+ ['LIST_APPLICATIONS', "domains/#{domain_id}/apps/", 'get' ],
454
+ ['UPDATE', "domains/#{domain_id}/update", 'put'],
455
+ ['DELETE', "domains/#{domain_id}/delete", 'post']]
456
+ end
457
+
458
+ def mock_key_links(key_id='test_key')
459
+ [['UPDATE', "user/keys/#{key_id}/update", 'post'],
460
+ ['DELETE', "user/keys/#{key_id}/delete", 'post']]
461
+ end
462
+
463
+ def mock_user_links
464
+ [['ADD_KEY', 'user/keys/add', 'post'],
465
+ ['LIST_KEYS', 'user/keys/', 'get' ]]
466
+ end
467
+
468
+ def mock_alias_links(domain_id='test_domain',app_id='test_app',alias_id='test.foo.com')
469
+ [['DELETE', "domains/#{domain_id}/apps/#{app_id}/aliases/#{alias_id}/delete", 'post'],
470
+ ['GET', "domains/#{domain_id}/apps/#{app_id}/aliases/#{alias_id}", 'get' ],
471
+ ['UPDATE', "domains/#{domain_id}/apps/#{app_id}/aliases/#{alias_id}/update", 'post' ]]
472
+ end
473
+
474
+ def mock_cartridge_response(cart_count=1, url=false)
475
+ carts = []
476
+ while carts.length < cart_count
477
+ carts << {
478
+ :name => "mock_cart_#{carts.length}",
479
+ :url => url ? "http://a.url/#{carts.length}" : nil,
480
+ :type => carts.empty? ? 'standalone' : 'embedded',
481
+ :links => mock_response_links(mock_cart_links('mock_domain','mock_app',"mock_cart_#{carts.length}"))
482
+ }
483
+ end
484
+
485
+ carts = carts[0] if cart_count == 1
486
+ type = cart_count == 1 ? 'cartridge' : 'cartridges'
487
+
488
+ return {
489
+ :body => {
490
+ :type => type,
491
+ :data => carts
492
+ }.to_json,
493
+ :status => 200
494
+ }
495
+ end
496
+
497
+ def mock_alias_response(count=1)
498
+ aliases = count.times.inject([]) do |arr, i|
499
+ arr << {:id => "www.alias#{i}.com"}
500
+ end
501
+
502
+ return {
503
+ :body => {
504
+ :type => count == 1 ? 'alias' : 'aliases',
505
+ :data => aliases
506
+ }.to_json,
507
+ :status => 200
508
+ }
509
+ end
510
+
511
+ def mock_gear_groups_response()
512
+ groups = [{}]
513
+ type = 'gear_groups'
514
+
515
+ return {
516
+ :body => {
517
+ :type => type,
518
+ :data => groups
519
+ }.to_json,
520
+ :status => 200
521
+ }
522
+ end
523
+ end
524
+
525
+ class MockRestClient < RHC::Rest::Client
526
+ include Helpers
527
+
528
+ def initialize(config=RHC::Config, version=1.0)
529
+ obj = self
530
+ if RHC::Rest::Client.respond_to?(:stub)
531
+ RHC::Rest::Client.stub(:new) { obj }
532
+ else
533
+ RHC::Rest::Client.instance_eval do
534
+ @obj = obj
535
+ def new(*args)
536
+ @obj
537
+ end
538
+ end
539
+ end
540
+ @domains = []
541
+ @user = MockRestUser.new(self, config.username)
542
+ @api = MockRestApi.new(self, config)
543
+ @version = version
544
+ end
545
+
546
+ def api
547
+ @api
548
+ end
549
+
550
+ def user
551
+ @user
552
+ end
553
+
554
+ def domains
555
+ @domains
556
+ end
557
+
558
+ def api_version_negotiated
559
+ @version
560
+ end
561
+
562
+ def cartridges
563
+ premium_embedded = MockRestCartridge.new(self, "premium_cart", "embedded")
564
+ premium_embedded.usage_rate = 0.05
565
+
566
+ [MockRestCartridge.new(self, "mock_cart-1", "embedded"), # code should sort this to be after standalone
567
+ MockRestCartridge.new(self, "mock_standalone_cart-1", "standalone"),
568
+ MockRestCartridge.new(self, "mock_standalone_cart-2", "standalone"),
569
+ MockRestCartridge.new(self, "mock_unique_standalone_cart-1", "standalone"),
570
+ MockRestCartridge.new(self, "jenkins-1", "standalone", nil, ['ci']),
571
+ MockRestCartridge.new(self, "mock_cart-2", "embedded"),
572
+ MockRestCartridge.new(self, "unique_mock_cart-1", "embedded"),
573
+ MockRestCartridge.new(self, "jenkins-client-1", "embedded", nil, ['ci_builder']),
574
+ MockRestCartridge.new(self, "embcart-1", "embedded"),
575
+ MockRestCartridge.new(self, "embcart-2", "embedded"),
576
+ premium_embedded
577
+ ]
578
+ end
579
+
580
+ def add_domain(id, extra=false)
581
+ d = MockRestDomain.new(self, id)
582
+ if extra
583
+ d.attributes['creation_time'] = '2013-07-21T15:00:44Z'
584
+ d.attributes['members'] = [{'owner' => true, 'name' => 'a_user_name'}]
585
+ d.attributes['allowed_gear_sizes'] = ['small']
586
+ end
587
+ @domains << d
588
+ d
589
+ end
590
+
591
+ def sshkeys
592
+ @user.keys
593
+ end
594
+
595
+ def add_key(name, type, content)
596
+ @user.add_key(name, type, content)
597
+ end
598
+
599
+ def delete_key(name)
600
+ @user.keys.delete_if { |key| key.name == name }
601
+ end
602
+
603
+ # Need to mock this since we are not registering HTTP requests when adding apps to the mock domain
604
+ def find_application(domain, name, options = {})
605
+ find_domain(domain).applications.each do |app|
606
+ return app if app.name.downcase == name.downcase
607
+ end
608
+
609
+ raise RHC::Rest::ApplicationNotFoundException.new("Application #{name} does not exist")
610
+ end
611
+
612
+ def find_application_gear_groups(domain, name, options = {})
613
+ find_domain(domain).applications.each do |app|
614
+ return app.gear_groups if app.name.downcase == name.downcase
615
+ end
616
+
617
+ raise RHC::Rest::ApplicationNotFoundException.new("Application #{name} does not exist")
618
+ end
619
+
620
+ def find_application_by_id(id, options={})
621
+ @domains.each{ |d| d.applications.each{ |a| return a if a.id == id } }
622
+ raise RHC::Rest::ApplicationNotFoundException.new("Application with id #{id} does not exist")
623
+ end
624
+
625
+ def find_application_by_id_gear_groups(id, options={})
626
+ @domains.each{ |d| d.applications.each{ |a| return a.gear_groups if a.id == id } }
627
+ raise RHC::Rest::ApplicationNotFoundException.new("Application with id #{id} does not exist")
628
+ end
629
+ end
630
+
631
+ class MockRestApi < RHC::Rest::Api
632
+ include Helpers
633
+
634
+ def initialize(client, config)
635
+ @client = client
636
+ @client_api_versions = RHC::Rest::Client::CLIENT_API_VERSIONS
637
+ @server_api_versions = @client_api_versions
638
+ self.attributes = {:links => mock_response_links(mock_client_links)}
639
+ end
640
+ end
641
+
642
+ class MockRestUser < RHC::Rest::User
643
+ include Helpers
644
+ def initialize(client, login)
645
+ super({}, client)
646
+ @login = login
647
+ @keys = [
648
+ MockRestKey.new(client, 'mockkey1', 'ssh-rsa', 'AAAAB3NzaC1yc2EAAAADAQABAAABAQDNK8xT3O+kSltmCMsSqBfAgheB3YFJ9Y0ESJnFjFASVxH70AcCQAgdQSD/r31+atYShJdP7f0AMWiQUTw2tK434XSylnZWEyIR0V+j+cyOPdVQlns6D5gPOnOtweFF0o18YulwCOK8Q1H28GK8qyWhLe0FcMmxtKbbQgaVRvQdXZz4ThzutCJOyJm9xVb93+fatvwZW76oLLvfFJcJSOK2sgW7tJM2A83bm4mwixFDF7wO/+C9WA+PgPKJUIjvy1gZjBhRB+3b58vLOnYhPOgMNruJwzB+wJ3pg8tLJEjxSbHyyoi6OqMBs4BVV7LdzvwTDxEjcgtHVvaVNXgO5iRX'),
649
+ MockRestKey.new(client, 'mockkey2', 'ssh-dsa', 'AAAAB3NzaC1kc3MAAACBAPaaFj6Xjrjd8Dc4AAkJe0HigqaXMxj/87xHoV+nPgerHIceJWhPUWdW40lSASrgpAV9Eq4zzD+L19kgYdbMw0vSX5Cj3XtNOsow9MmMxFsYjTxCv4eSs/rLdGPaYZ5GVRPDu8tN42Bm8lj5o+ky3HzwW+mkQMZwcADQIgqtn6QhAAAAFQCirDfIMf/JoMOFf8CTnsTKWw/0zwAAAIAIQp6t2sLIp1d2TBfd/qLjOJA10rPADcnhBzWB/cd/oFJ8a/2nmxeSPR5Ov18T6itWqbKwvZw2UC0MrXoYbgcfVNP/ym1bCd9rB5hu1sg8WO4JIxA/47PZooT6PwTKVxHuENEzQyJL2o6ZJq+wuV0taLvm6IaM5TAZuEJ2p4TC/gAAAIBpLcVXZREa7XLY55nyidt/+UC+PxpjhPHOHbzL1OvWEaumN4wcJk/JZPppgXX9+WDkTm1SD891U0cXnGMTP0OZOHkOUHF2ZcfUe7p9kX4WjHs0OccoxV0Lny6MC4DjalJyaaEbijJHSUX3QlLcBOlPHJWpEpvWQ9P8AN4PokiGzA=='),
650
+ MockRestKey.new(client, 'mockkey3', 'krb5-principal', 'mockuser@mockdomain')
651
+ ]
652
+ end
653
+
654
+ def keys
655
+ @keys
656
+ end
657
+
658
+ def add_key(name, type, content)
659
+ @keys << MockRestKey.new(client, name, type, content)
660
+ end
661
+ end
662
+
663
+ class MockRestDomain < RHC::Rest::Domain
664
+ include Helpers
665
+ def initialize(client, id)
666
+ super({}, client)
667
+ @name = id
668
+ @applications = []
669
+ self.attributes = {:links => mock_response_links(mock_domain_links(id))}
670
+ end
671
+
672
+ def rename(id)
673
+ @name = id
674
+ self
675
+ end
676
+
677
+ def destroy(force=false)
678
+ raise RHC::Rest::ClientErrorException.new("Applications must be empty.") unless @applications.empty? or force.present?
679
+ client.domains.delete_if { |d| d.name == @name }
680
+
681
+ @applications = nil
682
+ end
683
+
684
+ def add_application(name, type=nil, scale=nil, gear_profile='default', git_url=nil)
685
+ if type.is_a?(Hash)
686
+ scale = type[:scale]
687
+ gear_profile = type[:gear_profile]
688
+ git_url = type[:initial_git_url]
689
+ type = Array(type[:cartridges] || type[:cartridge])
690
+ end
691
+ a = MockRestApplication.new(client, name, type, self, scale, gear_profile, git_url)
692
+ builder = @applications.find{ |app| app.cartridges.map(&:name).any?{ |s| s =~ /^jenkins-[\d\.]+$/ } }
693
+ a.building_app = builder.name if builder
694
+ @applications << a
695
+ a.add_message("Success")
696
+ a
697
+ end
698
+
699
+ def applications(*args)
700
+ @applications
701
+ end
702
+
703
+ def add_member(member)
704
+ (@members ||= []) << member
705
+ (attributes['members'] ||= []) << member.attributes
706
+ self
707
+ end
708
+ end
709
+
710
+ class MockRestGearGroup < RHC::Rest::GearGroup
711
+ include Helpers
712
+ def initialize(client=nil, carts=['name' => 'fake_geargroup_cart-0.1'], count=1)
713
+ super({}, client)
714
+ @cartridges = carts
715
+ @gears = count.times.map do |i|
716
+ {'state' => 'started', 'id' => "fakegearid#{i}", 'ssh_url' => "ssh://fakegearid#{i}@fakesshurl.com"}
717
+ end
718
+ @gear_profile = 'small'
719
+ @base_gear_storage = 1
720
+ end
721
+ end
722
+
723
+ class MockRestAlias < RHC::Rest::Alias
724
+ include Helpers
725
+
726
+ def initialize(client, id, has_private_ssl_certificate=false, certificate_added_at=nil)
727
+ super({}, client)
728
+ @id = id
729
+ @has_private_ssl_certificate = has_private_ssl_certificate
730
+ @certificate_added_at = certificate_added_at
731
+ end
732
+
733
+ def add_certificate(ssl_certificate_content, private_key_content, pass_phrase)
734
+ if (client.api_version_negotiated >= 1.4)
735
+ @has_private_ssl_certificate = true
736
+ @certificate_added_at = Time.now
737
+ else
738
+ raise RHC::Rest::SslCertificatesNotSupported, "The server does not support SSL certificates for custom aliases."
739
+ end
740
+ end
741
+
742
+ def delete_certificate
743
+ if (client.api_version_negotiated >= 1.4)
744
+ @has_private_ssl_certificate = false
745
+ @certificate_added_at = nil
746
+ else
747
+ raise RHC::Rest::SslCertificatesNotSupported, "The server does not support SSL certificates for custom aliases."
748
+ end
749
+ end
750
+
751
+ def destroy
752
+ puts @application.inspect
753
+ puts self.inspect
754
+ @application.aliases.delete self
755
+ end
756
+ end
757
+
758
+ class MockRestApplication < RHC::Rest::Application
759
+ include Helpers
760
+ def fakeuuid
761
+ "fakeuuidfortests#{@name}"
762
+ end
763
+
764
+ def initialize(client, name, type, domain, scale=nil, gear_profile='default', initial_git_url=nil, environment_variables=nil)
765
+ super({}, client)
766
+ @name = name
767
+ @domain = domain
768
+ @cartridges = []
769
+ @creation_time = Date.new(2000, 1, 1).strftime('%Y-%m-%dT%H:%M:%S%z')
770
+ @uuid = fakeuuid
771
+ @initial_git_url = initial_git_url
772
+ @git_url = "git:fake.foo/git/#{@name}.git"
773
+ @app_url = "https://#{@name}-#{@domain.name}.fake.foo/"
774
+ @ssh_url = "ssh://#{@uuid}@127.0.0.1"
775
+ @aliases = []
776
+ @environment_variables = environment_variables || []
777
+ @gear_profile = gear_profile
778
+ @auto_deploy = true
779
+ @keep_deployments = 1
780
+ if scale
781
+ @scalable = true
782
+ end
783
+ self.attributes = {:links => mock_response_links(mock_app_links('mock_domain_0', 'mock_app_0')), :messages => []}
784
+ self.gear_count = 5
785
+ types = Array(type)
786
+ cart = add_cartridge(types.first, false) if types.first
787
+ if scale
788
+ cart.supported_scales_to = (cart.scales_to = -1)
789
+ cart.supported_scales_from = (cart.scales_from = 2)
790
+ cart.current_scale = 2
791
+ cart.scales_with = "haproxy-1.4"
792
+ prox = add_cartridge('haproxy-1.4')
793
+ prox.collocated_with = [types.first]
794
+ end
795
+ types.drop(1).each{ |c| add_cartridge(c, false) }
796
+ @framework = types.first
797
+ end
798
+
799
+ def destroy
800
+ @domain.applications.delete self
801
+ end
802
+
803
+ def add_cartridge(cart, embedded=true, environment_variables=nil)
804
+ name, url =
805
+ if cart.is_a? String
806
+ [cart, nil]
807
+ elsif cart.respond_to? :[]
808
+ [cart[:name] || cart['name'], cart[:url] || cart['url']]
809
+ elsif RHC::Rest::Cartridge === cart
810
+ [cart.name, cart.url]
811
+ end
812
+
813
+ type = embedded ? "embedded" : "standalone"
814
+ c = MockRestCartridge.new(client, name, type, self)
815
+ if url
816
+ c.url = url
817
+ c.name = c.url_basename
818
+ end
819
+ #set_environment_variables(environment_variables)
820
+ c.properties << {'name' => 'prop1', 'value' => 'value1', 'description' => 'description1' }
821
+ @cartridges << c
822
+ c.messages << "Cartridge added with properties"
823
+ c
824
+ end
825
+
826
+ def id
827
+ @uuid || attributes['uuid'] || attributes['id']
828
+ end
829
+
830
+ def gear_groups
831
+ # we don't have heavy interaction with gear groups yet so keep this simple
832
+ @gear_groups ||= begin
833
+ if @scalable
834
+ cartridges.map{ |c| MockRestGearGroup.new(client, [c.name], c.current_scale) if c.name != 'haproxy-1.4' }.compact
835
+ else
836
+ [MockRestGearGroup.new(client, cartridges.map{ |c| {'name' => c.name} }, 1)]
837
+ end
838
+ end
839
+ end
840
+
841
+ def cartridges
842
+ @cartridges
843
+ end
844
+
845
+ def start
846
+ @app
847
+ end
848
+
849
+ def stop(*args)
850
+ @app
851
+ end
852
+
853
+ def restart
854
+ @app
855
+ end
856
+
857
+ def reload
858
+ @app
859
+ end
860
+
861
+ def tidy
862
+ @app
863
+ end
864
+
865
+ def scale_up
866
+ @app
867
+ end
868
+
869
+ def scale_down
870
+ @app
871
+ end
872
+
873
+ def add_alias(app_alias)
874
+ @aliases << MockRestAlias.new(@client, app_alias)
875
+ end
876
+
877
+ def remove_alias(app_alias)
878
+ @aliases.delete_if {|x| x.id == app_alias}
879
+ end
880
+
881
+ def aliases
882
+ @aliases
883
+ end
884
+
885
+ def environment_variables
886
+ if supports? "LIST_ENVIRONMENT_VARIABLES"
887
+ @environment_variables || []
888
+ else
889
+ raise RHC::EnvironmentVariablesNotSupportedException.new
890
+ end
891
+ end
892
+
893
+ def set_environment_variables(env_vars=[])
894
+ if supports? "SET_UNSET_ENVIRONMENT_VARIABLES"
895
+ environment_variables.concat env_vars
896
+ else
897
+ raise RHC::EnvironmentVariablesNotSupportedException.new
898
+ end
899
+ end
900
+
901
+ def unset_environment_variables(env_vars=[])
902
+ if supports? "SET_UNSET_ENVIRONMENT_VARIABLES"
903
+ env_vars.each { |item| environment_variables.delete_if { |env_var| env_var.name == item } }
904
+ else
905
+ raise RHC::EnvironmentVariablesNotSupportedException.new
906
+ end
907
+ end
908
+
909
+ def add_member(member)
910
+ (@members ||= []) << member
911
+ (attributes['members'] ||= []) << member.attributes
912
+ self
913
+ end
914
+
915
+ def configure(options={})
916
+ options.each {|key,value| self.instance_variable_set("@#{key.to_s}", value)}
917
+ end
918
+
919
+ def auto_deploy
920
+ @auto_deploy || false
921
+ end
922
+
923
+ def keep_deployments
924
+ @keep_deployments
925
+ end
926
+
927
+ def deployments
928
+ base_time1 = Time.local(2000,1,1,1,0,0).strftime('%Y-%m-%dT%H:%M:%S%z')
929
+ base_time2 = Time.local(2000,1,1,2,0,0).strftime('%Y-%m-%dT%H:%M:%S%z')
930
+ base_time3 = Time.local(2000,1,1,3,0,0).strftime('%Y-%m-%dT%H:%M:%S%z')
931
+ base_time4 = Time.local(2000,1,1,4,0,0).strftime('%Y-%m-%dT%H:%M:%S%z')
932
+ base_time5 = Time.local(2000,1,1,5,0,0).strftime('%Y-%m-%dT%H:%M:%S%z')
933
+ base_time6 = Time.local(2000,1,1,6,0,0).strftime('%Y-%m-%dT%H:%M:%S%z')
934
+ [
935
+ MockRestDeployment.new(self, '0000001', 'master', '0000001', nil, false, base_time1, false, [base_time1]),
936
+ MockRestDeployment.new(self, '0000002', 'master', '0000002', nil, false, base_time2, false, [base_time2, base_time6]),
937
+ MockRestDeployment.new(self, '0000003', 'master', '0000003', nil, false, base_time3, false, [base_time3, base_time5]),
938
+ MockRestDeployment.new(self, '0000004', 'master', '0000004', nil, false, base_time4, false, [base_time4]),
939
+ MockRestDeployment.new(self, '0000005', 'master', '0000005', nil, false, base_time5, false, [base_time5]),
940
+ ]
941
+ end
942
+ end
943
+
944
+ class MockRestCartridge < RHC::Rest::Cartridge
945
+ include Helpers
946
+
947
+ attr_accessor :usage_rate
948
+
949
+ def initialize(client, name, type, app=nil, tags=[], properties=[{'type' => 'cart_data', 'name' => 'connection_url', 'value' => "http://fake.url" }], description=nil)
950
+ super({}, client)
951
+ @name = name
952
+ @description = description || "Description of #{name}"
953
+ @type = type
954
+ @app = app
955
+ @tags = tags
956
+ @properties = properties.each(&:stringify_keys!)
957
+ @status_messages = [{"message" => "started", "gear_id" => "123"}]
958
+ @scales_from = 1
959
+ @scales_to = 1
960
+ @current_scale = 1
961
+ @gear_profile = 'small'
962
+ @additional_gear_storage = 5
963
+ @usage_rate = 0.0
964
+ end
965
+
966
+ def destroy
967
+ @app.cartridges.delete self
968
+ end
969
+
970
+ def status
971
+ @status_messages
972
+ end
973
+
974
+ def start
975
+ @status_messages = [{"message" => "started", "gear_id" => "123"}]
976
+ @app
977
+ end
978
+
979
+ def stop
980
+ @status_messages = [{"message" => "stopped", "gear_id" => "123"}]
981
+ @app
982
+ end
983
+
984
+ def restart
985
+ @status_messages = [{"message" => "started", "gear_id" => "123"}]
986
+ @app
987
+ end
988
+
989
+ def reload
990
+ @app
991
+ end
992
+
993
+ def set_scales(values)
994
+ values.delete_if{|k,v| v.nil? }
995
+ @scales_from = values[:scales_from] if values[:scales_from]
996
+ @scales_to = values[:scales_to] if values[:scales_to]
997
+ self
998
+ end
999
+
1000
+ def set_storage(values)
1001
+ @additional_gear_storage = values[:additional_gear_storage] if values[:additional_gear_storage]
1002
+ self
1003
+ end
1004
+ end
1005
+
1006
+ class MockRestKey < RHC::Rest::Key
1007
+ include Helpers
1008
+ def initialize(client, name, type, content)
1009
+ super({}, client)
1010
+ @name = name
1011
+ @type = type
1012
+ @content = content
1013
+ end
1014
+ end
1015
+
1016
+ class MockRestDeployment < RHC::Rest::Deployment
1017
+ def initialize(client, id, ref, sha1, artifact_url, hot_deploy, created_at, force_clean_build, activations)
1018
+ super({}, client)
1019
+ @id = id
1020
+ @ref = ref
1021
+ @sha1 = sha1
1022
+ @artifact_url = artifact_url
1023
+ @hot_deploy = hot_deploy
1024
+ @created_at = created_at
1025
+ @force_clean_build = force_clean_build
1026
+ @activations = activations
1027
+ end
1028
+
1029
+ def activations
1030
+ @activations.map{|activation| MockRestActivation.new(client, RHC::Helpers.datetime_rfc3339(activation))}.sort
1031
+ end
1032
+ end
1033
+
1034
+
1035
+ class MockRestActivation < RHC::Rest::Activation
1036
+ def initialize(client, created_at)
1037
+ super({}, client)
1038
+ @created_at = created_at
1039
+ end
1040
+ end
1041
+ end
1042
+