-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_config.py
More file actions
620 lines (554 loc) · 24 KB
/
test_config.py
File metadata and controls
620 lines (554 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# -*- coding: utf-8 -*-
"""
Unit tests for the Drycc api app.
Run the tests with "./manage.py test api"
"""
import json
from io import StringIO
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.conf import settings
from django.core.management import call_command
from unittest import mock
from api.models.app import App
from api.models.base import PROCFILE_TYPE_RUN, PROCFILE_TYPE_WEB
from api.models.config import Config
from api.serializers import CONFIG_LIMITS_MISMATCH_MSG
from api.models.build import Build
from api.models.release import Release
from api.tests import adapter, DryccTransactionTestCase
import requests_mock
User = get_user_model()
@requests_mock.Mocker(real_http=True, adapter=adapter)
class ConfigTest(DryccTransactionTestCase):
"""Tests setting and updating config values"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = self.get_or_create_token(self.user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = '/v2/apps'
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
self.assertEqual(response.status_code, 201, response.data)
self.app = App.objects.all()[0]
def tearDown(self):
# Restore default tags to empty string
settings.DRYCC_DEFAULT_CONFIG_TAGS = ''
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_config(self, mock_requests):
"""
Test that config is auto-created for a new app and that
config can be updated using a PATCH
"""
app_id = self.create_app()
# check to see that an initial/empty config was created
url = f"/v2/apps/{app_id}/config"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('values', response.data)
self.assertEqual(response.data['values'], {})
config1 = response.data
# set an initial config value
body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
config2 = response.data
self.assertNotEqual(config1['uuid'], config2['uuid'])
self.assertIn('NEW_URL1', response.data['values'])
# read the config
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
config3 = response.data
self.assertEqual(config2, config3)
self.assertIn('NEW_URL1', response.data['values'])
# set an additional config value
body = {'values': json.dumps({'NEW_URL2': 'http://localhost:8080/'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
config3 = response.data
self.assertNotEqual(config2['uuid'], config3['uuid'])
self.assertIn('NEW_URL1', response.data['values'])
self.assertIn('NEW_URL2', response.data['values'])
# read the config again
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
config4 = response.data
self.assertEqual(config3, config4)
self.assertIn('NEW_URL1', response.data['values'])
self.assertIn('NEW_URL2', response.data['values'])
# unset a config value
body = {'values': json.dumps({'NEW_URL2': None})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
config5 = response.data
self.assertNotEqual(config4['uuid'], config5['uuid'])
self.assertNotIn('NEW_URL2', json.dumps(response.data['values']))
# unset all config values
body = {'values': json.dumps({'NEW_URL1': None})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertNotIn('NEW_URL1', json.dumps(response.data['values']))
# set a port and then unset it to make sure validation ignores the unset
body = {'values': json.dumps({'PORT': '5000'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('PORT', response.data['values'])
body = {'values': json.dumps({'PORT': None})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertNotIn('PORT', response.data['values'])
# disallow put/patch/delete
response = self.client.put(url)
self.assertEqual(response.status_code, 405, response.data)
response = self.client.patch(url)
self.assertEqual(response.status_code, 405, response.data)
response = self.client.delete(url)
self.assertEqual(response.status_code, 405, response.data)
return config5
def test_default_tags(self, mock_requests):
settings.DRYCC_DEFAULT_CONFIG_TAGS = '{"ssd": "true"}'
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
response = self.client.get(url)
expected = {
'owner': self.user.username,
'app': app_id,
'values': {},
'limits': {
PROCFILE_TYPE_RUN: 'std1.large.c1m1',
PROCFILE_TYPE_WEB: 'std1.large.c1m1'
},
'tags': {'ssd': 'true'},
'registry': {}
}
self.assertEqual(response.data, expected | response.data)
# make sure changes not drop tags
body = {'values': json.dumps({'PORT': '5001'})}
response = self.client.post(url, body)
expected = {
'owner': self.user.username,
'app': app_id,
'values': {'PORT': '5001'},
'limits': {
PROCFILE_TYPE_RUN: 'std1.large.c1m1',
PROCFILE_TYPE_WEB: 'std1.large.c1m1'
},
'tags': {'ssd': 'true'},
'registry': {}
}
self.assertEqual(response.data, response.data | expected)
def test_response_data(self, mock_requests):
"""Test that the serialized response contains only relevant data."""
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
# set an initial config value
body = {'values': json.dumps({'PORT': '5000'})}
response = self.client.post(url, body)
for key in response.data:
self.assertIn(key, ['uuid', 'owner', 'created', 'updated', 'app', 'values',
'typed_values', 'limits', 'tags', 'registry', 'healthcheck',
'lifecycle_post_start', 'lifecycle_pre_stop',
'termination_grace_period'])
expected = {
'owner': self.user.username,
'app': app_id,
'values': {'PORT': '5000'},
'limits': {
PROCFILE_TYPE_RUN: 'std1.large.c1m1',
PROCFILE_TYPE_WEB: 'std1.large.c1m1'
},
'tags': {},
'registry': {}
}
self.assertEqual(response.data, response.data | expected)
def test_response_data_types_converted(self, mock_requests):
"""Test that config data is converted into the correct type."""
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
body = {
'values': json.dumps({'PORT': 5000}),
'limits': {
PROCFILE_TYPE_WEB: 'std1.large.c1m2',
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
for key in response.data:
self.assertIn(key, ['uuid', 'owner', 'created', 'updated', 'app', 'values', 'limits',
'typed_values', 'tags', 'registry', 'healthcheck',
'lifecycle_post_start', 'lifecycle_pre_stop',
'termination_grace_period'])
expected = {
'owner': self.user.username,
'app': app_id,
'values': {'PORT': '5000'},
'limits': {
PROCFILE_TYPE_RUN: 'std1.large.c1m1',
PROCFILE_TYPE_WEB: 'std1.large.c1m2'
},
'tags': {},
'registry': {}
}
self.assertEqual(response.data, expected | response.data)
body = {'limits': {PROCFILE_TYPE_WEB: "not-exist"}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
self.assertEqual(
str(response.data["limits"][0]),
CONFIG_LIMITS_MISMATCH_MSG.format("not-exist")
)
def test_config_set_same_key(self, mock_requests):
"""
Test that config sets on the same key function properly
"""
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
# set an initial config value
body = {'values': json.dumps({'PORT': '5000'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('PORT', response.data['values'])
# reset same config value
body = {'values': json.dumps({'PORT': '5001'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('PORT', response.data['values'])
self.assertEqual(response.data['values']['PORT'], '5001')
def test_config_set_unicode(self, mock_requests):
"""
Test that config sets with unicode values are accepted.
"""
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
# set an initial config value
body = {'values': json.dumps({'POWERED_BY': 'Деис'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('POWERED_BY', response.data['values'])
# reset same config value
body = {'values': json.dumps({'POWERED_BY': 'Кроликов'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('POWERED_BY', response.data['values'])
self.assertEqual(response.data['values']['POWERED_BY'], 'Кроликов')
# set an integer to test unicode regression
body = {'values': json.dumps({'INTEGER': 1})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('INTEGER', response.data['values'])
self.assertEqual(response.data['values']['INTEGER'], '1')
def test_config_str(self, mock_requests):
"""Test the text representation of a node."""
config5 = self.test_config()
config = Config.objects.get(uuid=config5['uuid'])
self.assertEqual(str(config), "{}-{}".format(config5['app'], str(config5['uuid'])[:7]))
def test_valid_config_keys(self, mock_requests):
"""Test that valid config keys are accepted.
"""
keys = ("FOO", "_foo", "f001", "FOO_BAR_BAZ_")
app_id = self.create_app()
url = f'/v2/apps/{app_id}/config'
for k in keys:
body = {'values': json.dumps({k: "testvalue"})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201)
self.assertIn(k, response.data['values'])
def test_config_deploy_failure(self, mock_requests):
"""
Cause an Exception in app.deploy to cause a release.delete
"""
app_id = self.create_app()
# deploy app to get a build
url = "/v2/apps/{}/builds".format(app_id)
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(response.data['image'], body['image'])
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.side_effect = Exception('Boom!')
url = f'/v2/apps/{app_id}/config'
body = {'values': json.dumps({'test': "testvalue"})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400)
def test_invalid_config_keys(self, mock_requests):
"""Test that invalid config keys are rejected.
"""
keys = ("$123", "../../foo", "FOO/", "*FOO-BAR")
app_id = self.create_app()
url = f'/v2/apps/{app_id}/config'
for k in keys:
body = {'values': json.dumps({k: "testvalue"})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400)
def test_invalid_config_values(self, mock_requests):
"""
Test that invalid config values are rejected.
Right now only PORT is checked
"""
data = [
{'field': 'PORT', 'value': 'dog'},
{'field': 'PORT', 'value': 99999}
]
app_id = self.create_app()
url = f'/v2/apps/{app_id}/config'
for row in data:
body = {'values': json.dumps({row['field']: row['value']})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
def test_admin_can_create_config_on_other_apps(self, mock_requests):
"""If a non-admin creates an app, an administrator should be able to set config
values for that app.
"""
user = User.objects.get(username='autotest2')
token = self.get_or_create_token(user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
# set an initial config value
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
body = {'values': json.dumps({'PORT': '5000'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('PORT', response.data['values'])
return response
def test_config_owner_is_requesting_user(self, mock_requests):
"""
Ensure that setting the config value is owned by the requesting user
See https://github.com/drycc/drycc/issues/2650
"""
response = self.test_admin_can_create_config_on_other_apps()
self.assertEqual(response.data['owner'], self.user.username)
def test_unauthorized_user_cannot_modify_config(self, mock_requests):
"""
An unauthorized user should not be able to modify other config.
Since an unauthorized user can't access the application, these
requests should return a 403.
"""
app_id = self.create_app()
unauthorized_user = User.objects.get(username='autotest2')
unauthorized_token = self.get_or_create_token(unauthorized_user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
url = '/v2/apps/{}/config'.format(app_id)
body = {'values': {'FOO': 'bar'}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 403)
def test_config_app_not_exists(self, mock_requests):
url = '/v2/apps/{}/config'.format('fake')
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data, 'No App matches the given query.')
def test_config_failures(self, mock_requests):
app_id = self.create_app()
app = App.objects.get(id=app_id)
# deploy app to get a build
url = "/v2/apps/{}/builds".format(app_id)
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(response.data['image'], body['image'])
# set an initial config value
url = f"/v2/apps/{app_id}/config"
body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('NEW_URL1', response.data['values'])
success_config = app.release_set.latest().config
# create a failed config to check that failed release is created
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.side_effect = Exception('Boom!')
url = f'/v2/apps/{app_id}/config'
body = {'values': json.dumps({'test': "testvalue"})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400)
self.assertEqual(app.release_set.latest().version, 4)
self.assertEqual(app.release_set.filter(failed=False).latest().version, 3)
# create a build to see that the new release is created with the last successful config
url = "/v2/apps/{}/builds".format(app_id)
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(app.release_set.latest().version, 5)
self.assertEqual(app.release_set.latest().config, success_config)
self.assertEqual(app.config_set.count(), 3)
def test_unset_limits(self, mock_requests):
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
body = {
'values': json.dumps({'PORT': 5000}),
'limits': {
"task": 'std1.large.c2m4',
PROCFILE_TYPE_RUN: 'std1.large.c2m4',
PROCFILE_TYPE_WEB: 'std1.large.c2m4',
},
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["limits"], body["limits"])
# unset ok
body = {
'limits': {
"task": None,
},
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201)
response = self.client.get(url)
self.assertEqual(
response.data["limits"],
{PROCFILE_TYPE_RUN: 'std1.large.c2m4', PROCFILE_TYPE_WEB: 'std1.large.c2m4'},
)
def test_unset_limits_error(self, mock_requests):
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
body = {
'values': json.dumps({'PORT': 5000}),
'limits': {
"task": 'std1.large.c2m4',
PROCFILE_TYPE_RUN: 'std1.large.c2m4',
PROCFILE_TYPE_WEB: 'std1.large.c2m4',
},
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201)
# dockerfile + procfile worflow
app = App.objects.get(id=app_id)
user = User.objects.get(username='autotest')
build = Build.objects.create(
owner=user,
app=app,
image="qwerty",
procfile={
'web': 'node server.js',
'worker': 'node worker.js'
},
dockerfile='foo',
sha='somereallylongsha'
)
# create an initial release
release = Release.objects.create(
version=3,
owner=user,
app=app,
config=app.config_set.latest(),
build=build
)
# deploy
app.pipeline(release)
# unset error
body = {
'limits': {
"no-exists": None,
},
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 422)
self.assertEqual(str(response.data["detail"]), "no-exists does not exist under limits")
# scale up
body = {'web': 3}
response = self.client.post(f"/v2/apps/{app_id}/scale", body)
self.assertEqual(response.status_code, 204, response.data)
body = {
'limits': {
PROCFILE_TYPE_WEB: None,
},
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 422)
self.assertEqual(
str(response.data["detail"]), "the web has already been used and cannot be deleted")
def call_command(self, *args, **kwargs):
out = StringIO()
call_command(
"measure_apps",
*args,
stdout=out,
stderr=StringIO(),
**kwargs,
)
return out.getvalue()
def test_measure_config(self, *args, **kwargs):
# create
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
body = {
'values': json.dumps({'PORT': 5000}),
'limits': {
PROCFILE_TYPE_RUN: 'std1.large.c2m4',
PROCFILE_TYPE_WEB: 'std1.large.c2m4',
},
}
response = self.client.post(url, body)
out = self.call_command()
self.assertIn(out, "done\n")
self.assertEqual(response.status_code, 201)
def test_set_config_limits_run(self, *args, **kwargs):
# create
app_id = self.create_app()
# dockerfile + procfile worflow
app = App.objects.get(id=app_id)
user = User.objects.get(username='autotest')
build = Build.objects.create(
owner=user,
app=app,
image="qwerty",
procfile={
'web': 'node server.js',
'worker': 'node worker.js'
},
dockerfile='foo',
sha='somereallylongsha'
)
# create an initial release
release = Release.objects.create(
version=3,
owner=user,
app=app,
config=app.config_set.latest(),
build=build
)
# deploy
app.pipeline(release)
body = {
'values': json.dumps({'PORT': 5000}),
'limits': {
PROCFILE_TYPE_RUN: 'std1.large.c2m4',
PROCFILE_TYPE_WEB: 'std1.large.c2m4',
},
}
url = f"/v2/apps/{app_id}/config"
response = self.client.post(url, body)
url = f"/v2/apps/{app_id}/config"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
expect = {'run': 'std1.large.c2m4', 'web': 'std1.large.c2m4', 'worker': 'std1.large.c1m1'}
self.assertEqual(expect, response.json()["limits"], response.data)
def test_config_set_typed_values(self, mock_requests):
"""
Test that config sets on the same key function properly
"""
app_id = self.create_app()
url = f"/v2/apps/{app_id}/config"
# set an initial config value
body = {'typed_values': {'web': {'PORT': '5000'}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('PORT', response.data['typed_values']['web'])
# reset same config value
body = {'typed_values': {'web': {'PORT': '5001'}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('PORT', response.data['typed_values']['web'])
self.assertEqual(response.data['typed_values']['web']['PORT'], '5001')
# unset PORT
body = {'typed_values': {'web': {'PORT': None}}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(response.data['typed_values']['web'], {}, response.data)
# unset web
body = {'typed_values': {'web': None}}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(response.data['typed_values'], {}, response.data)