3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
15 import run_test_server
16 import arvados_testutil as tutil
17 from arvados.ranges import Range, LocatorAndRange
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_keep_local_store(self):
59 self.assertEqual(self.keep_client.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
60 self.assertEqual(self.keep_client.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
62 def test_local_collection_writer(self):
63 self.assertEqual(self.write_foo_bar_baz(),
64 '23ca013983d6239e98931cc779e68426+114',
65 'wrong locator hash: ' + self.write_foo_bar_baz())
67 def test_local_collection_reader(self):
68 foobarbaz = self.write_foo_bar_baz()
69 cr = arvados.CollectionReader(
70 foobarbaz + '+Xzizzle', self.api_client)
72 for s in cr.all_streams():
73 for f in s.all_files():
74 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
75 expected = [[3, '.', 'foo.txt', 'foo'],
76 [3, '.', 'bar.txt', 'bar'],
77 [3, './baz', 'baz.txt', 'baz']]
80 stream0 = cr.all_streams()[0]
81 self.assertEqual(stream0.readfrom(0, 0),
83 'reading zero bytes should have returned empty string')
84 self.assertEqual(stream0.readfrom(0, 2**26),
86 'reading entire stream failed')
87 self.assertEqual(stream0.readfrom(2**26, 0),
89 'reading zero bytes should have returned empty string')
91 def _test_subset(self, collection, expected):
92 cr = arvados.CollectionReader(collection, self.api_client)
93 for s in cr.all_streams():
97 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
100 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
102 def test_collection_manifest_subset(self):
103 foobarbaz = self.write_foo_bar_baz()
104 self._test_subset(foobarbaz,
105 [[3, '.', 'bar.txt', 'bar'],
106 [3, '.', 'foo.txt', 'foo'],
107 [3, './baz', 'baz.txt', 'baz']])
108 self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
109 (self.keep_client.put("foo"),
110 self.keep_client.put("bar"))),
111 [[3, '.', 'bar.txt', 'bar'],
112 [3, '.', 'foo.txt', 'foo']])
113 self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
114 (self.keep_client.put("foo"),
115 self.keep_client.put("bar"))),
116 [[2, '.', 'fo.txt', 'fo'],
117 [4, '.', 'obar.txt', 'obar']])
118 self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
119 (self.keep_client.put("foo"),
120 self.keep_client.put("bar"))),
121 [[2, '.', 'ar.txt', 'ar'],
122 [2, '.', 'fo.txt', 'fo'],
123 [2, '.', 'ob.txt', 'ob'],
124 [0, '.', 'zero.txt', '']])
126 def test_collection_empty_file(self):
127 cw = arvados.CollectionWriter(self.api_client)
128 cw.start_new_file('zero.txt')
131 self.assertEqual(cw.manifest_text(), ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:zero.txt\n")
132 self.check_manifest_file_sizes(cw.manifest_text(), [0])
133 cw = arvados.CollectionWriter(self.api_client)
134 cw.start_new_file('zero.txt')
136 cw.start_new_file('one.txt')
138 cw.start_new_stream('foo')
139 cw.start_new_file('zero.txt')
141 self.check_manifest_file_sizes(cw.manifest_text(), [0,1,0])
143 def test_no_implicit_normalize(self):
144 cw = arvados.CollectionWriter(self.api_client)
145 cw.start_new_file('b')
147 cw.start_new_file('a')
149 self.check_manifest_file_sizes(cw.manifest_text(), [1,0])
150 self.check_manifest_file_sizes(
151 arvados.CollectionReader(
152 cw.manifest_text()).manifest_text(normalize=True),
155 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
156 cr = arvados.CollectionReader(manifest_text, self.api_client)
158 for f in cr.all_files():
159 got_sizes += [f.size()]
160 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
162 def test_normalized_collection(self):
163 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
164 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
165 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
167 self.assertEqual(arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True),
168 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
171 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
173 self.assertEqual(arvados.CollectionReader(m2, self.api_client).manifest_text(normalize=True), m2)
175 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
176 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
177 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
179 self.assertEqual(arvados.CollectionReader(m3, self.api_client).manifest_text(normalize=True),
180 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
183 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
184 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
185 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar
187 self.assertEqual(arvados.CollectionReader(m4, self.api_client).manifest_text(normalize=True),
188 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
189 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
192 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
193 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
194 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar
196 self.assertEqual(arvados.CollectionReader(m5, self.api_client).manifest_text(normalize=True),
197 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
198 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
201 with self.data_file('1000G_ref_manifest') as f6:
203 self.assertEqual(arvados.CollectionReader(m6, self.api_client).manifest_text(normalize=True), m6)
205 with self.data_file('jlake_manifest') as f7:
207 self.assertEqual(arvados.CollectionReader(m7, self.api_client).manifest_text(normalize=True), m7)
209 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
211 self.assertEqual(arvados.CollectionReader(m8, self.api_client).manifest_text(normalize=True), m8)
213 def test_locators_and_ranges(self):
214 blocks2 = [Range('a', 0, 10),
221 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
222 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
223 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
224 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
225 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
226 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
227 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
228 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
230 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [LocatorAndRange('a', 10, 0, 2)])
231 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [LocatorAndRange('b', 10, 0, 2)])
232 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [LocatorAndRange('c', 10, 0, 2)])
233 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [LocatorAndRange('d', 10, 0, 2)])
234 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [LocatorAndRange('e', 10, 0, 2)])
235 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [LocatorAndRange('f', 10, 0, 2)])
236 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
237 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
239 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [LocatorAndRange('a', 10, 9, 1), LocatorAndRange('b', 10, 0, 1)])
240 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [LocatorAndRange('b', 10, 9, 1), LocatorAndRange('c', 10, 0, 1)])
241 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [LocatorAndRange('c', 10, 9, 1), LocatorAndRange('d', 10, 0, 1)])
242 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [LocatorAndRange('d', 10, 9, 1), LocatorAndRange('e', 10, 0, 1)])
243 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [LocatorAndRange('e', 10, 9, 1), LocatorAndRange('f', 10, 0, 1)])
244 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [LocatorAndRange('f', 10, 9, 1)])
247 blocks3 = [Range('a', 0, 10),
255 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
256 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
257 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
258 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
259 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
260 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
261 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [LocatorAndRange('g', 10, 2, 2)])
264 blocks = [Range('a', 0, 10),
267 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
268 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [LocatorAndRange('a', 10, 0, 5)])
269 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [LocatorAndRange('a', 10, 3, 5)])
270 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [LocatorAndRange('a', 10, 0, 10)])
272 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [LocatorAndRange('a', 10, 0, 10),
273 LocatorAndRange('b', 15, 0, 1)])
274 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [LocatorAndRange('a', 10, 1, 9),
275 LocatorAndRange('b', 15, 0, 2)])
276 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [LocatorAndRange('a', 10, 0, 10),
277 LocatorAndRange('b', 15, 0, 15)])
279 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [LocatorAndRange('a', 10, 0, 10),
280 LocatorAndRange('b', 15, 0, 15),
281 LocatorAndRange('c', 5, 0, 5)])
282 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [LocatorAndRange('a', 10, 1, 9),
283 LocatorAndRange('b', 15, 0, 15),
284 LocatorAndRange('c', 5, 0, 5)])
285 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [LocatorAndRange('a', 10, 0, 10),
286 LocatorAndRange('b', 15, 0, 15),
287 LocatorAndRange('c', 5, 0, 5)])
289 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [LocatorAndRange('b', 15, 5, 5)])
291 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [LocatorAndRange('a', 10, 8, 2),
292 LocatorAndRange('b', 15, 0, 15)])
294 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [LocatorAndRange('a', 10, 8, 2),
295 LocatorAndRange('b', 15, 0, 15),
296 LocatorAndRange('c', 5, 0, 3)])
298 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [LocatorAndRange('c', 5, 1, 2)])
300 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [LocatorAndRange('a', 10, 9, 1),
301 LocatorAndRange('b', 15, 0, 14)])
302 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [LocatorAndRange('b', 15, 0, 15)])
303 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [LocatorAndRange('b', 15, 1, 14),
304 LocatorAndRange('c', 5, 0, 1)])
306 class MockKeep(object):
307 def __init__(self, content, num_retries=0):
308 self.content = content
310 def get(self, locator, num_retries=0):
311 return self.content[locator]
313 def test_stream_reader(self):
314 keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
315 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
316 'cccccccccccccccccccccccccccccccc+5': 'z0123'}
317 mk = self.MockKeep(keepblocks)
319 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
321 content = 'abcdefghijklmnopqrstuvwxyz0123456789'
323 self.assertEqual(sr.readfrom(0, 30), content[0:30])
324 self.assertEqual(sr.readfrom(2, 30), content[2:30])
326 self.assertEqual(sr.readfrom(2, 8), content[2:10])
327 self.assertEqual(sr.readfrom(0, 10), content[0:10])
329 self.assertEqual(sr.readfrom(0, 5), content[0:5])
330 self.assertEqual(sr.readfrom(5, 5), content[5:10])
331 self.assertEqual(sr.readfrom(10, 5), content[10:15])
332 self.assertEqual(sr.readfrom(15, 5), content[15:20])
333 self.assertEqual(sr.readfrom(20, 5), content[20:25])
334 self.assertEqual(sr.readfrom(25, 5), content[25:30])
335 self.assertEqual(sr.readfrom(30, 5), '')
337 def test_extract_file(self):
338 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
339 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
340 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
341 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
342 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt
345 m2 = arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True)
348 ". 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")
349 files = arvados.CollectionReader(
350 m2, self.api_client).all_streams()[0].files()
352 self.assertEqual(files['md5sum.txt'].as_manifest(),
353 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
354 self.assertEqual(files['md6sum.txt'].as_manifest(),
355 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
356 self.assertEqual(files['md7sum.txt'].as_manifest(),
357 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
358 self.assertEqual(files['md9sum.txt'].as_manifest(),
359 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
361 def test_write_directory_tree(self):
362 cwriter = arvados.CollectionWriter(self.api_client)
363 cwriter.write_directory_tree(self.build_directory_tree(
364 ['basefile', 'subdir/subfile']))
365 self.assertEqual(cwriter.manifest_text(),
366 """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
367 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
369 def test_write_named_directory_tree(self):
370 cwriter = arvados.CollectionWriter(self.api_client)
371 cwriter.write_directory_tree(self.build_directory_tree(
372 ['basefile', 'subdir/subfile']), 'root')
374 cwriter.manifest_text(),
375 """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
376 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
378 def test_write_directory_tree_in_one_stream(self):
379 cwriter = arvados.CollectionWriter(self.api_client)
380 cwriter.write_directory_tree(self.build_directory_tree(
381 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
382 self.assertEqual(cwriter.manifest_text(),
383 """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile 8:14:subdir/subfile\n""")
385 def test_write_directory_tree_with_limited_recursion(self):
386 cwriter = arvados.CollectionWriter(self.api_client)
387 cwriter.write_directory_tree(
388 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
389 max_manifest_depth=1)
390 self.assertEqual(cwriter.manifest_text(),
391 """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
392 ./d1 50170217e5b04312024aa5cd42934494+13 0:8:d2/f3 8:5:f2\n""")
394 def test_write_directory_tree_with_zero_recursion(self):
395 cwriter = arvados.CollectionWriter(self.api_client)
396 content = 'd1/d2/f3d1/f2f1'
397 blockhash = hashlib.md5(content).hexdigest() + '+' + str(len(content))
398 cwriter.write_directory_tree(
399 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
400 max_manifest_depth=0)
402 cwriter.manifest_text(),
403 ". {} 0:8:d1/d2/f3 8:5:d1/f2 13:2:f1\n".format(blockhash))
405 def test_write_one_file(self):
406 cwriter = arvados.CollectionWriter(self.api_client)
407 with self.make_test_file() as testfile:
408 cwriter.write_file(testfile.name)
410 cwriter.manifest_text(),
411 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
412 os.path.basename(testfile.name)))
414 def test_write_named_file(self):
415 cwriter = arvados.CollectionWriter(self.api_client)
416 with self.make_test_file() as testfile:
417 cwriter.write_file(testfile.name, 'foo')
418 self.assertEqual(cwriter.manifest_text(),
419 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
421 def test_write_multiple_files(self):
422 cwriter = arvados.CollectionWriter(self.api_client)
424 with self.make_test_file(letter) as testfile:
425 cwriter.write_file(testfile.name, letter)
427 cwriter.manifest_text(),
428 ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
430 def test_basic_resume(self):
431 cwriter = TestResumableWriter()
432 with self.make_test_file() as testfile:
433 cwriter.write_file(testfile.name, 'test')
434 resumed = TestResumableWriter.from_state(cwriter.current_state())
435 self.assertEquals(cwriter.manifest_text(), resumed.manifest_text(),
436 "resumed CollectionWriter had different manifest")
438 def test_resume_fails_when_missing_dependency(self):
439 cwriter = TestResumableWriter()
440 with self.make_test_file() as testfile:
441 cwriter.write_file(testfile.name, 'test')
442 self.assertRaises(arvados.errors.StaleWriterStateError,
443 TestResumableWriter.from_state,
444 cwriter.current_state())
446 def test_resume_fails_when_dependency_mtime_changed(self):
447 cwriter = TestResumableWriter()
448 with self.make_test_file() as testfile:
449 cwriter.write_file(testfile.name, 'test')
450 os.utime(testfile.name, (0, 0))
451 self.assertRaises(arvados.errors.StaleWriterStateError,
452 TestResumableWriter.from_state,
453 cwriter.current_state())
455 def test_resume_fails_when_dependency_is_nonfile(self):
456 cwriter = TestResumableWriter()
457 cwriter.write_file('/dev/null', 'empty')
458 self.assertRaises(arvados.errors.StaleWriterStateError,
459 TestResumableWriter.from_state,
460 cwriter.current_state())
462 def test_resume_fails_when_dependency_size_changed(self):
463 cwriter = TestResumableWriter()
464 with self.make_test_file() as testfile:
465 cwriter.write_file(testfile.name, 'test')
466 orig_mtime = os.fstat(testfile.fileno()).st_mtime
467 testfile.write('extra')
469 os.utime(testfile.name, (orig_mtime, orig_mtime))
470 self.assertRaises(arvados.errors.StaleWriterStateError,
471 TestResumableWriter.from_state,
472 cwriter.current_state())
474 def test_resume_fails_with_expired_locator(self):
475 cwriter = TestResumableWriter()
476 state = cwriter.current_state()
477 # Add an expired locator to the state.
478 state['_current_stream_locators'].append(''.join([
479 'a' * 32, '+1+A', 'b' * 40, '@', '10000000']))
480 self.assertRaises(arvados.errors.StaleWriterStateError,
481 TestResumableWriter.from_state, state)
483 def test_arbitrary_objects_not_resumable(self):
484 cwriter = TestResumableWriter()
485 with open('/dev/null') as badfile:
486 self.assertRaises(arvados.errors.AssertionError,
487 cwriter.write_file, badfile)
489 def test_arbitrary_writes_not_resumable(self):
490 cwriter = TestResumableWriter()
491 self.assertRaises(arvados.errors.AssertionError,
492 cwriter.write, "badtext")
494 def test_read_arbitrary_data_with_collection_reader(self):
495 # arv-get relies on this to do "arv-get {keep-locator} -".
496 self.write_foo_bar_baz()
499 arvados.CollectionReader(
500 '3858f62230ac3c915f300c664312c63f+6'
504 class CollectionTestMixin(object):
506 'items_available': 1,
508 'uuid': 'zzzzz-bi6l4-mockproxy012345',
509 'owner_uuid': 'zzzzz-tpzed-mockowner012345',
510 'service_host': tutil.TEST_HOST,
511 'service_port': 65535,
512 'service_ssl_flag': True,
513 'service_type': 'proxy',
515 API_COLLECTIONS = run_test_server.fixture('collections')
516 DEFAULT_COLLECTION = API_COLLECTIONS['foo_file']
517 DEFAULT_DATA_HASH = DEFAULT_COLLECTION['portable_data_hash']
518 DEFAULT_MANIFEST = DEFAULT_COLLECTION['manifest_text']
519 DEFAULT_UUID = DEFAULT_COLLECTION['uuid']
520 ALT_COLLECTION = API_COLLECTIONS['bar_file']
521 ALT_DATA_HASH = ALT_COLLECTION['portable_data_hash']
522 ALT_MANIFEST = ALT_COLLECTION['manifest_text']
524 def _mock_api_call(self, mock_method, code, body):
525 mock_method = mock_method().execute
527 mock_method.return_value = body
529 mock_method.side_effect = arvados.errors.ApiError(
530 tutil.fake_httplib2_response(code), "{}")
532 def mock_keep_services(self, api_mock, code, body):
533 self._mock_api_call(api_mock.keep_services().accessible, code, body)
535 def api_client_mock(self, code=200):
536 client = mock.MagicMock(name='api_client')
537 self.mock_keep_services(client, code, self.PROXY_RESPONSE)
542 class CollectionReaderTestCase(unittest.TestCase, CollectionTestMixin):
543 def mock_get_collection(self, api_mock, code, body):
544 body = self.API_COLLECTIONS.get(body)
545 self._mock_api_call(api_mock.collections().get, code, body)
547 def api_client_mock(self, code=200):
548 client = super(CollectionReaderTestCase, self).api_client_mock(code)
549 self.mock_get_collection(client, code, 'foo_file')
552 def test_init_no_default_retries(self):
553 client = self.api_client_mock(200)
554 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
555 reader.manifest_text()
556 client.collections().get().execute.assert_called_with(num_retries=0)
558 def test_uuid_init_success(self):
559 client = self.api_client_mock(200)
560 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
562 self.assertEqual(self.DEFAULT_COLLECTION['manifest_text'],
563 reader.manifest_text())
564 client.collections().get().execute.assert_called_with(num_retries=3)
566 def test_uuid_init_failure_raises_api_error(self):
567 client = self.api_client_mock(500)
568 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
569 with self.assertRaises(arvados.errors.ApiError):
570 reader.manifest_text()
572 def test_locator_init(self):
573 client = self.api_client_mock(200)
574 # Ensure Keep will not return anything if asked.
575 with tutil.mock_get_responses(None, 404):
576 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
578 self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
580 def test_locator_init_fallback_to_keep(self):
581 # crunch-job needs this to read manifests that have only ever
582 # been written to Keep.
583 client = self.api_client_mock(200)
584 self.mock_get_collection(client, 404, None)
585 with tutil.mock_get_responses(self.DEFAULT_MANIFEST, 200):
586 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
588 self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
590 def test_uuid_init_no_fallback_to_keep(self):
591 # Do not look up a collection UUID in Keep.
592 client = self.api_client_mock(404)
593 reader = arvados.CollectionReader(self.DEFAULT_UUID,
595 with tutil.mock_get_responses(self.DEFAULT_MANIFEST, 200):
596 with self.assertRaises(arvados.errors.ApiError):
597 reader.manifest_text()
599 def test_try_keep_first_if_permission_hint(self):
600 # To verify that CollectionReader tries Keep first here, we
601 # mock API server to return the wrong data.
602 client = self.api_client_mock(200)
603 with tutil.mock_get_responses(self.ALT_MANIFEST, 200):
606 arvados.CollectionReader(
607 self.ALT_DATA_HASH + '+Affffffffffffffffffffffffffffffffffffffff@fedcba98',
608 api_client=client).manifest_text())
610 def test_init_num_retries_propagated(self):
611 # More of an integration test...
612 client = self.api_client_mock(200)
613 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
615 with tutil.mock_get_responses('foo', 500, 500, 200):
616 self.assertEqual('foo',
617 ''.join(f.read(9) for f in reader.all_files()))
619 def test_read_nonnormalized_manifest_with_collection_reader(self):
620 # client should be able to use CollectionReader on a manifest without normalizing it
621 client = self.api_client_mock(500)
622 nonnormal = ". acbd18db4cc2f85cedef654fccc4a4d8+3+Aabadbadbee@abeebdee 0:3:foo.txt 1:0:bar.txt 0:3:foo.txt\n"
623 reader = arvados.CollectionReader(
625 api_client=client, num_retries=0)
626 # Ensure stripped_manifest() doesn't mangle our manifest in
627 # any way other than stripping hints.
629 re.sub('\+[^\d\s\+]+', '', nonnormal),
630 reader.stripped_manifest())
631 # Ensure stripped_manifest() didn't mutate our reader.
632 self.assertEqual(nonnormal, reader.manifest_text())
633 # Ensure the files appear in the order given in the manifest.
635 [[6, '.', 'foo.txt'],
636 [0, '.', 'bar.txt']],
637 [[f.size(), f.stream_name(), f.name()]
638 for f in reader.all_streams()[0].all_files()])
640 def test_read_empty_collection(self):
641 client = self.api_client_mock(200)
642 self.mock_get_collection(client, 200, 'empty')
643 reader = arvados.CollectionReader('d41d8cd98f00b204e9800998ecf8427e+0',
645 self.assertEqual('', reader.manifest_text())
647 def test_api_response(self):
648 client = self.api_client_mock()
649 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
650 self.assertEqual(self.DEFAULT_COLLECTION, reader.api_response())
652 def test_api_response_with_collection_from_keep(self):
653 client = self.api_client_mock()
654 self.mock_get_collection(client, 404, 'foo')
655 with tutil.mock_get_responses(self.DEFAULT_MANIFEST, 200):
656 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
658 api_response = reader.api_response()
659 self.assertIsNone(api_response)
661 def check_open_file(self, coll_file, stream_name, file_name, file_size):
662 self.assertFalse(coll_file.closed, "returned file is not open")
663 self.assertEqual(stream_name, coll_file.stream_name())
664 self.assertEqual(file_name, coll_file.name())
665 self.assertEqual(file_size, coll_file.size())
667 def test_open_collection_file_one_argument(self):
668 client = self.api_client_mock(200)
669 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
670 cfile = reader.open('./foo')
671 self.check_open_file(cfile, '.', 'foo', 3)
673 def test_open_collection_file_two_arguments(self):
674 client = self.api_client_mock(200)
675 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
676 cfile = reader.open('.', 'foo')
677 self.check_open_file(cfile, '.', 'foo', 3)
679 def test_open_deep_file(self):
680 coll_name = 'collection_with_files_in_subdir'
681 client = self.api_client_mock(200)
682 self.mock_get_collection(client, 200, coll_name)
683 reader = arvados.CollectionReader(
684 self.API_COLLECTIONS[coll_name]['uuid'], api_client=client)
685 cfile = reader.open('./subdir2/subdir3/file2_in_subdir3.txt')
686 self.check_open_file(cfile, './subdir2/subdir3', 'file2_in_subdir3.txt',
689 def test_open_nonexistent_stream(self):
690 client = self.api_client_mock(200)
691 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
692 self.assertRaises(ValueError, reader.open, './nonexistent', 'foo')
694 def test_open_nonexistent_file(self):
695 client = self.api_client_mock(200)
696 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
697 self.assertRaises(ValueError, reader.open, '.', 'nonexistent')
701 class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
702 def mock_keep(self, body, *codes, **headers):
703 headers.setdefault('x-keep-replicas-stored', 2)
704 return tutil.mock_put_responses(body, *codes, **headers)
706 def foo_writer(self, **kwargs):
707 api_client = self.api_client_mock()
708 writer = arvados.CollectionWriter(api_client, **kwargs)
709 writer.start_new_file('foo')
713 def test_write_whole_collection(self):
714 writer = self.foo_writer()
715 with self.mock_keep(self.DEFAULT_DATA_HASH, 200, 200):
716 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
718 def test_write_no_default(self):
719 writer = self.foo_writer()
720 with self.mock_keep(None, 500):
721 with self.assertRaises(arvados.errors.KeepWriteError):
724 def test_write_whole_collection_through_retries(self):
725 writer = self.foo_writer(num_retries=2)
726 with self.mock_keep(self.DEFAULT_DATA_HASH,
727 500, 500, 200, 500, 500, 200):
728 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
730 def test_flush_data_retries(self):
731 writer = self.foo_writer(num_retries=2)
732 foo_hash = self.DEFAULT_MANIFEST.split()[1]
733 with self.mock_keep(foo_hash, 500, 200):
735 self.assertEqual(self.DEFAULT_MANIFEST, writer.manifest_text())
737 def test_one_open(self):
738 client = self.api_client_mock()
739 writer = arvados.CollectionWriter(client)
740 with writer.open('out') as out_file:
741 self.assertEqual('.', writer.current_stream_name())
742 self.assertEqual('out', writer.current_file_name())
743 out_file.write('test data')
744 data_loc = hashlib.md5('test data').hexdigest() + '+9'
745 self.assertTrue(out_file.closed, "writer file not closed after context")
746 self.assertRaises(ValueError, out_file.write, 'extra text')
747 with self.mock_keep(data_loc, 200) as keep_mock:
748 self.assertEqual(". {} 0:9:out\n".format(data_loc),
749 writer.manifest_text())
751 def test_open_writelines(self):
752 client = self.api_client_mock()
753 writer = arvados.CollectionWriter(client)
754 with writer.open('six') as out_file:
755 out_file.writelines(['12', '34', '56'])
756 data_loc = hashlib.md5('123456').hexdigest() + '+6'
757 with self.mock_keep(data_loc, 200) as keep_mock:
758 self.assertEqual(". {} 0:6:six\n".format(data_loc),
759 writer.manifest_text())
761 def test_open_flush(self):
762 client = self.api_client_mock()
763 writer = arvados.CollectionWriter(client)
764 with writer.open('flush_test') as out_file:
765 out_file.write('flush1')
766 data_loc1 = hashlib.md5('flush1').hexdigest() + '+6'
767 with self.mock_keep(data_loc1, 200) as keep_mock:
769 out_file.write('flush2')
770 data_loc2 = hashlib.md5('flush2').hexdigest() + '+6'
771 with self.mock_keep(data_loc2, 200) as keep_mock:
772 self.assertEqual(". {} {} 0:12:flush_test\n".format(data_loc1,
774 writer.manifest_text())
776 def test_two_opens_same_stream(self):
777 client = self.api_client_mock()
778 writer = arvados.CollectionWriter(client)
779 with writer.open('.', '1') as out_file:
780 out_file.write('1st')
781 with writer.open('.', '2') as out_file:
782 out_file.write('2nd')
783 data_loc = hashlib.md5('1st2nd').hexdigest() + '+6'
784 with self.mock_keep(data_loc, 200) as keep_mock:
785 self.assertEqual(". {} 0:3:1 3:3:2\n".format(data_loc),
786 writer.manifest_text())
788 def test_two_opens_two_streams(self):
789 client = self.api_client_mock()
790 writer = arvados.CollectionWriter(client)
791 with writer.open('file') as out_file:
792 out_file.write('file')
793 data_loc1 = hashlib.md5('file').hexdigest() + '+4'
794 with self.mock_keep(data_loc1, 200) as keep_mock:
795 with writer.open('./dir', 'indir') as out_file:
796 out_file.write('indir')
797 data_loc2 = hashlib.md5('indir').hexdigest() + '+5'
798 with self.mock_keep(data_loc2, 200) as keep_mock:
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 NewCollectionTestCase(unittest.TestCase, CollectionTestMixin):
811 def test_import_manifest(self):
812 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
813 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
814 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
816 self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", arvados.export_manifest(arvados.import_manifest(m1)))
818 def test_init_manifest(self):
819 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
820 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
821 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
823 self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", arvados.export_manifest(arvados.Collection(m1)))
825 if __name__ == '__main__':