gaptool-server 0.7.4 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/api_test.rb CHANGED
@@ -2,13 +2,12 @@ require_relative 'test_helper'
2
2
  require 'json'
3
3
  require 'set'
4
4
 
5
- describe "Test API" do
5
+ redis = Gaptool.redis
6
+
7
+ describe 'Test API' do
6
8
  before(:all) do
7
9
  ENV['DRYRUN'] = 'true'
8
- ENV['GAPTOOL_CHECK_CLIENT_VERSION'] = 'true'
9
- @version = File.read(File.realpath(
10
- File.join(File.dirname(__FILE__), "..", 'VERSION')
11
- )).strip.split(".")[0..1].join(".")
10
+ ENV['GAPTOOL_CHECK_CLIENT_VERSION'] = nil
12
11
  end
13
12
 
14
13
  after(:all) do
@@ -19,25 +18,24 @@ describe "Test API" do
19
18
  before(:each) do
20
19
  header 'X-GAPTOOL-USER', 'test'
21
20
  header 'X-GAPTOOL-KEY', 'test'
22
- header 'X-GAPTOOL-VERSION', @version
23
- $redis.flushall
21
+ header 'X-GAPTOOL-VERSION', version
22
+ redis.flushall
24
23
  DH.useradd('test', 'test')
25
24
  $time = Time.now
26
25
  end
27
26
 
28
- def add_and_register_server data=nil
27
+ def add_and_register_server(data = nil)
29
28
  data ||= host_data
30
29
  post '/init', data.to_json
31
30
  expect(last_response.status).to eq(200)
32
31
  res = JSON.parse(last_response.body)
33
32
  id = res['instance']
34
- secret = res['secret']
35
33
  # there is no API to get the secret, get it from the database.
36
- secret = Gaptool::Data::get_server_data(id)['secret']
37
- put '/register', {'role' => data['role'],
38
- 'environment' => data['environment'],
39
- 'secret'=> secret,
40
- 'zone'=> data['zone']}.to_json
34
+ secret = Gaptool::Data.get_server_data(id)['secret']
35
+ put '/register', { 'role' => data['role'],
36
+ 'environment' => data['environment'],
37
+ 'secret' => secret,
38
+ 'zone' => data['zone'] }.to_json
41
39
  expect(last_response.status).to eq(200)
42
40
  res
43
41
  end
@@ -52,8 +50,8 @@ describe "Test API" do
52
50
  'terminable' => true,
53
51
  'zone' => 'my-zone-1a',
54
52
  'itype' => 'm1.type',
55
- "hostname" => 'fake.hostname.gild.com',
56
- "launch_time" => $time.to_i
53
+ 'hostname' => 'fake.hostname.gild.com',
54
+ 'launch_time' => $time.to_i
57
55
  }
58
56
  end
59
57
 
@@ -61,7 +59,13 @@ describe "Test API" do
61
59
  ['recipe[init]', 'recipe[myrecipe]']
62
60
  end
63
61
 
64
- it "should return 200" do
62
+ def version
63
+ @v ||= File.read(
64
+ File.realpath(File.join(File.dirname(__FILE__), '..', 'VERSION')
65
+ )).strip
66
+ end
67
+
68
+ it 'should return 200' do
65
69
  get '/'
66
70
  expect(last_response.status).to eq(200)
67
71
  expect(last_response.content_type).to include('text/html')
@@ -72,48 +76,48 @@ describe "Test API" do
72
76
  expect(last_response.content_type).to include('application/json')
73
77
  end
74
78
 
75
- it "should create a instance" do
79
+ it 'should create a instance' do
76
80
  post '/init', host_data.to_json
77
81
  expect(last_response.status).to eq(200)
78
- expect(JSON.parse(last_response.body).keys).to eq(["instance", "ami", "role", "hostname", "launch_time",
79
- "environment", "secret", "terminable", "security_group"])
82
+ expect(JSON.parse(last_response.body).keys).to eq(%w(instance ami role hostname launch_time
83
+ environment secret terminable security_group))
80
84
  end
