Skip to content

Commit

Permalink
add test coverage around mutiple apps for formations that do/dont sup…
Browse files Browse the repository at this point in the history
…port it
  • Loading branch information
Gabriel Monroy committed Sep 17, 2013
1 parent 28af652 commit c64c2e2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
49 changes: 41 additions & 8 deletions api/tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,20 @@ def setUp(self):
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
formation_id = 'autotest'
response = self.client.post('/api/formations', json.dumps(
{'id': formation_id, 'domain': 'localhost.localdomain'}),
content_type='application/json')
response = self.client.post('/api/formations', json.dumps({'id': formation_id}),
content_type='application/json')
self.assertEqual(response.status_code, 201)
# create & scale a basic formation
url = '/api/formations/{formation_id}/layers'.format(**locals())
body = {'id': 'proxy', 'flavor': 'autotest', 'proxy': True,
'run_list': 'recipe[deis::proxy]'}
body = {'id': 'proxy', 'flavor': 'autotest', 'proxy': True}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
url = '/api/formations/{formation_id}/layers'.format(**locals())
body = {'id': 'runtime', 'flavor': 'autotest', 'runtime': True,
'run_list': 'recipe[deis::proxy]'}
body = {'id': 'runtime', 'flavor': 'autotest', 'runtime': True}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
url = '/api/formations/{formation_id}/scale'.format(**locals())
body = {'proxy': 2, 'runtime': 4}
body = {'proxy': 1, 'runtime': 2}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 200)

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

def test_multiple_apps(self):
url = '/api/apps'
body = {'formation': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app1_id = response.data['id']
# test single app domain
url = "/api/apps/{app1_id}/calculate".format(**locals())
response = self.client.post(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['domains'], ['localhost.localdomain.local'])
# create second app without multi-app support
url = '/api/apps'
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertContains(response, 'Formation does not support multiple apps', status_code=400)
# add domain for multi-app support
url = '/api/formations/autotest'
response = self.client.patch(url, json.dumps({'domain': 'deisapp.local'}),
content_type='application/json')
self.assertEqual(response.status_code, 200)
# create second app
url = '/api/apps'
body = {'formation': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app2_id = response.data['id']
# test multiple app domains
url = "/api/apps/{app1_id}/calculate".format(**locals())
response = self.client.post(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['domains'], ['{}.deisapp.local'.format(app1_id)])
url = "/api/apps/{app2_id}/calculate".format(**locals())
response = self.client.post(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['domains'], ['{}.deisapp.local'.format(app2_id)])

def test_app_actions(self):
url = '/api/apps'
body = {'formation': 'autotest', 'id': 'autotest'}
Expand Down
11 changes: 11 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ def post_save(self, app, created=False, **kwargs):
group(*[tasks.converge_formation.si(app.formation), # @UndefinedVariable
tasks.converge_controller.si()]).apply_async().join() # @UndefinedVariable

def pre_save(self, app, created=False, **kwargs):
if not app.pk and not app.formation.domain and app.formation.app_set.count() > 0:
raise EnvironmentError('Formation does not support multiple apps')
return super(AppViewSet, self).pre_save(app, **kwargs)

def create(self, request, **kwargs):
try:
return OwnerViewSet.create(self, request, **kwargs)
except EnvironmentError as e:
return Response(str(e), status=HTTP_400_BAD_REQUEST)

def scale(self, request, **kwargs):
new_structure = {}
try:
Expand Down

0 comments on commit c64c2e2

Please sign in to comment.