Skip to content

Commit ae9e617

Browse files
author
Matthew Fisher
committed
test(controller): mock import_repository task
1 parent c818ef2 commit ae9e617

7 files changed

Lines changed: 62 additions & 3 deletions

File tree

controller/api/tasks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ def deploy_release(app, release):
3838

3939
@task
4040
def import_repository(source, target_repository):
41-
"""Imports an image from a remote into our own private registry"""
42-
41+
"""Imports an image from a remote registry into our own private registry"""
4342
data = {
4443
'src': source,
4544
}

controller/api/tests/test_build.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
from __future__ import unicode_literals
88

99
import json
10+
import mock
11+
import requests
1012

1113
from django.test import TransactionTestCase
1214
from django.test.utils import override_settings
1315

1416
from api.models import Build
1517

1618

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
24+
25+
1726
@override_settings(CELERY_ALWAYS_EAGER=True)
1827
class BuildTest(TransactionTestCase):
1928

@@ -30,6 +39,7 @@ def setUp(self):
3039
content_type='application/json')
3140
self.assertEqual(response.status_code, 201)
3241

42+
@mock.patch('requests.post', mock_import_repository_task)
3343
def test_build(self):
3444
"""
3545
Test that a null build is created and that users can post new builds
@@ -71,6 +81,7 @@ def test_build(self):
7181
self.assertEqual(self.client.patch(url).status_code, 405)
7282
self.assertEqual(self.client.delete(url).status_code, 405)
7383

84+
@mock.patch('requests.post', mock_import_repository_task)
7485
def test_build_default_containers(self):
7586
url = '/api/apps'
7687
body = {'cluster': 'autotest'}
@@ -152,6 +163,7 @@ def test_build_default_containers(self):
152163
self.assertEqual(container['type'], 'web')
153164
self.assertEqual(container['num'], 1)
154165

166+
@mock.patch('requests.post', mock_import_repository_task)
155167
def test_build_str(self):
156168
"""Test the text representation of a build."""
157169
url = '/api/apps'

controller/api/tests/test_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
from __future__ import unicode_literals
88

99
import json
10+
import mock
11+
import requests
1012

1113
from django.test import TransactionTestCase
1214
from django.test.utils import override_settings
1315

1416
from api.models import Config
1517

1618

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
24+
25+
1726
@override_settings(CELERY_ALWAYS_EAGER=True)
1827
class ConfigTest(TransactionTestCase):
1928

@@ -30,6 +39,7 @@ def setUp(self):
3039
content_type='application/json')
3140
self.assertEqual(response.status_code, 201)
3241

42+
@mock.patch('requests.post', mock_import_repository_task)
3343
def test_config(self):
3444
"""
3545
Test that config is auto-created for a new app and that
@@ -94,6 +104,7 @@ def test_config(self):
94104
self.assertEqual(self.client.delete(url).status_code, 405)
95105
return config5
96106

