1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
17 from unittest import mock
19 from . import run_test_server
20 from arvados._ranges import Range, LocatorAndRange
21 from arvados.collection import Collection, CollectionReader
22 from . import arvados_testutil as tutil
23 from .arvados_testutil import make_block_cache
25 class TestResumableWriter(arvados.ResumableCollectionWriter):
26 KEEP_BLOCK_SIZE = 1024 # PUT to Keep every 1K.
28 def current_state(self):
29 return self.dump_state(copy.deepcopy)
31 @parameterized.parameterized_class([{"disk_cache": True}, {"disk_cache": False}])
32 class ArvadosCollectionsTest(run_test_server.TestCaseWithServers,
33 tutil.ArvadosBaseTestCase):
39 super(ArvadosCollectionsTest, cls).setUpClass()
40 # need admin privileges to make collections with unsigned blocks
41 run_test_server.authorize_with('admin')
42 cls.api_client = arvados.api('v1')
43 cls.keep_client = arvados.KeepClient(api_client=cls.api_client,
44 local_store=cls.local_store,
45 block_cache=make_block_cache(cls.disk_cache))
47 def write_foo_bar_baz(self):
48 cw = arvados.CollectionWriter(self.api_client)
49 self.assertEqual(cw.current_stream_name(), '.',
50 'current_stream_name() should be "." now')
51 cw.set_current_file_name('foo.txt')
53 self.assertEqual(cw.current_file_name(), 'foo.txt',
54 'current_file_name() should be foo.txt now')
55 cw.start_new_file('bar.txt')
57 cw.start_new_stream('baz')
59 cw.set_current_file_name('baz.txt')
60 self.assertEqual(cw.manifest_text(),
61 ". 3858f62230ac3c915f300c664312c63f+6 0:3:foo.txt 3:3:bar.txt\n" +
62 "./baz 73feffa4b7f6bb68e44cf984c85f6e88+3 0:3:baz.txt\n",
63 "wrong manifest: got {}".format(cw.manifest_text()))
65 return cw.portable_data_hash()
67 def test_pdh_is_native_str(self):
68 pdh = self.write_foo_bar_baz()
69 self.assertEqual(type(''), type(pdh))
71 def test_keep_local_store(self):
72 self.assertEqual(self.keep_client.put(b'foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
73 self.assertEqual(self.keep_client.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), b'foo', 'wrong data from Keep.get')
75 def test_local_collection_writer(self):
76 self.assertEqual(self.write_foo_bar_baz(),
77 '23ca013983d6239e98931cc779e68426+114',
78 'wrong locator hash: ' + self.write_foo_bar_baz())
80 def test_local_collection_reader(self):
81 foobarbaz = self.write_foo_bar_baz()
82 cr = arvados.CollectionReader(
83 foobarbaz + '+Xzizzle', self.api_client)
85 for s in cr.all_streams():
86 for f in s.all_files():
87 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
88 expected = [[3, '.', 'foo.txt', b'foo'],
89 [3, '.', 'bar.txt', b'bar'],
90 [3, './baz', 'baz.txt', b'baz']]
93 stream0 = cr.all_streams()[0]
94 self.assertEqual(stream0.readfrom(0, 0),
96 'reading zero bytes should have returned empty string')
97 self.assertEqual(stream0.readfrom(0, 2**26),
99 'reading entire stream failed')
100 self.assertEqual(stream0.readfrom(2**26, 0),
102 'reading zero bytes should have returned empty string')
103 self.assertEqual(3, len(cr))
106 def _test_subset(self, collection, expected):
107 cr = arvados.CollectionReader(collection, self.api_client)
108 for s in cr.all_streams():
112 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
113 self.assertEqual(got,
115 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
117 def test_collection_manifest_subset(self):
118 foobarbaz = self.write_foo_bar_baz()
119 self._test_subset(foobarbaz,
120 [[3, '.', 'bar.txt', b'bar'],
121 [3, '.', 'foo.txt', b'foo'],
122 [3, './baz', 'baz.txt', b'baz']])
123 self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
124 (self.keep_client.put(b"foo"),
125 self.keep_client.put(b"bar"))),
126 [[3, '.', 'bar.txt', b'bar'],
127 [3, '.', 'foo.txt', b'foo']])
128 self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
129 (self.keep_client.put(b"foo"),
130 self.keep_client.put(b"bar"))),
131 [[2, '.', 'fo.txt', b'fo'],
132 [4, '.', 'obar.txt', b'obar']])
133 self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
134 (self.keep_client.put(b"foo"),
135 self.keep_client.put(b"bar"))),
136 [[2, '.', 'ar.txt', b'ar'],
137 [2, '.', 'fo.txt', b'fo'],
138 [2, '.', 'ob.txt', b'ob'],
139 [0, '.', 'zero.txt', b'']])
141 def test_collection_empty_file(self):
142 cw = arvados.CollectionWriter(self.api_client)
143 cw.start_new_file('zero.txt')
146 self.assertEqual(cw.manifest_text(), ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:zero.txt\n")
147 self.check_manifest_file_sizes(cw.manifest_text(), [0])
148 cw = arvados.CollectionWriter(self.api_client)
149 cw.start_new_file('zero.txt')
151 cw.start_new_file('one.txt')
153 cw.start_new_stream('foo')
154 cw.start_new_file('zero.txt')
156 self.check_manifest_file_sizes(cw.manifest_text(), [0,1,0])
158 def test_no_implicit_normalize(self):
159 cw = arvados.CollectionWriter(self.api_client)
160 cw.start_new_file('b')
162 cw.start_new_file('a')
164 self.check_manifest_file_sizes(cw.manifest_text(), [1,0])
165 self.check_manifest_file_sizes(
166 arvados.CollectionReader(
167 cw.manifest_text()).manifest_text(normalize=True),
170 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
171 cr = arvados.CollectionReader(manifest_text, self.api_client)
173 for f in cr.all_files():
174 got_sizes += [f.size()]
175 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
177 def test_normalized_collection(self):
178 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
179 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
180 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
182 self.assertEqual(arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True),
183 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
186 m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
188 self.assertEqual(arvados.CollectionReader(m2, self.api_client).manifest_text(normalize=True), m2)
190 m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
191 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
192 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
194 self.assertEqual(arvados.CollectionReader(m3, self.api_client).manifest_text(normalize=True),
195 """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
198 m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
199 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
200 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar
202 self.assertEqual(arvados.CollectionReader(m4, self.api_client).manifest_text(normalize=True),
203 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
204 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
207 m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
208 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
209 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar
211 self.assertEqual(arvados.CollectionReader(m5, self.api_client).manifest_text(normalize=True),
212 """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
213 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
216 with self.data_file('1000G_ref_manifest') as f6:
218 self.assertEqual(arvados.CollectionReader(m6, self.api_client).manifest_text(normalize=True), m6)
220 with self.data_file('jlake_manifest') as f7:
222 self.assertEqual(arvados.CollectionReader(m7, self.api_client).manifest_text(normalize=True), m7)
224 m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
226 self.assertEqual(arvados.CollectionReader(m8, self.api_client).manifest_text(normalize=True), m8)
228 def test_locators_and_ranges(self):
229 blocks2 = [Range('a', 0, 10),
236 self.assertEqual(arvados.locators_and_ranges(blocks2, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
237 self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
238 self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
239 self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
240 self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
241 self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
242 self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
243 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
245 self.assertEqual(arvados.locators_and_ranges(blocks2, 0, 2), [LocatorAndRange('a', 10, 0, 2)])
246 self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [LocatorAndRange('b', 10, 0, 2)])
247 self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [LocatorAndRange('c', 10, 0, 2)])
248 self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [LocatorAndRange('d', 10, 0, 2)])
249 self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [LocatorAndRange('e', 10, 0, 2)])
250 self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [LocatorAndRange('f', 10, 0, 2)])
251 self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
252 self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
254 self.assertEqual(arvados.locators_and_ranges(blocks2, 9, 2), [LocatorAndRange('a', 10, 9, 1), LocatorAndRange('b', 10, 0, 1)])
255 self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [LocatorAndRange('b', 10, 9, 1), LocatorAndRange('c', 10, 0, 1)])
256 self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [LocatorAndRange('c', 10, 9, 1), LocatorAndRange('d', 10, 0, 1)])
257 self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [LocatorAndRange('d', 10, 9, 1), LocatorAndRange('e', 10, 0, 1)])
258 self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [LocatorAndRange('e', 10, 9, 1), LocatorAndRange('f', 10, 0, 1)])
259 self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [LocatorAndRange('f', 10, 9, 1)])
262 blocks3 = [Range('a', 0, 10),
270 self.assertEqual(arvados.locators_and_ranges(blocks3, 2, 2), [LocatorAndRange('a', 10, 2, 2)])
271 self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
272 self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
273 self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
274 self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
275 self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
276 self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [LocatorAndRange('g', 10, 2, 2)])
279 blocks = [Range('a', 0, 10),
282 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
283 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [LocatorAndRange('a', 10, 0, 5)])
284 self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [LocatorAndRange('a', 10, 3, 5)])
285 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [LocatorAndRange('a', 10, 0, 10)])
287 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [LocatorAndRange('a', 10, 0, 10),
288 LocatorAndRange('b', 15, 0, 1)])
289 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [LocatorAndRange('a', 10, 1, 9),
290 LocatorAndRange('b', 15, 0, 2)])
291 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [LocatorAndRange('a', 10, 0, 10),
292 LocatorAndRange('b', 15, 0, 15)])
294 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [LocatorAndRange('a', 10, 0, 10),
295 LocatorAndRange('b', 15, 0, 15),
296 LocatorAndRange('c', 5, 0, 5)])
297 self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [LocatorAndRange('a', 10, 1, 9),
298 LocatorAndRange('b', 15, 0, 15),
299 LocatorAndRange('c', 5, 0, 5)])
300 self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [LocatorAndRange('a', 10, 0, 10),
301 LocatorAndRange('b', 15, 0, 15),
302 LocatorAndRange('c', 5, 0, 5)])
304 self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [LocatorAndRange('b', 15, 5, 5)])
306 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [LocatorAndRange('a', 10, 8, 2),
307 LocatorAndRange('b', 15, 0, 15)])
309 self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [LocatorAndRange('a', 10, 8, 2),
310 LocatorAndRange('b', 15, 0, 15),
311 LocatorAndRange('c', 5, 0, 3)])
313 self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [LocatorAndRange('c', 5, 1, 2)])
315 self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [LocatorAndRange('a', 10, 9, 1),
316 LocatorAndRange('b', 15, 0, 14)])
317 self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [LocatorAndRange('b', 15, 0, 15)])
318 self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [LocatorAndRange('b', 15, 1, 14),
319 LocatorAndRange('c', 5, 0, 1)])
321 class MockKeep(object):
322 def __init__(self, content, num_retries=0):
323 self.content = content
324 self.num_prefetch_threads = 1
326 def get(self, locator, num_retries=0, prefetch=False):
327 return self.content[locator]
329 def test_stream_reader(self):
331 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': b'abcdefghij',
332 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': b'klmnopqrstuvwxy',
333 'cccccccccccccccccccccccccccccccc+5': b'z0123',
335 mk = self.MockKeep(keepblocks)
337 sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
339 content = b'abcdefghijklmnopqrstuvwxyz0123456789'
341 self.assertEqual(sr.readfrom(0, 30), content[0:30])
342 self.assertEqual(sr.readfrom(2, 30), content[2:30])
344 self.assertEqual(sr.readfrom(2, 8), content[2:10])
345 self.assertEqual(sr.readfrom(0, 10), content[0:10])
347 self.assertEqual(sr.readfrom(0, 5), content[0:5])
348 self.assertEqual(sr.readfrom(5, 5), content[5:10])
349 self.assertEqual(sr.readfrom(10, 5), content[10:15])
350 self.assertEqual(sr.readfrom(15, 5), content[15:20])
351 self.assertEqual(sr.readfrom(20, 5), content[20:25])
352 self.assertEqual(sr.readfrom(25, 5), content[25:30])
353 self.assertEqual(sr.readfrom(30, 5), b'')
355 def test_extract_file(self):
356 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
357 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
358 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
359 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
360 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt
363 m2 = arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True)
366 ". 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")
367 files = arvados.CollectionReader(
368 m2, self.api_client).all_streams()[0].files()
370 self.assertEqual(files['md5sum.txt'].as_manifest(),
371 ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
372 self.assertEqual(files['md6sum.txt'].as_manifest(),
373 ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
374 self.assertEqual(files['md7sum.txt'].as_manifest(),
375 ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
376 self.assertEqual(files['md9sum.txt'].as_manifest(),
377 ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
379 def test_write_directory_tree(self):
380 cwriter = arvados.CollectionWriter(self.api_client)
381 cwriter.write_directory_tree(self.build_directory_tree(
382 ['basefile', 'subdir/subfile']))
383 self.assertEqual(cwriter.manifest_text(),
384 """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
385 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
387 def test_write_named_directory_tree(self):
388 cwriter = arvados.CollectionWriter(self.api_client)
389 cwriter.write_directory_tree(self.build_directory_tree(
390 ['basefile', 'subdir/subfile']), 'root')
392 cwriter.manifest_text(),
393 """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
394 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
396 def test_write_directory_tree_in_one_stream(self):
397 cwriter = arvados.CollectionWriter(self.api_client)
398 cwriter.write_directory_tree(self.build_directory_tree(
399 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
400 self.assertEqual(cwriter.manifest_text(),
401 """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile 8:14:subdir/subfile\n""")
403 def test_write_directory_tree_with_limited_recursion(self):
404 cwriter = arvados.CollectionWriter(self.api_client)
405 cwriter.write_directory_tree(
406 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
407 max_manifest_depth=1)
408 self.assertEqual(cwriter.manifest_text(),
409 """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
410 ./d1 50170217e5b04312024aa5cd42934494+13 0:8:d2/f3 8:5:f2\n""")
412 def test_write_directory_tree_with_zero_recursion(self):
413 cwriter = arvados.CollectionWriter(self.api_client)
414 content = 'd1/d2/f3d1/f2f1'
415 blockhash = tutil.str_keep_locator(content)
416 cwriter.write_directory_tree(
417 self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
418 max_manifest_depth=0)
420 cwriter.manifest_text(),
421 ". {} 0:8:d1/d2/f3 8:5:d1/f2 13:2:f1\n".format(blockhash))
423 def test_write_one_file(self):
424 cwriter = arvados.CollectionWriter(self.api_client)
425 with self.make_test_file() as testfile:
426 cwriter.write_file(testfile.name)
428 cwriter.manifest_text(),
429 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
430 os.path.basename(testfile.name)))
432 def test_write_named_file(self):
433 cwriter = arvados.CollectionWriter(self.api_client)
434 with self.make_test_file() as testfile:
435 cwriter.write_file(testfile.name, 'foo')
436 self.assertEqual(cwriter.manifest_text(),
437 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
439 def test_write_multiple_files(self):
440 cwriter = arvados.CollectionWriter(self.api_client)
442 with self.make_test_file(letter.encode()) as testfile:
443 cwriter.write_file(testfile.name, letter)
445 cwriter.manifest_text(),
446 ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
448 def test_basic_resume(self):
449 cwriter = TestResumableWriter()
450 with self.make_test_file() as testfile:
451 cwriter.write_file(testfile.name, 'test')
452 resumed = TestResumableWriter.from_state(cwriter.current_state())
453 self.assertEqual(cwriter.manifest_text(), resumed.manifest_text(),
454 "resumed CollectionWriter had different manifest")
456 def test_resume_fails_when_missing_dependency(self):
457 cwriter = TestResumableWriter()
458 with self.make_test_file() as testfile:
459 cwriter.write_file(testfile.name, 'test')
460 self.assertRaises(arvados.errors.StaleWriterStateError,
461 TestResumableWriter.from_state,
462 cwriter.current_state())
464 def test_resume_fails_when_dependency_mtime_changed(self):
465 cwriter = TestResumableWriter()
466 with self.make_test_file() as testfile:
467 cwriter.write_file(testfile.name, 'test')
468 os.utime(testfile.name, (0, 0))
469 self.assertRaises(arvados.errors.StaleWriterStateError,
470 TestResumableWriter.from_state,
471 cwriter.current_state())
473 def test_resume_fails_when_dependency_is_nonfile(self):
474 cwriter = TestResumableWriter()
475 cwriter.write_file('/dev/null', 'empty')
476 self.assertRaises(arvados.errors.StaleWriterStateError,
477 TestResumableWriter.from_state,
478 cwriter.current_state())
480 def test_resume_fails_when_dependency_size_changed(self):
481 cwriter = TestResumableWriter()
482 with self.make_test_file() as testfile:
483 cwriter.write_file(testfile.name, 'test')
484 orig_mtime = os.fstat(testfile.fileno()).st_mtime
485 testfile.write(b'extra')
487 os.utime(testfile.name, (orig_mtime, orig_mtime))
488 self.assertRaises(arvados.errors.StaleWriterStateError,
489 TestResumableWriter.from_state,
490 cwriter.current_state())
492 def test_resume_fails_with_expired_locator(self):
493 cwriter = TestResumableWriter()
494 state = cwriter.current_state()
495 # Add an expired locator to the state.
496 state['_current_stream_locators'].append(''.join([
497 'a' * 32, '+1+A', 'b' * 40, '@', '10000000']))
498 self.assertRaises(arvados.errors.StaleWriterStateError,
499 TestResumableWriter.from_state, state)
501 def test_arbitrary_objects_not_resumable(self):
502 cwriter = TestResumableWriter()
503 with open('/dev/null') as badfile:
504 self.assertRaises(arvados.errors.AssertionError,
505 cwriter.write_file, badfile)
507 def test_arbitrary_writes_not_resumable(self):
508 cwriter = TestResumableWriter()
509 self.assertRaises(arvados.errors.AssertionError,
510 cwriter.write, "badtext")
513 class CollectionTestMixin(tutil.ApiClientMock):
514 API_COLLECTIONS = run_test_server.fixture('collections')
515 DEFAULT_COLLECTION = API_COLLECTIONS['foo_file']
516 DEFAULT_DATA_HASH = DEFAULT_COLLECTION['portable_data_hash']
517 DEFAULT_MANIFEST = DEFAULT_COLLECTION['manifest_text']
518 DEFAULT_UUID = DEFAULT_COLLECTION['uuid']
519 ALT_COLLECTION = API_COLLECTIONS['bar_file']
520 ALT_DATA_HASH = ALT_COLLECTION['portable_data_hash']
521 ALT_MANIFEST = ALT_COLLECTION['manifest_text']
523 def api_client_mock(self, status=200):
524 client = super(CollectionTestMixin, self).api_client_mock()
525 self.mock_keep_services(client, status=status, service_type='proxy', count=1)
530 class CollectionReaderTestCase(unittest.TestCase, CollectionTestMixin):
531 def mock_get_collection(self, api_mock, code, fixturename):
532 body = self.API_COLLECTIONS.get(fixturename)
533 self._mock_api_call(api_mock.collections().get, code, body)
535 def api_client_mock(self, status=200):
536 client = super(CollectionReaderTestCase, self).api_client_mock()
537 self.mock_get_collection(client, status, 'foo_file')
540 def test_init_default_retries(self):
541 client = self.api_client_mock(200)
542 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
543 reader.manifest_text()
544 client.collections().get().execute.assert_called_with(num_retries=10)
546 def test_uuid_init_success(self):
547 client = self.api_client_mock(200)
548 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
550 self.assertEqual(self.DEFAULT_COLLECTION['manifest_text'],
551 reader.manifest_text())
552 client.collections().get().execute.assert_called_with(num_retries=3)
554 def test_uuid_init_failure_raises_api_error(self):
555 client = self.api_client_mock(500)
556 with self.assertRaises(arvados.errors.ApiError):
557 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
559 def test_locator_init(self):
560 client = self.api_client_mock(200)
561 # Ensure Keep will not return anything if asked.
562 with tutil.mock_keep_responses(None, 404):
563 reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
565 self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
567 def test_init_no_fallback_to_keep(self):
568 # Do not look up a collection UUID or PDH in Keep.
569 for key in [self.DEFAULT_UUID, self.DEFAULT_DATA_HASH]:
570 client = self.api_client_mock(404)
571 with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
572 with self.assertRaises(arvados.errors.ApiError):
573 reader = arvados.CollectionReader(key, api_client=client)
575 def test_init_num_retries_propagated(self):
576 # More of an integration test...
577 client = self.api_client_mock(200)
578 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
580 with tutil.mock_keep_responses('foo', 500, 500, 200):
581 self.assertEqual(b'foo',
582 b''.join(f.read(9) for f in reader.all_files()))
584 def test_read_nonnormalized_manifest_with_collection_reader(self):
585 # client should be able to use CollectionReader on a manifest without normalizing it
586 client = self.api_client_mock(500)
587 nonnormal = ". acbd18db4cc2f85cedef654fccc4a4d8+3+Aabadbadbee@abeebdee 0:3:foo.txt 1:0:bar.txt 0:3:foo.txt\n"
588 reader = arvados.CollectionReader(
590 api_client=client, num_retries=0)
591 # Ensure stripped_manifest() doesn't mangle our manifest in
592 # any way other than stripping hints.
594 re.sub(r'\+[^\d\s\+]+', '', nonnormal),
595 reader.stripped_manifest())
596 # Ensure stripped_manifest() didn't mutate our reader.
597 self.assertEqual(nonnormal, reader.manifest_text())
598 # Ensure the files appear in the order given in the manifest.
600 [[6, '.', 'foo.txt'],
601 [0, '.', 'bar.txt']],
602 [[f.size(), f.stream_name(), f.name()]
603 for f in reader.all_streams()[0].all_files()])
605 def test_read_empty_collection(self):
606 client = self.api_client_mock(200)
607 self.mock_get_collection(client, 200, 'empty')
608 reader = arvados.CollectionReader('d41d8cd98f00b204e9800998ecf8427e+0',
610 self.assertEqual('', reader.manifest_text())
611 self.assertEqual(0, len(reader))
612 self.assertFalse(reader)
614 def test_api_response(self):
615 client = self.api_client_mock()
616 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
617 self.assertEqual(self.DEFAULT_COLLECTION, reader.api_response())
619 def check_open_file(self, coll_file, stream_name, file_name, file_size):
620 self.assertFalse(coll_file.closed, "returned file is not open")
621 self.assertEqual(stream_name, coll_file.stream_name())
622 self.assertEqual(file_name, coll_file.name)
623 self.assertEqual(file_size, coll_file.size())
625 def test_open_collection_file_one_argument(self):
626 client = self.api_client_mock(200)
627 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
628 cfile = reader.open('./foo', 'rb')
629 self.check_open_file(cfile, '.', 'foo', 3)
631 def test_open_deep_file(self):
632 coll_name = 'collection_with_files_in_subdir'
633 client = self.api_client_mock(200)
634 self.mock_get_collection(client, 200, coll_name)
635 reader = arvados.CollectionReader(
636 self.API_COLLECTIONS[coll_name]['uuid'], api_client=client)
637 cfile = reader.open('./subdir2/subdir3/file2_in_subdir3.txt', 'rb')
638 self.check_open_file(cfile, './subdir2/subdir3', 'file2_in_subdir3.txt',
641 def test_open_nonexistent_stream(self):
642 client = self.api_client_mock(200)
643 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
644 self.assertRaises(IOError, reader.open, './nonexistent/foo')
646 def test_open_nonexistent_file(self):
647 client = self.api_client_mock(200)
648 reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
649 self.assertRaises(IOError, reader.open, 'nonexistent')
653 class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
654 def mock_keep(self, body, *codes, **headers):
655 headers.setdefault('x-keep-replicas-stored', 2)
656 return tutil.mock_keep_responses(body, *codes, **headers)
658 def foo_writer(self, **kwargs):
659 kwargs.setdefault('api_client', self.api_client_mock())
660 writer = arvados.CollectionWriter(**kwargs)
661 writer.start_new_file('foo')
665 def test_write_whole_collection(self):
666 writer = self.foo_writer()
667 with self.mock_keep(self.DEFAULT_DATA_HASH, 200, 200):
668 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
670 def test_write_no_default(self):
671 writer = self.foo_writer()
672 with self.mock_keep(None, 500):
673 with self.assertRaises(arvados.errors.KeepWriteError):
676 def test_write_insufficient_replicas_via_proxy(self):
677 writer = self.foo_writer(replication=3)
678 with self.mock_keep(None, 200, **{'x-keep-replicas-stored': 2}):
679 with self.assertRaises(arvados.errors.KeepWriteError):
680 writer.manifest_text()
682 def test_write_insufficient_replicas_via_disks(self):
683 client = mock.MagicMock(name='api_client')
686 **{'x-keep-replicas-stored': 1}) as keepmock:
687 self.mock_keep_services(client, status=200, service_type='disk', count=2)
688 writer = self.foo_writer(api_client=client, replication=3)
689 with self.assertRaises(arvados.errors.KeepWriteError):
690 writer.manifest_text()
692 def test_write_three_replicas(self):
693 client = mock.MagicMock(name='api_client')
695 "", 500, 500, 500, 200, 200, 200,
696 **{'x-keep-replicas-stored': 1}) as keepmock:
697 self.mock_keep_services(client, status=200, service_type='disk', count=6)
698 writer = self.foo_writer(api_client=client, replication=3)
699 writer.manifest_text()
700 self.assertEqual(6, keepmock.call_count)
702 def test_write_whole_collection_through_retries(self):
703 writer = self.foo_writer(num_retries=2)
704 with self.mock_keep(self.DEFAULT_DATA_HASH,
705 500, 500, 200, 500, 500, 200):
706 self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
708 def test_flush_data_retries(self):
709 writer = self.foo_writer(num_retries=2)
710 foo_hash = self.DEFAULT_MANIFEST.split()[1]
711 with self.mock_keep(foo_hash, 500, 200):
713 self.assertEqual(self.DEFAULT_MANIFEST, writer.manifest_text())
715 def test_one_open(self):
716 client = self.api_client_mock()
717 writer = arvados.CollectionWriter(client)
718 with writer.open('out') as out_file:
719 self.assertEqual('.', writer.current_stream_name())
720 self.assertEqual('out', writer.current_file_name())
721 out_file.write(b'test data')
722 data_loc = tutil.str_keep_locator('test data')
723 self.assertTrue(out_file.closed, "writer file not closed after context")
724 self.assertRaises(ValueError, out_file.write, 'extra text')
725 with self.mock_keep(data_loc, 200) as keep_mock:
726 self.assertEqual(". {} 0:9:out\n".format(data_loc),
727 writer.manifest_text())
729 def test_open_writelines(self):
730 client = self.api_client_mock()
731 writer = arvados.CollectionWriter(client)
732 with writer.open('six') as out_file:
733 out_file.writelines(['12', '34', '56'])
734 data_loc = tutil.str_keep_locator('123456')
735 with self.mock_keep(data_loc, 200) as keep_mock:
736 self.assertEqual(". {} 0:6:six\n".format(data_loc),
737 writer.manifest_text())
739 def test_open_flush(self):
740 client = self.api_client_mock()
741 data_loc1 = tutil.str_keep_locator('flush1')
742 data_loc2 = tutil.str_keep_locator('flush2')
743 with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
744 writer = arvados.CollectionWriter(client)
745 with writer.open('flush_test') as out_file:
746 out_file.write(b'flush1')
748 out_file.write(b'flush2')
749 self.assertEqual(". {} {} 0:12:flush_test\n".format(data_loc1,
751 writer.manifest_text())
753 def test_two_opens_same_stream(self):
754 client = self.api_client_mock()
755 writer = arvados.CollectionWriter(client)
756 with writer.open('.', '1') as out_file:
757 out_file.write(b'1st')
758 with writer.open('.', '2') as out_file:
759 out_file.write(b'2nd')
760 data_loc = tutil.str_keep_locator('1st2nd')
761 with self.mock_keep(data_loc, 200) as keep_mock:
762 self.assertEqual(". {} 0:3:1 3:3:2\n".format(data_loc),
763 writer.manifest_text())
765 def test_two_opens_two_streams(self):
766 client = self.api_client_mock()
767 data_loc1 = tutil.str_keep_locator('file')
768 data_loc2 = tutil.str_keep_locator('indir')
769 with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
770 writer = arvados.CollectionWriter(client)
771 with writer.open('file') as out_file:
772 out_file.write(b'file')
773 with writer.open('./dir', 'indir') as out_file:
774 out_file.write(b'indir')
775 expected = ". {} 0:4:file\n./dir {} 0:5:indir\n".format(
776 data_loc1, data_loc2)
777 self.assertEqual(expected, writer.manifest_text())
779 def test_dup_open_fails(self):
780 client = self.api_client_mock()
781 writer = arvados.CollectionWriter(client)
782 file1 = writer.open('one')
783 self.assertRaises(arvados.errors.AssertionError, writer.open, 'two')
786 class CollectionMethods(run_test_server.TestCaseWithServers):
788 def test_keys_values_items_support_indexing(self):
790 with c.open('foo', 'wb') as f:
792 with c.open('bar', 'wb') as f:
794 self.assertEqual(2, len(c.keys()))
795 if sys.version_info < (3, 0):
796 # keys() supports indexing only for python2 callers.
801 self.assertEqual(2, len(c.values()))
804 self.assertEqual(2, len(c.items()))
805 self.assertEqual(fn0, c.items()[0][0])
806 self.assertEqual(fn1, c.items()[1][0])
808 def test_get_properties(self):
810 self.assertEqual(c.get_properties(), {})
811 c.save_new(properties={"foo":"bar"})
812 self.assertEqual(c.get_properties(), {"foo":"bar"})
814 def test_get_trash_at(self):
816 self.assertEqual(c.get_trash_at(), None)
817 c.save_new(trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
818 self.assertEqual(c.get_trash_at(), ciso8601.parse_datetime('2111-01-01T11:11:11.111111000Z'))
821 class CollectionOpenModes(run_test_server.TestCaseWithServers):
823 def test_open_binary_modes(self):
825 for mode in ['wb', 'wb+', 'ab', 'ab+']:
826 with c.open('foo', mode) as f:
829 def test_open_invalid_modes(self):
831 for mode in ['+r', 'aa', '++', 'r+b', 'beer', '', None]:
832 with self.assertRaises(Exception):
835 def test_open_text_modes(self):
837 with c.open('foo', 'wb') as f:
839 for mode in ['r', 'rt', 'r+', 'rt+', 'w', 'wt', 'a', 'at']:
840 with c.open('foo', mode) as f:
841 if mode[0] == 'r' and '+' not in mode:
842 self.assertEqual('foo', f.read(3))
845 f.seek(0, os.SEEK_SET)
846 self.assertEqual('bar', f.read(3))
849 class TextModes(run_test_server.TestCaseWithServers):
852 arvados.config.KEEP_BLOCK_SIZE = 4
853 if sys.version_info < (3, 0):
855 self.sailboat = unicodedata.lookup('SAILBOAT')
856 self.snowman = unicodedata.lookup('SNOWMAN')
858 self.sailboat = '\N{SAILBOAT}'
859 self.snowman = '\N{SNOWMAN}'
862 arvados.config.KEEP_BLOCK_SIZE = 2 ** 26
864 def test_read_sailboat_across_block_boundary(self):
866 f = c.open('sailboats', 'wb')
867 data = self.sailboat.encode('utf-8')
873 self.assertRegex(c.portable_manifest_text(), r'\+4 .*\+3 ')
875 f = c.open('sailboats', 'r')
876 string = f.readline()
877 self.assertEqual(string, self.sailboat+self.sailboat+'\n')
880 def test_write_snowman_across_block_boundary(self):
882 f = c.open('snowmany', 'w')
884 f.write(data+data+'\n'+data+'\n')
886 self.assertRegex(c.portable_manifest_text(), r'\+4 .*\+4 .*\+3 ')
888 f = c.open('snowmany', 'r')
889 self.assertEqual(f.readline(), self.snowman+self.snowman+'\n')
890 self.assertEqual(f.readline(), self.snowman+'\n')
894 class NewCollectionTestCase(unittest.TestCase, CollectionTestMixin):
896 def test_replication_desired_kept_on_load(self):
897 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
898 c1 = Collection(m, replication_desired=1)
900 loc = c1.manifest_locator()
902 self.assertEqual(c1.manifest_text(strip=True), c2.manifest_text(strip=True))
903 self.assertEqual(c1.replication_desired, c2.replication_desired)
905 def test_replication_desired_not_loaded_if_provided(self):
906 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
907 c1 = Collection(m, replication_desired=1)
909 loc = c1.manifest_locator()
910 c2 = Collection(loc, replication_desired=2)
911 self.assertEqual(c1.manifest_text(strip=True), c2.manifest_text(strip=True))
912 self.assertNotEqual(c1.replication_desired, c2.replication_desired)
914 def test_storage_classes_desired_kept_on_load(self):
915 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
916 c1 = Collection(m, storage_classes_desired=['archival'])
918 loc = c1.manifest_locator()
920 self.assertEqual(c1.manifest_text(strip=True), c2.manifest_text(strip=True))
921 self.assertEqual(c1.storage_classes_desired(), c2.storage_classes_desired())
923 def test_storage_classes_change_after_save(self):
924 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
925 c1 = Collection(m, storage_classes_desired=['archival'])
927 loc = c1.manifest_locator()
929 self.assertEqual(['archival'], c2.storage_classes_desired())
930 c2.save(storage_classes=['highIO'])
931 self.assertEqual(['highIO'], c2.storage_classes_desired())
933 self.assertEqual(c1.manifest_text(strip=True), c3.manifest_text(strip=True))
934 self.assertEqual(['highIO'], c3.storage_classes_desired())
936 def test_storage_classes_desired_not_loaded_if_provided(self):
937 m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
938 c1 = Collection(m, storage_classes_desired=['archival'])
940 loc = c1.manifest_locator()
941 c2 = Collection(loc, storage_classes_desired=['default'])
942 self.assertEqual(c1.manifest_text(strip=True), c2.manifest_text(strip=True))
943 self.assertNotEqual(c1.storage_classes_desired(), c2.storage_classes_desired())
945 def test_init_manifest(self):
946 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
947 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
948 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
950 self.assertEqual(m1, CollectionReader(m1).manifest_text(normalize=False))
951 self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", CollectionReader(m1).manifest_text(normalize=True))
953 def test_init_manifest_with_collision(self):
954 m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
955 ./md5sum.txt 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
957 with self.assertRaises(arvados.errors.ArgumentError):
958 self.assertEqual(m1, CollectionReader(m1))
960 def test_init_manifest_with_error(self):
961 m1 = """. 0:43:md5sum.txt"""
962 with self.assertRaises(arvados.errors.ArgumentError):
963 self.assertEqual(m1, CollectionReader(m1))
965 def test_remove(self):
966 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
967 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
968 self.assertIn("count1.txt", c)
969 c.remove("count1.txt")
970 self.assertNotIn("count1.txt", c)
971 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
972 with self.assertRaises(arvados.errors.ArgumentError):
975 def test_remove_recursive(self):
976 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:a/b/c/d/efg.txt 0:10:xyz.txt\n')
977 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:xyz.txt\n./a/b/c/d 781e5e245d69b566979b86e28d23f2c7+10 0:10:efg.txt\n", c.portable_manifest_text())
978 self.assertIn("a", c)
979 self.assertEqual(1, len(c["a"].keys()))
980 # cannot remove non-empty directory with default recursive=False
981 with self.assertRaises(OSError):
983 with self.assertRaises(OSError):
985 c.remove("a/b", recursive=True)
986 self.assertEqual(0, len(c["a"].keys()))
987 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:xyz.txt\n./a d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\056\n", c.portable_manifest_text())
990 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
991 self.assertIs(c.find("."), c)
992 self.assertIs(c.find("./count1.txt"), c["count1.txt"])
993 self.assertIs(c.find("count1.txt"), c["count1.txt"])
994 with self.assertRaises(IOError):
996 with self.assertRaises(arvados.errors.ArgumentError):
998 self.assertIs(c.find("./nonexistant.txt"), None)
999 self.assertIs(c.find("./nonexistantsubdir/nonexistant.txt"), None)
1001 def test_escaped_paths_dont_get_unescaped_on_manifest(self):
1002 # Dir & file names are literally '\056' (escaped form: \134056)
1003 manifest = './\\134056\\040Test d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\134056\n'
1004 c = Collection(manifest)
1005 self.assertEqual(c.portable_manifest_text(), manifest)
1007 def test_other_special_chars_on_file_token(self):
1013 ('\\134400', '\\400'),
1015 for encoded, decoded in cases:
1016 manifest = '. d41d8cd98f00b204e9800998ecf8427e+0 0:0:some%sfile.txt\n' % encoded
1017 c = Collection(manifest)
1018 self.assertEqual(c.portable_manifest_text(), manifest)
1019 self.assertIn('some%sfile.txt' % decoded, c.keys())
1021 def test_escaped_paths_do_get_unescaped_on_listing(self):
1022 # Dir & file names are literally '\056' (escaped form: \134056)
1023 manifest = './\\134056\\040Test d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\134056\n'
1024 c = Collection(manifest)
1025 self.assertIn('\\056 Test', c.keys())
1026 self.assertIn('\\056', c['\\056 Test'].keys())
1028 def test_make_empty_dir_with_escaped_chars(self):
1030 c.mkdirs('./Empty\\056Dir')
1031 self.assertEqual(c.portable_manifest_text(),
1032 './Empty\\134056Dir d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\056\n')
1034 def test_make_empty_dir_with_spaces(self):
1036 c.mkdirs('./foo bar/baz waz')
1037 self.assertEqual(c.portable_manifest_text(),
1038 './foo\\040bar/baz\\040waz d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\056\n')
1040 def test_remove_in_subdir(self):
1041 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1042 c.remove("foo/count2.txt")
1043 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo d41d8cd98f00b204e9800998ecf8427e+0 0:0:\\056\n", c.portable_manifest_text())
1045 def test_remove_empty_subdir(self):
1046 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1047 c.remove("foo/count2.txt")
1049 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
1051 def test_remove_nonempty_subdir(self):
1052 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1053 with self.assertRaises(IOError):
1055 c.remove("foo", recursive=True)
1056 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
1058 def test_copy_to_file_in_dir(self):
1059 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1060 c.copy("count1.txt", "foo/count2.txt")
1061 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
1063 def test_copy_file(self):
1064 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1065 c.copy("count1.txt", "count2.txt")
1066 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
1068 def test_copy_to_existing_dir(self):
1069 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1070 c.copy("count1.txt", "foo")
1071 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())
1073 def test_copy_to_new_dir(self):
1074 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1075 c.copy("count1.txt", "foo/")
1076 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
1078 def test_rename_file(self):
1079 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1080 c.rename("count1.txt", "count2.txt")
1081 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
1083 def test_move_file_to_dir(self):
1084 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1086 c.rename("count1.txt", "foo/count2.txt")
1087 self.assertEqual("./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
1089 def test_move_file_to_other(self):
1090 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1092 c2.rename("count1.txt", "count2.txt", source_collection=c1)
1093 self.assertEqual("", c1.manifest_text())
1094 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c2.manifest_text())
1096 def test_clone(self):
1097 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1099 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", cl.portable_manifest_text())
1101 def test_diff_del_add(self):
1102 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1103 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1105 self.assertEqual(sorted(d), [
1106 ('add', './count1.txt', c1["count1.txt"]),
1107 ('del', './count2.txt', c2["count2.txt"]),
1110 self.assertEqual(sorted(d), [
1111 ('add', './count2.txt', c2["count2.txt"]),
1112 ('del', './count1.txt', c1["count1.txt"]),
1114 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1116 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1118 def test_diff_same(self):
1119 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1120 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1122 self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1124 self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1126 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1128 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1130 def test_diff_mod(self):
1131 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1132 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
1134 self.assertEqual(d, [('mod', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1136 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1138 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1140 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1142 def test_diff_add(self):
1143 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1144 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt 10:20:count2.txt\n')
1146 self.assertEqual(sorted(d), [
1147 ('del', './count2.txt', c2["count2.txt"]),
1148 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1151 self.assertEqual(sorted(d), [
1152 ('add', './count2.txt', c2["count2.txt"]),
1153 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1156 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1158 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1160 def test_diff_add_in_subcollection(self):
1161 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1162 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1164 self.assertEqual(sorted(d), [
1165 ('del', './foo', c2["foo"]),
1166 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1169 self.assertEqual(sorted(d), [
1170 ('add', './foo', c2["foo"]),
1171 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1173 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1175 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1177 def test_diff_del_add_in_subcollection(self):
1178 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1179 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:3:count3.txt\n')
1181 self.assertEqual(sorted(d), [
1182 ('add', './foo/count2.txt', c1.find("foo/count2.txt")),
1183 ('del', './foo/count3.txt', c2.find("foo/count3.txt")),
1184 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1187 self.assertEqual(sorted(d), [
1188 ('add', './foo/count3.txt', c2.find("foo/count3.txt")),
1189 ('del', './foo/count2.txt', c1.find("foo/count2.txt")),
1190 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1193 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1195 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1197 def test_diff_mod_in_subcollection(self):
1198 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1199 c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:3:foo\n')
1201 self.assertEqual(sorted(d), [
1202 ('mod', './foo', c2["foo"], c1["foo"]),
1203 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1206 self.assertEqual(sorted(d), [
1207 ('mod', './foo', c1["foo"], c2["foo"]),
1208 ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1211 self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1213 self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1215 def test_conflict_keep_local_change(self):
1216 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1217 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1219 self.assertEqual(sorted(d), [
1220 ('add', './count2.txt', c2["count2.txt"]),
1221 ('del', './count1.txt', c1["count1.txt"]),
1223 f = c1.open("count1.txt", "wb")
1226 # c1 changed, so it should not be deleted.
1228 self.assertEqual(c1.portable_manifest_text(), ". 95ebc3c7b3b9f1d2c40fec14415d3cb8+5 5348b82a029fd9e971a811ce1f71360b+43 0:5:count1.txt 5:10:count2.txt\n")
1230 def test_conflict_mod(self):
1231 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1232 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1234 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1235 f = c1.open("count1.txt", "wb")
1238 # c1 changed, so c2 mod will go to a conflict file
1241 c1.portable_manifest_text(),
1242 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~$")
1244 def test_conflict_add(self):
1245 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1246 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
1248 self.assertEqual(sorted(d), [
1249 ('add', './count1.txt', c2["count1.txt"]),
1250 ('del', './count2.txt', c1["count2.txt"]),
1252 f = c1.open("count1.txt", "wb")
1255 # c1 added count1.txt, so c2 add will go to a conflict file
1258 c1.portable_manifest_text(),
1259 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~$")
1261 def test_conflict_del(self):
1262 c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1263 c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1265 self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1266 c1.remove("count1.txt")
1268 # c1 deleted, so c2 mod will go to a conflict file
1271 c1.portable_manifest_text(),
1272 r"\. 5348b82a029fd9e971a811ce1f71360b\+43 0:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1274 def test_notify(self):
1277 c1.subscribe(lambda event, collection, name, item: events.append((event, collection, name, item)))
1278 f = c1.open("foo.txt", "wb")
1279 self.assertEqual(events[0], (arvados.collection.ADD, c1, "foo.txt", f.arvadosfile))
1281 def test_open_w(self):
1282 c1 = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n")
1283 self.assertEqual(c1["count1.txt"].size(), 10)
1284 c1.open("count1.txt", "wb").close()
1285 self.assertEqual(c1["count1.txt"].size(), 0)
1288 class NewCollectionTestCaseWithServersAndTokens(run_test_server.TestCaseWithServers):
1291 local_locator_re = r"[0-9a-f]{32}\+\d+\+A[a-f0-9]{40}@[a-f0-9]{8}"
1292 remote_locator_re = r"[0-9a-f]{32}\+\d+\+R[a-z]{5}-[a-f0-9]{40}@[a-f0-9]{8}"
1295 self.keep_put = getattr(arvados.keep.KeepClient, 'put')
1297 @mock.patch('arvados.keep.KeepClient.put', autospec=True)
1298 def test_storage_classes_desired(self, put_mock):
1299 put_mock.side_effect = self.keep_put
1300 c = Collection(storage_classes_desired=['default'])
1301 with c.open("file.txt", 'wb') as f:
1304 _, kwargs = put_mock.call_args
1305 self.assertEqual(['default'], kwargs['classes'])
1307 @mock.patch('arvados.keep.KeepClient.put', autospec=True)
1308 def test_repacked_block_submission_get_permission_token(self, mocked_put):
1310 Make sure that those blocks that are committed after repacking small ones,
1311 get their permission tokens assigned on the collection manifest.
1313 def wrapped_keep_put(*args, **kwargs):
1314 # Simulate slow put operations
1316 return self.keep_put(*args, **kwargs)
1318 mocked_put.side_effect = wrapped_keep_put
1320 # Write 70 files ~1MiB each so we force to produce 1 big block by repacking
1321 # small ones before finishing the upload.
1323 f = c.open("file_{}.txt".format(i), 'wb')
1324 f.write(random.choice('abcdefghijklmnopqrstuvwxyz') * (2**20+i))
1325 f.close(flush=False)
1326 # We should get 2 blocks with their tokens
1327 self.assertEqual(len(re.findall(self.local_locator_re, c.manifest_text())), 2)
1329 @mock.patch('arvados.keep.KeepClient.refresh_signature')
1330 def test_copy_remote_blocks_on_save_new(self, rs_mock):
1331 remote_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+Remote-" + "a" * 40 + "@abcdef01"
1332 local_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+A" + "b" * 40 + "@abcdef01"
1333 rs_mock.return_value = local_block_loc
1334 c = Collection(". " + remote_block_loc + " 0:3:foofile.txt\n")
1336 len(re.findall(self.remote_locator_re, c.manifest_text())), 1)
1338 len(re.findall(self.local_locator_re, c.manifest_text())), 0)
1340 rs_mock.assert_called()
1342 len(re.findall(self.remote_locator_re, c.manifest_text())), 0)
1344 len(re.findall(self.local_locator_re, c.manifest_text())), 1)
1346 @mock.patch('arvados.keep.KeepClient.refresh_signature')
1347 def test_copy_remote_blocks_on_save(self, rs_mock):
1348 remote_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+Remote-" + "a" * 40 + "@abcdef01"
1349 local_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+A" + "b" * 40 + "@abcdef01"
1350 rs_mock.return_value = local_block_loc
1352 remote_c = Collection(". " + remote_block_loc + " 0:3:foofile.txt\n")
1354 len(re.findall(self.remote_locator_re, remote_c.manifest_text())), 1)
1356 local_c = Collection()
1357 with local_c.open('barfile.txt', 'wb') as f:
1361 len(re.findall(self.local_locator_re, local_c.manifest_text())), 1)
1363 len(re.findall(self.remote_locator_re, local_c.manifest_text())), 0)
1364 # Copy remote file to local collection
1365 local_c.copy('./foofile.txt', './copied/foofile.txt', remote_c)
1367 len(re.findall(self.local_locator_re, local_c.manifest_text())), 1)
1369 len(re.findall(self.remote_locator_re, local_c.manifest_text())), 1)
1370 # Save local collection: remote block should be copied
1372 rs_mock.assert_called()
1374 len(re.findall(self.local_locator_re, local_c.manifest_text())), 2)
1376 len(re.findall(self.remote_locator_re, local_c.manifest_text())), 0)
1379 class NewCollectionTestCaseWithServers(run_test_server.TestCaseWithServers):
1380 def test_preserve_version_on_save(self):
1382 c.save_new(preserve_version=True)
1383 coll_record = arvados.api().collections().get(uuid=c.manifest_locator()).execute()
1384 self.assertEqual(coll_record['version'], 1)
1385 self.assertEqual(coll_record['preserve_version'], True)
1386 with c.open("foo.txt", "wb") as foo:
1388 c.save(preserve_version=True)
1389 coll_record = arvados.api().collections().get(uuid=c.manifest_locator()).execute()
1390 self.assertEqual(coll_record['version'], 2)
1391 self.assertEqual(coll_record['preserve_version'], True)
1392 with c.open("bar.txt", "wb") as foo:
1394 c.save(preserve_version=False)
1395 coll_record = arvados.api().collections().get(uuid=c.manifest_locator()).execute()
1396 self.assertEqual(coll_record['version'], 3)
1397 self.assertEqual(coll_record['preserve_version'], False)
1399 def test_get_manifest_text_only_committed(self):
1401 with c.open("count.txt", "wb") as f:
1402 # One file committed
1403 with c.open("foo.txt", "wb") as foo:
1405 foo.flush() # Force block commit
1406 f.write(b"0123456789")
1407 # Other file not committed. Block not written to keep yet.
1409 c._get_manifest_text(".",
1412 only_committed=True),
1413 '. acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:count.txt 0:3:foo.txt\n')
1414 # And now with the file closed...
1415 f.flush() # Force block commit
1417 c._get_manifest_text(".",
1420 only_committed=True),
1421 ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:10:count.txt 10:3:foo.txt\n")
1423 def test_only_small_blocks_are_packed_together(self):
1425 # Write a couple of small files,
1426 f = c.open("count.txt", "wb")
1427 f.write(b"0123456789")
1428 f.close(flush=False)
1429 foo = c.open("foo.txt", "wb")
1431 foo.close(flush=False)
1432 # Then, write a big file, it shouldn't be packed with the ones above
1433 big = c.open("bigfile.txt", "wb")
1434 big.write(b"x" * 1024 * 1024 * 33) # 33 MB > KEEP_BLOCK_SIZE/2
1435 big.close(flush=False)
1437 c.manifest_text("."),
1438 '. 2d303c138c118af809f39319e5d507e9+34603008 a8430a058b8fbf408e1931b794dbd6fb+13 0:34603008:bigfile.txt 34603008:10:count.txt 34603018:3:foo.txt\n')
1440 def test_flush_after_small_block_packing(self):
1442 # Write a couple of small files,
1443 f = c.open("count.txt", "wb")
1444 f.write(b"0123456789")
1445 f.close(flush=False)
1446 foo = c.open("foo.txt", "wb")
1448 foo.close(flush=False)
1452 '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1454 f = c.open("count.txt", "rb+")
1459 '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1461 def test_write_after_small_block_packing2(self):
1463 # Write a couple of small files,
1464 f = c.open("count.txt", "wb")
1465 f.write(b"0123456789")
1466 f.close(flush=False)
1467 foo = c.open("foo.txt", "wb")
1469 foo.close(flush=False)
1473 '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1475 f = c.open("count.txt", "rb+")
1477 f.close(flush=False)
1481 '. 900150983cd24fb0d6963f7d28e17f72+3 a8430a058b8fbf408e1931b794dbd6fb+13 0:3:count.txt 6:7:count.txt 13:3:foo.txt\n')
1484 def test_small_block_packing_with_overwrite(self):
1486 c.open("b1", "wb").close()
1487 c["b1"].writeto(0, b"b1", 0)
1489 c.open("b2", "wb").close()
1490 c["b2"].writeto(0, b"b2", 0)
1492 c["b1"].writeto(0, b"1b", 0)
1494 self.assertEqual(c.manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 0:2:b1 2:2:b2\n")
1495 self.assertEqual(c["b1"].manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 0:2:b1\n")
1496 self.assertEqual(c["b2"].manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 2:2:b2\n")
1499 class CollectionCreateUpdateTest(run_test_server.TestCaseWithServers):
1503 def create_count_txt(self):
1504 # Create an empty collection, save it to the API server, then write a
1505 # file, but don't save it.
1508 c.save_new("CollectionCreateUpdateTest", ensure_unique_name=True)
1509 self.assertEqual(c.portable_data_hash(), "d41d8cd98f00b204e9800998ecf8427e+0")
1510 self.assertEqual(c.api_response()["portable_data_hash"], "d41d8cd98f00b204e9800998ecf8427e+0" )
1512 with c.open("count.txt", "wb") as f:
1513 f.write(b"0123456789")
1515 self.assertEqual(c.portable_manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1519 def test_create_and_save(self):
1520 c = self.create_count_txt()
1521 c.save(properties={'type' : 'Intermediate'},
1522 storage_classes=['archive'],
1523 trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
1527 r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1528 self.assertEqual(c.api_response()["storage_classes_desired"], ['archive'])
1529 self.assertEqual(c.api_response()["properties"], {'type' : 'Intermediate'})
1530 self.assertEqual(c.api_response()["trash_at"], '2111-01-01T11:11:11.111111000Z')
1533 def test_create_and_save_new(self):
1534 c = self.create_count_txt()
1535 c.save_new(properties={'type' : 'Intermediate'},
1536 storage_classes=['archive'],
1537 trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
1541 r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1542 self.assertEqual(c.api_response()["storage_classes_desired"], ['archive'])
1543 self.assertEqual(c.api_response()["properties"], {'type' : 'Intermediate'})
1544 self.assertEqual(c.api_response()["trash_at"], '2111-01-01T11:11:11.111111000Z')
1546 def test_create_and_save_after_commiting(self):
1547 c = self.create_count_txt()
1548 c.save(properties={'type' : 'Intermediate'},
1549 storage_classes=['hot'],
1550 trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
1551 c.save(properties={'type' : 'Output'},
1552 storage_classes=['cold'],
1553 trash_at=datetime.datetime(2222, 2, 2, 22, 22, 22, 222222))
1555 self.assertEqual(c.api_response()["storage_classes_desired"], ['cold'])
1556 self.assertEqual(c.api_response()["properties"], {'type' : 'Output'})
1557 self.assertEqual(c.api_response()["trash_at"], '2222-02-02T22:22:22.222222000Z')
1559 def test_create_diff_apply(self):
1560 c1 = self.create_count_txt()
1563 c2 = Collection(c1.manifest_locator())
1564 with c2.open("count.txt", "wb") as f:
1569 self.assertEqual(diff[0], (arvados.collection.MOD, u'./count.txt', c1["count.txt"], c2["count.txt"]))
1572 self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1574 def test_diff_apply_with_token(self):
1575 baseline = CollectionReader(". 781e5e245d69b566979b86e28d23f2c7+10+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:10:count.txt\n")
1576 c = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1577 other = CollectionReader(". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1579 diff = baseline.diff(other)
1580 self.assertEqual(diff, [('mod', u'./count.txt', c["count.txt"], other["count.txt"])])
1584 self.assertEqual(c.manifest_text(), ". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1587 def test_create_and_update(self):
1588 c1 = self.create_count_txt()
1591 c2 = arvados.collection.Collection(c1.manifest_locator())
1592 with c2.open("count.txt", "wb") as f:
1597 self.assertNotEqual(c1.portable_data_hash(), c2.portable_data_hash())
1599 self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1602 def test_create_and_update_with_conflict(self):
1603 c1 = self.create_count_txt()
1606 with c1.open("count.txt", "wb") as f:
1609 c2 = arvados.collection.Collection(c1.manifest_locator())
1610 with c2.open("count.txt", "wb") as f:
1618 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~$")
1620 def test_pdh_is_native_str(self):
1621 c1 = self.create_count_txt()
1622 pdh = c1.portable_data_hash()
1623 self.assertEqual(type(''), type(pdh))
1626 if __name__ == '__main__':