-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_release.py
More file actions
719 lines (657 loc) · 32.6 KB
/
test_release.py
File metadata and controls
719 lines (657 loc) · 32.6 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import hashlib
import hmac
import json
import logging
import requests
import uuid
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.test.utils import override_settings
from unittest import mock
from api.models.app import App
from api.models.base import PROCFILE_TYPE_WEB
from api.models.build import Build
from api.models.release import Release
from scheduler import KubeHTTPException
from api.tests import adapter, DryccTransactionTestCase
import requests_mock
User = get_user_model()
@requests_mock.Mocker(real_http=True, adapter=adapter)
class ReleaseTest(DryccTransactionTestCase):
"""Tests push notification from build system"""
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)
def tearDown(self):
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_release(self, mock_requests):
"""
Test that a release is created when an app is created, and
that updating config or build or triggers a new release
"""
app_id = self.create_app()
# check that updating config rolls a new release
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'])
# check to see that an initial release was created
url = f'/v2/apps/{app_id}/releases'
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
# account for the config release as well
self.assertEqual(response.data['count'], 2)
url = f'/v2/apps/{app_id}/releases/v1'
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
release1 = response.data
self.assertIn('config', response.data)
self.assertIn('build', response.data)
self.assertEqual(release1['version'], 1)
# check to see that a new release was created
url = f'/v2/apps/{app_id}/releases/v2'
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
release2 = response.data
self.assertNotEqual(release1['uuid'], release2['uuid'])
self.assertNotEqual(release1['config'], release2['config'])
self.assertEqual(release1['build'], release2['build'])
self.assertEqual(release2['version'], 2)
# check that updating the build rolls a new release
url = f'/v2/apps/{app_id}/builds'
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'])
# check to see that a new release was created
url = f'/v2/apps/{app_id}/releases/v3'
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
release3 = response.data
self.assertNotEqual(release2['uuid'], release3['uuid'])
self.assertNotEqual(release2['build'], release3['build'])
self.assertEqual(release3['version'], 3)
# check that we can fetch a previous release
url = f'/v2/apps/{app_id}/releases/v2'
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
release2 = response.data
self.assertNotEqual(release2['uuid'], release3['uuid'])
self.assertNotEqual(release2['build'], release3['build'])
self.assertEqual(release2['version'], 2)
# disallow post/put/patch/delete
url = f'/v2/apps/{app_id}/releases'
response = self.client.post(url)
self.assertEqual(response.status_code, 405, response.content)
response = self.client.put(url)
self.assertEqual(response.status_code, 405, response.content)
response = self.client.patch(url)
self.assertEqual(response.status_code, 405, response.content)
response = self.client.delete(url)
self.assertEqual(response.status_code, 405, response.content)
return release3
def test_conditions(self, mock_requests):
app_id = self.create_app()
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'stack': 'heroku-18',
'sha': 'a'*40,
'procfile': {
'web': 'node server.js',
'worker-test1': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f'/v2/apps/{app_id}/releases/'
response = self.client.get(url)
self.assertEqual(len(response.data['results'][0]['conditions']), 1)
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.side_effect = Exception('Boom!')
url = f"/v2/apps/{app_id}/builds"
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f'/v2/apps/{app_id}/releases/'
response = self.client.get(url)
self.assertEqual(response.data['results'][0]['conditions'][0]['exception'], 'Boom!')
def test_get_image(self, mock_requests):
app_id = self.create_app()
url = f"/v2/apps/{app_id}/builds"
body = {
'image': '127.0.0.1:5555/autotest/example:git-fadf1231',
'stack': 'heroku-18',
'sha': 'a'*40,
'dryccfile': {
"build": {
"docker": {"web": "Dockerfile", "worker": "worker/Dockerfile"},
"config": {"RAILS_ENV": "development", "FOO": "bar"}
},
"run": [
{
"command": ["./deployment-tasks.sh"],
"image": "worker",
}
],
"deploy": {
"web": {
"command": ["bash", "-c"],
"args": ["bundle exec puma -C config/puma.rb"],
},
"worker": {
"command": ["bash", "-c"],
"args": ["python myworker.py"],
},
"worker-1": {
"image": "worker"
},
"worker-2": {},
"worker-3": {
"command": ["bash", "-c"],
"args": ["bundle exec puma -C config/puma.rb"],
"image": "web"
},
"worker-4": {
"command": ["bash", "-c"],
"args": ["bundle exec puma -C config/puma.rb"],
"image": "127.0.0.1:7070/myapp/web:git-123fsa1"
}
}
}
}
default_image = '127.0.0.1:5555/autotest/example:git-fadf1231'
worker_image = "127.0.0.1:5555/autotest/example:git-fadf1231-worker"
worker_4_image = "127.0.0.1:7070/myapp/web:git-123fsa1"
with mock.patch('scheduler.resources.pod.Pod.watch') as mock_kube:
mock_kube.return_value = ['up', 'down']
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
app = App.objects.get(id=app_id)
release_obj = app.release_set.filter(version=2)[0]
runners = release_obj.get_runners(["web"])
self.assertEqual(len(runners), 1, release_obj.build.dryccfile)
self.assertEqual(
runners[0]["image"],
worker_image, release_obj.build.dryccfile)
self.assertEqual(
release_obj.get_deploy_image("web"),
default_image, release_obj.build.dryccfile)
self.assertEqual(
release_obj.get_deploy_image("worker"),
worker_image, release_obj.build.dryccfile)
self.assertEqual(
release_obj.get_deploy_image("worker-1"),
worker_image, release_obj.build.dryccfile)
self.assertEqual(
release_obj.get_deploy_image("worker-2"),
default_image, release_obj.build.dryccfile)
self.assertEqual(
release_obj.get_deploy_image("worker-3"),
default_image, release_obj.build.dryccfile)
self.assertEqual(
release_obj.get_deploy_image("worker-4"),
worker_4_image, release_obj.build.dryccfile)
def test_response_data(self, mock_requests):
app_id = self.create_app()
body = {'values': json.dumps({'NEW_URL': 'http://localhost:8080/'})}
url = '/v2/apps/{}/config'.format(app_id)
config_response = self.client.post(url, body)
url = '/v2/apps/{}/releases/v2'.format(app_id)
response = self.client.get(url)
for key in response.data.keys():
self.assertIn(key, ['uuid', 'owner', 'created', 'updated', 'app', 'build', 'config',
'summary', 'version', 'state', 'failed', 'conditions',
'exception'])
expected = {
'owner': self.user.username,
'app': app_id,
'build': None,
'config': uuid.UUID(config_response.data['uuid']),
'summary': '{} added values NEW_URL'.format(self.user.username),
'version': 2
}
self.assertEqual(response.data, response.data | expected)
def test_release_deploy(self, mock_requests):
app_id = self.create_app()
# try to rollback with only 1 release extant, expecting 400
url = f"/v2/apps/{app_id}/releases/deploy/"
# app.deploy
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.return_value = None
response = self.client.post(url, {"ptypes": "web"})
self.assertEqual(response.status_code, 400)
# post a new build
build_url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'stack': 'heroku-18',
'sha': 'a'*40,
'procfile': {
'web': 'node server.js',
'worker': 'node worker.js'
}
}
response = self.client.post(build_url, body)
self.assertEqual(response.status_code, 201)
# app.deploy
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.return_value = None
response = self.client.post(url, {"ptypes": "web"})
self.assertEqual(response.status_code, 204)
def test_release_rollback(self, mock_requests):
app_id = self.create_app()
app = App.objects.get(id=app_id)
# try to rollback with only 1 release extant, expecting 400
url = f"/v2/apps/{app_id}/releases/rollback/"
response = self.client.post(url)
self.assertEqual(response.status_code, 400, response.data)
self.assertEqual(response.data, {'detail': 'version cannot be below 0'})
self.assertEqual(response.get('content-type'), 'application/json')
# update the build to roll a new release
url = f'/v2/apps/{app_id}/builds'
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# update config to roll another release
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)
# create another release with a different build
url = f'/v2/apps/{app_id}/builds'
body = {'image': 'autotest/example:latest', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# rollback and check to see that a 5th release was created
# with the build and config of release #3
url = f"/v2/apps/{app_id}/releases/rollback/"
response = self.client.post(url)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(Release.objects.count(), 5)
Release.objects.get(app=app, version=1)
Release.objects.get(app=app, version=2)
release3 = Release.objects.get(app=app, version=3)
release4 = Release.objects.get(app=app, version=4)
release5 = Release.objects.get(app=app, version=5)
# verify the rollback to v3
self.assertNotEqual(release5.uuid, release3.uuid)
self.assertNotEqual(release5.build, release4.build)
self.assertEqual(release5.build, release3.build)
self.assertEqual(release5.config.values, release3.config.values)
# double-check to see that the current build and config is the same as v3
self.assertEqual(release5.get_deploy_image(PROCFILE_TYPE_WEB), 'autotest/example')
self.assertEqual(release5.config.values, {'NEW_URL1': 'http://localhost:8080/'})
# try to rollback to v1 and verify that the rollback failed
# (v1 is an initial release with no build)
url = f"/v2/apps/{app_id}/releases/rollback/"
body = {'version': 1}
response = self.client.post(url, body)
self.assertContains(response, 'Cannot roll back to initial release.', status_code=400)
# roll back to v2 so we can verify config gets rolled back too
url = f"/v2/apps/{app_id}/releases/rollback/"
body = {'version': 2}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(Release.objects.count(), 6)
release6 = Release.objects.get(app=app, version=6)
self.assertEqual(release6.get_deploy_image(PROCFILE_TYPE_WEB), 'autotest/example')
self.assertEqual(release6.config.values, {})
def test_release_str(self, mock_requests):
"""Test the text representation of a release."""
release3 = self.test_release()
release = Release.objects.get(uuid=release3['uuid'])
self.assertEqual(str(release), "{}-v3".format(release3['app']))
def test_release_summary(self, mock_requests):
"""Test the text summary of a release."""
release = self.test_release()
app = App.objects.get(id=release['app'])
release = app.release_set.latest()
# check that the release has push and env change messages
self.assertIn('autotest deployed ', release.summary)
# add config, confirm that config objects are in the summary
url = f'/v2/apps/{app.id}/config'
body = {
'values': json.dumps({'FOO': 'bar'}),
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(
'autotest added values FOO',
app.release_set.latest().summary)
def test_admin_can_create_release(self, mock_requests):
"""If a non-user creates an app, an admin should be able to create releases."""
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()
# check that updating config rolls a new release
url = f'/v2/apps/{app_id}/config'
body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})}
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('NEW_URL1', response.data['values'])
# check to see that an initial release was created
url = f'/v2/apps/{app_id}/releases'
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
# account for the config release as well
self.assertEqual(response.data['count'], 2)
def test_unauthorized_user_cannot_modify_release(self, mock_requests):
"""
An unauthorized user should not be able to modify other releases.
Since an unauthorized user should not know about the application at all, these
requests should return a 404.
"""
app_id = self.create_app()
# push a new build
url = f'/v2/apps/{app_id}/builds'
body = {'image': 'test', 'stack': 'container'}
response = self.client.post(url, body)
# update config to roll a new release
url = f'/v2/apps/{app_id}/config'
body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})}
response = self.client.post(url, body)
unauthorized_user = User.objects.get(username='autotest2')
unauthorized_token = self.get_or_create_token(unauthorized_user)
# try to rollback
self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
url = f'/v2/apps/{app_id}/releases/rollback/'
response = self.client.post(url)
self.assertEqual(response.status_code, 403)
def test_release_rollback_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'])
# update config to roll a new release
url = '/v2/apps/{}/config'.format(app_id)
body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# app.deploy exception
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.side_effect = Exception('Boom!')
url = "/v2/apps/{}/releases/rollback/".format(app_id)
body = {'version': 2}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
data = self.client.get(f"/v2/apps/{app_id}/releases/", body).json()
self.assertEqual(data["results"][0]["state"], "crashed", data)
# update config to roll a new release
url = '/v2/apps/{}/config'.format(app_id)
body = {'values': json.dumps({'NEW_URL2': 'http://localhost:8080/'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# try to rollback to v4 and verify that the rollback failed
# (v4 is a failed release)
url = f"/v2/apps/{app_id}/releases/rollback/"
body = {'version': 4}
response = self.client.post(url, body)
self.assertContains(response, 'Cannot roll back to failed release.', status_code=400)
# app.deploy exception followed by a KubeHTTPException of 404
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.side_effect = Exception('Boom!')
with mock.patch(
'api.models.release.Release._delete_release_in_scheduler') as mock_kube:
# instead of full request mocking, fake it out in a simple way
class Response(object):
def json(self):
return '{}'
response = Response()
response.status_code = 404
response.reason = "Not Found"
kube_exception = KubeHTTPException(response, 'big boom')
mock_kube.side_effect = kube_exception
url = "/v2/apps/{}/releases/rollback/".format(app_id)
body = {'version': 2}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
data = self.client.get(f"/v2/apps/{app_id}/releases/", body).json()
self.assertEqual(data["results"][0]["state"], "crashed", data)
def test_release_unset_config(self, mock_requests):
"""
Test that a release is created when an app is created, a config can be
set and then unset without causing a 409 (conflict)
"""
app_id = self.create_app()
# check that updating config rolls a new release
url = f'/v2/apps/{app_id}/config'
body = {'limits': json.dumps({'cmd': None})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 422, response.data)
def test_release_no_change(self, mock_requests):
"""
Test that a release is created when an app is created, and
then has 2 identical config set, causing a 409 as there was
no change
"""
app_id = self.create_app()
# check that updating config rolls a new release
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'])
# trigger identical release
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, 409, response.data)
def test_release_get_port(self, mock_requests):
"""
Test that get_port always returns the proper value.
"""
app_id = self.create_app()
app = App.objects.get(id=app_id)
url = f'/v2/apps/{app_id}/builds'
body = {'sha': '123456', 'image': 'autotest/example', 'stack': 'heroku-18'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
release = app.release_set.latest()
# when app is not routable, then it still return 5000
self.assertEqual(release.get_port('web'), 5000)
# switch to a dockerfile app or else it'll automatically default to 5000
url = f'/v2/apps/{app_id}/builds'
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f'/v2/apps/{app_id}/config'
body = {'values': json.dumps({'PORT': '8080'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
release = app.release_set.latest()
self.assertEqual(release.get_port('web'), 8080)
url = f'/v2/apps/{app_id}/config'
body = {'typed_values': json.dumps({"web": {'PORT': '9000'}})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
release = app.release_set.latest()
self.assertEqual(release.get_port('web'), 9000)
# not web procfile
self.assertEqual(release.get_port('task'), 8080)
url = f'/v2/apps/{app_id}/config'
body = {'values': json.dumps({'PORT': '9000'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
release = app.release_set.latest()
self.assertEqual(release.get_port('task'), 9000)
# set typed_values port
body = {'typed_values': json.dumps({"task": {'PORT': '9001'}})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
release = app.release_set.latest()
self.assertEqual(release.get_port('task'), 9001)
@override_settings(DRYCC_DEPLOY_HOOK_URLS=['http://drycc.rocks'])
@mock.patch('api.models.release.logger')
def test_deploy_hooks_logged(self, mock_requests, mock_logger):
"""
Verifies that a configured deploy hook is dumped into the logs when a release is created.
"""
app_id = 'foo'
body = {'sha': '123456', 'image': 'autotest/example', 'stack': 'heroku-18'}
mr_rocks = mock_requests.post(f'http://drycc.rocks?app={app_id}&user={self.user.username}&sha=&release=v1&release_summary={self.user.username}+created+initial+release') # noqa
self.create_app(app_id)
# check app logs
exp_msg = f"[{app_id}]: Sent deploy hook to http://drycc.rocks"
mock_logger.log.assert_any_call(logging.INFO, exp_msg)
self.assertTrue(mr_rocks.called)
self.assertEqual(mr_rocks.call_count, 1)
# override DRYCC_DEPLOY_HOOK_URLS again, ensuring that the new deploy hooks get the same
# treatment
url = f'/v2/apps/{app_id}/builds'
with self.settings(DRYCC_DEPLOY_HOOK_URLS=['http://drycc.ninja', 'http://cat.dog']):
mr_ninja = mock_requests.post(f"http://drycc.ninja?app={app_id}&user={self.user.username}&sha=123456&release=v2&release_summary={self.user.username}+deployed+123456") # noqa
mr_catdog = mock_requests.post(f"http://cat.dog?app={app_id}&user={self.user.username}&sha=123456&release=v2&release_summary={self.user.username}+deployed+123456") # noqa
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# check app logs
exp_msg = f"[{app_id}]: Sent deploy hook to http://drycc.ninja"
mock_logger.log.assert_any_call(logging.INFO, exp_msg)
self.assertTrue(mr_ninja.called)
self.assertEqual(mr_ninja.call_count, 1)
exp_msg = f"[{app_id}]: Sent deploy hook to http://cat.dog"
mock_logger.log.assert_any_call(logging.INFO, exp_msg)
self.assertTrue(mr_catdog.called)
self.assertEqual(mr_catdog.call_count, 1)
sha = '2345678'
body['sha'] = sha
# Ensure that when requests.Exception is raised, the error is noted and life carries on.
with self.settings(DRYCC_DEPLOY_HOOK_URLS=['http://cat.ninja', 'http://drycc.dog']):
def raise_callback(request, context):
raise requests.ConnectionError('poop')
mr_ninja = mock_requests.post(f"http://cat.ninja?app={app_id}&user={self.user.username}&sha={sha}&release=v3&release_summary={self.user.username}+deployed+{sha}", text=raise_callback) # noqa
mr_catdog = mock_requests.post(f"http://drycc.dog?app={app_id}&user={self.user.username}&sha={sha}&release=v3&release_summary={self.user.username}+deployed+{sha}") # noqa
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# check app logs
exp_msg = f"[{app_id}]: An error occurred while sending the deploy hook to http://cat.ninja: poop" # noqa
mock_logger.log.assert_any_call(logging.ERROR, exp_msg)
self.assertTrue(mr_ninja.called)
self.assertEqual(mr_ninja.call_count, 1)
exp_msg = f"[{app_id}]: Sent deploy hook to http://drycc.dog"
mock_logger.log.assert_any_call(logging.INFO, exp_msg)
self.assertTrue(mr_catdog.called)
self.assertEqual(mr_catdog.call_count, 1)
# ensure that when a secret key is used, a Drycc-Signature header is present
# which was generated by using HMAC-SHA1 against the target URL
secret = 'Hasta la vista, baby.'
hook_url = 'http://drycc.com'
sha = '3456789'
body['sha'] = sha
# target URL MUST be in the exact alphabetized order when calculating the HMAC signature.
target_url = '{}?app={}&release=v4&release_summary={}+deployed+{}&sha={}&user={}'.format(
hook_url,
app_id,
self.user.username,
sha,
sha,
self.user.username,
)
signature = hmac.new(
secret.encode('utf-8'),
target_url.encode('utf-8'),
hashlib.sha1,
).hexdigest()
request_headers = {'Authorization': signature}
with self.settings(DRYCC_DEPLOY_HOOK_SECRET_KEY=secret, DRYCC_DEPLOY_HOOK_URLS=[hook_url]):
mr_terminator = mock_requests.post(
target_url,
request_headers=request_headers,
)
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# check app logs
exp_msg = f"[{app_id}]: Sent deploy hook to {hook_url}"
mock_logger.log.assert_any_call(logging.INFO, exp_msg)
self.assertTrue(mr_terminator.called)
self.assertEqual(mr_terminator.call_count, 1)
@override_settings(REGISTRY_LOCATION="off-cluster")
def test_release_external_registry(self, mock_requests):
"""
Test that get_port always returns the proper value.
"""
app_id = self.create_app()
# set the required port for external registries
body = {'values': json.dumps({'PORT': '3000'})}
config_response = self.client.post('/v2/apps/{}/config'.format(app_id), body)
self.assertEqual(config_response.status_code, 201, config_response.data)
app = App.objects.get(id=app_id)
url = f'/v2/apps/{app_id}/builds'
body = {'image': 'test/autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
release = app.release_set.latest()
self.assertEqual(release.get_port('web'), 3000)
self.assertEqual(release.get_deploy_image(PROCFILE_TYPE_WEB), 'test/autotest/example')
url = f'/v2/apps/{app_id}/config'
body = {'typed_values': json.dumps({"web": {'PORT': '9000'}})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
release = app.release_set.latest()
self.assertEqual(release.get_port('web'), 9000)
@override_settings(DRYCC_PILELINE_RUN_TIMEOUT=600)
def test_run(self, mock_requests):
app_id = self.create_app()
app = App.objects.get(id=app_id)
user = User.objects.get(username='autotest')
dryccfile = {
"run": [
{
"args": ["sleep", "60s"],
"image": "registry.drycc.cc/drycc/base:bookworm-web",
"when": {"ptypes": ["web"]}
},
{
"args": ["sleep", "60s"],
"image": "registry.drycc.cc/drycc/base:bookworm-task",
"when": {"ptypes": ["task"]},
"timeout": 3000
}
],
"deploy": {
"web": {
"image": "registry.drycc.cc/drycc/python-dev",
"args": ["python", "-m", "http.server", "5000"]
},
"task": {
"image": "docker.io/library/nginx:mainline-bookworm-perl",
"command": ["sleep"],
"args": ["infinity"]
}
}
}
build = Build.objects.create(
owner=user, app=app, image="qwerty", procfile={},
sha='african-swallow', dockerfile={}, dryccfile=dryccfile)
release = Release.objects.create(
version=2, owner=user, app=app, config=app.config_set.latest(),
build=build, state="succeed")
runners = release.get_runners(["web"])
self.assertEqual(len(runners), 1)
self.assertEqual(runners[0]["timeout"], 600)
runners = release.get_runners(["task"])
self.assertEqual(len(runners), 1)
self.assertEqual(runners[0]["timeout"], 3000)
# change timeout
dryccfile['run'][0]['timeout'] = 3600
dryccfile['deploy']['web']['image'] = 'registry.drycc.cc/drycc/python-dev:latest'
build = Build.objects.create(
owner=user, app=app, image="qwerty", procfile={},
sha='african-swallow', dockerfile={}, dryccfile=dryccfile)
release = Release.objects.create(
version=3, owner=user, app=app, config=app.config_set.latest(),
build=build, state="succeed")
runners = release.get_runners(["web"])
self.assertEqual(len(runners), 1)
self.assertEqual(runners[0]["timeout"], 3600)