-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_build.py
More file actions
835 lines (732 loc) · 32.2 KB
/
test_build.py
File metadata and controls
835 lines (732 loc) · 32.2 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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
"""
Unit tests for the Drycc api app.
Run the tests with "./manage.py test api"
"""
import json
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.build import Build
from api.models.app import App
from api.models.base import PTYPE_WEB
from scheduler import KubeException
from api.tests import adapter, DryccTransactionTestCase
import requests_mock
User = get_user_model()
@requests_mock.Mocker(real_http=True, adapter=adapter)
class BuildTest(DryccTransactionTestCase):
"""Tests build 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_build(self, mock_requests):
"""
Test that a null build is created and that users can post new builds
"""
app_id = self.create_app()
# check to see that no initial build was created
url = f"/v2/apps/{app_id}/builds"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data['count'], 0)
# post a new build
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
build_id = str(response.data['uuid'])
build1 = response.data
self.assertEqual(response.data['image'], body['image'])
# read the build
url = f"/v2/apps/{app_id}/builds/{build_id}"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
build2 = response.data
self.assertEqual(build1, build2)
# post a new build
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)
build3 = response.data
self.assertEqual(response.data['image'], body['image'])
self.assertNotEqual(build2['uuid'], build3['uuid'])
# disallow put/patch/delete
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)
def test_response_data(self, mock_requests):
"""Test that the serialized response contains only relevant data."""
app_id = self.create_app()
# post an image as a build
url = f"/v2/apps/{app_id}/builds"
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
for key in response.data:
self.assertIn(key, ['uuid', 'owner', 'created', 'updated', 'app', 'dockerfile',
'dryccfile', 'image', 'stack', 'procfile', 'sha'])
expected = {
'owner': self.user.username,
'app': app_id,
'dockerfile': '',
'image': 'autotest/example',
'stack': 'container',
'procfile': {},
'sha': ''
}
self.assertEqual(response.data, expected | response.data)
def test_build_default_containers(self, mock_requests):
app_id = self.create_app()
# post an image as a build
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}/pods"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v2", "up")
# post an image as a build with a procfile
app_id = self.create_app()
# post an image as a build
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'stack': 'heroku-18',
'procfile': {
'web': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v2", "up")
# start with a new app
app_id = self.create_app()
# post a new build with procfile
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'container',
'dockerfile': "FROM scratch"
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v2", "up")
# start with a new app
app_id = self.create_app()
# post a new build with procfile
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'container',
'dockerfile': "FROM scratch",
'procfile': {
'worker': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v2", "up")
# start with a new app
app_id = self.create_app()
# post a new build with procfile
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'heroku-18',
'procfile': {
'web': 'node server.js',
'worker': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v2", "up")
# start with a new app
app_id = self.create_app()
# post a new build with procfile and no routable type
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'heroku-18',
'procfile': {
'rake': 'node server.js',
'worker': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
@override_settings(DRYCC_DEPLOY_PROCFILE_MISSING_REMOVE=True)
def test_build_forgotten_procfile(self, mock_requests):
"""
Test that when a user first posts a build with a Procfile
and then later without it that missing process are actually
scaled down
"""
# start with a new app
app_id = self.create_app()
# post a new build with procfile
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'heroku-18',
'procfile': {
'web': 'node server.js',
'worker': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# verify web
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v2", "up")
# scale worker
url = f"/v2/apps/{app_id}/ptypes/scale"
body = {'worker': 1}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 204, response.data)
# verify worker
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'worker', "v2", "up")
# do another deploy for this time forget Procfile
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)
# verify worker is not there
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(len(response.data['results']), 0)
# verify web is not there
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(len(response.data['results']), 0)
# look at the app structure
url = f"/v2/apps/{app_id}"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.json()['structure'], {'web': 0})
@override_settings(DRYCC_DEPLOY_PROCFILE_MISSING_REMOVE=False)
def test_build_no_remove_process(self, mock_requests):
"""
Specifically test PROCFILE_REMOVE_PROCS_ON_DEPLOY being turned off
and a user posting a new build without a Procfile that was previously
applied
"""
# start with a new app
app_id = self.create_app()
# post a new build with procfile
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'heroku-18',
'procfile': {
'web': 'node server.js',
'worker': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# scale worker
url = f"/v2/apps/{app_id}/ptypes/scale"
body = {'worker': 1}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 204, response.data)
# verify worker
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'worker', "v2", "up")
# do another deploy for this time forget Procfile
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)
# verify worker is still there
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'worker', "v3", "up")
# verify web is still there
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v3", "up")
# look at the app structure
url = f"/v2/apps/{app_id}"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.json()['structure'], {'web': 1, 'worker': 1})
# scale worker to make sure no info was lost
url = f"/v2/apps/{app_id}/ptypes/scale"
body = {'worker': 2} # bump from 1 to 2
response = self.client.post(url, body)
self.assertEqual(response.status_code, 204, response.data)
# verify worker info
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'worker', "v3", "up")
@override_settings(DRYCC_DEPLOY_REJECT_IF_PROCFILE_MISSING=True)
def test_build_forgotten_procfile_reject(self, mock_requests):
"""
Test that when a user first posts a build with a Procfile
and then later without it that missing process are actually
scaled down
"""
# start with a new app
app_id = self.create_app()
# post a new build with procfile
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(url, body)
self.assertEqual(response.status_code, 201, response.data)
# scale worker
url = f"/v2/apps/{app_id}/ptypes/scale"
body = {'worker': 1}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 204, response.data)
# verify worker
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'worker', "v2", "up")
# do another deploy for this time forget Procfile
url = f"/v2/apps/{app_id}/builds"
body = {'image': 'autotest/example', 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 409, response.data)
def test_build_str(self, mock_requests):
"""Test the text representation of a build."""
app_id = self.create_app()
# post a new build
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)
build = Build.objects.get(uuid=response.data['uuid'])
self.assertEqual(str(build), "{}-{}".format(
response.data['app'], str(response.data['uuid'])[:7]))
def test_admin_can_create_builds_on_other_apps(self, mock_requests):
"""If a user creates an application, an administrator should be able
to push builds.
"""
# create app as non-admin
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()
# post a new build as admin
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
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)
build = Build.objects.get(uuid=response.data['uuid'])
self.assertEqual(str(build), "{}-{}".format(
response.data['app'], str(response.data['uuid'])[:7]))
def test_unauthorized_user_cannot_modify_build(self, mock_requests):
"""
An unauthorized user should not be able to modify other builds.
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/{}/builds'.format(app_id)
body = {'image': 'foo'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 403)
def test_new_build_does_not_scale_up_automatically(self, mock_requests):
"""
After the first initial deploy, if the containers are scaled down to zero,
they should stay that way on a new release.
"""
app_id = self.create_app()
# post a new build
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'heroku-18',
'procfile': {
'web': 'node server.js',
'worker': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertPodContains(response.data['results'], app_id, 'web', "v2", "up")
# scale to zero
url = f"/v2/apps/{app_id}/ptypes/scale"
body = {'web': 0}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 204, response.data)
# post another build
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'sha': 'a'*40,
'stack': 'heroku-18',
'procfile': {
'web': 'node server.js',
'worker': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
url = f"/v2/apps/{app_id}/pods/"
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(len(response.data['results']), 0)
def test_build_image_in_registry(self, mock_requests):
"""When the image is already in the drycc registry no pull/tag/push happens"""
app_id = self.create_app()
# post an image as a build using registry hostname
url = f"/v2/apps/{app_id}/builds"
image = '127.0.0.1:5000/autotest/example'
body = {'image': image, 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
build = Build.objects.get(uuid=response.data['uuid'])
release = build.app.release_set.latest()
self.assertEqual(release.get_deploy_image(PTYPE_WEB), image)
# post an image as a build using registry hostname + port
url = f"/v2/apps/{app_id}/builds"
image = '127.0.0.1:5000/autotest/example'
body = {'image': image, 'stack': 'container'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
build = Build.objects.get(uuid=response.data['uuid'])
release = build.app.release_set.latest()
self.assertEqual(release.get_deploy_image(PTYPE_WEB), image)
def test_build_image_in_registry_with_auth(self, mock_requests):
"""add authentication to the build"""
app_id = self.create_app()
# post an image as a build using registry hostname
url = f"/v2/apps/{app_id}/builds"
image = 'autotest/example'
response = self.client.post(url, {'image': image, 'stack': 'container'})
self.assertEqual(response.status_code, 201, response.data)
# add the required PORT information
url = f'/v2/apps/{app_id}/config'
body = {'values': json.dumps({'PORT': '80'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# set some registry information
url = f'/v2/apps/{app_id}/config'
body = {'registry': json.dumps({'username': 'bob', 'password': 'zoomzoom'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
def test_build_image_in_registry_with_auth_no_port(self, mock_requests):
"""add authentication to the build but with no PORT config"""
app_id = self.create_app()
# post an image as a build using registry hostname
url = f"/v2/apps/{app_id}/builds"
image = 'autotest/example'
response = self.client.post(url, {'image': image, 'stack': 'container'})
self.assertEqual(response.status_code, 201, response.data)
# set some registry information
url = f'/v2/apps/{app_id}/config'
body = {'registry': json.dumps({'username': 'bob', 'password': 'zoomzoom'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
def test_release_create_failure(self, mock_requests):
"""
Cause an Exception in app.deploy to cause a failed release in build.create
"""
app_id = self.create_app()
# deploy app to get a build
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'])
with mock.patch('api.models.app.App.deploy') as mock_deploy:
mock_deploy.side_effect = Exception('Boom!')
url = f"/v2/apps/{app_id}/builds"
body = {'image': 'autotest/example'}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
def test_build_deploy_kube_failure(self, mock_requests):
"""
Cause an Exception in scheduler.deploy
"""
app_id = self.create_app()
with mock.patch('scheduler.KubeHTTPClient.deploy') as mock_deploy:
mock_deploy.side_effect = KubeException('Boom!')
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)
data = self.client.get(f"/v2/apps/{app_id}/releases/", body).json()
self.assertEqual(data["results"][0]["state"], "crashed", data)
def test_build_failures(self, mock_requests):
app_id = self.create_app()
app = App.objects.get(id=app_id)
# deploy app to get a build
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'])
success_build = app.release_set.latest().build
# create a failed build 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}/builds"
body = {'image': 'autotest/example', 'stack': 'container'}
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)
# create a config to see that the new release is created with the last successful build
url = f"/v2/apps/{app_id}/config"
body = {'values': json.dumps({'Test': 'test'})}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(app.release_set.latest().version, 4)
self.assertEqual(app.release_set.latest().build, success_build)
self.assertEqual(app.build_set.count(), 2)
def test_build_validate_procfile(self, mock_requests):
app_id = self.create_app()
# deploy app with incorrect proctype
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'stack': 'heroku-18',
'sha': 'a'*40,
'procfile': {
'web': 'node server.js',
'worker_test': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
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, 400, response.data)
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'stack': 'heroku-18',
'sha': 'a'*40,
'procfile': {
'web': 'node server.js',
'-': 'node worker.js'
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
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(url, body)
self.assertEqual(response.status_code, 400, response.data)
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(url, body)
self.assertEqual(response.status_code, 400, response.data)
# deploy app with empty command
url = f"/v2/apps/{app_id}/builds"
body = {
'image': 'autotest/example',
'stack': 'heroku-18',
'sha': 'a'*40,
'procfile': {
'web': 'node server.js',
'worker': ''
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
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)
def test_dryccfile_ok(self, mock_requests):
app_id = self.create_app()
url = f"/v2/apps/{app_id}/builds"
default_image = "autotest/example"
run_image = "127.0.0.1:7070/myapp/run:git-123fsa1"
web_image = "127.0.0.1:7070/myapp/web:git-123fsa1"
worker_image = "127.0.0.1:7070/myapp/worker:git-123fsa1"
body = {
'image': 'autotest/example',
'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": run_image,
}
],
"deploy": {
"web": {
"command": ["bash", "-c"],
"args": ["bundle exec puma -C config/puma.rb"],
"image": "127.0.0.1:7070/myapp/web:git-123fsa1"
},
"worker": {
"command": ["bash", "-c"],
"args": ["python myworker.py"],
"image": "127.0.0.1:7070/myapp/worker: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)
data = self.client.get(f"/v2/apps/{app_id}/releases/", body).json()
self.assertEqual(data["results"][0]["state"], "succeed", data)
app = App.objects.get(id=app_id)
for pod in app.list_pods():
if pod['type'] == 'run':
self.assertEqual(pod["state"], "down", pod)
elif pod['type'] == 'web':
self.assertEqual(pod["state"], "up", pod)
release = self.client.get(f"/v2/apps/{app_id}/releases/", body).json()["results"][0]
self.assertEqual(release["state"], "succeed", data)
self.assertEqual(release["version"], 2, data)
release_obj = app.release_set.filter(version=release["version"])[0]
runners = release_obj.get_runners(["web"])
self.assertEqual(len(runners), 1)
self.assertEqual(runners[0]["image"], run_image, data)
self.assertEqual(release_obj.get_deploy_image("web"), web_image, data)
self.assertEqual(release_obj.get_deploy_image("worker"), worker_image, data)
self.assertEqual(release_obj.get_deploy_image("noexist"), default_image, data)
def test_dryccfile_format(self, mock_requests):
body = {
'image': 'autotest/example',
'stack': 'heroku-18',
'sha': 'a'*40,
'dryccfile': {
"build": {
"docker": {"web": "Dockerfile", "worker": "worker/Dockerfile"},
"config": {"RAILS_ENV": "development", "FOO": "bar"}
},
}
}
with mock.patch('scheduler.resources.pod.Pod.watch') as mock_kube:
mock_kube.return_value = ['up', 'down']
app_id = self.create_app()
url = f"/v2/apps/{app_id}/builds"
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
body['dryccfile']['deploy'] = {
"web-new": {
'image': "127.0.0.1/cat/cat"
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
del body['dryccfile']['deploy']['web-new']
body['dryccfile']['deploy'] = {
"web": {
'image': "127.0.0.1/cat/cat"
}
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
body['dryccfile']['run'] = [
{
'command': ["bash", "-c"],
'args': ["ls /"]
}
]
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
body['dryccfile']['deploy'] = {}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 400, response.data)
body['dryccfile'] = {}
body['procfile'] = {
'web': 'node server.js',
'worker-test1': 'node worker.js'
}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)