2 import arvados_fuse as fuse
8 from .. import run_test_server
9 from ..mount_test_base import MountTestBase
11 logger = logging.getLogger('arvados.arv-mount')
13 from performance_profiler import profiled
15 def fuse_createCollectionWithMultipleBlocks(mounttmp, streams=1, files_per_stream=1, data='x'):
16 class Test(unittest.TestCase):
18 self.createCollectionWithMultipleBlocks()
21 def createCollectionWithMultipleBlocks(self):
22 for i in range(0, streams):
23 os.mkdir(os.path.join(mounttmp, "./stream" + str(i)))
26 for j in range(0, files_per_stream):
27 with open(os.path.join(mounttmp, "./stream" + str(i), "file" + str(j) +".txt"), "w") as f:
32 def fuse_readContentsFromCollectionWithMultipleBlocks(mounttmp, streams=1, files_per_stream=1, data='x'):
33 class Test(unittest.TestCase):
35 self.readContentsFromCollectionWithMultipleBlocks()
38 def readContentsFromCollectionWithMultipleBlocks(self):
39 for i in range(0, streams):
40 d1 = llfuse.listdir(os.path.join(mounttmp, 'stream'+str(i)))
41 for j in range(0, files_per_stream):
42 with open(os.path.join(mounttmp, 'stream'+str(i), 'file'+str(i)+'.txt')) as f:
43 self.assertEqual(data, f.read())
47 def fuse_moveFileFromCollectionWithMultipleBlocks(mounttmp, stream, filename):
48 class Test(unittest.TestCase):
50 self.moveFileFromCollectionWithMultipleBlocks()
53 def moveFileFromCollectionWithMultipleBlocks(self):
54 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
55 self.assertIn(filename, d1)
57 os.rename(os.path.join(mounttmp, stream, filename), os.path.join(mounttmp, 'moved_from_'+stream+'_'+filename))
59 d1 = llfuse.listdir(os.path.join(mounttmp))
60 self.assertIn('moved_from_'+stream+'_'+filename, d1)
62 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
63 self.assertNotIn(filename, d1)
67 def fuse_deleteFileFromCollectionWithMultipleBlocks(mounttmp, stream, filename):
68 class Test(unittest.TestCase):
70 self.deleteFileFromCollectionWithMultipleBlocks()
73 def deleteFileFromCollectionWithMultipleBlocks(self):
74 os.remove(os.path.join(mounttmp, stream, filename))
78 # Create a collection with 2 streams, 3 files_per_stream, 2 blocks_per_file, 2**26 bytes_per_block
79 class CreateCollectionWithMultipleBlocksAndMoveAndDeleteFile(MountTestBase):
81 super(CreateCollectionWithMultipleBlocksAndMoveAndDeleteFile, self).setUp()
83 def test_CreateCollectionWithManyBlocksAndMoveAndDeleteFile(self):
84 collection = arvados.collection.Collection(api_client=self.api)
87 m = self.make_mount(fuse.CollectionDirectory)
89 m.new_collection(collection.api_response(), collection)
90 self.assertTrue(m.writable())
95 bytes_per_block = 2**26
97 data = 'x' * blocks_per_file * bytes_per_block
99 self.pool.apply(fuse_createCollectionWithMultipleBlocks, (self.mounttmp, streams, files_per_stream, data,))
101 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
103 for i in range(0, streams):
104 self.assertIn('./stream' + str(i), collection2["manifest_text"])
106 for i in range(0, files_per_stream):
107 self.assertIn('file' + str(i) + '.txt', collection2["manifest_text"])
110 self.pool.apply(fuse_readContentsFromCollectionWithMultipleBlocks, (self.mounttmp, streams, files_per_stream, data,))
112 # Move file0.txt out of the streams into .
113 for i in range(0, streams):
114 self.pool.apply(fuse_moveFileFromCollectionWithMultipleBlocks, (self.mounttmp, 'stream'+str(i), 'file0.txt',))
116 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
118 manifest_streams = collection2['manifest_text'].split('\n')
119 self.assertEqual(4, len(manifest_streams))
121 for i in range(0, streams):
122 self.assertIn('file0.txt', manifest_streams[0])
124 for i in range(0, streams):
125 self.assertNotIn('file0.txt', manifest_streams[i+1])
127 for i in range(0, streams):
128 for j in range(1, files_per_stream):
129 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
131 # Delete 'file1.txt' from all the streams
132 for i in range(0, streams):
133 self.pool.apply(fuse_deleteFileFromCollectionWithMultipleBlocks, (self.mounttmp, 'stream'+str(i), 'file1.txt'))
135 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
137 manifest_streams = collection2['manifest_text'].split('\n')
138 self.assertEqual(4, len(manifest_streams))
140 for i in range(0, streams):
141 self.assertIn('file0.txt', manifest_streams[0])
143 self.assertNotIn('file1.txt', collection2['manifest_text'])
145 for i in range(0, streams):
146 for j in range(2, files_per_stream):
147 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
150 def fuse_createCollectionWithManyFiles(mounttmp, streams=1, files_per_stream=1, data='x'):
151 class Test(unittest.TestCase):
153 self.createCollectionWithManyFiles()
156 def createCollectionWithManyFiles(self):
157 for i in range(0, streams):
158 os.mkdir(os.path.join(mounttmp, "./stream" + str(i)))
161 for j in range(0, files_per_stream):
162 with open(os.path.join(mounttmp, "./stream" + str(i), "file" + str(j) +".txt"), "w") as f:
167 def fuse_readContentsFromCollectionWithManyFiles(mounttmp, streams=1, files_per_stream=1, data='x'):
168 class Test(unittest.TestCase):
170 self.readContentsFromCollectionWithManyFiles()
173 def readContentsFromCollectionWithManyFiles(self):
174 for i in range(0, streams):
175 d1 = llfuse.listdir(os.path.join(mounttmp, 'stream'+str(i)))
176 for j in range(0, files_per_stream):
177 with open(os.path.join(mounttmp, 'stream'+str(i), 'file'+str(i)+'.txt')) as f:
178 self.assertEqual(data, f.read())
182 def fuse_moveFileFromCollectionWithManyFiles(mounttmp, stream, filename):
183 class Test(unittest.TestCase):
185 self.moveFileFromCollectionWithManyFiles()
188 def moveFileFromCollectionWithManyFiles(self):
189 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
190 self.assertIn(filename, d1)
192 os.rename(os.path.join(mounttmp, stream, filename), os.path.join(mounttmp, 'moved_from_'+stream+'_'+filename))
194 d1 = llfuse.listdir(os.path.join(mounttmp))
195 self.assertIn('moved_from_'+stream+'_'+filename, d1)
197 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
198 self.assertNotIn(filename, d1)
202 def fuse_deleteFileFromCollectionWithManyFiles(mounttmp, stream, filename):
203 class Test(unittest.TestCase):
205 self.deleteFileFromCollectionWithManyFiles()
208 def deleteFileFromCollectionWithManyFiles(self):
209 os.remove(os.path.join(mounttmp, stream, filename))
213 # Create a collection with two streams, each with 200 files
214 class CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
216 super(CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
218 def test_CreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
219 collection = arvados.collection.Collection(api_client=self.api)
220 collection.save_new()
222 m = self.make_mount(fuse.CollectionDirectory)
224 m.new_collection(collection.api_response(), collection)
225 self.assertTrue(m.writable())
228 files_per_stream = 200
232 self.pool.apply(fuse_createCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
234 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
236 for i in range(0, streams):
237 self.assertIn('./stream' + str(i), collection2["manifest_text"])
239 for i in range(0, files_per_stream):
240 self.assertIn('file' + str(i) + '.txt', collection2["manifest_text"])
243 self.pool.apply(fuse_readContentsFromCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
245 # Move file0.txt out of the streams into .
246 for i in range(0, streams):
247 self.pool.apply(fuse_moveFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file0.txt',))
249 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
251 manifest_streams = collection2['manifest_text'].split('\n')
252 self.assertEqual(4, len(manifest_streams))
254 for i in range(0, streams):
255 self.assertIn('file0.txt', manifest_streams[0])
257 for i in range(0, streams):
258 self.assertNotIn('file0.txt', manifest_streams[i+1])
260 for i in range(0, streams):
261 for j in range(1, files_per_stream):
262 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
264 # Delete 'file1.txt' from all the streams
265 for i in range(0, streams):
266 self.pool.apply(fuse_deleteFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file1.txt'))
268 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
270 manifest_streams = collection2['manifest_text'].split('\n')
271 self.assertEqual(4, len(manifest_streams))
273 for i in range(0, streams):
274 self.assertIn('file0.txt', manifest_streams[0])
276 self.assertNotIn('file1.txt', collection2['manifest_text'])
278 for i in range(0, streams):
279 for j in range(2, files_per_stream):
280 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
283 def magicDirTest_MoveFileFromCollection(mounttmp, collection1, collection2, stream, filename):
284 class Test(unittest.TestCase):
286 #os.rename(os.path.join(mounttmp, collection1, stream, filename), os.path.join(mounttmp, collection2, stream, filename))
287 os.rename(os.path.join(mounttmp, collection1, filename), os.path.join(mounttmp, collection2, filename))
291 def magicDirTest_RemoveFileFromCollection(mounttmp, collection1, stream, filename):
292 class Test(unittest.TestCase):
294 os.remove(os.path.join(mounttmp, collection1, filename))
298 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
300 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
303 def magicDirTest_createCollectionWithManyFiles(self, streams=0, files_per_stream=0, data='x'):
305 collection = arvados.collection.Collection(api_client=self.api)
306 for j in range(0, files_per_stream):
307 with collection.open("file"+str(j)+".txt", "w") as f:
309 collection.save_new()
313 def magicDirTest_readCollectionContents(self, collection, streams=1, files_per_stream=1, data='x'):
314 mount_ls = os.listdir(os.path.join(self.mounttmp, collection))
317 for j in range(0, files_per_stream):
318 files[os.path.join(self.mounttmp, collection, 'file'+str(j)+'.txt')] = data
319 #files[os.path.join(self.mounttmp, collection, 'stream'+str(i)+'/file'+str(j)+'.txt')] = data
321 for k, v in files.items():
322 with open(os.path.join(self.mounttmp, collection, k)) as f:
323 self.assertEqual(v, f.read())
326 def magicDirTest_moveFileFromCollection(self, from_collection, to_collection):
327 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
328 to_collection.manifest_locator(), 'stream0', 'file0.txt',))
329 from_collection.update()
330 to_collection.update()
333 def magicDirTest_removeFileFromCollection(self, collection):
334 self.pool.apply(magicDirTest_RemoveFileFromCollection, (self.mounttmp, collection.manifest_locator(), 'stream0', 'file1.txt',))
337 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
339 files_per_stream = 200
342 collection1 = self.magicDirTest_createCollectionWithManyFiles()
343 # Create collection with multiple files
344 collection2 = self.magicDirTest_createCollectionWithManyFiles(streams, files_per_stream, data)
347 self.make_mount(fuse.MagicDirectory)
349 self.magicDirTest_readCollectionContents(collection2.manifest_locator(), streams, files_per_stream, data)
351 # Move file0.txt out of the collection2 into collection1
352 self.magicDirTest_moveFileFromCollection(collection2, collection1)
353 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
354 self.assertFalse('file0.txt' in updated_collection['manifest_text'])
355 self.assertTrue('file1.txt' in updated_collection['manifest_text'])
357 # Delete file1.txt from collection2
358 self.magicDirTest_removeFileFromCollection(collection2)
359 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
360 self.assertFalse('file1.txt' in updated_collection['manifest_text'])
361 self.assertTrue('file2.txt' in updated_collection['manifest_text'])
364 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAllFilesIntoAnother(MountTestBase):
366 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAllFilesIntoAnother, self).setUp()
369 def magicDirTestMoveAllFiles_createCollectionWithManyFiles(self, streams=0, files_per_stream=0,
370 blocks_per_file=0, bytes_per_block=0, data='x'):
372 collection = arvados.collection.Collection(api_client=self.api)
373 for j in range(0, files_per_stream):
374 with collection.open("file"+str(j)+".txt", "w") as f:
376 collection.save_new()
380 def magicDirTestMoveAllFiles_moveFilesFromCollection(self, from_collection, to_collection, files_per_stream):
381 for j in range(0, files_per_stream):
382 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
383 to_collection.manifest_locator(), 'stream0', 'file'+str(j)+'.txt',))
384 from_collection.update()
385 to_collection.update()
387 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAllFilesIntoAnother(self):
389 files_per_stream = 200
392 collection1 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles()
393 # Create collection with multiple files
394 collection2 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
397 self.make_mount(fuse.MagicDirectory)
399 # Move all files from collection2 into collection1
400 self.magicDirTestMoveAllFiles_moveFilesFromCollection(collection2, collection1, files_per_stream)
402 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
403 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
404 for name in file_names:
405 self.assertFalse(name in updated_collection['manifest_text'])
407 updated_collection = self.api.collections().get(uuid=collection1.manifest_locator()).execute()
408 for name in file_names:
409 self.assertTrue(name in updated_collection['manifest_text'])
412 # Move one file at a time from one collection into another
413 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveEachFileIntoAnother(MountTestBase):
415 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveEachFileIntoAnother, self).setUp()
418 def magicDirTestMoveFiles_createCollectionWithManyFiles(self, streams=0, files_per_stream=0, data='x'):
420 collection = arvados.collection.Collection(api_client=self.api)
421 for j in range(0, files_per_stream):
422 with collection.open("file"+str(j)+".txt", "w") as f:
424 collection.save_new()
428 def magicDirTestMoveFiles_oneEachIntoAnother(self, from_collection, to_collection, files_per_stream):
429 for j in range(0, files_per_stream):
430 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
431 to_collection.manifest_locator(), 'stream0', 'file'+str(j)+'.txt',))
432 from_collection.update()
433 to_collection.update()
435 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveEachFileIntoAnother(self):
437 files_per_stream = 200
440 collection1 = self.magicDirTestMoveFiles_createCollectionWithManyFiles()
441 # Create collection with multiple files
442 collection2 = self.magicDirTestMoveFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
445 self.make_mount(fuse.MagicDirectory)
447 # Move all files from collection2 into collection1
448 self.magicDirTestMoveFiles_oneEachIntoAnother(collection2, collection1, files_per_stream)
450 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
451 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
452 for name in file_names:
453 self.assertFalse(name in updated_collection['manifest_text'])
455 updated_collection = self.api.collections().get(uuid=collection1.manifest_locator()).execute()
456 for name in file_names:
457 self.assertTrue(name in updated_collection['manifest_text'])
459 class FuseListLargeProjectContents(MountTestBase):
461 def getProjectWithManyCollections(self):
462 project_contents = llfuse.listdir(self.mounttmp)
463 self.assertEqual(201, len(project_contents))
464 self.assertIn('Collection_1', project_contents)
465 return project_contents
468 def listContentsInProjectWithManyCollections(self, project_contents):
469 project_contents = llfuse.listdir(self.mounttmp)
470 self.assertEqual(201, len(project_contents))
471 self.assertIn('Collection_1', project_contents)
473 for collection_name in project_contents:
474 collection_contents = llfuse.listdir(os.path.join(self.mounttmp, collection_name))
475 self.assertIn('baz', collection_contents)
477 def test_listLargeProjectContents(self):
478 self.make_mount(fuse.ProjectDirectory,
479 project_object=run_test_server.fixture('groups')['project_with_201_collections'])
480 project_contents = self.getProjectWithManyCollections()
481 self.listContentsInProjectWithManyCollections(project_contents)