3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
12 class KeepLocalStoreTest(unittest.TestCase):
14 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
16 self.assertEqual(arvados.Keep.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
17 self.assertEqual(arvados.Keep.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
19 class LocalCollectionWriterTest(unittest.TestCase):
21 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
23 cw = arvados.CollectionWriter()
24 self.assertEqual(cw.current_stream_name(), '.',
25 'current_stream_name() should be "." now')
26 cw.set_current_file_name('foo.txt')
28 self.assertEqual(cw.current_file_name(), 'foo.txt',
29 'current_file_name() should be foo.txt now')
30 cw.start_new_file('bar.txt')
32 cw.start_new_stream('baz')
34 cw.set_current_file_name('baz.txt')
36 self.assertEqual(hash,
37 'd6c3b8e571f1b81ebb150a45ed06c884+114',
38 "resulting manifest hash was {0}, expecting d6c3b8e571f1b81ebb150a45ed06c884+114".format(hash))
40 class LocalCollectionReaderTest(unittest.TestCase):
42 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
43 LocalCollectionWriterTest().runTest()
45 cr = arvados.CollectionReader('d6c3b8e571f1b81ebb150a45ed06c884+114+Xzizzle')
47 for s in cr.all_streams():
48 for f in s.all_files():
49 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
50 expected = [[3, '.', 'bar.txt', 'bar'],
51 [3, '.', 'foo.txt', 'foo'],
52 [3, './baz', 'baz.txt', 'baz']]
55 stream0 = cr.all_streams()[0]
56 self.assertEqual(stream0.readfrom(0, 0),
58 'reading zero bytes should have returned empty string')
59 self.assertEqual(stream0.readfrom(0, 2**26),
61 'reading entire stream failed')
62 self.assertEqual(stream0.readfrom(2**26, 0),
64 'reading zero bytes should have returned empty string')
66 class LocalCollectionManifestSubsetTest(unittest.TestCase):
68 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
69 LocalCollectionWriterTest().runTest()
71 self._runTest('d6c3b8e571f1b81ebb150a45ed06c884+114',
72 [[3, '.', 'bar.txt', 'bar'],
73 [3, '.', 'foo.txt', 'foo'],
74 [3, './baz', 'baz.txt', 'baz']])
75 self._runTest((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
76 (arvados.Keep.put("foo"),
77 arvados.Keep.put("bar"))),
78 [[3, '.', 'bar.txt', 'bar'],
79 [3, '.', 'foo.txt', 'foo']])
80 self._runTest((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
81 (arvados.Keep.put("foo"),
82 arvados.Keep.put("bar"))),
83 [[2, '.', 'fo.txt', 'fo'],
84 [4, '.', 'obar.txt', 'obar']])
85 self._runTest((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
86 (arvados.Keep.put("foo"),
87 arvados.Keep.put("bar"))),
88 [[2, '.', 'ar.txt', 'ar'],
89 [2, '.', 'fo.txt', 'fo'],
90 [2, '.', 'ob.txt', 'ob'],
91 [0, '.', 'zero.txt', '']])
93 def _runTest(self, collection, expected):
94 cr = arvados.CollectionReader(collection)
95 for s in cr.all_streams():
99 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
100 self.assertEqual(got,
102 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
104 class LocalCollectionReadlineTest(unittest.TestCase):
106 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
107 def _runTest(self, what_in, what_out):
108 cw = arvados.CollectionWriter()
109 cw.start_new_file('test.txt')
112 cr = arvados.CollectionReader(test1)
114 for x in list(cr.all_files())[0].readlines():
116 self.assertEqual(got,
118 "readlines did not split lines correctly: %s" % got)
120 self._runTest("\na\nbcd\n\nefg\nz",
121 ["\n", "a\n", "bcd\n", "\n", "efg\n", "z"])
122 self._runTest("ab\ncd\n",
125 class LocalCollectionEmptyFileTest(unittest.TestCase):
127 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
129 cw = arvados.CollectionWriter()
130 cw.start_new_file('zero.txt')
133 self.assertEqual(cw.manifest_text(), ". 0:0:zero.txt\n")
134 self.check_manifest_file_sizes(cw.manifest_text(), [0])
135 cw = arvados.CollectionWriter()
136 cw.start_new_file('zero.txt')
138 cw.start_new_file('one.txt')
140 cw.start_new_stream('foo')
141 cw.start_new_file('zero.txt')
143 self.check_manifest_file_sizes(cw.manifest_text(), [1,0,0])
145 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
146 cr = arvados.CollectionReader(manifest_text)
148 for f in cr.all_files():
149 got_sizes += [f.size()]
150 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
152 class LocalCollectionBZ2DecompressionTest(unittest.TestCase):
154 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
158 for x in xrange(0, 18):
160 compressed_data_in = bz2.compress(data_in)
161 cw = arvados.CollectionWriter()
162 cw.start_new_file('test.bz2')
163 cw.write(compressed_data_in)
164 bz2_manifest = cw.manifest_text()
166 cr = arvados.CollectionReader(bz2_manifest)
169 for x in list(cr.all_files())[0].readlines():
170 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
172 self.assertEqual(got,
174 "decompression returned %d lines instead of %d" % (got, n_lines_in))
176 class LocalCollectionGzipDecompressionTest(unittest.TestCase):
178 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
182 for x in xrange(0, 18):
184 p = subprocess.Popen(["gzip", "-1cn"],
185 stdout=subprocess.PIPE,
186 stdin=subprocess.PIPE,
187 stderr=subprocess.PIPE,
188 shell=False, close_fds=True)
189 compressed_data_in, stderrdata = p.communicate(data_in)
191 cw = arvados.CollectionWriter()
192 cw.start_new_file('test.gz')
193 cw.write(compressed_data_in)
194 gzip_manifest = cw.manifest_text()
196 cr = arvados.CollectionReader(gzip_manifest)
198 for x in list(cr.all_files())[0].readlines():
199 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
201 self.assertEqual(got,
203 "decompression returned %d lines instead of %d" % (got, n_lines_in))
205 class NormalizedCollectionTest(unittest.TestCase):
207 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
208 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
209 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
210 self.assertEqual(arvados.CollectionReader(m1).manifest_text(),
211 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
214 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
216 self.assertEqual(arvados.CollectionReader(m2).manifest_text(), m2)
218 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
219 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
220 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
221 self.assertEqual(arvados.CollectionReader(m3).manifest_text(),
222 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
225 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
226 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
227 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar"""
228 self.assertEqual(arvados.CollectionReader(m4).manifest_text(),
229 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
230 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
233 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
234 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
235 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar"""
236 self.assertEqual(arvados.CollectionReader(m5).manifest_text(),
237 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
238 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
241 with open('testdata/1000G_ref_manifest') as f6:
243 self.assertEqual(arvados.CollectionReader(m6).manifest_text(), m6)
245 with open('testdata/jlake_manifest') as f7:
247 self.assertEqual(arvados.CollectionReader(m7).manifest_text(), m7)
249 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
251 self.assertEqual(arvados.CollectionReader(m8).manifest_text(), m8)
253 class LocatorsAndRangesTest(unittest.TestCase):
255 blocks2 = [['a', 10, 0],
262 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [['a', 10, 2, 2]])
263 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [['b', 10, 2, 2]])
264 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [['c', 10, 2, 2]])
265 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [['d', 10, 2, 2]])
266 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [['e', 10, 2, 2]])
267 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [['f', 10, 2, 2]])
268 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
269 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
271 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [['a', 10, 0, 2]])
272 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [['b', 10, 0, 2]])
273 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [['c', 10, 0, 2]])
274 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [['d', 10, 0, 2]])
275 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [['e', 10, 0, 2]])
276 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [['f', 10, 0, 2]])
277 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
278 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
280 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [['a', 10, 9, 1], ['b', 10, 0, 1]])
281 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [['b', 10, 9, 1], ['c', 10, 0, 1]])
282 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [['c', 10, 9, 1], ['d', 10, 0, 1]])
283 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [['d', 10, 9, 1], ['e', 10, 0, 1]])
284 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [['e', 10, 9, 1], ['f', 10, 0, 1]])
285 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [['f', 10, 9, 1]])
288 blocks3 = [['a', 10, 0],
296 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [['a', 10, 2, 2]])
297 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [['b', 10, 2, 2]])
298 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [['c', 10, 2, 2]])
299 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [['d', 10, 2, 2]])
300 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [['e', 10, 2, 2]])
301 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [['f', 10, 2, 2]])
302 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [['g', 10, 2, 2]])
305 blocks = [['a', 10, 0],
308 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
309 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [['a', 10, 0, 5]])
310 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [['a', 10, 3, 5]])
311 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [['a', 10, 0, 10]])
313 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [['a', 10, 0, 10],
315 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [['a', 10, 1, 9],
317 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [['a', 10, 0, 10],
320 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [['a', 10, 0, 10],
323 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [['a', 10, 1, 9],
326 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [['a', 10, 0, 10],
330 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [['b', 15, 5, 5]])
332 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [['a', 10, 8, 2],
335 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [['a', 10, 8, 2],
339 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [['c', 5, 1, 2]])
341 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [['a', 10, 9, 1],
343 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [['b', 15, 0, 15]])
344 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [['b', 15, 1, 14],
347 class FileStreamTest(unittest.TestCase):
348 class MockStreamReader(object):
349 def __init__(self, content):
350 self.content = content
352 def readfrom(self, start, size):
353 return self.content[start:start+size]
356 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
357 msr = FileStreamTest.MockStreamReader(content)
358 segments = [[0, 10, 0],
362 sfr = arvados.StreamFileReader(msr, segments, "test")
364 self.assertEqual(sfr.name(), "test")
365 self.assertEqual(sfr.size(), 30)
367 self.assertEqual(sfr.readfrom(0, 30), content[0:30])
368 self.assertEqual(sfr.readfrom(2, 30), content[2:30])
370 self.assertEqual(sfr.readfrom(2, 8), content[2:10])
371 self.assertEqual(sfr.readfrom(0, 10), content[0:10])
373 self.assertEqual(sfr.tell(), 0)
374 self.assertEqual(sfr.read(5), content[0:5])
375 self.assertEqual(sfr.tell(), 5)
376 self.assertEqual(sfr.read(5), content[5:10])
377 self.assertEqual(sfr.tell(), 10)
378 self.assertEqual(sfr.read(5), content[10:15])
379 self.assertEqual(sfr.tell(), 15)
380 self.assertEqual(sfr.read(5), content[15:20])
381 self.assertEqual(sfr.tell(), 20)
382 self.assertEqual(sfr.read(5), content[20:25])
383 self.assertEqual(sfr.tell(), 25)
384 self.assertEqual(sfr.read(5), content[25:30])
385 self.assertEqual(sfr.tell(), 30)
386 self.assertEqual(sfr.read(5), '')
387 self.assertEqual(sfr.tell(), 30)
389 segments = [[26, 10, 0],
393 sfr = arvados.StreamFileReader(msr, segments, "test")
395 self.assertEqual(sfr.size(), 30)
397 self.assertEqual(sfr.readfrom(0, 30), content[26:36] + content[0:20])
398 self.assertEqual(sfr.readfrom(2, 30), content[28:36] + content[0:20])
400 self.assertEqual(sfr.readfrom(2, 8), content[28:36])
401 self.assertEqual(sfr.readfrom(0, 10), content[26:36])
403 self.assertEqual(sfr.tell(), 0)
404 self.assertEqual(sfr.read(5), content[26:31])
405 self.assertEqual(sfr.tell(), 5)
406 self.assertEqual(sfr.read(5), content[31:36])
407 self.assertEqual(sfr.tell(), 10)
408 self.assertEqual(sfr.read(5), content[0:5])
409 self.assertEqual(sfr.tell(), 15)
410 self.assertEqual(sfr.read(5), content[5:10])
411 self.assertEqual(sfr.tell(), 20)
412 self.assertEqual(sfr.read(5), content[10:15])
413 self.assertEqual(sfr.tell(), 25)
414 self.assertEqual(sfr.read(5), content[15:20])
415 self.assertEqual(sfr.tell(), 30)
416 self.assertEqual(sfr.read(5), '')
417 self.assertEqual(sfr.tell(), 30)
420 class StreamReaderTest(unittest.TestCase):
422 class MockKeep(object):
423 def __init__(self, content):
424 self.content = content
426 def get(self, locator):
427 return self.content[locator]
430 keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
431 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
432 'cccccccccccccccccccccccccccccccc+5': 'z0123'}
433 mk = StreamReaderTest.MockKeep(keepblocks)
435 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
437 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
439 self.assertEqual(sr.readfrom(0, 30), content[0:30])
440 self.assertEqual(sr.readfrom(2, 30), content[2:30])
442 self.assertEqual(sr.readfrom(2, 8), content[2:10])
443 self.assertEqual(sr.readfrom(0, 10), content[0:10])
445 self.assertEqual(sr.readfrom(0, 5), content[0:5])
446 self.assertEqual(sr.readfrom(5, 5), content[5:10])
447 self.assertEqual(sr.readfrom(10, 5), content[10:15])
448 self.assertEqual(sr.readfrom(15, 5), content[15:20])
449 self.assertEqual(sr.readfrom(20, 5), content[20:25])
450 self.assertEqual(sr.readfrom(25, 5), content[25:30])
451 self.assertEqual(sr.readfrom(30, 5), '')
453 class ExtractFileTest(unittest.TestCase):
455 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
456 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
457 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
458 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
459 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt"""
461 m2 = arvados.CollectionReader(m1)
463 self.assertEqual(m2.manifest_text(),
464 ". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt 43:41:md6sum.txt 84:43:md7sum.txt 6:37:md8sum.txt 84:43:md8sum.txt 83:1:md9sum.txt 0:43:md9sum.txt 84:36:md9sum.txt\n")
466 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md5sum.txt'].as_manifest(),
467 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
468 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md6sum.txt'].as_manifest(),
469 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
470 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md7sum.txt'].as_manifest(),
471 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
472 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md9sum.txt'].as_manifest(),
473 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")