1111from unittest import mock
1212from rest_framework .authtoken .models import Token
1313
14- from api .models import Build
14+ from api .models import Build , App
1515from registry .dockerclient import RegistryException
1616from scheduler import KubeException
1717
@@ -401,7 +401,7 @@ def test_build_image_in_registry_with_auth_no_port(self, mock_requests):
401401
402402 def test_release_create_failure (self , mock_requests ):
403403 """
404- Cause an Exception in app.deploy to cause a release.delete in build.create
404+ Cause an Exception in app.deploy to cause a failed release in build.create
405405 """
406406 app_id = self .create_app ()
407407
@@ -422,7 +422,7 @@ def test_release_create_failure(self, mock_requests):
422422
423423 def test_release_registry_create_failure (self , mock_requests ):
424424 """
425- Cause a RegistryException in app.deploy to cause a release.delete in build.create
425+ Cause a RegistryException in app.deploy to cause a failed release in build.create
426426 """
427427 app_id = self .create_app ()
428428
@@ -454,3 +454,36 @@ def test_build_deploy_kube_failure(self, mock_requests):
454454 body = {'image' : 'autotest/example' }
455455 response = self .client .post (url , body )
456456 self .assertEqual (response .status_code , 400 , response .data )
457+
458+ def test_build_failures (self , mock_requests ):
459+ app_id = self .create_app ()
460+ app = App .objects .get (id = app_id )
461+
462+ # deploy app to get a build
463+ url = "/v2/apps/{app_id}/builds" .format (** locals ())
464+ body = {'image' : 'autotest/example' }
465+ response = self .client .post (url , body )
466+ self .assertEqual (response .status_code , 201 , response .data )
467+ self .assertEqual (response .data ['image' ], body ['image' ])
468+ success_build = app .release_set .latest ().build
469+
470+ # create a failed build to check that failed release is created
471+ with mock .patch ('api.models.App.deploy' ) as mock_deploy :
472+ mock_deploy .side_effect = Exception ('Boom!' )
473+
474+ url = "/v2/apps/{app_id}/builds" .format (** locals ())
475+ body = {'image' : 'autotest/example' }
476+ response = self .client .post (url , body )
477+ self .assertEqual (response .status_code , 400 , response .data )
478+ self .assertEqual (app .release_set .latest ().version , 3 )
479+ self .assertEqual (app .release_set .filter (failed = False ).latest ().version , 2 )
480+
481+ # create a config to see that the new release is created with the last successful build
482+ url = "/v2/apps/{app_id}/config" .format (** locals ())
483+
484+ body = {'values' : json .dumps ({'Test' : 'test' })}
485+ response = self .client .post (url , body )
486+ self .assertEqual (response .status_code , 201 , response .data )
487+ self .assertEqual (app .release_set .latest ().version , 4 )
488+ self .assertEqual (app .release_set .latest ().build , success_build )
489+ self .assertEqual (app .build_set .count (), 2 )
0 commit comments