Merge branch '4533-remote-reset' closes #4533
[arvados.git] / services / fuse / tests / test_mount.py
1 import unittest
2 import arvados
3 import arvados_fuse as fuse
4 import threading
5 import time
6 import os
7 import llfuse
8 import tempfile
9 import shutil
10 import subprocess
11 import glob
12 import run_test_server
13 import json
14
15 class MountTestBase(unittest.TestCase):
16     def setUp(self):
17         self.keeptmp = tempfile.mkdtemp()
18         os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
19         self.mounttmp = tempfile.mkdtemp()
20         run_test_server.run(False)
21         run_test_server.authorize_with("admin")
22         self.api = api = fuse.SafeApi(arvados.config)
23
24     def tearDown(self):
25         run_test_server.stop()
26
27         # llfuse.close is buggy, so use fusermount instead.
28         #llfuse.close(unmount=True)
29         count = 0
30         success = 1
31         while (count < 9 and success != 0):
32           success = subprocess.call(["fusermount", "-u", self.mounttmp])
33           time.sleep(0.5)
34           count += 1
35
36         os.rmdir(self.mounttmp)
37         shutil.rmtree(self.keeptmp)
38
39     def assertDirContents(self, subdir, expect_content):
40         path = self.mounttmp
41         if subdir:
42             path = os.path.join(path, subdir)
43         self.assertEqual(sorted(expect_content), sorted(os.listdir(path)))
44
45
46 class FuseMountTest(MountTestBase):
47     def setUp(self):
48         super(FuseMountTest, self).setUp()
49
50         cw = arvados.CollectionWriter()
51
52         cw.start_new_file('thing1.txt')
53         cw.write("data 1")
54         cw.start_new_file('thing2.txt')
55         cw.write("data 2")
56         cw.start_new_stream('dir1')
57
58         cw.start_new_file('thing3.txt')
59         cw.write("data 3")
60         cw.start_new_file('thing4.txt')
61         cw.write("data 4")
62
63         cw.start_new_stream('dir2')
64         cw.start_new_file('thing5.txt')
65         cw.write("data 5")
66         cw.start_new_file('thing6.txt')
67         cw.write("data 6")
68
69         cw.start_new_stream('dir2/dir3')
70         cw.start_new_file('thing7.txt')
71         cw.write("data 7")
72
73         cw.start_new_file('thing8.txt')
74         cw.write("data 8")
75
76         cw.start_new_stream('edgecases')
77         for f in ":/./../.../-/*/\x01\\/ ".split("/"):
78             cw.start_new_file(f)
79             cw.write('x')
80
81         for f in ":/../.../-/*/\x01\\/ ".split("/"):
82             cw.start_new_stream('edgecases/dirs/' + f)
83             cw.start_new_file('x/x')
84             cw.write('x')
85
86         self.testcollection = cw.finish()
87         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
88
89     def runTest(self):
90         # Create the request handler
91         operations = fuse.Operations(os.getuid(), os.getgid())
92         e = operations.inodes.add_entry(fuse.CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.testcollection))
93
94         llfuse.init(operations, self.mounttmp, [])
95         t = threading.Thread(None, lambda: llfuse.main())
96         t.start()
97
98         # wait until the driver is finished initializing
99         operations.initlock.wait()
100
101         # now check some stuff
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 FuseMagicTest(MountTestBase):
127     def setUp(self):
128         super(FuseMagicTest, self).setUp()
129
130         cw = arvados.CollectionWriter()
131
132         cw.start_new_file('thing1.txt')
133         cw.write("data 1")
134
135         self.testcollection = cw.finish()
136         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
137
138     def runTest(self):
139         # Create the request handler
140         operations = fuse.Operations(os.getuid(), os.getgid())
141         e = operations.inodes.add_entry(fuse.MagicDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0))
142
143         llfuse.init(operations, self.mounttmp, [])
144         t = threading.Thread(None, lambda: llfuse.main())
145         t.start()
146
147         # wait until the driver is finished initializing
148         operations.initlock.wait()
149
150         # now check some stuff
151         mount_ls = os.listdir(self.mounttmp)
152         self.assertIn('README', mount_ls)
153         self.assertFalse(any(arvados.util.keep_locator_pattern.match(fn) or
154                              arvados.util.uuid_pattern.match(fn)
155                              for fn in mount_ls),
156                          "new FUSE MagicDirectory lists Collection")
157         self.assertDirContents(self.testcollection, ['thing1.txt'])
158         self.assertDirContents(os.path.join('by_id', self.testcollection),
159                                ['thing1.txt'])
160         mount_ls = os.listdir(self.mounttmp)
161         self.assertIn('README', mount_ls)
162         self.assertIn(self.testcollection, mount_ls)
163         self.assertIn(self.testcollection,
164                       os.listdir(os.path.join(self.mounttmp, 'by_id')))
165
166         files = {}
167         files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
168
169         for k, v in files.items():
170             with open(os.path.join(self.mounttmp, k)) as f:
171                 self.assertEqual(v, f.read())
172
173
174 class FuseTagsTest(MountTestBase):
175     def runTest(self):
176         operations = fuse.Operations(os.getuid(), os.getgid())
177         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0))
178
179         llfuse.init(operations, self.mounttmp, [])
180         t = threading.Thread(None, lambda: llfuse.main())
181         t.start()
182
183         # wait until the driver is finished initializing
184         operations.initlock.wait()
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         operations = fuse.Operations(os.getuid(), os.getgid())
209         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, poll_time=1))
210
211         llfuse.init(operations, self.mounttmp, [])
212         t = threading.Thread(None, lambda: llfuse.main())
213         t.start()
214
215         # wait until the driver is finished initializing
216         operations.initlock.wait()
217         self.assertIn('foo_tag', os.listdir(self.mounttmp))
218
219         bar_uuid = run_test_server.fixture('collections')['bar_file']['uuid']
220         self.tag_collection(bar_uuid, 'fuse_test_tag')
221         time.sleep(1)
222         self.assertIn('fuse_test_tag', os.listdir(self.mounttmp))
223         self.assertDirContents('fuse_test_tag', [bar_uuid])
224
225         baz_uuid = run_test_server.fixture('collections')['baz_file']['uuid']
226         l = self.tag_collection(baz_uuid, 'fuse_test_tag')
227         time.sleep(1)
228         self.assertDirContents('fuse_test_tag', [bar_uuid, baz_uuid])
229
230         self.api.links().delete(uuid=l['uuid']).execute()
231         time.sleep(1)
232         self.assertDirContents('fuse_test_tag', [bar_uuid])
233
234
235 class FuseSharedTest(MountTestBase):
236     def runTest(self):
237         operations = fuse.Operations(os.getuid(), os.getgid())
238         e = operations.inodes.add_entry(fuse.SharedDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()['uuid']))
239
240         llfuse.init(operations, self.mounttmp, [])
241         t = threading.Thread(None, lambda: llfuse.main())
242         t.start()
243
244         # wait until the driver is finished initializing
245         operations.initlock.wait()
246
247         # shared_dirs is a list of the directories exposed
248         # by fuse.SharedDirectory (i.e. any object visible
249         # to the current user)
250         shared_dirs = os.listdir(self.mounttmp)
251         shared_dirs.sort()
252         self.assertIn('FUSE User', shared_dirs)
253
254         # fuse_user_objs is a list of the objects owned by the FUSE
255         # test user (which present as files in the 'FUSE User'
256         # directory)
257         fuse_user_objs = os.listdir(os.path.join(self.mounttmp, 'FUSE User'))
258         fuse_user_objs.sort()
259         self.assertEqual(['Empty collection.link',                # permission link on collection
260                           'FUSE Test Project',                    # project owned by user
261                           'collection #1 owned by FUSE',          # collection owned by user
262                           'collection #2 owned by FUSE',          # collection owned by user
263                           'pipeline instance owned by FUSE.pipelineInstance',  # pipeline instance owned by user
264                       ], fuse_user_objs)
265
266         # test_proj_files is a list of the files in the FUSE Test Project.
267         test_proj_files = os.listdir(os.path.join(self.mounttmp, 'FUSE User', 'FUSE Test Project'))
268         test_proj_files.sort()
269         self.assertEqual(['collection in FUSE project',
270                           'pipeline instance in FUSE project.pipelineInstance',
271                           'pipeline template in FUSE project.pipelineTemplate'
272                       ], test_proj_files)
273
274         # Double check that we can open and read objects in this folder as a file,
275         # and that its contents are what we expect.
276         with open(os.path.join(
277                 self.mounttmp,
278                 'FUSE User',
279                 'FUSE Test Project',
280                 'pipeline template in FUSE project.pipelineTemplate')) as f:
281             j = json.load(f)
282             self.assertEqual("pipeline template in FUSE project", j['name'])
283
284
285 class FuseHomeTest(MountTestBase):
286     def runTest(self):
287         operations = fuse.Operations(os.getuid(), os.getgid())
288         e = operations.inodes.add_entry(fuse.ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()))
289
290         llfuse.init(operations, self.mounttmp, [])
291         t = threading.Thread(None, lambda: llfuse.main())
292         t.start()
293
294         # wait until the driver is finished initializing
295         operations.initlock.wait()
296
297         d1 = os.listdir(self.mounttmp)
298         d1.sort()
299         self.assertIn('Unrestricted public data', d1)
300
301         d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
302         d2.sort()
303         self.assertEqual(['GNU General Public License, version 3'], d2)
304
305         d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
306         d3.sort()
307         self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
308
309
310 class FuseUnitTest(unittest.TestCase):
311     def test_sanitize_filename(self):
312         acceptable = [
313             "foo.txt",
314             ".foo",
315             "..foo",
316             "...",
317             "foo...",
318             "foo..",
319             "foo.",
320             "-",
321             "\x01\x02\x03",
322             ]
323         unacceptable = [
324             "f\00",
325             "\00\00",
326             "/foo",
327             "foo/",
328             "//",
329             ]
330         for f in acceptable:
331             self.assertEqual(f, fuse.sanitize_filename(f))
332         for f in unacceptable:
333             self.assertNotEqual(f, fuse.sanitize_filename(f))
334             # The sanitized filename should be the same length, though.
335             self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
336         # Special cases
337         self.assertEqual("_", fuse.sanitize_filename(""))
338         self.assertEqual("_", fuse.sanitize_filename("."))
339         self.assertEqual("__", fuse.sanitize_filename(".."))