Skip to content

Commit cf231e6

Browse files
committed
Merge pull request #4533 from mboersma/fixup-controller-tests
ref(controller): remove REGISTRY_MODULE hack
2 parents c2dedfb + 865b9b3 commit cf231e6

16 files changed

Lines changed: 52 additions & 102 deletions

File tree

controller/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ setup-venv:
7474
venv/bin/pip install --disable-pip-version-check -q -r requirements.txt -r dev_requirements.txt
7575

7676
test-style: setup-venv
77-
venv/bin/flake8
77+
venv/bin/flake8 --show-pep8 --show-source
7878
shellcheck $(SHELL_SCRIPTS)
7979

8080
test-unit: setup-venv test-style

controller/api/models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,7 @@ def publish(self, source_version='latest'):
867867
source_tag = 'git-{}'.format(self.build.sha) if self.build.sha else source_version
868868
source_image = '{}:{}'.format(self.build.image, source_tag)
869869
# IOW, this image did not come from the builder
870-
# FIXME: remove check for mock registry module
871-
if not self.build.sha and 'mock' not in settings.REGISTRY_MODULE:
870+
if not self.build.sha:
872871
# we assume that the image is not present on our registry,
873872
# so shell out a task to pull in the repository
874873
data = {

controller/api/tests/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.conf import settings
77
from django.test.client import RequestFactory, Client
88
from django.test.simple import DjangoTestSuiteRunner
9+
import requests
910

1011

1112
# add patch support to built-in django test client
@@ -46,18 +47,25 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
4647
test_labels, extra_tests, **kwargs)
4748

4849

50+
def mock_status_ok(*args, **kwargs):
51+
resp = requests.Response()
52+
resp.status_code = 200
53+
resp._content_consumed = True
54+
return resp
55+
56+
4957
from .test_api_middleware import * # noqa
5058
from .test_app import * # noqa
5159
from .test_auth import * # noqa
5260
from .test_build import * # noqa
53-
from .test_config import * # noqa
54-
from .test_domain import * # noqa
5561
from .test_certificate import * # noqa
62+
from .test_config import * # noqa
5663
from .test_container import * # noqa
64+
from .test_domain import * # noqa
5765
from .test_hooks import * # noqa
5866
from .test_key import * # noqa
67+
from .test_limits import * # noqa
5968
from .test_perm import * # noqa
6069
from .test_release import * # noqa
6170
from .test_scheduler import * # noqa
6271
from .test_users import * # noqa
63-
from .test_limits import * # noqa

controller/api/tests/test_app.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@
1010
import logging
1111
import mock
1212
import os.path
13-
import requests
1413

1514
from django.conf import settings
1615
from django.contrib.auth.models import User
1716
from django.test import TestCase
1817
from rest_framework.authtoken.models import Token
1918

2019
from api.models import App
21-
22-
23-
def mock_import_repository_task(*args, **kwargs):
24-
resp = requests.Response()
25-
resp.status_code = 200
26-
resp._content_consumed = True
27-
return resp
20+
from . import mock_status_ok
2821

2922

3023
class AppTest(TestCase):
@@ -198,7 +191,7 @@ def test_app_structure_is_valid_json(self):
198191
self.assertIn('structure', response.data)
199192
self.assertEqual(response.data['structure'], {"web": 1})
200193

201-
@mock.patch('requests.post', mock_import_repository_task)
194+
@mock.patch('requests.post', mock_status_ok)
202195
@mock.patch('api.models.logger')
203196
def test_admin_can_manage_other_apps(self, mock_logger):
204197
"""Administrators of Deis should be able to manage all applications.

controller/api/tests/test_build.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import mock
11-
import requests
1210

1311
from django.contrib.auth.models import User
1412
from django.test import TransactionTestCase
13+
import mock
1514
from rest_framework.authtoken.models import Token
1615

1716
from api.models import Build
17+
from . import mock_status_ok
1818

1919

20-
def mock_import_repository_task(*args, **kwargs):
21-
resp = requests.Response()
22-
resp.status_code = 200
23-
resp._content_consumed = True
24-
return resp
25-
26-
20+
@mock.patch('api.models.publish_release', lambda *args: None)
2721
class BuildTest(TransactionTestCase):
2822

2923
"""Tests build notification from build system"""
@@ -34,7 +28,7 @@ def setUp(self):
3428
self.user = User.objects.get(username='autotest')
3529
self.token = Token.objects.get(user=self.user).key
3630

37-
@mock.patch('requests.post', mock_import_repository_task)
31+
@mock.patch('requests.post', mock_status_ok)
3832
def test_build(self):
3933
"""
4034
Test that a null build is created and that users can post new builds
@@ -82,7 +76,7 @@ def test_build(self):
8276
response = self.client.delete(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
8377
self.assertEqual(response.status_code, 405)
8478

85-
@mock.patch('requests.post', mock_import_repository_task)
79+
@mock.patch('requests.post', mock_status_ok)
8680
def test_response_data(self):
8781
"""Test that the serialized response contains only relevant data."""
8882
body = {'id': 'test'}
@@ -108,7 +102,7 @@ def test_response_data(self):
108102
}
109103
self.assertDictContainsSubset(expected, response.data)
110104

