77from unittest import mock
88import requests
99
10+ from django .conf import settings
1011from django .contrib .auth .models import User
1112from django .core .cache import cache
1213from rest_framework .test import APITestCase
@@ -27,6 +28,7 @@ def _mock_run(*args, **kwargs):
2728
2829
2930@requests_mock .Mocker (real_http = True , adapter = adapter )
31+ @mock .patch ('api.models.release.publish_release' , lambda * args : None )
3032class AppTest (APITestCase ):
3133 """Tests creation of applications"""
3234
@@ -382,6 +384,101 @@ def test_app_exists_in_kubernetes(self, mock_requests):
382384 status_code = 409
383385 )
384386
387+ def test_app_verify_application_health_success (self , mock_requests ):
388+ """
389+ Create an application which in turn causes a health check to run against
390+ the router. Make it succeed on the 6th try
391+ """
392+ responses = [
393+ {'text' : 'Not Found' , 'status_code' : 404 },
394+ {'text' : 'Not Found' , 'status_code' : 404 },
395+ {'text' : 'Not Found' , 'status_code' : 404 },
396+ {'text' : 'Not Found' , 'status_code' : 404 },
397+ {'text' : 'Not Found' , 'status_code' : 404 },
398+ {'text' : 'OK' , 'status_code' : 200 }
399+ ]
400+ hostname = 'http://{}:{}/' .format (settings .ROUTER_HOST , settings .ROUTER_PORT )
401+ mr = mock_requests .register_uri ('GET' , hostname , responses )
402+
403+ # create app
404+ body = {'id' : 'myid' }
405+ response = self .client .post ('/v2/apps' , body )
406+ self .assertEqual (response .status_code , 201 )
407+
408+ # deploy app to get verification
409+ url = "/v2/apps/myid/builds"
410+ body = {'image' : 'autotest/example' }
411+ response = self .client .post (url , body )
412+ self .assertEqual (response .status_code , 201 )
413+ self .assertEqual (response .data ['image' ], body ['image' ])
414+
415+ self .assertEqual (mr .called , True )
416+ self .assertEqual (mr .call_count , 6 )
417+
418+ def test_app_verify_application_health_failure_404 (self , mock_requests ):
419+ """
420+ Create an application which in turn causes a health check to run against
421+ the router. Make it fail with a 404 after 10 tries
422+ """
423+ # function tries to hit router 10 times
424+ responses = [
425+ {'text' : 'Not Found' , 'status_code' : 404 },
426+ {'text' : 'Not Found' , 'status_code' : 404 },
427+ {'text' : 'Not Found' , 'status_code' : 404 },
428+ {'text' : 'Not Found' , 'status_code' : 404 },
429+ {'text' : 'Not Found' , 'status_code' : 404 },
430+ {'text' : 'Not Found' , 'status_code' : 404 },
431+ {'text' : 'Not Found' , 'status_code' : 404 },
432+ {'text' : 'Not Found' , 'status_code' : 404 },
433+ {'text' : 'Not Found' , 'status_code' : 404 },
434+ {'text' : 'Not Found' , 'status_code' : 404 },
435+ ]
436+ hostname = 'http://{}:{}/' .format (settings .ROUTER_HOST , settings .ROUTER_PORT )
437+ mr = mock_requests .register_uri ('GET' , hostname , responses )
438+
439+ # create app
440+ body = {'id' : 'myid' }
441+ response = self .client .post ('/v2/apps' , body )
442+ self .assertEqual (response .status_code , 201 )
443+
444+ # deploy app to get verification
445+ url = "/v2/apps/myid/builds"
446+ body = {'image' : 'autotest/example' }
447+ response = self .client .post (url , body )
448+ self .assertEqual (response .status_code , 201 )
449+ self .assertEqual (response .data ['image' ], body ['image' ])
450+
451+ self .assertEqual (mr .called , True )
452+ self .assertEqual (mr .call_count , 10 )
453+
454+ def test_app_verify_application_health_failure_exceptions (self , mock_requests ):
455+ """
456+ Create an application which in turn causes a health check to run against
457+ the router. Make it fail with a python-requets exception
458+ """
459+ def _raise_exception (request , ctx ):
460+ raise requests .exceptions .RequestException ('Boom!' )
461+
462+ # function tries to hit router 10 times
463+ hostname = 'http://{}:{}/' .format (settings .ROUTER_HOST , settings .ROUTER_PORT )
464+ mr = mock_requests .register_uri ('GET' , hostname , text = _raise_exception )
465+
466+ # create app
467+ body = {'id' : 'myid' }
468+ response = self .client .post ('/v2/apps' , body )
469+ self .assertEqual (response .status_code , 201 )
470+
471+ # deploy app to get verification
472+ url = "/v2/apps/myid/builds"
473+ body = {'image' : 'autotest/example' }
474+ response = self .client .post (url , body )
475+ self .assertEqual (response .status_code , 201 )
476+ self .assertEqual (response .data ['image' ], body ['image' ])
477+
478+ # Called 10 times due to the exception
479+ self .assertEqual (mr .called , True )
480+ self .assertEqual (mr .call_count , 10 )
481+
385482FAKE_LOG_DATA = """
3864832013-08-15 12:41:25 [33454] [INFO] Starting gunicorn 17.5
3874842013-08-15 12:41:25 [33454] [INFO] Listening at: http://0.0.0.0:5000 (33454)
0 commit comments