14259: Collection class copies remote blocks when saving.
[arvados.git] / sdk / python / tests / test_collections.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 from __future__ import absolute_import
6
7 from builtins import object
8 import arvados
9 import copy
10 import mock
11 import os
12 import pprint
13 import random
14 import re
15 import sys
16 import tempfile
17 import datetime
18 import ciso8601
19 import time
20 import unittest
21
22 from . import run_test_server
23 from arvados._ranges import Range, LocatorAndRange
24 from arvados.collection import Collection, CollectionReader
25 from . import arvados_testutil as tutil
26
27 class TestResumableWriter(arvados.ResumableCollectionWriter):
28     KEEP_BLOCK_SIZE = 1024  # PUT to Keep every 1K.
29
30     def current_state(self):
31         return self.dump_state(copy.deepcopy)
32
33
34 class ArvadosCollectionsTest(run_test_server.TestCaseWithServers,
35                              tutil.ArvadosBaseTestCase):
36     MAIN_SERVER = {}
37
38     @classmethod
39     def setUpClass(cls):
40         super(ArvadosCollectionsTest, cls).setUpClass()
41         # need admin privileges to make collections with unsigned blocks
42         run_test_server.authorize_with('admin')
43         cls.api_client = arvados.api('v1')
44         cls.keep_client = arvados.KeepClient(api_client=cls.api_client,
45                                              local_store=cls.local_store)
46
47     def write_foo_bar_baz(self):
48         cw = arvados.CollectionWriter(self.api_client)
49         self.assertEqual(cw.current_stream_name(), '.',
50                          'current_stream_name() should be "." now')
51         cw.set_current_file_name('foo.txt')
52         cw.write(b'foo')
53         self.assertEqual(cw.current_file_name(), 'foo.txt',
54                          'current_file_name() should be foo.txt now')
55         cw.start_new_file('bar.txt')
56         cw.write(b'bar')
57         cw.start_new_stream('baz')
58         cw.write(b'baz')
59         cw.set_current_file_name('baz.txt')
60         self.assertEqual(cw.manifest_text(),
61                          ". 3858f62230ac3c915f300c664312c63f+6 0:3:foo.txt 3:3:bar.txt\n" +
62                          "./baz 73feffa4b7f6bb68e44cf984c85f6e88+3 0:3:baz.txt\n",
63                          "wrong manifest: got {}".format(cw.manifest_text()))
64         cw.save_new()
65         return cw.portable_data_hash()
66
67     def test_pdh_is_native_str(self):
68         pdh = self.write_foo_bar_baz()
69         self.assertEqual(type(''), type(pdh))
70
71     def test_keep_local_store(self):
72         self.assertEqual(self.keep_client.put(b'foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
73         self.assertEqual(self.keep_client.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), b'foo', 'wrong data from Keep.get')
74
75     def test_local_collection_writer(self):
76         self.assertEqual(self.write_foo_bar_baz(),
77                          '23ca013983d6239e98931cc779e68426+114',
78                          'wrong locator hash: ' + self.write_foo_bar_baz())
79
80     def test_local_collection_reader(self):
81         foobarbaz = self.write_foo_bar_baz()
82         cr = arvados.CollectionReader(
83             foobarbaz + '+Xzizzle', self.api_client)
84         got = []
85         for s in cr.all_streams():
86             for f in s.all_files():
87                 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
88         expected = [[3, '.', 'foo.txt', b'foo'],
89                     [3, '.', 'bar.txt', b'bar'],
90                     [3, './baz', 'baz.txt', b'baz']]
91         self.assertEqual(got,
92                          expected)
93         stream0 = cr.all_streams()[0]
94         self.assertEqual(stream0.readfrom(0, 0),
95                          b'',
96                          'reading zero bytes should have returned empty string')
97         self.assertEqual(stream0.readfrom(0, 2**26),
98                          b'foobar',
99                          'reading entire stream failed')
100         self.assertEqual(stream0.readfrom(2**26, 0),
101                          b'',
102                          'reading zero bytes should have returned empty string')
103         self.assertEqual(3, len(cr))
104         self.assertTrue(cr)
105
106     def _test_subset(self, collection, expected):
107         cr = arvados.CollectionReader(collection, self.api_client)
108         for s in cr.all_streams():
109             for ex in expected:
110                 if ex[0] == s:
111                     f = s.files()[ex[2]]
112                     got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
113                     self.assertEqual(got,
114                                      ex,
115                                      'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, ex))
116
117     def test_collection_manifest_subset(self):
118         foobarbaz = self.write_foo_bar_baz()
119         self._test_subset(foobarbaz,
120                           [[3, '.',     'bar.txt', b'bar'],
121                            [3, '.',     'foo.txt', b'foo'],
122                            [3, './baz', 'baz.txt', b'baz']])
123         self._test_subset((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
124                            (self.keep_client.put(b"foo"),
125                             self.keep_client.put(b"bar"))),
126                           [[3, '.', 'bar.txt', b'bar'],
127                            [3, '.', 'foo.txt', b'foo']])
128         self._test_subset((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
129                            (self.keep_client.put(b"foo"),
130                             self.keep_client.put(b"bar"))),
131                           [[2, '.', 'fo.txt', b'fo'],
132                            [4, '.', 'obar.txt', b'obar']])
133         self._test_subset((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
134                            (self.keep_client.put(b"foo"),
135                             self.keep_client.put(b"bar"))),
136                           [[2, '.', 'ar.txt', b'ar'],
137                            [2, '.', 'fo.txt', b'fo'],
138                            [2, '.', 'ob.txt', b'ob'],
139                            [0, '.', 'zero.txt', b'']])
140
141     def test_collection_empty_file(self):
142         cw = arvados.CollectionWriter(self.api_client)
143         cw.start_new_file('zero.txt')
144         cw.write(b'')
145
146         self.assertEqual(cw.manifest_text(), ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:zero.txt\n")
147         self.check_manifest_file_sizes(cw.manifest_text(), [0])
148         cw = arvados.CollectionWriter(self.api_client)
149         cw.start_new_file('zero.txt')
150         cw.write(b'')
151         cw.start_new_file('one.txt')
152         cw.write(b'1')
153         cw.start_new_stream('foo')
154         cw.start_new_file('zero.txt')
155         cw.write(b'')
156         self.check_manifest_file_sizes(cw.manifest_text(), [0,1,0])
157
158     def test_no_implicit_normalize(self):
159         cw = arvados.CollectionWriter(self.api_client)
160         cw.start_new_file('b')
161         cw.write(b'b')
162         cw.start_new_file('a')
163         cw.write(b'')
164         self.check_manifest_file_sizes(cw.manifest_text(), [1,0])
165         self.check_manifest_file_sizes(
166             arvados.CollectionReader(
167                 cw.manifest_text()).manifest_text(normalize=True),
168             [0,1])
169
170     def check_manifest_file_sizes(self, manifest_text, expect_sizes):
171         cr = arvados.CollectionReader(manifest_text, self.api_client)
172         got_sizes = []
173         for f in cr.all_files():
174             got_sizes += [f.size()]
175         self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
176
177     def test_normalized_collection(self):
178         m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
179 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
180 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
181 """
182         self.assertEqual(arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True),
183                          """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt
184 """)
185
186         m2 = """. 204e43b8a1185621ca55a94839582e6f+67108864 b9677abbac956bd3e86b1deb28dfac03+67108864 fc15aff2a762b13f521baf042140acec+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:227212247:var-GS000016015-ASM.tsv.bz2
187 """
188         self.assertEqual(arvados.CollectionReader(m2, self.api_client).manifest_text(normalize=True), m2)
189
190         m3 = """. 5348b82a029fd9e971a811ce1f71360b+43 3:40:md5sum.txt
191 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
192 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
193 """
194         self.assertEqual(arvados.CollectionReader(m3, self.api_client).manifest_text(normalize=True),
195                          """. 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 3:124:md5sum.txt
196 """)
197
198         m4 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
199 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
200 ./foo 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar
201 """
202         self.assertEqual(arvados.CollectionReader(m4, self.api_client).manifest_text(normalize=True),
203                          """./foo 204e43b8a1185621ca55a94839582e6f+67108864 323d2a3ce20370c4ca1d3462a344f8fd+25885655 0:3:bar 67108864:3:bar
204 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
205 """)
206
207         m5 = """. 204e43b8a1185621ca55a94839582e6f+67108864 0:3:foo/bar
208 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
209 ./foo 204e43b8a1185621ca55a94839582e6f+67108864 3:3:bar
210 """
211         self.assertEqual(arvados.CollectionReader(m5, self.api_client).manifest_text(normalize=True),
212                          """./foo 204e43b8a1185621ca55a94839582e6f+67108864 0:6:bar
213 ./zzz 204e43b8a1185621ca55a94839582e6f+67108864 0:999:zzz
214 """)
215
216         with self.data_file('1000G_ref_manifest') as f6:
217             m6 = f6.read()
218             self.assertEqual(arvados.CollectionReader(m6, self.api_client).manifest_text(normalize=True), m6)
219
220         with self.data_file('jlake_manifest') as f7:
221             m7 = f7.read()
222             self.assertEqual(arvados.CollectionReader(m7, self.api_client).manifest_text(normalize=True), m7)
223
224         m8 = """./a\\040b\\040c 59ca0efa9f5633cb0371bbc0355478d8+13 0:13:hello\\040world.txt
225 """
226         self.assertEqual(arvados.CollectionReader(m8, self.api_client).manifest_text(normalize=True), m8)
227
228     def test_locators_and_ranges(self):
229         blocks2 = [Range('a', 0, 10),
230                    Range('b', 10, 10),
231                    Range('c', 20, 10),
232                    Range('d', 30, 10),
233                    Range('e', 40, 10),
234                    Range('f', 50, 10)]
235
236         self.assertEqual(arvados.locators_and_ranges(blocks2,  2,  2), [LocatorAndRange('a', 10, 2, 2)])
237         self.assertEqual(arvados.locators_and_ranges(blocks2, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
238         self.assertEqual(arvados.locators_and_ranges(blocks2, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
239         self.assertEqual(arvados.locators_and_ranges(blocks2, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
240         self.assertEqual(arvados.locators_and_ranges(blocks2, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
241         self.assertEqual(arvados.locators_and_ranges(blocks2, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
242         self.assertEqual(arvados.locators_and_ranges(blocks2, 62, 2), [])
243         self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
244
245         self.assertEqual(arvados.locators_and_ranges(blocks2,  0,  2), [LocatorAndRange('a', 10, 0, 2)])
246         self.assertEqual(arvados.locators_and_ranges(blocks2, 10, 2), [LocatorAndRange('b', 10, 0, 2)])
247         self.assertEqual(arvados.locators_and_ranges(blocks2, 20, 2), [LocatorAndRange('c', 10, 0, 2)])
248         self.assertEqual(arvados.locators_and_ranges(blocks2, 30, 2), [LocatorAndRange('d', 10, 0, 2)])
249         self.assertEqual(arvados.locators_and_ranges(blocks2, 40, 2), [LocatorAndRange('e', 10, 0, 2)])
250         self.assertEqual(arvados.locators_and_ranges(blocks2, 50, 2), [LocatorAndRange('f', 10, 0, 2)])
251         self.assertEqual(arvados.locators_and_ranges(blocks2, 60, 2), [])
252         self.assertEqual(arvados.locators_and_ranges(blocks2, -2, 2), [])
253
254         self.assertEqual(arvados.locators_and_ranges(blocks2,  9,  2), [LocatorAndRange('a', 10, 9, 1), LocatorAndRange('b', 10, 0, 1)])
255         self.assertEqual(arvados.locators_and_ranges(blocks2, 19, 2), [LocatorAndRange('b', 10, 9, 1), LocatorAndRange('c', 10, 0, 1)])
256         self.assertEqual(arvados.locators_and_ranges(blocks2, 29, 2), [LocatorAndRange('c', 10, 9, 1), LocatorAndRange('d', 10, 0, 1)])
257         self.assertEqual(arvados.locators_and_ranges(blocks2, 39, 2), [LocatorAndRange('d', 10, 9, 1), LocatorAndRange('e', 10, 0, 1)])
258         self.assertEqual(arvados.locators_and_ranges(blocks2, 49, 2), [LocatorAndRange('e', 10, 9, 1), LocatorAndRange('f', 10, 0, 1)])
259         self.assertEqual(arvados.locators_and_ranges(blocks2, 59, 2), [LocatorAndRange('f', 10, 9, 1)])
260
261
262         blocks3 = [Range('a', 0, 10),
263                   Range('b', 10, 10),
264                   Range('c', 20, 10),
265                   Range('d', 30, 10),
266                   Range('e', 40, 10),
267                   Range('f', 50, 10),
268                    Range('g', 60, 10)]
269
270         self.assertEqual(arvados.locators_and_ranges(blocks3,  2,  2), [LocatorAndRange('a', 10, 2, 2)])
271         self.assertEqual(arvados.locators_and_ranges(blocks3, 12, 2), [LocatorAndRange('b', 10, 2, 2)])
272         self.assertEqual(arvados.locators_and_ranges(blocks3, 22, 2), [LocatorAndRange('c', 10, 2, 2)])
273         self.assertEqual(arvados.locators_and_ranges(blocks3, 32, 2), [LocatorAndRange('d', 10, 2, 2)])
274         self.assertEqual(arvados.locators_and_ranges(blocks3, 42, 2), [LocatorAndRange('e', 10, 2, 2)])
275         self.assertEqual(arvados.locators_and_ranges(blocks3, 52, 2), [LocatorAndRange('f', 10, 2, 2)])
276         self.assertEqual(arvados.locators_and_ranges(blocks3, 62, 2), [LocatorAndRange('g', 10, 2, 2)])
277
278
279         blocks = [Range('a', 0, 10),
280                   Range('b', 10, 15),
281                   Range('c', 25, 5)]
282         self.assertEqual(arvados.locators_and_ranges(blocks, 1, 0), [])
283         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 5), [LocatorAndRange('a', 10, 0, 5)])
284         self.assertEqual(arvados.locators_and_ranges(blocks, 3, 5), [LocatorAndRange('a', 10, 3, 5)])
285         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 10), [LocatorAndRange('a', 10, 0, 10)])
286
287         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 11), [LocatorAndRange('a', 10, 0, 10),
288                                                                       LocatorAndRange('b', 15, 0, 1)])
289         self.assertEqual(arvados.locators_and_ranges(blocks, 1, 11), [LocatorAndRange('a', 10, 1, 9),
290                                                                       LocatorAndRange('b', 15, 0, 2)])
291         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 25), [LocatorAndRange('a', 10, 0, 10),
292                                                                       LocatorAndRange('b', 15, 0, 15)])
293
294         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 30), [LocatorAndRange('a', 10, 0, 10),
295                                                                       LocatorAndRange('b', 15, 0, 15),
296                                                                       LocatorAndRange('c', 5, 0, 5)])
297         self.assertEqual(arvados.locators_and_ranges(blocks, 1, 30), [LocatorAndRange('a', 10, 1, 9),
298                                                                       LocatorAndRange('b', 15, 0, 15),
299                                                                       LocatorAndRange('c', 5, 0, 5)])
300         self.assertEqual(arvados.locators_and_ranges(blocks, 0, 31), [LocatorAndRange('a', 10, 0, 10),
301                                                                       LocatorAndRange('b', 15, 0, 15),
302                                                                       LocatorAndRange('c', 5, 0, 5)])
303
304         self.assertEqual(arvados.locators_and_ranges(blocks, 15, 5), [LocatorAndRange('b', 15, 5, 5)])
305
306         self.assertEqual(arvados.locators_and_ranges(blocks, 8, 17), [LocatorAndRange('a', 10, 8, 2),
307                                                                       LocatorAndRange('b', 15, 0, 15)])
308
309         self.assertEqual(arvados.locators_and_ranges(blocks, 8, 20), [LocatorAndRange('a', 10, 8, 2),
310                                                                       LocatorAndRange('b', 15, 0, 15),
311                                                                       LocatorAndRange('c', 5, 0, 3)])
312
313         self.assertEqual(arvados.locators_and_ranges(blocks, 26, 2), [LocatorAndRange('c', 5, 1, 2)])
314
315         self.assertEqual(arvados.locators_and_ranges(blocks, 9, 15), [LocatorAndRange('a', 10, 9, 1),
316                                                                       LocatorAndRange('b', 15, 0, 14)])
317         self.assertEqual(arvados.locators_and_ranges(blocks, 10, 15), [LocatorAndRange('b', 15, 0, 15)])
318         self.assertEqual(arvados.locators_and_ranges(blocks, 11, 15), [LocatorAndRange('b', 15, 1, 14),
319                                                                        LocatorAndRange('c', 5, 0, 1)])
320
321     class MockKeep(object):
322         def __init__(self, content, num_retries=0):
323             self.content = content
324
325         def get(self, locator, num_retries=0):
326             return self.content[locator]
327
328     def test_stream_reader(self):
329         keepblocks = {
330             'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10': b'abcdefghij',
331             'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15': b'klmnopqrstuvwxy',
332             'cccccccccccccccccccccccccccccccc+5': b'z0123',
333         }
334         mk = self.MockKeep(keepblocks)
335
336         sr = arvados.StreamReader([".", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+10", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+15", "cccccccccccccccccccccccccccccccc+5", "0:30:foo"], mk)
337
338         content = b'abcdefghijklmnopqrstuvwxyz0123456789'
339
340         self.assertEqual(sr.readfrom(0, 30), content[0:30])
341         self.assertEqual(sr.readfrom(2, 30), content[2:30])
342
343         self.assertEqual(sr.readfrom(2, 8), content[2:10])
344         self.assertEqual(sr.readfrom(0, 10), content[0:10])
345
346         self.assertEqual(sr.readfrom(0, 5), content[0:5])
347         self.assertEqual(sr.readfrom(5, 5), content[5:10])
348         self.assertEqual(sr.readfrom(10, 5), content[10:15])
349         self.assertEqual(sr.readfrom(15, 5), content[15:20])
350         self.assertEqual(sr.readfrom(20, 5), content[20:25])
351         self.assertEqual(sr.readfrom(25, 5), content[25:30])
352         self.assertEqual(sr.readfrom(30, 5), b'')
353
354     def test_extract_file(self):
355         m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
356 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt
357 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt
358 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 47:80:md8sum.txt
359 . 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt
360 """
361
362         m2 = arvados.CollectionReader(m1, self.api_client).manifest_text(normalize=True)
363
364         self.assertEqual(m2,
365                          ". 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")
366         files = arvados.CollectionReader(
367             m2, self.api_client).all_streams()[0].files()
368
369         self.assertEqual(files['md5sum.txt'].as_manifest(),
370                          ". 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt\n")
371         self.assertEqual(files['md6sum.txt'].as_manifest(),
372                          ". 085c37f02916da1cad16f93c54d899b7+41 0:41:md6sum.txt\n")
373         self.assertEqual(files['md7sum.txt'].as_manifest(),
374                          ". 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md7sum.txt\n")
375         self.assertEqual(files['md9sum.txt'].as_manifest(),
376                          ". 085c37f02916da1cad16f93c54d899b7+41 5348b82a029fd9e971a811ce1f71360b+43 8b22da26f9f433dea0a10e5ec66d73ba+43 40:80:md9sum.txt\n")
377
378     def test_write_directory_tree(self):
379         cwriter = arvados.CollectionWriter(self.api_client)
380         cwriter.write_directory_tree(self.build_directory_tree(
381                 ['basefile', 'subdir/subfile']))
382         self.assertEqual(cwriter.manifest_text(),
383                          """. c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
384 ./subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
385
386     def test_write_named_directory_tree(self):
387         cwriter = arvados.CollectionWriter(self.api_client)
388         cwriter.write_directory_tree(self.build_directory_tree(
389                 ['basefile', 'subdir/subfile']), 'root')
390         self.assertEqual(
391             cwriter.manifest_text(),
392             """./root c5110c5ac93202d8e0f9e381f22bac0f+8 0:8:basefile
393 ./root/subdir 1ca4dec89403084bf282ad31e6cf7972+14 0:14:subfile\n""")
394
395     def test_write_directory_tree_in_one_stream(self):
396         cwriter = arvados.CollectionWriter(self.api_client)
397         cwriter.write_directory_tree(self.build_directory_tree(
398                 ['basefile', 'subdir/subfile']), max_manifest_depth=0)
399         self.assertEqual(cwriter.manifest_text(),
400                          """. 4ace875ffdc6824a04950f06858f4465+22 0:8:basefile 8:14:subdir/subfile\n""")
401
402     def test_write_directory_tree_with_limited_recursion(self):
403         cwriter = arvados.CollectionWriter(self.api_client)
404         cwriter.write_directory_tree(
405             self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
406             max_manifest_depth=1)
407         self.assertEqual(cwriter.manifest_text(),
408                          """. bd19836ddb62c11c55ab251ccaca5645+2 0:2:f1
409 ./d1 50170217e5b04312024aa5cd42934494+13 0:8:d2/f3 8:5:f2\n""")
410
411     def test_write_directory_tree_with_zero_recursion(self):
412         cwriter = arvados.CollectionWriter(self.api_client)
413         content = 'd1/d2/f3d1/f2f1'
414         blockhash = tutil.str_keep_locator(content)
415         cwriter.write_directory_tree(
416             self.build_directory_tree(['f1', 'd1/f2', 'd1/d2/f3']),
417             max_manifest_depth=0)
418         self.assertEqual(
419             cwriter.manifest_text(),
420             ". {} 0:8:d1/d2/f3 8:5:d1/f2 13:2:f1\n".format(blockhash))
421
422     def test_write_one_file(self):
423         cwriter = arvados.CollectionWriter(self.api_client)
424         with self.make_test_file() as testfile:
425             cwriter.write_file(testfile.name)
426             self.assertEqual(
427                 cwriter.manifest_text(),
428                 ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:{}\n".format(
429                     os.path.basename(testfile.name)))
430
431     def test_write_named_file(self):
432         cwriter = arvados.CollectionWriter(self.api_client)
433         with self.make_test_file() as testfile:
434             cwriter.write_file(testfile.name, 'foo')
435             self.assertEqual(cwriter.manifest_text(),
436                              ". 098f6bcd4621d373cade4e832627b4f6+4 0:4:foo\n")
437
438     def test_write_multiple_files(self):
439         cwriter = arvados.CollectionWriter(self.api_client)
440         for letter in 'ABC':
441             with self.make_test_file(letter.encode()) as testfile:
442                 cwriter.write_file(testfile.name, letter)
443         self.assertEqual(
444             cwriter.manifest_text(),
445             ". 902fbdd2b1df0c4f70b4a5d23525e932+3 0:1:A 1:1:B 2:1:C\n")
446
447     def test_basic_resume(self):
448         cwriter = TestResumableWriter()
449         with self.make_test_file() as testfile:
450             cwriter.write_file(testfile.name, 'test')
451             resumed = TestResumableWriter.from_state(cwriter.current_state())
452         self.assertEqual(cwriter.manifest_text(), resumed.manifest_text(),
453                           "resumed CollectionWriter had different manifest")
454
455     def test_resume_fails_when_missing_dependency(self):
456         cwriter = TestResumableWriter()
457         with self.make_test_file() as testfile:
458             cwriter.write_file(testfile.name, 'test')
459         self.assertRaises(arvados.errors.StaleWriterStateError,
460                           TestResumableWriter.from_state,
461                           cwriter.current_state())
462
463     def test_resume_fails_when_dependency_mtime_changed(self):
464         cwriter = TestResumableWriter()
465         with self.make_test_file() as testfile:
466             cwriter.write_file(testfile.name, 'test')
467             os.utime(testfile.name, (0, 0))
468             self.assertRaises(arvados.errors.StaleWriterStateError,
469                               TestResumableWriter.from_state,
470                               cwriter.current_state())
471
472     def test_resume_fails_when_dependency_is_nonfile(self):
473         cwriter = TestResumableWriter()
474         cwriter.write_file('/dev/null', 'empty')
475         self.assertRaises(arvados.errors.StaleWriterStateError,
476                           TestResumableWriter.from_state,
477                           cwriter.current_state())
478
479     def test_resume_fails_when_dependency_size_changed(self):
480         cwriter = TestResumableWriter()
481         with self.make_test_file() as testfile:
482             cwriter.write_file(testfile.name, 'test')
483             orig_mtime = os.fstat(testfile.fileno()).st_mtime
484             testfile.write(b'extra')
485             testfile.flush()
486             os.utime(testfile.name, (orig_mtime, orig_mtime))
487             self.assertRaises(arvados.errors.StaleWriterStateError,
488                               TestResumableWriter.from_state,
489                               cwriter.current_state())
490
491     def test_resume_fails_with_expired_locator(self):
492         cwriter = TestResumableWriter()
493         state = cwriter.current_state()
494         # Add an expired locator to the state.
495         state['_current_stream_locators'].append(''.join([
496                     'a' * 32, '+1+A', 'b' * 40, '@', '10000000']))
497         self.assertRaises(arvados.errors.StaleWriterStateError,
498                           TestResumableWriter.from_state, state)
499
500     def test_arbitrary_objects_not_resumable(self):
501         cwriter = TestResumableWriter()
502         with open('/dev/null') as badfile:
503             self.assertRaises(arvados.errors.AssertionError,
504                               cwriter.write_file, badfile)
505
506     def test_arbitrary_writes_not_resumable(self):
507         cwriter = TestResumableWriter()
508         self.assertRaises(arvados.errors.AssertionError,
509                           cwriter.write, "badtext")
510
511
512 class CollectionTestMixin(tutil.ApiClientMock):
513     API_COLLECTIONS = run_test_server.fixture('collections')
514     DEFAULT_COLLECTION = API_COLLECTIONS['foo_file']
515     DEFAULT_DATA_HASH = DEFAULT_COLLECTION['portable_data_hash']
516     DEFAULT_MANIFEST = DEFAULT_COLLECTION['manifest_text']
517     DEFAULT_UUID = DEFAULT_COLLECTION['uuid']
518     ALT_COLLECTION = API_COLLECTIONS['bar_file']
519     ALT_DATA_HASH = ALT_COLLECTION['portable_data_hash']
520     ALT_MANIFEST = ALT_COLLECTION['manifest_text']
521
522     def api_client_mock(self, status=200):
523         client = super(CollectionTestMixin, self).api_client_mock()
524         self.mock_keep_services(client, status=status, service_type='proxy', count=1)
525         return client
526
527
528 @tutil.skip_sleep
529 class CollectionReaderTestCase(unittest.TestCase, CollectionTestMixin):
530     def mock_get_collection(self, api_mock, code, fixturename):
531         body = self.API_COLLECTIONS.get(fixturename)
532         self._mock_api_call(api_mock.collections().get, code, body)
533
534     def api_client_mock(self, status=200):
535         client = super(CollectionReaderTestCase, self).api_client_mock()
536         self.mock_get_collection(client, status, 'foo_file')
537         return client
538
539     def test_init_no_default_retries(self):
540         client = self.api_client_mock(200)
541         reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
542         reader.manifest_text()
543         client.collections().get().execute.assert_called_with(num_retries=0)
544
545     def test_uuid_init_success(self):
546         client = self.api_client_mock(200)
547         reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
548                                           num_retries=3)
549         self.assertEqual(self.DEFAULT_COLLECTION['manifest_text'],
550                          reader.manifest_text())
551         client.collections().get().execute.assert_called_with(num_retries=3)
552
553     def test_uuid_init_failure_raises_api_error(self):
554         client = self.api_client_mock(500)
555         with self.assertRaises(arvados.errors.ApiError):
556             reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
557
558     def test_locator_init(self):
559         client = self.api_client_mock(200)
560         # Ensure Keep will not return anything if asked.
561         with tutil.mock_keep_responses(None, 404):
562             reader = arvados.CollectionReader(self.DEFAULT_DATA_HASH,
563                                               api_client=client)
564             self.assertEqual(self.DEFAULT_MANIFEST, reader.manifest_text())
565
566     def test_init_no_fallback_to_keep(self):
567         # Do not look up a collection UUID or PDH in Keep.
568         for key in [self.DEFAULT_UUID, self.DEFAULT_DATA_HASH]:
569             client = self.api_client_mock(404)
570             with tutil.mock_keep_responses(self.DEFAULT_MANIFEST, 200):
571                 with self.assertRaises(arvados.errors.ApiError):
572                     reader = arvados.CollectionReader(key, api_client=client)
573
574     def test_init_num_retries_propagated(self):
575         # More of an integration test...
576         client = self.api_client_mock(200)
577         reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client,
578                                           num_retries=3)
579         with tutil.mock_keep_responses('foo', 500, 500, 200):
580             self.assertEqual(b'foo',
581                              b''.join(f.read(9) for f in reader.all_files()))
582
583     def test_read_nonnormalized_manifest_with_collection_reader(self):
584         # client should be able to use CollectionReader on a manifest without normalizing it
585         client = self.api_client_mock(500)
586         nonnormal = ". acbd18db4cc2f85cedef654fccc4a4d8+3+Aabadbadbee@abeebdee 0:3:foo.txt 1:0:bar.txt 0:3:foo.txt\n"
587         reader = arvados.CollectionReader(
588             nonnormal,
589             api_client=client, num_retries=0)
590         # Ensure stripped_manifest() doesn't mangle our manifest in
591         # any way other than stripping hints.
592         self.assertEqual(
593             re.sub('\+[^\d\s\+]+', '', nonnormal),
594             reader.stripped_manifest())
595         # Ensure stripped_manifest() didn't mutate our reader.
596         self.assertEqual(nonnormal, reader.manifest_text())
597         # Ensure the files appear in the order given in the manifest.
598         self.assertEqual(
599             [[6, '.', 'foo.txt'],
600              [0, '.', 'bar.txt']],
601             [[f.size(), f.stream_name(), f.name()]
602              for f in reader.all_streams()[0].all_files()])
603
604     def test_read_empty_collection(self):
605         client = self.api_client_mock(200)
606         self.mock_get_collection(client, 200, 'empty')
607         reader = arvados.CollectionReader('d41d8cd98f00b204e9800998ecf8427e+0',
608                                           api_client=client)
609         self.assertEqual('', reader.manifest_text())
610         self.assertEqual(0, len(reader))
611         self.assertFalse(reader)
612
613     def test_api_response(self):
614         client = self.api_client_mock()
615         reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
616         self.assertEqual(self.DEFAULT_COLLECTION, reader.api_response())
617
618     def check_open_file(self, coll_file, stream_name, file_name, file_size):
619         self.assertFalse(coll_file.closed, "returned file is not open")
620         self.assertEqual(stream_name, coll_file.stream_name())
621         self.assertEqual(file_name, coll_file.name)
622         self.assertEqual(file_size, coll_file.size())
623
624     def test_open_collection_file_one_argument(self):
625         client = self.api_client_mock(200)
626         reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
627         cfile = reader.open('./foo', 'rb')
628         self.check_open_file(cfile, '.', 'foo', 3)
629
630     def test_open_deep_file(self):
631         coll_name = 'collection_with_files_in_subdir'
632         client = self.api_client_mock(200)
633         self.mock_get_collection(client, 200, coll_name)
634         reader = arvados.CollectionReader(
635             self.API_COLLECTIONS[coll_name]['uuid'], api_client=client)
636         cfile = reader.open('./subdir2/subdir3/file2_in_subdir3.txt', 'rb')
637         self.check_open_file(cfile, './subdir2/subdir3', 'file2_in_subdir3.txt',
638                              32)
639
640     def test_open_nonexistent_stream(self):
641         client = self.api_client_mock(200)
642         reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
643         self.assertRaises(IOError, reader.open, './nonexistent/foo')
644
645     def test_open_nonexistent_file(self):
646         client = self.api_client_mock(200)
647         reader = arvados.CollectionReader(self.DEFAULT_UUID, api_client=client)
648         self.assertRaises(IOError, reader.open, 'nonexistent')
649
650
651 @tutil.skip_sleep
652 class CollectionWriterTestCase(unittest.TestCase, CollectionTestMixin):
653     def mock_keep(self, body, *codes, **headers):
654         headers.setdefault('x-keep-replicas-stored', 2)
655         return tutil.mock_keep_responses(body, *codes, **headers)
656
657     def foo_writer(self, **kwargs):
658         kwargs.setdefault('api_client', self.api_client_mock())
659         writer = arvados.CollectionWriter(**kwargs)
660         writer.start_new_file('foo')
661         writer.write(b'foo')
662         return writer
663
664     def test_write_whole_collection(self):
665         writer = self.foo_writer()
666         with self.mock_keep(self.DEFAULT_DATA_HASH, 200, 200):
667             self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
668
669     def test_write_no_default(self):
670         writer = self.foo_writer()
671         with self.mock_keep(None, 500):
672             with self.assertRaises(arvados.errors.KeepWriteError):
673                 writer.finish()
674
675     def test_write_insufficient_replicas_via_proxy(self):
676         writer = self.foo_writer(replication=3)
677         with self.mock_keep(None, 200, **{'x-keep-replicas-stored': 2}):
678             with self.assertRaises(arvados.errors.KeepWriteError):
679                 writer.manifest_text()
680
681     def test_write_insufficient_replicas_via_disks(self):
682         client = mock.MagicMock(name='api_client')
683         with self.mock_keep(
684                 None, 200, 200,
685                 **{'x-keep-replicas-stored': 1}) as keepmock:
686             self.mock_keep_services(client, status=200, service_type='disk', count=2)
687             writer = self.foo_writer(api_client=client, replication=3)
688             with self.assertRaises(arvados.errors.KeepWriteError):
689                 writer.manifest_text()
690
691     def test_write_three_replicas(self):
692         client = mock.MagicMock(name='api_client')
693         with self.mock_keep(
694                 "", 500, 500, 500, 200, 200, 200,
695                 **{'x-keep-replicas-stored': 1}) as keepmock:
696             self.mock_keep_services(client, status=200, service_type='disk', count=6)
697             writer = self.foo_writer(api_client=client, replication=3)
698             writer.manifest_text()
699             self.assertEqual(6, keepmock.call_count)
700
701     def test_write_whole_collection_through_retries(self):
702         writer = self.foo_writer(num_retries=2)
703         with self.mock_keep(self.DEFAULT_DATA_HASH,
704                             500, 500, 200, 500, 500, 200):
705             self.assertEqual(self.DEFAULT_DATA_HASH, writer.finish())
706
707     def test_flush_data_retries(self):
708         writer = self.foo_writer(num_retries=2)
709         foo_hash = self.DEFAULT_MANIFEST.split()[1]
710         with self.mock_keep(foo_hash, 500, 200):
711             writer.flush_data()
712         self.assertEqual(self.DEFAULT_MANIFEST, writer.manifest_text())
713
714     def test_one_open(self):
715         client = self.api_client_mock()
716         writer = arvados.CollectionWriter(client)
717         with writer.open('out') as out_file:
718             self.assertEqual('.', writer.current_stream_name())
719             self.assertEqual('out', writer.current_file_name())
720             out_file.write(b'test data')
721             data_loc = tutil.str_keep_locator('test data')
722         self.assertTrue(out_file.closed, "writer file not closed after context")
723         self.assertRaises(ValueError, out_file.write, 'extra text')
724         with self.mock_keep(data_loc, 200) as keep_mock:
725             self.assertEqual(". {} 0:9:out\n".format(data_loc),
726                              writer.manifest_text())
727
728     def test_open_writelines(self):
729         client = self.api_client_mock()
730         writer = arvados.CollectionWriter(client)
731         with writer.open('six') as out_file:
732             out_file.writelines(['12', '34', '56'])
733             data_loc = tutil.str_keep_locator('123456')
734         with self.mock_keep(data_loc, 200) as keep_mock:
735             self.assertEqual(". {} 0:6:six\n".format(data_loc),
736                              writer.manifest_text())
737
738     def test_open_flush(self):
739         client = self.api_client_mock()
740         data_loc1 = tutil.str_keep_locator('flush1')
741         data_loc2 = tutil.str_keep_locator('flush2')
742         with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
743             writer = arvados.CollectionWriter(client)
744             with writer.open('flush_test') as out_file:
745                 out_file.write(b'flush1')
746                 out_file.flush()
747                 out_file.write(b'flush2')
748             self.assertEqual(". {} {} 0:12:flush_test\n".format(data_loc1,
749                                                                 data_loc2),
750                              writer.manifest_text())
751
752     def test_two_opens_same_stream(self):
753         client = self.api_client_mock()
754         writer = arvados.CollectionWriter(client)
755         with writer.open('.', '1') as out_file:
756             out_file.write(b'1st')
757         with writer.open('.', '2') as out_file:
758             out_file.write(b'2nd')
759         data_loc = tutil.str_keep_locator('1st2nd')
760         with self.mock_keep(data_loc, 200) as keep_mock:
761             self.assertEqual(". {} 0:3:1 3:3:2\n".format(data_loc),
762                              writer.manifest_text())
763
764     def test_two_opens_two_streams(self):
765         client = self.api_client_mock()
766         data_loc1 = tutil.str_keep_locator('file')
767         data_loc2 = tutil.str_keep_locator('indir')
768         with self.mock_keep((data_loc1, 200), (data_loc2, 200)) as keep_mock:
769             writer = arvados.CollectionWriter(client)
770             with writer.open('file') as out_file:
771                 out_file.write(b'file')
772             with writer.open('./dir', 'indir') as out_file:
773                 out_file.write(b'indir')
774             expected = ". {} 0:4:file\n./dir {} 0:5:indir\n".format(
775                 data_loc1, data_loc2)
776             self.assertEqual(expected, writer.manifest_text())
777
778     def test_dup_open_fails(self):
779         client = self.api_client_mock()
780         writer = arvados.CollectionWriter(client)
781         file1 = writer.open('one')
782         self.assertRaises(arvados.errors.AssertionError, writer.open, 'two')
783
784
785 class CollectionMethods(run_test_server.TestCaseWithServers):
786
787     def test_keys_values_items_support_indexing(self):
788         c = Collection()
789         with c.open('foo', 'wb') as f:
790             f.write(b'foo')
791         with c.open('bar', 'wb') as f:
792             f.write(b'bar')
793         self.assertEqual(2, len(c.keys()))
794         if sys.version_info < (3, 0):
795             # keys() supports indexing only for python2 callers.
796             fn0 = c.keys()[0]
797             fn1 = c.keys()[1]
798         else:
799             fn0, fn1 = c.keys()
800         self.assertEqual(2, len(c.values()))
801         f0 = c.values()[0]
802         f1 = c.values()[1]
803         self.assertEqual(2, len(c.items()))
804         self.assertEqual(fn0, c.items()[0][0])
805         self.assertEqual(fn1, c.items()[1][0])
806
807     def test_get_properties(self):
808         c = Collection()
809         self.assertEqual(c.get_properties(), {})
810         c.save_new(properties={"foo":"bar"})
811         self.assertEqual(c.get_properties(), {"foo":"bar"})
812
813     def test_get_trash_at(self):
814         c = Collection()
815         self.assertEqual(c.get_trash_at(), None)
816         c.save_new(trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
817         self.assertEqual(c.get_trash_at(), ciso8601.parse_datetime('2111-01-01T11:11:11.111111000Z'))
818
819
820 class CollectionOpenModes(run_test_server.TestCaseWithServers):
821
822     def test_open_binary_modes(self):
823         c = Collection()
824         for mode in ['wb', 'wb+', 'ab', 'ab+']:
825             with c.open('foo', mode) as f:
826                 f.write(b'foo')
827
828     def test_open_invalid_modes(self):
829         c = Collection()
830         for mode in ['+r', 'aa', '++', 'r+b', 'beer', '', None]:
831             with self.assertRaises(Exception):
832                 c.open('foo', mode)
833
834     def test_open_text_modes(self):
835         c = Collection()
836         with c.open('foo', 'wb') as f:
837             f.write('foo')
838         for mode in ['r', 'rt', 'r+', 'rt+', 'w', 'wt', 'a', 'at']:
839             if sys.version_info >= (3, 0):
840                 with self.assertRaises(NotImplementedError):
841                     c.open('foo', mode)
842             else:
843                 with c.open('foo', mode) as f:
844                     if mode[0] == 'r' and '+' not in mode:
845                         self.assertEqual('foo', f.read(3))
846                     else:
847                         f.write('bar')
848                         f.seek(-3, os.SEEK_CUR)
849                         self.assertEqual('bar', f.read(3))
850
851
852 class NewCollectionTestCase(unittest.TestCase, CollectionTestMixin):
853
854     def test_replication_desired_kept_on_load(self):
855         m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
856         c1 = Collection(m, replication_desired=1)
857         c1.save_new()
858         loc = c1.manifest_locator()
859         c2 = Collection(loc)
860         self.assertEqual(c1.manifest_text, c2.manifest_text)
861         self.assertEqual(c1.replication_desired, c2.replication_desired)
862
863     def test_replication_desired_not_loaded_if_provided(self):
864         m = '. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n'
865         c1 = Collection(m, replication_desired=1)
866         c1.save_new()
867         loc = c1.manifest_locator()
868         c2 = Collection(loc, replication_desired=2)
869         self.assertEqual(c1.manifest_text, c2.manifest_text)
870         self.assertNotEqual(c1.replication_desired, c2.replication_desired)
871
872     def test_init_manifest(self):
873         m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
874 . 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
875 . 8b22da26f9f433dea0a10e5ec66d73ba+43 0:43:md5sum.txt
876 """
877         self.assertEqual(m1, CollectionReader(m1).manifest_text(normalize=False))
878         self.assertEqual(". 5348b82a029fd9e971a811ce1f71360b+43 085c37f02916da1cad16f93c54d899b7+41 8b22da26f9f433dea0a10e5ec66d73ba+43 0:127:md5sum.txt\n", CollectionReader(m1).manifest_text(normalize=True))
879
880     def test_init_manifest_with_collision(self):
881         m1 = """. 5348b82a029fd9e971a811ce1f71360b+43 0:43:md5sum.txt
882 ./md5sum.txt 085c37f02916da1cad16f93c54d899b7+41 0:41:md5sum.txt
883 """
884         with self.assertRaises(arvados.errors.ArgumentError):
885             self.assertEqual(m1, CollectionReader(m1))
886
887     def test_init_manifest_with_error(self):
888         m1 = """. 0:43:md5sum.txt"""
889         with self.assertRaises(arvados.errors.ArgumentError):
890             self.assertEqual(m1, CollectionReader(m1))
891
892     def test_remove(self):
893         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
894         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
895         self.assertIn("count1.txt", c)
896         c.remove("count1.txt")
897         self.assertNotIn("count1.txt", c)
898         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
899         with self.assertRaises(arvados.errors.ArgumentError):
900             c.remove("")
901
902     def test_find(self):
903         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n')
904         self.assertIs(c.find("."), c)
905         self.assertIs(c.find("./count1.txt"), c["count1.txt"])
906         self.assertIs(c.find("count1.txt"), c["count1.txt"])
907         with self.assertRaises(IOError):
908             c.find("/.")
909         with self.assertRaises(arvados.errors.ArgumentError):
910             c.find("")
911         self.assertIs(c.find("./nonexistant.txt"), None)
912         self.assertIs(c.find("./nonexistantsubdir/nonexistant.txt"), None)
913
914     def test_remove_in_subdir(self):
915         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
916         c.remove("foo/count2.txt")
917         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
918
919     def test_remove_empty_subdir(self):
920         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
921         c.remove("foo/count2.txt")
922         c.remove("foo")
923         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
924
925     def test_remove_nonempty_subdir(self):
926         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
927         with self.assertRaises(IOError):
928             c.remove("foo")
929         c.remove("foo", recursive=True)
930         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
931
932     def test_copy_to_file_in_dir(self):
933         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
934         c.copy("count1.txt", "foo/count2.txt")
935         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.portable_manifest_text())
936
937     def test_copy_file(self):
938         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
939         c.copy("count1.txt", "count2.txt")
940         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
941
942     def test_copy_to_existing_dir(self):
943         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
944         c.copy("count1.txt", "foo")
945         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:10:count2.txt\n", c.portable_manifest_text())
946
947     def test_copy_to_new_dir(self):
948         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
949         c.copy("count1.txt", "foo/")
950         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n", c.portable_manifest_text())
951
952     def test_rename_file(self):
953         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
954         c.rename("count1.txt", "count2.txt")
955         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
956
957     def test_move_file_to_dir(self):
958         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
959         c.mkdirs("foo")
960         c.rename("count1.txt", "foo/count2.txt")
961         self.assertEqual("./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c.manifest_text())
962
963     def test_move_file_to_other(self):
964         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
965         c2 = Collection()
966         c2.rename("count1.txt", "count2.txt", source_collection=c1)
967         self.assertEqual("", c1.manifest_text())
968         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", c2.manifest_text())
969
970     def test_clone(self):
971         c = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
972         cl = c.clone()
973         self.assertEqual(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n", cl.portable_manifest_text())
974
975     def test_diff_del_add(self):
976         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
977         c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
978         d = c2.diff(c1)
979         self.assertEqual(sorted(d), [
980             ('add', './count1.txt', c1["count1.txt"]),
981             ('del', './count2.txt', c2["count2.txt"]),
982         ])
983         d = c1.diff(c2)
984         self.assertEqual(sorted(d), [
985             ('add', './count2.txt', c2["count2.txt"]),
986             ('del', './count1.txt', c1["count1.txt"]),
987         ])
988         self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
989         c1.apply(d)
990         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
991
992     def test_diff_same(self):
993         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
994         c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
995         d = c2.diff(c1)
996         self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
997         d = c1.diff(c2)
998         self.assertEqual(d, [('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
999
1000         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1001         c1.apply(d)
1002         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1003
1004     def test_diff_mod(self):
1005         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1006         c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
1007         d = c2.diff(c1)
1008         self.assertEqual(d, [('mod', './count1.txt', c2["count1.txt"], c1["count1.txt"])])
1009         d = c1.diff(c2)
1010         self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1011
1012         self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1013         c1.apply(d)
1014         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1015
1016     def test_diff_add(self):
1017         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1018         c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt 10:20:count2.txt\n')
1019         d = c2.diff(c1)
1020         self.assertEqual(sorted(d), [
1021             ('del', './count2.txt', c2["count2.txt"]),
1022             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1023         ])
1024         d = c1.diff(c2)
1025         self.assertEqual(sorted(d), [
1026             ('add', './count2.txt', c2["count2.txt"]),
1027             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1028         ])
1029
1030         self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1031         c1.apply(d)
1032         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1033
1034     def test_diff_add_in_subcollection(self):
1035         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1036         c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1037         d = c2.diff(c1)
1038         self.assertEqual(sorted(d), [
1039             ('del', './foo', c2["foo"]),
1040             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1041         ])
1042         d = c1.diff(c2)
1043         self.assertEqual(sorted(d), [
1044             ('add', './foo', c2["foo"]),
1045             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1046         ])
1047         self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1048         c1.apply(d)
1049         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1050
1051     def test_diff_del_add_in_subcollection(self):
1052         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1053         c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:3:count3.txt\n')
1054         d = c2.diff(c1)
1055         self.assertEqual(sorted(d), [
1056             ('add', './foo/count2.txt', c1.find("foo/count2.txt")),
1057             ('del', './foo/count3.txt', c2.find("foo/count3.txt")),
1058             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1059         ])
1060         d = c1.diff(c2)
1061         self.assertEqual(sorted(d), [
1062             ('add', './foo/count3.txt', c2.find("foo/count3.txt")),
1063             ('del', './foo/count2.txt', c1.find("foo/count2.txt")),
1064             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1065         ])
1066
1067         self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1068         c1.apply(d)
1069         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1070
1071     def test_diff_mod_in_subcollection(self):
1072         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n./foo 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1073         c2 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt 0:3:foo\n')
1074         d = c2.diff(c1)
1075         self.assertEqual(sorted(d), [
1076             ('mod', './foo', c2["foo"], c1["foo"]),
1077             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1078         ])
1079         d = c1.diff(c2)
1080         self.assertEqual(sorted(d), [
1081             ('mod', './foo', c1["foo"], c2["foo"]),
1082             ('tok', './count1.txt', c2["count1.txt"], c1["count1.txt"]),
1083         ])
1084
1085         self.assertNotEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1086         c1.apply(d)
1087         self.assertEqual(c1.portable_manifest_text(), c2.portable_manifest_text())
1088
1089     def test_conflict_keep_local_change(self):
1090         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n')
1091         c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count2.txt\n')
1092         d = c1.diff(c2)
1093         self.assertEqual(sorted(d), [
1094             ('add', './count2.txt', c2["count2.txt"]),
1095             ('del', './count1.txt', c1["count1.txt"]),
1096         ])
1097         f = c1.open("count1.txt", "wb")
1098         f.write(b"zzzzz")
1099
1100         # c1 changed, so it should not be deleted.
1101         c1.apply(d)
1102         self.assertEqual(c1.portable_manifest_text(), ". 95ebc3c7b3b9f1d2c40fec14415d3cb8+5 5348b82a029fd9e971a811ce1f71360b+43 0:5:count1.txt 5:10:count2.txt\n")
1103
1104     def test_conflict_mod(self):
1105         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1106         c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1107         d = c1.diff(c2)
1108         self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1109         f = c1.open("count1.txt", "wb")
1110         f.write(b"zzzzz")
1111
1112         # c1 changed, so c2 mod will go to a conflict file
1113         c1.apply(d)
1114         self.assertRegex(
1115             c1.portable_manifest_text(),
1116             r"\. 95ebc3c7b3b9f1d2c40fec14415d3cb8\+5 5348b82a029fd9e971a811ce1f71360b\+43 0:5:count1\.txt 5:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1117
1118     def test_conflict_add(self):
1119         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count2.txt\n')
1120         c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt\n')
1121         d = c1.diff(c2)
1122         self.assertEqual(sorted(d), [
1123             ('add', './count1.txt', c2["count1.txt"]),
1124             ('del', './count2.txt', c1["count2.txt"]),
1125         ])
1126         f = c1.open("count1.txt", "wb")
1127         f.write(b"zzzzz")
1128
1129         # c1 added count1.txt, so c2 add will go to a conflict file
1130         c1.apply(d)
1131         self.assertRegex(
1132             c1.portable_manifest_text(),
1133             r"\. 95ebc3c7b3b9f1d2c40fec14415d3cb8\+5 5348b82a029fd9e971a811ce1f71360b\+43 0:5:count1\.txt 5:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1134
1135     def test_conflict_del(self):
1136         c1 = Collection('. 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt')
1137         c2 = Collection('. 5348b82a029fd9e971a811ce1f71360b+43 0:10:count1.txt')
1138         d = c1.diff(c2)
1139         self.assertEqual(d, [('mod', './count1.txt', c1["count1.txt"], c2["count1.txt"])])
1140         c1.remove("count1.txt")
1141
1142         # c1 deleted, so c2 mod will go to a conflict file
1143         c1.apply(d)
1144         self.assertRegex(
1145             c1.portable_manifest_text(),
1146             r"\. 5348b82a029fd9e971a811ce1f71360b\+43 0:10:count1\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1147
1148     def test_notify(self):
1149         c1 = Collection()
1150         events = []
1151         c1.subscribe(lambda event, collection, name, item: events.append((event, collection, name, item)))
1152         f = c1.open("foo.txt", "wb")
1153         self.assertEqual(events[0], (arvados.collection.ADD, c1, "foo.txt", f.arvadosfile))
1154
1155     def test_open_w(self):
1156         c1 = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count1.txt\n")
1157         self.assertEqual(c1["count1.txt"].size(), 10)
1158         c1.open("count1.txt", "wb").close()
1159         self.assertEqual(c1["count1.txt"].size(), 0)
1160
1161
1162 class NewCollectionTestCaseWithServersAndTokens(run_test_server.TestCaseWithServers):
1163     MAIN_SERVER = {}
1164     KEEP_SERVER = {}
1165     local_locator_re = r"[0-9a-f]{32}\+\d+\+A[a-f0-9]{40}@[a-f0-9]{8}"
1166     remote_locator_re = r"[0-9a-f]{32}\+\d+\+R[a-z]{5}-[a-f0-9]{40}@[a-f0-9]{8}"
1167
1168     def setUp(self):
1169         self.keep_put = getattr(arvados.keep.KeepClient, 'put')
1170
1171     @mock.patch('arvados.keep.KeepClient.put', autospec=True)
1172     def test_repacked_block_submission_get_permission_token(self, mocked_put):
1173         '''
1174         Make sure that those blocks that are committed after repacking small ones,
1175         get their permission tokens assigned on the collection manifest.
1176         '''
1177         def wrapped_keep_put(*args, **kwargs):
1178             # Simulate slow put operations
1179             time.sleep(1)
1180             return self.keep_put(*args, **kwargs)
1181
1182         mocked_put.side_effect = wrapped_keep_put
1183         c = Collection()
1184         # Write 70 files ~1MiB each so we force to produce 1 big block by repacking
1185         # small ones before finishing the upload.
1186         for i in range(70):
1187             f = c.open("file_{}.txt".format(i), 'wb')
1188             f.write(random.choice('abcdefghijklmnopqrstuvwxyz') * (2**20+i))
1189             f.close(flush=False)
1190         # We should get 2 blocks with their tokens
1191         self.assertEqual(len(re.findall(self.local_locator_re, c.manifest_text())), 2)
1192
1193     @mock.patch('arvados.keep.KeepClient.refresh_signature')
1194     def test_copy_remote_blocks_on_save_new(self, rs_mock):
1195         remote_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+Remote-" + "a" * 40 + "@abcdef01"
1196         local_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+A" + "b" * 40 + "@abcdef01"
1197         rs_mock.return_value = local_block_loc
1198         c = Collection(". " + remote_block_loc + " 0:3:foofile.txt\n")
1199         self.assertEqual(
1200             len(re.findall(self.remote_locator_re, c.manifest_text())), 1)
1201         c.save_new()
1202         rs_mock.assert_called()
1203         self.assertEqual(
1204             len(re.findall(self.local_locator_re, c.manifest_text())), 1)
1205
1206     @mock.patch('arvados.keep.KeepClient.refresh_signature')
1207     def test_copy_remote_blocks_on_save(self, rs_mock):
1208         remote_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+Remote-" + "a" * 40 + "@abcdef01"
1209         local_block_loc = "acbd18db4cc2f85cedef654fccc4a4d8+3+A" + "b" * 40 + "@abcdef01"
1210         rs_mock.return_value = local_block_loc
1211         # Remote collection
1212         remote_c = Collection(". " + remote_block_loc + " 0:3:foofile.txt\n")
1213         self.assertEqual(
1214             len(re.findall(self.remote_locator_re, remote_c.manifest_text())), 1)
1215         # Local collection
1216         local_c = Collection()
1217         with local_c.open('barfile.txt', 'wb') as f:
1218             f.write('bar')
1219         local_c.save_new()
1220         self.assertEqual(
1221             len(re.findall(self.local_locator_re, local_c.manifest_text())), 1)
1222         self.assertEqual(
1223             len(re.findall(self.remote_locator_re, local_c.manifest_text())), 0)
1224         # Copy remote file to local collection
1225         local_c.copy('./foofile.txt', './copied/foofile.txt', remote_c)
1226         self.assertEqual(
1227             len(re.findall(self.remote_locator_re, local_c.manifest_text())), 1)
1228         # Save local collection: remote block should be copied
1229         local_c.save()
1230         self.assertEqual(
1231             len(re.findall(self.local_locator_re, local_c.manifest_text())), 2)
1232         self.assertEqual(
1233             len(re.findall(self.remote_locator_re, local_c.manifest_text())), 0)
1234
1235
1236 class NewCollectionTestCaseWithServers(run_test_server.TestCaseWithServers):
1237     def test_get_manifest_text_only_committed(self):
1238         c = Collection()
1239         with c.open("count.txt", "wb") as f:
1240             # One file committed
1241             with c.open("foo.txt", "wb") as foo:
1242                 foo.write(b"foo")
1243                 foo.flush() # Force block commit
1244             f.write(b"0123456789")
1245             # Other file not committed. Block not written to keep yet.
1246             self.assertEqual(
1247                 c._get_manifest_text(".",
1248                                      strip=False,
1249                                      normalize=False,
1250                                      only_committed=True),
1251                 '. acbd18db4cc2f85cedef654fccc4a4d8+3 0:0:count.txt 0:3:foo.txt\n')
1252             # And now with the file closed...
1253             f.flush() # Force block commit
1254         self.assertEqual(
1255             c._get_manifest_text(".",
1256                                  strip=False,
1257                                  normalize=False,
1258                                  only_committed=True),
1259             ". 781e5e245d69b566979b86e28d23f2c7+10 acbd18db4cc2f85cedef654fccc4a4d8+3 0:10:count.txt 10:3:foo.txt\n")
1260
1261     def test_only_small_blocks_are_packed_together(self):
1262         c = Collection()
1263         # Write a couple of small files,
1264         f = c.open("count.txt", "wb")
1265         f.write(b"0123456789")
1266         f.close(flush=False)
1267         foo = c.open("foo.txt", "wb")
1268         foo.write(b"foo")
1269         foo.close(flush=False)
1270         # Then, write a big file, it shouldn't be packed with the ones above
1271         big = c.open("bigfile.txt", "wb")
1272         big.write(b"x" * 1024 * 1024 * 33) # 33 MB > KEEP_BLOCK_SIZE/2
1273         big.close(flush=False)
1274         self.assertEqual(
1275             c.manifest_text("."),
1276             '. 2d303c138c118af809f39319e5d507e9+34603008 a8430a058b8fbf408e1931b794dbd6fb+13 0:34603008:bigfile.txt 34603008:10:count.txt 34603018:3:foo.txt\n')
1277
1278     def test_flush_after_small_block_packing(self):
1279         c = Collection()
1280         # Write a couple of small files,
1281         f = c.open("count.txt", "wb")
1282         f.write(b"0123456789")
1283         f.close(flush=False)
1284         foo = c.open("foo.txt", "wb")
1285         foo.write(b"foo")
1286         foo.close(flush=False)
1287
1288         self.assertEqual(
1289             c.manifest_text(),
1290             '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1291
1292         f = c.open("count.txt", "rb+")
1293         f.close(flush=True)
1294
1295         self.assertEqual(
1296             c.manifest_text(),
1297             '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1298
1299     def test_write_after_small_block_packing2(self):
1300         c = Collection()
1301         # Write a couple of small files,
1302         f = c.open("count.txt", "wb")
1303         f.write(b"0123456789")
1304         f.close(flush=False)
1305         foo = c.open("foo.txt", "wb")
1306         foo.write(b"foo")
1307         foo.close(flush=False)
1308
1309         self.assertEqual(
1310             c.manifest_text(),
1311             '. a8430a058b8fbf408e1931b794dbd6fb+13 0:10:count.txt 10:3:foo.txt\n')
1312
1313         f = c.open("count.txt", "rb+")
1314         f.write(b"abc")
1315         f.close(flush=False)
1316
1317         self.assertEqual(
1318             c.manifest_text(),
1319             '. 900150983cd24fb0d6963f7d28e17f72+3 a8430a058b8fbf408e1931b794dbd6fb+13 0:3:count.txt 6:7:count.txt 13:3:foo.txt\n')
1320
1321
1322     def test_small_block_packing_with_overwrite(self):
1323         c = Collection()
1324         c.open("b1", "wb").close()
1325         c["b1"].writeto(0, b"b1", 0)
1326
1327         c.open("b2", "wb").close()
1328         c["b2"].writeto(0, b"b2", 0)
1329
1330         c["b1"].writeto(0, b"1b", 0)
1331
1332         self.assertEqual(c.manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 0:2:b1 2:2:b2\n")
1333         self.assertEqual(c["b1"].manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 0:2:b1\n")
1334         self.assertEqual(c["b2"].manifest_text(), ". ed4f3f67c70b02b29c50ce1ea26666bd+4 2:2:b2\n")
1335
1336
1337 class CollectionCreateUpdateTest(run_test_server.TestCaseWithServers):
1338     MAIN_SERVER = {}
1339     KEEP_SERVER = {}
1340
1341     def create_count_txt(self):
1342         # Create an empty collection, save it to the API server, then write a
1343         # file, but don't save it.
1344
1345         c = Collection()
1346         c.save_new("CollectionCreateUpdateTest", ensure_unique_name=True)
1347         self.assertEqual(c.portable_data_hash(), "d41d8cd98f00b204e9800998ecf8427e+0")
1348         self.assertEqual(c.api_response()["portable_data_hash"], "d41d8cd98f00b204e9800998ecf8427e+0" )
1349
1350         with c.open("count.txt", "wb") as f:
1351             f.write(b"0123456789")
1352
1353         self.assertEqual(c.portable_manifest_text(), ". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1354
1355         return c
1356
1357     def test_create_and_save(self):
1358         c = self.create_count_txt()
1359         c.save(properties={'type' : 'Intermediate'},
1360                storage_classes=['archive'],
1361                trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
1362
1363         self.assertRegex(
1364             c.manifest_text(),
1365             r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1366         self.assertEqual(c.api_response()["storage_classes_desired"], ['archive'])
1367         self.assertEqual(c.api_response()["properties"], {'type' : 'Intermediate'})
1368         self.assertEqual(c.api_response()["trash_at"], '2111-01-01T11:11:11.111111000Z')
1369
1370
1371     def test_create_and_save_new(self):
1372         c = self.create_count_txt()
1373         c.save_new(properties={'type' : 'Intermediate'},
1374                    storage_classes=['archive'],
1375                    trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
1376
1377         self.assertRegex(
1378             c.manifest_text(),
1379             r"^\. 781e5e245d69b566979b86e28d23f2c7\+10\+A[a-f0-9]{40}@[a-f0-9]{8} 0:10:count\.txt$",)
1380         self.assertEqual(c.api_response()["storage_classes_desired"], ['archive'])
1381         self.assertEqual(c.api_response()["properties"], {'type' : 'Intermediate'})
1382         self.assertEqual(c.api_response()["trash_at"], '2111-01-01T11:11:11.111111000Z')
1383
1384     def test_create_and_save_after_commiting(self):
1385         c = self.create_count_txt()
1386         c.save(properties={'type' : 'Intermediate'},
1387                storage_classes=['hot'],
1388                trash_at=datetime.datetime(2111, 1, 1, 11, 11, 11, 111111))
1389         c.save(properties={'type' : 'Output'},
1390                storage_classes=['cold'],
1391                trash_at=datetime.datetime(2222, 2, 2, 22, 22, 22, 222222))
1392
1393         self.assertEqual(c.api_response()["storage_classes_desired"], ['cold'])
1394         self.assertEqual(c.api_response()["properties"], {'type' : 'Output'})
1395         self.assertEqual(c.api_response()["trash_at"], '2222-02-02T22:22:22.222222000Z')
1396
1397     def test_create_diff_apply(self):
1398         c1 = self.create_count_txt()
1399         c1.save()
1400
1401         c2 = Collection(c1.manifest_locator())
1402         with c2.open("count.txt", "wb") as f:
1403             f.write(b"abcdefg")
1404
1405         diff = c1.diff(c2)
1406
1407         self.assertEqual(diff[0], (arvados.collection.MOD, u'./count.txt', c1["count.txt"], c2["count.txt"]))
1408
1409         c1.apply(diff)
1410         self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1411
1412     def test_diff_apply_with_token(self):
1413         baseline = CollectionReader(". 781e5e245d69b566979b86e28d23f2c7+10+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:10:count.txt\n")
1414         c = Collection(". 781e5e245d69b566979b86e28d23f2c7+10 0:10:count.txt\n")
1415         other = CollectionReader(". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1416
1417         diff = baseline.diff(other)
1418         self.assertEqual(diff, [('mod', u'./count.txt', c["count.txt"], other["count.txt"])])
1419
1420         c.apply(diff)
1421
1422         self.assertEqual(c.manifest_text(), ". 7ac66c0f148de9519b8bd264312c4d64+7+A715fd31f8111894f717eb1003c1b0216799dd9ec@54f5dd1a 0:7:count.txt\n")
1423
1424
1425     def test_create_and_update(self):
1426         c1 = self.create_count_txt()
1427         c1.save()
1428
1429         c2 = arvados.collection.Collection(c1.manifest_locator())
1430         with c2.open("count.txt", "wb") as f:
1431             f.write(b"abcdefg")
1432
1433         c2.save()
1434
1435         self.assertNotEqual(c1.portable_data_hash(), c2.portable_data_hash())
1436         c1.update()
1437         self.assertEqual(c1.portable_data_hash(), c2.portable_data_hash())
1438
1439
1440     def test_create_and_update_with_conflict(self):
1441         c1 = self.create_count_txt()
1442         c1.save()
1443
1444         with c1.open("count.txt", "wb") as f:
1445             f.write(b"XYZ")
1446
1447         c2 = arvados.collection.Collection(c1.manifest_locator())
1448         with c2.open("count.txt", "wb") as f:
1449             f.write(b"abcdefg")
1450
1451         c2.save()
1452
1453         c1.update()
1454         self.assertRegex(
1455             c1.manifest_text(),
1456             r"\. e65075d550f9b5bf9992fa1d71a131be\+3\S* 7ac66c0f148de9519b8bd264312c4d64\+7\S* 0:3:count\.txt 3:7:count\.txt~\d\d\d\d\d\d\d\d-\d\d\d\d\d\d~conflict~$")
1457
1458     def test_pdh_is_native_str(self):
1459         c1 = self.create_count_txt()
1460         pdh = c1.portable_data_hash()
1461         self.assertEqual(type(''), type(pdh))
1462
1463
1464 if __name__ == '__main__':
1465     unittest.main()