1 from __future__ import absolute_import
4 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
6 from builtins import object
17 from . import run_test_server
18 from arvados._ranges import Range, LocatorAndRange
19 from arvados.collection import Collection, CollectionReader
20 from . import arvados_testutil as tutil
22 class TestResumableWriter(arvados.ResumableCollectionWriter):
23 KEEP_BLOCK_SIZE = 1024 # PUT to Keep every 1K.
25 def current_state(self):
26 return self.dump_state(copy.deepcopy)
29 class ArvadosCollectionsTest(run_test_server.TestCaseWithServers,
30 tutil.ArvadosBaseTestCase):
35 super(ArvadosCollectionsTest, cls).setUpClass()
36 run_test_server.authorize_with('active')
37 cls.api_client = arvados.api('v1')
38 cls.keep_client = arvados.KeepClient(api_client=cls.api_client,
39 local_store=cls.local_store)
41 def write_foo_bar_baz(self):
42 cw = arvados.CollectionWriter(self.api_client)
43 self.assertEqual(cw.current_stream_name(), '.',
44 'current_stream_name() should be "." now')
45 cw.set_current_file_name('foo.txt')
47 self.assertEqual(cw.current_file_name(), 'foo.txt',
48 'current_file_name() should be foo.txt now')
49 cw.start_new_file('bar.txt')
51 cw.start_new_stream('baz')
53 cw.set_current_file_name('baz.txt')
54 self.assertEqual(cw.manifest_text(),
55 ". 3858f62230ac3c915f300c664312c63f+6 0:3:foo.txt 3:3:bar.txt\n" +
56 "./baz 73feffa4b7f6bb68e44cf984c85f6e88+3 0:3:baz.txt\n",
57 "wrong manifest: got {}".format(cw.manifest_text()))
59 return cw.portable_data_hash()
61 def test_keep_local_store(self):
62 self.assertEqual(self.keep_client.put(b'foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
63 self.assertEqual(self.keep_client.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), b'foo', 'wrong data from Keep.get')
65 def test_local_collection_writer(self):
66 self.assertEqual(self.write_foo_bar_baz(),
67 '23ca013983d6239e98931cc779e68426+114',
68 'wrong locator hash: ' + self.write_foo_bar_baz())
70 def test_local_collection_reader(self):
71 foobarbaz = self.write_foo_bar_baz()
72 cr = arvados.CollectionReader(
73 foobarbaz + '+Xzizzle', self.api_client)
75 for s in cr.all_streams():
76 for f in s.all_files():
77 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
78 expected = [[3, '.', 'foo.txt', b'foo'],
79 [3, '.', 'bar.txt', b'bar'],
80 [3, './baz', 'baz.txt', b'baz']]
83 stream0 = cr.all_streams()[0]
84 self.assertEqual(stream0.readfrom(0, 0),
86 'reading zero bytes should have returned empty string')
87 self.assertEqual(stream0.readfrom(0, 2**26),
89 'reading entire stream failed')
90 self.assertEqual(stream0.readfrom(2**26, 0),
92 'reading zero bytes should have returned empty string')
94 def _test_subset(self, collection, expected):
95 cr = arvados.CollectionReader(collection, self.api_client)
96 for s in cr.all_streams():
100 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
101 self.assertEqual(got,
103 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
105 def test_collection_manifest_subset(self):
106 foobarbaz = self.write_foo_bar_baz()
107 self._test_subset(foobarbaz,
108 [[3, '.', 'bar.txt', b'bar'],
109 [3, '.', 'foo.txt', b'foo'],
110 [3, './baz', 'baz.txt', b'baz']])
111 self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
112 (self.keep_client.put(b"foo"),
113 self.keep_client.put(b"bar"))),
114 [[3, '.', 'bar.txt', b'bar'],
115 [3, '.', 'foo.txt', b'foo']])
116 self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
117 (self.keep_client.put(b"foo"),
118 self.keep_client.put(b"bar"))),
119 [[2, '.', 'fo.txt', b'fo'],
120 [4, '.', 'obar.txt', b'obar']])
121 self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
122 (self.keep_client.put(b"foo"),
123 self.keep_client.put(b"bar"))),
124 [[2, '.', 'ar.txt', b'ar'],
125 [2, '.', 'fo.txt', b'fo'],
126 [2, '.', 'ob.txt', b'ob'],
127 [0, '.', 'zero.txt', b'']])
129 def test_collection_empty_file(self):
130 cw = arvados.CollectionWriter(self.api_client)
131 cw.start_new_file('zero.txt')
134 self.assertEqual(cw.manifest_text(), ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:zero.txt\n")
135 self.check_manifest_file_sizes(cw.manifest_text(), [0])
136 cw = arvados.CollectionWriter(self.api_client)
137 cw.start_new_file('zero.txt')
139 cw.start_new_file('one.txt')
141 cw.start_new_stream('foo')
142 cw.start_new_file('zero.txt')
144 self.check_manifest_file_sizes(cw.manifest_text(), [0,1,0])
146 def test_no_implicit_normalize(self):
147 cw = arvados.CollectionWriter(self.api_client)
148 cw.start_new_file('b')
150 cw.start_new_file('a')
152 self.check_manifest_file_sizes(cw.manifest_text(), [1,0])
153 self.check_manifest_file_sizes(
154 arvados.CollectionReader(
155 cw.manifest_text()).manifest_text(normalize=True),
158 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
159 cr = arvados.CollectionReader(manifest_text, self.api_client)
161 for f in cr.all_files():
162 got_sizes += [f.size()]
163 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
165 def test_normalized_collection(self):
166 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
167 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
168 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
170 self.assertEqual(arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True),
171 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
174 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
176 self.assertEqual(arvados.CollectionReader(m2, self.api_client).manifest_text(normalize=True), m2)
178 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
179 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
180 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
182 self.assertEqual(arvados.CollectionReader(m3, self.api_client).manifest_text(normalize=True),
183 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
186 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
187 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
188 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar
190 self.assertEqual(arvados.CollectionReader(m4, self.api_client).manifest_text(normalize=True),
191 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
192 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
195 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
196 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
197 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar
199 self.assertEqual(arvados.CollectionReader(m5, self.api_client).manifest_text(normalize=True),
200 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
201 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
204 with self.data_file('1000G_ref_manifest') as f6:
206 self.assertEqual(arvados.CollectionReader(m6, self.api_client).manifest_text(normalize=True), m6)
208 with self.data_file('jlake_manifest') as f7:
210 self.assertEqual(arvados.CollectionReader(m7, self.api_client).manifest_text(normalize=True), m7)
212 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
214 self.assertEqual(arvados.CollectionReader(m8, self.api_client).manifest_text(normalize=True), m8)
216 def test_locators_and_ranges(self):
217 blocks2 = [Range('a', 0, 10),
224 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
225 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
226 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
227 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
228 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
229 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
230 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
231 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
233 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [LocatorAndRange('a', 10, 0, 2)])
234 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [LocatorAndRange('b', 10, 0, 2)])
235 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [LocatorAndRange('c', 10, 0, 2)])
236 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [LocatorAndRange('d', 10, 0, 2)])
237 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [LocatorAndRange('e', 10, 0, 2)])
238 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [LocatorAndRange('f', 10, 0, 2)])
239 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
240 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
242 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [LocatorAndRange('a', 10, 9, 1), LocatorAndRange('b', 10, 0, 1)])
243 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [LocatorAndRange('b', 10, 9, 1), LocatorAndRange('c', 10, 0, 1)])
244 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [LocatorAndRange('c', 10, 9, 1), LocatorAndRange('d', 10, 0, 1)])
245 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [LocatorAndRange('d', 10, 9, 1), LocatorAndRange('e', 10, 0, 1)])
246 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [LocatorAndRange('e', 10, 9, 1), LocatorAndRange('f', 10, 0, 1)])
247 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [LocatorAndRange('f', 10, 9, 1)])
250 blocks3 = [Range('a', 0, 10),
258 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
259 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
260 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
261 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
262 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
263 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
264 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [LocatorAndRange('g', 10, 2, 2)])
267 blocks = [Range('a', 0, 10),
270 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
271 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [LocatorAndRange('a', 10, 0, 5)])
272 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [LocatorAndRange('a', 10, 3, 5)])
273 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [LocatorAndRange('a', 10, 0, 10)])
275 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [LocatorAndRange('a', 10, 0, 10),
276 LocatorAndRange('b', 15, 0, 1)])
277 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [LocatorAndRange('a', 10, 1, 9),
278 LocatorAndRange('b', 15, 0, 2)])
279 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [LocatorAndRange('a', 10, 0, 10),
280 LocatorAndRange('b', 15, 0, 15)])
282 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [LocatorAndRange('a', 10, 0, 10),
283 LocatorAndRange('b', 15, 0, 15),
284 LocatorAndRange('c', 5, 0, 5)])
285 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [LocatorAndRange('a', 10, 1, 9),
286 LocatorAndRange('b', 15, 0, 15),
287 LocatorAndRange('c', 5, 0, 5)])
288 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [LocatorAndRange('a', 10, 0, 10),
289 LocatorAndRange('b', 15, 0, 15),
290 LocatorAndRange('c', 5, 0, 5)])
292 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [LocatorAndRange('b', 15, 5, 5)])
294 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [LocatorAndRange('a', 10, 8, 2),
295 LocatorAndRange('b', 15, 0, 15)])
297 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [LocatorAndRange('a', 10, 8, 2),
298 LocatorAndRange('b', 15, 0, 15),
299 LocatorAndRange('c', 5, 0, 3)])
301 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [LocatorAndRange('c', 5, 1, 2)])
303 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [LocatorAndRange('a', 10, 9, 1),
304 LocatorAndRange('b', 15, 0, 14)])
305 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [LocatorAndRange('b', 15, 0, 15)])
306 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [LocatorAndRange('b', 15, 1, 14),
307 LocatorAndRange('c', 5, 0, 1)])
309 class MockKeep(object):
310 def __init__(self, content, num_retries=0):
311 self.content = content
313 def get(self, locator, num_retries=0):
314 return self.content[locator]
316 def test_stream_reader(self):
318 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': b'abcdefghij',
319 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': b'klmnopqrstuvwxy',
320 'cccccccccccccccccccccccccccccccc+5': b'z0123',
322 mk = self.MockKeep(keepblocks)
324 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
326 content = b'abcdefghijklmnopqrstuvwxyz0123456789'
328 self.assertEqual(sr.readfrom(0, 30), content[0:30])
329 self.assertEqual(sr.readfrom(2, 30), content[2:30])
331 self.assertEqual(sr.readfrom(2, 8), content[2:10])
332 self.assertEqual(sr.readfrom(0, 10), content[0:10])
334 self.assertEqual(sr.readfrom(0, 5), content[0:5])
335 self.assertEqual(sr.readfrom(5, 5), content[5:10])
336 self.assertEqual(sr.readfrom(10, 5), content[10:15])
337 self.assertEqual(sr.readfrom(15, 5), content[15:20])
338 self.assertEqual(sr.readfrom(20, 5), content[20:25])
339 self.assertEqual(sr.readfrom(25, 5), content[25:30])
340 self.assertEqual(sr.readfrom(30, 5), b'')
342 def test_extract_file(self):
343 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
344 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
345 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
346 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
347 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt
350 m2 = arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True)
353 ". 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")
354 files = arvados.CollectionReader(
355 m2, self.api_client).all_streams()[0].files()
357 self.assertEqual(files['md5sum.txt'].as_manifest(),
358 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
359 self.assertEqual(files['md6sum.txt'].as_manifest(),
360 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
361 self.assertEqual(files['md7sum.txt'].as_manifest(),
362 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
363 self.assertEqual(files['md9sum.txt'].as_manifest(),
364 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
366 def test_write_directory_tree(self):
367 cwriter = arvados.CollectionWriter(self.api_client)
368 cwriter.write_directory_tree(self.build_directory_tree(
369 ['basefile', 'subdir/subfile']))
370 self.assertEqual(cwriter.manifest_text(),
371 """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
372 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
374 def test_write_named_directory_tree(self):
375 cwriter = arvados.CollectionWriter(self.api_client)
376 cwriter.write_directory_tree(self.build_directory_tree(
377 ['basefile', 'subdir/subfile']), 'root')
379 cwriter.manifest_text(),
380 """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
381 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
383 def test_write_directory_tree_in_one_stream(self):
384 cwriter = arvados.CollectionWriter(self.api_client)
385 cwriter.write_directory_tree(self.build_directory_tree(
386 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
387 self.assertEqual(cwriter.manifest_text(),
388 """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile 8:14:subdir/subfile\n""")
390 def test_write_directory_tree_with_limited_recursion(self):
391 cwriter = arvados.CollectionWriter(self.api_client)
392 cwriter.write_directory_tree(
393 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
394 max_manifest_depth=1)
395 self.assertEqual(cwriter.manifest_text(),
396 """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
397 ./d1 50170217e5b04312024aa5cd42934494+13 0:8:d2/f3 8:5:f2\n""")
399 def test_write_directory_tree_with_zero_recursion(self):
400 cwriter = arvados.CollectionWriter(self.api_client)
401 content = 'd1/d2/f3d1/f2f1'
402 blockhash = tutil.str_keep_locator(content)
403 cwriter.write_directory_tree(
404 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
405 max_manifest_depth=0)
407 cwriter.manifest_text(),
408 ". {} 0:8:d1/d2/f3 8:5:d1/f2 13:2:f1\n".format(blockhash))
410 def test_write_one_file(self):
411 cwriter = arvados.CollectionWriter(self.api_client)
412 with self.make_test_file() as testfile:
413 cwriter.write_file(testfile.name)
415 cwriter.manifest_text(),
416 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
417 os.path.basename(testfile.name)))
419 def test_write_named_file(self):
420 cwriter = arvados.CollectionWriter(self.api_client)
421 with self.make_test_file() as testfile:
422 cwriter.write_file(testfile.name, 'foo')
423 self.assertEqual(cwriter.manifest_text(),
424 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
426 def test_write_multiple_files(self):
427 cwriter = arvados.CollectionWriter(self.api_client)
429 with self.make_test_file(letter.encode()) as testfile:
430 cwriter.write_file(testfile.name, letter)
432 cwriter.manifest_text(),
433 ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
435 def test_basic_resume(self):
436 cwriter = TestResumableWriter()
437 with self.make_test_file() as testfile:
438 cwriter.write_file(testfile.name, 'test')
439 resumed = TestResumableWriter.from_state(cwriter.current_state())
440 self.assertEqual(cwriter.manifest_text(), resumed.manifest_text(),
441 "resumed CollectionWriter had different manifest")
443 def test_resume_fails_when_missing_dependency(self):
444 cwriter = TestResumableWriter()
445 with self.make_test_file() as testfile:
446 cwriter.write_file(testfile.name, 'test')
447 self.assertRaises(arvados.errors.StaleWriterStateError,
448 TestResumableWriter.from_state,
449 cwriter.current_state())
451 def test_resume_fails_when_dependency_mtime_changed(self):
452 cwriter = TestResumableWriter()
453 with self.make_test_file() as testfile:
454 cwriter.write_file(testfile.name, 'test')
455 os.utime(testfile.name, (0, 0))
456 self.assertRaises(arvados.errors.StaleWriterStateError,
457 TestResumableWriter.from_state,
458 cwriter.current_state())
460 def test_resume_fails_when_dependency_is_nonfile(self):
461 cwriter = TestResumableWriter()
462 cwriter.write_file('/dev/null', 'empty')
463 self.assertRaises(arvados.errors.StaleWriterStateError,
464 TestResumableWriter.from_state,
465 cwriter.current_state())
467 def test_resume_fails_when_dependency_size_changed(self):
468 cwriter = TestResumableWriter()
469 with self.make_test_file() as testfile:
470 cwriter.write_file(testfile.name, 'test')
471 orig_mtime = os.fstat(testfile.fileno()).st_mtime
472 testfile.write(b'extra')
474 os.utime(testfile.name, (orig_mtime, orig_mtime))
475 self.assertRaises(arvados.errors.StaleWriterStateError,
476 TestResumableWriter.from_state,
477 cwriter.current_state())
479 def test_resume_fails_with_expired_locator(self):
480 cwriter = TestResumableWriter()
481 state = cwriter.current_state()
482 # Add an expired locator to the state.
483 state['_current_stream_locators'].append(''.join([
484 'a' * 32, '+1+A', 'b' * 40, '@', '10000000']))
485 self.assertRaises(arvados.errors.StaleWriterStateError,
486 TestResumableWriter.from_state, state)
488 def test_arbitrary_objects_not_resumable(self):
489 cwriter = TestResumableWriter()
490 with open('/dev/null') as badfile:
491 self.assertRaises(arvados.errors.AssertionError,
492 cwriter.write_file, badfile)
494 def test_arbitrary_writes_not_resumable(self):
495 cwriter = TestResumableWriter()
496 self.assertRaises(arvados.errors.AssertionError,
497 cwriter.write, "badtext")
499 def test_read_arbitrary_data_with_collection_reader(self):
500 # arv-get relies on this to do "arv-get {keep-locator} -".
501 self.write_foo_bar_baz()
504 arvados.CollectionReader(
505 '3858f62230ac3c915f300c664312c63f+6'
509 class CollectionTestMixin(tutil.ApiClientMock):
510 API_COLLECTIONS = run_test_server.fixture('collections')
511 DEFAULT_COLLECTION = API_COLLECTIONS['foo_file']
512 DEFAULT_DATA_HASH = DEFAULT_COLLECTION['portable_data_hash']
513 DEFAULT_MANIFEST = DEFAULT_COLLECTION['manifest_text']
514 DEFAULT_UUID = DEFAULT_COLLECTION['uuid']
515 ALT_COLLECTION = API_COLLECTIONS['bar_file']
516 ALT_DATA_HASH = ALT_COLLECTION['portable_data_hash']
517 ALT_MANIFEST = ALT_COLLECTION['manifest_text']
519 def api_client_mock(self, status=200):
520 client = super(CollectionTestMixin, self).api_client_mock()
521 self.mock_keep_services(client, status=status, service_type='proxy', count=1)
526 class CollectionReaderTestCase(unittest.TestCase, CollectionTestMixin):
527 def mock_get_collection(self, api_mock, code, fixturename):
528 body = self.API_COLLECTIONS.get(fixturename)
529 self._mock_api_call(api_mock.collections().get, code, body)
531 def api_client_mock(self, status=200):
532 client = super(CollectionReaderTestCase, self).api_client_mock()
533 self.mock_get_collection(client, status, 'foo_file')
536 def test_init_no_default_retries(self):
537 client = self.api_client_mock(200)
538 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
539 reader.manifest_text()
540 client.collections().get().execute.assert_called_with(num_retries=0)
542 def test_uuid_init_success(self):
543 client = self.api_client_mock(200)
544 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
546 self.assertEqual(self.DEFAULT_COLLECTION['manifest_text'],
547 reader.manifest_text())
548 client.collections().get().execute.assert_called_with(num_retries=3)
550 def test_uuid_init_failure_raises_api_error(self):
551 client = self.api_client_mock(500)
552 with self.assertRaises(arvados.errors.ApiError):
553 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
555 def test_locator_init(self):
556 client = self.api_client_mock(200)
557 # Ensure Keep will not return anything if asked.
558 with tutil.mock_keep_responses(None, 404):
559 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
561 self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
563 def test_locator_init_fallback_to_keep(self):
564 # crunch-job needs this to read manifests that have only ever
565 # been written to Keep.
566 client = self.api_client_mock(200)
567 self.mock_get_collection(client, 404, None)
568 with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
569 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
571 self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
573 def test_uuid_init_no_fallback_to_keep(self):
574 # Do not look up a collection UUID in Keep.
575 client = self.api_client_mock(404)
576 with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
577 with self.assertRaises(arvados.errors.ApiError):
578 reader = arvados.CollectionReader(self.DEFAULT_UUID,
581 def test_try_keep_first_if_permission_hint(self):
582 # To verify that CollectionReader tries Keep first here, we
583 # mock API server to return the wrong data.
584 client = self.api_client_mock(200)
585 with tutil.mock_keep_responses(self.ALT_MANIFEST, 200):
588 arvados.CollectionReader(
589 self.ALT_DATA_HASH + '+Affffffffffffffffffffffffffffffffffffffff@fedcba98',
590 api_client=client).manifest_text())
592 def test_init_num_retries_propagated(self):
593 # More of an integration test...
594 client = self.api_client_mock(200)
595 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
597 with tutil.mock_keep_responses('foo', 500, 500, 200):
598 self.assertEqual(b'foo',
599 b''.join(f.read(9) for f in reader.all_files()))
601 def test_read_nonnormalized_manifest_with_collection_reader(self):
602 # client should be able to use CollectionReader on a manifest without normalizing it
603 client = self.api_client_mock(500)
604 nonnormal = ". acbd18db4cc2f85cedef654fccc4a4d8+3+Aabadbadbee@abeebdee 0:3:foo.txt 1:0:bar.txt 0:3:foo.txt\n"
605 reader = arvados.CollectionReader(
607 api_client=client, num_retries=0)
608 # Ensure stripped_manifest() doesn't mangle our manifest in
609 # any way other than stripping hints.
611 re.sub('\+[^\d\s\+]+', '', nonnormal),
612 reader.stripped_manifest())
613 # Ensure stripped_manifest() didn't mutate our reader.
614 self.assertEqual(nonnormal, reader.manifest_text())
615 # Ensure the files appear in the order given in the manifest.
617 [[6, '.', 'foo.txt'],
618 [0, '.', 'bar.txt']],
619 [[f.size(), f.stream_name(), f.name()]
620 for f in reader.all_streams()[0].all_files()])
622 def test_read_empty_collection(self):
623 client = self.api_client_mock(200)
624 self.mock_get_collection(client, 200, 'empty')
625 reader = arvados.CollectionReader('d41d8cd98f00b204e9800998ecf8427e+0',
627 self.assertEqual('', reader.manifest_text())
629 def test_api_response(self):
630 client = self.api_client_mock()
631 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
632 self.assertEqual(self.DEFAULT_COLLECTION, reader.api_response())
634 def test_api_response_with_collection_from_keep(self):
635 client = self.api_client_mock()
636 self.mock_get_collection(client, 404, 'foo')
637 with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
638 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
640 api_response = reader.api_response()
641 self.assertIsNone(api_response)
643 def check_open_file(self, coll_file, stream_name, file_name, file_size):
644 self.assertFalse(coll_file.closed, "returned file is not open")
645 self.assertEqual(stream_name, coll_file.stream_name())
646 self.assertEqual(file_name, coll_file.name)
647 self.assertEqual(file_size, coll_file.size())
649 def test_open_collection_file_one_argument(self):
650 client = self.api_client_mock(200)
651 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
652 cfile = reader.open('./foo', 'rb')
653 self.check_open_file(cfile, '.', 'foo', 3)
655 def test_open_deep_file(self):
656 coll_name = 'collection_with_files_in_subdir'
657 client = self.api_client_mock(200)
658 self.mock_get_collection(client, 200, coll_name)
659 reader = arvados.CollectionReader(
660 self.API_COLLECTIONS[coll_name]['uuid'], api_client=client)
661 cfile = reader.open('./subdir2/subdir3/file2_in_subdir3.txt', 'rb')
662 self.check_open_file(cfile, './subdir2/subdir3', 'file2_in_subdir3.txt',
665 def test_open_nonexistent_stream(self):
666 client = self.api_client_mock(200)
667 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
668 self.assertRaises(IOError, reader.open, './nonexistent/foo')
670 def test_open_nonexistent_file(self):
671 client = self.api_client_mock(200)
672 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
673 self.assertRaises(IOError, reader.open, 'nonexistent')
677 class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
678 def mock_keep(self, body, *codes, **headers):
679 headers.setdefault('x-keep-replicas-stored', 2)
680 return tutil.mock_keep_responses(body, *codes, **headers)
682 def foo_writer(self, **kwargs):
683 kwargs.setdefault('api_client', self.api_client_mock())
684 writer = arvados.CollectionWriter(**kwargs)
685 writer.start_new_file('foo')
689 def test_write_whole_collection(self):
690 writer = self.foo_writer()
691 with self.mock_keep(self.DEFAULT_DATA_HASH, 200, 200):
692 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
694 def test_write_no_default(self):
695 writer = self.foo_writer()
696 with self.mock_keep(None, 500):
697 with self.assertRaises(arvados.errors.KeepWriteError):
700 def test_write_insufficient_replicas_via_proxy(self):
701 writer = self.foo_writer(replication=3)
702 with self.mock_keep(None, 200, **{'x-keep-replicas-stored': 2}):
703 with self.assertRaises(arvados.errors.KeepWriteError):
704 writer.manifest_text()
706 def test_write_insufficient_replicas_via_disks(self):
707 client = mock.MagicMock(name='api_client')
710 **{'x-keep-replicas-stored': 1}) as keepmock:
711 self.mock_keep_services(client, status=200, service_type='disk', count=2)
712 writer = self.foo_writer(api_client=client, replication=3)
713 with self.assertRaises(arvados.errors.KeepWriteError):
714 writer.manifest_text()
716 def test_write_three_replicas(self):
717 client = mock.MagicMock(name='api_client')
719 "", 500, 500, 500, 200, 200, 200,
720 **{'x-keep-replicas-stored': 1}) as keepmock:
721 self.mock_keep_services(client, status=200, service_type='disk', count=6)
722 writer = self.foo_writer(api_client=client, replication=3)
723 writer.manifest_text()
724 self.assertEqual(6, keepmock.call_count)
726 def test_write_whole_collection_through_retries(self):
727 writer = self.foo_writer(num_retries=2)
728 with self.mock_keep(self.DEFAULT_DATA_HASH,
729 500, 500, 200, 500, 500, 200):
730 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
732 def test_flush_data_retries(self):
733 writer = self.foo_writer(num_retries=2)
734 foo_hash = self.DEFAULT_MANIFEST.split()[1]
735 with self.mock_keep(foo_hash, 500, 200):
737 self.assertEqual(self.DEFAULT_MANIFEST, writer.manifest_text())
739 def test_one_open(self):
740 client = self.api_client_mock()
741 writer = arvados.CollectionWriter(client)
742 with writer.open('out') as out_file:
743 self.assertEqual('.', writer.current_stream_name())
744 self.assertEqual('out', writer.current_file_name())
745 out_file.write(b'test data')
746 data_loc = tutil.str_keep_locator('test data')
747 self.assertTrue(out_file.closed, "writer file not closed after context")
748 self.assertRaises(ValueError, out_file.write, 'extra text')
749 with self.mock_keep(data_loc, 200) as keep_mock:
750 self.assertEqual(". {} 0:9:out\n".format(data_loc),
751 writer.manifest_text())
753 def test_open_writelines(self):
754 client = self.api_client_mock()
755 writer = arvados.CollectionWriter(client)
756 with writer.open('six') as out_file:
757 out_file.writelines(['12', '34', '56'])
758 data_loc = tutil.str_keep_locator('123456')
759 with self.mock_keep(data_loc, 200) as keep_mock:
760 self.assertEqual(". {} 0:6:six\n".format(data_loc),
761 writer.manifest_text())
763 def test_open_flush(self):
764 client = self.api_client_mock()
765 data_loc1 = tutil.str_keep_locator('flush1')
766 data_loc2 = tutil.str_keep_locator('flush2')
767 with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
768 writer = arvados.CollectionWriter(client)
769 with writer.open('flush_test') as out_file:
770 out_file.write(b'flush1')
772 out_file.write(b'flush2')
773 self.assertEqual(". {} {} 0:12:flush_test\n".format(data_loc1,
775 writer.manifest_text())
777 def test_two_opens_same_stream(self):
778 client = self.api_client_mock()
779 writer = arvados.CollectionWriter(client)
780 with writer.open('.', '1') as out_file:
781 out_file.write(b'1st')
782 with writer.open('.', '2') as out_file:
783 out_file.write(b'2nd')
784 data_loc = tutil.str_keep_locator('1st2nd')
785 with self.mock_keep(data_loc, 200) as keep_mock:
786 self.assertEqual(". {} 0:3:1 3:3:2\n".format(data_loc),
787 writer.manifest_text())
789 def test_two_opens_two_streams(self):
790 client = self.api_client_mock()
791 data_loc1 = tutil.str_keep_locator('file')
792 data_loc2 = tutil.str_keep_locator('indir')
793 with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
794 writer = arvados.CollectionWriter(client)
795 with writer.open('file') as out_file:
796 out_file.write(b'file')
797 with writer.open('./dir', 'indir') as out_file:
798 out_file.write(b'indir')
799 expected = ". {} 0:4:file\n./dir {} 0:5:indir\n".format(
800 data_loc1, data_loc2)
801 self.assertEqual(expected, writer.manifest_text())
803 def test_dup_open_fails(self):
804 client = self.api_client_mock()
805 writer = arvados.CollectionWriter(client)
806 file1 = writer.open('one')
807 self.assertRaises(arvados.errors.AssertionError, writer.open, 'two')
810 class CollectionOpenModes(run_test_server.TestCaseWithServers):
812 def test_open_binary_modes(self):
814 for mode in ['wb', 'wb+', 'ab', 'ab+']:
815 with c.open('foo', 'wb') as f:
818 def test_open_invalid_modes(self):
820 for mode in ['+r', 'aa', '++', 'r+b', 'beer', '', None]:
821 with self.assertRaises(Exception):
824 def test_open_text_modes(self):
826 with c.open('foo', 'wb') as f:
828 for mode in ['r', 'rt', 'r+', 'rt+', 'w', 'wt', 'a', 'at']:
829 if sys.version_info >= (3, 0):
830 with self.assertRaises(NotImplementedError):
833 with c.open('foo', mode) as f:
834 if mode[0] == 'r' and '+' not in mode:
835 self.assertEqual('foo', f.read(3))
838 f.seek(-3, os.SEEK_CUR)
839 self.assertEqual('bar', f.read(3))
842 class NewCollectionTestCase(unittest.TestCase, CollectionTestMixin):
844 def test_replication_desired_kept_on_load(self):
845 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
846 c1 = Collection(m, replication_desired=1)
848 loc = c1.manifest_locator()
850 self.assertEqual(c1.manifest_text, c2.manifest_text)
851 self.assertEqual(c1.replication_desired, c2.replication_desired)
853 def test_replication_desired_not_loaded_if_provided(self):
854 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
855 c1 = Collection(m, replication_desired=1)
857 loc = c1.manifest_locator()
858 c2 = Collection(loc, replication_desired=2)
859 self.assertEqual(c1.manifest_text, c2.manifest_text)
860 self.assertNotEqual(c1.replication_desired, c2.replication_desired)
862 def test_init_manifest(self):
863 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
864 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
865 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
867 self.assertEqual(m1, CollectionReader(m1).manifest_text(normalize=False))
868 self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", CollectionReader(m1).manifest_text(normalize=True))
870 def test_init_manifest_with_collision(self):
871 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
872 ./md5sum.txt 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
874 with self.assertRaises(arvados.errors.ArgumentError):
875 self.assertEqual(m1, CollectionReader(m1))
877 def test_init_manifest_with_error(self):
878 m1 = """. 0:43:md5sum.txt"""
879 with self.assertRaises(arvados.errors.ArgumentError):
880 self.assertEqual(m1, CollectionReader(m1))
882 def test_remove(self):
883 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
884 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
885 self.assertIn("count1.txt", c)
886 c.remove("count1.txt")
887 self.assertNotIn("count1.txt", c)
888 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
889 with self.assertRaises(arvados.errors.ArgumentError):
893 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
894 self.assertIs(c.find("."), c)
895 self.assertIs(c.find("./count1.txt"), c["count1.txt"])
896 self.assertIs(c.find("count1.txt"), c["count1.txt"])
897 with self.assertRaises(IOError):
899 with self.assertRaises(arvados.errors.ArgumentError):
901 self.assertIs(c.find("./nonexistant.txt"), None)
902 self.assertIs(c.find("./nonexistantsubdir/nonexistant.txt"), None)
904 def test_remove_in_subdir(self):
905 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
906 c.remove("foo/count2.txt")
907 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
909 def test_remove_empty_subdir(self):
910 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
911 c.remove("foo/count2.txt")
913 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
915 def test_remove_nonempty_subdir(self):
916 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
917 with self.assertRaises(IOError):
919 c.remove("foo", recursive=True)
920 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
922 def test_copy_to_file_in_dir(self):
923 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
924 c.copy("count1.txt", "foo/count2.txt")
925 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
927 def test_copy_file(self):
928 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
929 c.copy("count1.txt", "count2.txt")
930 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
932 def test_copy_to_existing_dir(self):
933 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
934 c.copy("count1.txt", "foo")
935 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())
937 def test_copy_to_new_dir(self):
938 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
939 c.copy("count1.txt", "foo/")
940 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
942 def test_rename_file(self):
943 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
944 c.rename("count1.txt", "count2.txt")
945 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
947 def test_move_file_to_dir(self):
948 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
950 c.rename("count1.txt", "foo/count2.txt")
951 self.assertEqual("./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
953 def test_move_file_to_other(self):
954 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
956 c2.rename("count1.txt", "count2.txt", source_collection=c1)
957 self.assertEqual("", c1.manifest_text())
958 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c2.manifest_text())
960 def test_clone(self):
961 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
963 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", cl.portable_manifest_text())
965 def test_diff_del_add(self):
966 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
967 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
969 self.assertEqual(sorted(d), [
970 ('add', './count1.txt', c1["count1.txt"]),
971 ('del', './count2.txt', c2["count2.txt"]),
974 self.assertEqual(sorted(d), [
975 ('add', './count2.txt', c2["count2.txt"]),
976 ('del', './count1.txt', c1["count1.txt"]),
978 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
980 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
982 def test_diff_same(self):
983 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
984 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
986 self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
988 self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
990 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
992 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
994 def test_diff_mod(self):
995 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
996 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
998 self.assertEqual(d, [('mod', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1000 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["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_add(self):
1007 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1008 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt 10:20:count2.txt\n')
1010 self.assertEqual(sorted(d), [
1011 ('del', './count2.txt', c2["count2.txt"]),
1012 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1015 self.assertEqual(sorted(d), [
1016 ('add', './count2.txt', c2["count2.txt"]),
1017 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1020 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1022 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1024 def test_diff_add_in_subcollection(self):
1025 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1026 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1028 self.assertEqual(sorted(d), [
1029 ('del', './foo', c2["foo"]),
1030 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1033 self.assertEqual(sorted(d), [
1034 ('add', './foo', c2["foo"]),
1035 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1037 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1039 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1041 def test_diff_del_add_in_subcollection(self):
1042 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1043 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:3:count3.txt\n')
1045 self.assertEqual(sorted(d), [
1046 ('add', './foo/count2.txt', c1.find("foo/count2.txt")),
1047 ('del', './foo/count3.txt', c2.find("foo/count3.txt")),
1048 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1051 self.assertEqual(sorted(d), [
1052 ('add', './foo/count3.txt', c2.find("foo/count3.txt")),
1053 ('del', './foo/count2.txt', c1.find("foo/count2.txt")),
1054 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1057 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1059 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1061 def test_diff_mod_in_subcollection(self):
1062 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1063 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:3:foo\n')
1065 self.assertEqual(sorted(d), [
1066 ('mod', './foo', c2["foo"], c1["foo"]),
1067 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1070 self.assertEqual(sorted(d), [
1071 ('mod', './foo', c1["foo"], c2["foo"]),
1072 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1075 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1077 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1079 def test_conflict_keep_local_change(self):
1080 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1081 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1083 self.assertEqual(sorted(d), [
1084 ('add', './count2.txt', c2["count2.txt"]),
1085 ('del', './count1.txt', c1["count1.txt"]),
1087 f = c1.open("count1.txt", "wb")
1090 # c1 changed, so it should not be deleted.
1092 self.assertEqual(c1.portable_manifest_text(), ". 95ebc3c7b3b9f1d2c40fec14415d3cb8+5 5348b82a029fd9e971a811ce1f71360b+43 0:5:count1.txt 5:10:count2.txt\n")
1094 def test_conflict_mod(self):
1095 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1096 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1098 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1099 f = c1.open("count1.txt", "wb")
1102 # c1 changed, so c2 mod will go to a conflict file
1105 c1.portable_manifest_text(),
1106 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~$")
1108 def test_conflict_add(self):
1109 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1110 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
1112 self.assertEqual(sorted(d), [
1113 ('add', './count1.txt', c2["count1.txt"]),
1114 ('del', './count2.txt', c1["count2.txt"]),
1116 f = c1.open("count1.txt", "wb")
1119 # c1 added count1.txt, so c2 add will go to a conflict file
1122 c1.portable_manifest_text(),
1123 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~$")
1125 def test_conflict_del(self):
1126 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1127 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1129 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1130 c1.remove("count1.txt")
1132 # c1 deleted, so c2 mod will go to a conflict file
1135 c1.portable_manifest_text(),
1136 r"\. 5348b82a029fd9e971a811ce1f71360b\+43 0:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1138 def test_notify(self):
1141 c1.subscribe(lambda event, collection, name, item: events.append((event, collection, name, item)))
1142 f = c1.open("foo.txt", "wb")
1143 self.assertEqual(events[0], (arvados.collection.ADD, c1, "foo.txt", f.arvadosfile))
1145 def test_open_w(self):
1146 c1 = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n")
1147 self.assertEqual(c1["count1.txt"].size(), 10)
1148 c1.open("count1.txt", "wb").close()
1149 self.assertEqual(c1["count1.txt"].size(), 0)
1152 class NewCollectionTestCaseWithServers(run_test_server.TestCaseWithServers):
1153 def test_get_manifest_text_only_committed(self):
1155 with c.open("count.txt", "wb") as f:
1156 # One file committed
1157 with c.open("foo.txt", "wb") as foo:
1159 foo.flush() # Force block commit
1160 f.write(b"0123456789")
1161 # Other file not committed. Block not written to keep yet.
1163 c._get_manifest_text(".",
1166 only_committed=True),
1167 '. acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:count.txt 0:3:foo.txt\n')
1168 # And now with the file closed...
1169 f.flush() # Force block commit
1171 c._get_manifest_text(".",
1174 only_committed=True),
1175 ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:10:count.txt 10:3:foo.txt\n")
1177 def test_only_small_blocks_are_packed_together(self):
1179 # Write a couple of small files,
1180 f = c.open("count.txt", "wb")
1181 f.write(b"0123456789")
1182 f.close(flush=False)
1183 foo = c.open("foo.txt", "wb")
1185 foo.close(flush=False)
1186 # Then, write a big file, it shouldn't be packed with the ones above
1187 big = c.open("bigfile.txt", "wb")
1188 big.write(b"x" * 1024 * 1024 * 33) # 33 MB > KEEP_BLOCK_SIZE/2
1189 big.close(flush=False)
1191 c.manifest_text("."),
1192 '. 2d303c138c118af809f39319e5d507e9+34603008 a8430a058b8fbf408e1931b794dbd6fb+13 0:34603008:bigfile.txt 34603008:10:count.txt 34603018:3:foo.txt\n')
1195 class CollectionCreateUpdateTest(run_test_server.TestCaseWithServers):
1199 def create_count_txt(self):
1200 # Create an empty collection, save it to the API server, then write a
1201 # file, but don't save it.
1204 c.save_new("CollectionCreateUpdateTest", ensure_unique_name=True)
1205 self.assertEqual(c.portable_data_hash(), "d41d8cd98f00b204e9800998ecf8427e+0")
1206 self.assertEqual(c.api_response()["portable_data_hash"], "d41d8cd98f00b204e9800998ecf8427e+0" )
1208 with c.open("count.txt", "wb") as f:
1209 f.write(b"0123456789")
1211 self.assertEqual(c.portable_manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1215 def test_create_and_save(self):
1216 c = self.create_count_txt()
1220 r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1222 def test_create_and_save_new(self):
1223 c = self.create_count_txt()
1227 r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1229 def test_create_diff_apply(self):
1230 c1 = self.create_count_txt()
1233 c2 = Collection(c1.manifest_locator())
1234 with c2.open("count.txt", "wb") as f:
1239 self.assertEqual(diff[0], (arvados.collection.MOD, u'./count.txt', c1["count.txt"], c2["count.txt"]))
1242 self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1244 def test_diff_apply_with_token(self):
1245 baseline = CollectionReader(". 781e5e245d69b566979b86e28d23f2c7+10+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:10:count.txt\n")
1246 c = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1247 other = CollectionReader(". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1249 diff = baseline.diff(other)
1250 self.assertEqual(diff, [('mod', u'./count.txt', c["count.txt"], other["count.txt"])])
1254 self.assertEqual(c.manifest_text(), ". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1257 def test_create_and_update(self):
1258 c1 = self.create_count_txt()
1261 c2 = arvados.collection.Collection(c1.manifest_locator())
1262 with c2.open("count.txt", "wb") as f:
1267 self.assertNotEqual(c1.portable_data_hash(), c2.portable_data_hash())
1269 self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1272 def test_create_and_update_with_conflict(self):
1273 c1 = self.create_count_txt()
1276 with c1.open("count.txt", "wb") as f:
1279 c2 = arvados.collection.Collection(c1.manifest_locator())
1280 with c2.open("count.txt", "wb") as f:
1288 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~$")
1291 if __name__ == '__main__':