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, copies=None):
33 pdh = tutil.str_keep_locator(data)
34 self.blocks[pdh] = str(data)
37 class MockApi(object):
38 def __init__(self, b, r):
41 self._schema = ArvadosFileWriterTestCase.MockApi.MockSchema()
43 class MockSchema(object):
45 self.schemas = {'Collection': {'properties': {'replication_desired': {'type':'integer'}}}}
46 class MockCollections(object):
47 def __init__(self, b, r):
50 class Execute(object):
51 def __init__(self, r):
53 def execute(self, num_retries=None):
55 def create(self, ensure_unique_name=False, body=None):
57 raise Exception("Body %s does not match expectation %s" % (body, self.body))
58 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
59 def update(self, uuid=None, body=None):
60 return ArvadosFileWriterTestCase.MockApi.MockCollections.Execute(self.response)
61 def collections(self):
62 return ArvadosFileWriterTestCase.MockApi.MockCollections(self.body, self.response)
65 def test_truncate(self):
66 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
67 api = ArvadosFileWriterTestCase.MockApi({"name":"test_truncate",
68 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
69 "replication_desired":None},
70 {"uuid":"zzzzz-4zz18-mockcollection0",
71 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
72 "portable_data_hash":"7fcd0eaac3aad4c31a6a0e756475da92+52"})
73 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
74 api_client=api, keep_client=keep) as c:
75 writer = c.open("count.txt", "r+")
76 self.assertEqual(writer.size(), 10)
77 self.assertEqual("0123456789", writer.read(12))
81 # Make sure reading off the end doesn't break
82 self.assertEqual("", writer.read(12))
84 self.assertEqual(writer.size(), 8)
85 writer.seek(0, os.SEEK_SET)
86 self.assertEqual("01234567", writer.read(12))
88 self.assertIsNone(c.manifest_locator())
89 self.assertTrue(c.modified())
90 c.save_new("test_truncate")
91 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
92 self.assertFalse(c.modified())
95 def test_truncate2(self):
96 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
97 api = ArvadosFileWriterTestCase.MockApi({"name":"test_truncate2",
98 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 7f614da9329cd3aebf59b91aadc30bf0+67108864 0:12:count.txt\n",
99 "replication_desired":None},
100 {"uuid":"zzzzz-4zz18-mockcollection0",
101 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 7f614da9329cd3aebf59b91aadc30bf0+67108864 0:12:count.txt\n",
102 "portable_data_hash":"272da898abdf86ddc71994835e3155f8+95"})
103 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
104 api_client=api, keep_client=keep) as c:
105 writer = c.open("count.txt", "r+")
106 self.assertEqual(writer.size(), 10)
107 self.assertEqual("0123456789", writer.read(12))
112 self.assertEqual(writer.size(), 12)
113 writer.seek(0, os.SEEK_SET)
114 self.assertEqual(b"0123456789\x00\x00", writer.read(12))
116 self.assertIsNone(c.manifest_locator())
117 self.assertTrue(c.modified())
118 c.save_new("test_truncate2")
119 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
120 self.assertFalse(c.modified())
122 def test_truncate3(self):
123 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789",
124 "a925576942e94b2ef57a066101b48876+10": "abcdefghij"})
125 api = ArvadosFileWriterTestCase.MockApi({"name":"test_truncate",
126 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
127 "replication_desired":None},
128 {"uuid":"zzzzz-4zz18-mockcollection0",
129 "manifest_text":". 781e5e245d69b566979b86e28d23f2c7+10 0:8:count.txt\n",
130 "portable_data_hash":"7fcd0eaac3aad4c31a6a0e756475da92+52"})
131 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n',
132 api_client=api, keep_client=keep) as c:
133 writer = c.open("count.txt", "r+")
134 self.assertEqual(writer.size(), 20)
135 self.assertEqual("0123456789ab", writer.read(12))
136 self.assertEqual(12, writer.tell())
140 # Make sure reading off the end doesn't break
141 self.assertEqual(12, writer.tell())
142 self.assertEqual("", writer.read(12))
144 self.assertEqual(writer.size(), 8)
145 self.assertEqual(2, writer.seek(-10, os.SEEK_CUR))
146 self.assertEqual("234567", writer.read(12))
148 self.assertIsNone(c.manifest_locator())
149 self.assertTrue(c.modified())
150 c.save_new("test_truncate")
151 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
152 self.assertFalse(c.modified())
156 def test_write_to_end(self):
157 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
158 api = ArvadosFileWriterTestCase.MockApi({"name":"test_append",
159 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n",
160 "replication_desired":None},
161 {"uuid":"zzzzz-4zz18-mockcollection0",
162 "manifest_text": ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:13:count.txt\n",
163 "portable_data_hash":"c5c3af76565c8efb6a806546bcf073f3+88"})
164 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
165 api_client=api, keep_client=keep) as c:
166 writer = c.open("count.txt", "r+")
167 self.assertEqual(writer.size(), 10)
169 self.assertEqual(5, writer.seek(5, os.SEEK_SET))
170 self.assertEqual("56789", writer.read(8))
172 writer.seek(10, os.SEEK_SET)
174 self.assertEqual(writer.size(), 13)
176 writer.seek(5, os.SEEK_SET)
177 self.assertEqual("56789foo", writer.read(8))
179 self.assertIsNone(c.manifest_locator())
180 self.assertTrue(c.modified())
181 self.assertIsNone(keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
183 c.save_new("test_append")
184 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
185 self.assertFalse(c.modified())
186 self.assertEqual("foo", keep.get("acbd18db4cc2f85cedef654fccc4a4d8+3"))
189 def test_append(self):
190 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
191 c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', keep_client=keep)
192 writer = c.open("count.txt", "a+")
193 self.assertEqual(writer.read(20), "0123456789")
194 writer.seek(0, os.SEEK_SET)
196 writer.write("hello")
197 self.assertEqual(writer.read(20), "0123456789hello")
198 writer.seek(0, os.SEEK_SET)
200 writer.write("world")
201 self.assertEqual(writer.read(20), "0123456789helloworld")
203 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 fc5e038d38a57032085441e7fe7010b0+10 0:20:count.txt\n", c.portable_manifest_text())
205 def test_write_at_beginning(self):
206 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
207 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
208 keep_client=keep) as c:
209 writer = c.open("count.txt", "r+")
210 self.assertEqual("0123456789", writer.readfrom(0, 13))
211 writer.seek(0, os.SEEK_SET)
213 self.assertEqual(writer.size(), 10)
214 self.assertEqual("foo3456789", writer.readfrom(0, 13))
215 self.assertEqual(". acbd18db4cc2f85cedef654fccc4a4d8+3 781e5e245d69b566979b86e28d23f2c7+10 0:3:count.txt 6:7:count.txt\n", c.portable_manifest_text())
217 def test_write_empty(self):
218 keep = ArvadosFileWriterTestCase.MockKeep({})
219 with Collection(keep_client=keep) as c:
220 writer = c.open("count.txt", "w")
221 self.assertEqual(writer.size(), 0)
222 self.assertEqual(". d41d8cd98f00b204e9800998ecf8427e+0 0:0:count.txt\n", c.portable_manifest_text())
224 def test_save_manifest_text(self):
225 keep = ArvadosFileWriterTestCase.MockKeep({})
226 with Collection(keep_client=keep) as c:
227 writer = c.open("count.txt", "w")
228 writer.write("0123456789")
229 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
230 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
232 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.save_new(create_collection_record=False))
233 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
235 def test_get_manifest_text_commits(self):
236 keep = ArvadosFileWriterTestCase.MockKeep({})
237 with Collection(keep_client=keep) as c:
238 writer = c.open("count.txt", "w")
239 writer.write("0123456789")
240 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.portable_manifest_text())
241 self.assertNotIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
242 self.assertEqual('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n', c.manifest_text())
243 self.assertIn('781e5e245d69b566979b86e28d23f2c7+10', keep.blocks)
246 def test_write_in_middle(self):
247 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
248 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
249 keep_client=keep) as c:
250 writer = c.open("count.txt", "r+")
251 self.assertEqual("0123456789", writer.readfrom(0, 13))
252 writer.seek(3, os.SEEK_SET)
254 self.assertEqual(writer.size(), 10)
255 self.assertEqual("012foo6789", writer.readfrom(0, 13))
256 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:count.txt 10:3:count.txt 6:4:count.txt\n", c.portable_manifest_text())
258 def test_write_at_end(self):
259 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
260 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
261 keep_client=keep) as c:
262 writer = c.open("count.txt", "r+")
263 self.assertEqual("0123456789", writer.readfrom(0, 13))
264 writer.seek(7, os.SEEK_SET)
266 self.assertEqual(writer.size(), 10)
267 self.assertEqual("0123456foo", writer.readfrom(0, 13))
268 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:7:count.txt 10:3:count.txt\n", c.portable_manifest_text())
270 def test_write_across_segment_boundary(self):
271 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
272 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt 0:10:count.txt\n',
273 keep_client=keep) as c:
274 writer = c.open("count.txt", "r+")
275 self.assertEqual("012345678901234", writer.readfrom(0, 15))
276 writer.seek(7, os.SEEK_SET)
277 writer.write("foobar")
278 self.assertEqual(writer.size(), 20)
279 self.assertEqual("0123456foobar34", writer.readfrom(0, 15))
280 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 3858f62230ac3c915f300c664312c63f+6 0:7:count.txt 10:6:count.txt 3:7:count.txt\n", c.portable_manifest_text())
282 def test_write_across_several_segments(self):
283 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
284 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:4:count.txt 0:4:count.txt 0:4:count.txt',
285 keep_client=keep) as c:
286 writer = c.open("count.txt", "r+")
287 self.assertEqual("012301230123", writer.readfrom(0, 15))
288 writer.seek(2, os.SEEK_SET)
289 writer.write("abcdefg")
290 self.assertEqual(writer.size(), 12)
291 self.assertEqual("01abcdefg123", writer.readfrom(0, 15))
292 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 7ac66c0f148de9519b8bd264312c4d64+7 0:2:count.txt 10:7:count.txt 1:3:count.txt\n", c.portable_manifest_text())
294 def test_write_large(self):
295 keep = ArvadosFileWriterTestCase.MockKeep({})
296 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
297 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n",
298 "replication_desired":None},
299 {"uuid":"zzzzz-4zz18-mockcollection0",
300 "manifest_text": ". a5de24f4417cfba9d5825eadc2f4ca49+67108000 598cc1a4ccaef8ab6e4724d87e675d78+32892000 0:100000000:count.txt\n",
301 "portable_data_hash":"9132ca8e3f671c76103a38f5bc24328c+108"})
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 = "0123456789" * 100
306 for b in xrange(0, 100000):
308 self.assertEqual(writer.size(), 100000000)
310 self.assertIsNone(c.manifest_locator())
311 self.assertTrue(c.modified())
312 c.save_new("test_write_large")
313 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
314 self.assertFalse(c.modified())
317 def test_large_write(self):
318 keep = ArvadosFileWriterTestCase.MockKeep({})
319 api = ArvadosFileWriterTestCase.MockApi({}, {})
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 self.assertEqual(writer.size(), 0)
327 text = "0123456789" * 9999999
329 self.assertEqual(writer.size(), 100000000)
331 self.assertEqual(c.manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 48dd23ea1645fd47d789804d71b5bb8e+67108864 77c57dc6ac5a10bb2205caaa73187994+32891126 0:100000000:count.txt\n")
333 def test_sparse_write(self):
334 keep = ArvadosFileWriterTestCase.MockKeep({})
335 api = ArvadosFileWriterTestCase.MockApi({}, {})
336 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
337 api_client=api, keep_client=keep) as c:
338 writer = c.open("count.txt", "r+")
339 self.assertEqual(writer.size(), 0)
344 self.assertEqual(writer.size(), 12)
345 writer.seek(0, os.SEEK_SET)
346 self.assertEqual(writer.read(), b"\x00\x00"+text)
348 self.assertEqual(c.manifest_text(), ". 7f614da9329cd3aebf59b91aadc30bf0+67108864 781e5e245d69b566979b86e28d23f2c7+10 0:2:count.txt 67108864:10:count.txt\n")
351 def test_sparse_write2(self):
352 keep = ArvadosFileWriterTestCase.MockKeep({})
353 api = ArvadosFileWriterTestCase.MockApi({}, {})
354 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
355 api_client=api, keep_client=keep) as c:
356 writer = c.open("count.txt", "r+")
357 self.assertEqual(writer.size(), 0)
360 writer.seek((arvados.config.KEEP_BLOCK_SIZE*2) + 2)
362 self.assertEqual(writer.size(), (arvados.config.KEEP_BLOCK_SIZE*2) + 12)
363 writer.seek(0, os.SEEK_SET)
365 self.assertEqual(c.manifest_text(), ". 7f614da9329cd3aebf59b91aadc30bf0+67108864 781e5e245d69b566979b86e28d23f2c7+10 0:67108864:count.txt 0:67108864:count.txt 0:2:count.txt 67108864:10:count.txt\n")
368 def test_sparse_write3(self):
369 keep = ArvadosFileWriterTestCase.MockKeep({})
370 api = ArvadosFileWriterTestCase.MockApi({}, {})
371 for r in [[0, 1, 2, 3, 4], [4, 3, 2, 1, 0], [3, 2, 0, 4, 1]]:
372 with Collection() as c:
373 writer = c.open("count.txt", "r+")
374 self.assertEqual(writer.size(), 0)
381 self.assertEqual(writer.read(), "00000000001111111111222222222233333333334444444444")
383 def test_sparse_write4(self):
384 keep = ArvadosFileWriterTestCase.MockKeep({})
385 api = ArvadosFileWriterTestCase.MockApi({}, {})
386 for r in [[0, 1, 2, 4], [4, 2, 1, 0], [2, 0, 4, 1]]:
387 with Collection() as c:
388 writer = c.open("count.txt", "r+")
389 self.assertEqual(writer.size(), 0)
396 self.assertEqual(writer.read(), b"000000000011111111112222222222\x00\x00\x00\x00\x00\x00\x00\x00\x00\x004444444444")
399 def test_rewrite_on_empty_file(self):
400 keep = ArvadosFileWriterTestCase.MockKeep({})
401 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
402 keep_client=keep) as c:
403 writer = c.open("count.txt", "r+")
404 for b in xrange(0, 10):
405 writer.seek(0, os.SEEK_SET)
406 writer.write("0123456789")
408 self.assertEqual(writer.size(), 10)
409 self.assertEqual("0123456789", writer.readfrom(0, 20))
410 self.assertEqual(". 7a08b07e84641703e5f2c836aa59a170+100 90:10:count.txt\n", c.portable_manifest_text())
412 self.assertEqual(writer.size(), 10)
413 self.assertEqual("0123456789", writer.readfrom(0, 20))
414 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n", c.portable_manifest_text())
416 def test_rewrite_append_existing_file(self):
417 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
418 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
419 keep_client=keep) as c:
420 writer = c.open("count.txt", "r+")
421 for b in xrange(0, 10):
422 writer.seek(10, os.SEEK_SET)
423 writer.write("abcdefghij")
425 self.assertEqual(writer.size(), 20)
426 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
427 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:10:count.txt 100:10:count.txt\n", c.portable_manifest_text())
429 writer.arvadosfile.flush()
430 self.assertEqual(writer.size(), 20)
431 self.assertEqual("0123456789abcdefghij", writer.readfrom(0, 20))
432 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:20:count.txt\n", c.portable_manifest_text())
434 def test_rewrite_over_existing_file(self):
435 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
436 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt',
437 keep_client=keep) as c:
438 writer = c.open("count.txt", "r+")
439 for b in xrange(0, 10):
440 writer.seek(5, os.SEEK_SET)
441 writer.write("abcdefghij")
443 self.assertEqual(writer.size(), 15)
444 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
445 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 ae5f43bab79cf0be33f025fa97ae7398+100 0:5:count.txt 100:10:count.txt\n", c.portable_manifest_text())
447 writer.arvadosfile.flush()
449 self.assertEqual(writer.size(), 15)
450 self.assertEqual("01234abcdefghij", writer.readfrom(0, 20))
451 self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 a925576942e94b2ef57a066101b48876+10 0:5:count.txt 10:10:count.txt\n", c.portable_manifest_text())
453 def test_write_large_rewrite(self):
454 keep = ArvadosFileWriterTestCase.MockKeep({})
455 api = ArvadosFileWriterTestCase.MockApi({"name":"test_write_large",
456 "manifest_text": ". 3dc0d4bc21f48060bedcb2c91af4f906+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 0:3:count.txt 32892006:67107997:count.txt 3:32892000:count.txt\n",
457 "replication_desired":None},
458 {"uuid":"zzzzz-4zz18-mockcollection0",
459 "manifest_text": ". 3dc0d4bc21f48060bedcb2c91af4f906+32892003 a5de24f4417cfba9d5825eadc2f4ca49+67108000 0:3:count.txt 32892006:67107997:count.txt 3:32892000:count.txt\n",
460 "portable_data_hash":"217665c6b713e1b78dfba7ebd42344db+156"})
461 with Collection('. ' + arvados.config.EMPTY_BLOCK_LOCATOR + ' 0:0:count.txt',
462 api_client=api, keep_client=keep) as c:
463 writer = c.open("count.txt", "r+")
464 text = ''.join(["0123456789" for a in xrange(0, 100)])
465 for b in xrange(0, 100000):
467 writer.seek(0, os.SEEK_SET)
469 self.assertEqual(writer.size(), 100000000)
471 self.assertIsNone(c.manifest_locator())
472 self.assertTrue(c.modified())
473 c.save_new("test_write_large")
474 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
475 self.assertFalse(c.modified())
477 def test_create(self):
478 keep = ArvadosFileWriterTestCase.MockKeep({})
479 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
480 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
481 "replication_desired":None},
482 {"uuid":"zzzzz-4zz18-mockcollection0",
483 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
484 "portable_data_hash":"7a461a8c58601798f690f8b368ac4423+51"})
485 with Collection(api_client=api, keep_client=keep) as c:
486 writer = c.open("count.txt", "w+")
487 self.assertEqual(writer.size(), 0)
488 writer.write("01234567")
489 self.assertEqual(writer.size(), 8)
491 self.assertIsNone(c.manifest_locator())
492 self.assertTrue(c.modified())
493 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
494 c.save_new("test_create")
495 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
496 self.assertFalse(c.modified())
497 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
500 def test_create_subdir(self):
501 keep = ArvadosFileWriterTestCase.MockKeep({})
502 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create",
503 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
504 "replication_desired":None},
505 {"uuid":"zzzzz-4zz18-mockcollection0",
506 "manifest_text":"./foo/bar 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
507 "portable_data_hash":"1b02aaa62528d28a5be41651cbb9d7c7+59"})
508 with Collection(api_client=api, keep_client=keep) as c:
509 self.assertIsNone(c.api_response())
510 writer = c.open("foo/bar/count.txt", "w+")
511 writer.write("01234567")
512 self.assertFalse(c.committed())
513 c.save_new("test_create")
514 self.assertTrue(c.committed())
515 self.assertEqual(c.api_response(), api.response)
517 def test_overwrite(self):
518 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
519 api = ArvadosFileWriterTestCase.MockApi({"name":"test_overwrite",
520 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
521 "replication_desired":None},
522 {"uuid":"zzzzz-4zz18-mockcollection0",
523 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 0:8:count.txt\n",
524 "portable_data_hash":"7a461a8c58601798f690f8b368ac4423+51"})
525 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n',
526 api_client=api, keep_client=keep) as c:
527 writer = c.open("count.txt", "w+")
528 self.assertEqual(writer.size(), 0)
529 writer.write("01234567")
530 self.assertEqual(writer.size(), 8)
532 self.assertIsNone(c.manifest_locator())
533 self.assertTrue(c.modified())
534 c.save_new("test_overwrite")
535 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
536 self.assertFalse(c.modified())
538 def test_file_not_found(self):
539 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
540 with self.assertRaises(IOError):
541 writer = c.open("nocount.txt", "r")
543 def test_cannot_open_directory(self):
544 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n') as c:
545 with self.assertRaises(IOError):
546 writer = c.open(".", "r")
548 def test_create_multiple(self):
549 keep = ArvadosFileWriterTestCase.MockKeep({})
550 api = ArvadosFileWriterTestCase.MockApi({"name":"test_create_multiple",
551 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n",
552 "replication_desired":None},
553 {"uuid":"zzzzz-4zz18-mockcollection0",
554 "manifest_text":". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:8:count1.txt 8:8:count2.txt\n",
555 "portable_data_hash":"71e7bb6c00d31fc2b4364199fd97be08+102"})
556 with Collection(api_client=api, keep_client=keep) as c:
557 w1 = c.open("count1.txt", "w")
558 w2 = c.open("count2.txt", "w")
561 self.assertEqual(w1.size(), 8)
562 self.assertEqual(w2.size(), 8)
564 self.assertIsNone(c.manifest_locator())
565 self.assertTrue(c.modified())
566 self.assertIsNone(keep.get("2e9ec317e197819358fbc43afca7d837+8"))
567 c.save_new("test_create_multiple")
568 self.assertEqual("zzzzz-4zz18-mockcollection0", c.manifest_locator())
569 self.assertFalse(c.modified())
570 self.assertEqual("01234567", keep.get("2e9ec317e197819358fbc43afca7d837+8"))
573 class ArvadosFileReaderTestCase(StreamFileReaderTestCase):
574 class MockParent(object):
575 class MockBlockMgr(object):
576 def __init__(self, blocks, nocache):
578 self.nocache = nocache
580 def block_prefetch(self, loc):
583 def get_block_contents(self, loc, num_retries=0, cache_only=False):
584 if self.nocache and cache_only:
586 return self.blocks[loc]
588 def __init__(self, blocks, nocache):
590 self.nocache = nocache
591 self.lock = arvados.arvfile.NoopLock()
593 def root_collection(self):
596 def _my_block_manager(self):
597 return ArvadosFileReaderTestCase.MockParent.MockBlockMgr(self.blocks, self.nocache)
600 def make_count_reader(self, nocache=False):
604 for d in ['01234', '34567', '67890']:
605 loc = tutil.str_keep_locator(d)
607 stream.append(Range(loc, n, len(d)))
609 af = ArvadosFile(ArvadosFileReaderTestCase.MockParent(blocks, nocache), "count.txt", stream=stream, segments=[Range(1, 0, 3), Range(6, 3, 3), Range(11, 6, 3)])
610 return ArvadosFileReader(af)
612 def test_read_block_crossing_behavior(self):
613 # read() needs to return all the data requested if possible, even if it
614 # crosses uncached blocks: https://arvados.org/issues/5856
615 sfile = self.make_count_reader(nocache=True)
616 self.assertEqual('12345678', sfile.read(8))
618 def test_successive_reads(self):
619 # Override StreamFileReaderTestCase.test_successive_reads
620 sfile = self.make_count_reader(nocache=True)
621 self.assertEqual('1234', sfile.read(4))
622 self.assertEqual('5678', sfile.read(4))
623 self.assertEqual('9', sfile.read(4))
624 self.assertEqual('', sfile.read(4))
626 def test_tell_after_block_read(self):
627 # Override StreamFileReaderTestCase.test_tell_after_block_read
628 sfile = self.make_count_reader(nocache=True)
629 self.assertEqual('12345678', sfile.read(8))
630 self.assertEqual(8, sfile.tell())
632 def test_prefetch(self):
633 keep = ArvadosFileWriterTestCase.MockKeep({"2e9ec317e197819358fbc43afca7d837+8": "01234567", "e8dc4081b13434b45189a720b77b6818+8": "abcdefgh"})
634 with Collection(". 2e9ec317e197819358fbc43afca7d837+8 e8dc4081b13434b45189a720b77b6818+8 0:16:count.txt\n", keep_client=keep) as c:
635 r = c.open("count.txt", "r")
636 self.assertEqual("0123", r.read(4))
637 self.assertIn("2e9ec317e197819358fbc43afca7d837+8", keep.requests)
638 self.assertIn("e8dc4081b13434b45189a720b77b6818+8", keep.requests)
640 def test__eq__from_manifest(self):
641 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
642 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c2:
643 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
644 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
646 def test__eq__from_writes(self):
647 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
648 with Collection() as c2:
649 f = c2.open("count1.txt", "w")
650 f.write("0123456789")
652 self.assertTrue(c1["count1.txt"] == c2["count1.txt"])
653 self.assertFalse(c1["count1.txt"] != c2["count1.txt"])
655 def test__ne__(self):
656 with Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt') as c1:
657 with Collection() as c2:
658 f = c2.open("count1.txt", "w")
659 f.write("1234567890")
661 self.assertTrue(c1["count1.txt"] != c2["count1.txt"])
662 self.assertFalse(c1["count1.txt"] == c2["count1.txt"])
665 class ArvadosFileReadTestCase(unittest.TestCase, StreamRetryTestMixin):
666 def reader_for(self, coll_name, **kwargs):
670 for d in self.manifest_for(coll_name).split():
673 segments.append(Range(n, n, k.size))
674 stream.append(Range(d, n, k.size))
679 blockmanager = arvados.arvfile._BlockManager(self.keep_client())
680 blockmanager.prefetch_enabled = False
681 col = Collection(keep_client=self.keep_client(), block_manager=blockmanager)
682 af = ArvadosFile(col, "test",
685 return ArvadosFileReader(af, **kwargs)
687 def read_for_test(self, reader, byte_count, **kwargs):
688 return reader.read(byte_count, **kwargs)
691 class ArvadosFileReadFromTestCase(ArvadosFileReadTestCase):
692 def read_for_test(self, reader, byte_count, **kwargs):
693 return reader.readfrom(0, byte_count, **kwargs)
696 class ArvadosFileReadAllTestCase(ArvadosFileReadTestCase):
697 def read_for_test(self, reader, byte_count, **kwargs):
698 return ''.join(reader.readall(**kwargs))
701 class ArvadosFileReadAllDecompressedTestCase(ArvadosFileReadTestCase):
702 def read_for_test(self, reader, byte_count, **kwargs):
703 return ''.join(reader.readall_decompressed(**kwargs))
706 class ArvadosFileReadlinesTestCase(ArvadosFileReadTestCase):
707 def read_for_test(self, reader, byte_count, **kwargs):
708 return ''.join(reader.readlines(**kwargs))
711 class ArvadosFileTestCase(unittest.TestCase):
712 def datetime_to_hex(self, dt):
713 return hex(int(time.mktime(dt.timetuple())))[2:]
715 def test_permission_expired(self):
716 base_manifest = ". 781e5e245d69b566979b86e28d23f2c7+10+A715fd31f8111894f717eb1003c1b0216799dd9ec@{} 0:10:count.txt\n"
717 now = datetime.datetime.now()
718 a_week_ago = now - datetime.timedelta(days=7)
719 a_month_ago = now - datetime.timedelta(days=30)
720 a_week_from_now = now + datetime.timedelta(days=7)
721 with Collection(base_manifest.format(self.datetime_to_hex(a_week_from_now))) as c:
722 self.assertFalse(c.find('count.txt').permission_expired())
723 with Collection(base_manifest.format(self.datetime_to_hex(a_week_ago))) as c:
724 f = c.find('count.txt')
725 self.assertTrue(f.permission_expired())
726 self.assertTrue(f.permission_expired(a_week_from_now))
727 self.assertFalse(f.permission_expired(a_month_ago))
730 class BlockManagerTest(unittest.TestCase):
731 def test_bufferblock_append(self):
732 keep = ArvadosFileWriterTestCase.MockKeep({})
733 with arvados.arvfile._BlockManager(keep) as blockmanager:
734 bufferblock = blockmanager.alloc_bufferblock()
735 bufferblock.append("foo")
737 self.assertEqual(bufferblock.size(), 3)
738 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
739 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
741 bufferblock.append("bar")
743 self.assertEqual(bufferblock.size(), 6)
744 self.assertEqual(bufferblock.buffer_view[0:6], "foobar")
745 self.assertEqual(bufferblock.locator(), "3858f62230ac3c915f300c664312c63f+6")
747 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
748 with self.assertRaises(arvados.errors.AssertionError):
749 bufferblock.append("bar")
751 def test_bufferblock_dup(self):
752 keep = ArvadosFileWriterTestCase.MockKeep({})
753 with arvados.arvfile._BlockManager(keep) as blockmanager:
754 bufferblock = blockmanager.alloc_bufferblock()
755 bufferblock.append("foo")
757 self.assertEqual(bufferblock.size(), 3)
758 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
759 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
760 bufferblock.set_state(arvados.arvfile._BufferBlock.PENDING)
762 bufferblock2 = blockmanager.dup_block(bufferblock, None)
763 self.assertNotEqual(bufferblock.blockid, bufferblock2.blockid)
765 bufferblock2.append("bar")
767 self.assertEqual(bufferblock2.size(), 6)
768 self.assertEqual(bufferblock2.buffer_view[0:6], "foobar")
769 self.assertEqual(bufferblock2.locator(), "3858f62230ac3c915f300c664312c63f+6")
771 self.assertEqual(bufferblock.size(), 3)
772 self.assertEqual(bufferblock.buffer_view[0:3], "foo")
773 self.assertEqual(bufferblock.locator(), "acbd18db4cc2f85cedef654fccc4a4d8+3")
775 def test_bufferblock_get(self):
776 keep = ArvadosFileWriterTestCase.MockKeep({"781e5e245d69b566979b86e28d23f2c7+10": "0123456789"})
777 with arvados.arvfile._BlockManager(keep) as blockmanager:
778 bufferblock = blockmanager.alloc_bufferblock()
779 bufferblock.append("foo")
781 self.assertEqual(blockmanager.get_block_contents("781e5e245d69b566979b86e28d23f2c7+10", 1), "0123456789")
782 self.assertEqual(blockmanager.get_block_contents(bufferblock.blockid, 1), "foo")
784 def test_bufferblock_commit(self):
785 mockkeep = mock.MagicMock()
786 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
787 bufferblock = blockmanager.alloc_bufferblock()
788 bufferblock.owner = mock.MagicMock()
789 def flush(sync=None):
790 blockmanager.commit_bufferblock(bufferblock, sync)
791 bufferblock.owner.flush.side_effect = flush
792 bufferblock.append("foo")
793 blockmanager.commit_all()
794 self.assertTrue(bufferblock.owner.flush.called)
795 self.assertTrue(mockkeep.put.called)
796 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
797 self.assertIsNone(bufferblock.buffer_view)
799 def test_bufferblock_commit_pending(self):
801 mockkeep = mock.MagicMock()
802 mockkeep.put.side_effect = lambda x: time.sleep(1)
803 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
804 bufferblock = blockmanager.alloc_bufferblock()
805 bufferblock.append("foo")
807 blockmanager.commit_bufferblock(bufferblock, False)
808 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.PENDING)
810 blockmanager.commit_bufferblock(bufferblock, True)
811 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.COMMITTED)
814 def test_bufferblock_commit_with_error(self):
815 mockkeep = mock.MagicMock()
816 mockkeep.put.side_effect = arvados.errors.KeepWriteError("fail")
817 with arvados.arvfile._BlockManager(mockkeep) as blockmanager:
818 bufferblock = blockmanager.alloc_bufferblock()
819 bufferblock.owner = mock.MagicMock()
820 def flush(sync=None):
821 blockmanager.commit_bufferblock(bufferblock, sync)
822 bufferblock.owner.flush.side_effect = flush
823 bufferblock.append("foo")
824 with self.assertRaises(arvados.errors.KeepWriteError) as err:
825 blockmanager.commit_all()
826 self.assertTrue(bufferblock.owner.flush.called)
827 self.assertEqual(str(err.exception), "Error writing some blocks: block acbd18db4cc2f85cedef654fccc4a4d8+3 raised KeepWriteError (fail)")
828 self.assertEqual(bufferblock.state(), arvados.arvfile._BufferBlock.ERROR)