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 = "%s+%i" % (hashlib.md5(data).hexdigest(), len(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.assertEqual(None, c.manifest_locator())
81 self.assertEqual(True, c.modified())
82 c.save_new("test_truncate")
83 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
84 self.assertEqual(False, 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.assertEqual(None, c.manifest_locator())
108 self.assertEqual(True, c.modified())
109 self.assertEqual(None, keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
111 c.save_new("test_append")
112 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
113 self.assertEqual(False, 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.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.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.manifest_text())
152 def test_write_in_middle(self):
153 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
154 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
155 keep_client=keep) as c:
156 writer = c.open("count.txt", "r+")
157 self.assertEqual("0123456789", writer.readfrom(0, 13))
158 writer.seek(3, os.SEEK_SET)
160 self.assertEqual(writer.size(), 10)
161 self.assertEqual("012foo6789", writer.readfrom(0, 13))
162 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:count.txt 10:3:count.txt 6:4:count.txt\n", c.manifest_text())
164 def test_write_at_end(self):
165 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
166 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
167 keep_client=keep) as c:
168 writer = c.open("count.txt", "r+")
169 self.assertEqual("0123456789", writer.readfrom(0, 13))
170 writer.seek(7, os.SEEK_SET)
172 self.assertEqual(writer.size(), 10)
173 self.assertEqual("0123456foo", writer.readfrom(0, 13))
174 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:7:count.txt 10:3:count.txt\n", c.manifest_text())
176 def test_write_across_segment_boundary(self):
177 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
178 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt 0:10:count.txt\n',
179 keep_client=keep) as c:
180 writer = c.open("count.txt", "r+")
181 self.assertEqual("012345678901234", writer.readfrom(0, 15))
182 writer.seek(7, os.SEEK_SET)
183 writer.write("foobar")
184 self.assertEqual(writer.size(), 20)
185 self.assertEqual("0123456foobar34", writer.readfrom(0, 15))
186 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 3858f62230ac3c915f300c664312c63f+6 0:7:count.txt 10:6:count.txt 3:7:count.txt\n", c.manifest_text())
188 def test_write_across_several_segments(self):
189 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
190 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:4:count.txt 0:4:count.txt 0:4:count.txt',
191 keep_client=keep) as c:
192 writer = c.open("count.txt", "r+")
193 self.assertEqual("012301230123", writer.readfrom(0, 15))
194 writer.seek(2, os.SEEK_SET)
195 writer.write("abcdefg")
196 self.assertEqual(writer.size(), 12)
197 self.assertEqual("01abcdefg123", writer.readfrom(0, 15))
198 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 7ac66c0f148de9519b8bd264312c4d64+7 0:2:count.txt 10:7:count.txt 1:3:count.txt\n", c.manifest_text())
200 def test_write_large(self):
201 keep = ArvadosFileWriterTestCase.MockKeep({})
202 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
203 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n"},
204 {"uuid":"zzzzz-4zz18-mockcollection0",
205 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n"})
206 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
207 api_client=api, keep_client=keep) as c:
208 writer = c.open("count.txt", "r+")
209 text = ''.join(["0123456789" for a in xrange(0, 100)])
210 for b in xrange(0, 100000):
212 self.assertEqual(writer.size(), 100000000)
214 self.assertEqual(None, c.manifest_locator())
215 self.assertEqual(True, c.modified())
216 c.save_new("test_write_large")
217 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
218 self.assertEqual(False, c.modified())
220 def test_rewrite_on_empty_file(self):
221 keep = ArvadosFileWriterTestCase.MockKeep({})
222 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
223 keep_client=keep) as c:
224 writer = c.open("count.txt", "r+")
225 for b in xrange(0, 10):
226 writer.seek(0, os.SEEK_SET)
227 writer.write("0123456789")
229 self.assertEqual(writer.size(), 10)
230 self.assertEqual("0123456789", writer.readfrom(0, 20))
231 self.assertEqual(". 7a08b07e84641703e5f2c836aa59a170+100 90:10:count.txt\n", c.manifest_text())
233 self.assertEqual(writer.size(), 10)
234 self.assertEqual("0123456789", writer.readfrom(0, 20))
235 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n", c.manifest_text())
237 def test_rewrite_append_existing_file(self):
238 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
239 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
240 keep_client=keep) as c:
241 writer = c.open("count.txt", "r+")
242 for b in xrange(0, 10):
243 writer.seek(10, os.SEEK_SET)
244 writer.write("abcdefghij")
246 self.assertEqual(writer.size(), 20)
247 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
248 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:10:count.txt 100:10:count.txt\n", c.manifest_text())
250 writer.arvadosfile.flush()
251 self.assertEqual(writer.size(), 20)
252 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
253 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n", c.manifest_text())
255 def test_rewrite_over_existing_file(self):
256 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
257 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
258 keep_client=keep) as c:
259 writer = c.open("count.txt", "r+")
260 for b in xrange(0, 10):
261 writer.seek(5, os.SEEK_SET)
262 writer.write("abcdefghij")
264 self.assertEqual(writer.size(), 15)
265 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
266 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:5:count.txt 100:10:count.txt\n", c.manifest_text())
268 writer.arvadosfile.flush()
270 self.assertEqual(writer.size(), 15)
271 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
272 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:5:count.txt 10:10:count.txt\n", c.manifest_text())
274 def test_write_large_rewrite(self):
275 keep = ArvadosFileWriterTestCase.MockKeep({})
276 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
277 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"},
278 {"uuid":"zzzzz-4zz18-mockcollection0",
279 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"})
280 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
281 api_client=api, keep_client=keep) as c:
282 writer = c.open("count.txt", "r+")
283 text = ''.join(["0123456789" for a in xrange(0, 100)])
284 for b in xrange(0, 100000):
286 writer.seek(0, os.SEEK_SET)
288 self.assertEqual(writer.size(), 100000000)
290 self.assertEqual(None, c.manifest_locator())
291 self.assertEqual(True, c.modified())
292 c.save_new("test_write_large")
293 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
294 self.assertEqual(False, c.modified())
296 def test_create(self):
297 keep = ArvadosFileWriterTestCase.MockKeep({})
298 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
299 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
300 {"uuid":"zzzzz-4zz18-mockcollection0",
301 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
302 with Collection(api_client=api, keep_client=keep) as c:
303 writer = c.open("count.txt", "w+")
304 self.assertEqual(writer.size(), 0)
305 writer.write("01234567")
306 self.assertEqual(writer.size(), 8)
308 self.assertEqual(None, c.manifest_locator())
309 self.assertEqual(True, c.modified())
310 self.assertEqual(None, keep.get("2e9ec317e197819358fbc43afca7d837+8"))
311 c.save_new("test_create")
312 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
313 self.assertEqual(False, c.modified())
314 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
317 def test_create_subdir(self):
318 keep = ArvadosFileWriterTestCase.MockKeep({})
319 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
320 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
321 {"uuid":"zzzzz-4zz18-mockcollection0",
322 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
323 with Collection(api_client=api, keep_client=keep) as c:
324 self.assertIsNone(c.api_response())
325 writer = c.open("foo/bar/count.txt", "w+")
326 writer.write("01234567")
327 c.save_new("test_create")
328 self.assertEqual(c.api_response(), api.response)
330 def test_overwrite(self):
331 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
332 api = ArvadosFileWriterTestCase.MockApi({"name":"test_overwrite",
333 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
334 {"uuid":"zzzzz-4zz18-mockcollection0",
335 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
336 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
337 api_client=api, keep_client=keep) as c:
338 writer = c.open("count.txt", "w+")
339 self.assertEqual(writer.size(), 0)
340 writer.write("01234567")
341 self.assertEqual(writer.size(), 8)
343 self.assertEqual(None, c.manifest_locator())
344 self.assertEqual(True, c.modified())
345 c.save_new("test_overwrite")
346 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
347 self.assertEqual(False, c.modified())
349 def test_file_not_found(self):
350 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
351 with self.assertRaises(IOError):
352 writer = c.open("nocount.txt", "r")
354 def test_cannot_open_directory(self):
355 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
356 with self.assertRaises(IOError):
357 writer = c.open(".", "r")
359 def test_create_multiple(self):
360 keep = ArvadosFileWriterTestCase.MockKeep({})
361 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create_multiple",
362 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"},
363 {"uuid":"zzzzz-4zz18-mockcollection0",
364 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"})
365 with Collection(api_client=api, keep_client=keep) as c:
366 w1 = c.open("count1.txt", "w")
367 w2 = c.open("count2.txt", "w")
370 self.assertEqual(w1.size(), 8)
371 self.assertEqual(w2.size(), 8)
373 self.assertEqual(None, c.manifest_locator())
374 self.assertEqual(True, c.modified())
375 self.assertEqual(None, keep.get("2e9ec317e197819358fbc43afca7d837+8"))
376 c.save_new("test_create_multiple")
377 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
378 self.assertEqual(False, c.modified())
379 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
382 class ArvadosFileReaderTestCase(StreamFileReaderTestCase):
383 class MockParent(object):
384 class MockBlockMgr(object):
385 def __init__(self, blocks, nocache):
387 self.nocache = nocache
389 def block_prefetch(self, loc):
392 def get_block_contents(self, loc, num_retries=0, cache_only=False):
393 if self.nocache and cache_only:
395 return self.blocks[loc]
397 def __init__(self, blocks, nocache):
399 self.nocache = nocache
400 self.lock = arvados.arvfile.NoopLock()
402 def root_collection(self):
405 def _my_block_manager(self):
406 return ArvadosFileReaderTestCase.MockParent.MockBlockMgr(self.blocks, self.nocache)
409 def make_count_reader(self, nocache=False):
413 for d in ['01234', '34567', '67890']:
414 loc = '{}+{}'.format(hashlib.md5(d).hexdigest(), len(d))
416 stream.append(Range(loc, n, len(d)))
418 af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache), stream=stream, segments=[Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)])
419 return ArvadosFileReader(af, "count.txt")
421 def test_read_returns_first_block(self):
422 # read() calls will be aligned on block boundaries - see #3663.
423 sfile = self.make_count_reader(nocache=True)
424 self.assertEqual('123', sfile.read(10))
426 def test_successive_reads(self):
427 sfile = self.make_count_reader(nocache=True)
428 for expect in ['123', '456', '789', '']:
429 self.assertEqual(expect, sfile.read(10))
431 def test_tell_after_block_read(self):
432 sfile = self.make_count_reader(nocache=True)
434 self.assertEqual(3, sfile.tell())
436 def test_prefetch(self):
437 keep = ArvadosFileWriterTestCase.MockKeep({"2e9ec317e197819358fbc43afca7d837+8": "01234567", "e8dc4081b13434b45189a720b77b6818+8": "abcdefgh"})
438 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
439 r = c.open("count.txt", "r")
440 self.assertEqual("0123", r.read(4))
441 self.assertIn("2e9ec317e197819358fbc43afca7d837+8", keep.requests)
442 self.assertIn("e8dc4081b13434b45189a720b77b6818+8", keep.requests)
444 def test__eq__from_manifest(self):
445 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
446 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c2:
447 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
448 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
450 def test__eq__from_writes(self):
451 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
452 with Collection() as c2:
453 with c2.open("count1.txt", "w") as f:
454 f.write("0123456789")
456 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
457 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
459 def test__ne__(self):
460 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
461 with Collection() as c2:
462 with c2.open("count1.txt", "w") as f:
463 f.write("1234567890")
465 self.assertTrue(c1["count1.txt"] != c2["count1.txt"])
466 self.assertFalse(c1["count1.txt"] == c2["count1.txt"])
469 class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
470 def reader_for(self, coll_name, **kwargs):
474 for d in self.manifest_for(coll_name).split():
477 segments.append(Range(n, n, k.size))
478 stream.append(Range(d, n, k.size))
483 blockmanager = arvados.arvfile._BlockManager(self.keep_client())
484 blockmanager.prefetch_enabled = False
485 col = Collection(keep_client=self.keep_client(), block_manager=blockmanager)
486 af = ArvadosFile(col,
489 return ArvadosFileReader(af, "test", **kwargs)
491 def read_for_test(self, reader, byte_count, **kwargs):
492 return reader.read(byte_count, **kwargs)
495 class ArvadosFileReadFromTestCase(ArvadosFileReadTestCase):
496 def read_for_test(self, reader, byte_count, **kwargs):
497 return reader.readfrom(0, byte_count, **kwargs)
500 class ArvadosFileReadAllTestCase(ArvadosFileReadTestCase):
501 def read_for_test(self, reader, byte_count, **kwargs):
502 return ''.join(reader.readall(**kwargs))
505 class ArvadosFileReadAllDecompressedTestCase(ArvadosFileReadTestCase):
506 def read_for_test(self, reader, byte_count, **kwargs):
507 return ''.join(reader.readall_decompressed(**kwargs))
510 class ArvadosFileReadlinesTestCase(ArvadosFileReadTestCase):
511 def read_for_test(self, reader, byte_count, **kwargs):
512 return ''.join(reader.readlines(**kwargs))
514 class BlockManagerTest(unittest.TestCase):
515 def test_bufferblock_append(self):
516 keep = ArvadosFileWriterTestCase.MockKeep({})
517 blockmanager = arvados.arvfile._BlockManager(keep)
518 bufferblock = blockmanager.alloc_bufferblock()
519 bufferblock.append("foo")
521 self.assertEqual(bufferblock.size(), 3)
522 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
523 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
525 bufferblock.append("bar")
527 self.assertEqual(bufferblock.size(), 6)
528 self.assertEqual(bufferblock.buffer_view[0:6], "foobar")
529 self.assertEqual(bufferblock.locator(), "3858f62230ac3c915f300c664312c63f+6")
531 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
532 with self.assertRaises(arvados.errors.AssertionError):
533 bufferblock.append("bar")
535 def test_bufferblock_dup(self):
536 keep = ArvadosFileWriterTestCase.MockKeep({})
537 blockmanager = arvados.arvfile._BlockManager(keep)
538 bufferblock = blockmanager.alloc_bufferblock()
539 bufferblock.append("foo")
541 self.assertEqual(bufferblock.size(), 3)
542 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
543 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
544 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
546 bufferblock2 = blockmanager.dup_block(bufferblock, None)
547 self.assertNotEqual(bufferblock.blockid, bufferblock2.blockid)
549 bufferblock2.append("bar")
551 self.assertEqual(bufferblock2.size(), 6)
552 self.assertEqual(bufferblock2.buffer_view[0:6], "foobar")
553 self.assertEqual(bufferblock2.locator(), "3858f62230ac3c915f300c664312c63f+6")
555 self.assertEqual(bufferblock.size(), 3)
556 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
557 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
559 def test_bufferblock_get(self):
560 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
561 blockmanager = arvados.arvfile._BlockManager(keep)
562 bufferblock = blockmanager.alloc_bufferblock()
563 bufferblock.append("foo")
565 self.assertEqual(blockmanager.get_block_contents("781e5e245d69b566979b86e28d23f2c7+10", 1), "0123456789")
566 self.assertEqual(blockmanager.get_block_contents(bufferblock.blockid, 1), "foo")
568 def test_bufferblock_commit(self):
569 mockkeep = mock.MagicMock()
570 blockmanager = arvados.arvfile._BlockManager(mockkeep)
571 bufferblock = blockmanager.alloc_bufferblock()
572 bufferblock.append("foo")
573 blockmanager.commit_all()
574 self.assertTrue(mockkeep.put.called)
575 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
576 self.assertIsNone(bufferblock.buffer_view)
579 def test_bufferblock_commit_with_error(self):
580 mockkeep = mock.MagicMock()
581 mockkeep.put.side_effect = arvados.errors.KeepWriteError("fail")
582 blockmanager = arvados.arvfile._BlockManager(mockkeep)
583 bufferblock = blockmanager.alloc_bufferblock()
584 bufferblock.append("foo")
585 with self.assertRaises(arvados.errors.KeepWriteError) as err:
586 blockmanager.commit_all()
587 self.assertEqual(str(err.exception), "Error writing some blocks: block acbd18db4cc2f85cedef654fccc4a4d8+3 raised KeepWriteError (fail)")
588 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.PENDING)