1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
10 from unittest import mock
13 from arvados._ranges import Range
14 from arvados.keep import KeepLocator
15 from arvados.collection import Collection
16 from arvados.arvfile import ArvadosFile, ArvadosFileReader
18 from . import arvados_testutil as tutil
19 from .test_stream import StreamFileReaderTestCase, StreamRetryTestMixin
21 class ArvadosFileWriterTestCase(unittest.TestCase):
22 class MockKeep(object):
23 def __init__(self, blocks):
26 self.num_prefetch_threads = 1
27 def get(self, locator, num_retries=0, prefetch=False):
28 self.requests.append(locator)
29 return self.blocks.get(locator)
30 def get_from_cache(self, locator):
31 self.requests.append(locator)
32 return self.blocks.get(locator)
33 def put(self, data, num_retries=None, copies=None, classes=[]):
34 pdh = tutil.str_keep_locator(data)
35 self.blocks[pdh] = bytes(data)
37 def block_prefetch(self, loc):
38 self.requests.append(loc)
40 class MockApi(object):
41 def __init__(self, b, r):
44 self._schema = ArvadosFileWriterTestCase.MockApi.MockSchema()
46 class MockSchema(object):
48 self.schemas = {'Collection': {'properties': {'replication_desired': {'type':'integer'}}}}
49 class MockCollections(object):
50 def __init__(self, b, r):
53 class Execute(object):
54 def __init__(self, r):
56 def execute(self, num_retries=None):
58 def create(self, ensure_unique_name=False, body=None):
60 raise Exception("Body %s does not match expectation %s" % (body, self.body))
61 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
62 def update(self, uuid=None, body=None):
63 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
64 def collections(self):
65 return ArvadosFileWriterTestCase.MockApi.MockCollections(self.body, self.response)
68 def test_truncate(self):
69 keep = ArvadosFileWriterTestCase.MockKeep({
70 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
72 api = ArvadosFileWriterTestCase.MockApi({
73 "name": "test_truncate",
74 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
75 "replication_desired": None,
77 "uuid": "zzzzz-4zz18-mockcollection0",
78 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
79 "portable_data_hash":"7fcd0eaac3aad4c31a6a0e756475da92+52",
81 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
82 api_client=api, keep_client=keep) as c:
83 writer = c.open("count.txt", "rb+")
84 self.assertEqual(writer.size(), 10)
85 self.assertEqual(b"0123456789", writer.read(12))
89 # Make sure reading off the end doesn't break
90 self.assertEqual(b"", writer.read(12))
92 self.assertEqual(writer.size(), 8)
93 writer.seek(0, os.SEEK_SET)
94 self.assertEqual(b"01234567", writer.read(12))
96 self.assertIsNone(c.manifest_locator())
97 self.assertTrue(c.modified())
98 c.save_new("test_truncate")
99 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
100 self.assertFalse(c.modified())
103 def test_truncate2(self):
104 keep = ArvadosFileWriterTestCase.MockKeep({
105 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
107 api = ArvadosFileWriterTestCase.MockApi({
108 "name": "test_truncate2",
109 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 7f614da9329cd3aebf59b91aadc30bf0+67108864 0:12:count.txt\n",
110 "replication_desired": None,
112 "uuid": "zzzzz-4zz18-mockcollection0",
113 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 7f614da9329cd3aebf59b91aadc30bf0+67108864 0:12:count.txt\n",
114 "portable_data_hash": "272da898abdf86ddc71994835e3155f8+95",
116 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
117 api_client=api, keep_client=keep) as c:
118 writer = c.open("count.txt", "rb+")
119 self.assertEqual(writer.size(), 10)
120 self.assertEqual(b"0123456789", writer.read(12))
125 self.assertEqual(writer.size(), 12)
126 writer.seek(0, os.SEEK_SET)
127 self.assertEqual(b"0123456789\x00\x00", writer.read(12))
129 self.assertIsNone(c.manifest_locator())
130 self.assertTrue(c.modified())
131 c.save_new("test_truncate2")
132 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
133 self.assertFalse(c.modified())
135 def test_truncate3(self):
136 keep = ArvadosFileWriterTestCase.MockKeep({
137 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
138 "a925576942e94b2ef57a066101b48876+10": b"abcdefghij",
140 api = ArvadosFileWriterTestCase.MockApi({
141 "name": "test_truncate",
142 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
143 "replication_desired": None,
145 "uuid": "zzzzz-4zz18-mockcollection0",
146 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
147 "portable_data_hash": "7fcd0eaac3aad4c31a6a0e756475da92+52",
149 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n',
150 api_client=api, keep_client=keep) as c:
151 writer = c.open("count.txt", "rb+")
152 self.assertEqual(writer.size(), 20)
153 self.assertEqual(b"0123456789ab", writer.read(12))
154 self.assertEqual(12, writer.tell())
158 # Make sure reading off the end doesn't break
159 self.assertEqual(12, writer.tell())
160 self.assertEqual(b"", writer.read(12))
162 self.assertEqual(writer.size(), 8)
163 self.assertEqual(2, writer.seek(-10, os.SEEK_CUR))
164 self.assertEqual(b"234567", writer.read(12))
166 self.assertIsNone(c.manifest_locator())
167 self.assertTrue(c.modified())
168 c.save_new("test_truncate")
169 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
170 self.assertFalse(c.modified())
172 def test_write_to_end(self):
173 keep = ArvadosFileWriterTestCase.MockKeep({
174 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
176 api = ArvadosFileWriterTestCase.MockApi({
177 "name": "test_append",
178 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n",
179 "replication_desired": None,
181 "uuid": "zzzzz-4zz18-mockcollection0",
182 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n",
183 "portable_data_hash": "c5c3af76565c8efb6a806546bcf073f3+88",
185 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
186 api_client=api, keep_client=keep) as c:
187 writer = c.open("count.txt", "rb+")
188 self.assertEqual(writer.size(), 10)
190 self.assertEqual(5, writer.seek(5, os.SEEK_SET))
191 self.assertEqual(b"56789", writer.read(8))
193 writer.seek(10, os.SEEK_SET)
195 self.assertEqual(writer.size(), 13)
197 writer.seek(5, os.SEEK_SET)
198 self.assertEqual(b"56789foo", writer.read(8))
200 self.assertIsNone(c.manifest_locator())
201 self.assertTrue(c.modified())
202 self.assertIsNone(keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
204 c.save_new("test_append")
205 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
206 self.assertFalse(c.modified())
207 self.assertEqual(b"foo", keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
210 def test_append(self):
211 keep = ArvadosFileWriterTestCase.MockKeep({
212 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
214 for (mode, convert) in (
215 ('a+', lambda data: data.decode(encoding='utf-8')),
216 ('at+', lambda data: data.decode(encoding='utf-8')),
217 ('ab+', lambda data: data)):
218 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', keep_client=keep)
219 writer = c.open("count.txt", mode)
220 self.assertEqual(writer.read(20), convert(b"0123456789"))
222 writer.seek(0, os.SEEK_SET)
223 writer.write(convert(b"hello"))
224 self.assertEqual(writer.read(), convert(b""))
226 writer.seek(-5, os.SEEK_CUR)
227 self.assertEqual(writer.read(3), convert(b"hel"))
228 self.assertEqual(writer.read(), convert(b"lo"))
230 with self.assertRaises(IOError):
231 writer.seek(-5, os.SEEK_CUR)
232 with self.assertRaises(IOError):
233 writer.seek(-3, os.SEEK_END)
234 writer.seek(0, os.SEEK_SET)
236 self.assertEqual(7, writer.tell())
237 self.assertEqual(7, writer.seek(7, os.SEEK_SET))
239 writer.seek(0, os.SEEK_SET)
240 self.assertEqual(writer.read(), convert(b"0123456789hello"))
243 writer.write(convert(b"world"))
244 self.assertEqual(writer.read(), convert(b""))
246 self.assertEqual(writer.read(), convert(b"0123456789helloworld"))
248 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 fc5e038d38a57032085441e7fe7010b0+10 0:20:count.txt\n", c.portable_manifest_text())
250 def test_write_at_beginning(self):
251 keep = ArvadosFileWriterTestCase.MockKeep({
252 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
254 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
255 keep_client=keep) as c:
256 writer = c.open("count.txt", "rb+")
257 self.assertEqual(b"0123456789", writer.readfrom(0, 13))
258 writer.seek(0, os.SEEK_SET)
260 self.assertEqual(writer.size(), 10)
261 self.assertEqual(b"foo3456789", writer.readfrom(0, 13))
262 self.assertEqual(". acbd18db4cc2f85cedef654fccc4a4d8+3 781e5e245d69b566979b86e28d23f2c7+10 0:3:count.txt 6:7:count.txt\n", c.portable_manifest_text())
264 def test_write_empty(self):
265 keep = ArvadosFileWriterTestCase.MockKeep({})
266 with Collection(keep_client=keep) as c:
267 writer = c.open("count.txt", "wb")
268 self.assertEqual(writer.size(), 0)
269 self.assertEqual(". d41d8cd98f00b204e9800998ecf8427e+0 0:0:count.txt\n", c.portable_manifest_text())
271 def test_save_manifest_text(self):
272 keep = ArvadosFileWriterTestCase.MockKeep({})
273 with Collection(keep_client=keep) as c:
274 writer = c.open("count.txt", "wb")
275 writer.write(b"0123456789")
276 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
277 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
279 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.save_new(create_collection_record=False))
280 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
282 def test_get_manifest_text_commits(self):
283 keep = ArvadosFileWriterTestCase.MockKeep({})
284 with Collection(keep_client=keep) as c:
285 writer = c.open("count.txt", "wb")
286 writer.write("0123456789")
287 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
288 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
289 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.manifest_text())
290 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
293 def test_write_in_middle(self):
294 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": b"0123456789"})
295 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
296 keep_client=keep) as c:
297 writer = c.open("count.txt", "rb+")
298 self.assertEqual(b"0123456789", writer.readfrom(0, 13))
299 writer.seek(3, os.SEEK_SET)
301 self.assertEqual(writer.size(), 10)
302 self.assertEqual(b"012foo6789", writer.readfrom(0, 13))
303 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:count.txt 10:3:count.txt 6:4:count.txt\n", c.portable_manifest_text())
305 def test_write_at_end(self):
306 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": b"0123456789"})
307 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
308 keep_client=keep) as c:
309 writer = c.open("count.txt", "rb+")
310 self.assertEqual(b"0123456789", writer.readfrom(0, 13))
311 writer.seek(7, os.SEEK_SET)
313 self.assertEqual(writer.size(), 10)
314 self.assertEqual(b"0123456foo", writer.readfrom(0, 13))
315 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:7:count.txt 10:3:count.txt\n", c.portable_manifest_text())
317 def test_write_across_segment_boundary(self):
318 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": b"0123456789"})
319 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt 0:10:count.txt\n',
320 keep_client=keep) as c:
321 writer = c.open("count.txt", "rb+")
322 self.assertEqual(b"012345678901234", writer.readfrom(0, 15))
323 writer.seek(7, os.SEEK_SET)
324 writer.write("foobar")
325 self.assertEqual(writer.size(), 20)
326 self.assertEqual(b"0123456foobar34", writer.readfrom(0, 15))
327 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 3858f62230ac3c915f300c664312c63f+6 0:7:count.txt 10:6:count.txt 3:7:count.txt\n", c.portable_manifest_text())
329 def test_write_across_several_segments(self):
330 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": b"0123456789"})
331 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:4:count.txt 0:4:count.txt 0:4:count.txt',
332 keep_client=keep) as c:
333 writer = c.open("count.txt", "rb+")
334 self.assertEqual(b"012301230123", writer.readfrom(0, 15))
335 writer.seek(2, os.SEEK_SET)
336 writer.write("abcdefg")
337 self.assertEqual(writer.size(), 12)
338 self.assertEqual(b"01abcdefg123", writer.readfrom(0, 15))
339 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 7ac66c0f148de9519b8bd264312c4d64+7 0:2:count.txt 10:7:count.txt 1:3:count.txt\n", c.portable_manifest_text())
341 def test_write_large(self):
342 keep = ArvadosFileWriterTestCase.MockKeep({})
343 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
344 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n",
345 "replication_desired":None},
346 {"uuid":"zzzzz-4zz18-mockcollection0",
347 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n",
348 "portable_data_hash":"9132ca8e3f671c76103a38f5bc24328c+108"})
349 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
350 api_client=api, keep_client=keep) as c:
351 writer = c.open("count.txt", "rb+")
352 text = "0123456789" * 100
353 for b in range(0, 100000):
355 self.assertEqual(writer.size(), 100000000)
357 self.assertIsNone(c.manifest_locator())
358 self.assertTrue(c.modified())
359 c.save_new("test_write_large")
360 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
361 self.assertFalse(c.modified())
363 def test_large_write(self):
364 keep = ArvadosFileWriterTestCase.MockKeep({})
365 api = ArvadosFileWriterTestCase.MockApi({}, {})
366 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
367 api_client=api, keep_client=keep) as c:
368 writer = c.open("count.txt", "rb+")
369 self.assertEqual(writer.size(), 0)
373 text = "0123456789" * 9999999
375 self.assertEqual(writer.size(), 100000000)
377 self.assertEqual(c.manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 48dd23ea1645fd47d789804d71b5bb8e+67108864 77c57dc6ac5a10bb2205caaa73187994+32891126 0:100000000:count.txt\n")
379 def test_sparse_write(self):
380 keep = ArvadosFileWriterTestCase.MockKeep({})
381 api = ArvadosFileWriterTestCase.MockApi({}, {})
382 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
383 api_client=api, keep_client=keep) as c:
384 writer = c.open("count.txt", "rb+")
385 self.assertEqual(writer.size(), 0)
390 self.assertEqual(writer.size(), 12)
391 writer.seek(0, os.SEEK_SET)
392 self.assertEqual(writer.read(), b"\x00\x00"+text)
394 self.assertEqual(c.manifest_text(), ". 7f614da9329cd3aebf59b91aadc30bf0+67108864 781e5e245d69b566979b86e28d23f2c7+10 0:2:count.txt 67108864:10:count.txt\n")
396 def test_sparse_write2(self):
397 keep = ArvadosFileWriterTestCase.MockKeep({})
398 api = ArvadosFileWriterTestCase.MockApi({}, {})
399 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
400 api_client=api, keep_client=keep) as c:
401 writer = c.open("count.txt", "rb+")
402 self.assertEqual(writer.size(), 0)
405 writer.seek((arvados.config.KEEP_BLOCK_SIZE*2) + 2)
407 self.assertEqual(writer.size(), (arvados.config.KEEP_BLOCK_SIZE*2) + 12)
408 writer.seek(0, os.SEEK_SET)
410 self.assertEqual(c.manifest_text(), ". 7f614da9329cd3aebf59b91aadc30bf0+67108864 781e5e245d69b566979b86e28d23f2c7+10 0:67108864:count.txt 0:67108864:count.txt 0:2:count.txt 67108864:10:count.txt\n")
412 def test_sparse_write3(self):
413 keep = ArvadosFileWriterTestCase.MockKeep({})
414 api = ArvadosFileWriterTestCase.MockApi({}, {})
415 for r in [[0, 1, 2, 3, 4], [4, 3, 2, 1, 0], [3, 2, 0, 4, 1]]:
416 with Collection(api_client=api, keep_client=keep) as c:
417 writer = c.open("count.txt", "rb+")
418 self.assertEqual(writer.size(), 0)
423 writer.write(w.encode())
425 self.assertEqual(writer.read(), b"00000000001111111111222222222233333333334444444444")
427 def test_sparse_write4(self):
428 keep = ArvadosFileWriterTestCase.MockKeep({})
429 api = ArvadosFileWriterTestCase.MockApi({}, {})
430 for r in [[0, 1, 2, 4], [4, 2, 1, 0], [2, 0, 4, 1]]:
431 with Collection(api_client=api, keep_client=keep) as c:
432 writer = c.open("count.txt", "rb+")
433 self.assertEqual(writer.size(), 0)
438 writer.write(w.encode())
440 self.assertEqual(writer.read(), b"000000000011111111112222222222\x00\x00\x00\x00\x00\x00\x00\x00\x00\x004444444444")
442 def test_rewrite_on_empty_file(self):
443 keep = ArvadosFileWriterTestCase.MockKeep({})
444 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
445 keep_client=keep) as c:
446 writer = c.open("count.txt", "rb+")
447 for b in range(0, 10):
448 writer.seek(0, os.SEEK_SET)
449 writer.write("0123456789")
451 self.assertEqual(writer.size(), 10)
452 self.assertEqual(b"0123456789", writer.readfrom(0, 20))
453 self.assertEqual(". 7a08b07e84641703e5f2c836aa59a170+100 90:10:count.txt\n", c.portable_manifest_text())
455 self.assertEqual(writer.size(), 10)
456 self.assertEqual(b"0123456789", writer.readfrom(0, 20))
457 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n", c.portable_manifest_text())
459 def test_rewrite_append_existing_file(self):
460 keep = ArvadosFileWriterTestCase.MockKeep({
461 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
463 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
464 keep_client=keep) as c:
465 writer = c.open("count.txt", "rb+")
466 for b in range(0, 10):
467 writer.seek(10, os.SEEK_SET)
468 writer.write("abcdefghij")
470 self.assertEqual(writer.size(), 20)
471 self.assertEqual(b"0123456789abcdefghij", writer.readfrom(0, 20))
472 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:10:count.txt 100:10:count.txt\n", c.portable_manifest_text())
474 writer.arvadosfile.flush()
475 self.assertEqual(writer.size(), 20)
476 self.assertEqual(b"0123456789abcdefghij", writer.readfrom(0, 20))
477 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n", c.portable_manifest_text())
479 def test_rewrite_over_existing_file(self):
480 keep = ArvadosFileWriterTestCase.MockKeep({
481 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
483 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
484 keep_client=keep) as c:
485 writer = c.open("count.txt", "rb+")
486 for b in range(0, 10):
487 writer.seek(5, os.SEEK_SET)
488 writer.write("abcdefghij")
490 self.assertEqual(writer.size(), 15)
491 self.assertEqual(b"01234abcdefghij", writer.readfrom(0, 20))
492 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:5:count.txt 100:10:count.txt\n", c.portable_manifest_text())
494 writer.arvadosfile.flush()
496 self.assertEqual(writer.size(), 15)
497 self.assertEqual(b"01234abcdefghij", writer.readfrom(0, 20))
498 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:5:count.txt 10:10:count.txt\n", c.portable_manifest_text())
500 def test_write_large_rewrite(self):
501 keep = ArvadosFileWriterTestCase.MockKeep({})
502 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
503 "manifest_text": ". 3dc0d4bc21f48060bedcb2c91af4f906+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 0:3:count.txt 32892006:67107997:count.txt 3:32892000:count.txt\n",
504 "replication_desired":None},
505 {"uuid":"zzzzz-4zz18-mockcollection0",
506 "manifest_text": ". 3dc0d4bc21f48060bedcb2c91af4f906+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 0:3:count.txt 32892006:67107997:count.txt 3:32892000:count.txt\n",
507 "portable_data_hash":"217665c6b713e1b78dfba7ebd42344db+156"})
508 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
509 api_client=api, keep_client=keep) as c:
510 writer = c.open("count.txt", "rb+")
511 text = b''.join([b"0123456789" for a in range(0, 100)])
512 for b in range(0, 100000):
514 writer.seek(0, os.SEEK_SET)
516 self.assertEqual(writer.size(), 100000000)
518 self.assertIsNone(c.manifest_locator())
519 self.assertTrue(c.modified())
520 c.save_new("test_write_large")
521 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
522 self.assertFalse(c.modified())
524 def test_create(self):
525 keep = ArvadosFileWriterTestCase.MockKeep({})
526 api = ArvadosFileWriterTestCase.MockApi({
527 "name":"test_create",
528 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
529 "replication_desired":None,
531 "uuid":"zzzzz-4zz18-mockcollection0",
532 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
533 "portable_data_hash":"7a461a8c58601798f690f8b368ac4423+51",
535 with Collection(api_client=api, keep_client=keep) as c:
536 writer = c.open("count.txt", "wb+")
537 self.assertEqual(writer.size(), 0)
538 writer.write("01234567")
539 self.assertEqual(writer.size(), 8)
541 self.assertIsNone(c.manifest_locator())
542 self.assertTrue(c.modified())
543 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
544 c.save_new("test_create")
545 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
546 self.assertFalse(c.modified())
547 self.assertEqual(b"01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
550 def test_create_subdir(self):
551 keep = ArvadosFileWriterTestCase.MockKeep({})
552 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
553 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
554 "replication_desired":None},
555 {"uuid":"zzzzz-4zz18-mockcollection0",
556 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
557 "portable_data_hash":"1b02aaa62528d28a5be41651cbb9d7c7+59"})
558 with Collection(api_client=api, keep_client=keep) as c:
559 self.assertIsNone(c.api_response())
560 writer = c.open("foo/bar/count.txt", "wb+")
561 writer.write("01234567")
562 self.assertFalse(c.committed())
563 c.save_new("test_create")
564 self.assertTrue(c.committed())
565 self.assertEqual(c.api_response(), api.response)
567 def test_overwrite(self):
568 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
569 api = ArvadosFileWriterTestCase.MockApi({"name":"test_overwrite",
570 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
571 "replication_desired":None},
572 {"uuid":"zzzzz-4zz18-mockcollection0",
573 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
574 "portable_data_hash":"7a461a8c58601798f690f8b368ac4423+51"})
575 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
576 api_client=api, keep_client=keep) as c:
577 writer = c.open("count.txt", "wb+")
578 self.assertEqual(writer.size(), 0)
579 writer.write("01234567")
580 self.assertEqual(writer.size(), 8)
582 self.assertIsNone(c.manifest_locator())
583 self.assertTrue(c.modified())
584 c.save_new("test_overwrite")
585 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
586 self.assertFalse(c.modified())
588 def test_file_not_found(self):
589 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
590 with self.assertRaises(IOError):
591 writer = c.open("nocount.txt", "rb")
593 def test_cannot_open_directory(self):
594 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
595 with self.assertRaises(IOError):
596 writer = c.open(".", "rb")
598 def test_create_multiple(self):
599 keep = ArvadosFileWriterTestCase.MockKeep({})
600 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create_multiple",
601 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n",
602 "replication_desired":None},
603 {"uuid":"zzzzz-4zz18-mockcollection0",
604 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n",
605 "portable_data_hash":"71e7bb6c00d31fc2b4364199fd97be08+102"})
606 with Collection(api_client=api, keep_client=keep) as c:
607 w1 = c.open("count1.txt", "wb")
608 w2 = c.open("count2.txt", "wb")
611 self.assertEqual(w1.size(), 8)
612 self.assertEqual(w2.size(), 8)
614 self.assertIsNone(c.manifest_locator())
615 self.assertTrue(c.modified())
616 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
617 c.save_new("test_create_multiple")
618 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
619 self.assertFalse(c.modified())
620 self.assertEqual(b"01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
623 class ArvadosFileReaderTestCase(StreamFileReaderTestCase):
624 class MockParent(object):
625 class MockBlockMgr(object):
626 def __init__(self, blocks, nocache):
628 self.nocache = nocache
629 self._keep = ArvadosFileWriterTestCase.MockKeep({})
630 self.prefetch_lookahead = 0
632 def block_prefetch(self, loc):
635 def get_block_contents(self, loc, num_retries=0, cache_only=False):
636 if self.nocache and cache_only:
638 return self.blocks[loc]
640 def __init__(self, blocks, nocache):
642 self.nocache = nocache
643 self.lock = arvados.arvfile.NoopLock()
645 def root_collection(self):
648 def _my_block_manager(self):
649 return ArvadosFileReaderTestCase.MockParent.MockBlockMgr(self.blocks, self.nocache)
652 def make_count_reader(self, nocache=False):
656 for d in [b'01234', b'34567', b'67890']:
657 loc = tutil.str_keep_locator(d)
659 stream.append(Range(loc, n, len(d)))
661 af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache), "count.txt", stream=stream, segments=[Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)])
662 return ArvadosFileReader(af, mode="rb")
664 def test_read_block_crossing_behavior(self):
665 # read() needs to return all the data requested if possible, even if it
666 # crosses uncached blocks: https://arvados.org/issues/5856
667 sfile = self.make_count_reader(nocache=True)
668 self.assertEqual(b'12345678', sfile.read(8))
670 def test_successive_reads(self):
671 # Override StreamFileReaderTestCase.test_successive_reads
672 sfile = self.make_count_reader(nocache=True)
673 self.assertEqual(b'1234', sfile.read(4))
674 self.assertEqual(b'5678', sfile.read(4))
675 self.assertEqual(b'9', sfile.read(4))
676 self.assertEqual(b'', sfile.read(4))
678 def test_tell_after_block_read(self):
679 # Override StreamFileReaderTestCase.test_tell_after_block_read
680 sfile = self.make_count_reader(nocache=True)
681 self.assertEqual(b'12345678', sfile.read(8))
682 self.assertEqual(8, sfile.tell())
684 def test_prefetch(self):
685 keep = ArvadosFileWriterTestCase.MockKeep({
686 "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
687 "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
689 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
690 r = c.open("count.txt", "rb")
691 self.assertEqual(b"0123", r.read(4))
692 self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
693 "e8dc4081b13434b45189a720b77b6818+8"], keep.requests)
695 def test_prefetch_disabled(self):
696 keep = ArvadosFileWriterTestCase.MockKeep({
697 "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
698 "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
700 keep.num_prefetch_threads = 0
701 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
702 r = c.open("count.txt", "rb")
703 self.assertEqual(b"0123", r.read(4))
705 self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8"], keep.requests)
707 def test_prefetch_first_read_only(self):
708 # test behavior that prefetch only happens every 128 reads
709 # check that it doesn't make a prefetch request on the second read
710 keep = ArvadosFileWriterTestCase.MockKeep({
711 "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
712 "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
714 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
715 r = c.open("count.txt", "rb")
716 self.assertEqual(b"0123", r.read(4))
717 self.assertEqual(b"45", r.read(2))
718 self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
719 "e8dc4081b13434b45189a720b77b6818+8",
720 "2e9ec317e197819358fbc43afca7d837+8"], keep.requests)
721 self.assertEqual(3, len(keep.requests))
723 def test_prefetch_again(self):
724 # test behavior that prefetch only happens every 128 reads
725 # check that it does make another prefetch request after 128 reads
726 keep = ArvadosFileWriterTestCase.MockKeep({
727 "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
728 "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
730 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
731 r = c.open("count.txt", "rb")
732 for i in range(0, 129):
734 self.assertEqual(b"0123", r.read(4))
735 self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
736 "e8dc4081b13434b45189a720b77b6818+8",
737 "2e9ec317e197819358fbc43afca7d837+8",
738 "2e9ec317e197819358fbc43afca7d837+8"], keep.requests[0:4])
739 self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
740 "2e9ec317e197819358fbc43afca7d837+8",
741 "2e9ec317e197819358fbc43afca7d837+8",
742 "e8dc4081b13434b45189a720b77b6818+8"], keep.requests[127:131])
743 # gets the 1st block 129 times from keep (cache),
744 # and the 2nd block twice to get 131 requests
745 self.assertEqual(131, len(keep.requests))
747 def test__eq__from_manifest(self):
748 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
749 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c2:
750 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
751 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
753 def test__eq__from_writes(self):
754 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
755 with Collection() as c2:
756 f = c2.open("count1.txt", "wb")
757 f.write("0123456789")
759 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
760 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
762 def test__ne__(self):
763 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
764 with Collection() as c2:
765 f = c2.open("count1.txt", "wb")
766 f.write("1234567890")
768 self.assertTrue(c1["count1.txt"] != c2["count1.txt"])
769 self.assertFalse(c1["count1.txt"] == c2["count1.txt"])
772 class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
773 def reader_for(self, coll_name, **kwargs):
777 for d in self.manifest_for(coll_name).split():
780 segments.append(Range(n, n, k.size))
781 stream.append(Range(d, n, k.size))
786 blockmanager = arvados.arvfile._BlockManager(self.keep_client())
787 blockmanager.prefetch_enabled = False
788 col = Collection(keep_client=self.keep_client(), block_manager=blockmanager)
789 af = ArvadosFile(col, "test",
792 kwargs.setdefault('mode', 'rb')
793 return ArvadosFileReader(af, **kwargs)
795 def read_for_test(self, reader, byte_count, **kwargs):
796 return reader.read(byte_count, **kwargs)
799 class ArvadosFileReadFromTestCase(ArvadosFileReadTestCase):
800 def read_for_test(self, reader, byte_count, **kwargs):
801 return reader.readfrom(0, byte_count, **kwargs)
804 class ArvadosFileReadAllTestCase(ArvadosFileReadTestCase):
805 def read_for_test(self, reader, byte_count, **kwargs):
806 return b''.join(reader.readall(**kwargs))
809 class ArvadosFileReadAllDecompressedTestCase(ArvadosFileReadTestCase):
810 def read_for_test(self, reader, byte_count, **kwargs):
811 return b''.join(reader.readall_decompressed(**kwargs))
814 class ArvadosFileReadlinesTestCase(ArvadosFileReadTestCase):
815 def read_for_test(self, reader, byte_count, **kwargs):
816 return ''.join(reader.readlines(**kwargs)).encode()
819 class ArvadosFileTestCase(unittest.TestCase):
820 def datetime_to_hex(self, dt):
821 return hex(int(time.mktime(dt.timetuple())))[2:]
823 def test_permission_expired(self):
824 base_manifest = ". 781e5e245d69b566979b86e28d23f2c7+10+A715fd31f8111894f717eb1003c1b0216799dd9ec@{} 0:10:count.txt\n"
825 now = datetime.datetime.now()
826 a_week_ago = now - datetime.timedelta(days=7)
827 a_month_ago = now - datetime.timedelta(days=30)
828 a_week_from_now = now + datetime.timedelta(days=7)
829 with Collection(base_manifest.format(self.datetime_to_hex(a_week_from_now))) as c:
830 self.assertFalse(c.find('count.txt').permission_expired())
831 with Collection(base_manifest.format(self.datetime_to_hex(a_week_ago))) as c:
832 f = c.find('count.txt')
833 self.assertTrue(f.permission_expired())
834 self.assertTrue(f.permission_expired(a_week_from_now))
835 self.assertFalse(f.permission_expired(a_month_ago))
838 class BlockManagerTest(unittest.TestCase):
839 def test_bufferblock_append(self):
840 keep = ArvadosFileWriterTestCase.MockKeep({})
841 with arvados.arvfile._BlockManager(keep) as blockmanager:
842 bufferblock = blockmanager.alloc_bufferblock()
843 bufferblock.append("foo")
845 self.assertEqual(bufferblock.size(), 3)
846 self.assertEqual(bufferblock.buffer_view[0:3], b"foo")
847 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
849 bufferblock.append("bar")
851 self.assertEqual(bufferblock.size(), 6)
852 self.assertEqual(bufferblock.buffer_view[0:6], b"foobar")
853 self.assertEqual(bufferblock.locator(), "3858f62230ac3c915f300c664312c63f+6")
855 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
856 with self.assertRaises(arvados.errors.AssertionError):
857 bufferblock.append("bar")
859 def test_bufferblock_dup(self):
860 keep = ArvadosFileWriterTestCase.MockKeep({})
861 with arvados.arvfile._BlockManager(keep) as blockmanager:
862 bufferblock = blockmanager.alloc_bufferblock()
863 bufferblock.append("foo")
865 self.assertEqual(bufferblock.size(), 3)
866 self.assertEqual(bufferblock.buffer_view[0:3], b"foo")
867 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
868 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
870 bufferblock2 = blockmanager.dup_block(bufferblock, None)
871 self.assertNotEqual(bufferblock.blockid, bufferblock2.blockid)
873 bufferblock2.append("bar")
875 self.assertEqual(bufferblock2.size(), 6)
876 self.assertEqual(bufferblock2.buffer_view[0:6], b"foobar")
877 self.assertEqual(bufferblock2.locator(), "3858f62230ac3c915f300c664312c63f+6")
879 self.assertEqual(bufferblock.size(), 3)
880 self.assertEqual(bufferblock.buffer_view[0:3], b"foo")
881 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
883 def test_bufferblock_get(self):
884 keep = ArvadosFileWriterTestCase.MockKeep({
885 "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
887 with arvados.arvfile._BlockManager(keep) as blockmanager:
888 bufferblock = blockmanager.alloc_bufferblock()
889 bufferblock.append("foo")
891 self.assertEqual(blockmanager.get_block_contents("781e5e245d69b566979b86e28d23f2c7+10", 1), b"0123456789")
892 self.assertEqual(blockmanager.get_block_contents(bufferblock.blockid, 1), b"foo")
894 def test_bufferblock_commit(self):
895 mockkeep = mock.MagicMock()
896 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
897 bufferblock = blockmanager.alloc_bufferblock()
898 bufferblock.owner = mock.MagicMock(spec=arvados.arvfile.ArvadosFile)
899 def flush(sync=None):
900 blockmanager.commit_bufferblock(bufferblock, sync)
901 bufferblock.owner.flush.side_effect = flush
902 bufferblock.append("foo")
903 blockmanager.commit_all()
904 self.assertTrue(bufferblock.owner.flush.called)
905 self.assertTrue(mockkeep.put.called)
906 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
907 self.assertIsNone(bufferblock.buffer_view)
909 def test_bufferblock_commit_pending(self):
911 mockkeep = mock.MagicMock()
912 mockkeep.put.side_effect = lambda *args, **kwargs: time.sleep(1)
913 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
914 bufferblock = blockmanager.alloc_bufferblock()
915 bufferblock.append("foo")
917 blockmanager.commit_bufferblock(bufferblock, False)
918 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.PENDING)
920 blockmanager.commit_bufferblock(bufferblock, True)
921 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
923 def test_bufferblock_commit_with_error(self):
924 mockkeep = mock.MagicMock()
925 mockkeep.put.side_effect = arvados.errors.KeepWriteError("fail")
926 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
927 bufferblock = blockmanager.alloc_bufferblock()
928 bufferblock.owner = mock.MagicMock(spec=arvados.arvfile.ArvadosFile)
929 def flush(sync=None):
930 blockmanager.commit_bufferblock(bufferblock, sync)
931 bufferblock.owner.flush.side_effect = flush
932 bufferblock.append("foo")
933 with self.assertRaises(arvados.errors.KeepWriteError) as err:
934 blockmanager.commit_all()
935 self.assertTrue(bufferblock.owner.flush.called)
936 self.assertEqual(str(err.exception), "Error writing some blocks: block acbd18db4cc2f85cedef654fccc4a4d8+3 raised KeepWriteError (fail)")
937 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.ERROR)