1 from __future__ import absolute_import
3 from builtins import object
14 from . import run_test_server
15 from arvados._ranges import Range, LocatorAndRange
16 from arvados.collection import Collection, CollectionReader
17 from . import arvados_testutil as tutil
19 class TestResumableWriter(arvados.ResumableCollectionWriter):
20 KEEP_BLOCK_SIZE = 1024 # PUT to Keep every 1K.
22 def current_state(self):
23 return self.dump_state(copy.deepcopy)
26 class ArvadosCollectionsTest(run_test_server.TestCaseWithServers,
27 tutil.ArvadosBaseTestCase):
32 super(ArvadosCollectionsTest, cls).setUpClass()
33 run_test_server.authorize_with('active')
34 cls.api_client = arvados.api('v1')
35 cls.keep_client = arvados.KeepClient(api_client=cls.api_client,
36 local_store=cls.local_store)
38 def write_foo_bar_baz(self):
39 cw = arvados.CollectionWriter(self.api_client)
40 self.assertEqual(cw.current_stream_name(), '.',
41 'current_stream_name() should be "." now')
42 cw.set_current_file_name('foo.txt')
44 self.assertEqual(cw.current_file_name(), 'foo.txt',
45 'current_file_name() should be foo.txt now')
46 cw.start_new_file('bar.txt')
48 cw.start_new_stream('baz')
50 cw.set_current_file_name('baz.txt')
51 self.assertEqual(cw.manifest_text(),
52 ". 3858f62230ac3c915f300c664312c63f+6 0:3:foo.txt 3:3:bar.txt\n" +
53 "./baz 73feffa4b7f6bb68e44cf984c85f6e88+3 0:3:baz.txt\n",
54 "wrong manifest: got {}".format(cw.manifest_text()))
56 return cw.portable_data_hash()
58 def test_pdh_is_native_str(self):
59 pdh = self.write_foo_bar_baz()
60 self.assertEqual(type(''), type(pdh))
62 def test_keep_local_store(self):
63 self.assertEqual(self.keep_client.put(b'foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
64 self.assertEqual(self.keep_client.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), b'foo', 'wrong data from Keep.get')
66 def test_local_collection_writer(self):
67 self.assertEqual(self.write_foo_bar_baz(),
68 '23ca013983d6239e98931cc779e68426+114',
69 'wrong locator hash: ' + self.write_foo_bar_baz())
71 def test_local_collection_reader(self):
72 foobarbaz = self.write_foo_bar_baz()
73 cr = arvados.CollectionReader(
74 foobarbaz + '+Xzizzle', self.api_client)
76 for s in cr.all_streams():
77 for f in s.all_files():
78 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
79 expected = [[3, '.', 'foo.txt', b'foo'],
80 [3, '.', 'bar.txt', b'bar'],
81 [3, './baz', 'baz.txt', b'baz']]
84 stream0 = cr.all_streams()[0]
85 self.assertEqual(stream0.readfrom(0, 0),
87 'reading zero bytes should have returned empty string')
88 self.assertEqual(stream0.readfrom(0, 2**26),
90 'reading entire stream failed')
91 self.assertEqual(stream0.readfrom(2**26, 0),
93 'reading zero bytes should have returned empty string')
95 def _test_subset(self, collection, expected):
96 cr = arvados.CollectionReader(collection, self.api_client)
97 for s in cr.all_streams():
101 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
102 self.assertEqual(got,
104 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
106 def test_collection_manifest_subset(self):
107 foobarbaz = self.write_foo_bar_baz()
108 self._test_subset(foobarbaz,
109 [[3, '.', 'bar.txt', b'bar'],
110 [3, '.', 'foo.txt', b'foo'],
111 [3, './baz', 'baz.txt', b'baz']])
112 self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
113 (self.keep_client.put(b"foo"),
114 self.keep_client.put(b"bar"))),
115 [[3, '.', 'bar.txt', b'bar'],
116 [3, '.', 'foo.txt', b'foo']])
117 self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
118 (self.keep_client.put(b"foo"),
119 self.keep_client.put(b"bar"))),
120 [[2, '.', 'fo.txt', b'fo'],
121 [4, '.', 'obar.txt', b'obar']])
122 self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
123 (self.keep_client.put(b"foo"),
124 self.keep_client.put(b"bar"))),
125 [[2, '.', 'ar.txt', b'ar'],
126 [2, '.', 'fo.txt', b'fo'],
127 [2, '.', 'ob.txt', b'ob'],
128 [0, '.', 'zero.txt', b'']])
130 def test_collection_empty_file(self):
131 cw = arvados.CollectionWriter(self.api_client)
132 cw.start_new_file('zero.txt')
135 self.assertEqual(cw.manifest_text(), ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:zero.txt\n")
136 self.check_manifest_file_sizes(cw.manifest_text(), [0])
137 cw = arvados.CollectionWriter(self.api_client)
138 cw.start_new_file('zero.txt')
140 cw.start_new_file('one.txt')
142 cw.start_new_stream('foo')
143 cw.start_new_file('zero.txt')
145 self.check_manifest_file_sizes(cw.manifest_text(), [0,1,0])
147 def test_no_implicit_normalize(self):
148 cw = arvados.CollectionWriter(self.api_client)
149 cw.start_new_file('b')
151 cw.start_new_file('a')
153 self.check_manifest_file_sizes(cw.manifest_text(), [1,0])
154 self.check_manifest_file_sizes(
155 arvados.CollectionReader(
156 cw.manifest_text()).manifest_text(normalize=True),
159 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
160 cr = arvados.CollectionReader(manifest_text, self.api_client)
162 for f in cr.all_files():
163 got_sizes += [f.size()]
164 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
166 def test_normalized_collection(self):
167 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
168 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
169 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
171 self.assertEqual(arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True),
172 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
175 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
177 self.assertEqual(arvados.CollectionReader(m2, self.api_client).manifest_text(normalize=True), m2)
179 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
180 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
181 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
183 self.assertEqual(arvados.CollectionReader(m3, self.api_client).manifest_text(normalize=True),
184 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
187 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
188 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
189 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar
191 self.assertEqual(arvados.CollectionReader(m4, self.api_client).manifest_text(normalize=True),
192 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
193 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
196 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
197 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
198 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar
200 self.assertEqual(arvados.CollectionReader(m5, self.api_client).manifest_text(normalize=True),
201 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
202 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
205 with self.data_file('1000G_ref_manifest') as f6:
207 self.assertEqual(arvados.CollectionReader(m6, self.api_client).manifest_text(normalize=True), m6)
209 with self.data_file('jlake_manifest') as f7:
211 self.assertEqual(arvados.CollectionReader(m7, self.api_client).manifest_text(normalize=True), m7)
213 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
215 self.assertEqual(arvados.CollectionReader(m8, self.api_client).manifest_text(normalize=True), m8)
217 def test_locators_and_ranges(self):
218 blocks2 = [Range('a', 0, 10),
225 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
226 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
227 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
228 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
229 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
230 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
231 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
232 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
234 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [LocatorAndRange('a', 10, 0, 2)])
235 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [LocatorAndRange('b', 10, 0, 2)])
236 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [LocatorAndRange('c', 10, 0, 2)])
237 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [LocatorAndRange('d', 10, 0, 2)])
238 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [LocatorAndRange('e', 10, 0, 2)])
239 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [LocatorAndRange('f', 10, 0, 2)])
240 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
241 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
243 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [LocatorAndRange('a', 10, 9, 1), LocatorAndRange('b', 10, 0, 1)])
244 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [LocatorAndRange('b', 10, 9, 1), LocatorAndRange('c', 10, 0, 1)])
245 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [LocatorAndRange('c', 10, 9, 1), LocatorAndRange('d', 10, 0, 1)])
246 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [LocatorAndRange('d', 10, 9, 1), LocatorAndRange('e', 10, 0, 1)])
247 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [LocatorAndRange('e', 10, 9, 1), LocatorAndRange('f', 10, 0, 1)])
248 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [LocatorAndRange('f', 10, 9, 1)])
251 blocks3 = [Range('a', 0, 10),
259 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
260 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
261 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
262 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
263 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
264 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
265 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [LocatorAndRange('g', 10, 2, 2)])
268 blocks = [Range('a', 0, 10),
271 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
272 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [LocatorAndRange('a', 10, 0, 5)])
273 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [LocatorAndRange('a', 10, 3, 5)])
274 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [LocatorAndRange('a', 10, 0, 10)])
276 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [LocatorAndRange('a', 10, 0, 10),
277 LocatorAndRange('b', 15, 0, 1)])
278 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [LocatorAndRange('a', 10, 1, 9),
279 LocatorAndRange('b', 15, 0, 2)])
280 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [LocatorAndRange('a', 10, 0, 10),
281 LocatorAndRange('b', 15, 0, 15)])
283 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [LocatorAndRange('a', 10, 0, 10),
284 LocatorAndRange('b', 15, 0, 15),
285 LocatorAndRange('c', 5, 0, 5)])
286 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [LocatorAndRange('a', 10, 1, 9),
287 LocatorAndRange('b', 15, 0, 15),
288 LocatorAndRange('c', 5, 0, 5)])
289 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [LocatorAndRange('a', 10, 0, 10),
290 LocatorAndRange('b', 15, 0, 15),
291 LocatorAndRange('c', 5, 0, 5)])
293 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [LocatorAndRange('b', 15, 5, 5)])
295 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [LocatorAndRange('a', 10, 8, 2),
296 LocatorAndRange('b', 15, 0, 15)])
298 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [LocatorAndRange('a', 10, 8, 2),
299 LocatorAndRange('b', 15, 0, 15),
300 LocatorAndRange('c', 5, 0, 3)])
302 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [LocatorAndRange('c', 5, 1, 2)])
304 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [LocatorAndRange('a', 10, 9, 1),
305 LocatorAndRange('b', 15, 0, 14)])
306 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [LocatorAndRange('b', 15, 0, 15)])
307 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [LocatorAndRange('b', 15, 1, 14),
308 LocatorAndRange('c', 5, 0, 1)])
310 class MockKeep(object):
311 def __init__(self, content, num_retries=0):
312 self.content = content
314 def get(self, locator, num_retries=0):
315 return self.content[locator]
317 def test_stream_reader(self):
319 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': b'abcdefghij',
320 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': b'klmnopqrstuvwxy',
321 'cccccccccccccccccccccccccccccccc+5': b'z0123',
323 mk = self.MockKeep(keepblocks)
325 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
327 content = b'abcdefghijklmnopqrstuvwxyz0123456789'
329 self.assertEqual(sr.readfrom(0, 30), content[0:30])
330 self.assertEqual(sr.readfrom(2, 30), content[2:30])
332 self.assertEqual(sr.readfrom(2, 8), content[2:10])
333 self.assertEqual(sr.readfrom(0, 10), content[0:10])
335 self.assertEqual(sr.readfrom(0, 5), content[0:5])
336 self.assertEqual(sr.readfrom(5, 5), content[5:10])
337 self.assertEqual(sr.readfrom(10, 5), content[10:15])
338 self.assertEqual(sr.readfrom(15, 5), content[15:20])
339 self.assertEqual(sr.readfrom(20, 5), content[20:25])
340 self.assertEqual(sr.readfrom(25, 5), content[25:30])
341 self.assertEqual(sr.readfrom(30, 5), b'')
343 def test_extract_file(self):
344 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
345 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
346 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
347 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
348 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt
351 m2 = arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True)
354 ". 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")
355 files = arvados.CollectionReader(
356 m2, self.api_client).all_streams()[0].files()
358 self.assertEqual(files['md5sum.txt'].as_manifest(),
359 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
360 self.assertEqual(files['md6sum.txt'].as_manifest(),
361 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
362 self.assertEqual(files['md7sum.txt'].as_manifest(),
363 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
364 self.assertEqual(files['md9sum.txt'].as_manifest(),
365 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
367 def test_write_directory_tree(self):
368 cwriter = arvados.CollectionWriter(self.api_client)
369 cwriter.write_directory_tree(self.build_directory_tree(
370 ['basefile', 'subdir/subfile']))
371 self.assertEqual(cwriter.manifest_text(),
372 """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
373 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
375 def test_write_named_directory_tree(self):
376 cwriter = arvados.CollectionWriter(self.api_client)
377 cwriter.write_directory_tree(self.build_directory_tree(
378 ['basefile', 'subdir/subfile']), 'root')
380 cwriter.manifest_text(),
381 """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
382 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
384 def test_write_directory_tree_in_one_stream(self):
385 cwriter = arvados.CollectionWriter(self.api_client)
386 cwriter.write_directory_tree(self.build_directory_tree(
387 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
388 self.assertEqual(cwriter.manifest_text(),
389 """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile 8:14:subdir/subfile\n""")
391 def test_write_directory_tree_with_limited_recursion(self):
392 cwriter = arvados.CollectionWriter(self.api_client)
393 cwriter.write_directory_tree(
394 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
395 max_manifest_depth=1)
396 self.assertEqual(cwriter.manifest_text(),
397 """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
398 ./d1 50170217e5b04312024aa5cd42934494+13 0:8:d2/f3 8:5:f2\n""")
400 def test_write_directory_tree_with_zero_recursion(self):
401 cwriter = arvados.CollectionWriter(self.api_client)
402 content = 'd1/d2/f3d1/f2f1'
403 blockhash = tutil.str_keep_locator(content)
404 cwriter.write_directory_tree(
405 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
406 max_manifest_depth=0)
408 cwriter.manifest_text(),
409 ". {} 0:8:d1/d2/f3 8:5:d1/f2 13:2:f1\n".format(blockhash))
411 def test_write_one_file(self):
412 cwriter = arvados.CollectionWriter(self.api_client)
413 with self.make_test_file() as testfile:
414 cwriter.write_file(testfile.name)
416 cwriter.manifest_text(),
417 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
418 os.path.basename(testfile.name)))
420 def test_write_named_file(self):
421 cwriter = arvados.CollectionWriter(self.api_client)
422 with self.make_test_file() as testfile:
423 cwriter.write_file(testfile.name, 'foo')
424 self.assertEqual(cwriter.manifest_text(),
425 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
427 def test_write_multiple_files(self):
428 cwriter = arvados.CollectionWriter(self.api_client)
430 with self.make_test_file(letter.encode()) as testfile:
431 cwriter.write_file(testfile.name, letter)
433 cwriter.manifest_text(),
434 ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
436 def test_basic_resume(self):
437 cwriter = TestResumableWriter()
438 with self.make_test_file() as testfile:
439 cwriter.write_file(testfile.name, 'test')
440 resumed = TestResumableWriter.from_state(cwriter.current_state())
441 self.assertEqual(cwriter.manifest_text(), resumed.manifest_text(),
442 "resumed CollectionWriter had different manifest")
444 def test_resume_fails_when_missing_dependency(self):
445 cwriter = TestResumableWriter()
446 with self.make_test_file() as testfile:
447 cwriter.write_file(testfile.name, 'test')
448 self.assertRaises(arvados.errors.StaleWriterStateError,
449 TestResumableWriter.from_state,
450 cwriter.current_state())
452 def test_resume_fails_when_dependency_mtime_changed(self):
453 cwriter = TestResumableWriter()
454 with self.make_test_file() as testfile:
455 cwriter.write_file(testfile.name, 'test')
456 os.utime(testfile.name, (0, 0))
457 self.assertRaises(arvados.errors.StaleWriterStateError,
458 TestResumableWriter.from_state,
459 cwriter.current_state())
461 def test_resume_fails_when_dependency_is_nonfile(self):
462 cwriter = TestResumableWriter()
463 cwriter.write_file('/dev/null', 'empty')
464 self.assertRaises(arvados.errors.StaleWriterStateError,
465 TestResumableWriter.from_state,
466 cwriter.current_state())
468 def test_resume_fails_when_dependency_size_changed(self):
469 cwriter = TestResumableWriter()
470 with self.make_test_file() as testfile:
471 cwriter.write_file(testfile.name, 'test')
472 orig_mtime = os.fstat(testfile.fileno()).st_mtime
473 testfile.write(b'extra')
475 os.utime(testfile.name, (orig_mtime, orig_mtime))
476 self.assertRaises(arvados.errors.StaleWriterStateError,
477 TestResumableWriter.from_state,
478 cwriter.current_state())
480 def test_resume_fails_with_expired_locator(self):
481 cwriter = TestResumableWriter()
482 state = cwriter.current_state()
483 # Add an expired locator to the state.
484 state['_current_stream_locators'].append(''.join([
485 'a' * 32, '+1+A', 'b' * 40, '@', '10000000']))
486 self.assertRaises(arvados.errors.StaleWriterStateError,
487 TestResumableWriter.from_state, state)
489 def test_arbitrary_objects_not_resumable(self):
490 cwriter = TestResumableWriter()
491 with open('/dev/null') as badfile:
492 self.assertRaises(arvados.errors.AssertionError,
493 cwriter.write_file, badfile)
495 def test_arbitrary_writes_not_resumable(self):
496 cwriter = TestResumableWriter()
497 self.assertRaises(arvados.errors.AssertionError,
498 cwriter.write, "badtext")
500 def test_read_arbitrary_data_with_collection_reader(self):
501 # arv-get relies on this to do "arv-get {keep-locator} -".
502 self.write_foo_bar_baz()
505 arvados.CollectionReader(
506 '3858f62230ac3c915f300c664312c63f+6'
510 class CollectionTestMixin(tutil.ApiClientMock):
511 API_COLLECTIONS = run_test_server.fixture('collections')
512 DEFAULT_COLLECTION = API_COLLECTIONS['foo_file']
513 DEFAULT_DATA_HASH = DEFAULT_COLLECTION['portable_data_hash']
514 DEFAULT_MANIFEST = DEFAULT_COLLECTION['manifest_text']
515 DEFAULT_UUID = DEFAULT_COLLECTION['uuid']
516 ALT_COLLECTION = API_COLLECTIONS['bar_file']
517 ALT_DATA_HASH = ALT_COLLECTION['portable_data_hash']
518 ALT_MANIFEST = ALT_COLLECTION['manifest_text']
520 def api_client_mock(self, status=200):
521 client = super(CollectionTestMixin, self).api_client_mock()
522 self.mock_keep_services(client, status=status, service_type='proxy', count=1)
527 class CollectionReaderTestCase(unittest.TestCase, CollectionTestMixin):
528 def mock_get_collection(self, api_mock, code, fixturename):
529 body = self.API_COLLECTIONS.get(fixturename)
530 self._mock_api_call(api_mock.collections().get, code, body)
532 def api_client_mock(self, status=200):
533 client = super(CollectionReaderTestCase, self).api_client_mock()
534 self.mock_get_collection(client, status, 'foo_file')
537 def test_init_no_default_retries(self):
538 client = self.api_client_mock(200)
539 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
540 reader.manifest_text()
541 client.collections().get().execute.assert_called_with(num_retries=0)
543 def test_uuid_init_success(self):
544 client = self.api_client_mock(200)
545 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
547 self.assertEqual(self.DEFAULT_COLLECTION['manifest_text'],
548 reader.manifest_text())
549 client.collections().get().execute.assert_called_with(num_retries=3)
551 def test_uuid_init_failure_raises_api_error(self):
552 client = self.api_client_mock(500)
553 with self.assertRaises(arvados.errors.ApiError):
554 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
556 def test_locator_init(self):
557 client = self.api_client_mock(200)
558 # Ensure Keep will not return anything if asked.
559 with tutil.mock_keep_responses(None, 404):
560 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
562 self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
564 def test_locator_init_fallback_to_keep(self):
565 # crunch-job needs this to read manifests that have only ever
566 # been written to Keep.
567 client = self.api_client_mock(200)
568 self.mock_get_collection(client, 404, None)
569 with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
570 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
572 self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
574 def test_uuid_init_no_fallback_to_keep(self):
575 # Do not look up a collection UUID in Keep.
576 client = self.api_client_mock(404)
577 with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
578 with self.assertRaises(arvados.errors.ApiError):
579 reader = arvados.CollectionReader(self.DEFAULT_UUID,
582 def test_try_keep_first_if_permission_hint(self):
583 # To verify that CollectionReader tries Keep first here, we
584 # mock API server to return the wrong data.
585 client = self.api_client_mock(200)
586 with tutil.mock_keep_responses(self.ALT_MANIFEST, 200):
589 arvados.CollectionReader(
590 self.ALT_DATA_HASH + '+Affffffffffffffffffffffffffffffffffffffff@fedcba98',
591 api_client=client).manifest_text())
593 def test_init_num_retries_propagated(self):
594 # More of an integration test...
595 client = self.api_client_mock(200)
596 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
598 with tutil.mock_keep_responses('foo', 500, 500, 200):
599 self.assertEqual(b'foo',
600 b''.join(f.read(9) for f in reader.all_files()))
602 def test_read_nonnormalized_manifest_with_collection_reader(self):
603 # client should be able to use CollectionReader on a manifest without normalizing it
604 client = self.api_client_mock(500)
605 nonnormal = ". acbd18db4cc2f85cedef654fccc4a4d8+3+Aabadbadbee@abeebdee 0:3:foo.txt 1:0:bar.txt 0:3:foo.txt\n"
606 reader = arvados.CollectionReader(
608 api_client=client, num_retries=0)
609 # Ensure stripped_manifest() doesn't mangle our manifest in
610 # any way other than stripping hints.
612 re.sub('\+[^\d\s\+]+', '', nonnormal),
613 reader.stripped_manifest())
614 # Ensure stripped_manifest() didn't mutate our reader.
615 self.assertEqual(nonnormal, reader.manifest_text())
616 # Ensure the files appear in the order given in the manifest.
618 [[6, '.', 'foo.txt'],
619 [0, '.', 'bar.txt']],
620 [[f.size(), f.stream_name(), f.name()]
621 for f in reader.all_streams()[0].all_files()])
623 def test_read_empty_collection(self):
624 client = self.api_client_mock(200)
625 self.mock_get_collection(client, 200, 'empty')
626 reader = arvados.CollectionReader('d41d8cd98f00b204e9800998ecf8427e+0',
628 self.assertEqual('', reader.manifest_text())
630 def test_api_response(self):
631 client = self.api_client_mock()
632 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
633 self.assertEqual(self.DEFAULT_COLLECTION, reader.api_response())
635 def test_api_response_with_collection_from_keep(self):
636 client = self.api_client_mock()
637 self.mock_get_collection(client, 404, 'foo')
638 with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
639 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
641 api_response = reader.api_response()
642 self.assertIsNone(api_response)
644 def check_open_file(self, coll_file, stream_name, file_name, file_size):
645 self.assertFalse(coll_file.closed, "returned file is not open")
646 self.assertEqual(stream_name, coll_file.stream_name())
647 self.assertEqual(file_name, coll_file.name)
648 self.assertEqual(file_size, coll_file.size())
650 def test_open_collection_file_one_argument(self):
651 client = self.api_client_mock(200)
652 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
653 cfile = reader.open('./foo', 'rb')
654 self.check_open_file(cfile, '.', 'foo', 3)
656 def test_open_deep_file(self):
657 coll_name = 'collection_with_files_in_subdir'
658 client = self.api_client_mock(200)
659 self.mock_get_collection(client, 200, coll_name)
660 reader = arvados.CollectionReader(
661 self.API_COLLECTIONS[coll_name]['uuid'], api_client=client)
662 cfile = reader.open('./subdir2/subdir3/file2_in_subdir3.txt', 'rb')
663 self.check_open_file(cfile, './subdir2/subdir3', 'file2_in_subdir3.txt',
666 def test_open_nonexistent_stream(self):
667 client = self.api_client_mock(200)
668 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
669 self.assertRaises(IOError, reader.open, './nonexistent/foo')
671 def test_open_nonexistent_file(self):
672 client = self.api_client_mock(200)
673 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
674 self.assertRaises(IOError, reader.open, 'nonexistent')
678 class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
679 def mock_keep(self, body, *codes, **headers):
680 headers.setdefault('x-keep-replicas-stored', 2)
681 return tutil.mock_keep_responses(body, *codes, **headers)
683 def foo_writer(self, **kwargs):
684 kwargs.setdefault('api_client', self.api_client_mock())
685 writer = arvados.CollectionWriter(**kwargs)
686 writer.start_new_file('foo')
690 def test_write_whole_collection(self):
691 writer = self.foo_writer()
692 with self.mock_keep(self.DEFAULT_DATA_HASH, 200, 200):
693 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
695 def test_write_no_default(self):
696 writer = self.foo_writer()
697 with self.mock_keep(None, 500):
698 with self.assertRaises(arvados.errors.KeepWriteError):
701 def test_write_insufficient_replicas_via_proxy(self):
702 writer = self.foo_writer(replication=3)
703 with self.mock_keep(None, 200, **{'x-keep-replicas-stored': 2}):
704 with self.assertRaises(arvados.errors.KeepWriteError):
705 writer.manifest_text()
707 def test_write_insufficient_replicas_via_disks(self):
708 client = mock.MagicMock(name='api_client')
711 **{'x-keep-replicas-stored': 1}) as keepmock:
712 self.mock_keep_services(client, status=200, service_type='disk', count=2)
713 writer = self.foo_writer(api_client=client, replication=3)
714 with self.assertRaises(arvados.errors.KeepWriteError):
715 writer.manifest_text()
717 def test_write_three_replicas(self):
718 client = mock.MagicMock(name='api_client')
720 "", 500, 500, 500, 200, 200, 200,
721 **{'x-keep-replicas-stored': 1}) as keepmock:
722 self.mock_keep_services(client, status=200, service_type='disk', count=6)
723 writer = self.foo_writer(api_client=client, replication=3)
724 writer.manifest_text()
725 self.assertEqual(6, keepmock.call_count)
727 def test_write_whole_collection_through_retries(self):
728 writer = self.foo_writer(num_retries=2)
729 with self.mock_keep(self.DEFAULT_DATA_HASH,
730 500, 500, 200, 500, 500, 200):
731 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
733 def test_flush_data_retries(self):
734 writer = self.foo_writer(num_retries=2)
735 foo_hash = self.DEFAULT_MANIFEST.split()[1]
736 with self.mock_keep(foo_hash, 500, 200):
738 self.assertEqual(self.DEFAULT_MANIFEST, writer.manifest_text())
740 def test_one_open(self):
741 client = self.api_client_mock()
742 writer = arvados.CollectionWriter(client)
743 with writer.open('out') as out_file:
744 self.assertEqual('.', writer.current_stream_name())
745 self.assertEqual('out', writer.current_file_name())
746 out_file.write(b'test data')
747 data_loc = tutil.str_keep_locator('test data')
748 self.assertTrue(out_file.closed, "writer file not closed after context")
749 self.assertRaises(ValueError, out_file.write, 'extra text')
750 with self.mock_keep(data_loc, 200) as keep_mock:
751 self.assertEqual(". {} 0:9:out\n".format(data_loc),
752 writer.manifest_text())
754 def test_open_writelines(self):
755 client = self.api_client_mock()
756 writer = arvados.CollectionWriter(client)
757 with writer.open('six') as out_file:
758 out_file.writelines(['12', '34', '56'])
759 data_loc = tutil.str_keep_locator('123456')
760 with self.mock_keep(data_loc, 200) as keep_mock:
761 self.assertEqual(". {} 0:6:six\n".format(data_loc),
762 writer.manifest_text())
764 def test_open_flush(self):
765 client = self.api_client_mock()
766 data_loc1 = tutil.str_keep_locator('flush1')
767 data_loc2 = tutil.str_keep_locator('flush2')
768 with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
769 writer = arvados.CollectionWriter(client)
770 with writer.open('flush_test') as out_file:
771 out_file.write(b'flush1')
773 out_file.write(b'flush2')
774 self.assertEqual(". {} {} 0:12:flush_test\n".format(data_loc1,
776 writer.manifest_text())
778 def test_two_opens_same_stream(self):
779 client = self.api_client_mock()
780 writer = arvados.CollectionWriter(client)
781 with writer.open('.', '1') as out_file:
782 out_file.write(b'1st')
783 with writer.open('.', '2') as out_file:
784 out_file.write(b'2nd')
785 data_loc = tutil.str_keep_locator('1st2nd')
786 with self.mock_keep(data_loc, 200) as keep_mock:
787 self.assertEqual(". {} 0:3:1 3:3:2\n".format(data_loc),
788 writer.manifest_text())
790 def test_two_opens_two_streams(self):
791 client = self.api_client_mock()
792 data_loc1 = tutil.str_keep_locator('file')
793 data_loc2 = tutil.str_keep_locator('indir')
794 with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
795 writer = arvados.CollectionWriter(client)
796 with writer.open('file') as out_file:
797 out_file.write(b'file')
798 with writer.open('./dir', 'indir') as out_file:
799 out_file.write(b'indir')
800 expected = ". {} 0:4:file\n./dir {} 0:5:indir\n".format(
801 data_loc1, data_loc2)
802 self.assertEqual(expected, writer.manifest_text())
804 def test_dup_open_fails(self):
805 client = self.api_client_mock()
806 writer = arvados.CollectionWriter(client)
807 file1 = writer.open('one')
808 self.assertRaises(arvados.errors.AssertionError, writer.open, 'two')
811 class CollectionMethods(run_test_server.TestCaseWithServers):
813 def test_keys_values_items_support_indexing(self):
815 with c.open('foo', 'wb') as f:
817 with c.open('bar', 'wb') as f:
819 self.assertEqual(2, len(c.keys()))
820 if sys.version_info < (3, 0):
821 # keys() supports indexing only for python2 callers.
826 self.assertEqual(2, len(c.values()))
829 self.assertEqual(2, len(c.items()))
830 self.assertEqual(fn0, c.items()[0][0])
831 self.assertEqual(fn1, c.items()[1][0])
834 class CollectionOpenModes(run_test_server.TestCaseWithServers):
836 def test_open_binary_modes(self):
838 for mode in ['wb', 'wb+', 'ab', 'ab+']:
839 with c.open('foo', mode) as f:
842 def test_open_invalid_modes(self):
844 for mode in ['+r', 'aa', '++', 'r+b', 'beer', '', None]:
845 with self.assertRaises(Exception):
848 def test_open_text_modes(self):
850 with c.open('foo', 'wb') as f:
852 for mode in ['r', 'rt', 'r+', 'rt+', 'w', 'wt', 'a', 'at']:
853 if sys.version_info >= (3, 0):
854 with self.assertRaises(NotImplementedError):
857 with c.open('foo', mode) as f:
858 if mode[0] == 'r' and '+' not in mode:
859 self.assertEqual('foo', f.read(3))
862 f.seek(-3, os.SEEK_CUR)
863 self.assertEqual('bar', f.read(3))
866 class NewCollectionTestCase(unittest.TestCase, CollectionTestMixin):
868 def test_replication_desired_kept_on_load(self):
869 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
870 c1 = Collection(m, replication_desired=1)
872 loc = c1.manifest_locator()
874 self.assertEqual(c1.manifest_text, c2.manifest_text)
875 self.assertEqual(c1.replication_desired, c2.replication_desired)
877 def test_replication_desired_not_loaded_if_provided(self):
878 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
879 c1 = Collection(m, replication_desired=1)
881 loc = c1.manifest_locator()
882 c2 = Collection(loc, replication_desired=2)
883 self.assertEqual(c1.manifest_text, c2.manifest_text)
884 self.assertNotEqual(c1.replication_desired, c2.replication_desired)
886 def test_init_manifest(self):
887 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
888 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
889 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
891 self.assertEqual(m1, CollectionReader(m1).manifest_text(normalize=False))
892 self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", CollectionReader(m1).manifest_text(normalize=True))
894 def test_init_manifest_with_collision(self):
895 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
896 ./md5sum.txt 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
898 with self.assertRaises(arvados.errors.ArgumentError):
899 self.assertEqual(m1, CollectionReader(m1))
901 def test_init_manifest_with_error(self):
902 m1 = """. 0:43:md5sum.txt"""
903 with self.assertRaises(arvados.errors.ArgumentError):
904 self.assertEqual(m1, CollectionReader(m1))
906 def test_remove(self):
907 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
908 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
909 self.assertIn("count1.txt", c)
910 c.remove("count1.txt")
911 self.assertNotIn("count1.txt", c)
912 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
913 with self.assertRaises(arvados.errors.ArgumentError):
917 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
918 self.assertIs(c.find("."), c)
919 self.assertIs(c.find("./count1.txt"), c["count1.txt"])
920 self.assertIs(c.find("count1.txt"), c["count1.txt"])
921 with self.assertRaises(IOError):
923 with self.assertRaises(arvados.errors.ArgumentError):
925 self.assertIs(c.find("./nonexistant.txt"), None)
926 self.assertIs(c.find("./nonexistantsubdir/nonexistant.txt"), None)
928 def test_remove_in_subdir(self):
929 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
930 c.remove("foo/count2.txt")
931 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
933 def test_remove_empty_subdir(self):
934 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
935 c.remove("foo/count2.txt")
937 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
939 def test_remove_nonempty_subdir(self):
940 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
941 with self.assertRaises(IOError):
943 c.remove("foo", recursive=True)
944 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
946 def test_copy_to_file_in_dir(self):
947 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
948 c.copy("count1.txt", "foo/count2.txt")
949 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
951 def test_copy_file(self):
952 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
953 c.copy("count1.txt", "count2.txt")
954 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
956 def test_copy_to_existing_dir(self):
957 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
958 c.copy("count1.txt", "foo")
959 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
961 def test_copy_to_new_dir(self):
962 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
963 c.copy("count1.txt", "foo/")
964 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
966 def test_rename_file(self):
967 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
968 c.rename("count1.txt", "count2.txt")
969 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
971 def test_move_file_to_dir(self):
972 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
974 c.rename("count1.txt", "foo/count2.txt")
975 self.assertEqual("./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
977 def test_move_file_to_other(self):
978 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
980 c2.rename("count1.txt", "count2.txt", source_collection=c1)
981 self.assertEqual("", c1.manifest_text())
982 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c2.manifest_text())
984 def test_clone(self):
985 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
987 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", cl.portable_manifest_text())
989 def test_diff_del_add(self):
990 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
991 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
993 self.assertEqual(sorted(d), [
994 ('add', './count1.txt', c1["count1.txt"]),
995 ('del', './count2.txt', c2["count2.txt"]),
998 self.assertEqual(sorted(d), [
999 ('add', './count2.txt', c2["count2.txt"]),
1000 ('del', './count1.txt', c1["count1.txt"]),
1002 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1004 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1006 def test_diff_same(self):
1007 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1008 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1010 self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1012 self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1014 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1016 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1018 def test_diff_mod(self):
1019 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1020 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
1022 self.assertEqual(d, [('mod', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1024 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1026 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1028 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1030 def test_diff_add(self):
1031 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1032 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt 10:20:count2.txt\n')
1034 self.assertEqual(sorted(d), [
1035 ('del', './count2.txt', c2["count2.txt"]),
1036 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1039 self.assertEqual(sorted(d), [
1040 ('add', './count2.txt', c2["count2.txt"]),
1041 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1044 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1046 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1048 def test_diff_add_in_subcollection(self):
1049 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1050 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1052 self.assertEqual(sorted(d), [
1053 ('del', './foo', c2["foo"]),
1054 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1057 self.assertEqual(sorted(d), [
1058 ('add', './foo', c2["foo"]),
1059 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1061 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1063 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1065 def test_diff_del_add_in_subcollection(self):
1066 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1067 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:3:count3.txt\n')
1069 self.assertEqual(sorted(d), [
1070 ('add', './foo/count2.txt', c1.find("foo/count2.txt")),
1071 ('del', './foo/count3.txt', c2.find("foo/count3.txt")),
1072 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1075 self.assertEqual(sorted(d), [
1076 ('add', './foo/count3.txt', c2.find("foo/count3.txt")),
1077 ('del', './foo/count2.txt', c1.find("foo/count2.txt")),
1078 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1081 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1083 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1085 def test_diff_mod_in_subcollection(self):
1086 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1087 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:3:foo\n')
1089 self.assertEqual(sorted(d), [
1090 ('mod', './foo', c2["foo"], c1["foo"]),
1091 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1094 self.assertEqual(sorted(d), [
1095 ('mod', './foo', c1["foo"], c2["foo"]),
1096 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1099 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1101 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1103 def test_conflict_keep_local_change(self):
1104 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1105 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1107 self.assertEqual(sorted(d), [
1108 ('add', './count2.txt', c2["count2.txt"]),
1109 ('del', './count1.txt', c1["count1.txt"]),
1111 f = c1.open("count1.txt", "wb")
1114 # c1 changed, so it should not be deleted.
1116 self.assertEqual(c1.portable_manifest_text(), ". 95ebc3c7b3b9f1d2c40fec14415d3cb8+5 5348b82a029fd9e971a811ce1f71360b+43 0:5:count1.txt 5:10:count2.txt\n")
1118 def test_conflict_mod(self):
1119 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1120 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1122 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1123 f = c1.open("count1.txt", "wb")
1126 # c1 changed, so c2 mod will go to a conflict file
1129 c1.portable_manifest_text(),
1130 r"\. 95ebc3c7b3b9f1d2c40fec14415d3cb8\+5 5348b82a029fd9e971a811ce1f71360b\+43 0:5:count1\.txt 5:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1132 def test_conflict_add(self):
1133 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1134 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
1136 self.assertEqual(sorted(d), [
1137 ('add', './count1.txt', c2["count1.txt"]),
1138 ('del', './count2.txt', c1["count2.txt"]),
1140 f = c1.open("count1.txt", "wb")
1143 # c1 added count1.txt, so c2 add will go to a conflict file
1146 c1.portable_manifest_text(),
1147 r"\. 95ebc3c7b3b9f1d2c40fec14415d3cb8\+5 5348b82a029fd9e971a811ce1f71360b\+43 0:5:count1\.txt 5:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1149 def test_conflict_del(self):
1150 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1151 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1153 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1154 c1.remove("count1.txt")
1156 # c1 deleted, so c2 mod will go to a conflict file
1159 c1.portable_manifest_text(),
1160 r"\. 5348b82a029fd9e971a811ce1f71360b\+43 0:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1162 def test_notify(self):
1165 c1.subscribe(lambda event, collection, name, item: events.append((event, collection, name, item)))
1166 f = c1.open("foo.txt", "wb")
1167 self.assertEqual(events[0], (arvados.collection.ADD, c1, "foo.txt", f.arvadosfile))
1169 def test_open_w(self):
1170 c1 = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n")
1171 self.assertEqual(c1["count1.txt"].size(), 10)
1172 c1.open("count1.txt", "wb").close()
1173 self.assertEqual(c1["count1.txt"].size(), 0)
1176 class NewCollectionTestCaseWithServers(run_test_server.TestCaseWithServers):
1177 def test_get_manifest_text_only_committed(self):
1179 with c.open("count.txt", "wb") as f:
1180 # One file committed
1181 with c.open("foo.txt", "wb") as foo:
1183 foo.flush() # Force block commit
1184 f.write(b"0123456789")
1185 # Other file not committed. Block not written to keep yet.
1187 c._get_manifest_text(".",
1190 only_committed=True),
1191 '. acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:count.txt 0:3:foo.txt\n')
1192 # And now with the file closed...
1193 f.flush() # Force block commit
1195 c._get_manifest_text(".",
1198 only_committed=True),
1199 ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:10:count.txt 10:3:foo.txt\n")
1201 def test_only_small_blocks_are_packed_together(self):
1203 # Write a couple of small files,
1204 f = c.open("count.txt", "wb")
1205 f.write(b"0123456789")
1206 f.close(flush=False)
1207 foo = c.open("foo.txt", "wb")
1209 foo.close(flush=False)
1210 # Then, write a big file, it shouldn't be packed with the ones above
1211 big = c.open("bigfile.txt", "wb")
1212 big.write(b"x" * 1024 * 1024 * 33) # 33 MB > KEEP_BLOCK_SIZE/2
1213 big.close(flush=False)
1215 c.manifest_text("."),
1216 '. 2d303c138c118af809f39319e5d507e9+34603008 a8430a058b8fbf408e1931b794dbd6fb+13 0:34603008:bigfile.txt 34603008:10:count.txt 34603018:3:foo.txt\n')
1218 def test_flush_after_small_block_packing(self):
1220 # Write a couple of small files,
1221 f = c.open("count.txt", "wb")
1222 f.write(b"0123456789")
1223 f.close(flush=False)
1224 foo = c.open("foo.txt", "wb")
1226 foo.close(flush=False)
1230 '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1232 f = c.open("count.txt", "rb+")
1237 '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1239 def test_write_after_small_block_packing2(self):
1241 # Write a couple of small files,
1242 f = c.open("count.txt", "wb")
1243 f.write(b"0123456789")
1244 f.close(flush=False)
1245 foo = c.open("foo.txt", "wb")
1247 foo.close(flush=False)
1251 '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1253 f = c.open("count.txt", "rb+")
1255 f.close(flush=False)
1259 '. 900150983cd24fb0d6963f7d28e17f72+3 a8430a058b8fbf408e1931b794dbd6fb+13 0:3:count.txt 6:7:count.txt 13:3:foo.txt\n')
1262 def test_small_block_packing_with_overwrite(self):
1264 c.open("b1", "wb").close()
1265 c["b1"].writeto(0, b"b1", 0)
1267 c.open("b2", "wb").close()
1268 c["b2"].writeto(0, b"b2", 0)
1270 c["b1"].writeto(0, b"1b", 0)
1272 self.assertEquals(c.manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 0:2:b1 2:2:b2\n")
1273 self.assertEquals(c["b1"].manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 0:2:b1\n")
1274 self.assertEquals(c["b2"].manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 2:2:b2\n")
1277 class CollectionCreateUpdateTest(run_test_server.TestCaseWithServers):
1281 def create_count_txt(self):
1282 # Create an empty collection, save it to the API server, then write a
1283 # file, but don't save it.
1286 c.save_new("CollectionCreateUpdateTest", ensure_unique_name=True)
1287 self.assertEqual(c.portable_data_hash(), "d41d8cd98f00b204e9800998ecf8427e+0")
1288 self.assertEqual(c.api_response()["portable_data_hash"], "d41d8cd98f00b204e9800998ecf8427e+0" )
1290 with c.open("count.txt", "wb") as f:
1291 f.write(b"0123456789")
1293 self.assertEqual(c.portable_manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1297 def test_create_and_save(self):
1298 c = self.create_count_txt()
1302 r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1304 def test_create_and_save_new(self):
1305 c = self.create_count_txt()
1309 r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1311 def test_create_diff_apply(self):
1312 c1 = self.create_count_txt()
1315 c2 = Collection(c1.manifest_locator())
1316 with c2.open("count.txt", "wb") as f:
1321 self.assertEqual(diff[0], (arvados.collection.MOD, u'./count.txt', c1["count.txt"], c2["count.txt"]))
1324 self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1326 def test_diff_apply_with_token(self):
1327 baseline = CollectionReader(". 781e5e245d69b566979b86e28d23f2c7+10+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:10:count.txt\n")
1328 c = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1329 other = CollectionReader(". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1331 diff = baseline.diff(other)
1332 self.assertEqual(diff, [('mod', u'./count.txt', c["count.txt"], other["count.txt"])])
1336 self.assertEqual(c.manifest_text(), ". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1339 def test_create_and_update(self):
1340 c1 = self.create_count_txt()
1343 c2 = arvados.collection.Collection(c1.manifest_locator())
1344 with c2.open("count.txt", "wb") as f:
1349 self.assertNotEqual(c1.portable_data_hash(), c2.portable_data_hash())
1351 self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1354 def test_create_and_update_with_conflict(self):
1355 c1 = self.create_count_txt()
1358 with c1.open("count.txt", "wb") as f:
1361 c2 = arvados.collection.Collection(c1.manifest_locator())
1362 with c2.open("count.txt", "wb") as f:
1370 r"\. e65075d550f9b5bf9992fa1d71a131be\+3\S* 7ac66c0f148de9519b8bd264312c4d64\+7\S* 0:3:count\.txt 3:7:count\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1372 def test_pdh_is_native_str(self):
1373 c1 = self.create_count_txt()
1374 pdh = c1.portable_data_hash()
1375 self.assertEqual(type(''), type(pdh))
1378 if __name__ == '__main__':