Merge branch '15397-remove-obsolete-apis'
[arvados.git] / sdk / python / tests / test_arvfile.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import datetime
6 import os
7 import time
8 import unittest
9
10 from unittest import mock
11
12 import arvados
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
17
18 from . import arvados_testutil as tutil
19 from .test_stream import StreamFileReaderTestMixin, StreamRetryTestMixin
20
21 class ArvadosFileWriterTestCase(unittest.TestCase):
22     class MockKeep(object):
23         def __init__(self, blocks):
24             self.blocks = blocks
25             self.requests = []
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)
36             return pdh
37         def block_prefetch(self, loc):
38             self.requests.append(loc)
39
40     class MockApi(object):
41         def __init__(self, b, r):
42             self.body = b
43             self.response = r
44             self._schema = ArvadosFileWriterTestCase.MockApi.MockSchema()
45             self._rootDesc = {}
46         class MockSchema(object):
47             def __init__(self):
48                 self.schemas = {'Collection': {'properties': {'replication_desired': {'type':'integer'}}}}
49         class MockCollections(object):
50             def __init__(self, b, r):
51                 self.body = b
52                 self.response = r
53             class Execute(object):
54                 def __init__(self, r):
55                     self.response = r
56                 def execute(self, num_retries=None):
57                     return self.response
58             def create(self, ensure_unique_name=False, body=None):
59                 if body != self.body:
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)
66
67
68     def test_truncate(self):
69         keep = ArvadosFileWriterTestCase.MockKeep({
70             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
71         })
72         api = ArvadosFileWriterTestCase.MockApi({
73             "name": "test_truncate",
74             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
75             "replication_desired": None,
76         }, {
77             "uuid": "zzzzz-4zz18-mockcollection0",
78             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
79             "portable_data_hash":"7fcd0eaac3aad4c31a6a0e756475da92+52",
80         })
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))
86
87             writer.truncate(8)
88
89             # Make sure reading off the end doesn't break
90             self.assertEqual(b"", writer.read(12))
91
92             self.assertEqual(writer.size(), 8)
93             writer.seek(0, os.SEEK_SET)
94             self.assertEqual(b"01234567", writer.read(12))
95
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())
101
102
103     def test_truncate2(self):
104         keep = ArvadosFileWriterTestCase.MockKeep({
105             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
106         })
107         api = ArvadosFileWriterTestCase.MockApi({
108             "name": "test_truncate2",
109             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 7f614da9329cd3aebf59b91aadc30bf0+67108864 0:12:count.txt\n",
110             "replication_desired": None,
111         }, {
112             "uuid": "zzzzz-4zz18-mockcollection0",
113             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 7f614da9329cd3aebf59b91aadc30bf0+67108864 0:12:count.txt\n",
114             "portable_data_hash": "272da898abdf86ddc71994835e3155f8+95",
115         })
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))
121
122             # extend file size
123             writer.truncate(12)
124
125             self.assertEqual(writer.size(), 12)
126             writer.seek(0, os.SEEK_SET)
127             self.assertEqual(b"0123456789\x00\x00", writer.read(12))
128
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())
134
135     def test_truncate3(self):
136         keep = ArvadosFileWriterTestCase.MockKeep({
137             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
138             "a925576942e94b2ef57a066101b48876+10": b"abcdefghij",
139         })
140         api = ArvadosFileWriterTestCase.MockApi({
141             "name": "test_truncate",
142             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
143             "replication_desired": None,
144         }, {
145             "uuid": "zzzzz-4zz18-mockcollection0",
146             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
147             "portable_data_hash": "7fcd0eaac3aad4c31a6a0e756475da92+52",
148         })
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())
155
156             writer.truncate(8)
157
158             # Make sure reading off the end doesn't break
159             self.assertEqual(12, writer.tell())
160             self.assertEqual(b"", writer.read(12))
161
162             self.assertEqual(writer.size(), 8)
163             self.assertEqual(2, writer.seek(-10, os.SEEK_CUR))
164             self.assertEqual(b"234567", writer.read(12))
165
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())
171
172     def test_write_to_end(self):
173         keep = ArvadosFileWriterTestCase.MockKeep({
174             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
175         })
176         api = ArvadosFileWriterTestCase.MockApi({
177             "name": "test_append",
178             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n",
179             "replication_desired": None,
180         }, {
181             "uuid": "zzzzz-4zz18-mockcollection0",
182             "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n",
183             "portable_data_hash": "c5c3af76565c8efb6a806546bcf073f3+88",
184         })
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)
189
190             self.assertEqual(5, writer.seek(5, os.SEEK_SET))
191             self.assertEqual(b"56789", writer.read(8))
192
193             writer.seek(10, os.SEEK_SET)
194             writer.write("foo")
195             self.assertEqual(writer.size(), 13)
196
197             writer.seek(5, os.SEEK_SET)
198             self.assertEqual(b"56789foo", writer.read(8))
199
200             self.assertIsNone(c.manifest_locator())
201             self.assertTrue(c.modified())
202             self.assertIsNone(keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
203
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"))
208
209
210     def test_append(self):
211         keep = ArvadosFileWriterTestCase.MockKeep({
212             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
213         })
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"))
221
222             writer.seek(0, os.SEEK_SET)
223             writer.write(convert(b"hello"))
224             self.assertEqual(writer.read(), convert(b""))
225             if 'b' in mode:
226                 writer.seek(-5, os.SEEK_CUR)
227                 self.assertEqual(writer.read(3), convert(b"hel"))
228                 self.assertEqual(writer.read(), convert(b"lo"))
229             else:
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)
235             writer.read(7)
236             self.assertEqual(7, writer.tell())
237             self.assertEqual(7, writer.seek(7, os.SEEK_SET))
238
239             writer.seek(0, os.SEEK_SET)
240             self.assertEqual(writer.read(), convert(b"0123456789hello"))
241
242             writer.seek(0)
243             writer.write(convert(b"world"))
244             self.assertEqual(writer.read(), convert(b""))
245             writer.seek(0)
246             self.assertEqual(writer.read(), convert(b"0123456789helloworld"))
247
248             self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 fc5e038d38a57032085441e7fe7010b0+10 0:20:count.txt\n", c.portable_manifest_text())
249
250     def test_write_at_beginning(self):
251         keep = ArvadosFileWriterTestCase.MockKeep({
252             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
253         })
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)
259             writer.write("foo")
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())
263
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())
270
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)
278
279             self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.save_new(create_collection_record=False))
280             self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
281
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)
291
292
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)
300             writer.write("foo")
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())
304
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)
312             writer.write("foo")
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())
316
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())
328
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())
340
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):
354                 writer.write(text)
355             self.assertEqual(writer.size(), 100000000)
356
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())
362
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)
370
371             text = "0123456789"
372             writer.write(text)
373             text = "0123456789" * 9999999
374             writer.write(text)
375             self.assertEqual(writer.size(), 100000000)
376
377             self.assertEqual(c.manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 48dd23ea1645fd47d789804d71b5bb8e+67108864 77c57dc6ac5a10bb2205caaa73187994+32891126 0:100000000:count.txt\n")
378
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)
386
387             text = b"0123456789"
388             writer.seek(2)
389             writer.write(text)
390             self.assertEqual(writer.size(), 12)
391             writer.seek(0, os.SEEK_SET)
392             self.assertEqual(writer.read(), b"\x00\x00"+text)
393
394             self.assertEqual(c.manifest_text(), ". 7f614da9329cd3aebf59b91aadc30bf0+67108864 781e5e245d69b566979b86e28d23f2c7+10 0:2:count.txt 67108864:10:count.txt\n")
395
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)
403
404             text = "0123456789"
405             writer.seek((arvados.config.KEEP_BLOCK_SIZE*2) + 2)
406             writer.write(text)
407             self.assertEqual(writer.size(), (arvados.config.KEEP_BLOCK_SIZE*2) + 12)
408             writer.seek(0, os.SEEK_SET)
409
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")
411
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)
419
420                 for i in r:
421                     w = ("%s" % i) * 10
422                     writer.seek(i*10)
423                     writer.write(w.encode())
424                 writer.seek(0)
425                 self.assertEqual(writer.read(), b"00000000001111111111222222222233333333334444444444")
426
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)
434
435                 for i in r:
436                     w = ("%s" % i) * 10
437                     writer.seek(i*10)
438                     writer.write(w.encode())
439                 writer.seek(0)
440                 self.assertEqual(writer.read(), b"000000000011111111112222222222\x00\x00\x00\x00\x00\x00\x00\x00\x00\x004444444444")
441
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")
450
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())
454             writer.flush()
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())
458
459     def test_rewrite_append_existing_file(self):
460         keep = ArvadosFileWriterTestCase.MockKeep({
461             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
462         })
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")
469
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())
473
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())
478
479     def test_rewrite_over_existing_file(self):
480         keep = ArvadosFileWriterTestCase.MockKeep({
481             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
482         })
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")
489
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())
493
494             writer.arvadosfile.flush()
495
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())
499
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):
513                 writer.write(text)
514             writer.seek(0, os.SEEK_SET)
515             writer.write("foo")
516             self.assertEqual(writer.size(), 100000000)
517
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())
523
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,
530         }, {
531             "uuid":"zzzzz-4zz18-mockcollection0",
532             "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
533             "portable_data_hash":"7a461a8c58601798f690f8b368ac4423+51",
534         })
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)
540
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"))
548
549
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)
566
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)
581
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())
587
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")
592
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")
597
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")
609             w1.write("01234567")
610             w2.write("abcdefgh")
611             self.assertEqual(w1.size(), 8)
612             self.assertEqual(w2.size(), 8)
613
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"))
621
622
623 class ArvadosFileReaderTestCase(unittest.TestCase, StreamFileReaderTestMixin):
624     class MockParent(object):
625         class MockBlockMgr(object):
626             def __init__(self, blocks, nocache):
627                 self.blocks = blocks
628                 self.nocache = nocache
629                 self._keep = ArvadosFileWriterTestCase.MockKeep({})
630                 self.prefetch_lookahead = 0
631
632             def block_prefetch(self, loc):
633                 pass
634
635             def get_block_contents(self, loc, num_retries=0, cache_only=False):
636                 if self.nocache and cache_only:
637                     return None
638                 return self.blocks[loc]
639
640         def __init__(self, blocks, nocache):
641             self.blocks = blocks
642             self.nocache = nocache
643             self.lock = arvados.arvfile.NoopLock()
644
645         def root_collection(self):
646             return self
647
648         def _my_block_manager(self):
649             return ArvadosFileReaderTestCase.MockParent.MockBlockMgr(self.blocks, self.nocache)
650
651
652     def make_file_reader(self, name='emptyfile', data='', nocache=False):
653         loc = tutil.str_keep_locator(data)
654         af = ArvadosFile(ArvadosFileReaderTestCase.MockParent({loc: data}, nocache=nocache), name, stream=[Range(loc, 0, len(data))], segments=[Range(0, len(data), len(data))])
655         return ArvadosFileReader(af, mode='rb')
656
657     def make_count_reader(self, nocache=False):
658         stream = []
659         n = 0
660         blocks = {}
661         for d in [b'01234', b'34567', b'67890']:
662             loc = tutil.str_keep_locator(d)
663             blocks[loc] = d
664             stream.append(Range(loc, n, len(d)))
665             n += len(d)
666         af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache=nocache), "count.txt", stream=stream, segments=[Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)])
667         return ArvadosFileReader(af, mode="rb")
668
669     def make_newlines_reader(self, nocache=False):
670         stream = []
671         segments = []
672         n = 0
673         blocks = {}
674         for d in [b'one\ntwo\n\nth', b'ree\nfour\n\n']:
675             loc = tutil.str_keep_locator(d)
676             blocks[loc] = d
677             stream.append(Range(loc, n, len(d)))
678             segments.append(Range(n, len(d), n+len(d)))
679             n += len(d)
680         af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache=nocache), "count.txt", stream=stream, segments=segments)
681         return ArvadosFileReader(af, mode="rb")
682
683     def test_read_block_crossing_behavior(self):
684         # read() needs to return all the data requested if possible, even if it
685         # crosses uncached blocks: https://arvados.org/issues/5856
686         sfile = self.make_count_reader(nocache=True)
687         self.assertEqual(b'12345678', sfile.read(8))
688
689     def test_tell_after_block_read(self):
690         sfile = self.make_count_reader(nocache=True)
691         self.assertEqual(b'12345678', sfile.read(8))
692         self.assertEqual(8, sfile.tell())
693
694     def test_prefetch(self):
695         keep = ArvadosFileWriterTestCase.MockKeep({
696             "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
697             "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
698         })
699         with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
700             r = c.open("count.txt", "rb")
701             self.assertEqual(b"0123", r.read(4))
702         self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
703                           "e8dc4081b13434b45189a720b77b6818+8"], keep.requests)
704
705     def test_prefetch_disabled(self):
706         keep = ArvadosFileWriterTestCase.MockKeep({
707             "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
708             "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
709         })
710         keep.num_prefetch_threads = 0
711         with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
712             r = c.open("count.txt", "rb")
713             self.assertEqual(b"0123", r.read(4))
714
715         self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8"], keep.requests)
716
717     def test_prefetch_first_read_only(self):
718         # test behavior that prefetch only happens every 128 reads
719         # check that it doesn't make a prefetch request on the second read
720         keep = ArvadosFileWriterTestCase.MockKeep({
721             "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
722             "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
723         })
724         with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
725             r = c.open("count.txt", "rb")
726             self.assertEqual(b"0123", r.read(4))
727             self.assertEqual(b"45", r.read(2))
728         self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
729                           "e8dc4081b13434b45189a720b77b6818+8",
730                           "2e9ec317e197819358fbc43afca7d837+8"], keep.requests)
731         self.assertEqual(3, len(keep.requests))
732
733     def test_prefetch_again(self):
734         # test behavior that prefetch only happens every 128 reads
735         # check that it does make another prefetch request after 128 reads
736         keep = ArvadosFileWriterTestCase.MockKeep({
737             "2e9ec317e197819358fbc43afca7d837+8": b"01234567",
738             "e8dc4081b13434b45189a720b77b6818+8": b"abcdefgh",
739         })
740         with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
741             r = c.open("count.txt", "rb")
742             for i in range(0, 129):
743                 r.seek(0)
744                 self.assertEqual(b"0123", r.read(4))
745         self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
746                           "e8dc4081b13434b45189a720b77b6818+8",
747                           "2e9ec317e197819358fbc43afca7d837+8",
748                           "2e9ec317e197819358fbc43afca7d837+8"], keep.requests[0:4])
749         self.assertEqual(["2e9ec317e197819358fbc43afca7d837+8",
750                           "2e9ec317e197819358fbc43afca7d837+8",
751                           "2e9ec317e197819358fbc43afca7d837+8",
752                           "e8dc4081b13434b45189a720b77b6818+8"], keep.requests[127:131])
753         # gets the 1st block 129 times from keep (cache),
754         # and the 2nd block twice to get 131 requests
755         self.assertEqual(131, len(keep.requests))
756
757     def test__eq__from_manifest(self):
758         with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
759             with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c2:
760                 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
761                 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
762
763     def test__eq__from_writes(self):
764         with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
765             with Collection() as c2:
766                 f = c2.open("count1.txt", "wb")
767                 f.write("0123456789")
768
769                 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
770                 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
771
772     def test__ne__(self):
773         with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
774             with Collection() as c2:
775                 f = c2.open("count1.txt", "wb")
776                 f.write("1234567890")
777
778                 self.assertTrue(c1["count1.txt"] != c2["count1.txt"])
779                 self.assertFalse(c1["count1.txt"] == c2["count1.txt"])
780
781
782 class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
783     def reader_for(self, coll_name, **kwargs):
784         stream = []
785         segments = []
786         n = 0
787         for d in self.manifest_for(coll_name).split():
788             try:
789                 k = KeepLocator(d)
790                 segments.append(Range(n, n, k.size))
791                 stream.append(Range(d, n, k.size))
792                 n += k.size
793             except ValueError:
794                 pass
795
796         blockmanager = arvados.arvfile._BlockManager(self.keep_client())
797         blockmanager.prefetch_enabled = False
798         col = Collection(keep_client=self.keep_client(), block_manager=blockmanager)
799         af = ArvadosFile(col, "test",
800                          stream=stream,
801                          segments=segments)
802         kwargs.setdefault('mode', 'rb')
803         return ArvadosFileReader(af, **kwargs)
804
805     def read_for_test(self, reader, byte_count, **kwargs):
806         return reader.read(byte_count, **kwargs)
807
808
809 class ArvadosFileReadFromTestCase(ArvadosFileReadTestCase):
810     def read_for_test(self, reader, byte_count, **kwargs):
811         return reader.readfrom(0, byte_count, **kwargs)
812
813
814 class ArvadosFileReadAllTestCase(ArvadosFileReadTestCase):
815     def read_for_test(self, reader, byte_count, **kwargs):
816         return b''.join(reader.readall(**kwargs))
817
818
819 class ArvadosFileReadAllDecompressedTestCase(ArvadosFileReadTestCase):
820     def read_for_test(self, reader, byte_count, **kwargs):
821         return b''.join(reader.readall_decompressed(**kwargs))
822
823
824 class ArvadosFileReadlinesTestCase(ArvadosFileReadTestCase):
825     def read_for_test(self, reader, byte_count, **kwargs):
826         return ''.join(reader.readlines(**kwargs)).encode()
827
828
829 class ArvadosFileTestCase(unittest.TestCase):
830     def datetime_to_hex(self, dt):
831         return hex(int(time.mktime(dt.timetuple())))[2:]
832
833     def test_permission_expired(self):
834         base_manifest = ". 781e5e245d69b566979b86e28d23f2c7+10+A715fd31f8111894f717eb1003c1b0216799dd9ec@{} 0:10:count.txt\n"
835         now = datetime.datetime.now()
836         a_week_ago = now - datetime.timedelta(days=7)
837         a_month_ago = now - datetime.timedelta(days=30)
838         a_week_from_now = now + datetime.timedelta(days=7)
839         with Collection(base_manifest.format(self.datetime_to_hex(a_week_from_now))) as c:
840             self.assertFalse(c.find('count.txt').permission_expired())
841         with Collection(base_manifest.format(self.datetime_to_hex(a_week_ago))) as c:
842             f = c.find('count.txt')
843             self.assertTrue(f.permission_expired())
844             self.assertTrue(f.permission_expired(a_week_from_now))
845             self.assertFalse(f.permission_expired(a_month_ago))
846
847
848 class BlockManagerTest(unittest.TestCase):
849     def test_bufferblock_append(self):
850         keep = ArvadosFileWriterTestCase.MockKeep({})
851         with arvados.arvfile._BlockManager(keep) as blockmanager:
852             bufferblock = blockmanager.alloc_bufferblock()
853             bufferblock.append("foo")
854
855             self.assertEqual(bufferblock.size(), 3)
856             self.assertEqual(bufferblock.buffer_view[0:3], b"foo")
857             self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
858
859             bufferblock.append("bar")
860
861             self.assertEqual(bufferblock.size(), 6)
862             self.assertEqual(bufferblock.buffer_view[0:6], b"foobar")
863             self.assertEqual(bufferblock.locator(), "3858f62230ac3c915f300c664312c63f+6")
864
865             bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
866             with self.assertRaises(arvados.errors.AssertionError):
867                 bufferblock.append("bar")
868
869     def test_bufferblock_dup(self):
870         keep = ArvadosFileWriterTestCase.MockKeep({})
871         with arvados.arvfile._BlockManager(keep) as blockmanager:
872             bufferblock = blockmanager.alloc_bufferblock()
873             bufferblock.append("foo")
874
875             self.assertEqual(bufferblock.size(), 3)
876             self.assertEqual(bufferblock.buffer_view[0:3], b"foo")
877             self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
878             bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
879
880             bufferblock2 = blockmanager.dup_block(bufferblock, None)
881             self.assertNotEqual(bufferblock.blockid, bufferblock2.blockid)
882
883             bufferblock2.append("bar")
884
885             self.assertEqual(bufferblock2.size(), 6)
886             self.assertEqual(bufferblock2.buffer_view[0:6], b"foobar")
887             self.assertEqual(bufferblock2.locator(), "3858f62230ac3c915f300c664312c63f+6")
888
889             self.assertEqual(bufferblock.size(), 3)
890             self.assertEqual(bufferblock.buffer_view[0:3], b"foo")
891             self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
892
893     def test_bufferblock_get(self):
894         keep = ArvadosFileWriterTestCase.MockKeep({
895             "781e5e245d69b566979b86e28d23f2c7+10": b"0123456789",
896         })
897         with arvados.arvfile._BlockManager(keep) as blockmanager:
898             bufferblock = blockmanager.alloc_bufferblock()
899             bufferblock.append("foo")
900
901             self.assertEqual(blockmanager.get_block_contents("781e5e245d69b566979b86e28d23f2c7+10", 1), b"0123456789")
902             self.assertEqual(blockmanager.get_block_contents(bufferblock.blockid, 1), b"foo")
903
904     def test_bufferblock_commit(self):
905         mockkeep = mock.MagicMock()
906         with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
907             bufferblock = blockmanager.alloc_bufferblock()
908             bufferblock.owner = mock.MagicMock(spec=arvados.arvfile.ArvadosFile)
909             def flush(sync=None):
910                 blockmanager.commit_bufferblock(bufferblock, sync)
911             bufferblock.owner.flush.side_effect = flush
912             bufferblock.append("foo")
913             blockmanager.commit_all()
914             self.assertTrue(bufferblock.owner.flush.called)
915             self.assertTrue(mockkeep.put.called)
916             self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
917             self.assertIsNone(bufferblock.buffer_view)
918
919     def test_bufferblock_commit_pending(self):
920         # Test for bug #7225
921         mockkeep = mock.MagicMock()
922         mockkeep.put.side_effect = lambda *args, **kwargs: time.sleep(1)
923         with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
924             bufferblock = blockmanager.alloc_bufferblock()
925             bufferblock.append("foo")
926
927             blockmanager.commit_bufferblock(bufferblock, False)
928             self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.PENDING)
929
930             blockmanager.commit_bufferblock(bufferblock, True)
931             self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
932
933     def test_bufferblock_commit_with_error(self):
934         mockkeep = mock.MagicMock()
935         mockkeep.put.side_effect = arvados.errors.KeepWriteError("fail")
936         with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
937             bufferblock = blockmanager.alloc_bufferblock()
938             bufferblock.owner = mock.MagicMock(spec=arvados.arvfile.ArvadosFile)
939             def flush(sync=None):
940                 blockmanager.commit_bufferblock(bufferblock, sync)
941             bufferblock.owner.flush.side_effect = flush
942             bufferblock.append("foo")
943             with self.assertRaises(arvados.errors.KeepWriteError) as err:
944                 blockmanager.commit_all()
945             self.assertTrue(bufferblock.owner.flush.called)
946             self.assertEqual(str(err.exception), "Error writing some blocks: block acbd18db4cc2f85cedef654fccc4a4d8+3 raised KeepWriteError (fail)")
947             self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.ERROR)