2755: Adjust arv-put test invocation.
[arvados.git] / sdk / python / tests / test_collections.py
1 # usage example:
2 #
3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
4
5 import arvados
6 import bz2
7 import copy
8 import os
9 import pprint
10 import subprocess
11 import tempfile
12 import unittest
13
14 from arvados_testutil import ArvadosKeepLocalStoreTestCase
15
16 class TestResumableWriter(arvados.ResumableCollectionWriter):
17     KEEP_BLOCK_SIZE = 1024  # PUT to Keep every 1K.
18
19     def current_state(self):
20         return self.dump_state(copy.deepcopy)
21
22
23 class ArvadosCollectionsTest(ArvadosKeepLocalStoreTestCase):
24     def write_foo_bar_baz(self):
25         cw = arvados.CollectionWriter()
26         self.assertEqual(cw.current_stream_name(), '.',
27                          'current_stream_name() should be "." now')
28         cw.set_current_file_name('foo.txt')
29         cw.write('foo')
30         self.assertEqual(cw.current_file_name(), 'foo.txt',
31                          'current_file_name() should be foo.txt now')
32         cw.start_new_file('bar.txt')
33         cw.write('bar')
34         cw.start_new_stream('baz')
35         cw.write('baz')
36         cw.set_current_file_name('baz.txt')
37         return cw.finish()
38
39     def test_keep_local_store(self):
40         self.assertEqual(arvados.Keep.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
41         self.assertEqual(arvados.Keep.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
42
43     def test_local_collection_writer(self):
44         self.assertEqual(self.write_foo_bar_baz(),
45                          'd6c3b8e571f1b81ebb150a45ed06c884+114',
46                          "wrong locator hash for files foo, bar, baz")
47
48     def test_local_collection_reader(self):
49         self.write_foo_bar_baz()
50         cr = arvados.CollectionReader('d6c3b8e571f1b81ebb150a45ed06c884+114+Xzizzle')
51         got = []
52         for s in cr.all_streams():
53             for f in s.all_files():
54                 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
55         expected = [[3, '.', 'bar.txt', 'bar'],
56                     [3, '.', 'foo.txt', 'foo'],
57                     [3, './baz', 'baz.txt', 'baz']]
58         self.assertEqual(got,
59                          expected)
60         stream0 = cr.all_streams()[0]
61         self.assertEqual(stream0.readfrom(0, 0),
62                          '',
63                          'reading zero bytes should have returned empty string')
64         self.assertEqual(stream0.readfrom(0, 2**26),
65                          'foobar',
66                          'reading entire stream failed')
67         self.assertEqual(stream0.readfrom(2**26, 0),
68                          '',
69                          'reading zero bytes should have returned empty string')
70
71     def _test_subset(self, collection, expected):
72         cr = arvados.CollectionReader(collection)
73         for s in cr.all_streams():
74             for ex in expected:
75                 if ex[0] == s:
76                     f = s.files()[ex[2]]
77                     got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
78                     self.assertEqual(got,
79                                      ex,
80                                      'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
81
82     def test_collection_manifest_subset(self):
83         self.write_foo_bar_baz()
84         self._test_subset('d6c3b8e571f1b81ebb150a45ed06c884+114',
85                           [[3, '.',     'bar.txt', 'bar'],
86                            [3, '.',     'foo.txt', 'foo'],
87                            [3, './baz', 'baz.txt', 'baz']])
88         self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
89                            (arvados.Keep.put("foo"),
90                             arvados.Keep.put("bar"))),
91                           [[3, '.', 'bar.txt', 'bar'],
92                            [3, '.', 'foo.txt', 'foo']])
93         self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
94                            (arvados.Keep.put("foo"),
95                             arvados.Keep.put("bar"))),
96                           [[2, '.', 'fo.txt', 'fo'],
97                            [4, '.', 'obar.txt', 'obar']])
98         self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
99                            (arvados.Keep.put("foo"),
100                             arvados.Keep.put("bar"))),
101                           [[2, '.', 'ar.txt', 'ar'],
102                            [2, '.', 'fo.txt', 'fo'],
103                            [2, '.', 'ob.txt', 'ob'],
104                            [0, '.', 'zero.txt', '']])
105
106     def _test_readline(self, what_in, what_out):
107         cw = arvados.CollectionWriter()
108         cw.start_new_file('test.txt')
109         cw.write(what_in)
110         test1 = cw.finish()
111         cr = arvados.CollectionReader(test1)
112         got = []
113         for x in list(cr.all_files())[0].readlines():
114             got += [x]
115         self.assertEqual(got,
116                          what_out,
117                          "readlines did not split lines correctly: %s" % got)
118
119     def test_collection_readline(self):
120         self._test_readline("\na\nbcd\n\nefg\nz",
121                             ["\n", "a\n", "bcd\n", "\n", "efg\n", "z"])
122         self._test_readline("ab\ncd\n",
123                             ["ab\n", "cd\n"])
124
125     def test_collection_empty_file(self):
126         cw = arvados.CollectionWriter()
127         cw.start_new_file('zero.txt')
128         cw.write('')
129
130         self.assertEqual(cw.manifest_text(), ". 0:0:zero.txt\n")
131         self.check_manifest_file_sizes(cw.manifest_text(), [0])
132         cw = arvados.CollectionWriter()
133         cw.start_new_file('zero.txt')
134         cw.write('')
135         cw.start_new_file('one.txt')
136         cw.write('1')
137         cw.start_new_stream('foo')
138         cw.start_new_file('zero.txt')
139         cw.write('')
140         self.check_manifest_file_sizes(cw.manifest_text(), [1,0,0])
141
142     def check_manifest_file_sizes(self, manifest_text, expect_sizes):
143         cr = arvados.CollectionReader(manifest_text)
144         got_sizes = []
145         for f in cr.all_files():
146             got_sizes += [f.size()]
147         self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
148
149     def test_collection_bz2_decompression(self):
150         n_lines_in = 2**18
151         data_in = "abc\n"
152         for x in xrange(0, 18):
153             data_in += data_in
154         compressed_data_in = bz2.compress(data_in)
155         cw = arvados.CollectionWriter()
156         cw.start_new_file('test.bz2')
157         cw.write(compressed_data_in)
158         bz2_manifest = cw.manifest_text()
159
160         cr = arvados.CollectionReader(bz2_manifest)
161
162         got = 0
163         for x in list(cr.all_files())[0].readlines():
164             self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
165             got += 1
166         self.assertEqual(got,
167                          n_lines_in,
168                          "decompression returned %d lines instead of %d" % (got, n_lines_in))
169
170     def test_collection_gzip_decompression(self):
171         n_lines_in = 2**18
172         data_in = "abc\n"
173         for x in xrange(0, 18):
174             data_in += data_in
175         p = subprocess.Popen(["gzip", "-1cn"],
176                              stdout=subprocess.PIPE,
177                              stdin=subprocess.PIPE,
178                              stderr=subprocess.PIPE,
179                              shell=False, close_fds=True)
180         compressed_data_in, stderrdata = p.communicate(data_in)
181
182         cw = arvados.CollectionWriter()
183         cw.start_new_file('test.gz')
184         cw.write(compressed_data_in)
185         gzip_manifest = cw.manifest_text()
186
187         cr = arvados.CollectionReader(gzip_manifest)
188         got = 0
189         for x in list(cr.all_files())[0].readlines():
190             self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
191             got += 1
192         self.assertEqual(got,
193                          n_lines_in,
194                          "decompression returned %d lines instead of %d" % (got, n_lines_in))
195
196     def test_normalized_collection(self):
197         m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
198 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
199 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
200         self.assertEqual(arvados.CollectionReader(m1).manifest_text(),
201                          """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
202 """)
203
204         m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
205 """
206         self.assertEqual(arvados.CollectionReader(m2).manifest_text(), m2)
207
208         m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
209 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
210 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt"""
211         self.assertEqual(arvados.CollectionReader(m3).manifest_text(),
212                          """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
213 """)
214
215         m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
216 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
217 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar"""
218         self.assertEqual(arvados.CollectionReader(m4).manifest_text(),
219                          """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
220 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
221 """)
222
223         m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
224 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
225 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar"""
226         self.assertEqual(arvados.CollectionReader(m5).manifest_text(),
227                          """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
228 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
229 """)
230
231         with self.data_file('1000G_ref_manifest') as f6:
232             m6 = f6.read()
233             self.assertEqual(arvados.CollectionReader(m6).manifest_text(), m6)
234
235         with self.data_file('jlake_manifest') as f7:
236             m7 = f7.read()
237             self.assertEqual(arvados.CollectionReader(m7).manifest_text(), m7)
238
239         m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
240 """
241         self.assertEqual(arvados.CollectionReader(m8).manifest_text(), m8)
242
243     def test_locators_and_ranges(self):
244         blocks2 = [['a', 10, 0],
245                   ['b', 10, 10],
246                   ['c', 10, 20],
247                   ['d', 10, 30],
248                   ['e', 10, 40],
249                   ['f', 10, 50]]
250
251         self.assertEqual(arvados.locators_and_ranges(blocks2,  2,  2), [['a', 10, 2, 2]])
252         self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [['b', 10, 2, 2]])
253         self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [['c', 10, 2, 2]])
254         self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [['d', 10, 2, 2]])
255         self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [['e', 10, 2, 2]])
256         self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [['f', 10, 2, 2]])
257         self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
258         self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
259
260         self.assertEqual(arvados.locators_and_ranges(blocks2,  0,  2), [['a', 10, 0, 2]])
261         self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [['b', 10, 0, 2]])
262         self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [['c', 10, 0, 2]])
263         self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [['d', 10, 0, 2]])
264         self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [['e', 10, 0, 2]])
265         self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [['f', 10, 0, 2]])
266         self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
267         self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
268
269         self.assertEqual(arvados.locators_and_ranges(blocks2,  9,  2), [['a', 10, 9, 1], ['b', 10, 0, 1]])
270         self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [['b', 10, 9, 1], ['c', 10, 0, 1]])
271         self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [['c', 10, 9, 1], ['d', 10, 0, 1]])
272         self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [['d', 10, 9, 1], ['e', 10, 0, 1]])
273         self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [['e', 10, 9, 1], ['f', 10, 0, 1]])
274         self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [['f', 10, 9, 1]])
275
276
277         blocks3 = [['a', 10, 0],
278                   ['b', 10, 10],
279                   ['c', 10, 20],
280                   ['d', 10, 30],
281                   ['e', 10, 40],
282                   ['f', 10, 50],
283                   ['g', 10, 60]]
284
285         self.assertEqual(arvados.locators_and_ranges(blocks3,  2,  2), [['a', 10, 2, 2]])
286         self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [['b', 10, 2, 2]])
287         self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [['c', 10, 2, 2]])
288         self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [['d', 10, 2, 2]])
289         self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [['e', 10, 2, 2]])
290         self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [['f', 10, 2, 2]])
291         self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [['g', 10, 2, 2]])
292
293
294         blocks = [['a', 10, 0],
295                   ['b', 15, 10],
296                   ['c', 5, 25]]
297         self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
298         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [['a', 10, 0, 5]])
299         self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [['a', 10, 3, 5]])
300         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [['a', 10, 0, 10]])
301
302         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [['a', 10, 0, 10],
303                                                                       ['b', 15, 0, 1]])
304         self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [['a', 10, 1, 9],
305                                                                       ['b', 15, 0, 2]])
306         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [['a', 10, 0, 10],
307                                                                       ['b', 15, 0, 15]])
308
309         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [['a', 10, 0, 10],
310                                                                       ['b', 15, 0, 15],
311                                                                       ['c', 5, 0, 5]])
312         self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [['a', 10, 1, 9],
313                                                                       ['b', 15, 0, 15],
314                                                                       ['c', 5, 0, 5]])
315         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [['a', 10, 0, 10],
316                                                                       ['b', 15, 0, 15],
317                                                                       ['c', 5, 0, 5]])
318
319         self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [['b', 15, 5, 5]])
320
321         self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [['a', 10, 8, 2],
322                                                                       ['b', 15, 0, 15]])
323
324         self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [['a', 10, 8, 2],
325                                                                       ['b', 15, 0, 15],
326                                                                       ['c', 5, 0, 3]])
327
328         self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [['c', 5, 1, 2]])
329
330         self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [['a', 10, 9, 1],
331                                                                       ['b', 15, 0, 14]])
332         self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [['b', 15, 0, 15]])
333         self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [['b', 15, 1, 14],
334                                                                        ['c', 5, 0, 1]])
335
336     class MockStreamReader(object):
337         def __init__(self, content):
338             self.content = content
339
340         def readfrom(self, start, size):
341             return self.content[start:start+size]
342
343     def test_file_stream(self):
344         content = 'abcdefghijklmnopqrstuvwxyz0123456789'
345         msr = self.MockStreamReader(content)
346         segments = [[0, 10, 0],
347                     [10, 15, 10],
348                     [25, 5, 25]]
349
350         sfr = arvados.StreamFileReader(msr, segments, "test")
351
352         self.assertEqual(sfr.name(), "test")
353         self.assertEqual(sfr.size(), 30)
354
355         self.assertEqual(sfr.readfrom(0, 30), content[0:30])
356         self.assertEqual(sfr.readfrom(2, 30), content[2:30])
357
358         self.assertEqual(sfr.readfrom(2, 8), content[2:10])
359         self.assertEqual(sfr.readfrom(0, 10), content[0:10])
360
361         self.assertEqual(sfr.tell(), 0)
362         self.assertEqual(sfr.read(5), content[0:5])
363         self.assertEqual(sfr.tell(), 5)
364         self.assertEqual(sfr.read(5), content[5:10])
365         self.assertEqual(sfr.tell(), 10)
366         self.assertEqual(sfr.read(5), content[10:15])
367         self.assertEqual(sfr.tell(), 15)
368         self.assertEqual(sfr.read(5), content[15:20])
369         self.assertEqual(sfr.tell(), 20)
370         self.assertEqual(sfr.read(5), content[20:25])
371         self.assertEqual(sfr.tell(), 25)
372         self.assertEqual(sfr.read(5), content[25:30])
373         self.assertEqual(sfr.tell(), 30)
374         self.assertEqual(sfr.read(5), '')
375         self.assertEqual(sfr.tell(), 30)
376
377         segments = [[26, 10, 0],
378                     [0, 15, 10],
379                     [15, 5, 25]]
380
381         sfr = arvados.StreamFileReader(msr, segments, "test")
382
383         self.assertEqual(sfr.size(), 30)
384
385         self.assertEqual(sfr.readfrom(0, 30), content[26:36] + content[0:20])
386         self.assertEqual(sfr.readfrom(2, 30), content[28:36] + content[0:20])
387
388         self.assertEqual(sfr.readfrom(2, 8), content[28:36])
389         self.assertEqual(sfr.readfrom(0, 10), content[26:36])
390
391         self.assertEqual(sfr.tell(), 0)
392         self.assertEqual(sfr.read(5), content[26:31])
393         self.assertEqual(sfr.tell(), 5)
394         self.assertEqual(sfr.read(5), content[31:36])
395         self.assertEqual(sfr.tell(), 10)
396         self.assertEqual(sfr.read(5), content[0:5])
397         self.assertEqual(sfr.tell(), 15)
398         self.assertEqual(sfr.read(5), content[5:10])
399         self.assertEqual(sfr.tell(), 20)
400         self.assertEqual(sfr.read(5), content[10:15])
401         self.assertEqual(sfr.tell(), 25)
402         self.assertEqual(sfr.read(5), content[15:20])
403         self.assertEqual(sfr.tell(), 30)
404         self.assertEqual(sfr.read(5), '')
405         self.assertEqual(sfr.tell(), 30)
406
407
408     class MockKeep(object):
409         def __init__(self, content):
410             self.content = content
411
412         def get(self, locator):
413             return self.content[locator]
414
415     def test_stream_reader(self):
416         keepblocks = {'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': 'abcdefghij',
417                       'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': 'klmnopqrstuvwxy',
418                       'cccccccccccccccccccccccccccccccc+5': 'z0123'}
419         mk = self.MockKeep(keepblocks)
420
421         sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
422
423         content = 'abcdefghijklmnopqrstuvwxyz0123456789'
424
425         self.assertEqual(sr.readfrom(0, 30), content[0:30])
426         self.assertEqual(sr.readfrom(2, 30), content[2:30])
427
428         self.assertEqual(sr.readfrom(2, 8), content[2:10])
429         self.assertEqual(sr.readfrom(0, 10), content[0:10])
430
431         self.assertEqual(sr.readfrom(0, 5), content[0:5])
432         self.assertEqual(sr.readfrom(5, 5), content[5:10])
433         self.assertEqual(sr.readfrom(10, 5), content[10:15])
434         self.assertEqual(sr.readfrom(15, 5), content[15:20])
435         self.assertEqual(sr.readfrom(20, 5), content[20:25])
436         self.assertEqual(sr.readfrom(25, 5), content[25:30])
437         self.assertEqual(sr.readfrom(30, 5), '')
438
439     def test_extract_file(self):
440         m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
441 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
442 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
443 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
444 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt"""
445
446         m2 = arvados.CollectionReader(m1)
447
448         self.assertEqual(m2.manifest_text(),
449                          ". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt 43:41:md6sum.txt 84:43:md7sum.txt 6:37:md8sum.txt 84:43:md8sum.txt 83:1:md9sum.txt 0:43:md9sum.txt 84:36:md9sum.txt\n")
450
451         self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md5sum.txt'].as_manifest(),
452                          ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
453         self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md6sum.txt'].as_manifest(),
454                          ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
455         self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md7sum.txt'].as_manifest(),
456                          ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
457         self.assertEqual(arvados.CollectionReader(m1).all_streams()[0].files()['md9sum.txt'].as_manifest(),
458                          ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
459
460     def test_write_directory_tree(self):
461         cwriter = arvados.CollectionWriter()
462         cwriter.write_directory_tree(self.build_directory_tree(
463                 ['basefile', 'subdir/subfile']))
464         self.assertEqual(cwriter.manifest_text(),
465                          """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
466 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
467
468     def test_write_named_directory_tree(self):
469         cwriter = arvados.CollectionWriter()
470         cwriter.write_directory_tree(self.build_directory_tree(
471                 ['basefile', 'subdir/subfile']), 'root')
472         self.assertEqual(
473             cwriter.manifest_text(),
474             """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
475 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
476
477     def test_write_directory_tree_in_one_stream(self):
478         cwriter = arvados.CollectionWriter()
479         cwriter.write_directory_tree(self.build_directory_tree(
480                 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
481         self.assertEqual(cwriter.manifest_text(),
482                          """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile
483 ./subdir 4ace875ffdc6824a04950f06858f4465+22 8:14:subfile\n""")
484
485     def test_write_directory_tree_with_limited_recursion(self):
486         cwriter = arvados.CollectionWriter()
487         cwriter.write_directory_tree(
488             self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
489             max_manifest_depth=1)
490         self.assertEqual(cwriter.manifest_text(),
491                          """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
492 ./d1 50170217e5b04312024aa5cd42934494+13 8:5:f2
493 ./d1/d2 50170217e5b04312024aa5cd42934494+13 0:8:f3\n""")
494
495     def test_write_one_file(self):
496         cwriter = arvados.CollectionWriter()
497         with self.make_test_file() as testfile:
498             cwriter.write_file(testfile.name)
499             self.assertEqual(
500                 cwriter.manifest_text(),
501                 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
502                     os.path.basename(testfile.name)))
503
504     def test_write_named_file(self):
505         cwriter = arvados.CollectionWriter()
506         with self.make_test_file() as testfile:
507             cwriter.write_file(testfile.name, 'foo')
508             self.assertEqual(cwriter.manifest_text(),
509                              ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
510
511     def test_write_multiple_files(self):
512         cwriter = arvados.CollectionWriter()
513         for letter in 'ABC':
514             with self.make_test_file(letter) as testfile:
515                 cwriter.write_file(testfile.name, letter)
516         self.assertEqual(
517             cwriter.manifest_text(),
518             ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
519
520     def test_basic_resume(self):
521         cwriter = TestResumableWriter()
522         with self.make_test_file() as testfile:
523             cwriter.write_file(testfile.name, 'test')
524             resumed = TestResumableWriter.from_state(cwriter.current_state())
525         self.assertEquals(cwriter.manifest_text(), resumed.manifest_text(),
526                           "resumed CollectionWriter had different manifest")
527
528     def test_resume_fails_when_missing_dependency(self):
529         cwriter = TestResumableWriter()
530         with self.make_test_file() as testfile:
531             cwriter.write_file(testfile.name, 'test')
532         self.assertRaises(arvados.errors.StaleWriterStateError,
533                           TestResumableWriter.from_state,
534                           cwriter.current_state())
535
536     def test_resume_fails_when_dependency_mtime_changed(self):
537         cwriter = TestResumableWriter()
538         with self.make_test_file() as testfile:
539             cwriter.write_file(testfile.name, 'test')
540             os.utime(testfile.name, (0, 0))
541             self.assertRaises(arvados.errors.StaleWriterStateError,
542                               TestResumableWriter.from_state,
543                               cwriter.current_state())
544
545     def test_resume_fails_when_dependency_is_nonfile(self):
546         cwriter = TestResumableWriter()
547         cwriter.write_file('/dev/null', 'empty')
548         self.assertRaises(arvados.errors.StaleWriterStateError,
549                           TestResumableWriter.from_state,
550                           cwriter.current_state())
551
552     def test_resume_fails_when_dependency_size_changed(self):
553         cwriter = TestResumableWriter()
554         with self.make_test_file() as testfile:
555             cwriter.write_file(testfile.name, 'test')
556             orig_mtime = os.fstat(testfile.fileno()).st_mtime
557             testfile.write('extra')
558             testfile.flush()
559             os.utime(testfile.name, (orig_mtime, orig_mtime))
560             self.assertRaises(arvados.errors.StaleWriterStateError,
561                               TestResumableWriter.from_state,
562                               cwriter.current_state())
563
564     def test_resume_fails_with_expired_locator(self):
565         cwriter = TestResumableWriter()
566         state = cwriter.current_state()
567         # Add an expired locator to the state.
568         state['_current_stream_locators'].append(''.join([
569                     'a' * 32, '+A', 'b' * 40, '@', '10000000']))
570         self.assertRaises(arvados.errors.StaleWriterStateError,
571                           TestResumableWriter.from_state, state)
572
573     def test_arbitrary_objects_not_resumable(self):
574         cwriter = TestResumableWriter()
575         with open('/dev/null') as badfile:
576             self.assertRaises(arvados.errors.AssertionError,
577                               cwriter.write_file, badfile)
578
579     def test_arbitrary_writes_not_resumable(self):
580         cwriter = TestResumableWriter()
581         self.assertRaises(arvados.errors.AssertionError,
582                           cwriter.write, "badtext")
583
584
585 if __name__ == '__main__':
586     unittest.main()