def test_app_delete_missing_namespace(self, mock_requests): """ Create an app and then delete but have namespace missing Should still succeed """ # create app_id = self.create_app() with mock.patch('scheduler.resources.namespace.Namespace.get') 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 response = self.client.delete('/v2/apps/{}'.format(app_id)) self.assertEqual(response.status_code, 204, response.data) # verify that app is gone response = self.client.get('/v2/apps/{}'.format(app_id)) self.assertEqual(response.status_code, 404, response.data)
def test_release_rollback_failure(self, mock_requests): """ Cause an Exception in app.deploy to cause a release.delete """ body = {'id': 'test'} self.client.post('/v2/apps', body) # deploy app to get a build url = "/v2/apps/test/builds" body = {'image': 'autotest/example'} 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/test/config' body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})} response = self.client.post(url, body) self.assertEqual(response.status_code, 201, response.data) # update config to roll a new release url = '/v2/apps/test/config' body = {'values': json.dumps({'NEW_URL2': '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.deploy') as mock_deploy: mock_deploy.side_effect = Exception('Boom!') url = "/v2/apps/test/releases/rollback/" body = {'version': 2} response = self.client.post(url, body) self.assertEqual(response.status_code, 400, response.data) # app.deploy exception followed by a KubeHTTPException of 404 with mock.patch('api.models.App.deploy') as mock_deploy: mock_deploy.side_effect = Exception('Boom!') with mock.patch('api.models.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/test/releases/rollback/" body = {'version': 2} response = self.client.post(url, body) self.assertEqual(response.status_code, 400, response.data)
def test_release_rollback_failure(self, mock_requests): """ Cause an Exception in app.deploy to cause a release.delete """ 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'} 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.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, 400, response.data) self.assertEqual(app.release_set.latest().version, 4) # 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 = "/v2/apps/{app_id}/releases/rollback/".format(**locals()) 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.deploy') as mock_deploy: mock_deploy.side_effect = Exception('Boom!') with mock.patch('api.models.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, 400, response.data)