107+
@mock.patch('requests.post', mock_import_repository_task)
97108
def test_config_set_same_key(self):
98109
"""
99110
Test that config sets on the same key function properly
@@ -116,6 +127,7 @@ def test_config_set_same_key(self):
116127
self.assertIn('PORT', json.loads(response.data['values']))
117128
self.assertEqual(json.loads(response.data['values'])['PORT'], '5001')
118129

130+
@mock.patch('requests.post', mock_import_repository_task)
119131
def test_config_str(self):
120132
"""Test the text representation of a node."""
121133
config5 = self.test_config()

controller/api/tests/test_container.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from __future__ import unicode_literals
88

99
import json
10+
import mock
11+
import requests
1012

1113
from django.contrib.auth.models import User
1214
from django.test import TransactionTestCase
@@ -17,9 +19,15 @@
1719
from api.models import Container, App
1820

1921

22+
def mock_import_repository_task(*args, **kwargs):
23+
resp = requests.Response()
24+
resp.status_code = 200
25+
resp._content_consumed = True
26+
return resp
27+
28+
2029
@override_settings(CELERY_ALWAYS_EAGER=True)
2130
class ContainerTest(TransactionTestCase):
22-
2331
"""Tests creation of containers on nodes"""
2432

2533
fixtures = ['tests.json']
@@ -174,6 +182,7 @@ def test_container_api_heroku(self):
174182
response = self.client.get(url)
175183
self.assertEqual(response.status_code, 200)
176184

185+
@mock.patch('requests.post', mock_import_repository_task)
177186
def test_container_api_docker(self):
178187
url = '/api/apps'
179188
body = {'cluster': 'autotest'}
@@ -233,6 +242,7 @@ def test_container_api_docker(self):
233242
response = self.client.get(url)
234243
self.assertEqual(response.status_code, 200)
235244

245+
@mock.patch('requests.post', mock_import_repository_task)
236246
def test_container_release(self):
237247
url = '/api/apps'
238248
body = {'cluster': 'autotest'}

controller/api/tests/test_hooks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
from __future__ import unicode_literals
88

99
import json
10+
import mock
11+
import requests
1012

1113
from django.test import TransactionTestCase
1214
from django.test.utils import override_settings
1315

1416
from django.conf import settings
1517

1618

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
24+
25+
1726
@override_settings(CELERY_ALWAYS_EAGER=True)
1827
class HookTest(TransactionTestCase):
1928

@@ -95,6 +104,7 @@ def test_push_abuse(self):
95104
HTTP_X_DEIS_BUILDER_AUTH=settings.BUILDER_KEY)
96105
self.assertEqual(response.status_code, 403)
97106

107+
@mock.patch('requests.post', mock_import_repository_task)
98108
def test_build_hook(self):
99109
"""Test creating a Build via an API Hook"""
100110
url = '/api/apps'

controller/api/tests/test_release.py

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

99
import json
10+
import mock
11+
import requests
1012

1113
from django.test import TransactionTestCase
1214
from django.test.utils import override_settings
1315

1416
from api.models import Release
1517

1618

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
24+
25+
1726
@override_settings(CELERY_ALWAYS_EAGER=True)
1827
class ReleaseTest(TransactionTestCase):
1928

@@ -30,6 +39,7 @@ def setUp(self):
3039
content_type='application/json')
3140
self.assertEqual(response.status_code, 201)
3241

42+
@mock.patch('requests.post', mock_import_repository_task)
3343
def test_release(self):
3444
"""
3545
Test that a release is created when a cluster is created, and
@@ -101,6 +111,7 @@ def test_release(self):
101111
self.assertEqual(self.client.delete(url).status_code, 405)
102112
return release3
103113

114+
@mock.patch('requests.post', mock_import_repository_task)
104115
def test_release_rollback(self):
105116
url = '/api/apps'
106117
body = {'cluster': 'autotest'}
@@ -187,12 +198,14 @@ def test_release_rollback(self):
187198
self.assertIn('NEW_URL1', values)
188199
self.assertEqual('http://localhost:8080/', values['NEW_URL1'])
189200

201+
@mock.patch('requests.post', mock_import_repository_task)
190202
def test_release_str(self):
191203
"""Test the text representation of a release."""
192204
release3 = self.test_release()
193205
release = Release.objects.get(uuid=release3['uuid'])
194206
self.assertEqual(str(release), "{}-v3".format(release3['app']))
195207

208+
@mock.patch('requests.post', mock_import_repository_task)
196209
def test_release_summary(self):
197210
"""Test the text summary of a release."""
198211
release3 = self.test_release()

controller/dev_requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ pyflakes==0.7.3
2626

2727
# Python expect is used to automate client-driven tests
2828
pexpect>=3.1
29+
30+
# Used for mocking endpoints
31+
mock==1.0.1

0 commit comments

Comments
 (0)