3 import arvados_fuse as fuse
16 import run_test_server
18 class MountTestBase(unittest.TestCase):
20 self.keeptmp = tempfile.mkdtemp()
21 os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
22 self.mounttmp = tempfile.mkdtemp()
24 run_test_server.authorize_with("admin")
25 self.api = arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
27 def make_mount(self, root_class, **root_kwargs):
28 operations = fuse.Operations(os.getuid(), os.getgid(), inode_cache=2)
29 operations.inodes.add_entry(root_class(
30 llfuse.ROOT_INODE, operations.inodes, self.api, 0, **root_kwargs))
31 llfuse.init(operations, self.mounttmp, [])
32 threading.Thread(None, llfuse.main).start()
33 # wait until the driver is finished initializing
34 operations.initlock.wait()
37 # llfuse.close is buggy, so use fusermount instead.
38 #llfuse.close(unmount=True)
41 while (count < 9 and success != 0):
42 success = subprocess.call(["fusermount", "-u", self.mounttmp])
46 os.rmdir(self.mounttmp)
47 shutil.rmtree(self.keeptmp)
48 run_test_server.reset()
50 def assertDirContents(self, subdir, expect_content):
53 path = os.path.join(path, subdir)
54 self.assertEqual(sorted(expect_content), sorted(os.listdir(path)))
57 class FuseMountTest(MountTestBase):
59 super(FuseMountTest, self).setUp()
61 cw = arvados.CollectionWriter()
63 cw.start_new_file('thing1.txt')
65 cw.start_new_file('thing2.txt')
67 cw.start_new_stream('dir1')
69 cw.start_new_file('thing3.txt')
71 cw.start_new_file('thing4.txt')
74 cw.start_new_stream('dir2')
75 cw.start_new_file('thing5.txt')
77 cw.start_new_file('thing6.txt')
80 cw.start_new_stream('dir2/dir3')
81 cw.start_new_file('thing7.txt')
84 cw.start_new_file('thing8.txt')
87 cw.start_new_stream('edgecases')
88 for f in ":/./../.../-/*/\x01\\/ ".split("/"):
92 for f in ":/../.../-/*/\x01\\/ ".split("/"):
93 cw.start_new_stream('edgecases/dirs/' + f)
94 cw.start_new_file('x/x')
97 self.testcollection = cw.finish()
98 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
101 self.make_mount(fuse.CollectionDirectory, collection=self.testcollection)
103 self.assertDirContents(None, ['thing1.txt', 'thing2.txt',
104 'edgecases', 'dir1', 'dir2'])
105 self.assertDirContents('dir1', ['thing3.txt', 'thing4.txt'])
106 self.assertDirContents('dir2', ['thing5.txt', 'thing6.txt', 'dir3'])
107 self.assertDirContents('dir2/dir3', ['thing7.txt', 'thing8.txt'])
108 self.assertDirContents('edgecases',
109 "dirs/:/_/__/.../-/*/\x01\\/ ".split("/"))
110 self.assertDirContents('edgecases/dirs',
111 ":/__/.../-/*/\x01\\/ ".split("/"))
113 files = {'thing1.txt': 'data 1',
114 'thing2.txt': 'data 2',
115 'dir1/thing3.txt': 'data 3',
116 'dir1/thing4.txt': 'data 4',
117 'dir2/thing5.txt': 'data 5',
118 'dir2/thing6.txt': 'data 6',
119 'dir2/dir3/thing7.txt': 'data 7',
120 'dir2/dir3/thing8.txt': 'data 8'}
122 for k, v in files.items():
123 with open(os.path.join(self.mounttmp, k)) as f:
124 self.assertEqual(v, f.read())
127 class FuseNoAPITest(MountTestBase):
129 super(FuseNoAPITest, self).setUp()
130 keep = arvados.keep.KeepClient(local_store=self.keeptmp)
131 self.file_data = "API-free text\n"
132 self.file_loc = keep.put(self.file_data)
133 self.coll_loc = keep.put(". {} 0:{}:api-free.txt\n".format(
134 self.file_loc, len(self.file_data)))
137 self.make_mount(fuse.MagicDirectory)
138 self.assertDirContents(self.coll_loc, ['api-free.txt'])
139 with open(os.path.join(
140 self.mounttmp, self.coll_loc, 'api-free.txt')) as keep_file:
141 actual = keep_file.read(-1)
142 self.assertEqual(self.file_data, actual)
145 class FuseMagicTest(MountTestBase):
147 super(FuseMagicTest, self).setUp()
149 cw = arvados.CollectionWriter()
151 cw.start_new_file('thing1.txt')
154 self.testcollection = cw.finish()
155 self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
158 self.make_mount(fuse.MagicDirectory)
160 mount_ls = os.listdir(self.mounttmp)
161 self.assertIn('README', mount_ls)
162 self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
163 arvados.util.uuid_pattern.match(fn)
165 "new FUSE MagicDirectory lists Collection")
166 self.assertDirContents(self.testcollection, ['thing1.txt'])
167 self.assertDirContents(os.path.join('by_id', self.testcollection),
169 mount_ls = os.listdir(self.mounttmp)
170 self.assertIn('README', mount_ls)
171 self.assertIn(self.testcollection, mount_ls)
172 self.assertIn(self.testcollection,
173 os.listdir(os.path.join(self.mounttmp, 'by_id')))
176 files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
178 for k, v in files.items():
179 with open(os.path.join(self.mounttmp, k)) as f:
180 self.assertEqual(v, f.read())
183 class FuseTagsTest(MountTestBase):
185 self.make_mount(fuse.TagsDirectory)
187 d1 = os.listdir(self.mounttmp)
189 self.assertEqual(['foo_tag'], d1)
191 d2 = os.listdir(os.path.join(self.mounttmp, 'foo_tag'))
193 self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
195 d3 = os.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
197 self.assertEqual(['foo'], d3)
200 class FuseTagsUpdateTest(MountTestBase):
201 def tag_collection(self, coll_uuid, tag_name):
202 return self.api.links().create(
203 body={'link': {'head_uuid': coll_uuid,
209 self.make_mount(fuse.TagsDirectory, poll_time=1)
211 self.assertIn('foo_tag', os.listdir(self.mounttmp))
213 bar_uuid = run_test_server.fixture('collections')['bar_file']['uuid']
214 self.tag_collection(bar_uuid, 'fuse_test_tag')
216 self.assertIn('fuse_test_tag', os.listdir(self.mounttmp))
217 self.assertDirContents('fuse_test_tag', [bar_uuid])
219 baz_uuid = run_test_server.fixture('collections')['baz_file']['uuid']
220 l = self.tag_collection(baz_uuid, 'fuse_test_tag')
222 self.assertDirContents('fuse_test_tag', [bar_uuid, baz_uuid])
224 self.api.links().delete(uuid=l['uuid']).execute()
226 self.assertDirContents('fuse_test_tag', [bar_uuid])
229 class FuseSharedTest(MountTestBase):
231 self.make_mount(fuse.SharedDirectory,
232 exclude=self.api.users().current().execute()['uuid'])
234 # shared_dirs is a list of the directories exposed
235 # by fuse.SharedDirectory (i.e. any object visible
236 # to the current user)
237 shared_dirs = os.listdir(self.mounttmp)
239 self.assertIn('FUSE User', shared_dirs)
241 # fuse_user_objs is a list of the objects owned by the FUSE
242 # test user (which present as files in the 'FUSE User'
244 fuse_user_objs = os.listdir(os.path.join(self.mounttmp, 'FUSE User'))
245 fuse_user_objs.sort()
246 self.assertEqual(['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 pipeline_template_path = os.path.join(
266 'pipeline template in FUSE project.pipelineTemplate')
267 with open(pipeline_template_path) as f:
269 self.assertEqual("pipeline template in FUSE project", j['name'])
271 # check mtime on template
272 st = os.stat(pipeline_template_path)
273 self.assertEqual(st.st_mtime, 1397493304)
275 # check mtime on collection
276 st = os.stat(os.path.join(
279 'collection #1 owned by FUSE'))
280 self.assertEqual(st.st_mtime, 1391448174)
283 class FuseHomeTest(MountTestBase):
285 self.make_mount(fuse.ProjectDirectory,
286 project_object=self.api.users().current().execute())
288 d1 = os.listdir(self.mounttmp)
289 self.assertIn('Unrestricted public data', d1)
291 d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
292 public_project = run_test_server.fixture('groups')[
293 'anonymously_accessible_project']
296 for name, item in run_test_server.fixture('collections').iteritems():
297 if 'name' not in item:
299 elif item['owner_uuid'] == public_project['uuid']:
300 self.assertIn(item['name'], d2)
303 # Artificial assumption here: there is no public
304 # collection fixture with the same name as a
305 # non-public collection.
306 self.assertNotIn(item['name'], d2)
308 self.assertNotEqual(0, found_in)
309 self.assertNotEqual(0, found_not_in)
311 d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
312 self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
315 class FuseUnitTest(unittest.TestCase):
316 def test_sanitize_filename(self):
336 self.assertEqual(f, fuse.sanitize_filename(f))
337 for f in unacceptable:
338 self.assertNotEqual(f, fuse.sanitize_filename(f))
339 # The sanitized filename should be the same length, though.
340 self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
342 self.assertEqual("_", fuse.sanitize_filename(""))
343 self.assertEqual("_", fuse.sanitize_filename("."))
344 self.assertEqual("__", fuse.sanitize_filename(".."))