81
85
 
82
- it "should get the runlist from the role" do
86
+ it 'should get the runlist from the role' do
83
87
  DH.save_role_data(host_data['role'], chef_runlist: ['recipe[other]'].to_json)
84
- id = add_and_register_server(host_data.reject{|k,v| k == 'chef_runlist'})['instance']
88
+ id = add_and_register_server(host_data.reject { |k, _v| k == 'chef_runlist' })['instance']
85
89
  get "/host/testrole/testenv/#{id}"
86
90
  resp = JSON.parse(last_response.body)
87
- expect(resp.keys).to include("chef_runlist")
91
+ expect(resp.keys).to include('chef_runlist')
88
92
  expect(resp['chef_runlist']).to eq(['recipe[other]'])
89
93
  end
90
94
 
91
- it "should get the security group from the role" do
92
- DH.save_role_data(host_data['role'], "sg" => {host_data['environment'] => "mysg-in-role"})
93
- id = add_and_register_server(host_data.reject{|k,v| k == 'security_group'})['instance']
95
+ it 'should get the security group from the role' do
96
+ DH.save_role_data(host_data['role'], 'sg' => { host_data['environment'] => 'mysg-in-role' })
97
+ id = add_and_register_server(host_data.reject { |k, _v| k == 'security_group' })['instance']
94
98
  get "/host/testrole/testenv/#{id}"
95
99
  resp = JSON.parse(last_response.body)
96
- expect(resp.keys).to include("security_group")
97
- expect(resp['security_group']).to eq("mysg-in-role")
100
+ expect(resp.keys).to include('security_group')
101
+ expect(resp['security_group']).to eq('mysg-in-role')
98
102
  end
99
103
 
100
- it "should get the default security group" do
101
- id = add_and_register_server(host_data.reject{|k,v| k == 'security_group'})['instance']
104
+ it 'should get the default security group' do
105
+ id = add_and_register_server(host_data.reject { |k, _v| k == 'security_group' })['instance']
102
106
  get "/host/testrole/testenv/#{id}"
103
107
  resp = JSON.parse(last_response.body)
104
- expect(resp.keys).to include("security_group")
108
+ expect(resp.keys).to include('security_group')
105
109
  expect(resp['security_group']).to eq("#{host_data['role']}-#{host_data['environment']}")
106
110
  end
107
111
 
108
- it "should set instance unterminable" do
109
- id = add_and_register_server()['instance']
112
+ it 'should set instance unterminable' do
113
+ id = add_and_register_server['instance']
110
114
 
111
115
  get "/instance/#{id}"
112
116
  expect(last_response.status).to eq(200)
113
117
  resp = JSON.parse(last_response.body)
114
- expect(resp.keys).not_to include("terminable")
118
+ expect(resp.keys).not_to include('terminable')
115
119
 
116
- patch "/instance/#{id}", {terminable: false}.to_json
120
+ patch "/instance/#{id}", { terminable: false }.to_json
117
121
  expect(last_response.status).to eq(200)
118
122
  resp = JSON.parse(last_response.body)
119
123
  expect(resp['terminable']).to eq(false)
@@ -123,37 +127,37 @@ describe "Test API" do
123
127
  resp = JSON.parse(last_response.body)
124
128
  expect(resp['terminable']).to eq(false)
125
129
 
126
- post '/terminate', {'id' => id}.to_json
130
+ post '/terminate', { 'id' => id }.to_json
127
131
  expect(last_response.status).to eq(409)
128
132
 
129
- patch "/instance/#{id}", {terminable: true}.to_json
133
+ patch "/instance/#{id}", { terminable: true }.to_json
130
134
  expect(last_response.status).to eq(200)
131
135
  resp = JSON.parse(last_response.body)
132
136
  expect(resp['terminable']).to eq(true)
133
137
 
