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