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
231 self.pool.apply(fuse_createCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
233 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
235 for i in range(0, streams):
236 self.assertIn('./stream' + str(i), collection2["manifest_text"])
238 for i in range(0, files_per_stream):
239 self.assertIn('file' + str(i) + '.txt', collection2["manifest_text"])
242 self.pool.apply(fuse_readContentsFromCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
244 # Move file0.txt out of the streams into .
245 for i in range(0, streams):
246 self.pool.apply(fuse_moveFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file0.txt',))
248 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
250 manifest_streams = collection2['manifest_text'].split('\n')
251 self.assertEqual(4, len(manifest_streams))
253 for i in range(0, streams):
254 self.assertIn('file0.txt', manifest_streams[0])
256 for i in range(0, streams):
257 self.assertNotIn('file0.txt', manifest_streams[i+1])
259 for i in range(0, streams):
260 for j in range(1, files_per_stream):
261 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
263 # Delete 'file1.txt' from all the streams
264 for i in range(0, streams):
265 self.pool.apply(fuse_deleteFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file1.txt'))
267 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
269 manifest_streams = collection2['manifest_text'].split('\n')
270 self.assertEqual(4, len(manifest_streams))
272 for i in range(0, streams):
273 self.assertIn('file0.txt', manifest_streams[0])
275 self.assertNotIn('file1.txt', collection2['manifest_text'])
277 for i in range(0, streams):
278 for j in range(2, files_per_stream):
279 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
282 def magicDirTest_MoveFileFromCollection(mounttmp, collection1, collection2, stream, filename):
283 class Test(unittest.TestCase):
285 self.magicDirTest_moveFileFromCollection()
288 def magicDirTest_moveFileFromCollection(self):
289 os.rename(os.path.join(mounttmp, collection1, filename), os.path.join(mounttmp, collection2, filename))
293 def magicDirTest_RemoveFileFromCollection(mounttmp, collection1, stream, filename):
294 class Test(unittest.TestCase):
296 self.magicDirTest_removeFileFromCollection()
299 def magicDirTest_removeFileFromCollection(self):
300 os.remove(os.path.join(mounttmp, collection1, filename))
304 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
306 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
309 def magicDirTest_createCollectionWithManyFiles(self, streams=0, files_per_stream=0, data='x'):
311 collection = arvados.collection.Collection(api_client=self.api)
312 for j in range(0, files_per_stream):
313 with collection.open("file"+str(j)+".txt", "w") as f:
315 collection.save_new()
319 def magicDirTest_readCollectionContents(self, collection, streams=1, files_per_stream=1, data='x'):
320 mount_ls = os.listdir(os.path.join(self.mounttmp, collection))
323 for j in range(0, files_per_stream):
324 files[os.path.join(self.mounttmp, collection, 'file'+str(j)+'.txt')] = data
326 for k, v in files.items():
327 with open(os.path.join(self.mounttmp, collection, k)) as f:
328 self.assertEqual(v, f.read())
330 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
332 files_per_stream = 200
335 collection1 = self.magicDirTest_createCollectionWithManyFiles()
336 # Create collection with multiple files
337 collection2 = self.magicDirTest_createCollectionWithManyFiles(streams, files_per_stream, data)
340 self.make_mount(fuse.MagicDirectory)
342 self.magicDirTest_readCollectionContents(collection2.manifest_locator(), streams, files_per_stream, data)
344 # Move file0.txt out of the collection2 into collection1
345 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, collection2.manifest_locator(),
346 collection1.manifest_locator(), 'stream0', 'file0.txt',))
347 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
348 self.assertFalse('file0.txt' in updated_collection['manifest_text'])
349 self.assertTrue('file1.txt' in updated_collection['manifest_text'])
351 # Delete file1.txt from collection2
352 self.pool.apply(magicDirTest_RemoveFileFromCollection, (self.mounttmp, collection2.manifest_locator(), 'stream0', 'file1.txt',))
353 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
354 self.assertFalse('file1.txt' in updated_collection['manifest_text'])
355 self.assertTrue('file2.txt' in updated_collection['manifest_text'])
358 def magicDirTest_MoveAllFilesFromCollection(mounttmp, from_collection, to_collection, stream, files_per_stream):
359 class Test(unittest.TestCase):
361 self.magicDirTest_moveAllFilesFromCollection()
364 def magicDirTest_moveAllFilesFromCollection(self):
365 for j in range(0, files_per_stream):
366 os.rename(os.path.join(mounttmp, from_collection, 'file'+str(j)+'.txt'), os.path.join(mounttmp, to_collection, 'file'+str(j)+'.txt'))
370 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAllFilesIntoAnother(MountTestBase):
372 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAllFilesIntoAnother, self).setUp()
375 def magicDirTestMoveAllFiles_createCollectionWithManyFiles(self, streams=0, files_per_stream=0,
376 blocks_per_file=0, bytes_per_block=0, data='x'):
378 collection = arvados.collection.Collection(api_client=self.api)
379 for j in range(0, files_per_stream):
380 with collection.open("file"+str(j)+".txt", "w") as f:
382 collection.save_new()
386 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAllFilesIntoAnother(self):
388 files_per_stream = 200
391 collection1 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles()
392 # Create collection with multiple files
393 collection2 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
396 self.make_mount(fuse.MagicDirectory)
398 # Move all files from collection2 into collection1
399 self.pool.apply(magicDirTest_MoveAllFilesFromCollection, (self.mounttmp, collection2.manifest_locator(),
400 collection1.manifest_locator(), 'stream0', 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()
427 def magicDirTestMoveFiles_oneEachIntoAnother(self, from_collection, to_collection, files_per_stream):
428 for j in range(0, files_per_stream):
429 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
430 to_collection.manifest_locator(), 'stream0', 'file'+str(j)+'.txt',))
433 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveEachFileIntoAnother(self):
435 files_per_stream = 200
438 collection1 = self.magicDirTestMoveFiles_createCollectionWithManyFiles()
439 # Create collection with multiple files
440 collection2 = self.magicDirTestMoveFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
443 self.make_mount(fuse.MagicDirectory)
445 # Move all files from collection2 into collection1
446 self.magicDirTestMoveFiles_oneEachIntoAnother(collection2, collection1, files_per_stream)
448 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
449 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
450 for name in file_names:
451 self.assertFalse(name in updated_collection['manifest_text'])
453 updated_collection = self.api.collections().get(uuid=collection1.manifest_locator()).execute()
454 for name in file_names:
455 self.assertTrue(name in updated_collection['manifest_text'])
457 class FuseListLargeProjectContents(MountTestBase):
459 def getProjectWithManyCollections(self):
460 project_contents = llfuse.listdir(self.mounttmp)
461 self.assertEqual(201, len(project_contents))
462 self.assertIn('Collection_1', project_contents)
463 return project_contents
466 def listContentsInProjectWithManyCollections(self, project_contents):
467 project_contents = llfuse.listdir(self.mounttmp)
468 self.assertEqual(201, len(project_contents))
469 self.assertIn('Collection_1', project_contents)
471 for collection_name in project_contents:
472 collection_contents = llfuse.listdir(os.path.join(self.mounttmp, collection_name))
473 self.assertIn('baz', collection_contents)
475 def test_listLargeProjectContents(self):
476 self.make_mount(fuse.ProjectDirectory,
477 project_object=run_test_server.fixture('groups')['project_with_201_collections'])
478 project_contents = self.getProjectWithManyCollections()
479 self.listContentsInProjectWithManyCollections(project_contents)