Skip to content

Commit 780eb92

Browse files
authored
Merge pull request #947 from helgi/cleanup_tests
Cleanup tests and start splitting them out
2 parents 5e647b2 + 66977d1 commit 780eb92

38 files changed

Lines changed: 1082 additions & 1397 deletions

rootfs/api/tests/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from django.conf import settings
88
from django.test.runner import DiscoverRunner
9+
from rest_framework.test import APITestCase, APITransactionTestCase
910

1011

1112
def mock_port(*args, **kwargs):
@@ -53,3 +54,27 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
5354
logging.disable(logging.ERROR)
5455
return super(SilentDjangoTestSuiteRunner, self).run_tests(
5556
test_labels, extra_tests, **kwargs)
57+
58+
59+
class DeisTestCase(APITestCase):
60+
def create_app(self, name=None):
61+
body = {}
62+
if name:
63+
body = {'id': name}
64+
65+
response = self.client.post('/v2/apps', body)
66+
self.assertEqual(response.status_code, 201, response.data)
67+
self.assertIn('id', response.data)
68+
return response.data['id']
69+
70+
71+
class DeisTransactionTestCase(APITransactionTestCase):
72+
def create_app(self, name=None):
73+
body = {}
74+
if name:
75+
body = {'id': name}
76+
77+
response = self.client.post('/v2/apps', body)
78+
self.assertEqual(response.status_code, 201, response.data)
79+
self.assertIn('id', response.data)
80+
return response.data['id']

rootfs/api/tests/deployments/test_app.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
from django.contrib.auth.models import User
1212
from django.core.cache import cache
1313
from django.test import override_settings
14-
from rest_framework.test import APITestCase
1514
from rest_framework.authtoken.models import Token
1615

1716
from api.models import App
1817
from scheduler import KubeException
1918

20-
from api.tests import adapter
21-
from api.tests import mock_port
19+
from api.tests import adapter, mock_port, DeisTestCase
2220
import requests_mock
2321

2422

@@ -34,7 +32,7 @@ def _mock_run(*args, **kwargs):
3432
@requests_mock.Mocker(real_http=True, adapter=adapter)
3533
@mock.patch('api.models.release.publish_release', lambda *args: None)
3634
@mock.patch('api.models.release.docker_get_port', mock_port)
37-
class AppTest(APITestCase):
35+
class AppTest(DeisTestCase):
3836
"""Tests creation of applications"""
3937

4038
fixtures = ['tests.json']
@@ -52,14 +50,7 @@ def test_app(self, mock_requests):
5250
"""
5351
Test that a user can create, read, update and delete an application
5452
"""
55-
url = '/v2/apps'
56-
response = self.client.post(url)
57-
self.assertEqual(response.status_code, 201, response.data)
58-
app_id = response.data['id'] # noqa
59-
self.assertIn('id', response.data)
60-
response = self.client.get('/v2/apps')
61-
self.assertEqual(response.status_code, 200, response.data)
62-
self.assertEqual(len(response.data['results']), 1)
53+
app_id = self.create_app()
6354
url = '/v2/apps/{app_id}'.format(**locals())
6455
response = self.client.get(url)
6556
self.assertEqual(response.status_code, 200, response.data)
@@ -186,12 +177,7 @@ def test_app_reserved_names(self, mock_requests):
186177

187178
def test_app_structure_is_valid_json(self, mock_requests):
188179
"""Application structures should be valid JSON objects."""
189-
url = '/v2/apps'
190-
response = self.client.post(url)
191-
self.assertEqual(response.status_code, 201, response.data)
192-
app_id = response.data['id']
193-
self.assertIn('structure', response.data)
194-
self.assertEqual(response.data['structure'], {})
180+
app_id = self.create_app()
195181
app = App.objects.get(id=app_id)
196182
app.structure = {'web': 1}
197183
app.save()

rootfs/api/tests/deployments/test_build.py

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,22 @@
1111
from django.core.cache import cache
1212
from django.conf import settings
1313
from django.test import override_settings
14-
from rest_framework.test import APITransactionTestCase
1514
from unittest import mock
1615
from rest_framework.authtoken.models import Token
1716

1817
from api.models import Build
1918
from registry.dockerclient import RegistryException
2019
from scheduler import KubeException
2120

22-
from api.tests import adapter
23-
from api.tests import mock_port
21+
from api.tests import adapter, mock_port, DeisTransactionTestCase
2422
import requests_mock
2523

2624