134
- post '/terminate', {'id' => id}.to_json
138
+ post '/terminate', { 'id' => id }.to_json
135
139
  expect(last_response.status).to eq(200)
136
140
  end
137
141
 
138
- it "should set an instance as hidden" do
139
- id = add_and_register_server()['instance']
142
+ it 'should set an instance as hidden' do
143
+ id = add_and_register_server['instance']
140
144
 
141
145
  get "/instance/#{id}"
142
146
  expect(last_response.status).to eq(200)
143
147
  resp = JSON.parse(last_response.body)
144
- expect(resp.keys).not_to include("hidden")
148
+ expect(resp.keys).not_to include('hidden')
145
149
 
146
- patch "/instance/#{id}", {hidden: false}.to_json
150
+ patch "/instance/#{id}", { hidden: false }.to_json
147
151
  expect(last_response.status).to eq(200)
148
152
  resp = JSON.parse(last_response.body)
149
- expect(resp.keys).not_to include("hidden")
153
+ expect(resp.keys).not_to include('hidden')
150
154
 
151
155
  get "/instance/#{id}"
152
156
  expect(last_response.status).to eq(200)
153
157
  resp = JSON.parse(last_response.body)
154
- expect(resp.keys).not_to include("hidden")
158
+ expect(resp.keys).not_to include('hidden')
155
159
 
156
- patch "/instance/#{id}", {hidden: true}.to_json
160
+ patch "/instance/#{id}", { hidden: true }.to_json
157
161
  expect(last_response.status).to eq(200)
158
162
  resp = JSON.parse(last_response.body)
159
163
  expect(resp['hidden']).to eq(true)
@@ -168,7 +172,7 @@ describe "Test API" do
168
172
  resp = JSON.parse(last_response.body)
169
173
  expect(resp['hidden']).to eq(true)
170
174
 
171
- get "/hosts"
175
+ get '/hosts'
172
176
  expect(last_response.status).to eq(200)
173
177
  expect(JSON.parse(last_response.body)).to eq([])
174
178
 
@@ -185,98 +189,98 @@ describe "Test API" do
185
189
  expect(JSON.parse(last_response.body)).to eq([])
186
190
  end
187
191
 
188
- it "should remove default runlist" do
189
- id = add_and_register_server(host_data.merge("chef_runlist"=> ['recipe[init]']))['instance']
192
+ it 'should remove default runlist' do
193
+ id = add_and_register_server(host_data.merge('chef_runlist' => ['recipe[init]']))['instance']
190
194
  get "/host/testrole/testenv/#{id}"
191
195
  resp = JSON.parse(last_response.body)
192
- expect(resp.keys).not_to include("chef_runlist")
196
+ expect(resp.keys).not_to include('chef_runlist')
193
197
  end
194
198
 
195
- it "should remove default runlist from role" do
199
+ it 'should remove default runlist from role' do
196
200
  DH.save_role_data(host_data['role'], chef_runlist: ['recipe[init]'].to_json)
197
- id = add_and_register_server(host_data.reject{|k,v| k == 'chef_runlist'})['instance']
201
+ id = add_and_register_server(host_data.reject { |k, _v| k == 'chef_runlist' })['instance']
198
202
  get "/host/testrole/testenv/#{id}"
199
203
  resp = JSON.parse(last_response.body)
200
- expect(resp.keys).not_to include("chef_runlist")
204
+ expect(resp.keys).not_to include('chef_runlist')
201
205
  expect(resp['chef_runlist']).to be(nil)
202
206
  end
203
207
 
204
- it "should get the ami from the role" do
205
- DH.save_role_data(host_data['role'], 'amis' => {'my-zone-1' => 'ami-other'})
206
- res = add_and_register_server(host_data.reject{|k,v| k == 'ami'})
208
+ it 'should get the ami from the role' do
209
+ DH.save_role_data(host_data['role'], 'amis' => { 'my-zone-1' => 'ami-other' })
210
+ res = add_and_register_server(host_data.reject { |k, _v| k == 'ami' })
207
211
  expect(res['ami']).to eq('ami-other')
