Skip to content

Commit 8517d7d

Browse files
author
Matthew Fisher
committed
test(controller): add more container state tests
This commit tests more of the finite state machine's functionality that was introduced in #699, including disabled direct modification of the FSM and most of the expected state transitions. closes #700
1 parent ac966be commit 8517d7d

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

controller/api/tests/test_container.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from django.test import TransactionTestCase
1313
from django.test.utils import override_settings
1414

15+
from django_fsm import TransitionNotAllowed
16+
1517
from api.models import Container, App
1618

1719

@@ -30,8 +32,15 @@ def setUp(self):
3032
response = self.client.post('/api/clusters', json.dumps(body),
3133
content_type='application/json')
3234
self.assertEqual(response.status_code, 201)
35+
# create a malicious scheduler as well
36+
body['id'] = 'autotest2'
37+
body['type'] = 'faulty'
38+
response = self.client.post('/api/clusters', json.dumps(body),
39+
content_type='application/json')
40+
self.assertEqual(response.status_code, 201)
3341

34-
def test_container(self):
42+
def test_container_state_good(self):
43+
"""Test that the finite state machine transitions with a good scheduler"""
3544
url = '/api/apps'
3645
body = {'cluster': 'autotest'}
3746
response = self.client.post(url, json.dumps(body), content_type='application/json')
@@ -44,13 +53,57 @@ def test_container(self):
4453
type='web',
4554
num=1)
4655
self.assertEqual(c.state, 'initialized')
56+
# test an illegal transition
57+
self.assertRaises(TransitionNotAllowed, lambda: c.start())
4758
c.create()
4859
self.assertEqual(c.state, 'created')
4960
c.start()
5061
self.assertEqual(c.state, 'up')
62+
c.deploy(App.objects.get(id=app_id).release_set.latest())
63+
self.assertEqual(c.state, 'up')
5164
c.destroy()
5265
self.assertEqual(c.state, 'destroyed')
5366

67+
def test_container_state_bad(self):
68+
"""Test that the finite state machine transitions with a faulty scheduler"""
69+
url = '/api/apps'
70+
body = {'cluster': 'autotest2'}
71+
response = self.client.post(url, json.dumps(body), content_type='application/json')
72+
self.assertEqual(response.status_code, 201)
73+
app_id = response.data['id']
74+
# create a container
75+
c = Container.objects.create(owner=User.objects.get(username='autotest'),
76+
app=App.objects.get(id=app_id),
77+
release=App.objects.get(id=app_id).release_set.latest(),
78+
type='web',
79+
num=1)
80+
self.assertEqual(c.state, 'initialized')
81+
self.assertRaises(Exception, lambda: c.create())
82+
self.assertEqual(c.state, 'initialized')
83+
# test an illegal transition
84+
self.assertRaises(TransitionNotAllowed, lambda: c.start())
85+
self.assertEqual(c.state, 'initialized')
86+
self.assertRaises(Exception, lambda: c.deploy(App.objects.get(id=app_id).release_set.latest()))
87+
self.assertEqual(c.state, 'down')
88+
self.assertRaises(Exception, lambda: c.destroy())
89+
self.assertEqual(c.state, 'down')
90+
self.assertRaises(Exception, lambda: c.run('echo hello world'))
91+
self.assertEqual(c.state, 'down')
92+
93+
def test_container_state_protected(self):
94+
"""Test that you cannot directly modify the state"""
95+
url = '/api/apps'
96+
body = {'cluster': 'autotest'}
97+
response = self.client.post(url, json.dumps(body), content_type='application/json')
98+
self.assertEqual(response.status_code, 201)
99+
app_id = response.data['id']
100+
c = Container.objects.create(owner=User.objects.get(username='autotest'),
101+
app=App.objects.get(id=app_id),
102+
release=App.objects.get(id=app_id).release_set.latest(),
103+
type='web',
104+
num=1)
105+
self.assertRaises(AttributeError, lambda: setattr(c, 'state', 'up'))
106+
54107
def test_container_api(self):
55108
url = '/api/apps'
56109
body = {'cluster': 'autotest'}

0 commit comments

Comments
 (0)