111-
@mock.patch('requests.post', mock_import_repository_task)
105+
@mock.patch('requests.post', mock_status_ok)
112106
def test_build_default_containers(self):
113107
url = '/v1/apps'
114108
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -194,7 +188,7 @@ def test_build_default_containers(self):
194188
self.assertEqual(container['type'], 'web')
195189
self.assertEqual(container['num'], 1)
196190

197-
@mock.patch('requests.post', mock_import_repository_task)
191+
@mock.patch('requests.post', mock_status_ok)
198192
def test_build_str(self):
199193
"""Test the text representation of a build."""
200194
url = '/v1/apps'
@@ -211,7 +205,7 @@ def test_build_str(self):
211205
self.assertEqual(str(build), "{}-{}".format(
212206
response.data['app'], response.data['uuid'][:7]))
213207

214-
@mock.patch('requests.post', mock_import_repository_task)
208+
@mock.patch('requests.post', mock_status_ok)
215209
def test_admin_can_create_builds_on_other_apps(self):
216210
"""If a user creates an application, an administrator should be able
217211
to push builds.
@@ -233,7 +227,7 @@ def test_admin_can_create_builds_on_other_apps(self):
233227
self.assertEqual(str(build), "{}-{}".format(
234228
response.data['app'], response.data['uuid'][:7]))
235229

236-
@mock.patch('requests.post', mock_import_repository_task)
230+
@mock.patch('requests.post', mock_status_ok)
237231
def test_unauthorized_user_cannot_modify_build(self):
238232
"""
239233
An unauthorized user should not be able to modify other builds.
@@ -254,7 +248,7 @@ def test_unauthorized_user_cannot_modify_build(self):
254248
HTTP_AUTHORIZATION='token {}'.format(unauthorized_token))
255249
self.assertEqual(response.status_code, 403)
256250

257-
@mock.patch('requests.post', mock_import_repository_task)
251+
@mock.patch('requests.post', mock_status_ok)
258252
def test_new_build_does_not_scale_up_automatically(self):
259253
"""
260254
After the first initial deploy, if the containers are scaled down to zero,

controller/api/tests/test_config.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,17 @@
99

1010
import json
1111
import logging
12-
import mock
1312
import requests
1413

1514
from django.contrib.auth.models import User
1615
from django.test import TransactionTestCase
1716
import etcd
17+
import mock
1818
from rest_framework.authtoken.models import Token
1919

2020
import api.exceptions
2121
from api.models import App, Config
22-
23-
24-
def mock_status_ok(*args, **kwargs):
25-
resp = requests.Response()
26-
resp.status_code = 200
27-
resp._content_consumed = True
28-
return resp
22+
from . import mock_status_ok
2923

3024

3125
def mock_status_not_found(*args, **kwargs):
@@ -52,6 +46,7 @@ def get(self, key, *args, **kwargs):
5246
return etcd.EtcdResult(None, node)
5347

5448

49+
@mock.patch('api.models.publish_release', lambda *args: None)
5550
class ConfigTest(TransactionTestCase):
5651

5752
"""Tests setting and updating config values"""

controller/api/tests/test_container.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,18 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import mock
11-
import requests
1210

1311
from django.contrib.auth.models import User
1412
from django.test import TransactionTestCase
15-
from scheduler.states import TransitionError
13+
import mock
1614
from rest_framework.authtoken.models import Token
1715

1816
from api.models import App, Build, Container, Release
17+
from scheduler.states import TransitionError
18+
from . import mock_status_ok
1919

2020

21-
def mock_import_repository_task(*args, **kwargs):
22-
resp = requests.Response()
23-
resp.status_code = 200
24-
resp._content_consumed = True
25-
return resp
26-
27-
21+
@mock.patch('api.models.publish_release', lambda *args: None)
2822
class ContainerTest(TransactionTestCase):
2923
"""Tests creation of containers on nodes"""
3024

@@ -170,7 +164,7 @@ def test_container_api_heroku(self):
170164
response = self.client.get(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
171165
self.assertEqual(response.status_code, 200)
172166

173-
@mock.patch('requests.post', mock_import_repository_task)
167+
@mock.patch('requests.post', mock_status_ok)
174168
def test_container_api_docker(self):
175169
url = '/v1/apps'
176170
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -233,7 +227,7 @@ def test_container_api_docker(self):
233227
response = self.client.get(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
234228
self.assertEqual(response.status_code, 200)
235229

236-
@mock.patch('requests.post', mock_import_repository_task)
230+
@mock.patch('requests.post', mock_status_ok)
237231
def test_container_release(self):
238232
url = '/v1/apps'
239233
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))

controller/api/tests/test_hooks.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,17 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import mock
11-
import requests
1210

1311
from django.conf import settings
1412
from django.contrib.auth.models import User
1513
from django.test import TransactionTestCase
14+
import mock
1615
from rest_framework.authtoken.models import Token
1716

18-
19-
def mock_import_repository_task(*args, **kwargs):
20-
resp = requests.Response()
21-
resp.status_code = 200
22-
resp._content_consumed = True
23-
return resp
17+
from . import mock_status_ok
2418

2519

20+
@mock.patch('api.models.publish_release', lambda *args: None)
2621
class HookTest(TransactionTestCase):
2722

2823
"""Tests API hooks used to trigger actions from external components"""
@@ -97,7 +92,7 @@ def test_push_abuse(self):
9792
HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY)
9893
self.assertEqual(response.status_code, 403)
9994

100-
@mock.patch('requests.post', mock_import_repository_task)
95+
@mock.patch('requests.post', mock_status_ok)
10196
def test_build_hook(self):
10297
"""Test creating a Build via an API Hook"""
10398
url = '/v1/apps'

controller/api/tests/test_release.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
from __future__ import unicode_literals
88

99
import json
10-
import mock
11-
import requests
1210

1311
from django.contrib.auth.models import User
1412
from django.test import TransactionTestCase
13+
import mock
1514
from rest_framework.authtoken.models import Token
1615

1716
from api.models import Release
17+
from . import mock_status_ok
1818

1919

20-
def mock_import_repository_task(*args, **kwargs):
21-
resp = requests.Response()
22-
resp.status_code = 200
23-
resp._content_consumed = True
24-
return resp
25-
26-
20+
@mock.patch('api.models.publish_release', lambda *args: None)
2721
class ReleaseTest(TransactionTestCase):
2822

2923
"""Tests push notification from build system"""
@@ -34,7 +28,7 @@ def setUp(self):
3428
self.user = User.objects.get(username='autotest')
3529
self.token = Token.objects.get(user=self.user).key
3630

37-
@mock.patch('requests.post', mock_import_repository_task)
31+
@mock.patch('requests.post', mock_status_ok)
3832
def test_release(self):
3933
"""
4034
Test that a release is created when an app is created, and
@@ -111,7 +105,7 @@ def test_release(self):
111105
self.assertEqual(response.status_code, 405)
112106
return release3
113107