208
212
  get "/host/testrole/testenv/#{res['instance']}"
209
213
  resp = JSON.parse(last_response.body)
210
- expect(resp.keys).to_not include("ami")
214
+ expect(resp.keys).to_not include('ami')
211
215
  end
212
216
 
213
- it "should get the ami from global conf" do
214
- DH.set_amis({'my-zone-1' => 'ami-default'})
215
- res = add_and_register_server(host_data.reject{|k,v| k == 'ami'})
217
+ it 'should get the ami from global conf' do
218
+ DH.amis('my-zone-1' => 'ami-default')
219
+ res = add_and_register_server(host_data.reject { |k, _v| k == 'ami' })
216
220
  expect(res['ami']).to eq('ami-default')
217
221
  get "/host/testrole/testenv/#{res['instance']}"
218
222
  resp = JSON.parse(last_response.body)
219
- expect(resp.keys).to_not include("ami")
223
+ expect(resp.keys).to_not include('ami')
220
224
  end
221
225
 
222
- it "should fail to parse client data" do
226
+ it 'should fail to parse client data' do
223
227
  post '/init', host_data
224
228
  expect(last_response.status).to eq(400)
225
229
  end
226
230
 
227
- it "should fail to create a instance" do
231
+ it 'should fail to create a instance' do
228
232
  %w(role environment zone itype).each do |req|
229
- post '/init', host_data.reject{|k, v| k == req}.to_json
233
+ post '/init', host_data.reject { |k, _v| k == req }.to_json
230
234
  expect(last_response.status).to eq(400)
231
235
  expect(JSON.parse(last_response.body).keys).to eq(%w(result message))
232
236
  end
233
237
  end
234
238
 
235
- it "should fail for missing parameter to terminate" do
239
+ it 'should fail for missing parameter to terminate' do
236
240
  post '/terminate'
237
241
  expect(last_response.status).to eq(400)
238
242
  end
239
243
 
240
- it "should fail to terminate non-existing instance" do
241
- post '/terminate', {'id' => 'i-1234567'}.to_json
244
+ it 'should fail to terminate non-existing instance' do
245
+ post '/terminate', { 'id' => 'i-1234567' }.to_json
242
246
  expect(last_response.status).to eq(404)
243
247
  end
244
248
 
245
- it "should fail to terminate non-terminable instance" do
249
+ it 'should fail to terminate non-terminable instance' do
246
250
  post '/init', host_data.merge('terminable' => false).to_json
247
251
  expect(last_response.status).to eq(200)
248
252
  id = JSON.parse(last_response.body)['instance']
249
- post '/terminate', {'id' => id}.to_json
253
+ post '/terminate', { 'id' => id }.to_json
250
254
  expect(last_response.status).to eq(409)
251
255
  end
252
256
 
253
- it "should terminate instance" do
257
+ it 'should terminate instance' do
254
258
  post '/init', host_data.to_json
255
259
  expect(last_response.status).to eq(200)
256
260
  id = JSON.parse(last_response.body)['instance']
257
- post '/terminate', {'id' => id}.to_json
261
+ post '/terminate', { 'id' => id }.to_json
258
262
  expect(last_response.status).to eq(200)
259
- expect(JSON.parse(last_response.body)).to eq({id => {'status'=> 'terminated'}})
263
+ expect(JSON.parse(last_response.body)).to eq(id => { 'status' => 'terminated' })
260
264
  end
261
265
 
262
- it "should fail to register a server" do
266
+ it 'should fail to register a server' do
263
267
  required_keys = %w(role zone environment secret)
264
- hdata = host_data.select {|k, v| required_keys.include?(k)}
268
+ hdata = host_data.select { |k, _v| required_keys.include?(k) }
265
269
  hdata['secret'] = 'mysecret'
