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.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 = ''.join(["0123456789" for a in xrange(0, 100)])
232 for b in xrange(0, 100000):
234 self.assertEqual(writer.size(), 100000000)
236 self.assertEqual(None, c.manifest_locator())
237 self.assertEqual(True, c.modified())
238 c.save_new("test_write_large")
239 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
240 self.assertEqual(False, c.modified())
242 def test_rewrite_on_empty_file(self):
243 keep = ArvadosFileWriterTestCase.MockKeep({})
244 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
245 keep_client=keep) as c:
246 writer = c.open("count.txt", "r+")
247 for b in xrange(0, 10):
248 writer.seek(0, os.SEEK_SET)
249 writer.write("0123456789")
251 self.assertEqual(writer.size(), 10)
252 self.assertEqual("0123456789", writer.readfrom(0, 20))
253 self.assertEqual(". 7a08b07e84641703e5f2c836aa59a170+100 90:10:count.txt\n", c.portable_manifest_text())
255 self.assertEqual(writer.size(), 10)
256 self.assertEqual("0123456789", writer.readfrom(0, 20))
257 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n", c.portable_manifest_text())
259 def test_rewrite_append_existing_file(self):
260 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
261 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10: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(10, os.SEEK_SET)
266 writer.write("abcdefghij")
268 self.assertEqual(writer.size(), 20)
269 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
270 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:10:count.txt 100:10:count.txt\n", c.portable_manifest_text())
272 writer.arvadosfile.flush()
273 self.assertEqual(writer.size(), 20)
274 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
275 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n", c.portable_manifest_text())
277 def test_rewrite_over_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(5, os.SEEK_SET)
284 writer.write("abcdefghij")
286 self.assertEqual(writer.size(), 15)
287 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
288 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:5:count.txt 100:10:count.txt\n", c.portable_manifest_text())
290 writer.arvadosfile.flush()
292 self.assertEqual(writer.size(), 15)
293 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
294 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:5:count.txt 10:10:count.txt\n", c.portable_manifest_text())
296 def test_write_large_rewrite(self):
297 keep = ArvadosFileWriterTestCase.MockKeep({})
298 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
299 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"},
300 {"uuid":"zzzzz-4zz18-mockcollection0",
301 "manifest_text": ". 37400a68af9abdd76ca5bf13e819e42a+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 32892000:3:count.txt 32892006:67107997:count.txt 0:32892000:count.txt\n"})
302 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
303 api_client=api, keep_client=keep) as c:
304 writer = c.open("count.txt", "r+")
305 text = ''.join(["0123456789" for a in xrange(0, 100)])
306 for b in xrange(0, 100000):
308 writer.seek(0, os.SEEK_SET)
310 self.assertEqual(writer.size(), 100000000)
312 self.assertEqual(None, c.manifest_locator())
313 self.assertEqual(True, c.modified())
314 c.save_new("test_write_large")
315 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
316 self.assertEqual(False, c.modified())
318 def test_create(self):
319 keep = ArvadosFileWriterTestCase.MockKeep({})
320 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
321 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
322 {"uuid":"zzzzz-4zz18-mockcollection0",
323 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
324 with Collection(api_client=api, keep_client=keep) as c:
325 writer = c.open("count.txt", "w+")
326 self.assertEqual(writer.size(), 0)
327 writer.write("01234567")
328 self.assertEqual(writer.size(), 8)
330 self.assertEqual(None, c.manifest_locator())
331 self.assertEqual(True, c.modified())
332 self.assertEqual(None, keep.get("2e9ec317e197819358fbc43afca7d837+8"))
333 c.save_new("test_create")
334 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
335 self.assertEqual(False, c.modified())
336 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
339 def test_create_subdir(self):
340 keep = ArvadosFileWriterTestCase.MockKeep({})
341 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
342 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
343 {"uuid":"zzzzz-4zz18-mockcollection0",
344 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
345 with Collection(api_client=api, keep_client=keep) as c:
346 self.assertIsNone(c.api_response())
347 writer = c.open("foo/bar/count.txt", "w+")
348 writer.write("01234567")
349 c.save_new("test_create")
350 self.assertEqual(c.api_response(), api.response)
352 def test_overwrite(self):
353 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
354 api = ArvadosFileWriterTestCase.MockApi({"name":"test_overwrite",
355 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"},
356 {"uuid":"zzzzz-4zz18-mockcollection0",
357 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n"})
358 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
359 api_client=api, keep_client=keep) as c:
360 writer = c.open("count.txt", "w+")
361 self.assertEqual(writer.size(), 0)
362 writer.write("01234567")
363 self.assertEqual(writer.size(), 8)
365 self.assertEqual(None, c.manifest_locator())
366 self.assertEqual(True, c.modified())
367 c.save_new("test_overwrite")
368 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
369 self.assertEqual(False, c.modified())
371 def test_file_not_found(self):
372 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
373 with self.assertRaises(IOError):
374 writer = c.open("nocount.txt", "r")
376 def test_cannot_open_directory(self):
377 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
378 with self.assertRaises(IOError):
379 writer = c.open(".", "r")
381 def test_create_multiple(self):
382 keep = ArvadosFileWriterTestCase.MockKeep({})
383 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create_multiple",
384 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"},
385 {"uuid":"zzzzz-4zz18-mockcollection0",
386 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n"})
387 with Collection(api_client=api, keep_client=keep) as c:
388 w1 = c.open("count1.txt", "w")
389 w2 = c.open("count2.txt", "w")
392 self.assertEqual(w1.size(), 8)
393 self.assertEqual(w2.size(), 8)
395 self.assertEqual(None, c.manifest_locator())
396 self.assertEqual(True, c.modified())
397 self.assertEqual(None, keep.get("2e9ec317e197819358fbc43afca7d837+8"))
398 c.save_new("test_create_multiple")
399 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
400 self.assertEqual(False, c.modified())
401 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
404 class ArvadosFileReaderTestCase(StreamFileReaderTestCase):
405 class MockParent(object):
406 class MockBlockMgr(object):
407 def __init__(self, blocks, nocache):
409 self.nocache = nocache
411 def block_prefetch(self, loc):
414 def get_block_contents(self, loc, num_retries=0, cache_only=False):
415 if self.nocache and cache_only:
417 return self.blocks[loc]
419 def __init__(self, blocks, nocache):
421 self.nocache = nocache
422 self.lock = arvados.arvfile.NoopLock()
424 def root_collection(self):
427 def _my_block_manager(self):
428 return ArvadosFileReaderTestCase.MockParent.MockBlockMgr(self.blocks, self.nocache)
431 def make_count_reader(self, nocache=False):
435 for d in ['01234', '34567', '67890']:
436 loc = '{}+{}'.format(hashlib.md5(d).hexdigest(), len(d))
438 stream.append(Range(loc, n, len(d)))
440 af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache), stream=stream, segments=[Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)])
441 return ArvadosFileReader(af, "count.txt")
443 def test_read_returns_first_block(self):
444 # read() calls will be aligned on block boundaries - see #3663.
445 sfile = self.make_count_reader(nocache=True)
446 self.assertEqual('123', sfile.read(10))
448 def test_successive_reads(self):
449 sfile = self.make_count_reader(nocache=True)
450 for expect in ['123', '456', '789', '']:
451 self.assertEqual(expect, sfile.read(10))
453 def test_tell_after_block_read(self):
454 sfile = self.make_count_reader(nocache=True)
456 self.assertEqual(3, sfile.tell())
458 def test_prefetch(self):
459 keep = ArvadosFileWriterTestCase.MockKeep({"2e9ec317e197819358fbc43afca7d837+8": "01234567", "e8dc4081b13434b45189a720b77b6818+8": "abcdefgh"})
460 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
461 r = c.open("count.txt", "r")
462 self.assertEqual("0123", r.read(4))
463 self.assertIn("2e9ec317e197819358fbc43afca7d837+8", keep.requests)
464 self.assertIn("e8dc4081b13434b45189a720b77b6818+8", keep.requests)
466 def test__eq__from_manifest(self):
467 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
468 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c2:
469 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
470 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
472 def test__eq__from_writes(self):
473 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
474 with Collection() as c2:
475 with c2.open("count1.txt", "w") as f:
476 f.write("0123456789")
478 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
479 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
481 def test__ne__(self):
482 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
483 with Collection() as c2:
484 with c2.open("count1.txt", "w") as f:
485 f.write("1234567890")
487 self.assertTrue(c1["count1.txt"] != c2["count1.txt"])
488 self.assertFalse(c1["count1.txt"] == c2["count1.txt"])
491 class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
492 def reader_for(self, coll_name, **kwargs):
496 for d in self.manifest_for(coll_name).split():
499 segments.append(Range(n, n, k.size))
500 stream.append(Range(d, n, k.size))
505 blockmanager = arvados.arvfile._BlockManager(self.keep_client())
506 blockmanager.prefetch_enabled = False
507 col = Collection(keep_client=self.keep_client(), block_manager=blockmanager)
508 af = ArvadosFile(col,
511 return ArvadosFileReader(af, "test", **kwargs)
513 def read_for_test(self, reader, byte_count, **kwargs):
514 return reader.read(byte_count, **kwargs)
517 class ArvadosFileReadFromTestCase(ArvadosFileReadTestCase):
518 def read_for_test(self, reader, byte_count, **kwargs):
519 return reader.readfrom(0, byte_count, **kwargs)
522 class ArvadosFileReadAllTestCase(ArvadosFileReadTestCase):
523 def read_for_test(self, reader, byte_count, **kwargs):
524 return ''.join(reader.readall(**kwargs))
527 class ArvadosFileReadAllDecompressedTestCase(ArvadosFileReadTestCase):
528 def read_for_test(self, reader, byte_count, **kwargs):
529 return ''.join(reader.readall_decompressed(**kwargs))
532 class ArvadosFileReadlinesTestCase(ArvadosFileReadTestCase):
533 def read_for_test(self, reader, byte_count, **kwargs):
534 return ''.join(reader.readlines(**kwargs))
536 class BlockManagerTest(unittest.TestCase):
537 def test_bufferblock_append(self):
538 keep = ArvadosFileWriterTestCase.MockKeep({})
539 blockmanager = arvados.arvfile._BlockManager(keep)
540 bufferblock = blockmanager.alloc_bufferblock()
541 bufferblock.append("foo")
543 self.assertEqual(bufferblock.size(), 3)
544 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
545 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
547 bufferblock.append("bar")
549 self.assertEqual(bufferblock.size(), 6)
550 self.assertEqual(bufferblock.buffer_view[0:6], "foobar")
551 self.assertEqual(bufferblock.locator(), "3858f62230ac3c915f300c664312c63f+6")
553 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
554 with self.assertRaises(arvados.errors.AssertionError):
555 bufferblock.append("bar")
557 def test_bufferblock_dup(self):
558 keep = ArvadosFileWriterTestCase.MockKeep({})
559 blockmanager = arvados.arvfile._BlockManager(keep)
560 bufferblock = blockmanager.alloc_bufferblock()
561 bufferblock.append("foo")
563 self.assertEqual(bufferblock.size(), 3)
564 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
565 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
566 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
568 bufferblock2 = blockmanager.dup_block(bufferblock, None)
569 self.assertNotEqual(bufferblock.blockid, bufferblock2.blockid)
571 bufferblock2.append("bar")
573 self.assertEqual(bufferblock2.size(), 6)
574 self.assertEqual(bufferblock2.buffer_view[0:6], "foobar")
575 self.assertEqual(bufferblock2.locator(), "3858f62230ac3c915f300c664312c63f+6")
577 self.assertEqual(bufferblock.size(), 3)
578 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
579 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
581 def test_bufferblock_get(self):
582 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
583 blockmanager = arvados.arvfile._BlockManager(keep)
584 bufferblock = blockmanager.alloc_bufferblock()
585 bufferblock.append("foo")
587 self.assertEqual(blockmanager.get_block_contents("781e5e245d69b566979b86e28d23f2c7+10", 1), "0123456789")
588 self.assertEqual(blockmanager.get_block_contents(bufferblock.blockid, 1), "foo")
590 def test_bufferblock_commit(self):
591 mockkeep = mock.MagicMock()
592 blockmanager = arvados.arvfile._BlockManager(mockkeep)
593 bufferblock = blockmanager.alloc_bufferblock()
594 bufferblock.owner = mock.MagicMock()
595 bufferblock.owner.flush.side_effect = lambda: blockmanager.commit_bufferblock(bufferblock)
596 bufferblock.append("foo")
597 blockmanager.commit_all()
598 self.assertTrue(bufferblock.owner.flush.called)
599 self.assertTrue(mockkeep.put.called)
600 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
601 self.assertIsNone(bufferblock.buffer_view)
604 def test_bufferblock_commit_with_error(self):
605 mockkeep = mock.MagicMock()
606 mockkeep.put.side_effect = arvados.errors.KeepWriteError("fail")
607 blockmanager = arvados.arvfile._BlockManager(mockkeep)
608 bufferblock = blockmanager.alloc_bufferblock()
609 bufferblock.owner = mock.MagicMock()
610 bufferblock.owner.flush.side_effect = lambda: blockmanager.commit_bufferblock(bufferblock)
611 bufferblock.append("foo")
612 with self.assertRaises(arvados.errors.KeepWriteError) as err:
613 blockmanager.commit_all()
614 self.assertTrue(bufferblock.owner.flush.called)
615 self.assertEqual(str(err.exception), "Error writing some blocks: block acbd18db4cc2f85cedef654fccc4a4d8+3 raised KeepWriteError (fail)")
616 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.PENDING)