3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
14 import run_test_server
15 from arvados_testutil import ArvadosBaseTestCase
17 class TestResumableWriter(arvados.ResumableCollectionWriter):
18 KEEP_BLOCK_SIZE = 1024 # PUT to Keep every 1K.
20 def current_state(self):
21 return self.dump_state(copy.deepcopy)
24 class ArvadosCollectionsTest(run_test_server.TestCaseWithServers,
30 super(ArvadosCollectionsTest, cls).setUpClass()
31 run_test_server.authorize_with('active')
32 cls.api_client = arvados.api('v1')
33 cls.keep_client = arvados.KeepClient(api_client=cls.api_client,
34 local_store=cls.local_store)
36 def write_foo_bar_baz(self):
37 cw = arvados.CollectionWriter(self.api_client)
38 self.assertEqual(cw.current_stream_name(), '.',
39 'current_stream_name() should be "." now')
40 cw.set_current_file_name('foo.txt')
42 self.assertEqual(cw.current_file_name(), 'foo.txt',
43 'current_file_name() should be foo.txt now')
44 cw.start_new_file('bar.txt')
46 cw.start_new_stream('baz')
48 cw.set_current_file_name('baz.txt')
51 def test_keep_local_store(self):
52 self.assertEqual(self.keep_client.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
53 self.assertEqual(self.keep_client.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
55 def test_local_collection_writer(self):
56 self.assertEqual(self.write_foo_bar_baz(),
57 'd6c3b8e571f1b81ebb150a45ed06c884+114',
58 "wrong locator hash for files foo, bar, baz")
60 def test_local_collection_reader(self):
61 self.write_foo_bar_baz()
62 cr = arvados.CollectionReader(
63 'd6c3b8e571f1b81ebb150a45ed06c884+114+Xzizzle', self.api_client)
65 for s in cr.all_streams():
66 for f in s.all_files():
67 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
68 expected = [[3, '.', 'bar.txt', 'bar'],
69 [3, '.', 'foo.txt', 'foo'],
70 [3, './baz', 'baz.txt', 'baz']]
73 stream0 = cr.all_streams()[0]
74 self.assertEqual(stream0.readfrom(0, 0),
76 'reading zero bytes should have returned empty string')
77 self.assertEqual(stream0.readfrom(0, 2**26),
79 'reading entire stream failed')
80 self.assertEqual(stream0.readfrom(2**26, 0),
82 'reading zero bytes should have returned empty string')
84 def _test_subset(self, collection, expected):
85 cr = arvados.CollectionReader(collection, self.api_client)
86 for s in cr.all_streams():
90 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
93 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
95 def test_collection_manifest_subset(self):
96 self.write_foo_bar_baz()
97 self._test_subset('d6c3b8e571f1b81ebb150a45ed06c884+114',
98 [[3, '.', 'bar.txt', 'bar'],
99 [3, '.', 'foo.txt', 'foo'],
100 [3, './baz', 'baz.txt', 'baz']])
101 self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
102 (self.keep_client.put("foo"),
103 self.keep_client.put("bar"))),
104 [[3, '.', 'bar.txt', 'bar'],
105 [3, '.', 'foo.txt', 'foo']])
106 self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
107 (self.keep_client.put("foo"),
108 self.keep_client.put("bar"))),
109 [[2, '.', 'fo.txt', 'fo'],
110 [4, '.', 'obar.txt', 'obar']])
111 self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
112 (self.keep_client.put("foo"),
113 self.keep_client.put("bar"))),
114 [[2, '.', 'ar.txt', 'ar'],
115 [2, '.', 'fo.txt', 'fo'],
116 [2, '.', 'ob.txt', 'ob'],
117 [0, '.', 'zero.txt', '']])
119 def _test_readline(self, what_in, what_out):
120 cw = arvados.CollectionWriter(self.api_client)
121 cw.start_new_file('test.txt')
124 cr = arvados.CollectionReader(test1, self.api_client)
126 for x in list(cr.all_files())[0].readlines():
128 self.assertEqual(got,
130 "readlines did not split lines correctly: %s" % got)
132 def test_collection_readline(self):
133 self._test_readline("\na\nbcd\n\nefg\nz",
134 ["\n", "a\n", "bcd\n", "\n", "efg\n", "z"])
135 self._test_readline("ab\ncd\n",
138 def test_collection_empty_file(self):
139 cw = arvados.CollectionWriter(self.api_client)
140 cw.start_new_file('zero.txt')
143 self.assertEqual(cw.manifest_text(), ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:zero.txt\n")
144 self.check_manifest_file_sizes(cw.manifest_text(), [0])
145 cw = arvados.CollectionWriter(self.api_client)
146 cw.start_new_file('zero.txt')
148 cw.start_new_file('one.txt')
150 cw.start_new_stream('foo')
151 cw.start_new_file('zero.txt')
153 self.check_manifest_file_sizes(cw.manifest_text(), [1,0,0])
155 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
156 cr = arvados.CollectionReader(manifest_text, self.api_client)
158 for f in cr.all_files():
159 got_sizes += [f.size()]
160 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
162 def test_collection_bz2_decompression(self):
165 for x in xrange(0, 18):
167 compressed_data_in = bz2.compress(data_in)
168 cw = arvados.CollectionWriter(self.api_client)
169 cw.start_new_file('test.bz2')
170 cw.write(compressed_data_in)
171 bz2_manifest = cw.manifest_text()
173 cr = arvados.CollectionReader(bz2_manifest, self.api_client)
176 for x in list(cr.all_files())[0].readlines():
177 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
179 self.assertEqual(got,
181 "decompression returned %d lines instead of %d" % (got, n_lines_in))
183 def test_collection_gzip_decompression(self):
186 for x in xrange(0, 18):
188 p = subprocess.Popen(["gzip", "-1cn"],
189 stdout=subprocess.PIPE,
190 stdin=subprocess.PIPE,
191 stderr=subprocess.PIPE,
192 shell=False, close_fds=True)
193 compressed_data_in, stderrdata = p.communicate(data_in)
195 cw = arvados.CollectionWriter(self.api_client)
196 cw.start_new_file('test.gz')
197 cw.write(compressed_data_in)
198 gzip_manifest = cw.manifest_text()
200 cr = arvados.CollectionReader(gzip_manifest, self.api_client)
202 for x in list(cr.all_files())[0].readlines():
203 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
205 self.assertEqual(got,
207 "decompression returned %d lines instead of %d" % (got, n_lines_in))
209 def test_normalized_collection(self):
210 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
211 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
212 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
213 self.assertEqual(arvados.CollectionReader(m1, self.api_client).manifest_text(),
214 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
217 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
219 self.assertEqual(arvados.CollectionReader(m2, self.api_client).manifest_text(), m2)
221 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
222 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
223 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
224 self.assertEqual(arvados.CollectionReader(m3, self.api_client).manifest_text(),
225 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
228 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
229 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
230 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar"""
231 self.assertEqual(arvados.CollectionReader(m4, self.api_client).manifest_text(),
232 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
233 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
236 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
237 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
238 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar"""
239 self.assertEqual(arvados.CollectionReader(m5, self.api_client).manifest_text(),
240 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
241 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
244 with self.data_file('1000G_ref_manifest') as f6:
246 self.assertEqual(arvados.CollectionReader(m6, self.api_client).manifest_text(), m6)
248 with self.data_file('jlake_manifest') as f7:
250 self.assertEqual(arvados.CollectionReader(m7, self.api_client).manifest_text(), m7)
252 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
254 self.assertEqual(arvados.CollectionReader(m8, self.api_client).manifest_text(), m8)
256 def test_locators_and_ranges(self):
257 blocks2 = [['a', 10, 0],
264 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [['a', 10, 2, 2]])
265 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [['b', 10, 2, 2]])
266 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [['c', 10, 2, 2]])
267 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [['d', 10, 2, 2]])
268 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [['e', 10, 2, 2]])
269 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [['f', 10, 2, 2]])
270 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
271 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
273 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [['a', 10, 0, 2]])
274 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [['b', 10, 0, 2]])
275 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [['c', 10, 0, 2]])
276 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [['d', 10, 0, 2]])
277 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [['e', 10, 0, 2]])
278 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [['f', 10, 0, 2]])
279 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
280 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
282 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [['a', 10, 9, 1], ['b', 10, 0, 1]])
283 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [['b', 10, 9, 1], ['c', 10, 0, 1]])
284 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [['c', 10, 9, 1], ['d', 10, 0, 1]])
285 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [['d', 10, 9, 1], ['e', 10, 0, 1]])
286 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [['e', 10, 9, 1], ['f', 10, 0, 1]])
287 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [['f', 10, 9, 1]])
290 blocks3 = [['a', 10, 0],
298 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [['a', 10, 2, 2]])
299 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [['b', 10, 2, 2]])
300 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [['c', 10, 2, 2]])
301 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [['d', 10, 2, 2]])
302 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [['e', 10, 2, 2]])
303 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [['f', 10, 2, 2]])
304 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [['g', 10, 2, 2]])
307 blocks = [['a', 10, 0],
310 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
311 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [['a', 10, 0, 5]])
312 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [['a', 10, 3, 5]])
313 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [['a', 10, 0, 10]])
315 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [['a', 10, 0, 10],
317 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [['a', 10, 1, 9],
319 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [['a', 10, 0, 10],
322 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [['a', 10, 0, 10],
325 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [['a', 10, 1, 9],
328 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [['a', 10, 0, 10],
332 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [['b', 15, 5, 5]])
334 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [['a', 10, 8, 2],
337 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [['a', 10, 8, 2],
341 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [['c', 5, 1, 2]])
343 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [['a', 10, 9, 1],
345 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [['b', 15, 0, 15]])
346 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [['b', 15, 1, 14],
349 class MockStreamReader(object):
350 def __init__(self, content):
351 self.content = content
353 def readfrom(self, start, size):
354 return self.content[start:start+size]
356 def test_file_stream(self):
357 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
358 msr = self.MockStreamReader(content)
359 segments = [[0, 10, 0],
363 sfr = arvados.StreamFileReader(msr, segments, "test")
365 self.assertEqual(sfr.name(), "test")
366 self.assertEqual(sfr.size(), 30)
368 self.assertEqual(sfr.readfrom(0, 30), content[0:30])
369 self.assertEqual(sfr.readfrom(2, 30), content[2:30])
371 self.assertEqual(sfr.readfrom(2, 8), content[2:10])
372 self.assertEqual(sfr.readfrom(0, 10), content[0:10])
374 self.assertEqual(sfr.tell(), 0)
375 self.assertEqual(sfr.read(5), content[0:5])
376 self.assertEqual(sfr.tell(), 5)
377 self.assertEqual(sfr.read(5), content[5:10])
378 self.assertEqual(sfr.tell(), 10)
379 self.assertEqual(sfr.read(5), content[10:15])
380 self.assertEqual(sfr.tell(), 15)
381 self.assertEqual(sfr.read(5), content[15:20])
382 self.assertEqual(sfr.tell(), 20)
383 self.assertEqual(sfr.read(5), content[20:25])
384 self.assertEqual(sfr.tell(), 25)
385 self.assertEqual(sfr.read(5), content[25:30])
386 self.assertEqual(sfr.tell(), 30)
387 self.assertEqual(sfr.read(5), '')
388 self.assertEqual(sfr.tell(), 30)
390 segments = [[26, 10, 0],
394 sfr = arvados.StreamFileReader(msr, segments, "test")
396 self.assertEqual(sfr.size(), 30)
398 self.assertEqual(sfr.readfrom(0, 30), content[26:36] + content[0:20])
399 self.assertEqual(sfr.readfrom(2, 30), content[28:36] + content[0:20])
401 self.assertEqual(sfr.readfrom(2, 8), content[28:36])
402 self.assertEqual(sfr.readfrom(0, 10), content[26:36])
404 self.assertEqual(sfr.tell(), 0)
405 self.assertEqual(sfr.read(5), content[26:31])
406 self.assertEqual(sfr.tell(), 5)
407 self.assertEqual(sfr.read(5), content[31:36])
408 self.assertEqual(sfr.tell(), 10)
409 self.assertEqual(sfr.read(5), content[0:5])
410 self.assertEqual(sfr.tell(), 15)
411 self.assertEqual(sfr.read(5), content[5:10])
412 self.assertEqual(sfr.tell(), 20)
413 self.assertEqual(sfr.read(5), content[10:15])
414 self.assertEqual(sfr.tell(), 25)
415 self.assertEqual(sfr.read(5), content[15:20])
416 self.assertEqual(sfr.tell(), 30)
417 self.assertEqual(sfr.read(5), '')
418 self.assertEqual(sfr.tell(), 30)
421 class MockKeep(object):
422 def __init__(self, content):
423 self.content = content
425 def get(self, locator):
426 return self.content[locator]
428 def test_stream_reader(self):
429 keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
430 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
431 'cccccccccccccccccccccccccccccccc+5': 'z0123'}
432 mk = self.MockKeep(keepblocks)
434 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
436 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
438 self.assertEqual(sr.readfrom(0, 30), content[0:30])
439 self.assertEqual(sr.readfrom(2, 30), content[2:30])
441 self.assertEqual(sr.readfrom(2, 8), content[2:10])
442 self.assertEqual(sr.readfrom(0, 10), content[0:10])
444 self.assertEqual(sr.readfrom(0, 5), content[0:5])
445 self.assertEqual(sr.readfrom(5, 5), content[5:10])
446 self.assertEqual(sr.readfrom(10, 5), content[10:15])
447 self.assertEqual(sr.readfrom(15, 5), content[15:20])
448 self.assertEqual(sr.readfrom(20, 5), content[20:25])
449 self.assertEqual(sr.readfrom(25, 5), content[25:30])
450 self.assertEqual(sr.readfrom(30, 5), '')
452 def test_file_reader(self):
453 keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
454 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
455 'cccccccccccccccccccccccccccccccc+5': 'z0123'}
456 mk = self.MockKeep(keepblocks)
458 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:10:foo", "15:10:foo"], mk)
460 content = 'abcdefghijpqrstuvwxy'
462 f = sr.files()["foo"]
464 # f.read() calls will be aligned on block boundaries (as a
465 # result of ticket #3663).
468 self.assertEqual(f.read(20), content[0:10])
471 self.assertEqual(f.read(6), content[0:6])
472 self.assertEqual(f.read(6), content[6:10])
473 self.assertEqual(f.read(6), content[10:16])
474 self.assertEqual(f.read(6), content[16:20])
476 def test_extract_file(self):
477 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
478 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
479 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
480 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
481 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt"""
483 m2 = arvados.CollectionReader(m1, self.api_client)
485 self.assertEqual(m2.manifest_text(),
486 ". 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")
488 self.assertEqual(arvados.CollectionReader(m1, self.api_client).all_streams()[0].files()['md5sum.txt'].as_manifest(),
489 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
490 self.assertEqual(arvados.CollectionReader(m1, self.api_client).all_streams()[0].files()['md6sum.txt'].as_manifest(),
491 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
492 self.assertEqual(arvados.CollectionReader(m1, self.api_client).all_streams()[0].files()['md7sum.txt'].as_manifest(),
493 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
494 self.assertEqual(arvados.CollectionReader(m1, self.api_client).all_streams()[0].files()['md9sum.txt'].as_manifest(),
495 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
497 def test_write_directory_tree(self):
498 cwriter = arvados.CollectionWriter(self.api_client)
499 cwriter.write_directory_tree(self.build_directory_tree(
500 ['basefile', 'subdir/subfile']))
501 self.assertEqual(cwriter.manifest_text(),
502 """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
503 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
505 def test_write_named_directory_tree(self):
506 cwriter = arvados.CollectionWriter(self.api_client)
507 cwriter.write_directory_tree(self.build_directory_tree(
508 ['basefile', 'subdir/subfile']), 'root')
510 cwriter.manifest_text(),
511 """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
512 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
514 def test_write_directory_tree_in_one_stream(self):
515 cwriter = arvados.CollectionWriter(self.api_client)
516 cwriter.write_directory_tree(self.build_directory_tree(
517 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
518 self.assertEqual(cwriter.manifest_text(),
519 """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile
520 ./subdir 4ace875ffdc6824a04950f06858f4465+22 8:14:subfile\n""")
522 def test_write_directory_tree_with_limited_recursion(self):
523 cwriter = arvados.CollectionWriter(self.api_client)
524 cwriter.write_directory_tree(
525 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
526 max_manifest_depth=1)
527 self.assertEqual(cwriter.manifest_text(),
528 """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
529 ./d1 50170217e5b04312024aa5cd42934494+13 8:5:f2
530 ./d1/d2 50170217e5b04312024aa5cd42934494+13 0:8:f3\n""")
532 def test_write_one_file(self):
533 cwriter = arvados.CollectionWriter(self.api_client)
534 with self.make_test_file() as testfile:
535 cwriter.write_file(testfile.name)
537 cwriter.manifest_text(),
538 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
539 os.path.basename(testfile.name)))
541 def test_write_named_file(self):
542 cwriter = arvados.CollectionWriter(self.api_client)
543 with self.make_test_file() as testfile:
544 cwriter.write_file(testfile.name, 'foo')
545 self.assertEqual(cwriter.manifest_text(),
546 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
548 def test_write_multiple_files(self):
549 cwriter = arvados.CollectionWriter(self.api_client)
551 with self.make_test_file(letter) as testfile:
552 cwriter.write_file(testfile.name, letter)
554 cwriter.manifest_text(),
555 ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
557 def test_basic_resume(self):
558 cwriter = TestResumableWriter()
559 with self.make_test_file() as testfile:
560 cwriter.write_file(testfile.name, 'test')
561 resumed = TestResumableWriter.from_state(cwriter.current_state())
562 self.assertEquals(cwriter.manifest_text(), resumed.manifest_text(),
563 "resumed CollectionWriter had different manifest")
565 def test_resume_fails_when_missing_dependency(self):
566 cwriter = TestResumableWriter()
567 with self.make_test_file() as testfile:
568 cwriter.write_file(testfile.name, 'test')
569 self.assertRaises(arvados.errors.StaleWriterStateError,
570 TestResumableWriter.from_state,
571 cwriter.current_state())
573 def test_resume_fails_when_dependency_mtime_changed(self):
574 cwriter = TestResumableWriter()
575 with self.make_test_file() as testfile:
576 cwriter.write_file(testfile.name, 'test')
577 os.utime(testfile.name, (0, 0))
578 self.assertRaises(arvados.errors.StaleWriterStateError,
579 TestResumableWriter.from_state,
580 cwriter.current_state())
582 def test_resume_fails_when_dependency_is_nonfile(self):
583 cwriter = TestResumableWriter()
584 cwriter.write_file('/dev/null', 'empty')
585 self.assertRaises(arvados.errors.StaleWriterStateError,
586 TestResumableWriter.from_state,
587 cwriter.current_state())
589 def test_resume_fails_when_dependency_size_changed(self):
590 cwriter = TestResumableWriter()
591 with self.make_test_file() as testfile:
592 cwriter.write_file(testfile.name, 'test')
593 orig_mtime = os.fstat(testfile.fileno()).st_mtime
594 testfile.write('extra')
596 os.utime(testfile.name, (orig_mtime, orig_mtime))
597 self.assertRaises(arvados.errors.StaleWriterStateError,
598 TestResumableWriter.from_state,
599 cwriter.current_state())
601 def test_resume_fails_with_expired_locator(self):
602 cwriter = TestResumableWriter()
603 state = cwriter.current_state()
604 # Add an expired locator to the state.
605 state['_current_stream_locators'].append(''.join([
606 'a' * 32, '+1+A', 'b' * 40, '@', '10000000']))
607 self.assertRaises(arvados.errors.StaleWriterStateError,
608 TestResumableWriter.from_state, state)
610 def test_arbitrary_objects_not_resumable(self):
611 cwriter = TestResumableWriter()
612 with open('/dev/null') as badfile:
613 self.assertRaises(arvados.errors.AssertionError,
614 cwriter.write_file, badfile)
616 def test_arbitrary_writes_not_resumable(self):
617 cwriter = TestResumableWriter()
618 self.assertRaises(arvados.errors.AssertionError,
619 cwriter.write, "badtext")
622 if __name__ == '__main__':