266
270
  required_keys.each do |key|
267
- put '/register', hdata.select {|k,v| k != key}.to_json
271
+ put '/register', hdata.select { |k, _v| k != key }.to_json
268
272
  expect(last_response.status).to eq(400)
269
273
  end
270
274
  put '/register', hdata.to_json
271
275
  expect(last_response.status).to eq(403)
272
276
  end
273
277
 
274
- it "should register the server" do
278
+ it 'should register the server' do
275
279
  add_and_register_server
276
280
  expect(last_response.body).to include("-E #{host_data['environment']}")
277
281
  end
278
282
 
279
- it "should not find any hosts" do
283
+ it 'should not find any hosts' do
280
284
  post '/init', host_data.to_json
281
285
  expect(last_response.status).to eq(200)
282
286
 
@@ -289,147 +293,209 @@ describe "Test API" do
289
293
  expect(JSON.parse(last_response.body)).to eq([])
290
294
  end
291
295
 
292
- it "should find an host" do
293
- id = add_and_register_server()['instance']
296
+ it 'should find an host' do
297
+ id = add_and_register_server['instance']
294
298
  get '/hosts'
295
299
  expect(last_response.status).to eq(200)
296
- exp_data = host_data.reject{|k,v| k == 'terminable'}.merge('chef_runlist' => expanded_runlist,
297
- 'apps'=> [])
300
+ exp_data = host_data.reject { |k, _v| k == 'terminable' }.merge('chef_runlist' => expanded_runlist,
301
+ 'apps' => [])
298
302
  exp_data['instance'] = id
299
303
  expect(JSON.parse(last_response.body)).to eq([exp_data])
300
304
  end
301
305
 
302
- it "should find two hosts" do
303
- id1 = add_and_register_server()['instance']
304
- id2 = add_and_register_server()['instance']
306
+ it 'should find two hosts' do
307
+ id1 = add_and_register_server['instance']
308
+ id2 = add_and_register_server['instance']
305
309
  get '/hosts'
306
310
  expect(last_response.status).to eq(200)
307
311
  exp_data = [
308
- host_data.reject{|k,v| k == 'terminable'}.merge({'instance' => id1,
309
- 'chef_runlist' => expanded_runlist,
310
- 'apps' => []}),
311
- host_data.reject{|k,v| k == 'terminable'}.merge({'instance' => id2,
312
- 'chef_runlist' => expanded_runlist,
313
- 'apps' => []})
312
+ host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id1,
313
+ 'chef_runlist' => expanded_runlist,
314
+ 'apps' => []),
315
+ host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id2,
316
+ 'chef_runlist' => expanded_runlist,
317
+ 'apps' => [])
314
318
  ].to_set
315
319
  expect(JSON.parse(last_response.body).to_set).to eq(exp_data)
316
320
  end
317
321
 
318
- it "should find one hidden host" do
319
- id1 = add_and_register_server()['instance']
320
- id2 = add_and_register_server()['instance']
321
- patch "/instance/#{id1}", {hidden: true}.to_json
322
+ it 'should find one hidden host' do
323
+ id1 = add_and_register_server['instance']
324
+ id2 = add_and_register_server['instance']
325
+ patch "/instance/#{id1}", { hidden: true }.to_json
322
326
  %W(/hosts /hosts/#{host_data['role']} /hosts/#{host_data['role']}/#{host_data['environment']} /hosts/ALL/#{host_data['environment']}).each do |url|
323
327
  get url
324
328
  expect(last_response.status).to eq(200)
325
- res = JSON.parse(last_response.body).map{|x| x.delete('apps'); x}.to_set
326
- exp_data = [host_data.reject{|k,v| k == 'terminable'}.merge({'instance' => id2,
327
- 'chef_runlist' => expanded_runlist})].to_set
329
+ res = JSON.parse(last_response.body).map { |x| x.delete('apps'); x }.to_set
330
+ exp_data = [host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id2,
331
+ 'chef_runlist' => expanded_runlist)].to_set
328
332
  expect(res).to eq(exp_data)
