Merge branch 'master' into 4904-arv-web
[arvados.git] / services / fuse / tests / test_mount.py
1 import arvados
2 import arvados_fuse as fuse
3 import glob
4 import json
5 import llfuse
6 import os
7 import shutil
8 import subprocess
9 import sys
10 import tempfile
11 import threading
12 import time
13 import unittest
14
15 import run_test_server
16
17 class MountTestBase(unittest.TestCase):
18     def setUp(self):
19         self.keeptmp = tempfile.mkdtemp()
20         os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
21         self.mounttmp = tempfile.mkdtemp()
22         run_test_server.run()
23         run_test_server.authorize_with("admin")
24         self.api = fuse.SafeApi(arvados.config)
25
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()
34
35     def tearDown(self):
36         # llfuse.close is buggy, so use fusermount instead.
37         #llfuse.close(unmount=True)
38         count = 0
39         success = 1
40         while (count < 9 and success != 0):
41           success = subprocess.call(["fusermount", "-u", self.mounttmp])
42           time.sleep(0.5)
43           count += 1
44
45         os.rmdir(self.mounttmp)
46         shutil.rmtree(self.keeptmp)
47         run_test_server.reset()
48
49     def assertDirContents(self, subdir, expect_content):
50         path = self.mounttmp
51         if subdir:
52             path = os.path.join(path, subdir)
53         self.assertEqual(sorted(expect_content), sorted(os.listdir(path)))
54
55
56 class FuseMountTest(MountTestBase):
57     def setUp(self):
58         super(FuseMountTest, self).setUp()
59
60         cw = arvados.CollectionWriter()
61
62         cw.start_new_file('thing1.txt')
63         cw.write("data 1")
64         cw.start_new_file('thing2.txt')
65         cw.write("data 2")
66         cw.start_new_stream('dir1')
67
68         cw.start_new_file('thing3.txt')
69         cw.write("data 3")
70         cw.start_new_file('thing4.txt')
71         cw.write("data 4")
72
73         cw.start_new_stream('dir2')
74         cw.start_new_file('thing5.txt')
75         cw.write("data 5")
76         cw.start_new_file('thing6.txt')
77         cw.write("data 6")
78
79         cw.start_new_stream('dir2/dir3')
80         cw.start_new_file('thing7.txt')
81         cw.write("data 7")
82
83         cw.start_new_file('thing8.txt')
84         cw.write("data 8")
85
86         cw.start_new_stream('edgecases')
87         for f in ":/./../.../-/*/\x01\\/ ".split("/"):
88             cw.start_new_file(f)
89             cw.write('x')
90
91         for f in ":/../.../-/*/\x01\\/ ".split("/"):
92             cw.start_new_stream('edgecases/dirs/' + f)
93             cw.start_new_file('x/x')
94             cw.write('x')
95
96         self.testcollection = cw.finish()
97         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
98
99     def runTest(self):
100         self.make_mount(fuse.CollectionDirectory, collection=self.testcollection)
101
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("/"))
111
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'}
120
121         for k, v in files.items():
122             with open(os.path.join(self.mounttmp, k)) as f:
123                 self.assertEqual(v, f.read())
124
125
126 class FuseNoAPITest(MountTestBase):
127     def setUp(self):
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)))
134
135     def runTest(self):
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)
142
143
144 class FuseMagicTest(MountTestBase):
145     def setUp(self):
146         super(FuseMagicTest, self).setUp()
147
148         cw = arvados.CollectionWriter()
149
150         cw.start_new_file('thing1.txt')
151         cw.write("data 1")
152
153         self.testcollection = cw.finish()
154         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
155
156     def runTest(self):
157         self.make_mount(fuse.MagicDirectory)
158
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)
163                              for fn in mount_ls),
164                          "new FUSE MagicDirectory lists Collection")
165         self.assertDirContents(self.testcollection, ['thing1.txt'])
166         self.assertDirContents(os.path.join('by_id', self.testcollection),
167                                ['thing1.txt'])
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')))
173
174         files = {}
175         files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
176
177         for k, v in files.items():
178             with open(os.path.join(self.mounttmp, k)) as f:
179                 self.assertEqual(v, f.read())
180
181
182 class FuseTagsTest(MountTestBase):
183     def runTest(self):
184         self.make_mount(fuse.TagsDirectory)
185
186         d1 = os.listdir(self.mounttmp)
187         d1.sort()
188         self.assertEqual(['foo_tag'], d1)
189
190         d2 = os.listdir(os.path.join(self.mounttmp, 'foo_tag'))
191         d2.sort()
192         self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
193
194         d3 = os.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
195         d3.sort()
196         self.assertEqual(['foo'], d3)
197
198
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,
203                            'link_class': 'tag',
204                            'name': tag_name,
205         }}).execute()
206
207     def runTest(self):
208         self.make_mount(fuse.TagsDirectory, poll_time=1)
209
210         self.assertIn('foo_tag', os.listdir(self.mounttmp))
211
212         bar_uuid = run_test_server.fixture('collections')['bar_file']['uuid']
213         self.tag_collection(bar_uuid, 'fuse_test_tag')
214         time.sleep(1)
215         self.assertIn('fuse_test_tag', os.listdir(self.mounttmp))
216         self.assertDirContents('fuse_test_tag', [bar_uuid])
217
218         baz_uuid = run_test_server.fixture('collections')['baz_file']['uuid']
219         l = self.tag_collection(baz_uuid, 'fuse_test_tag')
220         time.sleep(1)
221         self.assertDirContents('fuse_test_tag', [bar_uuid, baz_uuid])
222
223         self.api.links().delete(uuid=l['uuid']).execute()
224         time.sleep(1)
225         self.assertDirContents('fuse_test_tag', [bar_uuid])
226
227
228 class FuseSharedTest(MountTestBase):
229     def runTest(self):
230         self.make_mount(fuse.SharedDirectory,
231                         exclude=self.api.users().current().execute()['uuid'])
232
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)
237         shared_dirs.sort()
238         self.assertIn('FUSE User', shared_dirs)
239
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'
242         # directory)
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
250                       ], fuse_user_objs)
251
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'
258                       ], test_proj_files)
259
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(
263                 self.mounttmp,
264                 'FUSE User',
265                 'FUSE Test Project',
266                 'pipeline template in FUSE project.pipelineTemplate')) as f:
267             j = json.load(f)
268             self.assertEqual("pipeline template in FUSE project", j['name'])
269
270
271 class FuseHomeTest(MountTestBase):
272     def runTest(self):
273         self.make_mount(fuse.ProjectDirectory,
274                         project_object=self.api.users().current().execute())
275
276         d1 = os.listdir(self.mounttmp)
277         self.assertIn('Unrestricted public data', d1)
278
279         d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
280         self.assertEqual(['GNU General Public License, version 3',
281                           'Pipeline in publicly accessible project.pipelineInstance',
282                           'Pipeline template in publicly accessible project.pipelineTemplate'], d2)
283
284         d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
285         self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
286
287
288 class FuseUnitTest(unittest.TestCase):
289     def test_sanitize_filename(self):
290         acceptable = [
291             "foo.txt",
292             ".foo",
293             "..foo",
294             "...",
295             "foo...",
296             "foo..",
297             "foo.",
298             "-",
299             "\x01\x02\x03",
300             ]
301         unacceptable = [
302             "f\00",
303             "\00\00",
304             "/foo",
305             "foo/",
306             "//",
307             ]
308         for f in acceptable:
309             self.assertEqual(f, fuse.sanitize_filename(f))
310         for f in unacceptable:
311             self.assertNotEqual(f, fuse.sanitize_filename(f))
312             # The sanitized filename should be the same length, though.
313             self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
314         # Special cases
315         self.assertEqual("_", fuse.sanitize_filename(""))
316         self.assertEqual("_", fuse.sanitize_filename("."))
317         self.assertEqual("__", fuse.sanitize_filename(".."))