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.
20 self.saved_states = []
21 return super(TestResumableWriter, self).__init__()
23 def checkpoint_state(self):
24 self.saved_states.append(self.dump_state(copy.deepcopy))
27 assert self.saved_states, "resumable writer did not save any state"
28 return self.saved_states[-1]
31 class ArvadosCollectionsTest(ArvadosKeepLocalStoreTestCase):
32 def write_foo_bar_baz(self):
33 cw = arvados.CollectionWriter()
34 self.assertEqual(cw.current_stream_name(), '.',
35 'current_stream_name() should be "." now')
36 cw.set_current_file_name('foo.txt')
38 self.assertEqual(cw.current_file_name(), 'foo.txt',
39 'current_file_name() should be foo.txt now')
40 cw.start_new_file('bar.txt')
42 cw.start_new_stream('baz')
44 cw.set_current_file_name('baz.txt')
47 def test_keep_local_store(self):
48 self.assertEqual(arvados.Keep.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
49 self.assertEqual(arvados.Keep.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
51 def test_local_collection_writer(self):
52 self.assertEqual(self.write_foo_bar_baz(),
53 'd6c3b8e571f1b81ebb150a45ed06c884+114',
54 "wrong locator hash for files foo, bar, baz")
56 def test_local_collection_reader(self):
57 self.write_foo_bar_baz()
58 cr = arvados.CollectionReader('d6c3b8e571f1b81ebb150a45ed06c884+114+Xzizzle')
60 for s in cr.all_streams():
61 for f in s.all_files():
62 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
63 expected = [[3, '.', 'bar.txt', 'bar'],
64 [3, '.', 'foo.txt', 'foo'],
65 [3, './baz', 'baz.txt', 'baz']]
68 stream0 = cr.all_streams()[0]
69 self.assertEqual(stream0.readfrom(0, 0),
71 'reading zero bytes should have returned empty string')
72 self.assertEqual(stream0.readfrom(0, 2**26),
74 'reading entire stream failed')
75 self.assertEqual(stream0.readfrom(2**26, 0),
77 'reading zero bytes should have returned empty string')
79 def _test_subset(self, collection, expected):
80 cr = arvados.CollectionReader(collection)
81 for s in cr.all_streams():
85 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
88 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
90 def test_collection_manifest_subset(self):
91 self.write_foo_bar_baz()
92 self._test_subset('d6c3b8e571f1b81ebb150a45ed06c884+114',
93 [[3, '.', 'bar.txt', 'bar'],
94 [3, '.', 'foo.txt', 'foo'],
95 [3, './baz', 'baz.txt', 'baz']])
96 self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
97 (arvados.Keep.put("foo"),
98 arvados.Keep.put("bar"))),
99 [[3, '.', 'bar.txt', 'bar'],
100 [3, '.', 'foo.txt', 'foo']])
101 self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
102 (arvados.Keep.put("foo"),
103 arvados.Keep.put("bar"))),
104 [[2, '.', 'fo.txt', 'fo'],
105 [4, '.', 'obar.txt', 'obar']])
106 self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
107 (arvados.Keep.put("foo"),
108 arvados.Keep.put("bar"))),
109 [[2, '.', 'ar.txt', 'ar'],
110 [2, '.', 'fo.txt', 'fo'],
111 [2, '.', 'ob.txt', 'ob'],
112 [0, '.', 'zero.txt', '']])
114 def _test_readline(self, what_in, what_out):
115 cw = arvados.CollectionWriter()
116 cw.start_new_file('test.txt')
119 cr = arvados.CollectionReader(test1)
121 for x in list(cr.all_files())[0].readlines():
123 self.assertEqual(got,
125 "readlines did not split lines correctly: %s" % got)
127 def test_collection_readline(self):
128 self._test_readline("\na\nbcd\n\nefg\nz",
129 ["\n", "a\n", "bcd\n", "\n", "efg\n", "z"])
130 self._test_readline("ab\ncd\n",
133 def test_collection_empty_file(self):
134 cw = arvados.CollectionWriter()
135 cw.start_new_file('zero.txt')
138 self.assertEqual(cw.manifest_text(), ". 0:0:zero.txt\n")
139 self.check_manifest_file_sizes(cw.manifest_text(), [0])
140 cw = arvados.CollectionWriter()
141 cw.start_new_file('zero.txt')
143 cw.start_new_file('one.txt')
145 cw.start_new_stream('foo')
146 cw.start_new_file('zero.txt')
148 self.check_manifest_file_sizes(cw.manifest_text(), [1,0,0])
150 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
151 cr = arvados.CollectionReader(manifest_text)
153 for f in cr.all_files():
154 got_sizes += [f.size()]
155 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
157 def test_collection_bz2_decompression(self):
160 for x in xrange(0, 18):
162 compressed_data_in = bz2.compress(data_in)
163 cw = arvados.CollectionWriter()
164 cw.start_new_file('test.bz2')
165 cw.write(compressed_data_in)
166 bz2_manifest = cw.manifest_text()
168 cr = arvados.CollectionReader(bz2_manifest)
171 for x in list(cr.all_files())[0].readlines():
172 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
174 self.assertEqual(got,
176 "decompression returned %d lines instead of %d" % (got, n_lines_in))
178 def test_collection_gzip_decompression(self):
181 for x in xrange(0, 18):
183 p = subprocess.Popen(["gzip", "-1cn"],
184 stdout=subprocess.PIPE,
185 stdin=subprocess.PIPE,
186 stderr=subprocess.PIPE,
187 shell=False, close_fds=True)
188 compressed_data_in, stderrdata = p.communicate(data_in)
190 cw = arvados.CollectionWriter()
191 cw.start_new_file('test.gz')
192 cw.write(compressed_data_in)
193 gzip_manifest = cw.manifest_text()
195 cr = arvados.CollectionReader(gzip_manifest)
197 for x in list(cr.all_files())[0].readlines():
198 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
200 self.assertEqual(got,
202 "decompression returned %d lines instead of %d" % (got, n_lines_in))
204 def test_normalized_collection(self):
205 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
206 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
207 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
208 self.assertEqual(arvados.CollectionReader(m1).manifest_text(),
209 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
212 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
214 self.assertEqual(arvados.CollectionReader(m2).manifest_text(), m2)
216 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
217 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
218 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
219 self.assertEqual(arvados.CollectionReader(m3).manifest_text(),
220 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
223 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
224 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
225 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar"""
226 self.assertEqual(arvados.CollectionReader(m4).manifest_text(),
227 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
228 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
231 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
232 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
233 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar"""
234 self.assertEqual(arvados.CollectionReader(m5).manifest_text(),
235 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
236 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
239 with self.data_file('1000G_ref_manifest') as f6:
241 self.assertEqual(arvados.CollectionReader(m6).manifest_text(), m6)
243 with self.data_file('jlake_manifest') as f7:
245 self.assertEqual(arvados.CollectionReader(m7).manifest_text(), m7)
247 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
249 self.assertEqual(arvados.CollectionReader(m8).manifest_text(), m8)
251 def test_locators_and_ranges(self):
252 blocks2 = [['a', 10, 0],
259 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [['a', 10, 2, 2]])
260 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [['b', 10, 2, 2]])
261 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [['c', 10, 2, 2]])
262 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [['d', 10, 2, 2]])
263 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [['e', 10, 2, 2]])
264 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [['f', 10, 2, 2]])
265 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
266 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
268 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [['a', 10, 0, 2]])
269 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [['b', 10, 0, 2]])
270 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [['c', 10, 0, 2]])
271 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [['d', 10, 0, 2]])
272 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [['e', 10, 0, 2]])
273 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [['f', 10, 0, 2]])
274 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
275 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
277 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [['a', 10, 9, 1], ['b', 10, 0, 1]])
278 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [['b', 10, 9, 1], ['c', 10, 0, 1]])
279 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [['c', 10, 9, 1], ['d', 10, 0, 1]])
280 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [['d', 10, 9, 1], ['e', 10, 0, 1]])
281 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [['e', 10, 9, 1], ['f', 10, 0, 1]])
282 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [['f', 10, 9, 1]])
285 blocks3 = [['a', 10, 0],
293 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [['a', 10, 2, 2]])
294 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [['b', 10, 2, 2]])
295 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [['c', 10, 2, 2]])
296 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [['d', 10, 2, 2]])
297 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [['e', 10, 2, 2]])
298 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [['f', 10, 2, 2]])
299 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [['g', 10, 2, 2]])
302 blocks = [['a', 10, 0],
305 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
306 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [['a', 10, 0, 5]])
307 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [['a', 10, 3, 5]])
308 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [['a', 10, 0, 10]])
310 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [['a', 10, 0, 10],
312 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [['a', 10, 1, 9],
314 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [['a', 10, 0, 10],
317 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [['a', 10, 0, 10],
320 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [['a', 10, 1, 9],
323 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [['a', 10, 0, 10],
327 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [['b', 15, 5, 5]])
329 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [['a', 10, 8, 2],
332 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [['a', 10, 8, 2],
336 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [['c', 5, 1, 2]])
338 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [['a', 10, 9, 1],
340 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [['b', 15, 0, 15]])
341 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [['b', 15, 1, 14],
344 class MockStreamReader(object):
345 def __init__(self, content):
346 self.content = content
348 def readfrom(self, start, size):
349 return self.content[start:start+size]
351 def test_file_stream(self):
352 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
353 msr = self.MockStreamReader(content)
354 segments = [[0, 10, 0],
358 sfr = arvados.StreamFileReader(msr, segments, "test")
360 self.assertEqual(sfr.name(), "test")
361 self.assertEqual(sfr.size(), 30)
363 self.assertEqual(sfr.readfrom(0, 30), content[0:30])
364 self.assertEqual(sfr.readfrom(2, 30), content[2:30])
366 self.assertEqual(sfr.readfrom(2, 8), content[2:10])
367 self.assertEqual(sfr.readfrom(0, 10), content[0:10])
369 self.assertEqual(sfr.tell(), 0)
370 self.assertEqual(sfr.read(5), content[0:5])
371 self.assertEqual(sfr.tell(), 5)
372 self.assertEqual(sfr.read(5), content[5:10])
373 self.assertEqual(sfr.tell(), 10)
374 self.assertEqual(sfr.read(5), content[10:15])
375 self.assertEqual(sfr.tell(), 15)
376 self.assertEqual(sfr.read(5), content[15:20])
377 self.assertEqual(sfr.tell(), 20)
378 self.assertEqual(sfr.read(5), content[20:25])
379 self.assertEqual(sfr.tell(), 25)
380 self.assertEqual(sfr.read(5), content[25:30])
381 self.assertEqual(sfr.tell(), 30)
382 self.assertEqual(sfr.read(5), '')
383 self.assertEqual(sfr.tell(), 30)
385 segments = [[26, 10, 0],
389 sfr = arvados.StreamFileReader(msr, segments, "test")
391 self.assertEqual(sfr.size(), 30)
393 self.assertEqual(sfr.readfrom(0, 30), content[26:36] + content[0:20])
394 self.assertEqual(sfr.readfrom(2, 30), content[28:36] + content[0:20])
396 self.assertEqual(sfr.readfrom(2, 8), content[28:36])
397 self.assertEqual(sfr.readfrom(0, 10), content[26:36])
399 self.assertEqual(sfr.tell(), 0)
400 self.assertEqual(sfr.read(5), content[26:31])
401 self.assertEqual(sfr.tell(), 5)
402 self.assertEqual(sfr.read(5), content[31:36])
403 self.assertEqual(sfr.tell(), 10)
404 self.assertEqual(sfr.read(5), content[0:5])
405 self.assertEqual(sfr.tell(), 15)
406 self.assertEqual(sfr.read(5), content[5:10])
407 self.assertEqual(sfr.tell(), 20)
408 self.assertEqual(sfr.read(5), content[10:15])
409 self.assertEqual(sfr.tell(), 25)
410 self.assertEqual(sfr.read(5), content[15:20])
411 self.assertEqual(sfr.tell(), 30)
412 self.assertEqual(sfr.read(5), '')
413 self.assertEqual(sfr.tell(), 30)
416 class MockKeep(object):
417 def __init__(self, content):
418 self.content = content
420 def get(self, locator):
421 return self.content[locator]
423 def test_stream_reader(self):
424 keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
425 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
426 'cccccccccccccccccccccccccccccccc+5': 'z0123'}
427 mk = self.MockKeep(keepblocks)
429 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
431 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
433 self.assertEqual(sr.readfrom(0, 30), content[0:30])
434 self.assertEqual(sr.readfrom(2, 30), content[2:30])
436 self.assertEqual(sr.readfrom(2, 8), content[2:10])
437 self.assertEqual(sr.readfrom(0, 10), content[0:10])
439 self.assertEqual(sr.readfrom(0, 5), content[0:5])
440 self.assertEqual(sr.readfrom(5, 5), content[5:10])
441 self.assertEqual(sr.readfrom(10, 5), content[10:15])
442 self.assertEqual(sr.readfrom(15, 5), content[15:20])
443 self.assertEqual(sr.readfrom(20, 5), content[20:25])
444 self.assertEqual(sr.readfrom(25, 5), content[25:30])
445 self.assertEqual(sr.readfrom(30, 5), '')
447 def test_extract_file(self):
448 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
449 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
450 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
451 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
452 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt"""
454 m2 = arvados.CollectionReader(m1)
456 self.assertEqual(m2.manifest_text(),
457 ". 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")
459 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md5sum.txt'].as_manifest(),
460 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
461 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md6sum.txt'].as_manifest(),
462 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
463 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md7sum.txt'].as_manifest(),
464 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
465 self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md9sum.txt'].as_manifest(),
466 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
468 def test_write_directory_tree(self):
469 cwriter = arvados.CollectionWriter()
470 cwriter.write_directory_tree(self.build_directory_tree(
471 ['basefile', 'subdir/subfile']))
472 self.assertEqual(cwriter.manifest_text(),
473 """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
474 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
476 def test_write_named_directory_tree(self):
477 cwriter = arvados.CollectionWriter()
478 cwriter.write_directory_tree(self.build_directory_tree(
479 ['basefile', 'subdir/subfile']), 'root')
481 cwriter.manifest_text(),
482 """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
483 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
485 def test_write_directory_tree_in_one_stream(self):
486 cwriter = arvados.CollectionWriter()
487 cwriter.write_directory_tree(self.build_directory_tree(
488 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
489 self.assertEqual(cwriter.manifest_text(),
490 """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile
491 ./subdir 4ace875ffdc6824a04950f06858f4465+22 8:14:subfile\n""")
493 def test_write_directory_tree_with_limited_recursion(self):
494 cwriter = arvados.CollectionWriter()
495 cwriter.write_directory_tree(
496 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
497 max_manifest_depth=1)
498 self.assertEqual(cwriter.manifest_text(),
499 """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
500 ./d1 50170217e5b04312024aa5cd42934494+13 8:5:f2
501 ./d1/d2 50170217e5b04312024aa5cd42934494+13 0:8:f3\n""")
503 def test_write_one_file(self):
504 cwriter = arvados.CollectionWriter()
505 with self.make_test_file() as testfile:
506 cwriter.write_file(testfile.name)
508 cwriter.manifest_text(),
509 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
510 os.path.basename(testfile.name)))
512 def test_write_named_file(self):
513 cwriter = arvados.CollectionWriter()
514 with self.make_test_file() as testfile:
515 cwriter.write_file(testfile.name, 'foo')
516 self.assertEqual(cwriter.manifest_text(),
517 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
519 def test_write_multiple_files(self):
520 cwriter = arvados.CollectionWriter()
522 with self.make_test_file(letter) as testfile:
523 cwriter.write_file(testfile.name, letter)
525 cwriter.manifest_text(),
526 ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
528 def test_checkpoint_after_put(self):
529 cwriter = TestResumableWriter()
530 with self.make_test_file(
531 't' * (cwriter.KEEP_BLOCK_SIZE + 10)) as testfile:
532 testpath = os.path.realpath(testfile.name)
533 cwriter.write_file(testpath, 'test')
534 for state in cwriter.saved_states:
535 if state.get('_current_file') == (testpath,
536 cwriter.KEEP_BLOCK_SIZE):
539 self.fail("can't find state immediately after PUT to Keep")
540 self.assertIn('d45107e93f9052fa88a82fc08bb1d316+1024', # 't' * 1024
541 state['_current_stream_locators'])
543 def test_basic_resume(self):
544 cwriter = TestResumableWriter()
545 with self.make_test_file() as testfile:
546 cwriter.write_file(testfile.name, 'test')
547 last_state = cwriter.last_state()
548 resumed = TestResumableWriter.from_state(last_state)
549 self.assertEquals(cwriter.manifest_text(), resumed.manifest_text(),
550 "resumed CollectionWriter had different manifest")
552 def test_resume_fails_when_missing_dependency(self):
553 cwriter = TestResumableWriter()
554 with self.make_test_file() as testfile:
555 cwriter.write_file(testfile.name, 'test')
556 self.assertRaises(arvados.errors.StaleWriterStateError,
557 TestResumableWriter.from_state,
558 cwriter.last_state())
560 def test_resume_fails_when_dependency_mtime_changed(self):
561 cwriter = TestResumableWriter()
562 with self.make_test_file() as testfile:
563 cwriter.write_file(testfile.name, 'test')
564 os.utime(testfile.name, (0, 0))
565 self.assertRaises(arvados.errors.StaleWriterStateError,
566 TestResumableWriter.from_state,
567 cwriter.last_state())
569 def test_resume_fails_when_dependency_is_nonfile(self):
570 cwriter = TestResumableWriter()
571 cwriter.write_file('/dev/null', 'empty')
572 self.assertRaises(arvados.errors.StaleWriterStateError,
573 TestResumableWriter.from_state,
574 cwriter.last_state())
576 def test_resume_fails_when_dependency_size_changed(self):
577 cwriter = TestResumableWriter()
578 with self.make_test_file() as testfile:
579 cwriter.write_file(testfile.name, 'test')
580 orig_mtime = os.fstat(testfile.fileno()).st_mtime
581 testfile.write('extra')
583 os.utime(testfile.name, (orig_mtime, orig_mtime))
584 self.assertRaises(arvados.errors.StaleWriterStateError,
585 TestResumableWriter.from_state,
586 cwriter.last_state())
588 def test_successful_resumes(self):
589 # FIXME: This is more of an integration test than a unit test.
590 cwriter = TestResumableWriter()
591 source_tree = self.build_directory_tree(['basefile', 'subdir/subfile'])
592 with open(os.path.join(source_tree, 'long'), 'w') as longfile:
593 longfile.write('t' * (cwriter.KEEP_BLOCK_SIZE + 10))
594 cwriter.write_directory_tree(source_tree)
595 # A state for each file, plus a fourth for mid-longfile.
596 self.assertGreater(len(cwriter.saved_states), 3,
597 "CollectionWriter didn't save enough states to test")
599 for state in cwriter.saved_states:
600 new_writer = TestResumableWriter.from_state(state)
601 manifests = [writer.manifest_text()
602 for writer in (cwriter, new_writer)]
604 manifests[0], manifests[1],
605 "\n".join(["manifest mismatch after resuming from state:",
606 pprint.pformat(state), ""] + manifests))
608 def test_arbitrary_objects_not_resumable(self):
609 cwriter = TestResumableWriter()
610 with open('/dev/null') as badfile:
611 self.assertRaises(arvados.errors.AssertionError,
612 cwriter.write_file, badfile)
614 def test_arbitrary_writes_not_resumable(self):
615 cwriter = TestResumableWriter()
616 self.assertRaises(arvados.errors.AssertionError,
617 cwriter.write, "badtext")
620 if __name__ == '__main__':