329
- get url, {hidden: true}
333
+ get url, hidden: true
330
334
  expect(last_response.status).to eq(200)
331
335
  exp_data = [
332
- host_data.reject{|k,v| k == 'terminable'}.merge({'instance' => id1,
333
- 'chef_runlist' => expanded_runlist,
334
- 'hidden' => true}),
335
- host_data.reject{|k,v| k == 'terminable'}.merge({'instance' => id2,
336
- 'chef_runlist' => expanded_runlist})
336
+ host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id1,
337
+ 'chef_runlist' => expanded_runlist,
338
+ 'hidden' => true),
339
+ host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id2,
340
+ 'chef_runlist' => expanded_runlist)
337
341
  ].to_set
338
- expect(JSON.parse(last_response.body).map{|x| x.delete('apps'); x}.to_set).to eq(exp_data)
342
+ expect(JSON.parse(last_response.body).map { |x| x.delete('apps'); x }.to_set).to eq(exp_data)
339
343
  end
340
344
  end
341
345
 
342
- it "should find an host by role" do
343
- other = host_data.merge({'role' => 'otherrole'})
346
+ it 'should find an host by role' do
347
+ other = host_data.merge('role' => 'otherrole')
344
348
  id1 = add_and_register_server(other)['instance']
345
- add_and_register_server()['instance']
349
+ add_and_register_server['instance']
346
350
 
347
351
  get '/hosts/otherrole'
348
352
  expect(last_response.status).to eq(200)
349
- exp_data = [other.reject{|k,v| k == 'terminable'}.merge({'instance'=> id1,
350
- 'chef_runlist' => expanded_runlist,
351
- 'apps' => []})]
353
+ exp_data = [other.reject { |k, _v| k == 'terminable' }.merge('instance' => id1,
354
+ 'chef_runlist' => expanded_runlist,
355
+ 'apps' => [])]
352
356
  expect(JSON.parse(last_response.body)).to eq(exp_data)
353
357
  end
354
358
 
355
- it "should find an host by id" do
356
- id = add_and_register_server()['instance']
359
+ it 'should find an host by id' do
360
+ id = add_and_register_server['instance']
361
+ ["/instance/#{id}", "/host/testrole/testenv/#{id}", "/host/FAKE/FAKE/#{id}"].each do |url|
362
+ get url
363
+ expect(last_response.status).to eq(200)
364
+ exp_data = host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id,
365
+ 'chef_runlist' => expanded_runlist,
366
+ 'apps' => [])
367
+ expect(JSON.parse(last_response.body)).to eq(exp_data)
368
+ end
369
+ end
370
+
371
+ it 'should return the host attr json' do
372
+ DH.set_config('chef_repo', 'FAKECHEFREPO')
373
+ DH.set_config('chef_branch', 'chefbranch')
374
+ DH.set_config('url', 'localhost:666')
375
+ id = add_and_register_server['instance']
376
+ get "/instance/#{id}/attrs"
377
+ expect(last_response.status).to eq(200)
378
+ expect(JSON.parse(last_response.body)).to eq(
379
+ 'apps' => [],
380
+ 'branch' => 'master',
381
+ 'chefbranch' => 'chefbranch',
382
+ 'chefrepo' => 'FAKECHEFREPO',
383
+ 'deploy_apps' => [],
384
+ 'environment' => host_data['environment'],
385
+ 'gaptool' => {
386
+ 'user' => 'test',
387
+ 'key' => 'test',
388
+ 'url' => 'localhost:666'
389
+ },
390
+ 'migrate' => false,
391
+ 'role' => host_data['role'],
392
+ 'rollback' => false,
393
+ 'run_list' => ['recipe[init]', 'recipe[myrecipe]']
394
+ )
395
+ end
396
+
397
+ it 'old clients should receive apps as strings' do
398
+ header 'X-GAPTOOL-VERSION', '0.7.0'
399
+ id = add_and_register_server['instance']
400
+ ["/instance/#{id}", "/host/testrole/testenv/#{id}", "/host/FAKE/FAKE/#{id}"].each do |url|
401
+ get url
402
+ expect(last_response.status).to eq(200)
403
+ exp_data = host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id,
404
+ 'chef_runlist' => expanded_runlist,
405
+ 'apps' => '[]')
406
+ expect(JSON.parse(last_response.body)).to eq(exp_data)
407
+ end
408
+ end
409
+
410
+ it 'clients with no version should be handled as old' do
411
+ header 'X-GAPTOOL-VERSION', nil
412
+ id = add_and_register_server['instance']
357
413
  ["/instance/#{id}", "/host/testrole/testenv/#{id}", "/host/FAKE/FAKE/#{id}"].each do |url|