114-
@mock.patch('requests.post', mock_import_repository_task)
108+
@mock.patch('requests.post', mock_status_ok)
115109
def test_response_data(self):
116110
body = {'id': 'test'}
117111
response = self.client.post('/v1/apps', json.dumps(body),
@@ -136,7 +130,7 @@ def test_response_data(self):
136130
}
137131
self.assertDictContainsSubset(expected, response.data)
138132

139-
@mock.patch('requests.post', mock_import_repository_task)
133+
@mock.patch('requests.post', mock_status_ok)
140134
def test_release_rollback(self):
141135
url = '/v1/apps'
142136
response = self.client.post(url, HTTP_AUTHORIZATION='token {}'.format(self.token))
@@ -233,22 +227,22 @@ def test_release_rollback(self):
233227
self.assertIn('NEW_URL1', values)
234228
self.assertEqual('http://localhost:8080/', values['NEW_URL1'])
235229

236-
@mock.patch('requests.post', mock_import_repository_task)
230+
@mock.patch('requests.post', mock_status_ok)
237231
def test_release_str(self):
238232
"""Test the text representation of a release."""
239233
release3 = self.test_release()
240234
release = Release.objects.get(uuid=release3['uuid'])
241235
self.assertEqual(str(release), "{}-v3".format(release3['app']))
242236

243-
@mock.patch('requests.post', mock_import_repository_task)
237+
@mock.patch('requests.post', mock_status_ok)
244238
def test_release_summary(self):
245239
"""Test the text summary of a release."""
246240
release3 = self.test_release()
247241
release = Release.objects.get(uuid=release3['uuid'])
248242
# check that the release has push and env change messages
249243
self.assertIn('autotest deployed ', release.summary)
250244

251-
@mock.patch('requests.post', mock_import_repository_task)
245+
@mock.patch('requests.post', mock_status_ok)
252246
def test_admin_can_create_release(self):
253247
"""If a non-user creates an app, an admin should be able to create releases."""
254248
user = User.objects.get(username='autotest2')
@@ -272,7 +266,7 @@ def test_admin_can_create_release(self):
272266
# account for the config release as well
273267
self.assertEqual(response.data['count'], 2)
274268

275-
@mock.patch('requests.post', mock_import_repository_task)
269+
@mock.patch('requests.post', mock_status_ok)
276270
def test_unauthorized_user_cannot_modify_release(self):
277271
"""
278272
An unauthorized user should not be able to modify other releases.

controller/api/tests/test_scheduler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
from django.conf import settings
1212
from django.contrib.auth.models import User
1313
from django.test import TransactionTestCase
14+
import mock
1415
from rest_framework.authtoken.models import Token
1516

1617
from scheduler import chaos
1718

1819

20+
@mock.patch('api.models.publish_release', lambda *args: None)
1921
class SchedulerTest(TransactionTestCase):
2022
"""Tests creation of containers on nodes"""
2123

0 commit comments

Comments
 (0)