|
15 | 15 | from rest_framework.authtoken.models import Token |
16 | 16 |
|
17 | 17 | from api.models import Build |
| 18 | +from registry.dockerclient import RegistryException |
| 19 | +from scheduler import KubeException |
18 | 20 |
|
19 | 21 | from . import adapter |
20 | 22 | import requests_mock |
@@ -346,3 +348,78 @@ def test_build_image_in_registry(self, mock_requests): |
346 | 348 | release = build.app.release_set.latest() |
347 | 349 | # Registry host + port is internally stripped off |
348 | 350 | self.assertEqual(release.image, 'autotest/example') |
| 351 | + |
| 352 | + def test_build_image_in_registry_with_auth(self, mock_requests): |
| 353 | + """add authentication to the build""" |
| 354 | + self.client.post('/v2/apps', {'id': 'test'}) |
| 355 | + |
| 356 | + # post an image as a build using registry hostname |
| 357 | + url = "/v2/apps/test/builds" |
| 358 | + image = 'autotest/example' |
| 359 | + response = self.client.post(url, {'image': image}) |
| 360 | + self.assertEqual(response.status_code, 201) |
| 361 | + |
| 362 | + # set some registry information |
| 363 | + url = '/v2/apps/test/config' |
| 364 | + body = {'registry': json.dumps({'username': 'bob', 'password': 'zoomzoom'})} |
| 365 | + response = self.client.post(url, body) |
| 366 | + self.assertEqual(response.status_code, 201) |
| 367 | + |
| 368 | + def test_release_create_failure(self, mock_requests): |
| 369 | + """ |
| 370 | + Cause an Exception in app.deploy to cause a release.delete in build.create |
| 371 | + """ |
| 372 | + body = {'id': 'test'} |
| 373 | + self.client.post('/v2/apps', body) |
| 374 | + |
| 375 | + # deploy app to get a build |
| 376 | + url = "/v2/apps/test/builds" |
| 377 | + body = {'image': 'autotest/example'} |
| 378 | + response = self.client.post(url, body) |
| 379 | + self.assertEqual(response.status_code, 201) |
| 380 | + self.assertEqual(response.data['image'], body['image']) |
| 381 | + |
| 382 | + with mock.patch('api.models.App.deploy') as mock_deploy: |
| 383 | + mock_deploy.side_effect = Exception('Boom!') |
| 384 | + |
| 385 | + url = "/v2/apps/test/builds" |
| 386 | + body = {'image': 'autotest/example'} |
| 387 | + response = self.client.post(url, body) |
| 388 | + self.assertEqual(response.status_code, 400) |
| 389 | + |
| 390 | + def test_release_registry_create_failure(self, mock_requests): |
| 391 | + """ |
| 392 | + Cause a RegistryException in app.deploy to cause a release.delete in build.create |
| 393 | + """ |
| 394 | + body = {'id': 'test'} |
| 395 | + self.client.post('/v2/apps', body) |
| 396 | + |
| 397 | + # deploy app to get a build |
| 398 | + url = "/v2/apps/test/builds" |
| 399 | + body = {'image': 'autotest/example'} |
| 400 | + response = self.client.post(url, body) |
| 401 | + self.assertEqual(response.status_code, 201) |
| 402 | + self.assertEqual(response.data['image'], body['image']) |
| 403 | + |
| 404 | + with mock.patch('api.models.Release.publish') as mock_registry: |
| 405 | + mock_registry.side_effect = RegistryException('Boom!') |
| 406 | + |
| 407 | + url = "/v2/apps/test/builds" |
| 408 | + body = {'image': 'autotest/example'} |
| 409 | + response = self.client.post(url, body) |
| 410 | + self.assertEqual(response.status_code, 400) |
| 411 | + |
| 412 | + def test_build_deploy_kube_failure(self, mock_requests): |
| 413 | + """ |
| 414 | + Cause an Exception in scheduler.deploy |
| 415 | + """ |
| 416 | + body = {'id': 'test'} |
| 417 | + self.client.post('/v2/apps', body) |
| 418 | + |
| 419 | + with mock.patch('scheduler.KubeHTTPClient.deploy') as mock_deploy: |
| 420 | + mock_deploy.side_effect = KubeException('Boom!') |
| 421 | + |
| 422 | + url = "/v2/apps/test/builds" |
| 423 | + body = {'image': 'autotest/example'} |
| 424 | + response = self.client.post(url, body) |
| 425 | + self.assertEqual(response.status_code, 400) |
0 commit comments