12 from arvados._ranges import Range
13 from arvados.keep import KeepLocator
14 from arvados.collection import Collection, CollectionReader
15 from arvados.arvfile import ArvadosFile, ArvadosFileReader
17 import arvados_testutil as tutil
18 from test_stream import StreamFileReaderTestCase, StreamRetryTestMixin
20 class ArvadosFileWriterTestCase(unittest.TestCase):
21 class MockKeep(object):
22 def __init__(self, blocks):
25 def get(self, locator, num_retries=0):
26 self.requests.append(locator)
27 return self.blocks.get(locator)
28 def get_from_cache(self, locator):
29 self.requests.append(locator)
30 return self.blocks.get(locator)
31 def put(self, data, num_retries=None):
32 pdh = tutil.str_keep_locator(data)
33 self.blocks[pdh] = str(data)
36 class MockApi(object):
37 def __init__(self, b, r):
40 class MockCollections(object):
41 def __init__(self, b, r):
44 class Execute(object):
45 def __init__(self, r):
47 def execute(self, num_retries=None):
49 def create(self, ensure_unique_name=False, body=None):
51 raise Exception("Body %s does not match expectation %s" % (body, self.body))
52 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
53 def update(self, uuid=None, body=None):
54 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
55 def collections(self):
56 return ArvadosFileWriterTestCase.MockApi.MockCollections(self.body, self.response)
59 def test_truncate(self):
60 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
61 api = ArvadosFileWriterTestCase.MockApi({"name":"test_truncate",
62 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n"},
63 {"uuid":"zzzzz-4zz18-mockcollection0",
64 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n"})
65 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
66 api_client=api, keep_client=keep) as c:
67 writer = c.open("count.txt", "r+")
68 self.assertEqual(writer.size(), 10)
69 self.assertEqual("0123456789", writer.read(12))
73 # Make sure reading off the end doesn't break
74 self.assertEqual("", writer.read(12))
76 self.assertEqual(writer.size(), 8)
77 writer.seek(0, os.SEEK_SET)
78 self.assertEqual("01234567", writer.read(12))
80 self.assertIsNone(c.manifest_locator())
81 self.assertTrue(c.modified())
82 c.save_new("test_truncate")
83 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
84 self.assertFalse(c.modified())
86 def test_write_to_end(self):
87 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
88 api = ArvadosFileWriterTestCase.MockApi({"name":"test_append",
89 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n"},
90 {"uuid":"zzzzz-4zz18-mockcollection0",
91 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n"})
92 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
93 api_client=api, keep_client=keep) as c:
94 writer = c.open("count.txt", "r+")
95 self.assertEqual(writer.size(), 10)
97 writer.seek(5, os.SEEK_SET)
98 self.assertEqual("56789", writer.read(8))
100 writer.seek(10, os.SEEK_SET)
102 self.assertEqual(writer.size(), 13)
104 writer.seek(5, os.SEEK_SET)
105 self.assertEqual("56789foo", writer.read(8))
107 self.assertIsNone(c.manifest_locator())
108 self.assertTrue(c.modified())
109 self.assertIsNone(keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
111 c.save_new("test_append")
112 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
113 self.assertFalse(c.modified())
114 self.assertEqual("foo", keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
117 def test_append(self):
118 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
119 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', keep_client=keep)
120 writer = c.open("count.txt", "a+")
121 self.assertEqual(writer.read(20), "0123456789")
122 writer.seek(0, os.SEEK_SET)
124 writer.write("hello")
125 self.assertEqual(writer.read(20), "0123456789hello")
126 writer.seek(0, os.SEEK_SET)
128 writer.write("world")
129 self.assertEqual(writer.read(20), "0123456789helloworld")
131 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 fc5e038d38a57032085441e7fe7010b0+10 0:20:count.txt\n", c.portable_manifest_text())
133 def test_write_at_beginning(self):
134 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
135 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
136 keep_client=keep) as c:
137 writer = c.open("count.txt", "r+")
138 self.assertEqual("0123456789", writer.readfrom(0, 13))
139 writer.seek(0, os.SEEK_SET)
141 self.assertEqual(writer.size(), 10)
142 self.assertEqual("foo3456789", writer.readfrom(0, 13))
143 self.assertEqual(". acbd18db4cc2f85cedef654fccc4a4d8+3 781e5e245d69b566979b86e28d23f2c7+10 0:3:count.txt 6:7:count.txt\n", c.portable_manifest_text())
145 def test_write_empty(self):
146 keep = ArvadosFileWriterTestCase.MockKeep({})
147 with Collection(keep_client=keep) as c:
148 writer = c.open("count.txt", "w")
149 self.assertEqual(writer.size(), 0)
150 self.assertEqual(". d41d8cd98f00b204e9800998ecf8427e+0 0:0:count.txt\n", c.portable_manifest_text())
152 def test_save_manifest_text(self):
153 keep = ArvadosFileWriterTestCase.MockKeep({})
154 with Collection(keep_client=keep) as c:
155 writer = c.open("count.txt", "w")
156 writer.write("0123456789")
157 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
158 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
160 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.save_new(create_collection_record=False))
161 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
163 def test_get_manifest_text_commits(self):
164 keep = ArvadosFileWriterTestCase.MockKeep({})
165 with Collection(keep_client=keep) as c:
166 writer = c.open("count.txt", "w")
167 writer.write("0123456789")
168 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
169 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
170 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.manifest_text())
171 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
174 def test_write_in_middle(self):
175 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
176 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
177 keep_client=keep) as c:
178 writer = c.open("count.txt", "r+")
179 self.assertEqual("0123456789", writer.readfrom(0, 13))
180 writer.seek(3, os.SEEK_SET)
182 self.assertEqual(writer.size(), 10)
183 self.assertEqual("012foo6789", writer.readfrom(0, 13))
184 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:count.txt 10:3:count.txt 6:4:count.txt\n", c.portable_manifest_text())
186 def test_write_at_end(self):
187 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
188 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
189 keep_client=keep) as c:
190 writer = c.open("count.txt", "r+")
191 self.assertEqual("0123456789", writer.readfrom(0, 13))
192 writer.seek(7, os.SEEK_SET)
194 self.assertEqual(writer.size(), 10)
195 self.assertEqual("0123456foo", writer.readfrom(0, 13))
196 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:7:count.txt 10:3:count.txt\n", c.portable_manifest_text())
198 def test_write_across_segment_boundary(self):
199 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
200 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt 0:10:count.txt\n',
201 keep_client=keep) as c:
202 writer = c.open("count.txt", "r+")
203 self.assertEqual("012345678901234", writer.readfrom(0, 15))
204 writer.seek(7, os.SEEK_SET)
205 writer.write("foobar")
206 self.assertEqual(writer.size(), 20)
207 self.assertEqual("0123456foobar34", writer.readfrom(0, 15))
208 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 3858f62230ac3c915f300c664312c63f+6 0:7:count.txt 10:6:count.txt 3:7:count.txt\n", c.portable_manifest_text())
210 def test_write_across_several_segments(self):
211 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
212 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:4:count.txt 0:4:count.txt 0:4:count.txt',
213 keep_client=keep) as c:
214 writer = c.open("count.txt", "r+")
215 self.assertEqual("012301230123", writer.readfrom(0, 15))
216 writer.seek(2, os.SEEK_SET)
217 writer.write("abcdefg")
218 self.assertEqual(writer.size(), 12)
219 self.assertEqual("01abcdefg123", writer.readfrom(0, 15))
220 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 7ac66c0f148de9519b8bd264312c4d64+7 0:2:count.txt 10:7:count.txt 1:3:count.txt\n", c.portable_manifest_text())
222 def test_write_large(self):
223 keep = ArvadosFileWriterTestCase.MockKeep({})
224 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
225 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n"},
226 {"uuid":"zzzzz-4zz18-mockcollection0",
227 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n"})
228 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
229 api_client=api, keep_client=keep) as c:
230 writer = c.open("count.txt", "r+")
231 text = "0123456789" * 100
232 for b in xrange(0, 100000):
234 self.assertEqual(writer.size(), 100000000)
236 self.assertIsNone(c.manifest_locator())
237 self.assertTrue(c.modified())
238 c.save_new("test_write_large")
239 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
240 self.assertFalse(c.modified())
243 def test_large_write(self):
244 keep = ArvadosFileWriterTestCase.MockKeep({})
245 api = ArvadosFileWriterTestCase.MockApi({}, {})
246 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
247 api_client=api, keep_client=keep) as c:
248 writer = c.open("count.txt", "r+")
249 self.assertEqual(writer.size(), 0)
253 text = "0123456789" * 9999999
255 self.assertEqual(writer.size(), 100000000)
257 self.assertEqual(c.manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 48dd23ea1645fd47d789804d71b5bb8e+67108864 77c57dc6ac5a10bb2205caaa73187994+32891126 0:100000000:count.txt\n")
259 def test_rewrite_on_empty_file(self):
260 keep = ArvadosFileWriterTestCase.MockKeep({})
261 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
262 keep_client=keep) as c:
263 writer = c.open("count.txt", "r+")
264 for b in xrange(0, 10):
265 writer.seek(0, os.SEEK_SET)
266 writer.write("0123456789")
268 self.assertEqual(writer.size(), 10)
269 self.assertEqual("0123456789", writer.readfrom(0, 20))
270 self.assertEqual(". 7a08b07e84641703e5f2c836aa59a170+100 90:10:count.txt\n", c.portable_manifest_text())
272 self.assertEqual(writer.size(), 10)
273 self.assertEqual("0123456789", writer.readfrom(0, 20))
274 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n", c.portable_manifest_text())
276 def test_rewrite_append_existing_file(self):
277 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
278 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
279 keep_client=keep) as c:
280 writer = c.open("count.txt", "r+")
281 for b in xrange(0, 10):
282 writer.seek(10, os.SEEK_SET)
283 writer.write("abcdefghij")
285 self.assertEqual(writer.size(), 20)
286 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
287 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:10:count.txt 100:10:count.txt\n", c.portable_manifest_text())
289 writer.arvadosfile.flush()
290 self.assertEqual(writer.size(), 20)
291 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
292 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n", c.portable_manifest_text())
294 def test_rewrite_over_existing_file(self):
295 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
296 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
297 keep_client=keep) as c:
298 writer = c.open("count.txt", "r+")
299 for b in xrange(0, 10):
300 writer.seek(5, os.SEEK_SET)
301 writer.write("abcdefghij")
303 self.assertEqual(writer.size(), 15)
304 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
305 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:5:count.txt 100:10:count.txt\n", c.portable_manifest_text())
307 writer.arvadosfile.flush()
309 self.assertEqual(writer.size(), 15)
310 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
311 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:5:count.txt 10:10:count.txt\n", c.portable_manifest_text())
313 def test_write_large_rewrite(self):
314 keep = ArvadosFileWriterTestCase.MockKeep({})
315 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
316 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"},
317 {"uuid":"zzzzz-4zz18-mockcollection0",
318 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"})
319 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
320 api_client=api, keep_client=keep) as c:
321 writer = c.open("count.txt", "r+")
322 text = ''.join(["0123456789" for a in xrange(0, 100)])
323 for b in xrange(0, 100000):
325 writer.seek(0, os.SEEK_SET)
327 self.assertEqual(writer.size(), 100000000)
329 self.assertIsNone(c.manifest_locator())
330 self.assertTrue(c.modified())
331 c.save_new("test_write_large")
332 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
333 self.assertFalse(c.modified())
335 def test_create(self):
336 keep = ArvadosFileWriterTestCase.MockKeep({})
337 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
338 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
339 {"uuid":"zzzzz-4zz18-mockcollection0",
340 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
341 with Collection(api_client=api, keep_client=keep) as c:
342 writer = c.open("count.txt", "w+")
343 self.assertEqual(writer.size(), 0)
344 writer.write("01234567")
345 self.assertEqual(writer.size(), 8)
347 self.assertIsNone(c.manifest_locator())
348 self.assertTrue(c.modified())
349 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
350 c.save_new("test_create")
351 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
352 self.assertFalse(c.modified())
353 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
356 def test_create_subdir(self):
357 keep = ArvadosFileWriterTestCase.MockKeep({})
358 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
359 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
360 {"uuid":"zzzzz-4zz18-mockcollection0",
361 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
362 with Collection(api_client=api, keep_client=keep) as c:
363 self.assertIsNone(c.api_response())
364 writer = c.open("foo/bar/count.txt", "w+")
365 writer.write("01234567")
366 self.assertFalse(c.committed())
367 c.save_new("test_create")
368 self.assertTrue(c.committed())
369 self.assertEqual(c.api_response(), api.response)
371 def test_overwrite(self):
372 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
373 api = ArvadosFileWriterTestCase.MockApi({"name":"test_overwrite",
374 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
375 {"uuid":"zzzzz-4zz18-mockcollection0",
376 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
377 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
378 api_client=api, keep_client=keep) as c:
379 writer = c.open("count.txt", "w+")
380 self.assertEqual(writer.size(), 0)
381 writer.write("01234567")
382 self.assertEqual(writer.size(), 8)
384 self.assertIsNone(c.manifest_locator())
385 self.assertTrue(c.modified())
386 c.save_new("test_overwrite")
387 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
388 self.assertFalse(c.modified())
390 def test_file_not_found(self):
391 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
392 with self.assertRaises(IOError):
393 writer = c.open("nocount.txt", "r")
395 def test_cannot_open_directory(self):
396 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
397 with self.assertRaises(IOError):
398 writer = c.open(".", "r")
400 def test_create_multiple(self):
401 keep = ArvadosFileWriterTestCase.MockKeep({})
402 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create_multiple",
403 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"},
404 {"uuid":"zzzzz-4zz18-mockcollection0",
405 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"})
406 with Collection(api_client=api, keep_client=keep) as c:
407 w1 = c.open("count1.txt", "w")
408 w2 = c.open("count2.txt", "w")
411 self.assertEqual(w1.size(), 8)
412 self.assertEqual(w2.size(), 8)
414 self.assertIsNone(c.manifest_locator())
415 self.assertTrue(c.modified())
416 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
417 c.save_new("test_create_multiple")
418 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
419 self.assertFalse(c.modified())
420 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
423 class ArvadosFileReaderTestCase(StreamFileReaderTestCase):
424 class MockParent(object):
425 class MockBlockMgr(object):
426 def __init__(self, blocks, nocache):
428 self.nocache = nocache
430 def block_prefetch(self, loc):
433 def get_block_contents(self, loc, num_retries=0, cache_only=False):
434 if self.nocache and cache_only:
436 return self.blocks[loc]
438 def __init__(self, blocks, nocache):
440 self.nocache = nocache
441 self.lock = arvados.arvfile.NoopLock()
443 def root_collection(self):
446 def _my_block_manager(self):
447 return ArvadosFileReaderTestCase.MockParent.MockBlockMgr(self.blocks, self.nocache)
450 def make_count_reader(self, nocache=False):
454 for d in ['01234', '34567', '67890']:
455 loc = tutil.str_keep_locator(d)
457 stream.append(Range(loc, n, len(d)))
459 af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache), "count.txt", stream=stream, segments=[Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)])
460 return ArvadosFileReader(af)
462 def test_read_block_crossing_behavior(self):
463 # read() needs to return all the data requested if possible, even if it
464 # crosses uncached blocks: https://arvados.org/issues/5856
465 sfile = self.make_count_reader(nocache=True)
466 self.assertEqual('12345678', sfile.read(8))
468 def test_successive_reads(self):
469 # Override StreamFileReaderTestCase.test_successive_reads
470 sfile = self.make_count_reader(nocache=True)
471 self.assertEqual('1234', sfile.read(4))
472 self.assertEqual('5678', sfile.read(4))
473 self.assertEqual('9', sfile.read(4))
474 self.assertEqual('', sfile.read(4))
476 def test_tell_after_block_read(self):
477 # Override StreamFileReaderTestCase.test_tell_after_block_read
478 sfile = self.make_count_reader(nocache=True)
479 self.assertEqual('12345678', sfile.read(8))
480 self.assertEqual(8, sfile.tell())
482 def test_prefetch(self):
483 keep = ArvadosFileWriterTestCase.MockKeep({"2e9ec317e197819358fbc43afca7d837+8": "01234567", "e8dc4081b13434b45189a720b77b6818+8": "abcdefgh"})
484 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
485 r = c.open("count.txt", "r")
486 self.assertEqual("0123", r.read(4))
487 self.assertIn("2e9ec317e197819358fbc43afca7d837+8", keep.requests)
488 self.assertIn("e8dc4081b13434b45189a720b77b6818+8", keep.requests)
490 def test__eq__from_manifest(self):
491 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
492 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c2:
493 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
494 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
496 def test__eq__from_writes(self):
497 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
498 with Collection() as c2:
499 f = c2.open("count1.txt", "w")
500 f.write("0123456789")
502 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
503 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
505 def test__ne__(self):
506 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
507 with Collection() as c2:
508 f = c2.open("count1.txt", "w")
509 f.write("1234567890")
511 self.assertTrue(c1["count1.txt"] != c2["count1.txt"])
512 self.assertFalse(c1["count1.txt"] == c2["count1.txt"])
515 class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
516 def reader_for(self, coll_name, **kwargs):
520 for d in self.manifest_for(coll_name).split():
523 segments.append(Range(n, n, k.size))
524 stream.append(Range(d, n, k.size))
529 blockmanager = arvados.arvfile._BlockManager(self.keep_client())
530 blockmanager.prefetch_enabled = False
531 col = Collection(keep_client=self.keep_client(), block_manager=blockmanager)
532 af = ArvadosFile(col, "test",
535 return ArvadosFileReader(af, **kwargs)
537 def read_for_test(self, reader, byte_count, **kwargs):
538 return reader.read(byte_count, **kwargs)
541 class ArvadosFileReadFromTestCase(ArvadosFileReadTestCase):
542 def read_for_test(self, reader, byte_count, **kwargs):
543 return reader.readfrom(0, byte_count, **kwargs)
546 class ArvadosFileReadAllTestCase(ArvadosFileReadTestCase):
547 def read_for_test(self, reader, byte_count, **kwargs):
548 return ''.join(reader.readall(**kwargs))
551 class ArvadosFileReadAllDecompressedTestCase(ArvadosFileReadTestCase):
552 def read_for_test(self, reader, byte_count, **kwargs):
553 return ''.join(reader.readall_decompressed(**kwargs))
556 class ArvadosFileReadlinesTestCase(ArvadosFileReadTestCase):
557 def read_for_test(self, reader, byte_count, **kwargs):
558 return ''.join(reader.readlines(**kwargs))
560 class BlockManagerTest(unittest.TestCase):
561 def test_bufferblock_append(self):
562 keep = ArvadosFileWriterTestCase.MockKeep({})
563 with arvados.arvfile._BlockManager(keep) as blockmanager:
564 bufferblock = blockmanager.alloc_bufferblock()
565 bufferblock.append("foo")
567 self.assertEqual(bufferblock.size(), 3)
568 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
569 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
571 bufferblock.append("bar")
573 self.assertEqual(bufferblock.size(), 6)
574 self.assertEqual(bufferblock.buffer_view[0:6], "foobar")
575 self.assertEqual(bufferblock.locator(), "3858f62230ac3c915f300c664312c63f+6")
577 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
578 with self.assertRaises(arvados.errors.AssertionError):
579 bufferblock.append("bar")
581 def test_bufferblock_dup(self):
582 keep = ArvadosFileWriterTestCase.MockKeep({})
583 with arvados.arvfile._BlockManager(keep) as blockmanager:
584 bufferblock = blockmanager.alloc_bufferblock()
585 bufferblock.append("foo")
587 self.assertEqual(bufferblock.size(), 3)
588 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
589 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
590 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
592 bufferblock2 = blockmanager.dup_block(bufferblock, None)
593 self.assertNotEqual(bufferblock.blockid, bufferblock2.blockid)
595 bufferblock2.append("bar")
597 self.assertEqual(bufferblock2.size(), 6)
598 self.assertEqual(bufferblock2.buffer_view[0:6], "foobar")
599 self.assertEqual(bufferblock2.locator(), "3858f62230ac3c915f300c664312c63f+6")
601 self.assertEqual(bufferblock.size(), 3)
602 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
603 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
605 def test_bufferblock_get(self):
606 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
607 with arvados.arvfile._BlockManager(keep) as blockmanager:
608 bufferblock = blockmanager.alloc_bufferblock()
609 bufferblock.append("foo")
611 self.assertEqual(blockmanager.get_block_contents("781e5e245d69b566979b86e28d23f2c7+10", 1), "0123456789")
612 self.assertEqual(blockmanager.get_block_contents(bufferblock.blockid, 1), "foo")
614 def test_bufferblock_commit(self):
615 mockkeep = mock.MagicMock()
616 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
617 bufferblock = blockmanager.alloc_bufferblock()
618 bufferblock.owner = mock.MagicMock()
619 def flush(sync=None):
620 blockmanager.commit_bufferblock(bufferblock, sync)
621 bufferblock.owner.flush.side_effect = flush
622 bufferblock.append("foo")
623 blockmanager.commit_all()
624 self.assertTrue(bufferblock.owner.flush.called)
625 self.assertTrue(mockkeep.put.called)
626 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
627 self.assertIsNone(bufferblock.buffer_view)
629 def test_bufferblock_commit_pending(self):
631 mockkeep = mock.MagicMock()
632 mockkeep.put.side_effect = lambda x: time.sleep(1)
633 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
634 bufferblock = blockmanager.alloc_bufferblock()
635 bufferblock.append("foo")
637 blockmanager.commit_bufferblock(bufferblock, False)
638 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.PENDING)
640 blockmanager.commit_bufferblock(bufferblock, True)
641 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
644 def test_bufferblock_commit_with_error(self):
645 mockkeep = mock.MagicMock()
646 mockkeep.put.side_effect = arvados.errors.KeepWriteError("fail")
647 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
648 bufferblock = blockmanager.alloc_bufferblock()
649 bufferblock.owner = mock.MagicMock()
650 def flush(sync=None):
651 blockmanager.commit_bufferblock(bufferblock, sync)
652 bufferblock.owner.flush.side_effect = flush
653 bufferblock.append("foo")
654 with self.assertRaises(arvados.errors.KeepWriteError) as err:
655 blockmanager.commit_all()
656 self.assertTrue(bufferblock.owner.flush.called)
657 self.assertEqual(str(err.exception), "Error writing some blocks: block acbd18db4cc2f85cedef654fccc4a4d8+3 raised KeepWriteError (fail)")
658 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.ERROR)