Skip to content

Commit c64c2e2

Browse files
author
Gabriel Monroy
committed
add test coverage around mutiple apps for formations that do/dont support it
1 parent 28af652 commit c64c2e2

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

api/tests/app.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,20 @@ def setUp(self):
3434
response = self.client.post(url, json.dumps(body), content_type='application/json')
3535
self.assertEqual(response.status_code, 201)
3636
formation_id = 'autotest'
37-
response = self.client.post('/api/formations', json.dumps(
38-
{'id': formation_id, 'domain': 'localhost.localdomain'}),
39-
content_type='application/json')
37+
response = self.client.post('/api/formations', json.dumps({'id': formation_id}),
38+
content_type='application/json')
4039
self.assertEqual(response.status_code, 201)
4140
# create & scale a basic formation
4241
url = '/api/formations/{formation_id}/layers'.format(**locals())
43-
body = {'id': 'proxy', 'flavor': 'autotest', 'proxy': True,
44-
'run_list': 'recipe[deis::proxy]'}
42+
body = {'id': 'proxy', 'flavor': 'autotest', 'proxy': True}
4543
response = self.client.post(url, json.dumps(body), content_type='application/json')
4644
self.assertEqual(response.status_code, 201)
4745
url = '/api/formations/{formation_id}/layers'.format(**locals())
48-
body = {'id': 'runtime', 'flavor': 'autotest', 'runtime': True,
49-
'run_list': 'recipe[deis::proxy]'}
46+
body = {'id': 'runtime', 'flavor': 'autotest', 'runtime': True}
5047
response = self.client.post(url, json.dumps(body), content_type='application/json')
5148
self.assertEqual(response.status_code, 201)
5249
url = '/api/formations/{formation_id}/scale'.format(**locals())
53-
body = {'proxy': 2, 'runtime': 4}
50+
body = {'proxy': 1, 'runtime': 2}
5451
response = self.client.post(url, json.dumps(body), content_type='application/json')
5552
self.assertEqual(response.status_code, 200)
5653

@@ -112,6 +109,42 @@ def test_app_override_id(self):
112109
self.assertContains(response, 'App with this Id already exists.', status_code=400)
113110
return response
114111

112+
def test_multiple_apps(self):
113+
url = '/api/apps'
114+
body = {'formation': 'autotest'}
115+
response = self.client.post(url, json.dumps(body), content_type='application/json')
116+
self.assertEqual(response.status_code, 201)
117+
app1_id = response.data['id']
118+
# test single app domain
119+
url = "/api/apps/{app1_id}/calculate".format(**locals())
120+
response = self.client.post(url)
121+
self.assertEqual(response.status_code, 200)
122+
self.assertEqual(response.data['domains'], ['localhost.localdomain.local'])
123+
# create second app without multi-app support
124+
url = '/api/apps'
125+
response = self.client.post(url, json.dumps(body), content_type='application/json')
126+
self.assertContains(response, 'Formation does not support multiple apps', status_code=400)
127+
# add domain for multi-app support
128+
url = '/api/formations/autotest'
129+
response = self.client.patch(url, json.dumps({'domain': 'deisapp.local'}),
130+
content_type='application/json')
131+
self.assertEqual(response.status_code, 200)
132+
# create second app
133+
url = '/api/apps'
134+
body = {'formation': 'autotest'}
135+
response = self.client.post(url, json.dumps(body), content_type='application/json')
136+
self.assertEqual(response.status_code, 201)
137+
app2_id = response.data['id']
138+
# test multiple app domains
139+
url = "/api/apps/{app1_id}/calculate".format(**locals())
140+
response = self.client.post(url)
141+
self.assertEqual(response.status_code, 200)
142+
self.assertEqual(response.data['domains'], ['{}.deisapp.local'.format(app1_id)])
143+
url = "/api/apps/{app2_id}/calculate".format(**locals())
144+
response = self.client.post(url)
145+
self.assertEqual(response.status_code, 200)
146+
self.assertEqual(response.data['domains'], ['{}.deisapp.local'.format(app2_id)])
147+
115148
def test_app_actions(self):
116149
url = '/api/apps'
117150
body = {'formation': 'autotest', 'id': 'autotest'}

api/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ def post_save(self, app, created=False, **kwargs):
262262
group(*[tasks.converge_formation.si(app.formation), # @UndefinedVariable
263263
tasks.converge_controller.si()]).apply_async().join() # @UndefinedVariable
264264

265+
def pre_save(self, app, created=False, **kwargs):
266+
if not app.pk and not app.formation.domain and app.formation.app_set.count() > 0:
267+
raise EnvironmentError('Formation does not support multiple apps')
268+
return super(AppViewSet, self).pre_save(app, **kwargs)
269+
270+
def create(self, request, **kwargs):
271+
try:
272+
return OwnerViewSet.create(self, request, **kwargs)
273+
except EnvironmentError as e:
274+
return Response(str(e), status=HTTP_400_BAD_REQUEST)
275+
265276
def scale(self, request, **kwargs):
266277
new_structure = {}
267278
try:

0 commit comments

Comments
 (0)