3 import arvados_fuse as fuse
16 import multiprocessing
17 from .. import run_test_server
18 from ..mount_test_base import MountTestBase
20 logger = logging.getLogger('arvados.arv-mount')
22 from performance_profiler import profiled
24 def fuse_CreateCollection(mounttmp, streams=1, files_per_stream=1, data='x'):
25 class Test(unittest.TestCase):
27 for i in range(0, streams):
28 os.mkdir(os.path.join(mounttmp, "./stream" + str(i)))
31 for j in range(0, files_per_stream):
32 with open(os.path.join(mounttmp, "./stream" + str(i), "file" + str(j) +".txt"), "w") as f:
37 def fuse_ReadContentsFromCollectionWithManyFiles(mounttmp, streams=1, files_per_stream=1, data='x'):
38 class Test(unittest.TestCase):
40 for i in range(0, streams):
41 d1 = llfuse.listdir(os.path.join(mounttmp, 'stream'+str(i)))
42 for j in range(0, files_per_stream):
43 with open(os.path.join(mounttmp, 'stream'+str(i), 'file'+str(i)+'.txt')) as f:
44 self.assertEqual(data, f.read())
48 def fuse_MoveFileFromCollectionWithManyFiles(mounttmp, stream, filename):
49 class Test(unittest.TestCase):
51 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
52 self.assertIn(filename, d1)
54 os.rename(os.path.join(mounttmp, stream, filename), os.path.join(mounttmp, 'moved_from_'+stream+'_'+filename))
56 d1 = llfuse.listdir(os.path.join(mounttmp))
57 self.assertIn('moved_from_'+stream+'_'+filename, d1)
59 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
60 self.assertNotIn(filename, d1)
64 def fuse_DeleteFileFromCollectionWithManyFiles(mounttmp, stream, filename):
65 class Test(unittest.TestCase):
67 os.remove(os.path.join(mounttmp, stream, filename))
71 # Create a collection with 2 streams, 3 files_per_stream, 2 blocks_per_file, 2**26 bytes_per_block
72 class CreateCollectionWithMultipleBlocksAndMoveAndDeleteFile(MountTestBase):
74 super(CreateCollectionWithMultipleBlocksAndMoveAndDeleteFile, self).setUp()
77 def createCollectionWithMultipleBlocks(self, streams, files_per_stream, data):
78 self.pool.apply(fuse_CreateCollection, (self.mounttmp, streams, files_per_stream, data,))
81 def readContentsOfCollectionWithMultipleBlocks(self, streams, files_per_stream, data):
82 self.pool.apply(fuse_ReadContentsFromCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
85 def moveFileFromCollectionWithMultipleBlocks(self, streams):
86 for i in range(0, streams):
87 self.pool.apply(fuse_MoveFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file0.txt',))
90 def removeFileFromCollectionWithMultipleBlocks(self, streams):
91 for i in range(0, streams):
92 self.pool.apply(fuse_DeleteFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file1.txt'))
94 def test_CreateCollectionWithManyBlocksAndMoveAndDeleteFile(self):
95 collection = arvados.collection.Collection(api_client=self.api)
98 m = self.make_mount(fuse.CollectionDirectory)
100 m.new_collection(collection.api_response(), collection)
101 self.assertTrue(m.writable())
106 bytes_per_block = 2**26
108 data = 'x' * blocks_per_file * bytes_per_block
110 self.createCollectionWithMultipleBlocks(streams, files_per_stream, data)
112 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
114 for i in range(0, streams):
115 self.assertIn('./stream' + str(i), collection2["manifest_text"])
117 for i in range(0, files_per_stream):
118 self.assertIn('file' + str(i) + '.txt', collection2["manifest_text"])
121 self.readContentsOfCollectionWithMultipleBlocks(streams, files_per_stream, data)
123 # Move file0.txt out of the streams into .
124 self.moveFileFromCollectionWithMultipleBlocks(streams)
126 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
128 manifest_streams = collection2['manifest_text'].split('\n')
129 self.assertEqual(4, len(manifest_streams))
131 for i in range(0, streams):
132 self.assertIn('file0.txt', manifest_streams[0])
134 for i in range(0, streams):
135 self.assertNotIn('file0.txt', manifest_streams[i+1])
137 for i in range(0, streams):
138 for j in range(1, files_per_stream):
139 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
141 # Delete 'file1.txt' from all the streams
142 self.removeFileFromCollectionWithMultipleBlocks(streams)
144 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
146 manifest_streams = collection2['manifest_text'].split('\n')
147 self.assertEqual(4, len(manifest_streams))
149 for i in range(0, streams):
150 self.assertIn('file0.txt', manifest_streams[0])
152 self.assertNotIn('file1.txt', collection2['manifest_text'])
154 for i in range(0, streams):
155 for j in range(2, files_per_stream):
156 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
158 # Create a collection with two streams, each with 200 files
159 class CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
161 super(CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
164 def createCollectionWithManyFiles(self, streams, files_per_stream, data):
165 self.pool.apply(fuse_CreateCollection, (self.mounttmp, streams, files_per_stream, data,))
168 def readContentsOfCollectionWithManyFiles(self, streams, files_per_stream, data):
169 self.pool.apply(fuse_ReadContentsFromCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
172 def moveFileFromCollectionWithManyFiles(self, streams):
173 for i in range(0, streams):
174 self.pool.apply(fuse_MoveFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file0.txt',))
177 def removeFileFromCollectionWithManyFiles(self, streams):
178 for i in range(0, streams):
179 self.pool.apply(fuse_DeleteFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file1.txt'))
181 def test_CreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
182 collection = arvados.collection.Collection(api_client=self.api)
183 collection.save_new()
185 m = self.make_mount(fuse.CollectionDirectory)
187 m.new_collection(collection.api_response(), collection)
188 self.assertTrue(m.writable())
191 files_per_stream = 200
195 self.createCollectionWithManyFiles(streams, files_per_stream, data)
197 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
199 for i in range(0, streams):
200 self.assertIn('./stream' + str(i), collection2["manifest_text"])
202 for i in range(0, files_per_stream):
203 self.assertIn('file' + str(i) + '.txt', collection2["manifest_text"])
206 self.readContentsOfCollectionWithManyFiles(streams, files_per_stream, data)
208 # Move file0.txt out of the streams into .
209 self.moveFileFromCollectionWithManyFiles(streams)
211 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
213 manifest_streams = collection2['manifest_text'].split('\n')
214 self.assertEqual(4, len(manifest_streams))
216 for i in range(0, streams):
217 self.assertIn('file0.txt', manifest_streams[0])
219 for i in range(0, streams):
220 self.assertNotIn('file0.txt', manifest_streams[i+1])
222 for i in range(0, streams):
223 for j in range(1, files_per_stream):
224 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
226 # Delete 'file1.txt' from all the streams
227 self.removeFileFromCollectionWithManyFiles(streams)
229 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
231 manifest_streams = collection2['manifest_text'].split('\n')
232 self.assertEqual(4, len(manifest_streams))
234 for i in range(0, streams):
235 self.assertIn('file0.txt', manifest_streams[0])
237 self.assertNotIn('file1.txt', collection2['manifest_text'])
239 for i in range(0, streams):
240 for j in range(2, files_per_stream):
241 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
243 def magicDirTest_MoveFileFromCollection(mounttmp, collection1, collection2, stream, filename):
244 class Test(unittest.TestCase):
246 #os.rename(os.path.join(mounttmp, collection1, stream, filename), os.path.join(mounttmp, collection2, stream, filename))
247 os.rename(os.path.join(mounttmp, collection1, filename), os.path.join(mounttmp, collection2, filename))
251 def magicDirTest_RemoveFileFromCollection(mounttmp, collection1, stream, filename):
252 class Test(unittest.TestCase):
254 os.remove(os.path.join(mounttmp, collection1, filename))
258 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
260 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
263 def magicDirTest_createCollectionWithManyFiles(self, streams=0, files_per_stream=0, data='x'):
265 collection = arvados.collection.Collection(api_client=self.api)
266 for j in range(0, files_per_stream):
267 with collection.open("file"+str(j)+".txt", "w") as f:
269 collection.save_new()
273 def magicDirTest_readCollectionContents(self, collection, streams=1, files_per_stream=1, data='x'):
274 mount_ls = os.listdir(os.path.join(self.mounttmp, collection))
277 for j in range(0, files_per_stream):
278 files[os.path.join(self.mounttmp, collection, 'file'+str(j)+'.txt')] = data
279 #files[os.path.join(self.mounttmp, collection, 'stream'+str(i)+'/file'+str(j)+'.txt')] = data
281 for k, v in files.items():
282 with open(os.path.join(self.mounttmp, collection, k)) as f:
283 self.assertEqual(v, f.read())
286 def magicDirTest_moveFileFromCollection(self, from_collection, to_collection):
287 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
288 to_collection.manifest_locator(), 'stream0', 'file0.txt',))
289 from_collection.update()
290 to_collection.update()
293 def magicDirTest_removeFileFromCollection(self, collection):
294 self.pool.apply(magicDirTest_RemoveFileFromCollection, (self.mounttmp, collection.manifest_locator(), 'stream0', 'file1.txt',))
297 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
299 files_per_stream = 200
302 collection1 = self.magicDirTest_createCollectionWithManyFiles()
303 # Create collection with multiple files
304 collection2 = self.magicDirTest_createCollectionWithManyFiles(streams, files_per_stream, data)
307 self.make_mount(fuse.MagicDirectory)
309 self.magicDirTest_readCollectionContents(collection2.manifest_locator(), streams, files_per_stream, data)
311 # Move file0.txt out of the collection2 into collection1
312 self.magicDirTest_moveFileFromCollection(collection2, collection1)
313 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
314 self.assertFalse('file0.txt' in updated_collection['manifest_text'])
315 self.assertTrue('file1.txt' in updated_collection['manifest_text'])
317 # Delete file1.txt from collection2
318 self.magicDirTest_removeFileFromCollection(collection2)
319 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
320 self.assertFalse('file1.txt' in updated_collection['manifest_text'])
321 self.assertTrue('file2.txt' in updated_collection['manifest_text'])
324 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAllFilesIntoAnother(MountTestBase):
326 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAllFilesIntoAnother, self).setUp()
329 def magicDirTestMoveAllFiles_createCollectionWithManyFiles(self, streams=0, files_per_stream=0,
330 blocks_per_file=0, bytes_per_block=0, data='x'):
332 collection = arvados.collection.Collection(api_client=self.api)
333 for j in range(0, files_per_stream):
334 with collection.open("file"+str(j)+".txt", "w") as f:
336 collection.save_new()
340 def magicDirTestMoveAllFiles_moveFilesFromCollection(self, from_collection, to_collection, files_per_stream):
341 for j in range(0, files_per_stream):
342 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
343 to_collection.manifest_locator(), 'stream0', 'file'+str(j)+'.txt',))
344 from_collection.update()
345 to_collection.update()
347 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAllFilesIntoAnother(self):
349 files_per_stream = 200
352 collection1 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles()
353 # Create collection with multiple files
354 collection2 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
357 self.make_mount(fuse.MagicDirectory)
359 # Move all files from collection2 into collection1
360 self.magicDirTestMoveAllFiles_moveFilesFromCollection(collection2, collection1, files_per_stream)
362 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
363 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
364 for name in file_names:
365 self.assertFalse(name in updated_collection['manifest_text'])
367 updated_collection = self.api.collections().get(uuid=collection1.manifest_locator()).execute()
368 for name in file_names:
369 self.assertTrue(name in updated_collection['manifest_text'])
372 # Move one file at a time from one collection into another
373 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveEachFileIntoAnother(MountTestBase):
375 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveEachFileIntoAnother, self).setUp()
378 def magicDirTestMoveFiles_createCollectionWithManyFiles(self, streams=0, files_per_stream=0, data='x'):
380 collection = arvados.collection.Collection(api_client=self.api)
381 for j in range(0, files_per_stream):
382 with collection.open("file"+str(j)+".txt", "w") as f:
384 collection.save_new()
388 def magicDirTestMoveFiles_oneEachIntoAnother(self, from_collection, to_collection, files_per_stream):
389 for j in range(0, files_per_stream):
390 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
391 to_collection.manifest_locator(), 'stream0', 'file'+str(j)+'.txt',))
392 from_collection.update()
393 to_collection.update()
395 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveEachFileIntoAnother(self):
397 files_per_stream = 200
400 collection1 = self.magicDirTestMoveFiles_createCollectionWithManyFiles()
401 # Create collection with multiple files
402 collection2 = self.magicDirTestMoveFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
405 self.make_mount(fuse.MagicDirectory)
407 # Move all files from collection2 into collection1
408 self.magicDirTestMoveFiles_oneEachIntoAnother(collection2, collection1, files_per_stream)
410 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
411 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
412 for name in file_names:
413 self.assertFalse(name in updated_collection['manifest_text'])
415 updated_collection = self.api.collections().get(uuid=collection1.manifest_locator()).execute()
416 for name in file_names:
417 self.assertTrue(name in updated_collection['manifest_text'])