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
25 def fuse_CreateCollectionWithManyFiles(mounttmp, streams=1, files_per_stream=1, blocks_per_file=1, bytes_per_block=1, data='x'):
26 class Test(unittest.TestCase):
28 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
30 for i in range(0, streams):
31 os.mkdir(os.path.join(mounttmp, "./stream" + str(i)))
34 for j in range(0, files_per_stream):
35 with open(os.path.join(mounttmp, "./stream" + str(i), "file" + str(j) +".txt"), "w") as f:
41 def fuse_ReadContentsFromCollectionWithManyFiles(mounttmp, streams, files_per_stream, content):
42 class Test(unittest.TestCase):
44 for i in range(0, streams):
45 d1 = llfuse.listdir(os.path.join(mounttmp, 'stream'+str(i)))
46 for j in range(0, files_per_stream):
47 with open(os.path.join(mounttmp, 'stream'+str(i), 'file'+str(i)+'.txt')) as f:
48 self.assertEqual(content, f.read())
53 def fuse_MoveFileFromCollectionWithManyFiles(mounttmp, stream, filename):
54 class Test(unittest.TestCase):
56 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
57 self.assertIn(filename, d1)
59 os.rename(os.path.join(mounttmp, stream, filename), os.path.join(mounttmp, filename))
61 d1 = llfuse.listdir(os.path.join(mounttmp))
62 self.assertIn(filename, d1)
64 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
65 self.assertNotIn(filename, d1)
70 def fuse_DeleteFileFromCollectionWithManyFiles(mounttmp, stream, filename):
71 class Test(unittest.TestCase):
73 os.remove(os.path.join(mounttmp, stream, filename))
77 # Create a collection with two streams, each with 200 files
78 class CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
80 super(CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
82 def test_CreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
83 collection = arvados.collection.Collection(api_client=self.api)
86 m = self.make_mount(fuse.CollectionDirectory)
88 m.new_collection(collection.api_response(), collection)
89 self.assertTrue(m.writable())
92 files_per_stream = 200
96 data = 'x' * blocks_per_file * bytes_per_block
98 self.pool.apply(fuse_CreateCollectionWithManyFiles, (self.mounttmp, streams,
99 files_per_stream, blocks_per_file, bytes_per_block, 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_ReadContentsFromCollectionWithManyFiles, (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_MoveFileFromCollectionWithManyFiles, (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_DeleteFileFromCollectionWithManyFiles, (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])
149 def magicDirTest_MoveFileFromCollection(mounttmp, collection1, collection2, stream, filename):
150 class Test(unittest.TestCase):
152 #os.rename(os.path.join(mounttmp, collection1, stream, filename), os.path.join(mounttmp, collection2, stream, filename))
153 os.rename(os.path.join(mounttmp, collection1, filename), os.path.join(mounttmp, collection2, filename))
157 def magicDirTest_RemoveFileFromCollection(mounttmp, collection1, stream, filename):
158 class Test(unittest.TestCase):
160 os.remove(os.path.join(mounttmp, collection1, filename))
165 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
167 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
170 def magicDirTest_createCollectionWithManyFiles(self, streams=1, files_per_stream=1,
171 blocks_per_file=1, bytes_per_block=1, data='x'):
173 collection = arvados.collection.Collection(api_client=self.api)
174 for j in range(0, files_per_stream):
175 with collection.open("file"+str(j)+".txt", "w") as f:
177 collection.save_new()
181 def magicDirTest_readCollectionContents(self, collection, streams=1, files_per_stream=1,
182 blocks_per_file=1, bytes_per_block=1, data='x'):
183 mount_ls = os.listdir(os.path.join(self.mounttmp, collection))
186 for j in range(0, files_per_stream):
187 files[os.path.join(self.mounttmp, collection, 'file'+str(j)+'.txt')] = data
188 #files[os.path.join(self.mounttmp, collection, 'stream'+str(i)+'/file'+str(j)+'.txt')] = data
190 for k, v in files.items():
191 with open(os.path.join(self.mounttmp, collection, k)) as f:
192 self.assertEqual(v, f.read())
195 def magicDirTest_moveFileFromCollection(self, from_collection, to_collection):
196 self.pool.apply(magicDirTest_MoveFileFromCollection, (self.mounttmp, from_collection.manifest_locator(),
197 to_collection.manifest_locator(), 'stream0', 'file1.txt',))
198 from_collection.update()
199 to_collection.update()
202 def magicDirTest_removeFileFromCollection(self, collection):
203 self.pool.apply(magicDirTest_RemoveFileFromCollection, (self.mounttmp, collection.manifest_locator(), 'stream0', 'file0.txt',))
206 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
208 files_per_stream = 200
212 data = 'x' * blocks_per_file * bytes_per_block
214 collection1 = self.magicDirTest_createCollectionWithManyFiles()
215 # Create collection with multiple files
216 collection2 = self.magicDirTest_createCollectionWithManyFiles(streams, files_per_stream,
217 blocks_per_file, bytes_per_block, data)
220 self.make_mount(fuse.MagicDirectory)
222 self.magicDirTest_readCollectionContents(collection2.manifest_locator(), streams,
223 files_per_stream, blocks_per_file, bytes_per_block, data)
225 # Move file1.txt out of the collection2 into collection1
226 self.magicDirTest_moveFileFromCollection(collection2, collection1)
227 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
228 self.assertFalse('file1.txt' in updated_collection['manifest_text'])
229 self.assertTrue('file0.txt' in updated_collection['manifest_text'])
231 # Delete file0.txt from collection2
232 self.magicDirTest_removeFileFromCollection(collection2)
233 updated_collection = self.api.collections().get(uuid=collection2.manifest_locator()).execute()
234 self.assertFalse('file0.txt' in updated_collection['manifest_text'])
235 self.assertTrue('file2.txt' in updated_collection['manifest_text'])