3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
12 class KeepLocalStoreTest(unittest.TestCase):
14 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
16 self.assertEqual(arvados.Keep.put('foo'), 'acbd18db4cc2f85cedef654fccc4a4d8+3', 'wrong md5 hash from Keep.put')
17 self.assertEqual(arvados.Keep.get('acbd18db4cc2f85cedef654fccc4a4d8+3'), 'foo', 'wrong data from Keep.get')
19 class LocalCollectionWriterTest(unittest.TestCase):
21 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
23 cw = arvados.CollectionWriter()
24 self.assertEqual(cw.current_stream_name(), '.',
25 'current_stream_name() should be "." now')
26 cw.set_current_file_name('foo.txt')
28 self.assertEqual(cw.current_file_name(), 'foo.txt',
29 'current_file_name() should be foo.txt now')
30 cw.start_new_file('bar.txt')
32 cw.start_new_stream('baz')
34 cw.set_current_file_name('baz.txt')
36 self.assertEqual(hash,
37 '23ca013983d6239e98931cc779e68426+114',
38 'resulting manifest hash is not what I expected')
40 class LocalCollectionReaderTest(unittest.TestCase):
42 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
43 LocalCollectionWriterTest().runTest()
45 cr = arvados.CollectionReader('23ca013983d6239e98931cc779e68426+114')
47 for s in cr.all_streams():
48 for f in s.all_files():
49 got += [[f.size(), f.stream_name(), f.name(), f.read(2**26)]]
50 expected = [[3, '.', 'foo.txt', 'foo'],
51 [3, '.', 'bar.txt', 'bar'],
52 [3, './baz', 'baz.txt', 'baz']]
55 'resulting file list is not what I expected')
56 stream0 = cr.all_streams()[0]
57 self.assertEqual(stream0.read(0),
59 'reading zero bytes should have returned empty string')
60 self.assertEqual(stream0.read(2**26),
62 'reading entire stream failed')
63 self.assertEqual(stream0.read(2**26),
65 'reading past end of stream should have returned None')
66 self.assertEqual(stream0.read(0),
68 'reading zero bytes should have returned empty string')
70 class LocalCollectionManifestSubsetTest(unittest.TestCase):
72 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
73 LocalCollectionWriterTest().runTest()
75 self._runTest('23ca013983d6239e98931cc779e68426+114',
76 [[3, '.', 'foo.txt', 'foo'],
77 [3, '.', 'bar.txt', 'bar'],
78 [3, './baz', 'baz.txt', 'baz']])
79 self._runTest((". %s %s 0:3:foo.txt 3:3:bar.txt\n" %
80 (arvados.Keep.put("foo"),
81 arvados.Keep.put("bar"))),
82 [[3, '.', 'foo.txt', 'foo'],
83 [3, '.', 'bar.txt', 'bar']])
84 self._runTest((". %s %s 0:2:fo.txt 2:4:obar.txt\n" %
85 (arvados.Keep.put("foo"),
86 arvados.Keep.put("bar"))),
87 [[2, '.', 'fo.txt', 'fo'],
88 [4, '.', 'obar.txt', 'obar']])
89 self._runTest((". %s %s 0:2:fo.txt 2:0:zero.txt 2:2:ob.txt 4:2:ar.txt\n" %
90 (arvados.Keep.put("foo"),
91 arvados.Keep.put("bar"))),
92 [[2, '.', 'fo.txt', 'fo'],
93 [0, '.', 'zero.txt', ''],
94 [2, '.', 'ob.txt', 'ob'],
95 [2, '.', 'ar.txt', 'ar']])
96 def _runTest(self, collection, expected):
97 cr = arvados.CollectionReader(collection)
99 for s in cr.all_streams():
100 for f in s.all_files():
101 manifest_subsets += [f.as_manifest()]
103 for m in manifest_subsets:
104 cr = arvados.CollectionReader(m)
105 for f in cr.all_files():
106 got = [f.size(), f.stream_name(), f.name(), "".join(f.readall(2**26))]
107 self.assertEqual(got,
109 'all_files|as_manifest did not preserve manifest contents: got %s expected %s' % (got, expected[expect_i]))
112 class LocalCollectionReadlineTest(unittest.TestCase):
114 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
115 def _runTest(self, what_in, what_out):
116 cw = arvados.CollectionWriter()
117 cw.start_new_file('test.txt')
120 cr = arvados.CollectionReader(test1)
122 for x in list(cr.all_files())[0].readlines():
124 self.assertEqual(got,
126 "readlines did not split lines correctly: %s" % got)
128 self._runTest("\na\nbcd\n\nefg\nz",
129 ["\n", "a\n", "bcd\n", "\n", "efg\n", "z"])
130 self._runTest("ab\ncd\n",
133 class LocalCollectionEmptyFileTest(unittest.TestCase):
135 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
137 cw = arvados.CollectionWriter()
138 cw.start_new_file('zero.txt')
140 self.check_manifest_file_sizes(cw.manifest_text(), [0])
141 cw = arvados.CollectionWriter()
142 cw.start_new_file('zero.txt')
144 cw.start_new_file('one.txt')
146 cw.start_new_stream('foo')
147 cw.start_new_file('zero.txt')
149 self.check_manifest_file_sizes(cw.manifest_text(), [0,1,0])
150 def check_manifest_file_sizes(self, manifest_text, expect_sizes):
151 cr = arvados.CollectionReader(manifest_text)
153 for f in cr.all_files():
154 got_sizes += [f.size()]
155 self.assertEqual(got_sizes, expect_sizes, "got wrong file sizes %s, expected %s" % (got_sizes, expect_sizes))
157 class LocalCollectionBZ2DecompressionTest(unittest.TestCase):
159 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
163 for x in xrange(0, 18):
165 compressed_data_in = bz2.compress(data_in)
166 cw = arvados.CollectionWriter()
167 cw.start_new_file('test.bz2')
168 cw.write(compressed_data_in)
169 bz2_manifest = cw.manifest_text()
171 cr = arvados.CollectionReader(bz2_manifest)
173 for x in list(cr.all_files())[0].readlines():
174 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
176 self.assertEqual(got,
178 "decompression returned %d lines instead of %d" % (got, n_lines_in))
180 class LocalCollectionGzipDecompressionTest(unittest.TestCase):
182 os.environ['KEEP_LOCAL_STORE'] = '/tmp'
186 for x in xrange(0, 18):
188 p = subprocess.Popen(["gzip", "-1cn"],
189 stdout=subprocess.PIPE,
190 stdin=subprocess.PIPE,
191 stderr=subprocess.PIPE,
192 shell=False, close_fds=True)
193 compressed_data_in, stderrdata = p.communicate(data_in)
195 cw = arvados.CollectionWriter()
196 cw.start_new_file('test.gz')
197 cw.write(compressed_data_in)
198 gzip_manifest = cw.manifest_text()
200 cr = arvados.CollectionReader(gzip_manifest)
202 for x in list(cr.all_files())[0].readlines():
203 self.assertEqual(x, "abc\n", "decompression returned wrong data: %s" % x)
205 self.assertEqual(got,
207 "decompression returned %d lines instead of %d" % (got, n_lines_in))