2 import arvados_fuse as fuse
15 import run_test_server
17 class MountTestBase(unittest.TestCase):
19 self.keeptmp = tempfile.mkdtemp()
20 os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
21 self.mounttmp = tempfile.mkdtemp()
23 run_test_server.authorize_with("admin")
24 self.api = fuse.SafeApi(arvados.config)
26 def make_mount(self, root_class, **root_kwargs):
27 operations = fuse.Operations(os.getuid(), os.getgid())
28 operations.inodes.add_entry(root_class(
29 llfuse.ROOT_INODE, operations.inodes, self.api, 0, **root_kwargs))
30 llfuse.init(operations, self.mounttmp, [])
31 threading.Thread(None, llfuse.main).start()
32 # wait until the driver is finished initializing
33 operations.initlock.wait()
36 # llfuse.close is buggy, so use fusermount instead.
37 #llfuse.close(unmount=True)
40 while (count < 9 and success != 0):
41 success = subprocess.call(["fusermount", "-u", self.mounttmp])
45 os.rmdir(self.mounttmp)
46 shutil.rmtree(self.keeptmp)
47 run_test_server.reset()
49 def assertDirContents(self, subdir, expect_content):
52 path = os.path.join(path, subdir)
53 self.assertEqual(sorted(expect_content), sorted(os.listdir(path)))
56 class FuseMountTest(MountTestBase):
58 super(FuseMountTest, self).setUp()
60 cw = arvados.CollectionWriter()
62 cw.start_new_file('thing1.txt')
64 cw.start_new_file('thing2.txt')
66 cw.start_new_stream('dir1')
68 cw.start_new_file('thing3.txt')
70 cw.start_new_file('thing4.txt')
73 cw.start_new_stream('dir2')
74 cw.start_new_file('thing5.txt')
76 cw.start_new_file('thing6.txt')
79 cw.start_new_stream('dir2/dir3')
80 cw.start_new_file('thing7.txt')
83 cw.start_new_file('thing8.txt')
86 cw.start_new_stream('edgecases')
87 for f in ":/./../.../-/*/\x01\\/ ".split("/"):
91 for f in ":/../.../-/*/\x01\\/ ".split("/"):
92 cw.start_new_stream('edgecases/dirs/' + f)
93 cw.start_new_file('x/x')
96 self.testcollection = cw.finish()
97 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
100 self.make_mount(fuse.CollectionDirectory, collection=self.testcollection)
102 self.assertDirContents(None, ['thing1.txt', 'thing2.txt',
103 'edgecases', 'dir1', 'dir2'])
104 self.assertDirContents('dir1', ['thing3.txt', 'thing4.txt'])
105 self.assertDirContents('dir2', ['thing5.txt', 'thing6.txt', 'dir3'])
106 self.assertDirContents('dir2/dir3', ['thing7.txt', 'thing8.txt'])
107 self.assertDirContents('edgecases',
108 "dirs/:/_/__/.../-/*/\x01\\/ ".split("/"))
109 self.assertDirContents('edgecases/dirs',
110 ":/__/.../-/*/\x01\\/ ".split("/"))
112 files = {'thing1.txt': 'data 1',
113 'thing2.txt': 'data 2',
114 'dir1/thing3.txt': 'data 3',
115 'dir1/thing4.txt': 'data 4',
116 'dir2/thing5.txt': 'data 5',
117 'dir2/thing6.txt': 'data 6',
118 'dir2/dir3/thing7.txt': 'data 7',
119 'dir2/dir3/thing8.txt': 'data 8'}
121 for k, v in files.items():
122 with open(os.path.join(self.mounttmp, k)) as f:
123 self.assertEqual(v, f.read())
126 class FuseNoAPITest(MountTestBase):
128 super(FuseNoAPITest, self).setUp()
129 keep = arvados.keep.KeepClient(local_store=self.keeptmp)
130 self.file_data = "API-free text\n"
131 self.file_loc = keep.put(self.file_data)
132 self.coll_loc = keep.put(". {} 0:{}:api-free.txt\n".format(
133 self.file_loc, len(self.file_data)))
136 self.make_mount(fuse.MagicDirectory)
137 self.assertDirContents(self.coll_loc, ['api-free.txt'])
138 with open(os.path.join(
139 self.mounttmp, self.coll_loc, 'api-free.txt')) as keep_file:
140 actual = keep_file.read(-1)
141 self.assertEqual(self.file_data, actual)
144 class FuseMagicTest(MountTestBase):
146 super(FuseMagicTest, self).setUp()
148 cw = arvados.CollectionWriter()
150 cw.start_new_file('thing1.txt')
153 self.testcollection = cw.finish()
154 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
157 self.make_mount(fuse.MagicDirectory)
159 mount_ls = os.listdir(self.mounttmp)
160 self.assertIn('README', mount_ls)
161 self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
162 arvados.util.uuid_pattern.match(fn)
164 "new FUSE MagicDirectory lists Collection")
165 self.assertDirContents(self.testcollection, ['thing1.txt'])
166 self.assertDirContents(os.path.join('by_id', self.testcollection),
168 mount_ls = os.listdir(self.mounttmp)
169 self.assertIn('README', mount_ls)
170 self.assertIn(self.testcollection, mount_ls)
171 self.assertIn(self.testcollection,
172 os.listdir(os.path.join(self.mounttmp, 'by_id')))
175 files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
177 for k, v in files.items():
178 with open(os.path.join(self.mounttmp, k)) as f:
179 self.assertEqual(v, f.read())
182 class FuseTagsTest(MountTestBase):
184 self.make_mount(fuse.TagsDirectory)
186 d1 = os.listdir(self.mounttmp)
188 self.assertEqual(['foo_tag'], d1)
190 d2 = os.listdir(os.path.join(self.mounttmp, 'foo_tag'))
192 self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
194 d3 = os.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
196 self.assertEqual(['foo'], d3)
199 class FuseTagsUpdateTest(MountTestBase):
200 def tag_collection(self, coll_uuid, tag_name):
201 return self.api.links().create(
202 body={'link': {'head_uuid': coll_uuid,
208 self.make_mount(fuse.TagsDirectory, poll_time=1)
210 self.assertIn('foo_tag', os.listdir(self.mounttmp))
212 bar_uuid = run_test_server.fixture('collections')['bar_file']['uuid']
213 self.tag_collection(bar_uuid, 'fuse_test_tag')
215 self.assertIn('fuse_test_tag', os.listdir(self.mounttmp))
216 self.assertDirContents('fuse_test_tag', [bar_uuid])
218 baz_uuid = run_test_server.fixture('collections')['baz_file']['uuid']
219 l = self.tag_collection(baz_uuid, 'fuse_test_tag')
221 self.assertDirContents('fuse_test_tag', [bar_uuid, baz_uuid])
223 self.api.links().delete(uuid=l['uuid']).execute()
225 self.assertDirContents('fuse_test_tag', [bar_uuid])
228 class FuseSharedTest(MountTestBase):
230 self.make_mount(fuse.SharedDirectory,
231 exclude=self.api.users().current().execute()['uuid'])
233 # shared_dirs is a list of the directories exposed
234 # by fuse.SharedDirectory (i.e. any object visible
235 # to the current user)
236 shared_dirs = os.listdir(self.mounttmp)
238 self.assertIn('FUSE User', shared_dirs)
240 # fuse_user_objs is a list of the objects owned by the FUSE
241 # test user (which present as files in the 'FUSE User'
243 fuse_user_objs = os.listdir(os.path.join(self.mounttmp, 'FUSE User'))
244 fuse_user_objs.sort()
245 self.assertEqual(['Empty collection.link', # permission link on collection
246 'FUSE Test Project', # project owned by user
247 'collection #1 owned by FUSE', # collection owned by user
248 'collection #2 owned by FUSE', # collection owned by user
249 'pipeline instance owned by FUSE.pipelineInstance', # pipeline instance owned by user
252 # test_proj_files is a list of the files in the FUSE Test Project.
253 test_proj_files = os.listdir(os.path.join(self.mounttmp, 'FUSE User', 'FUSE Test Project'))
254 test_proj_files.sort()
255 self.assertEqual(['collection in FUSE project',
256 'pipeline instance in FUSE project.pipelineInstance',
257 'pipeline template in FUSE project.pipelineTemplate'
260 # Double check that we can open and read objects in this folder as a file,
261 # and that its contents are what we expect.
262 with open(os.path.join(
266 'pipeline template in FUSE project.pipelineTemplate')) as f:
268 self.assertEqual("pipeline template in FUSE project", j['name'])
271 class FuseHomeTest(MountTestBase):
273 self.make_mount(fuse.ProjectDirectory,
274 project_object=self.api.users().current().execute())
276 d1 = os.listdir(self.mounttmp)
277 self.assertIn('Unrestricted public data', d1)
279 d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
280 public_project = run_test_server.fixture('groups')[
281 'anonymously_accessible_project']
284 for name, item in run_test_server.fixture('collections').iteritems():
285 if 'name' not in item:
287 elif item['owner_uuid'] == public_project['uuid']:
288 self.assertIn(item['name'], d2)
291 # Artificial assumption here: there is no public
292 # collection fixture with the same name as a
293 # non-public collection.
294 self.assertNotIn(item['name'], d2)
296 self.assertNotEqual(0, found_in)
297 self.assertNotEqual(0, found_not_in)
299 d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
300 self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
303 class FuseUnitTest(unittest.TestCase):
304 def test_sanitize_filename(self):
324 self.assertEqual(f, fuse.sanitize_filename(f))
325 for f in unacceptable:
326 self.assertNotEqual(f, fuse.sanitize_filename(f))
327 # The sanitized filename should be the same length, though.
328 self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
330 self.assertEqual("_", fuse.sanitize_filename(""))
331 self.assertEqual("_", fuse.sanitize_filename("."))
332 self.assertEqual("__", fuse.sanitize_filename(".."))