13 from arvados._ranges import Range
14 from arvados.keep import KeepLocator
15 from arvados.collection import Collection, CollectionReader
16 from arvados.arvfile import ArvadosFile, ArvadosFileReader
18 import arvados_testutil as tutil
19 from test_stream import StreamFileReaderTestCase, StreamRetryTestMixin
21 class ArvadosFileWriterTestCase(unittest.TestCase):
22 class MockKeep(object):
23 def __init__(self, blocks):
26 def get(self, locator, num_retries=0):
27 self.requests.append(locator)
28 return self.blocks.get(locator)
29 def get_from_cache(self, locator):
30 self.requests.append(locator)
31 return self.blocks.get(locator)
32 def put(self, data, num_retries=None):
33 pdh = "%s+%i" % (hashlib.md5(data).hexdigest(), len(data))
34 self.blocks[pdh] = str(data)
37 class MockApi(object):
38 def __init__(self, b, r):
41 class MockCollections(object):
42 def __init__(self, b, r):
45 class Execute(object):
46 def __init__(self, r):
48 def execute(self, num_retries=None):
50 def create(self, ensure_unique_name=False, body=None):
52 raise Exception("Body %s does not match expectation %s" % (body, self.body))
53 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
54 def update(self, uuid=None, body=None):
55 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
56 def collections(self):
57 return ArvadosFileWriterTestCase.MockApi.MockCollections(self.body, self.response)
60 def test_truncate(self):
61 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
62 api = ArvadosFileWriterTestCase.MockApi({"name":"test_truncate",
63 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n"},
64 {"uuid":"zzzzz-4zz18-mockcollection0",
65 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n"})
66 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
67 api_client=api, keep_client=keep) as c:
68 writer = c.open("count.txt", "r+")
69 self.assertEqual(writer.size(), 10)
70 self.assertEqual("0123456789", writer.read(12))
74 # Make sure reading off the end doesn't break
75 self.assertEqual("", writer.read(12))
77 self.assertEqual(writer.size(), 8)
78 writer.seek(0, os.SEEK_SET)
79 self.assertEqual("01234567", writer.read(12))
81 self.assertIsNone(c.manifest_locator())
82 self.assertTrue(c.modified())
83 c.save_new("test_truncate")
84 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
85 self.assertFalse(c.modified())
87 def test_write_to_end(self):
88 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
89 api = ArvadosFileWriterTestCase.MockApi({"name":"test_append",
90 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n"},
91 {"uuid":"zzzzz-4zz18-mockcollection0",
92 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n"})
93 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
94 api_client=api, keep_client=keep) as c:
95 writer = c.open("count.txt", "r+")
96 self.assertEqual(writer.size(), 10)
98 writer.seek(5, os.SEEK_SET)
99 self.assertEqual("56789", writer.read(8))
101 writer.seek(10, os.SEEK_SET)
103 self.assertEqual(writer.size(), 13)
105 writer.seek(5, os.SEEK_SET)
106 self.assertEqual("56789foo", writer.read(8))
108 self.assertIsNone(c.manifest_locator())
109 self.assertTrue(c.modified())
110 self.assertIsNone(keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
112 c.save_new("test_append")
113 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
114 self.assertFalse(c.modified())
115 self.assertEqual("foo", keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
118 def test_append(self):
119 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
120 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', keep_client=keep)
121 writer = c.open("count.txt", "a+")
122 self.assertEqual(writer.read(20), "0123456789")
123 writer.seek(0, os.SEEK_SET)
125 writer.write("hello")
126 self.assertEqual(writer.read(20), "0123456789hello")
127 writer.seek(0, os.SEEK_SET)
129 writer.write("world")
130 self.assertEqual(writer.read(20), "0123456789helloworld")
132 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 fc5e038d38a57032085441e7fe7010b0+10 0:20:count.txt\n", c.portable_manifest_text())
134 def test_write_at_beginning(self):
135 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
136 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
137 keep_client=keep) as c:
138 writer = c.open("count.txt", "r+")
139 self.assertEqual("0123456789", writer.readfrom(0, 13))
140 writer.seek(0, os.SEEK_SET)
142 self.assertEqual(writer.size(), 10)
143 self.assertEqual("foo3456789", writer.readfrom(0, 13))
144 self.assertEqual(". acbd18db4cc2f85cedef654fccc4a4d8+3 781e5e245d69b566979b86e28d23f2c7+10 0:3:count.txt 6:7:count.txt\n", c.portable_manifest_text())
146 def test_write_empty(self):
147 keep = ArvadosFileWriterTestCase.MockKeep({})
148 with Collection(keep_client=keep) as c:
149 writer = c.open("count.txt", "w")
150 self.assertEqual(writer.size(), 0)
151 self.assertEqual(". d41d8cd98f00b204e9800998ecf8427e+0 0:0:count.txt\n", c.portable_manifest_text())
153 def test_save_manifest_text(self):
154 keep = ArvadosFileWriterTestCase.MockKeep({})
155 with Collection(keep_client=keep) as c:
156 writer = c.open("count.txt", "w")
157 writer.write("0123456789")
158 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
159 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
161 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.save_new(create_collection_record=False))
162 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
164 def test_get_manifest_text_commits(self):
165 keep = ArvadosFileWriterTestCase.MockKeep({})
166 with Collection(keep_client=keep) as c:
167 writer = c.open("count.txt", "w")
168 writer.write("0123456789")
169 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
170 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
171 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.manifest_text())
172 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
175 def test_write_in_middle(self):
176 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
177 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
178 keep_client=keep) as c:
179 writer = c.open("count.txt", "r+")
180 self.assertEqual("0123456789", writer.readfrom(0, 13))
181 writer.seek(3, os.SEEK_SET)
183 self.assertEqual(writer.size(), 10)
184 self.assertEqual("012foo6789", writer.readfrom(0, 13))
185 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:count.txt 10:3:count.txt 6:4:count.txt\n", c.portable_manifest_text())
187 def test_write_at_end(self):
188 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
189 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
190 keep_client=keep) as c:
191 writer = c.open("count.txt", "r+")
192 self.assertEqual("0123456789", writer.readfrom(0, 13))
193 writer.seek(7, os.SEEK_SET)
195 self.assertEqual(writer.size(), 10)
196 self.assertEqual("0123456foo", writer.readfrom(0, 13))
197 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:7:count.txt 10:3:count.txt\n", c.portable_manifest_text())
199 def test_write_across_segment_boundary(self):
200 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
201 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt 0:10:count.txt\n',
202 keep_client=keep) as c:
203 writer = c.open("count.txt", "r+")
204 self.assertEqual("012345678901234", writer.readfrom(0, 15))
205 writer.seek(7, os.SEEK_SET)
206 writer.write("foobar")
207 self.assertEqual(writer.size(), 20)
208 self.assertEqual("0123456foobar34", writer.readfrom(0, 15))
209 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 3858f62230ac3c915f300c664312c63f+6 0:7:count.txt 10:6:count.txt 3:7:count.txt\n", c.portable_manifest_text())
211 def test_write_across_several_segments(self):
212 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
213 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:4:count.txt 0:4:count.txt 0:4:count.txt',
214 keep_client=keep) as c:
215 writer = c.open("count.txt", "r+")
216 self.assertEqual("012301230123", writer.readfrom(0, 15))
217 writer.seek(2, os.SEEK_SET)
218 writer.write("abcdefg")
219 self.assertEqual(writer.size(), 12)
220 self.assertEqual("01abcdefg123", writer.readfrom(0, 15))
221 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 7ac66c0f148de9519b8bd264312c4d64+7 0:2:count.txt 10:7:count.txt 1:3:count.txt\n", c.portable_manifest_text())
223 def test_write_large(self):
224 keep = ArvadosFileWriterTestCase.MockKeep({})
225 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
226 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n"},
227 {"uuid":"zzzzz-4zz18-mockcollection0",
228 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n"})
229 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
230 api_client=api, keep_client=keep) as c:
231 writer = c.open("count.txt", "r+")
232 text = "0123456789" * 100
233 for b in xrange(0, 100000):
235 self.assertEqual(writer.size(), 100000000)
237 self.assertIsNone(c.manifest_locator())
238 self.assertTrue(c.modified())
239 c.save_new("test_write_large")
240 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
241 self.assertFalse(c.modified())
244 def test_large_write(self):
245 keep = ArvadosFileWriterTestCase.MockKeep({})
246 api = ArvadosFileWriterTestCase.MockApi({}, {})
247 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
248 api_client=api, keep_client=keep) as c:
249 writer = c.open("count.txt", "r+")
250 self.assertEqual(writer.size(), 0)
254 text = "0123456789" * 9999999
256 self.assertEqual(writer.size(), 100000000)
258 self.assertEqual(c.manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 48dd23ea1645fd47d789804d71b5bb8e+67108864 77c57dc6ac5a10bb2205caaa73187994+32891126 0:100000000:count.txt\n")
260 def test_rewrite_on_empty_file(self):
261 keep = ArvadosFileWriterTestCase.MockKeep({})
262 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
263 keep_client=keep) as c:
264 writer = c.open("count.txt", "r+")
265 for b in xrange(0, 10):
266 writer.seek(0, os.SEEK_SET)
267 writer.write("0123456789")
269 self.assertEqual(writer.size(), 10)
270 self.assertEqual("0123456789", writer.readfrom(0, 20))
271 self.assertEqual(". 7a08b07e84641703e5f2c836aa59a170+100 90:10:count.txt\n", c.portable_manifest_text())
273 self.assertEqual(writer.size(), 10)
274 self.assertEqual("0123456789", writer.readfrom(0, 20))
275 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n", c.portable_manifest_text())
277 def test_rewrite_append_existing_file(self):
278 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
279 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
280 keep_client=keep) as c:
281 writer = c.open("count.txt", "r+")
282 for b in xrange(0, 10):
283 writer.seek(10, os.SEEK_SET)
284 writer.write("abcdefghij")
286 self.assertEqual(writer.size(), 20)
287 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
288 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:10:count.txt 100:10:count.txt\n", c.portable_manifest_text())
290 writer.arvadosfile.flush()
291 self.assertEqual(writer.size(), 20)
292 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
293 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n", c.portable_manifest_text())
295 def test_rewrite_over_existing_file(self):
296 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
297 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
298 keep_client=keep) as c:
299 writer = c.open("count.txt", "r+")
300 for b in xrange(0, 10):
301 writer.seek(5, os.SEEK_SET)
302 writer.write("abcdefghij")
304 self.assertEqual(writer.size(), 15)
305 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
306 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:5:count.txt 100:10:count.txt\n", c.portable_manifest_text())
308 writer.arvadosfile.flush()
310 self.assertEqual(writer.size(), 15)
311 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
312 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:5:count.txt 10:10:count.txt\n", c.portable_manifest_text())
314 def test_write_large_rewrite(self):
315 keep = ArvadosFileWriterTestCase.MockKeep({})
316 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
317 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"},
318 {"uuid":"zzzzz-4zz18-mockcollection0",
319 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"})
320 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
321 api_client=api, keep_client=keep) as c:
322 writer = c.open("count.txt", "r+")
323 text = ''.join(["0123456789" for a in xrange(0, 100)])
324 for b in xrange(0, 100000):
326 writer.seek(0, os.SEEK_SET)
328 self.assertEqual(writer.size(), 100000000)
330 self.assertIsNone(c.manifest_locator())
331 self.assertTrue(c.modified())
332 c.save_new("test_write_large")
333 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
334 self.assertFalse(c.modified())
336 def test_create(self):
337 keep = ArvadosFileWriterTestCase.MockKeep({})
338 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
339 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
340 {"uuid":"zzzzz-4zz18-mockcollection0",
341 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
342 with Collection(api_client=api, keep_client=keep) as c:
343 writer = c.open("count.txt", "w+")
344 self.assertEqual(writer.size(), 0)
345 writer.write("01234567")
346 self.assertEqual(writer.size(), 8)
348 self.assertIsNone(c.manifest_locator())
349 self.assertTrue(c.modified())
350 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
351 c.save_new("test_create")
352 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
353 self.assertFalse(c.modified())
354 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
357 def test_create_subdir(self):
358 keep = ArvadosFileWriterTestCase.MockKeep({})
359 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
360 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
361 {"uuid":"zzzzz-4zz18-mockcollection0",
362 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
363 with Collection(api_client=api, keep_client=keep) as c:
364 self.assertIsNone(c.api_response())
365 writer = c.open("foo/bar/count.txt", "w+")
366 writer.write("01234567")
367 self.assertFalse(c.committed())
368 c.save_new("test_create")
369 self.assertTrue(c.committed())
370 self.assertEqual(c.api_response(), api.response)
372 def test_overwrite(self):
373 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
374 api = ArvadosFileWriterTestCase.MockApi({"name":"test_overwrite",
375 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
376 {"uuid":"zzzzz-4zz18-mockcollection0",
377 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
378 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
379 api_client=api, keep_client=keep) as c:
380 writer = c.open("count.txt", "w+")
381 self.assertEqual(writer.size(), 0)
382 writer.write("01234567")
383 self.assertEqual(writer.size(), 8)
385 self.assertIsNone(c.manifest_locator())
386 self.assertTrue(c.modified())
387 c.save_new("test_overwrite")
388 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
389 self.assertFalse(c.modified())
391 def test_file_not_found(self):
392 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
393 with self.assertRaises(IOError):
394 writer = c.open("nocount.txt", "r")
396 def test_cannot_open_directory(self):
397 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
398 with self.assertRaises(IOError):
399 writer = c.open(".", "r")
401 def test_create_multiple(self):
402 keep = ArvadosFileWriterTestCase.MockKeep({})
403 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create_multiple",
404 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"},
405 {"uuid":"zzzzz-4zz18-mockcollection0",
406 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"})
407 with Collection(api_client=api, keep_client=keep) as c:
408 w1 = c.open("count1.txt", "w")
409 w2 = c.open("count2.txt", "w")
412 self.assertEqual(w1.size(), 8)
413 self.assertEqual(w2.size(), 8)
415 self.assertIsNone(c.manifest_locator())
416 self.assertTrue(c.modified())
417 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
418 c.save_new("test_create_multiple")
419 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
420 self.assertFalse(c.modified())
421 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
424 class ArvadosFileReaderTestCase(StreamFileReaderTestCase):
425 class MockParent(object):
426 class MockBlockMgr(object):
427 def __init__(self, blocks, nocache):
429 self.nocache = nocache
431 def block_prefetch(self, loc):
434 def get_block_contents(self, loc, num_retries=0, cache_only=False):
435 if self.nocache and cache_only:
437 return self.blocks[loc]
439 def __init__(self, blocks, nocache):
441 self.nocache = nocache
442 self.lock = arvados.arvfile.NoopLock()
444 def root_collection(self):
447 def _my_block_manager(self):
448 return ArvadosFileReaderTestCase.MockParent.MockBlockMgr(self.blocks, self.nocache)
451 def make_count_reader(self, nocache=False):
455 for d in ['01234', '34567', '67890']:
456 loc = '{}+{}'.format(hashlib.md5(d).hexdigest(), len(d))
458 stream.append(Range(loc, n, len(d)))
460 af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache), "count.txt", stream=stream, segments=[Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)])
461 return ArvadosFileReader(af)
463 def test_read_block_crossing_behavior(self):
464 # read() needs to return all the data requested if possible, even if it
465 # crosses uncached blocks: https://arvados.org/issues/5856
466 sfile = self.make_count_reader(nocache=True)
467 self.assertEqual('12345678', sfile.read(8))
469 def test_successive_reads(self):
470 # Override StreamFileReaderTestCase.test_successive_reads
471 sfile = self.make_count_reader(nocache=True)
472 self.assertEqual('1234', sfile.read(4))
473 self.assertEqual('5678', sfile.read(4))
474 self.assertEqual('9', sfile.read(4))
475 self.assertEqual('', sfile.read(4))
477 def test_tell_after_block_read(self):
478 # Override StreamFileReaderTestCase.test_tell_after_block_read
479 sfile = self.make_count_reader(nocache=True)
480 self.assertEqual('12345678', sfile.read(8))
481 self.assertEqual(8, sfile.tell())
483 def test_prefetch(self):
484 keep = ArvadosFileWriterTestCase.MockKeep({"2e9ec317e197819358fbc43afca7d837+8": "01234567", "e8dc4081b13434b45189a720b77b6818+8": "abcdefgh"})
485 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
486 r = c.open("count.txt", "r")
487 self.assertEqual("0123", r.read(4))
488 self.assertIn("2e9ec317e197819358fbc43afca7d837+8", keep.requests)
489 self.assertIn("e8dc4081b13434b45189a720b77b6818+8", keep.requests)
491 def test__eq__from_manifest(self):
492 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
493 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c2:
494 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
495 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
497 def test__eq__from_writes(self):
498 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
499 with Collection() as c2:
500 f = c2.open("count1.txt", "w")
501 f.write("0123456789")
503 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
504 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
506 def test__ne__(self):
507 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
508 with Collection() as c2:
509 f = c2.open("count1.txt", "w")
510 f.write("1234567890")
512 self.assertTrue(c1["count1.txt"] != c2["count1.txt"])
513 self.assertFalse(c1["count1.txt"] == c2["count1.txt"])
516 class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
517 def reader_for(self, coll_name, **kwargs):
521 for d in self.manifest_for(coll_name).split():
524 segments.append(Range(n, n, k.size))
525 stream.append(Range(d, n, k.size))
530 blockmanager = arvados.arvfile._BlockManager(self.keep_client())
531 blockmanager.prefetch_enabled = False
532 col = Collection(keep_client=self.keep_client(), block_manager=blockmanager)
533 af = ArvadosFile(col, "test",
536 return ArvadosFileReader(af, **kwargs)
538 def read_for_test(self, reader, byte_count, **kwargs):
539 return reader.read(byte_count, **kwargs)
542 class ArvadosFileReadFromTestCase(ArvadosFileReadTestCase):
543 def read_for_test(self, reader, byte_count, **kwargs):
544 return reader.readfrom(0, byte_count, **kwargs)
547 class ArvadosFileReadAllTestCase(ArvadosFileReadTestCase):
548 def read_for_test(self, reader, byte_count, **kwargs):
549 return ''.join(reader.readall(**kwargs))
552 class ArvadosFileReadAllDecompressedTestCase(ArvadosFileReadTestCase):
553 def read_for_test(self, reader, byte_count, **kwargs):
554 return ''.join(reader.readall_decompressed(**kwargs))
557 class ArvadosFileReadlinesTestCase(ArvadosFileReadTestCase):
558 def read_for_test(self, reader, byte_count, **kwargs):
559 return ''.join(reader.readlines(**kwargs))
561 class BlockManagerTest(unittest.TestCase):
562 def test_bufferblock_append(self):
563 keep = ArvadosFileWriterTestCase.MockKeep({})
564 with arvados.arvfile._BlockManager(keep) as blockmanager:
565 bufferblock = blockmanager.alloc_bufferblock()
566 bufferblock.append("foo")
568 self.assertEqual(bufferblock.size(), 3)
569 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
570 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
572 bufferblock.append("bar")
574 self.assertEqual(bufferblock.size(), 6)
575 self.assertEqual(bufferblock.buffer_view[0:6], "foobar")
576 self.assertEqual(bufferblock.locator(), "3858f62230ac3c915f300c664312c63f+6")
578 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
579 with self.assertRaises(arvados.errors.AssertionError):
580 bufferblock.append("bar")
582 def test_bufferblock_dup(self):
583 keep = ArvadosFileWriterTestCase.MockKeep({})
584 with arvados.arvfile._BlockManager(keep) as blockmanager:
585 bufferblock = blockmanager.alloc_bufferblock()
586 bufferblock.append("foo")
588 self.assertEqual(bufferblock.size(), 3)
589 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
590 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
591 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
593 bufferblock2 = blockmanager.dup_block(bufferblock, None)
594 self.assertNotEqual(bufferblock.blockid, bufferblock2.blockid)
596 bufferblock2.append("bar")
598 self.assertEqual(bufferblock2.size(), 6)
599 self.assertEqual(bufferblock2.buffer_view[0:6], "foobar")
600 self.assertEqual(bufferblock2.locator(), "3858f62230ac3c915f300c664312c63f+6")
602 self.assertEqual(bufferblock.size(), 3)
603 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
604 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
606 def test_bufferblock_get(self):
607 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
608 with arvados.arvfile._BlockManager(keep) as blockmanager:
609 bufferblock = blockmanager.alloc_bufferblock()
610 bufferblock.append("foo")
612 self.assertEqual(blockmanager.get_block_contents("781e5e245d69b566979b86e28d23f2c7+10", 1), "0123456789")
613 self.assertEqual(blockmanager.get_block_contents(bufferblock.blockid, 1), "foo")
615 def test_bufferblock_commit(self):
616 mockkeep = mock.MagicMock()
617 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
618 bufferblock = blockmanager.alloc_bufferblock()
619 bufferblock.owner = mock.MagicMock()
620 def flush(sync=None):
621 blockmanager.commit_bufferblock(bufferblock, sync)
622 bufferblock.owner.flush.side_effect = flush
623 bufferblock.append("foo")
624 blockmanager.commit_all()
625 self.assertTrue(bufferblock.owner.flush.called)
626 self.assertTrue(mockkeep.put.called)
627 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
628 self.assertIsNone(bufferblock.buffer_view)
630 def test_bufferblock_commit_pending(self):
632 mockkeep = mock.MagicMock()
633 mockkeep.put.side_effect = lambda x: time.sleep(1)
634 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
635 bufferblock = blockmanager.alloc_bufferblock()
636 bufferblock.append("foo")
638 blockmanager.commit_bufferblock(bufferblock, False)
639 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.PENDING)
641 blockmanager.commit_bufferblock(bufferblock, True)
642 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
645 def test_bufferblock_commit_with_error(self):
646 mockkeep = mock.MagicMock()
647 mockkeep.put.side_effect = arvados.errors.KeepWriteError("fail")
648 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
649 bufferblock = blockmanager.alloc_bufferblock()
650 bufferblock.owner = mock.MagicMock()
651 def flush(sync=None):
652 blockmanager.commit_bufferblock(bufferblock, sync)
653 bufferblock.owner.flush.side_effect = flush
654 bufferblock.append("foo")
655 with self.assertRaises(arvados.errors.KeepWriteError) as err:
656 blockmanager.commit_all()
657 self.assertTrue(bufferblock.owner.flush.called)
658 self.assertEqual(str(err.exception), "Error writing some blocks: block acbd18db4cc2f85cedef654fccc4a4d8+3 raised KeepWriteError (fail)")
659 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.ERROR)