4533: Merge branch 'master' into 4533-remote-reset
[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
40 class FuseMountTest(MountTestBase):
41     def setUp(self):
42         super(FuseMountTest, self).setUp()
43
44         cw = arvados.CollectionWriter()
45
46         cw.start_new_file('thing1.txt')
47         cw.write("data 1")
48         cw.start_new_file('thing2.txt')
49         cw.write("data 2")
50         cw.start_new_stream('dir1')
51
52         cw.start_new_file('thing3.txt')
53         cw.write("data 3")
54         cw.start_new_file('thing4.txt')
55         cw.write("data 4")
56
57         cw.start_new_stream('dir2')
58         cw.start_new_file('thing5.txt')
59         cw.write("data 5")
60         cw.start_new_file('thing6.txt')
61         cw.write("data 6")
62
63         cw.start_new_stream('dir2/dir3')
64         cw.start_new_file('thing7.txt')
65         cw.write("data 7")
66
67         cw.start_new_file('thing8.txt')
68         cw.write("data 8")
69
70         cw.start_new_stream('edgecases')
71         for f in ":/./../.../-/*/\x01\\/ ".split("/"):
72             cw.start_new_file(f)
73             cw.write('x')
74
75         for f in ":/../.../-/*/\x01\\/ ".split("/"):
76             cw.start_new_stream('edgecases/dirs/' + f)
77             cw.start_new_file('x/x')
78             cw.write('x')
79
80         self.testcollection = cw.finish()
81         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
82
83     def assertDirContents(self, subdir, expect_content):
84         path = self.mounttmp
85         if subdir:
86             path = os.path.join(path, subdir)
87         self.assertEqual(sorted(expect_content), sorted(os.listdir(path)))
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         self.mounttmp = tempfile.mkdtemp()
144
145         llfuse.init(operations, self.mounttmp, [])
146         t = threading.Thread(None, lambda: llfuse.main())
147         t.start()
148
149         # wait until the driver is finished initializing
150         operations.initlock.wait()
151
152         # now check some stuff
153         d1 = os.listdir(self.mounttmp)
154         d1.sort()
155         self.assertEqual(['README'], d1)
156
157         d2 = os.listdir(os.path.join(self.mounttmp, self.testcollection))
158         d2.sort()
159         self.assertEqual(['thing1.txt'], d2)
160
161         d3 = os.listdir(self.mounttmp)
162         d3.sort()
163         self.assertEqual([self.testcollection, 'README'], d3)
164
165         files = {}
166         files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
167
168         for k, v in files.items():
169             with open(os.path.join(self.mounttmp, k)) as f:
170                 self.assertEqual(v, f.read())
171
172
173 class FuseTagsTest(MountTestBase):
174     def runTest(self):
175         operations = fuse.Operations(os.getuid(), os.getgid())
176         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0))
177
178         llfuse.init(operations, self.mounttmp, [])
179         t = threading.Thread(None, lambda: llfuse.main())
180         t.start()
181
182         # wait until the driver is finished initializing
183         operations.initlock.wait()
184
185         d1 = os.listdir(self.mounttmp)
186         d1.sort()
187         self.assertEqual(['foo_tag'], d1)
188
189         d2 = os.listdir(os.path.join(self.mounttmp, 'foo_tag'))
190         d2.sort()
191         self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
192
193         d3 = os.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
194         d3.sort()
195         self.assertEqual(['foo'], d3)
196
197
198 class FuseTagsUpdateTest(MountTestBase):
199     def runRealTest(self):
200         operations = fuse.Operations(os.getuid(), os.getgid())
201         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, poll_time=1))
202
203         llfuse.init(operations, self.mounttmp, [])
204         t = threading.Thread(None, lambda: llfuse.main())
205         t.start()
206
207         # wait until the driver is finished initializing
208         operations.initlock.wait()
209
210         d1 = os.listdir(self.mounttmp)
211         d1.sort()
212         self.assertEqual(['foo_tag'], d1)
213
214         self.api.links().create(body={'link': {
215             'head_uuid': 'fa7aeb5140e2848d39b416daeef4ffc5+45',
216             'link_class': 'tag',
217             'name': 'bar_tag'
218         }}).execute()
219
220         time.sleep(1)
221
222         d2 = os.listdir(self.mounttmp)
223         d2.sort()
224         self.assertEqual(['bar_tag', 'foo_tag'], d2)
225
226         d3 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
227         d3.sort()
228         self.assertEqual(['fa7aeb5140e2848d39b416daeef4ffc5+45'], d3)
229
230         l = self.api.links().create(body={'link': {
231             'head_uuid': 'ea10d51bcf88862dbcc36eb292017dfd+45',
232             'link_class': 'tag',
233             'name': 'bar_tag'
234         }}).execute()
235
236         time.sleep(1)
237
238         d4 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
239         d4.sort()
240         self.assertEqual(['ea10d51bcf88862dbcc36eb292017dfd+45', 'fa7aeb5140e2848d39b416daeef4ffc5+45'], d4)
241
242         self.api.links().delete(uuid=l['uuid']).execute()
243
244         time.sleep(1)
245
246         d5 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
247         d5.sort()
248         self.assertEqual(['fa7aeb5140e2848d39b416daeef4ffc5+45'], d5)
249
250
251 class FuseSharedTest(MountTestBase):
252     def runTest(self):
253         operations = fuse.Operations(os.getuid(), os.getgid())
254         e = operations.inodes.add_entry(fuse.SharedDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()['uuid']))
255
256         llfuse.init(operations, self.mounttmp, [])
257         t = threading.Thread(None, lambda: llfuse.main())
258         t.start()
259
260         # wait until the driver is finished initializing
261         operations.initlock.wait()
262
263         # shared_dirs is a list of the directories exposed
264         # by fuse.SharedDirectory (i.e. any object visible
265         # to the current user)
266         shared_dirs = os.listdir(self.mounttmp)
267         shared_dirs.sort()
268         self.assertIn('FUSE User', shared_dirs)
269
270         # fuse_user_objs is a list of the objects owned by the FUSE
271         # test user (which present as files in the 'FUSE User'
272         # directory)
273         fuse_user_objs = os.listdir(os.path.join(self.mounttmp, 'FUSE User'))
274         fuse_user_objs.sort()
275         self.assertEqual(['Empty collection.link',                # permission link on collection
276                           'FUSE Test Project',                    # project owned by user
277                           'collection #1 owned by FUSE',          # collection owned by user
278                           'collection #2 owned by FUSE',          # collection owned by user
279                           'pipeline instance owned by FUSE.pipelineInstance',  # pipeline instance owned by user
280                       ], fuse_user_objs)
281
282         # test_proj_files is a list of the files in the FUSE Test Project.
283         test_proj_files = os.listdir(os.path.join(self.mounttmp, 'FUSE User', 'FUSE Test Project'))
284         test_proj_files.sort()
285         self.assertEqual(['collection in FUSE project',
286                           'pipeline instance in FUSE project.pipelineInstance',
287                           'pipeline template in FUSE project.pipelineTemplate'
288                       ], test_proj_files)
289
290         # Double check that we can open and read objects in this folder as a file,
291         # and that its contents are what we expect.
292         with open(os.path.join(
293                 self.mounttmp,
294                 'FUSE User',
295                 'FUSE Test Project',
296                 'pipeline template in FUSE project.pipelineTemplate')) as f:
297             j = json.load(f)
298             self.assertEqual("pipeline template in FUSE project", j['name'])
299
300
301 class FuseHomeTest(MountTestBase):
302     def runTest(self):
303         operations = fuse.Operations(os.getuid(), os.getgid())
304         e = operations.inodes.add_entry(fuse.ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()))
305
306         llfuse.init(operations, self.mounttmp, [])
307         t = threading.Thread(None, lambda: llfuse.main())
308         t.start()
309
310         # wait until the driver is finished initializing
311         operations.initlock.wait()
312
313         d1 = os.listdir(self.mounttmp)
314         d1.sort()
315         self.assertIn('Unrestricted public data', d1)
316
317         d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
318         d2.sort()
319         self.assertEqual(['GNU General Public License, version 3'], d2)
320
321         d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
322         d3.sort()
323         self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
324
325
326 class FuseUnitTest(unittest.TestCase):
327     def test_sanitize_filename(self):
328         acceptable = [
329             "foo.txt",
330             ".foo",
331             "..foo",
332             "...",
333             "foo...",
334             "foo..",
335             "foo.",
336             "-",
337             "\x01\x02\x03",
338             ]
339         unacceptable = [
340             "f\00",
341             "\00\00",
342             "/foo",
343             "foo/",
344             "//",
345             ]
346         for f in acceptable:
347             self.assertEqual(f, fuse.sanitize_filename(f))
348         for f in unacceptable:
349             self.assertNotEqual(f, fuse.sanitize_filename(f))
350             # The sanitized filename should be the same length, though.
351             self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
352         # Special cases
353         self.assertEqual("_", fuse.sanitize_filename(""))
354         self.assertEqual("_", fuse.sanitize_filename("."))
355         self.assertEqual("__", fuse.sanitize_filename(".."))