Skip to content

Commit 5a38afd

Browse files
committed
Added several tests and improved coverage definition.
1 parent ebd177f commit 5a38afd

17 files changed

Lines changed: 232 additions & 21 deletions

.coveragerc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
omit =
33
*/venv/*
44
*/virtualenv/*
5+
*tests*
56
api/__init__.py
67
client/__init__.py
78
client/models.py
8-
client/tests/__init__.py
9+
cm/__init__.py
10+
cm/models.py
11+
provider/models.py
912
web/__init__.py
1013
web/models.py
14+
web/templatetags/__init__.py
1115

1216
[report]
1317
ignore_errors = True

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
all:
1+
runserver:
22
python manage.py runserver
33

44
db:
5-
python manage.py syncdb --noinput
6-
python manage.py migrate --noinput
5+
python manage.py syncdb --migrate --noinput
76

87
test:
98
python manage.py test --noinput api cm provider web

api/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ def formfield(self, **kwargs):
6262
add_introspection_rules([], [r'^api\.fields\.ParamsField'])
6363
add_introspection_rules([], [r'^api\.fields\.CloudInitField'])
6464
add_introspection_rules([], [r'^api\.fields\.NodeStatusField'])
65-
except ImportError:
65+
except ImportError: # pragma: no cover
6666
pass

api/tests/test_app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,18 @@ def test_app_actions(self):
197197
response = self.client.post(url)
198198
self.assertEqual(response.status_code, 200)
199199
self.assertEqual(response.data, FAKE_LOG_DATA)
200+
# test run with mock error
201+
url = '/api/apps/{app_id}/run'.format(**locals())
202+
body = {'command': 'error'}
203+
response = self.client.post(url, json.dumps(body), content_type='application/json')
204+
self.assertIn('run `git push deis master` first', response.data[0])
205+
self.assertEqual(response.data[1], 1)
200206
# test run
201207
url = '/api/apps/{app_id}/run'.format(**locals())
202208
body = {'command': 'ls -al'}
203209
response = self.client.post(url, json.dumps(body), content_type='application/json')
204210
self.assertEqual(response.status_code, 200)
211+
self.assertIn('drwx------ 2 deis deis 4096 Dec 21 10:00 .chef', response.data[0])
205212
self.assertEqual(response.data[1], 0)
206213
# test calculate
207214
url = '/api/apps/{app_id}/calculate'.format(**locals())

api/tests/test_build.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,19 @@ def test_build_push(self):
122122
self.assertIn('web', databag['containers'])
123123
self.assertIn('1', databag['containers']['web'])
124124
self.assertEqual(databag['containers']['web']['1'], 'up')
125+
126+
def test_build_str(self):
127+
"""Test the text representation of a build."""
128+
url = '/api/apps'
129+
body = {'formation': 'autotest'}
130+
response = self.client.post(url, json.dumps(body), content_type='application/json')
131+
self.assertEqual(response.status_code, 201)
132+
app_id = response.data['id']
133+
# check to see that no initial build was created
134+
url = "/api/apps/{app_id}/builds".format(**locals())
135+
response = self.client.get(url)
136+
self.assertEqual(response.status_code, 200)
137+
data = response.data['results'][0]
138+
build = Build.objects.get(uuid=data['uuid'])
139+
sha = ''
140+
self.assertEqual(str(build), "{}-{}".format(data['app'], sha))

api/tests/test_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from django.test import TestCase
1212
from django.test.utils import override_settings
1313

14+
from api.models import Config
15+
1416

1517
@override_settings(CELERY_ALWAYS_EAGER=True)
1618
class ConfigTest(TestCase):
@@ -98,3 +100,10 @@ def test_config(self):
98100
self.assertEqual(self.client.put(url).status_code, 405)
99101
self.assertEqual(self.client.patch(url).status_code, 405)
100102
self.assertEqual(self.client.delete(url).status_code, 405)
103+
return config5
104+
105+
def test_config_str(self):
106+
"""Test the text representation of a node."""
107+
config5 = self.test_config()
108+
config = Config.objects.get(uuid=config5['uuid'])
109+
self.assertEqual(str(config), "{}-v4".format(config5['app']))

api/tests/test_container.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.test import TestCase
1212
from django.test.utils import override_settings
1313

14+
from api.models import Container
1415
from deis import settings
1516

1617

@@ -455,3 +456,27 @@ def test_container_balance(self):
455456
b_min = min([len(by_backend[b]) for b in by_backend.keys()])
456457
b_max = max([len(by_backend[b]) for b in by_backend.keys()])
457458
self.assertLess(b_max - b_min, 2)
459+
460+
def test_container_str(self):
461+
"""Test the text representation of a container."""
462+
url = '/api/apps'
463+
body = {'formation': 'autotest'}
464+
response = self.client.post(url, json.dumps(body), content_type='application/json')
465+
self.assertEqual(response.status_code, 201)
466+
app_id = response.data['id']
467+
# scale up
468+
url = "/api/apps/{app_id}/scale".format(**locals())
469+
body = {'web': 4, 'worker': 2}
470+
response = self.client.post(url, json.dumps(body), content_type='application/json')
471+
self.assertEqual(response.status_code, 200)
472+
# should start with zero
473+
url = "/api/apps/{app_id}/containers".format(**locals())
474+
response = self.client.get(url)
475+
self.assertEqual(response.status_code, 200)
476+
self.assertEqual(len(response.data['results']), 6)
477+
uuid = response.data['results'][0]['uuid']
478+
container = Container.objects.get(uuid=uuid)
479+
self.assertEqual(container.short_name(),
480+
"{}.{}".format(container.type, container.num))
481+
self.assertEqual(str(container),
482+
"{} {}".format(container.formation.id, container.short_name()))

api/tests/test_flavor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from django.test import TestCase
1313

14+
from api.models import Flavor
15+
1416

1517
class FlavorTest(TestCase):
1618

@@ -95,3 +97,18 @@ def test_flavor_update(self):
9597
self.assertNotIn('zone', params)
9698
self.assertEqual(params['size'], 'c1.xlarge')
9799
self.assertEqual(params['image'], 'ami-c98d1bf9')
100+
101+
def test_flavor_str(self):
102+
"""Test the text representation of a flavor."""
103+
url = '/api/flavors'
104+
params = {
105+
'region': 'us-west-2',
106+
'size': 't1.micro',
107+
'zone': 'any',
108+
'image': 'i-1234567'
109+
}
110+
body = {'id': 'auto_test', 'provider': 'autotest', 'params': json.dumps(params)}
111+
response = self.client.post(url, json.dumps(body), content_type='application/json')
112+
self.assertEqual(response.status_code, 201)
113+
flavor = Flavor.objects.get(uuid=response.data['uuid'])
114+
self.assertEqual(str(flavor), 'auto_test')

api/tests/test_key.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from django.test import TestCase
1313
from django.test.utils import override_settings
1414

15+
from api.models import Key
1516
from deis import settings
1617

1718

@@ -79,3 +80,19 @@ def test_key_duplicate(self):
7980
self.assertEqual(response.status_code, 201)
8081
response = self.client.post(url, json.dumps(body), content_type='application/json')
8182
self.assertEqual(response.status_code, 400)
83+
84+
def test_key_str(self):
85+
"""Test the text representation of a key"""
86+
url = '/api/keys'
87+
body = {'id': 'autotest', 'public':
88+
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzqPAwHN70xsB0LXG//KzO'
89+
'gcPikyhdN/KRc4x3j/RA0pmFj63Ywv0PJ2b1LcMSqfR8F11WBlrW8c9xFua0'
90+
'ZAKzI+gEk5uqvOR78bs/SITOtKPomW4e/1d2xEkJqOmYH30u94+NZZYwEBqY'
91+
'aRb34fhtrnJS70XeGF0RhXE5Qea5eh7DBbeLxPfSYd8rfHgzMSb/wmx3h2vm'
92+
'HdQGho20pfJktNu7DxeVkTHn9REMUphf85su7slTgTlWKq++3fASE8PdmFGz'
93+
'b6PkOR4c+LS5WWXd2oM6HyBQBxxiwXbA2lSgQxOdgDiM2FzT0GVSFMUklkUH'
94+
'MdsaG6/HJDw9QckTS0vN autotest@deis.io'}
95+
response = self.client.post(url, json.dumps(body), content_type='application/json')
96+
self.assertEqual(response.status_code, 201)
97+
key = Key.objects.get(uuid=response.data['uuid'])
98+
self.assertEqual(str(key), 'ssh-rsa AAAAB3NzaC.../HJDw9QckTS0vN autotest@deis.io')

api/tests/test_layer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from django.test import TestCase
1313
from django.test.utils import override_settings
1414

15+
from api.models import Layer
16+
1517

1618
@override_settings(CELERY_ALWAYS_EAGER=True)
1719
class LayerTest(TestCase):
@@ -87,3 +89,18 @@ def test_layer_ssh_override(self):
8789
self.assertIn('ssh_public_key', response.data)
8890
self.assertEquals(response.data['ssh_public_key'], body['ssh_public_key'])
8991
self.assertIn('ssh_private_key', response.data)
92+
93+
def test_layer_str(self):
94+
"""Test the text representation of a layer."""
95+
url = '/api/formations'
96+
body = {'id': 'autotest', 'domain': 'localhost.localdomain'}
97+
response = self.client.post(url, json.dumps(body), content_type='application/json')
98+
self.assertEqual(response.status_code, 201)
99+
formation_id = response.data['id'] # noqa
100+
url = '/api/formations/{formation_id}/layers'.format(**locals())
101+
body = {'id': 'autotest', 'flavor': 'autotest',
102+
'config': json.dumps({'key': 'value'})}
103+
response = self.client.post(url, json.dumps(body), content_type='application/json')
104+
self.assertEqual(response.status_code, 201)
105+
layer = Layer.objects.get(uuid=response.data['uuid'])
106+
self.assertEqual(str(layer), 'autotest')

0 commit comments

Comments
 (0)