github.com/blystad/deis@v0.11.0/controller/api/tests/test_app.py (about) 1 """ 2 Unit tests for the Deis api app. 3 4 Run the tests with "./manage.py test api" 5 """ 6 7 from __future__ import unicode_literals 8 9 import json 10 import os.path 11 12 from django.test import TestCase 13 from django.test.utils import override_settings 14 15 from django.conf import settings 16 17 18 @override_settings(CELERY_ALWAYS_EAGER=True) 19 class AppTest(TestCase): 20 21 """Tests creation of applications""" 22 23 fixtures = ['tests.json'] 24 25 def setUp(self): 26 self.assertTrue( 27 self.client.login(username='autotest', password='password')) 28 body = {'id': 'autotest', 'domain': 'autotest.local', 'type': 'mock', 29 'hosts': 'host1,host2', 'auth': 'base64string', 'options': {}} 30 response = self.client.post('/api/clusters', json.dumps(body), 31 content_type='application/json') 32 self.assertEqual(response.status_code, 201) 33 34 def test_app(self): 35 """ 36 Test that a user can create, read, update and delete an application 37 """ 38 url = '/api/apps' 39 body = {'cluster': 'autotest'} 40 response = self.client.post(url, json.dumps(body), content_type='application/json') 41 self.assertEqual(response.status_code, 201) 42 app_id = response.data['id'] # noqa 43 self.assertIn('cluster', response.data) 44 self.assertIn('id', response.data) 45 self.assertIn('url', response.data) 46 self.assertEqual(response.data['url'], '{app_id}.autotest.local'.format(**locals())) 47 response = self.client.get('/api/apps') 48 self.assertEqual(response.status_code, 200) 49 self.assertEqual(len(response.data['results']), 1) 50 url = '/api/apps/{app_id}'.format(**locals()) 51 response = self.client.get(url) 52 self.assertEqual(response.status_code, 200) 53 body = {'id': 'new'} 54 response = self.client.patch(url, json.dumps(body), content_type='application/json') 55 self.assertEqual(response.status_code, 405) 56 response = self.client.delete(url) 57 self.assertEqual(response.status_code, 204) 58 59 def test_app_override_id(self): 60 body = {'cluster': 'autotest', 'id': 'myid'} 61 response = self.client.post('/api/apps', json.dumps(body), 62 content_type='application/json') 63 self.assertEqual(response.status_code, 201) 64 body = {'cluster': response.data['cluster'], 'id': response.data['id']} 65 response = self.client.post('/api/apps', json.dumps(body), 66 content_type='application/json') 67 self.assertContains(response, 'App with this Id already exists.', status_code=400) 68 return response 69 70 def test_app_actions(self): 71 url = '/api/apps' 72 body = {'cluster': 'autotest', 'id': 'autotest'} 73 response = self.client.post(url, json.dumps(body), content_type='application/json') 74 self.assertEqual(response.status_code, 201) 75 app_id = response.data['id'] # noqa 76 # test logs 77 if not os.path.exists(settings.DEIS_LOG_DIR): 78 os.mkdir(settings.DEIS_LOG_DIR) 79 path = os.path.join(settings.DEIS_LOG_DIR, app_id + '.log') 80 if os.path.exists(path): 81 os.remove(path) 82 url = '/api/apps/{app_id}/logs'.format(**locals()) 83 response = self.client.post(url) 84 self.assertEqual(response.status_code, 204) 85 self.assertEqual(response.data, 'No logs for {}'.format(app_id)) 86 # write out some fake log data and try again 87 with open(path, 'w') as f: 88 f.write(FAKE_LOG_DATA) 89 response = self.client.post(url) 90 self.assertEqual(response.status_code, 200) 91 self.assertEqual(response.data, FAKE_LOG_DATA) 92 # test run 93 url = '/api/apps/{app_id}/run'.format(**locals()) 94 body = {'command': 'ls -al'} 95 response = self.client.post(url, json.dumps(body), content_type='application/json') 96 self.assertEqual(response.status_code, 200) 97 self.assertEqual(response.data[0], 0) 98 99 def test_app_errors(self): 100 cluster_id, app_id = 'autotest', 'autotest-errors' 101 url = '/api/apps' 102 body = {'cluster': cluster_id, 'id': 'camelCase'} 103 response = self.client.post(url, json.dumps(body), content_type='application/json') 104 self.assertContains(response, 'App IDs can only contain [a-z0-9-]', status_code=400) 105 url = '/api/apps' 106 body = {'cluster': cluster_id, 'id': 'deis'} 107 response = self.client.post(url, json.dumps(body), content_type='application/json') 108 self.assertContains(response, "App IDs cannot be 'deis'", status_code=400) 109 body = {'cluster': cluster_id, 'id': app_id} 110 response = self.client.post(url, json.dumps(body), content_type='application/json') 111 self.assertEqual(response.status_code, 201) 112 app_id = response.data['id'] # noqa 113 url = '/api/apps/{app_id}'.format(**locals()) 114 response = self.client.delete(url) 115 self.assertEquals(response.status_code, 204) 116 for endpoint in ('containers', 'config', 'releases', 'builds'): 117 url = '/api/apps/{app_id}/{endpoint}'.format(**locals()) 118 response = self.client.get(url) 119 self.assertEquals(response.status_code, 404) 120 121 122 FAKE_LOG_DATA = """ 123 2013-08-15 12:41:25 [33454] [INFO] Starting gunicorn 17.5 124 2013-08-15 12:41:25 [33454] [INFO] Listening at: http://0.0.0.0:5000 (33454) 125 2013-08-15 12:41:25 [33454] [INFO] Using worker: sync 126 2013-08-15 12:41:25 [33457] [INFO] Booting worker with pid 33457 127 """