This repository was archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtests.py
308 lines (225 loc) · 9.83 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import base64
import os
import unittest
import time
import redis
from docker import Client
from flask import json
from mock import Mock
# Disable timeouts for testing
CURRENT_DISABLE_TIMEOUTS = os.environ.get('DISABLE_TIMEOUTS')
os.environ['DISABLE_TIMEOUTS'] = 'True'
existing_containers = []
r = redis.StrictRedis(host='localhost', port=6379)
client = Client()
from spindocker import app
from spindocker.tasks import celery, check_container_activity
celery.conf.CELERY_ALWAYS_EAGER = True
class SpinDockerTestCase(unittest.TestCase):
@classmethod
def setUpClass(self):
for c in client.containers(all=True, quiet=True):
existing_containers.append(c['Id'])
r.flushall()
@classmethod
def tearDownClass(self):
for c in client.containers(all=True, quiet=True):
if c['Id'] not in existing_containers:
client.kill(c['Id'])
client.remove_container(c['Id'])
if not CURRENT_DISABLE_TIMEOUTS:
del os.environ['DISABLE_TIMEOUTS']
def setUp(self):
# Set HTTP basic authentication headers
self.auth = {'Authorization': 'Basic ' + \
base64.b64encode('admin' + ":" + os.environ['SPIN_DOCKER_PASSWORD'])}
self.app = app.test_client()
def tearDown(self):
r.flushall()
def _get_containers(self):
resp = self.app.get('/v1/containers', headers=self.auth)
data = json.loads(resp.data)
return data
def _create_container(self, image='atbaker/sd-postgres'):
resp = self.app.post('/v1/containers',
headers=self.auth,
data={'image': image})
data = json.loads(resp.data)
return data
def test_bad_authentication(self):
resp = self.app.get('/v1/containers',
headers={'Authorization': 'Basic ' + \
base64.b64encode('wrong' + ":" + 'user')})
self.assertEqual(resp.status_code, 401)
def test_no_authentication(self):
resp = self.app.get('/v1/containers')
self.assertEqual(resp.status_code, 401)
def test_get_images(self):
resp = self.app.get('/v1/images',
headers=self.auth)
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 200)
self.assertIn('atbaker/sd-postgres:latest', data)
def test_container_list_empty(self):
data = self._get_containers()
self.assertEqual(data, [])
def test_container_list_one_container(self):
container = self._create_container()
data = self._get_containers()
self.assertEqual(len(data), 1)
self.assertEqual(data[0], container)
def test_container_list_multiple_containers(self):
container_one = self._create_container()
container_two = self._create_container()
data = self._get_containers()
self.assertEqual(len(data), 2)
self.assertIn(container_one, data)
self.assertIn(container_two, data)
def test_create_container_bad_image_name(self):
resp = self.app.post('/v1/containers',
headers=self.auth,
data={'image': 'badimage'})
data = json.loads(resp.data)
self.assertEqual(500, resp.status_code)
self.assertEqual('Image badimage not found on this server', data['message'])
def test_container_get(self):
container = self._create_container()
container_id = container['container_id']
resp = self.app.get('/v1/containers/%s' % container_id,
headers=self.auth)
data = json.loads(resp.data)
self.assertEqual(container, data)
self.assertIn('container_id', data.keys())
self.assertIn('image', data.keys())
self.assertIn('uri', data.keys())
self.assertIn('app_port', data.keys())
self.assertIn('ssh_port', data.keys())
self.assertIn('active', data.keys())
def test_container_get_bad_container_id(self):
self._create_container()
bad_container_id = '1337'
resp = self.app.get('/v1/containers/%s' % bad_container_id,
headers=self.auth)
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 404)
self.assertEqual(data['message'], "Container %s doesn't exist" % bad_container_id)
def test_container_delete(self):
container = self._create_container()
container_id = container['container_id']
resp = self.app.delete('/v1/containers/%s' % container_id,
headers=self.auth)
self.assertEqual(resp.status_code, 204)
self.assertEqual(self._get_containers(), [])
def test_container_stop(self):
container = self._create_container()
container_id = container['container_id']
resp = self.app.patch('/v1/containers/%s' % container_id,
headers=self.auth,
data=dict(status='stopped'))
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 200)
self.assertEqual(data['status'], 'stopping')
def test_container_start(self):
container = self._create_container()
container_id = container['container_id']
resp = self.app.patch('/v1/containers/%s' % container_id,
headers=self.auth,
data=dict(status='stopped'))
resp = self.app.patch('/v1/containers/%s' % container_id,
headers=self.auth,
data=dict(status='running'))
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 200)
self.assertEqual(data['status'], 'running')
def test_container_start_already_running(self):
container = self._create_container()
container_id = container['container_id']
resp = self.app.patch('/v1/containers/%s' % container_id,
headers=self.auth,
data=dict(status='running'))
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 500)
self.assertIn("Cannot start container %s" % container_id, data['message'])
def test_container_audit_remove_container(self):
container = self._create_container()
container_id = container['container_id']
client.kill(container_id)
client.remove_container(container_id)
resp = self.app.get('/v1/containers?audit=true',
headers=self.auth)
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 200)
self.assertEqual(data, [])
def test_container_audit_stopped_container(self):
container = self._create_container()
container_id = container['container_id']
client.stop(container_id)
resp = self.app.get('/v1/containers?audit=true',
headers=self.auth)
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 200)
self.assertEqual(data[0]['status'], 'stopped')
self.assertEqual(data[0]['ssh_port'], '')
self.assertEqual(data[0]['app_port'], '')
self.assertEqual(data[0]['active'], '0')
def test_container_audit_started_container(self):
container = self._create_container()
container_id = container['container_id']
resp = self.app.patch('/v1/containers/%s' % container_id,
headers=self.auth,
data=dict(status='stopped'))
self.assertEqual(resp.status_code, 200)
client.start(container_id)
resp = self.app.get('/v1/containers?audit=true',
headers=self.auth)
data = json.loads(resp.data)
self.assertEqual(resp.status_code, 200)
self.assertEqual(data[0]['status'], 'running')
def test_check_activity_no_container(self):
result = check_container_activity('bad_container_id')
self.assertIsNone(result)
def test_check_activity_not_running(self):
container = self._create_container()
container_id = container['container_id']
resp = self.app.patch('/v1/containers/%s' % container_id,
headers=self.auth,
data=dict(status='stopped'))
result = check_container_activity(container_id)
self.assertIsNone(result)
def test_check_activity_active_connections(self):
container = self._create_container()
container_id = container['container_id']
r.hset('containers:%s' % container_id, 'active', '1')
# Mock apply_async so we don't get stuck in infinite loop
check_container_activity.apply_async = Mock()
result = check_container_activity(container_id)
self.assertEqual(result, 'container active')
self.assertTrue(check_container_activity.apply_async.called)
def test_check_activity_no_connections(self):
container = self._create_container()
container_id = container['container_id']
result = check_container_activity(container_id)
self.assertEqual(result, 'container inactive')
def test_check_activity_no_connections_final(self):
container = self._create_container()
container_id = container['container_id']
result = check_container_activity(container_id, final=True)
self.assertEqual(result, 'stopping container')
def test_check_in(self):
container = self._create_container()
container_id = container['container_id']
self.app.post('/v1/check-in',
headers=self.auth,
data={'active': 1},
environ_base={'REMOTE_ADDR': client.inspect_container(container_id)['NetworkSettings']['IPAddress'],})
container_active = r.hget('containers:%s' % container_id, 'active')
self.assertEqual(container_active, '1')
def test_check_in_nonexistent_container(self):
resp = self.app.post('/v1/check-in',
headers=self.auth,
data={'active': 1},
environ_base={'REMOTE_ADDR': '127.0.0.99',})
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data, '')
if __name__ == '__main__':
unittest.main()