Merge branch '5322-sso-manual-account-doc' closes #5322
[arvados.git] / services / fuse / tests / test_mount.py
1 import arvados
2 import arvados.safeapi
3 import arvados_fuse as fuse
4 import glob
5 import json
6 import llfuse
7 import os
8 import shutil
9 import subprocess
10 import sys
11 import tempfile
12 import threading
13 import time
14 import unittest
15
16 import run_test_server
17
18 class MountTestBase(unittest.TestCase):
19     def setUp(self):
20         self.keeptmp = tempfile.mkdtemp()
21         os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
22         self.mounttmp = tempfile.mkdtemp()
23         run_test_server.run()
24         run_test_server.authorize_with("admin")
25         self.api = arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
26
27     def make_mount(self, root_class, **root_kwargs):
28         operations = fuse.Operations(os.getuid(), os.getgid())
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()
35
36     def tearDown(self):
37         # llfuse.close is buggy, so use fusermount instead.
38         #llfuse.close(unmount=True)
39         count = 0
40         success = 1
41         while (count < 9 and success != 0):
42           success = subprocess.call(["fusermount", "-u", self.mounttmp])
43           time.sleep(0.5)
44           count += 1
45
46         os.rmdir(self.mounttmp)
47         shutil.rmtree(self.keeptmp)
48         run_test_server.reset()
49
50     def assertDirContents(self, subdir, expect_content):
51         path = self.mounttmp
52         if subdir:
53             path = os.path.join(path, subdir)
54         self.assertEqual(sorted(expect_content), sorted(os.listdir(path)))
55
56
57 class FuseMountTest(MountTestBase):
58     def setUp(self):
59         super(FuseMountTest, self).setUp()
60
61         cw = arvados.CollectionWriter()
62
63         cw.start_new_file('thing1.txt')
64         cw.write("data 1")
65         cw.start_new_file('thing2.txt')
66         cw.write("data 2")
67         cw.start_new_stream('dir1')
68
69         cw.start_new_file('thing3.txt')
70         cw.write("data 3")
71         cw.start_new_file('thing4.txt')
72         cw.write("data 4")
73
74         cw.start_new_stream('dir2')
75         cw.start_new_file('thing5.txt')
76         cw.write("data 5")
77         cw.start_new_file('thing6.txt')
78         cw.write("data 6")
79
80         cw.start_new_stream('dir2/dir3')
81         cw.start_new_file('thing7.txt')
82         cw.write("data 7")
83
84         cw.start_new_file('thing8.txt')
85         cw.write("data 8")
86
87         cw.start_new_stream('edgecases')
88         for f in ":/./../.../-/*/\x01\\/ ".split("/"):
89             cw.start_new_file(f)
90             cw.write('x')
91
92         for f in ":/../.../-/*/\x01\\/ ".split("/"):
93             cw.start_new_stream('edgecases/dirs/' + f)
94             cw.start_new_file('x/x')
95             cw.write('x')
96
97         self.testcollection = cw.finish()
98         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
99
100     def runTest(self):
101         self.make_mount(fuse.CollectionDirectory, collection=self.testcollection)
102
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("/"))
112
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'}
121
122         for k, v in files.items():
123             with open(os.path.join(self.mounttmp, k)) as f:
124                 self.assertEqual(v, f.read())
125
126
127 class FuseNoAPITest(MountTestBase):
128     def setUp(self):
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)))
135
136     def runTest(self):
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)
143
144
145 class FuseMagicTest(MountTestBase):
146     def setUp(self):
147         super(FuseMagicTest, self).setUp()
148
149         cw = arvados.CollectionWriter()
150
151         cw.start_new_file('thing1.txt')
152         cw.write("data 1")
153
154         self.testcollection = cw.finish()
155         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
156
157     def runTest(self):
158         self.make_mount(fuse.MagicDirectory)
159
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)
164                              for fn in mount_ls),
165                          "new FUSE MagicDirectory lists Collection")
166         self.assertDirContents(self.testcollection, ['thing1.txt'])
167         self.assertDirContents(os.path.join('by_id', self.testcollection),
168                                ['thing1.txt'])
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')))
174
175         files = {}
176         files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
177
178         for k, v in files.items():
179             with open(os.path.join(self.mounttmp, k)) as f:
180                 self.assertEqual(v, f.read())
181
182
183 class FuseTagsTest(MountTestBase):
184     def runTest(self):
185         self.make_mount(fuse.TagsDirectory)
186
187         d1 = os.listdir(self.mounttmp)
188         d1.sort()
189         self.assertEqual(['foo_tag'], d1)
190
191         d2 = os.listdir(os.path.join(self.mounttmp, 'foo_tag'))
192         d2.sort()
193         self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
194
195         d3 = os.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
196         d3.sort()
197         self.assertEqual(['foo'], d3)
198
199
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,
204                            'link_class': 'tag',
205                            'name': tag_name,
206         }}).execute()
207
208     def runTest(self):
209         self.make_mount(fuse.TagsDirectory, poll_time=1)
210
211         self.assertIn('foo_tag', os.listdir(self.mounttmp))
212
213         bar_uuid = run_test_server.fixture('collections')['bar_file']['uuid']
214         self.tag_collection(bar_uuid, 'fuse_test_tag')
215         time.sleep(1)
216         self.assertIn('fuse_test_tag', os.listdir(self.mounttmp))
217         self.assertDirContents('fuse_test_tag', [bar_uuid])
218
219         baz_uuid = run_test_server.fixture('collections')['baz_file']['uuid']
220         l = self.tag_collection(baz_uuid, 'fuse_test_tag')
221         time.sleep(1)
222         self.assertDirContents('fuse_test_tag', [bar_uuid, baz_uuid])
223
224         self.api.links().delete(uuid=l['uuid']).execute()
225         time.sleep(1)
226         self.assertDirContents('fuse_test_tag', [bar_uuid])
227
228
229 class FuseSharedTest(MountTestBase):
230     def runTest(self):
231         self.make_mount(fuse.SharedDirectory,
232                         exclude=self.api.users().current().execute()['uuid'])
233
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)
238         shared_dirs.sort()
239         self.assertIn('FUSE User', shared_dirs)
240
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'
243         # directory)
244         fuse_user_objs = os.listdir(os.path.join(self.mounttmp, 'FUSE User'))
245         fuse_user_objs.sort()
246         self.assertEqual(['Empty collection.link',                # permission link on collection
247                           'FUSE Test Project',                    # project owned by user
248                           'collection #1 owned by FUSE',          # collection owned by user
249                           'collection #2 owned by FUSE',          # collection owned by user
250                           'pipeline instance owned by FUSE.pipelineInstance',  # pipeline instance owned by user
251                       ], fuse_user_objs)
252
253         # test_proj_files is a list of the files in the FUSE Test Project.
254         test_proj_files = os.listdir(os.path.join(self.mounttmp, 'FUSE User', 'FUSE Test Project'))
255         test_proj_files.sort()
256         self.assertEqual(['collection in FUSE project',
257                           'pipeline instance in FUSE project.pipelineInstance',
258                           'pipeline template in FUSE project.pipelineTemplate'
259                       ], test_proj_files)
260
261         # Double check that we can open and read objects in this folder as a file,
262         # and that its contents are what we expect.
263         pipeline_template_path = os.path.join(
264                 self.mounttmp,
265                 'FUSE User',
266                 'FUSE Test Project',
267                 'pipeline template in FUSE project.pipelineTemplate')
268         with open(pipeline_template_path) as f:
269             j = json.load(f)
270             self.assertEqual("pipeline template in FUSE project", j['name'])
271
272         # check mtime on template
273         st = os.stat(pipeline_template_path)
274         self.assertEqual(st.st_mtime, 1397493304)
275
276         # check mtime on collection
277         st = os.stat(os.path.join(
278                 self.mounttmp,
279                 'FUSE User',
280                 'collection #1 owned by FUSE'))
281         self.assertEqual(st.st_mtime, 1391448174)
282
283
284 class FuseHomeTest(MountTestBase):
285     def runTest(self):
286         self.make_mount(fuse.ProjectDirectory,
287                         project_object=self.api.users().current().execute())
288
289         d1 = os.listdir(self.mounttmp)
290         self.assertIn('Unrestricted public data', d1)
291
292         d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
293         public_project = run_test_server.fixture('groups')[
294             'anonymously_accessible_project']
295         found_in = 0
296         found_not_in = 0
297         for name, item in run_test_server.fixture('collections').iteritems():
298             if 'name' not in item:
299                 pass
300             elif item['owner_uuid'] == public_project['uuid']:
301                 self.assertIn(item['name'], d2)
302                 found_in += 1
303             else:
304                 # Artificial assumption here: there is no public
305                 # collection fixture with the same name as a
306                 # non-public collection.
307                 self.assertNotIn(item['name'], d2)
308                 found_not_in += 1
309         self.assertNotEqual(0, found_in)
310         self.assertNotEqual(0, found_not_in)
311
312         d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
313         self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
314
315
316 class FuseUnitTest(unittest.TestCase):
317     def test_sanitize_filename(self):
318         acceptable = [
319             "foo.txt",
320             ".foo",
321             "..foo",
322             "...",
323             "foo...",
324             "foo..",
325             "foo.",
326             "-",
327             "\x01\x02\x03",
328             ]
329         unacceptable = [
330             "f\00",
331             "\00\00",
332             "/foo",
333             "foo/",
334             "//",
335             ]
336         for f in acceptable:
337             self.assertEqual(f, fuse.sanitize_filename(f))
338         for f in unacceptable:
339             self.assertNotEqual(f, fuse.sanitize_filename(f))
340             # The sanitized filename should be the same length, though.
341             self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
342         # Special cases
343         self.assertEqual("_", fuse.sanitize_filename(""))
344         self.assertEqual("_", fuse.sanitize_filename("."))
345         self.assertEqual("__", fuse.sanitize_filename(".."))