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()
385 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAllFilesIntoAnother(self):
387 files_per_stream = 200
390 collection1 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles()
391 # Create collection with multiple files
392 collection2 = self.magicDirTestMoveAllFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
395 self.make_mount(fuse.MagicDirectory)
397 # Move all files from collection2 into collection1
398 self.pool.apply(magicDirTest_MoveAllFilesFromCollection, (self.mounttmp, collection2.manifest_locator(),
399 collection1.manifest_locator(), 'stream0', files_per_stream,))
401 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
402 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
403 for name in file_names:
404 self.assertFalse(name in updated_collection['manifest_text'])
406 updated_collection = self.api.collections().get(uuid=collection1.manifest_locator()).execute()
407 for name in file_names:
408 self.assertTrue(name in updated_collection['manifest_text'])
411 # Move one file at a time from one collection into another
412 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveEachFileIntoAnother(MountTestBase):
414 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveEachFileIntoAnother, self).setUp()
417 def magicDirTestMoveFiles_createCollectionWithManyFiles(self, streams=0, files_per_stream=0, data='x'):
419 collection = arvados.collection.Collection(api_client=self.api)
420 for j in range(0, files_per_stream):
421 with collection.open("file"+str(j)+".txt", "w") as f:
423 collection.save_new()
426 def magicDirTestMoveFiles_oneEachIntoAnother(self, from_collection, to_collection, files_per_stream):
427 for j in range(0, files_per_stream):
428 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
429 to_collection.manifest_locator(), 'stream0', 'file'+str(j)+'.txt',))
431 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveEachFileIntoAnother(self):
433 files_per_stream = 200
436 collection1 = self.magicDirTestMoveFiles_createCollectionWithManyFiles()
437 # Create collection with multiple files
438 collection2 = self.magicDirTestMoveFiles_createCollectionWithManyFiles(streams, files_per_stream, data)
441 self.make_mount(fuse.MagicDirectory)
443 # Move all files from collection2 into collection1
444 self.magicDirTestMoveFiles_oneEachIntoAnother(collection2, collection1, files_per_stream)
446 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
447 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
448 for name in file_names:
449 self.assertFalse(name in updated_collection['manifest_text'])
451 updated_collection = self.api.collections().get(uuid=collection1.manifest_locator()).execute()
452 for name in file_names:
453 self.assertTrue(name in updated_collection['manifest_text'])
455 class FuseListLargeProjectContents(MountTestBase):
457 def getProjectWithManyCollections(self):
458 project_contents = llfuse.listdir(self.mounttmp)
459 self.assertEqual(201, len(project_contents))
460 self.assertIn('Collection_1', project_contents)
461 return project_contents
464 def listContentsInProjectWithManyCollections(self, project_contents):
465 project_contents = llfuse.listdir(self.mounttmp)
466 self.assertEqual(201, len(project_contents))
467 self.assertIn('Collection_1', project_contents)
469 for collection_name in project_contents:
470 collection_contents = llfuse.listdir(os.path.join(self.mounttmp, collection_name))
471 self.assertIn('baz', collection_contents)
473 def test_listLargeProjectContents(self):
474 self.make_mount(fuse.ProjectDirectory,
475 project_object=run_test_server.fixture('groups')['project_with_201_collections'])
476 project_contents = self.getProjectWithManyCollections()
477 self.listContentsInProjectWithManyCollections(project_contents)