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 fuseCreateCollectionWithManyFiles(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 fuseReadContentsFromCollectionWithManyFiles(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 fuseMoveFileFromCollectionWithManyFiles(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 fuseDeleteFileFromCollectionWithManyFiles(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(fuseCreateCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, blocks_per_file, bytes_per_block, data))
100 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
102 for i in range(0, streams):
103 self.assertIn('./stream' + str(i), collection2["manifest_text"])
105 for i in range(0, files_per_stream):
106 self.assertIn('file' + str(i) + '.txt', collection2["manifest_text"])
109 self.pool.apply(fuseReadContentsFromCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
111 # Move file0.txt out of the streams into .
112 for i in range(0, streams):
113 self.pool.apply(fuseMoveFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file0.txt',))
115 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
117 manifest_streams = collection2['manifest_text'].split('\n')
118 self.assertEqual(4, len(manifest_streams))
120 for i in range(0, streams):
121 self.assertIn('file0.txt', manifest_streams[0])
123 for i in range(0, streams):
124 self.assertNotIn('file0.txt', manifest_streams[i+1])
126 for i in range(0, streams):
127 for j in range(1, files_per_stream):
128 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
130 # Delete 'file1.txt' from all the streams
131 for i in range(0, streams):
132 self.pool.apply(fuseDeleteFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file1.txt'))
134 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
136 manifest_streams = collection2['manifest_text'].split('\n')
137 self.assertEqual(4, len(manifest_streams))
139 for i in range(0, streams):
140 self.assertIn('file0.txt', manifest_streams[0])
142 self.assertNotIn('file1.txt', collection2['manifest_text'])
144 for i in range(0, streams):
145 for j in range(2, files_per_stream):
146 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
150 def magicDirTest_MoveFileFromCollectionWithManyFiles(mounttmp, collection1, collection2, stream, filename):
151 class Test(unittest.TestCase):
153 #os.rename(os.path.join(mounttmp, collection1, stream, filename), os.path.join(mounttmp, collection2, stream, filename))
158 class UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
160 super(UsingMagicDir_CreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
162 def magicDirTest_createCollectionWithManyFiles(self, streams=1, files_per_stream=1, blocks_per_file=1, bytes_per_block=1, data='x'):
164 cw = arvados.CollectionWriter()
165 for i in range(0, streams):
166 cw.start_new_stream('./stream' + str(i))
167 for j in range(0, files_per_stream):
168 cw.start_new_file('file' + str(j) + '.txt')
171 self.testcollection = cw.finish()
172 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
173 return self.testcollection
176 def magicDirTest_readCollectionContents(self, collection, streams=1, files_per_stream=1, blocks_per_file=1, bytes_per_block=1, data='x'):
177 stream_names = ["stream%i" % i for i in range(0, streams)]
178 file_names = ["file%i.txt" % i for i in range(0, files_per_stream)]
180 self.assertDirContents(collection, stream_names)
181 self.assertDirContents(os.path.join('by_id', collection), stream_names)
183 mount_ls = llfuse.listdir(self.mounttmp)
184 self.assertIn('README', mount_ls)
185 self.assertIn(collection, mount_ls)
186 self.assertIn(collection,
187 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
190 for i in range(0, streams):
191 for j in range(0, files_per_stream):
192 files[os.path.join(self.mounttmp, collection, 'stream'+str(i)+'/file'+str(j)+'.txt')] = data
194 for k, v in files.items():
195 with open(os.path.join(self.mounttmp, k)) as f:
196 self.assertEqual(v, f.read())
198 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
200 files_per_stream = 200
204 data = 'x' * blocks_per_file * bytes_per_block
206 collection1 = self.magicDirTest_createCollectionWithManyFiles(streams, files_per_stream, blocks_per_file, bytes_per_block, data)
207 collection2 = self.magicDirTest_createCollectionWithManyFiles()
210 self.make_mount(fuse.MagicDirectory)
212 self.magicDirTest_readCollectionContents(collection1, streams, files_per_stream, blocks_per_file, bytes_per_block, data)
214 # Move file0.txt out of the streams into .
215 self.pool.apply(magicDirTest_MoveFileFromCollectionWithManyFiles, (self.mounttmp, collection1, collection2, 'stream0', 'file1.txt',))