358
414
  get url
359
415
  expect(last_response.status).to eq(200)
360
- exp_data = host_data.reject{|k,v| k == 'terminable'}.merge({
361
- 'instance'=> id,
362
- 'chef_runlist' => expanded_runlist,
363
- 'apps' => "[]"})
416
+ exp_data = host_data.reject { |k, _v| k == 'terminable' }.merge('instance' => id,
417
+ 'chef_runlist' => expanded_runlist,
418
+ 'apps' => '[]')
364
419
  expect(JSON.parse(last_response.body)).to eq(exp_data)
365
420
  end
366
421
  end
367
422
 
368
- it "should not find an host by id" do
369
- add_and_register_server()['instance']
423
+ it 'should not find an host by id' do
424
+ add_and_register_server['instance']
370
425
  get '/instance/other'
371
426
  expect(last_response.status).to eq(404)
372
427
  expect(JSON.parse(last_response.body)).to eq(
373
- {"result" => "error",
374
- "message" => "instance with id 'other' not found"})
428
+ 'result' => 'error',
429
+ 'message' => "instance with id 'other' not found"
430
+ )
375
431
  end
376
432
 
377
- it "should find all hosts by environment and role" do
378
- id1 = add_and_register_server()['instance']
379
- id2 = add_and_register_server()['instance']
380
- add_and_register_server(host_data.merge({'environment' => 'otherenv'}))['instance']
381
- id4 = add_and_register_server(host_data.merge({'role' => 'otherrole'}))['instance']
433
+ it 'should find all hosts by environment and role' do
434
+ id1 = add_and_register_server['instance']
435
+ id2 = add_and_register_server['instance']
436
+ add_and_register_server(host_data.merge('environment' => 'otherenv'))['instance']
437
+ id4 = add_and_register_server(host_data.merge('role' => 'otherrole'))['instance']
382
438
  get '/hosts/testrole/testenv'
383
439
  expect(last_response.status).to eq(200)
384
- d = host_data.reject{|k,v| k == 'terminable'}.merge('chef_runlist' => expanded_runlist, 'apps'=> '[]')
440
+ d = host_data.reject { |k, _v| k == 'terminable' }.merge('chef_runlist' => expanded_runlist, 'apps' => [])
385
441
  exp_data = [
386
- d.merge({'instance' => id1}),
387
- d.merge({'instance' => id2})
442
+ d.merge('instance' => id1),
443
+ d.merge('instance' => id2)
388
444
  ].to_set
389
445
  expect(JSON.parse(last_response.body).to_set).to eq(exp_data)
390
446
 
391
447
  get '/hosts/ALL/testenv'
392
448
  expect(last_response.status).to eq(200)
393
449
  exp_data = [
394
- d.merge({'instance' => id1}),
395
- d.merge({'instance' => id2}),
396
- d.merge({'instance' => id4, 'role'=> 'otherrole'})
450
+ d.merge('instance' => id1),
451
+ d.merge('instance' => id2),
452
+ d.merge('instance' => id4, 'role' => 'otherrole')
397
453
  ].to_set