2725
@override_settings(DEIS_KUBERNETES_DEPLOYMENTS='1')
2826
@requests_mock.Mocker(real_http=True, adapter=adapter)
2927
@mock.patch('api.models.release.publish_release', lambda *args: None)
3028
@mock.patch('api.models.release.docker_get_port', mock_port)
31-
class BuildTest(APITransactionTestCase):
29+
class BuildTest(DeisTransactionTestCase):
3230

3331
"""Tests build notification from build system"""
3432

@@ -47,10 +45,7 @@ def test_build(self, mock_requests):
4745
"""
4846
Test that a null build is created and that users can post new builds
4947
"""
50-
url = '/v2/apps'
51-
response = self.client.post(url)
52-
self.assertEqual(response.status_code, 201, response.data)
53-
app_id = response.data['id']
48+
app_id = self.create_app()
5449
# check to see that no initial build was created
5550
url = "/v2/apps/{app_id}/builds".format(**locals())
5651
response = self.client.get(url)
@@ -109,10 +104,7 @@ def test_response_data(self, mock_requests):
109104
self.assertDictContainsSubset(expected, response.data)
110105

111106
def test_build_default_containers(self, mock_requests):
112-
url = '/v2/apps'
113-
response = self.client.post(url)
114-
self.assertEqual(response.status_code, 201, response.data)
115-
app_id = response.data['id']
107+
app_id = self.create_app()
116108
# post an image as a build
117109
url = "/v2/apps/{app_id}/builds".format(**locals())
118110
body = {'image': 'autotest/example'}
@@ -133,10 +125,7 @@ def test_build_default_containers(self, mock_requests):
133125
self.assertRegex(container['name'], app_id + '-v2-cmd-[a-z0-9]{5}')
134126

