3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
14 from arvados_testutil import ArvadosKeepLocalStoreTestCase
16 class TestResumableWriter(arvados.ResumableCollectionWriter):
17 KEEP_BLOCK_SIZE = 1024 # PUT to Keep every 1K.
19 def current_state(self):
20 return self.dump_state(copy.deepcopy)
23 class ArvadosCollectionsTest(ArvadosKeepLocalStoreTestCase):
24 def write_foo_bar_baz(self):
25 cw = arvados.CollectionWriter()
26 self.assertEqual(cw.current_stream_name(), '.',
27 'current_stream_name() should be "." now')
28 cw.set_current_file_name('foo.txt')
30 self.assertEqual(cw.current_file_name(), 'foo.txt',
31 'current_file_name() should be foo.txt now')
32 cw.start_new_file('bar.txt')
34 cw.start_new_stream('baz')
36 cw.set_current_file_name('baz.txt')
39 def test_keep_local_store(self):
40 self.assertEqual(arvados.Keep.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
41 self.assertEqual(arvados.Keep.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
43 def test_local_collection_writer(self):
44 self.assertEqual(self.write_foo_bar_baz(),
45 'd6c3b8e571f1b81ebb150a45ed06c884+114',
46 "wrong locator hash for files foo, bar, baz")
48 def test_local_collection_reader(self):
49 self.write_foo_bar_baz()
50 cr = arvados.CollectionReader('d6c3b8e571f1b81ebb150a45ed06c884+114+Xzizzle')
52 for s in cr.all_streams():
53 for f in s.all_files():
54 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
55 expected = [[3, '.', 'bar.txt', 'bar'],
56 [3, '.', 'foo.txt', 'foo'],
57 [3, './baz', 'baz.txt', 'baz']]
60 stream0 = cr.all_streams()[0]
61 self.assertEqual(stream0.readfrom(0, 0),
63 'reading zero bytes should have returned empty string')
64 self.assertEqual(stream0.readfrom(0, 2**26),
66 'reading entire stream failed')
67 self.assertEqual(stream0.readfrom(2**26, 0),
69 'reading zero bytes should have returned empty string')
71 def _test_subset(self, collection, expected):
72 cr = arvados.CollectionReader(collection)
73 for s in cr.all_streams():
77 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
80 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
82 def test_collection_manifest_subset(self):
83 self.write_foo_bar_baz()
84 self._test_subset('d6c3b8e571f1b81ebb150a45ed06c884+114',
85 [[3, '.', 'bar.txt', 'bar'],
86 [3, '.', 'foo.txt', 'foo'],
87 [3, './baz', 'baz.txt', 'baz']])
88 self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
89 (arvados.Keep.put("foo"),
90 arvados.Keep.put("bar"))),
91 [[3, '.', 'bar.txt', 'bar'],
92 [3, '.', 'foo.txt', 'foo']])
93 self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
94 (arvados.Keep.put("foo"),
95 arvados.Keep.put("bar"))),
96 [[2, '.', 'fo.txt', 'fo'],
97 [4, '.', 'obar.txt', 'obar']])
98 self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
99 (arvados.Keep.put("foo"),
100 arvados.Keep.put("bar"))),
101 [[2, '.', 'ar.txt', 'ar'],
102 [2, '.', 'fo.txt', 'fo'],
103 [2, '.', 'ob.txt', 'ob'],
104 [0, '.', 'zero.txt', '']])
106 def _test_readline(self, what_in, what_out):
107 cw = arvados.CollectionWriter()
108 cw.start_new_file('test.txt')
111 cr = arvados.CollectionReader(test1)
113 for x in list(cr.all_files())[0].readlines():
115 self.assertEqual(got,
117 "readlines did not split lines correctly: %s" % got)
119 def test_collection_readline(self):
120 self._test_readline("\na\nbcd\n\nefg\nz",
121 ["\n", "a\n", "bcd\n", "\n", "efg\n", "z"])
122 self._test_readline("ab\ncd\n",
125 def test_collection_empty_file(self):
126 cw = arvados.CollectionWriter()
127 cw.start_new_file('zero.txt')
130 self.assertEqual(cw.manifest_text(), ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:zero.txt\n")
131 self.check_manifest_file_sizes(cw.manifest_text(), [0])
132 cw = arvados.CollectionWriter()
133 cw.start_new_file('zero.txt')
135 cw.start_new_file('one.txt')
137 cw.start_new_stream('foo')
138 cw.start_new_file('zero.txt')
140 self.check_manifest_file_sizes(cw.manifest_text(), [1,0,0])
142 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
143 cr = arvados.CollectionReader(manifest_text)
145 for f in cr.all_files():
146 got_sizes += [f.size()]
147 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
149 def test_collection_bz2_decompression(self):
152 for x in xrange(0, 18):
154 compressed_data_in = bz2.compress(data_in)
155 cw = arvados.CollectionWriter()
156 cw.start_new_file('test.bz2')
157 cw.write(compressed_data_in)
158 bz2_manifest = cw.manifest_text()
160 cr = arvados.CollectionReader(bz2_manifest)
163 for x in list(cr.all_files())[0].readlines():
164 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
166 self.assertEqual(got,
168 "decompression returned %d lines instead of %d" % (got, n_lines_in))
170 def test_collection_gzip_decompression(self):
173 for x in xrange(0, 18):
175 p = subprocess.Popen(["gzip", "-1cn"],
176 stdout=subprocess.PIPE,
177 stdin=subprocess.PIPE,
178 stderr=subprocess.PIPE,
179 shell=False, close_fds=True)
180 compressed_data_in, stderrdata = p.communicate(data_in)
182 cw = arvados.CollectionWriter()
183 cw.start_new_file('test.gz')
184 cw.write(compressed_data_in)
185 gzip_manifest = cw.manifest_text()
187 cr = arvados.CollectionReader(gzip_manifest)
189 for x in list(cr.all_files())[0].readlines():
190 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
192 self.assertEqual(got,
194 "decompression returned %d lines instead of %d" % (got, n_lines_in))
196 def test_normalized_collection(self):
197 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
198 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
199 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
200 self.assertEqual(arvados.CollectionReader(m1).manifest_text(),
201 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
204 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
206 self.assertEqual(arvados.CollectionReader(m2).manifest_text(), m2)
208 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
209 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
210 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
211 self.assertEqual(arvados.CollectionReader(m3).manifest_text(),
212 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
215 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
216 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
217 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar"""
218 self.assertEqual(arvados.CollectionReader(m4).manifest_text(),
219 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
220 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
223 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
224 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
225 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar"""
226 self.assertEqual(arvados.CollectionReader(m5).manifest_text(),
227 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
228 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
231 with self.data_file('1000G_ref_manifest') as f6:
233 self.assertEqual(arvados.CollectionReader(m6).manifest_text(), m6)
235 with self.data_file('jlake_manifest') as f7:
237 self.assertEqual(arvados.CollectionReader(m7).manifest_text(), m7)
239 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
241 self.assertEqual(arvados.CollectionReader(m8).manifest_text(), m8)
243 def test_locators_and_ranges(self):
244 blocks2 = [['a', 10, 0],
251 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [['a', 10, 2, 2]])
252 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [['b', 10, 2, 2]])
253 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [['c', 10, 2, 2]])
254 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [['d', 10, 2, 2]])
255 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [['e', 10, 2, 2]])
256 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [['f', 10, 2, 2]])
257 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
258 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
260 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [['a', 10, 0, 2]])
261 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [['b', 10, 0, 2]])
262 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [['c', 10, 0, 2]])
263 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [['d', 10, 0, 2]])
264 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [['e', 10, 0, 2]])
265 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [['f', 10, 0, 2]])
266 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
267 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
269 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [['a', 10, 9, 1], ['b', 10, 0, 1]])
270 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [['b', 10, 9, 1], ['c', 10, 0, 1]])
271 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [['c', 10, 9, 1], ['d', 10, 0, 1]])
272 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [['d', 10, 9, 1], ['e', 10, 0, 1]])
273 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [['e', 10, 9, 1], ['f', 10, 0, 1]])
274 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [['f', 10, 9, 1]])
277 blocks3 = [['a', 10, 0],
285 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [['a', 10, 2, 2]])
286 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [['b', 10, 2, 2]])
287 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [['c', 10, 2, 2]])
288 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [['d', 10, 2, 2]])
289 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [['e', 10, 2, 2]])
290 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [['f', 10, 2, 2]])
291 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [['g', 10, 2, 2]])
294 blocks = [['a', 10, 0],
297 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
298 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [['a', 10, 0, 5]])
299 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [['a', 10, 3, 5]])
300 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [['a', 10, 0, 10]])
302 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [['a', 10, 0, 10],
304 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [['a', 10, 1, 9],
306 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [['a', 10, 0, 10],
309 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [['a', 10, 0, 10],
312 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [['a', 10, 1, 9],
315 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [['a', 10, 0, 10],
319 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [['b', 15, 5, 5]])
321 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [['a', 10, 8, 2],
324 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [['a', 10, 8, 2],
328 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [['c', 5, 1, 2]])
330 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [['a', 10, 9, 1],
332 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [['b', 15, 0, 15]])
333 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [['b', 15, 1, 14],
336 class MockStreamReader(object):
337 def __init__(self, content):
338 self.content = content
340 def readfrom(self, start, size):
341 return self.content[start:start+size]
343 def test_file_stream(self):
344 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
345 msr = self.MockStreamReader(content)
346 segments = [[0, 10, 0],
350 sfr = arvados.StreamFileReader(msr, segments, "test")
352 self.assertEqual(sfr.name(), "test")
353 self.assertEqual(sfr.size(), 30)
355 self.assertEqual(sfr.readfrom(0, 30), content[0:30])
356 self.assertEqual(sfr.readfrom(2, 30), content[2:30])
358 self.assertEqual(sfr.readfrom(2, 8), content[2:10])
359 self.assertEqual(sfr.readfrom(0, 10), content[0:10])
361 self.assertEqual(sfr.tell(), 0)
362 self.assertEqual(sfr.read(5), content[0:5])
363 self.assertEqual(sfr.tell(), 5)
364 self.assertEqual(sfr.read(5), content[5:10])
365 self.assertEqual(sfr.tell(), 10)
366 self.assertEqual(sfr.read(5), content[10:15])
367 self.assertEqual(sfr.tell(), 15)
368 self.assertEqual(sfr.read(5), content[15:20])
369 self.assertEqual(sfr.tell(), 20)
370 self.assertEqual(sfr.read(5), content[20:25])
371 self.assertEqual(sfr.tell(), 25)
372 self.assertEqual(sfr.read(5), content[25:30])
373 self.assertEqual(sfr.tell(), 30)
374 self.assertEqual(sfr.read(5), '')
375 self.assertEqual(sfr.tell(), 30)
377 segments = [[26, 10, 0],
381 sfr = arvados.StreamFileReader(msr, segments, "test")
383 self.assertEqual(sfr.size(), 30)
385 self.assertEqual(sfr.readfrom(0, 30), content[26:36] + content[0:20])
386 self.assertEqual(sfr.readfrom(2, 30), content[28:36] + content[0:20])
388 self.assertEqual(sfr.readfrom(2, 8), content[28:36])
389 self.assertEqual(sfr.readfrom(0, 10), content[26:36])
391 self.assertEqual(sfr.tell(), 0)
392 self.assertEqual(sfr.read(5), content[26:31])
393 self.assertEqual(sfr.tell(), 5)
394 self.assertEqual(sfr.read(5), content[31:36])
395 self.assertEqual(sfr.tell(), 10)
396 self.assertEqual(sfr.read(5), content[0:5])
397 self.assertEqual(sfr.tell(), 15)
398 self.assertEqual(sfr.read(5), content[5:10])
399 self.assertEqual(sfr.tell(), 20)
400 self.assertEqual(sfr.read(5), content[10:15])
401 self.assertEqual(sfr.tell(), 25)
402 self.assertEqual(sfr.read(5), content[15:20])
403 self.assertEqual(sfr.tell(), 30)
404 self.assertEqual(sfr.read(5), '')
405 self.assertEqual(sfr.tell(), 30)
408 class MockKeep(object):
409 def __init__(self, content):
410 self.content = content
412 def get(self, locator):
413 return self.content[locator]
415 def test_stream_reader(self):
416 keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
417 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
418 'cccccccccccccccccccccccccccccccc+5': 'z0123'}
419 mk = self.MockKeep(keepblocks)
421 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
423 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
425 self.assertEqual(sr.readfrom(0, 30), content[0:30])
426 self.assertEqual(sr.readfrom(2, 30), content[2:30])
428 self.assertEqual(sr.readfrom(2, 8), content[2:10])
429 self.assertEqual(sr.readfrom(0, 10), content[0:10])
431 self.assertEqual(sr.readfrom(0, 5), content[0:5])
432 self.assertEqual(sr.readfrom(5, 5), content[5:10])
433 self.assertEqual(sr.readfrom(10, 5), content[10:15])
434 self.assertEqual(sr.readfrom(15, 5), content[15:20])
435 self.assertEqual(sr.readfrom(20, 5), content[20:25])
436 self.assertEqual(sr.readfrom(25, 5), content[25:30])
437 self.assertEqual(sr.readfrom(30, 5), '')
439 def test_file_reader(self):
440 keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
441 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
442 'cccccccccccccccccccccccccccccccc+5': 'z0123'}
443 mk = self.MockKeep(keepblocks)
445 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:10:foo", "15:10:foo"], mk)
447 content = 'abcdefghijpqrstuvwxy'
449 f = sr.files()["foo"]
452 self.assertEqual(f.read(20), content[0:20])
455 self.assertEqual(f.read(6), content[0:6])
456 self.assertEqual(f.read(6), content[6:12])
457 self.assertEqual(f.read(6), content[12:18])
459 def test_extract_file(self):
460 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
461 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
462 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
463 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
464 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt"""
466 m2 = arvados.CollectionReader(m1)
468 self.assertEqual(m2.manifest_text(),
469 ". 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")
471 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md5sum.txt'].as_manifest(),
472 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
473 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md6sum.txt'].as_manifest(),
474 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
475 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md7sum.txt'].as_manifest(),
476 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
477 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md9sum.txt'].as_manifest(),
478 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
480 def test_write_directory_tree(self):
481 cwriter = arvados.CollectionWriter()
482 cwriter.write_directory_tree(self.build_directory_tree(
483 ['basefile', 'subdir/subfile']))
484 self.assertEqual(cwriter.manifest_text(),
485 """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
486 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
488 def test_write_named_directory_tree(self):
489 cwriter = arvados.CollectionWriter()
490 cwriter.write_directory_tree(self.build_directory_tree(
491 ['basefile', 'subdir/subfile']), 'root')
493 cwriter.manifest_text(),
494 """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
495 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
497 def test_write_directory_tree_in_one_stream(self):
498 cwriter = arvados.CollectionWriter()
499 cwriter.write_directory_tree(self.build_directory_tree(
500 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
501 self.assertEqual(cwriter.manifest_text(),
502 """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile
503 ./subdir 4ace875ffdc6824a04950f06858f4465+22 8:14:subfile\n""")
505 def test_write_directory_tree_with_limited_recursion(self):
506 cwriter = arvados.CollectionWriter()
507 cwriter.write_directory_tree(
508 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
509 max_manifest_depth=1)
510 self.assertEqual(cwriter.manifest_text(),
511 """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
512 ./d1 50170217e5b04312024aa5cd42934494+13 8:5:f2
513 ./d1/d2 50170217e5b04312024aa5cd42934494+13 0:8:f3\n""")
515 def test_write_one_file(self):
516 cwriter = arvados.CollectionWriter()
517 with self.make_test_file() as testfile:
518 cwriter.write_file(testfile.name)
520 cwriter.manifest_text(),
521 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
522 os.path.basename(testfile.name)))
524 def test_write_named_file(self):
525 cwriter = arvados.CollectionWriter()
526 with self.make_test_file() as testfile:
527 cwriter.write_file(testfile.name, 'foo')
528 self.assertEqual(cwriter.manifest_text(),
529 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
531 def test_write_multiple_files(self):
532 cwriter = arvados.CollectionWriter()
534 with self.make_test_file(letter) as testfile:
535 cwriter.write_file(testfile.name, letter)
537 cwriter.manifest_text(),
538 ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
540 def test_basic_resume(self):
541 cwriter = TestResumableWriter()
542 with self.make_test_file() as testfile:
543 cwriter.write_file(testfile.name, 'test')
544 resumed = TestResumableWriter.from_state(cwriter.current_state())
545 self.assertEquals(cwriter.manifest_text(), resumed.manifest_text(),
546 "resumed CollectionWriter had different manifest")
548 def test_resume_fails_when_missing_dependency(self):
549 cwriter = TestResumableWriter()
550 with self.make_test_file() as testfile:
551 cwriter.write_file(testfile.name, 'test')
552 self.assertRaises(arvados.errors.StaleWriterStateError,
553 TestResumableWriter.from_state,
554 cwriter.current_state())
556 def test_resume_fails_when_dependency_mtime_changed(self):
557 cwriter = TestResumableWriter()
558 with self.make_test_file() as testfile:
559 cwriter.write_file(testfile.name, 'test')
560 os.utime(testfile.name, (0, 0))
561 self.assertRaises(arvados.errors.StaleWriterStateError,
562 TestResumableWriter.from_state,
563 cwriter.current_state())
565 def test_resume_fails_when_dependency_is_nonfile(self):
566 cwriter = TestResumableWriter()
567 cwriter.write_file('/dev/null', 'empty')
568 self.assertRaises(arvados.errors.StaleWriterStateError,
569 TestResumableWriter.from_state,
570 cwriter.current_state())
572 def test_resume_fails_when_dependency_size_changed(self):
573 cwriter = TestResumableWriter()
574 with self.make_test_file() as testfile:
575 cwriter.write_file(testfile.name, 'test')
576 orig_mtime = os.fstat(testfile.fileno()).st_mtime
577 testfile.write('extra')
579 os.utime(testfile.name, (orig_mtime, orig_mtime))
580 self.assertRaises(arvados.errors.StaleWriterStateError,
581 TestResumableWriter.from_state,
582 cwriter.current_state())
584 def test_resume_fails_with_expired_locator(self):
585 cwriter = TestResumableWriter()
586 state = cwriter.current_state()
587 # Add an expired locator to the state.
588 state['_current_stream_locators'].append(''.join([
589 'a' * 32, '+A', 'b' * 40, '@', '10000000']))
590 self.assertRaises(arvados.errors.StaleWriterStateError,
591 TestResumableWriter.from_state, state)
593 def test_arbitrary_objects_not_resumable(self):
594 cwriter = TestResumableWriter()
595 with open('/dev/null') as badfile:
596 self.assertRaises(arvados.errors.AssertionError,
597 cwriter.write_file, badfile)
599 def test_arbitrary_writes_not_resumable(self):
600 cwriter = TestResumableWriter()
601 self.assertRaises(arvados.errors.AssertionError,
602 cwriter.write, "badtext")
605 if __name__ == '__main__':