pbox 1.17.2

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