/
unittest_server.py
executable file
·405 lines (354 loc) · 16.2 KB
/
unittest_server.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2016 by Mathieu Servillat
# Licensed under MIT (https://github.com/mservillat/uws-server/blob/master/LICENSE)
"""
Unit tests for UWS server
"""
import unittest
import datetime as dt
from webtest import TestApp
# Redefine LOG_FILE, SQLITE_FILE, MANAGER
now = dt.datetime.now()
now_str = now.isoformat().split('.')[0]
ALLOW_ANONYMOUS = True
CHECK_PERMISSIONS = False
CHECK_OWNER = False
SQLITE_FILE_NAME = 'job_database_test_{}.db'.format(now_str)
LOG_FILE_SUFFIX = '_test_' + now_str
MANAGER = ''
from uws_server import uws_server
test_app = TestApp(uws_server.app) # , extra_environ=dict(REMOTE_USER='test'))
jobname = 'ctbin'
class TestGet(unittest.TestCase):
def setUp(self):
print('\n***** TestGet *****')
def assert_status(self, url, status, content_type=''):
if status in [404, 405]:
response = test_app.get(url, status=status)
else:
response = test_app.get(url)
print(url)
print(' --> ' + response.status)
self.assertEqual(response.status_int, status)
if content_type:
self.assertEqual(response.headers['content-type'], content_type)
def test_get(self):
# Initialize db, must be localhost
response = test_app.get('/db/test', extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/db/show')
print('DB initialized')
# Test GET with job 22222222-e656-b924-c14a-fbd02f9ebaa9
jobid = '22222222-e656-b924-c14a-fbd02f9ebaa9'
url = '/rest/' + jobname
self.assert_status(url, 200, 'text/xml; charset=UTF-8')
url = '/rest/' + jobname + '/bad_jobid'
self.assert_status(url, 404)
url = '/rest/' + jobname + '/' + jobid
self.assert_status(url, 200, 'text/xml; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/bad_attribute'
self.assert_status(url, 405)
url = '/rest/' + jobname + '/' + jobid + '/phase'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/executionduration'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/destruction'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/error'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/quote'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/parameters'
self.assert_status(url, 200, 'text/xml; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/parameters/input'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/results'
self.assert_status(url, 200, 'text/xml; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/results/output'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
url = '/rest/' + jobname + '/' + jobid + '/owner'
self.assert_status(url, 200, 'text/plain; charset=UTF-8')
class TestJobUpdate(unittest.TestCase):
"""Test attribute updates"""
def setUp(self):
print('\n***** TestJobUpdate *****')
def assert_job_attribute(self, jobid, attribute, value):
url = '/rest/' + jobname + '/' + jobid + '/' + attribute
response = test_app.get(url)
self.assertEqual(response.text, value)
def test_update(self):
# Initialize db, must be localhost
response = test_app.get('/db/test', extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
print('DB initialized')
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/db/show')
# Update execution duration
jobid = '00000000-dbf3-6b04-b1e7-28d47ad32794'
url = '/rest/' + jobname + '/' + jobid + '/executionduration'
post = {'BAD_KEY': '120'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
post = {'EXECUTIONDURATION': 'BAD_VALUE'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
post = {'EXECUTIONDURATION': '120'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname + '/' + jobid)
self.assert_job_attribute(jobid, 'executionduration', '120')
# Update destruction time
jobid = '00000000-dbf3-6b04-b1e7-28d47ad32794'
url = '/rest/' + jobname + '/' + jobid + '/destruction'
post = {'BAD_KEY': '2016-01-01T00:00:00'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
post = {'DESTRUCTION': 'BAD_VALUE'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
post = {'DESTRUCTION': '2016-01-01 00:00:00'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
post = {'DESTRUCTION': '2016-01-01T00:00:00'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname + '/' + jobid)
self.assert_job_attribute(jobid, 'destruction', '2016-01-01T00:00:00')
post = {'DESTRUCTION': '2016-01-01T00:00:00.55555'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname + '/' + jobid)
self.assert_job_attribute(jobid, 'destruction', '2016-01-01T00:00:00')
class TestJobUpdateParam(unittest.TestCase):
"""Test attribute updates"""
def setUp(self):
print('\n***** TestJobUpdateParam *****')
def assert_job_attribute(self, jobid, attribute, value):
url = '/rest/' + jobname + '/' + jobid + '/' + attribute
response = test_app.get(url)
self.assertEqual(response.text, value)
def test_update_param(self):
# Initialize db, must be localhost
response = test_app.get('/db/test', extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
print('DB initialized')
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/db/show')
# Change parameter
jobid = '00000000-dbf3-6b04-b1e7-28d47ad32794'
url = '/rest/' + jobname + '/' + jobid + '/parameters/input'
post = {'BAD_KEY': 'Testing'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
self.assert_job_attribute(jobid, 'parameters/input', 'Test 0')
post = {'VALUE': 'Testing'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname + '/' + jobid)
self.assert_job_attribute(jobid, 'parameters/input', 'Testing')
# Change parameter of COMPLETED job
jobid = '22222222-e656-b924-c14a-fbd02f9ebaa9'
url = '/rest/' + jobname + '/' + jobid + '/parameters/input'
post = {'VALUE': 'Testing'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
self.assert_job_attribute(jobid, 'parameters/input', 'Test 2')
class TestJobAbort(unittest.TestCase):
"""Test abort command on jobs (COMPLETED jobs cannot be aborted)"""
def setUp(self):
print('\n***** TestJobAbort *****')
def assert_job_phase(self, jobid, phase):
url = '/rest/' + jobname + '/' + jobid + '/phase'
response = test_app.get(url)
self.assertEqual(response.text, phase)
def test_abort(self):
# Initialize db, must be localhost
response = test_app.get('/db/test', extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
print('DB initialized')
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/db/show')
# Abort PENDING job
jobid = '00000000-dbf3-6b04-b1e7-28d47ad32794'
url = '/rest/' + jobname + '/' + jobid + '/phase'
post = {'PHASE': 'ABORT'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname + '/' + jobid)
self.assert_job_phase(jobid, 'ABORTED')
# Abort EXECUTING job
jobid = '11111111-9c85-4873-a4b1-8d7e5e91ed57'
url = '/rest/' + jobname + '/' + jobid + '/phase'
post = {'PHASE': 'ABORT'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname + '/' + jobid)
self.assert_job_phase(jobid, 'ABORTED')
# Abort COMPLETED job (should return HTTP Error 500)
jobid = '22222222-e656-b924-c14a-fbd02f9ebaa9'
url = '/rest/' + jobname + '/' + jobid + '/phase'
post = {'PHASE': 'ABORT'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
self.assert_job_phase(jobid, 'COMPLETED')
class TestJobDelete(unittest.TestCase):
"""Test delete command on jobs"""
def setUp(self):
print('\n***** TestJobDelete *****')
def test_delete(self):
# Initialize db, must be localhost
response = test_app.get('/db/test', extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
print('DB initialized')
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/db/show')
# Delete PENDING job
jobid = '00000000-dbf3-6b04-b1e7-28d47ad32794'
url = '/rest/' + jobname + '/' + jobid
post = {'ACTION': 'BAD_VALUE'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
post = {'BAD_KEY': 'DELETE'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
post = {'ACTION': 'DELETE'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname)
# Delete EXECUTING job
jobid = '11111111-9c85-4873-a4b1-8d7e5e91ed57'
url = '/rest/' + jobname + '/' + jobid
response = test_app.delete(url, post)
print(url + ' DELETE')
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname)
# Delete COMPLETED job
jobid = '22222222-e656-b924-c14a-fbd02f9ebaa9'
url = '/rest/' + jobname + '/' + jobid
response = test_app.delete(url, post)
print(url + ' DELETE')
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname)
class TestJobSequence(unittest.TestCase):
"""Test default sequence for a job: creation, start, executing, completed"""
def setUp(self):
print('\n***** TestJobSequence *****')
def assert_job_phase(self, jobid, phase):
url = '/rest/' + jobname + '/' + jobid + '/phase'
response = test_app.get(url)
self.assertEqual(response.text, phase)
def test_job_sequence(self):
# Initialize db, must be localhost
response = test_app.get('/db/test', extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
print('DB initialized')
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/db/show')
# Create job
url = '/rest/' + jobname + ''
post = {'input': 'Testing'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/' + jobname + '/')
jobid = response.location.split('/')[-1]
self.assert_job_phase(jobid, 'PENDING')
# Start job
url = '/rest/' + jobname + '/' + jobid + '/phase'
post = {'PHASE': 'BAD_VALUE'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
self.assert_job_phase(jobid, 'PENDING')
post = {'BAD_KEY': 'RUN'}
response = test_app.post(url, post, status=500)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.html.pre.string)
self.assertEqual(response.status_int, 500)
self.assert_job_phase(jobid, 'PENDING')
post = {'PHASE': 'RUN'}
response = test_app.post(url, post)
print(url + ' ' + str(post))
print(' --> ' + response.status)
print(' --> ' + response.location)
self.assertEqual(response.status_int, 303)
self.assertRegex(response.location, '/rest/' + jobname + '/' + jobid)
self.assert_job_phase(jobid, 'QUEUED')
# job_event EXECUTING
url = '/handler/job_event'
post = {'jobid': '0', 'phase': 'EXECUTING'}
response = test_app.post(url, post, extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
print(url)
print(' --> ' + response.status)
self.assertEqual(response.status_int, 200)
self.assertEqual(response.text, '')
self.assert_job_phase(jobid, 'EXECUTING')
# job_event COMPLETED
post = {'jobid': '0', 'phase': 'COMPLETED'}
response = test_app.post(url, post, extra_environ=dict(REMOTE_ADDR='127.0.0.1'))
print(url)
print(' --> ' + response.status)
self.assertEqual(response.status_int, 200)
self.assertEqual(response.text, '')
self.assert_job_phase(jobid, 'COMPLETED')
# If running this file, run the tests
# invoke with `python -m unittest discover`
if __name__ == '__main__':
# Run tests
unittest.main()