398
454
  expect(JSON.parse(last_response.body).to_set).to eq(exp_data)
399
455
  end
400
456
 
401
- it "should return the server version" do
402
- version = File.read(File.realpath(
403
- File.join(File.dirname(__FILE__), "..", 'VERSION')
404
- )).strip
405
- get "/version"
457
+ it 'should return the server version' do
458
+ get '/version'
406
459
  expect(last_response.status).to eq(200)
407
- expect(JSON.parse(last_response.body)).to eq({"server_version" => version, "api" => {"v0" => "/"}})
460
+ expect(JSON.parse(last_response.body)).to eq('server_version' => version, 'api' => { 'v0' => '/' })
408
461
  end
409
462
 
410
- it "should return the list of apps" do
411
- DH.add_app("firstapp", "testrole", "testenv")
412
- DH.add_app("secondapp", "testrole", "testenv")
413
- apps_list = {"app:firstapp" => {"testenv" => "testrole"},
414
- "app:secondapp" => {"testenv" => "testrole"}}
415
- get "/apps"
463
+ it 'should return the list of apps' do
464
+ DH.add_app('firstapp', 'testrole', 'testenv')
465
+ DH.add_app('secondapp', 'testrole', 'testenv')
466
+ apps_list = { 'app:firstapp' => { 'testenv' => 'testrole' },
467
+ 'app:secondapp' => { 'testenv' => 'testrole' } }
468
+ get '/apps'
416
469
  expect(last_response.status).to eq(200)
417
470
  expect(JSON.parse(last_response.body)).to eq(apps_list)
418
471
  end
419
472
 
420
- it "should fail as client did not send version" do
473
+ it 'should fail as client did not send version' do
474
+ ENV['GAPTOOL_CHECK_CLIENT_VERSION'] = '1'
421
475
  header 'X-GAPTOOL-VERSION', nil
422
- get "/version"
476
+ get '/version'
423
477
  expect(last_response.status).to eq(400)
424
478
  resp = JSON.parse(last_response.body)
425
479
  expect(resp['result']).to eq('error')
426
480
  expect(resp['message']).to match(/^Invalid version/)
427
481
  end
428
482
 
429
- it "should not check version for clients" do
483
+ it 'should not check version for clients' do
430
484
  ENV['GAPTOOL_CHECK_CLIENT_VERSION'] = nil
431
485
  header 'X_GAPTOOL_VERSION', nil
432
- get "/version"
486
+ get '/version'
433
487
  expect(last_response.status).to eq(200)
434
488
  end
489
+
490
+ it 'should reject old clients versions' do
491
+ ENV['GAPTOOL_CHECK_CLIENT_VERSION'] = '1'
492
+ srv = Versionomy.parse(version)
493
+ cl = Versionomy.create(major: srv.major, minor: srv.minor - 1, tiny: 0)
494
+ header 'X_GAPTOOL_VERSION', cl.to_s
495
+ get '/version'
496
+ expect(last_response.status).to eq(400)
497
+ resp = JSON.parse(last_response.body)
498
+ expect(resp['result']).to eq('error')
499
+ expect(resp['message']).to match(/^Invalid version/)
500
+ end
435
501
  end
data/test/base_test.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  require_relative 'test_helper'
2
2
 
3
- describe "ping should work unauthenticated" do
4
- it "should return PONG" do
3
+ describe 'ping should work unauthenticated' do
4
+ it 'should return PONG' do
5
5
  get '/ping'
6
6
  expect(last_response.status).to eq(200)
7
- expect(last_response.body).to eq("PONG")
7
+ expect(last_response.body).to eq('PONG')
8
8
  end
9
9
  end
10
10
 
11
- describe "unauthenticated" do
12
- it "should return 401 error" do
11
+ describe 'unauthenticated' do
12
+ it 'should return 401 error' do
13
13
  get '/'
14
14
  expect(last_response.status).to eq(401)
15
15
  end