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
24 def fuseCreateCollectionWithManyFiles(mounttmp, streams=1, files_per_stream=1, blocks_per_file=1, bytes_per_block=1, data='x'):
25 class Test(unittest.TestCase):
28 for i in range(1, files_per_stream):
29 names += ',file' + str(i) + '.txt'
30 file_names = names.split(',')
32 for i in range(0, streams):
33 with self.assertRaises(IOError):
34 with open(os.path.join(mounttmp, "./stream", "file0.txt"), "w") as f:
37 os.mkdir(os.path.join(mounttmp, "./stream" + str(i)))
39 with self.assertRaises(OSError):
40 os.mkdir(os.path.join(mounttmp, "./stream" + str(i)))
43 for j in range(0, files_per_stream):
44 with open(os.path.join(mounttmp, "./stream" + str(i), "file" + str(j) +".txt"), "w") as f:
47 d1 = llfuse.listdir(os.path.join(mounttmp, "./stream" + str(i)))
48 self.assertEqual(sorted(file_names), sorted(d1))
52 def fuseReadContentsFromCollectionWithManyFiles(mounttmp, streams, files_per_stream, content):
53 class Test(unittest.TestCase):
55 for i in range(0, streams):
56 d1 = llfuse.listdir(os.path.join(mounttmp, 'stream'+str(i)))
57 for j in range(0, files_per_stream):
58 with open(os.path.join(mounttmp, 'stream'+str(i), 'file'+str(i)+'.txt')) as f:
59 self.assertEqual(content, f.read())
63 def fuseMoveFileFromCollectionWithManyFiles(mounttmp, stream, filename):
64 class Test(unittest.TestCase):
66 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
67 self.assertIn(filename, d1)
69 os.rename(os.path.join(mounttmp, stream, filename), os.path.join(mounttmp, 'moved-from-'+stream+'-'+filename))
71 d1 = llfuse.listdir(os.path.join(mounttmp))
72 self.assertIn('moved-from-'+stream+'-'+filename, d1)
74 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
75 self.assertNotIn(filename, d1)
79 def fuseDeleteFileFromCollectionWithManyFiles(mounttmp, stream, filename):
80 class Test(unittest.TestCase):
82 d1 = llfuse.listdir(os.path.join(mounttmp, stream))
85 os.remove(os.path.join(mounttmp, stream, filename))
87 # Try to delete it again
88 with self.assertRaises(OSError):
89 os.remove(os.path.join(mounttmp, "testdir", "file1.txt"))
93 # Create a collection with two streams, each with 200 files
94 class CreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
96 def test_CreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
97 collection = arvados.collection.Collection(api_client=self.api)
100 m = self.make_mount(fuse.CollectionDirectory)
102 m.new_collection(collection.api_response(), collection)
103 self.assertTrue(m.writable())
106 files_per_stream = 200
110 data = 'x' * blocks_per_file * bytes_per_block
112 self.pool.apply(fuseCreateCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, blocks_per_file, bytes_per_block, data))
114 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
116 for i in range(0, streams):
117 self.assertIn('./stream' + str(i), collection2["manifest_text"])
119 for i in range(0, files_per_stream):
120 self.assertIn('file' + str(i) + '.txt', collection2["manifest_text"])
123 self.pool.apply(fuseReadContentsFromCollectionWithManyFiles, (self.mounttmp, streams, files_per_stream, data,))
125 # Move file0.txt out of the streams into .
126 for i in range(0, streams):
127 self.pool.apply(fuseMoveFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file0.txt',))
129 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
131 manifest_streams = collection2['manifest_text'].split('\n')
132 self.assertEqual(4, len(manifest_streams))
134 for i in range(0, streams):
135 self.assertIn('moved-from-stream'+str(i)+'-file0.txt', manifest_streams[0])
137 for i in range(0, streams):
138 self.assertNotIn('file0.txt', manifest_streams[i+1])
140 for i in range(0, streams):
141 for j in range(1, files_per_stream):
142 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
144 # Delete 'file1.txt' from all the streams
145 for i in range(0, streams):
146 self.pool.apply(fuseDeleteFileFromCollectionWithManyFiles, (self.mounttmp, 'stream'+str(i), 'file1.txt'))
148 collection2 = self.api.collections().get(uuid=collection.manifest_locator()).execute()
150 manifest_streams = collection2['manifest_text'].split('\n')
151 self.assertEqual(4, len(manifest_streams))
153 for i in range(0, streams):
154 self.assertIn('moved-from-stream'+str(i)+'-file0.txt', manifest_streams[0])
156 self.assertNotIn('file1.txt', collection2['manifest_text'])
158 for i in range(0, streams):
159 for j in range(2, files_per_stream):
160 self.assertIn('file' + str(j) + '.txt', manifest_streams[i+1])
163 class UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile(MountTestBase):
165 super(UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile, self).setUp()
168 def test_UsingMagicDirCreateCollectionWithManyFilesAndMoveAndDeleteFile(self):
170 cw = arvados.CollectionWriter()
173 files_per_stream = 200
177 data = 'x' * blocks_per_file * bytes_per_block
178 for i in range(0, streams):
179 cw.start_new_stream('./stream' + str(i))
180 for j in range(0, files_per_stream):
181 cw.start_new_file('file' + str(j) + '.txt')
184 self.testcollection = cw.finish()
185 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
188 self.make_mount(fuse.MagicDirectory)
190 mount_ls = llfuse.listdir(self.mounttmp)
191 self.assertIn('README', mount_ls)
193 self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
194 arvados.util.uuid_pattern.match(fn)
196 "new FUSE MagicDirectory lists Collection")
199 for i in range(1, streams):
200 names += ',stream' + str(i)
201 stream_names = names.split(',')
204 for i in range(1, files_per_stream):
205 names += ',file' + str(i) + '.txt'
206 file_names = names.split(',')
208 self.assertDirContents(self.testcollection, stream_names)
209 self.assertDirContents(os.path.join('by_id', self.testcollection), stream_names)
211 mount_ls = llfuse.listdir(self.mounttmp)
212 self.assertIn('README', mount_ls)
213 self.assertIn(self.testcollection, mount_ls)
214 self.assertIn(self.testcollection,
215 llfuse.listdir(os.path.join(self.mounttmp, 'by_id')))
218 for i in range(0, streams):
219 for j in range(0, files_per_stream):
220 files[os.path.join(self.mounttmp, self.testcollection, 'stream'+str(i)+'/file'+str(j)+'.txt')] = data
222 for k, v in files.items():
223 with open(os.path.join(self.mounttmp, k)) as f:
224 self.assertEqual(v, f.read())