135127
# start with a new app
136-
url = '/v2/apps'
137-
response = self.client.post(url)
138-
self.assertEqual(response.status_code, 201, response.data)
139-
app_id = response.data['id']
128+
app_id = self.create_app()
140129
# post a new build with procfile
141130
url = "/v2/apps/{app_id}/builds".format(**locals())
142131
body = {
@@ -161,10 +150,7 @@ def test_build_default_containers(self, mock_requests):
161150
self.assertRegex(container['name'], app_id + '-v2-cmd-[a-z0-9]{5}')
162151

163152
# start with a new app
164-
url = '/v2/apps'
165-
response = self.client.post(url)
166-
self.assertEqual(response.status_code, 201, response.data)
167-
app_id = response.data['id']
153+
app_id = self.create_app()
168154

169155
# post a new build with procfile
170156
url = "/v2/apps/{app_id}/builds".format(**locals())
@@ -193,10 +179,7 @@ def test_build_default_containers(self, mock_requests):
193179
self.assertRegex(container['name'], app_id + '-v2-cmd-[a-z0-9]{5}')
194180

195181
# start with a new app
196-
url = '/v2/apps'
197-
response = self.client.post(url)
198-
self.assertEqual(response.status_code, 201, response.data)
199-
app_id = response.data['id']
182+
app_id = self.create_app()
200183
# post a new build with procfile
201184

202185
url = "/v2/apps/{app_id}/builds".format(**locals())
@@ -226,10 +209,7 @@ def test_build_default_containers(self, mock_requests):
226209

227210
def test_build_str(self, mock_requests):
228211
"""Test the text representation of a build."""
229-
url = '/v2/apps'
230-
response = self.client.post(url)
231-
self.assertEqual(response.status_code, 201, response.data)
232-
app_id = response.data['id']
212+
app_id = self.create_app()
233213
# post a new build
234214
url = "/v2/apps/{app_id}/builds".format(**locals())
235215
body = {'image': 'autotest/example'}
@@ -248,10 +228,7 @@ def test_admin_can_create_builds_on_other_apps(self, mock_requests):
248228
token = Token.objects.get(user=user).key
249229
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
250230

251-
url = '/v2/apps'
252-
response = self.client.post(url)
253-
self.assertEqual(response.status_code, 201, response.data)
254-
app_id = response.data['id']
231+
app_id = self.create_app()
255232

256233
# post a new build as admin
257234
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
@@ -289,10 +266,7 @@ def test_new_build_does_not_scale_up_automatically(self, mock_requests):
289266
After the first initial deploy, if the containers are scaled down to zero,
290267
they should stay that way on a new release.
291268
"""
292-
url = '/v2/apps'
293-
response = self.client.post(url)
294-
self.assertEqual(response.status_code, 201, response.data)
295-
app_id = response.data['id']
269+
app_id = self.create_app()
296270

297271
# post a new build
298272
url = "/v2/apps/{app_id}/builds".format(**locals())

rootfs/api/tests/deployments/test_certificate.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
from django.contrib.auth.models import User
22
from django.core.cache import cache
33
from django.test import override_settings
4-
from rest_framework.test import APITestCase
54
from rest_framework.authtoken.models import Token
65
from django.core.exceptions import SuspiciousOperation
76

87
from api.models import App, Certificate
9-
from api.tests import TEST_ROOT
8+
from api.tests import TEST_ROOT, DeisTestCase
109

1110

1211
@override_settings(DEIS_KUBERNETES_DEPLOYMENTS='1')
13-
class CertificateTest(APITestCase):
12+
class CertificateTest(DeisTestCase):
1413

1514
"""Tests creation of domain SSL certificates"""
1615

rootfs/api/tests/deployments/test_certificate_use_case_1.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from django.contrib.auth.models import User
22
from django.core.cache import cache
33
from django.test import override_settings
4-
from rest_framework.test import APITestCase
54
from rest_framework.authtoken.models import Token
65

76
from api.models import App, Certificate, Domain
8-
from api.tests import TEST_ROOT
7+
from api.tests import TEST_ROOT, DeisTestCase
98

109

1110
@override_settings(DEIS_KUBERNETES_DEPLOYMENTS='1')
12-
class CertificateUseCase1Test(APITestCase):
11+
class CertificateUseCase1Test(DeisTestCase):
1312

1413
"""
1514
Tests creation of domain SSL certificate and attach the

rootfs/api/tests/deployments/test_certificate_use_case_2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from django.contrib.auth.models import User
22
from django.core.cache import cache
33
from django.test import override_settings
4-
from rest_framework.test import APITestCase
54
from rest_framework.authtoken.models import Token
65

76
from api.models import App, Certificate, Domain
8-
from api.tests import TEST_ROOT
7+
from api.tests import TEST_ROOT, DeisTestCase
98

109

1110
@override_settings(DEIS_KUBERNETES_DEPLOYMENTS='1')
12-
class CertificateUseCase2Test(APITestCase):
11+
class CertificateUseCase2Test(DeisTestCase):
1312

1413
"""
1514
Tests creation of 2 domains and SSL certificate.

rootfs/api/tests/deployments/test_certificate_use_case_3.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from django.contrib.auth.models import User
22
from django.core.cache import cache
33
from django.test import override_settings
4-
from rest_framework.test import APITestCase
54
from rest_framework.authtoken.models import Token
65

76
from api.models import App, Certificate, Domain
8-
from api.tests import TEST_ROOT
7+
from api.tests import TEST_ROOT, DeisTestCase
98

109

1110
@override_settings(DEIS_KUBERNETES_DEPLOYMENTS='1')
12-
class CertificateUseCase3Test(APITestCase):
11+
class CertificateUseCase3Test(DeisTestCase):
1312

1413
"""
1514
Tests creation of 2 domains and 2 SSL certificate.

rootfs/api/tests/deployments/test_certificate_use_case_4.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from django.contrib.auth.models import User
22
from django.core.cache import cache
33
from django.test import override_settings
4-
from rest_framework.test import APITestCase
54
from rest_framework.authtoken.models import Token
65

76
from api.models import App, Certificate, Domain
8-
from api.tests import TEST_ROOT
7+
from api.tests import TEST_ROOT, DeisTestCase
98

109

1110
@override_settings(DEIS_KUBERNETES_DEPLOYMENTS='1')
12-
class CertificateUseCase4Test(APITestCase):
11+
class CertificateUseCase4Test(DeisTestCase):
1312

1413
"""
1514
Tests creation of 3 domains (one is a wildcard) and 3 SSL certificate (no wildcards).

rootfs/api/tests/deployments/test_certificate_use_case_5.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from django.contrib.auth.models import User
22
from django.core.cache import cache
33
from django.test import override_settings
4-
from rest_framework.test import APITestCase
54
from rest_framework.authtoken.models import Token
65

76
from api.models import App, Certificate, Domain
8-
from api.tests import TEST_ROOT
7+
from api.tests import TEST_ROOT, DeisTestCase
98

109

1110
@override_settings(DEIS_KUBERNETES_DEPLOYMENTS='1')
12-
class CertificateUseCase5Test(APITestCase):
11+
class CertificateUseCase5Test(DeisTestCase):
1312

1413
"""
1514
Tests creation of 3 domains (one is a wildcard) and 2 SSL certificate (one is a wildcard).

0 commit comments

Comments
 (0)