This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·536 lines (406 loc) · 16.2 KB
/
test.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/env python
import unittest
import string
import random
import webtest
import os
import shutil
import redislite
import napfs
import falcon
import hashlib
from napfs.helpers import condense_byte_ranges, \
get_last_contiguous_byte
redis_connection = redislite.StrictRedis(dbfilename='/tmp/test-napfs.db')
NAPFS_DATA_DIR = '/tmp/test-napfs'
NOT_FOUND_BODY = b'{"title": "404 Not Found"}'
NOT_FOUND_BODY_LEN = str(len(NOT_FOUND_BODY))
def create_app():
if not os.path.exists(NAPFS_DATA_DIR):
os.mkdir(NAPFS_DATA_DIR)
return webtest.TestApp(napfs.create_app(
data_dir=NAPFS_DATA_DIR, redis_connection=redis_connection))
def create_router_app(router):
if not os.path.exists(NAPFS_DATA_DIR):
os.mkdir(NAPFS_DATA_DIR)
app = falcon.API()
app.add_sink(router, '/')
return webtest.TestApp(app)
def create_app_no_redis():
return webtest.TestApp(napfs.create_app(data_dir=NAPFS_DATA_DIR))
def random_string(length=1024):
return ''.join(
[random.choice(string.ascii_letters + string.digits) for _ in
range(length)]).encode('utf-8')
def clean():
shutil.rmtree(NAPFS_DATA_DIR)
if redis_connection:
redis_connection.flushdb()
class TestMain(unittest.TestCase):
def test_read_write(self):
data = random_string()
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
app = create_app()
res = app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
res = app.patch(uri, params=data,
headers={'Content-Type': 'text/plain',
'x-head-foo': 'bar'})
self.assertEqual(res.status_code, 200)
self.assertEqual(res.headers['x-parts'], '0-1023')
res = app.get(uri)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, data)
self.assertEqual(res.headers['x-parts'], '0-1023')
self.assertEqual(res.headers['x-head-foo'], 'bar')
chunk = random_string()
app.patch('%s?offset=%s' % (uri, len(data)), params=chunk,
headers={'Content-Type': 'text/plain',
'x-head-bazz': 'test', 'x-head-uploadrate': '1234'})
res = app.get(uri)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, data + chunk)
self.assertEqual(res.headers['x-parts'], '0-2047')
res = app.get(uri, headers={'Range': 'bytes=100-'})
self.assertEqual(res.status_code, 206)
self.assertEqual(res.body, data[100:] + chunk)
self.assertEqual(res.headers['x-head-foo'], 'bar')
self.assertEqual(res.headers['x-head-bazz'], 'test')
app.patch('%s?offset=%s' % (uri, len(data + chunk) + 100),
params='test',
headers={'Content-Type': 'text/plain'})
res = app.get(uri)
self.assertEqual(res.body, data + chunk)
self.assertEqual(res.headers['x-parts'], '0-2047,2148-2151')
res = app.head(uri)
self.assertEqual(res.status_code, 200)
self.assertEqual(len(res.body), 0)
self.assertEqual(res.headers['content-length'],
'%d' % len(data + chunk))
res = app.delete(uri)
self.assertEqual(res.status_code, 200)
res = app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
def test_copy(self):
data = random_string()
src = "test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
dst = "test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
app = create_app()
res = app.post('/%s' % src, params=data,
headers={'Content-Type': 'text/plain'})
self.assertEqual(res.status_code, 200)
res = app.post('/%s' % dst, headers={'x-source': '/%s' % src})
self.assertEqual(res.status_code, 200)
res = app.get('/%s' % dst)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, data)
def test_checksum(self):
uri = "/%s" % random_string(10)
data = random_string()
app = create_app()
app.post(uri, headers={'Content-Type': 'text/plain'}, params=data)
sha = hashlib.sha1()
sha.update(data)
digest = sha.hexdigest().encode('utf-8')
res = app.get(uri, headers={'x-checksum': 'sha1'})
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, digest)
res = app.head(uri, headers={'x-checksum': 'sha1'})
self.assertEqual(res.status_code, 200)
self.assertEqual(res.headers['x-signature'], 'sha1=%s' % digest)
class ByteRangeTests(unittest.TestCase):
def test_gap(self):
byte_ranges = [[0, 9], [10, 19], [20, 29], [40, 49]]
res = condense_byte_ranges(byte_ranges)
self.assertEqual(res, [[0, 29], [40, 49]])
def test_empty(self):
byte_ranges = []
res = condense_byte_ranges(byte_ranges)
self.assertEqual(res, [])
def test_overlap(self):
byte_ranges = [[0, 9], [0, 19], [20, 29], [40, 49]]
res = condense_byte_ranges(byte_ranges)
self.assertEqual(res, [[0, 29], [40, 49]])
class ContiguousTests(unittest.TestCase):
def test_contiguous(self):
last = get_last_contiguous_byte([[0, 10], [11, 20]])
self.assertEqual(last, 20)
last = get_last_contiguous_byte([[0, 10], [0, 0], [5, 20]])
self.assertEqual(last, 20)
last = get_last_contiguous_byte([[0, 10], [22, 100], [11, 20]])
self.assertEqual(last, 20)
class TestLongPatch(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
seed = random_string()
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
data = b''
for i in range(0, 20):
res = self.app.patch(
"%s?offset=%d" % (uri, len(data)),
params=seed,
headers={'Content-Type': 'text/plain'})
data += seed
self.assertEqual(res.headers['x-parts'], '0-%d' % (len(data) - 1))
res = self.app.get(uri)
self.assertEqual(res.body, data)
self.assertEqual(res.headers['content-length'], '%d' % len(data))
new_uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
self.app.post(new_uri, headers={'x-source': uri})
res = self.app.get(new_uri)
self.assertEqual(res.body, data)
self.assertEqual(res.headers['content-length'], '%d' % len(data))
res = self.app.delete(uri)
self.assertEqual(res.body, b'OK')
res = self.app.delete(new_uri)
self.assertEqual(res.body, b'OK')
class NonSequentialTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
self.app.patch("%s?offset=0" % uri, params='aaa',
headers={'Content-Type': 'text/plain'})
self.app.patch("%s?offset=6" % uri, params='ccc',
headers={'Content-Type': 'text/plain'})
res = self.app.get(uri)
self.assertEqual(res.body, b'aaa')
self.assertEqual(res.headers['x-parts'], '0-2,6-8')
self.app.patch("%s?offset=3" % uri, params='bbb',
headers={'Content-Type': 'text/plain'})
res = self.app.get(uri)
self.assertEqual(res.body, b'aaabbbccc')
self.assertEqual(res.headers['x-parts'], '0-8')
class MissingFirstTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
self.app.patch("%s?offset=10" % uri, params='aaa',
headers={'Content-Type': 'text/plain'})
self.app.patch("%s?offset=13" % uri, params='ccc',
headers={'Content-Type': 'text/plain'})
res = self.app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
class FirstLastTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
self.app.patch("%s?offset=3" % uri, params='bbb',
headers={'Content-Type': 'text/plain'})
res = self.app.get(uri, headers={'Range': 'bytes=0-*'},
expect_errors=True)
self.assertEqual(res.status_code, 404)
self.assertEqual(res.headers['Content-Length'], NOT_FOUND_BODY_LEN)
self.app.patch("%s?offset=0" % uri, params='aaa',
headers={'Content-Type': 'text/plain'})
class ExpireDataTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
self.app.patch("%s?offset=0" % uri, params='aaa',
headers={'Content-Type': 'text/plain'})
redis_connection.flushdb()
res = self.app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
self.app.patch("%s?offset=0" % uri, params='aa',
headers={'Content-Type': 'text/plain'})
res = self.app.get(uri)
self.assertEqual(b'aa', res.body)
class SingleByteTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
self.app.patch("%s?offset=0" % uri, params=b'a',
headers={'Content-Type': 'text/plain'})
res = self.app.get(uri)
self.assertEqual(res.body, b'a')
class SingleByteOffsetTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
self.app.patch("%s?offset=1" % uri, params='a',
headers={'Content-Type': 'text/plain'})
res = self.app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
self.assertEqual(res.body, NOT_FOUND_BODY)
class ChecksumPartTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test_fail(self):
uri = "/test/%s.txt" % random_string(10)
res = self.app.patch("%s" % uri, params='a',
headers={'Content-Type': 'text/plain',
'x-checksum': 'garbage'},
expect_errors=True)
self.assertEqual(res.status_code, 412)
self.assertEqual(res.headers['x-error-code'], 'CHECKSUM_FAIL')
res = self.app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
def test_success(self):
uri = "/test/%s.txt" % random_string(10)
body = 'a'
checksum = hashlib.sha1()
checksum.update(body.encode('utf-8'))
res = self.app.patch("%s" % uri, params=body,
headers={'Content-Type': 'text/plain',
'x-checksum': checksum.hexdigest()})
self.assertEqual(res.status_code, 200)
res = self.app.get(uri)
self.assertEqual(res.status_code, 200)
class PassthroughHeadersTest(unittest.TestCase):
def setUp(self):
self.app = create_app()
router = napfs.Router(data_dir=NAPFS_DATA_DIR,
redis_connection=redis_connection,
passthrough_headers=['x-YO'])
self.passthru_app = create_router_app(router)
def tearDown(self):
clean()
def test_stripping_headers(self):
uri = "/test/1/2/3"
self.app.patch(uri, params='a',
headers={'x-yo': 'yoback'})
res = self.app.get(uri)
self.assertFalse('x-yo' in res.headers)
def test_passthrough_headers(self):
uri = "/test/1/2/3"
self.passthru_app.patch(uri, params='a', headers={'x-yo': 'yoBack'})
res = self.passthru_app.get(uri)
self.assertEqual(res.headers['x-yo'], 'yoBack')
class VideoContentTypeCheck(unittest.TestCase):
def setUp(self):
self.app = create_app()
def tearDown(self):
clean()
def test(self):
uri = "/test/%s/%s/%s.mp4" % (
random_string(2),
random_string(2),
random_string(10))
video_file_path = os.path.join(os.path.dirname(__file__),
'sample-video.mp4')
with open(video_file_path, 'rb') as f:
video_content = f.read()
self.app.patch(uri, params=video_content,
headers={'Content-Type': 'video/mp4'})
res = self.app.get(uri)
self.assertEqual(res.body, video_content)
self.assertEqual(res.headers['content-type'], 'video/mp4')
class NoRedisTest(unittest.TestCase):
def setUp(self):
self.app = create_app_no_redis()
def tearDown(self):
clean()
def test_read_write(self):
data = random_string()
uri = "/test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
app = self.app
res = app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
res = app.patch(uri, params=data,
headers={'Content-Type': 'text/plain'})
self.assertEqual(res.status_code, 200)
res = app.get(uri)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, data)
chunk = random_string()
app.patch('%s?offset=%s' % (uri, len(data)), params=chunk,
headers={'Content-Type': 'text/plain'})
res = app.get(uri)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, data + chunk)
res = app.get(uri, headers={'Range': 'bytes=100-'})
self.assertEqual(res.status_code, 206)
self.assertEqual(res.body, data[100:] + chunk)
res = app.get(uri)
self.assertEqual(res.body, data + chunk)
res = app.delete(uri)
self.assertEqual(res.status_code, 200)
res = app.get(uri, expect_errors=True)
self.assertEqual(res.status_code, 404)
def test_copy(self):
data = random_string()
src = "test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
dst = "test/%s/%s/%s.txt" % (
random_string(2),
random_string(2),
random_string(10))
app = self.app
res = app.post('/%s' % src, params=data,
headers={'Content-Type': 'text/plain'})
self.assertEqual(res.status_code, 200)
res = app.post('/%s' % dst, headers={'x-source': '/%s' % src})
self.assertEqual(res.status_code, 200)
res = app.get('/%s' % dst)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, data)
def test_checksum(self):
uri = "/%s" % random_string(10)
data = random_string()
app = self.app
app.post(uri, headers={'Content-Type': 'text/plain'}, params=data)
res = app.get(uri, headers={'x-checksum': 'sha1'})
self.assertEqual(res.status_code, 200)
if __name__ == '__main__':
unittest.main(verbosity=2)