4363: Compare unicode strings (ignore utf-8 encoding differences). Bump fuse->sdk...
[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._utf8 = ["\xe2\x9c\x8c",     # victory sign
81                       "\xe2\x9b\xb5",     # sailboat
82                       # "\xf0\x9f\x98\xb1", # scream. doesn't work!!
83                       ]
84         cw.start_new_stream('edgecases/utf8')
85         for f in self._utf8:
86             cw.start_new_file(f)
87             cw.write(f)
88
89         self.testcollection = cw.finish()
90         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
91
92     def assertDirContents(self, subdir, expect_content):
93         path = self.mounttmp
94         if subdir:
95             path = os.path.join(path, subdir)
96         self.assertEqual(sorted([fn.decode('utf-8') for fn in expect_content]),
97                          sorted([fn.decode('utf-8') for fn in os.listdir(path)]))
98
99     def runTest(self):
100         # Create the request handler
101         operations = fuse.Operations(os.getuid(), os.getgid())
102         e = operations.inodes.add_entry(fuse.CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.testcollection))
103
104         llfuse.init(operations, self.mounttmp, [])
105         t = threading.Thread(None, lambda: llfuse.main())
106         t.start()
107
108         # wait until the driver is finished initializing
109         operations.initlock.wait()
110
111         # now check some stuff
112         self.assertDirContents(None, ['thing1.txt', 'thing2.txt',
113                                       'edgecases', 'dir1', 'dir2'])
114         self.assertDirContents('dir1', ['thing3.txt', 'thing4.txt'])
115         self.assertDirContents('dir2', ['thing5.txt', 'thing6.txt', 'dir3'])
116         self.assertDirContents('dir2/dir3', ['thing7.txt', 'thing8.txt'])
117         self.assertDirContents('edgecases',
118                                "dirs/utf8/:/_/__/.../-/*/\x01\\/ ".split("/"))
119         self.assertDirContents('edgecases/dirs',
120                                ":/__/.../-/*/\x01\\/ ".split("/"))
121         self.assertDirContents('edgecases/utf8', self._utf8)
122
123         files = {'thing1.txt': 'data 1',
124                  'thing2.txt': 'data 2',
125                  'dir1/thing3.txt': 'data 3',
126                  'dir1/thing4.txt': 'data 4',
127                  'dir2/thing5.txt': 'data 5',
128                  'dir2/thing6.txt': 'data 6',
129                  'dir2/dir3/thing7.txt': 'data 7',
130                  'dir2/dir3/thing8.txt': 'data 8'}
131
132         for k, v in files.items():
133             with open(os.path.join(self.mounttmp, k)) as f:
134                 self.assertEqual(v, f.read())
135
136
137 class FuseMagicTest(MountTestBase):
138     def setUp(self):
139         super(FuseMagicTest, self).setUp()
140
141         cw = arvados.CollectionWriter()
142
143         cw.start_new_file('thing1.txt')
144         cw.write("data 1")
145
146         self.testcollection = cw.finish()
147         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
148
149     def runTest(self):
150         # Create the request handler
151         operations = fuse.Operations(os.getuid(), os.getgid())
152         e = operations.inodes.add_entry(fuse.MagicDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0))
153
154         self.mounttmp = tempfile.mkdtemp()
155
156         llfuse.init(operations, self.mounttmp, [])
157         t = threading.Thread(None, lambda: llfuse.main())
158         t.start()
159
160         # wait until the driver is finished initializing
161         operations.initlock.wait()
162
163         # now check some stuff
164         d1 = os.listdir(self.mounttmp)
165         d1.sort()
166         self.assertEqual(['README'], d1)
167
168         d2 = os.listdir(os.path.join(self.mounttmp, self.testcollection))
169         d2.sort()
170         self.assertEqual(['thing1.txt'], d2)
171
172         d3 = os.listdir(self.mounttmp)
173         d3.sort()
174         self.assertEqual([self.testcollection, 'README'], d3)
175
176         files = {}
177         files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
178
179         for k, v in files.items():
180             with open(os.path.join(self.mounttmp, k)) as f:
181                 self.assertEqual(v, f.read())
182
183
184 class FuseTagsTest(MountTestBase):
185     def runTest(self):
186         operations = fuse.Operations(os.getuid(), os.getgid())
187         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0))
188
189         llfuse.init(operations, self.mounttmp, [])
190         t = threading.Thread(None, lambda: llfuse.main())
191         t.start()
192
193         # wait until the driver is finished initializing
194         operations.initlock.wait()
195
196         d1 = os.listdir(self.mounttmp)
197         d1.sort()
198         self.assertEqual(['foo_tag'], d1)
199
200         d2 = os.listdir(os.path.join(self.mounttmp, 'foo_tag'))
201         d2.sort()
202         self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
203
204         d3 = os.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
205         d3.sort()
206         self.assertEqual(['foo'], d3)
207
208
209 class FuseTagsUpdateTest(MountTestBase):
210     def runRealTest(self):
211         operations = fuse.Operations(os.getuid(), os.getgid())
212         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, poll_time=1))
213
214         llfuse.init(operations, self.mounttmp, [])
215         t = threading.Thread(None, lambda: llfuse.main())
216         t.start()
217
218         # wait until the driver is finished initializing
219         operations.initlock.wait()
220
221         d1 = os.listdir(self.mounttmp)
222         d1.sort()
223         self.assertEqual(['foo_tag'], d1)
224
225         self.api.links().create(body={'link': {
226             'head_uuid': 'fa7aeb5140e2848d39b416daeef4ffc5+45',
227             'link_class': 'tag',
228             'name': 'bar_tag'
229         }}).execute()
230
231         time.sleep(1)
232
233         d2 = os.listdir(self.mounttmp)
234         d2.sort()
235         self.assertEqual(['bar_tag', 'foo_tag'], d2)
236
237         d3 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
238         d3.sort()
239         self.assertEqual(['fa7aeb5140e2848d39b416daeef4ffc5+45'], d3)
240
241         l = self.api.links().create(body={'link': {
242             'head_uuid': 'ea10d51bcf88862dbcc36eb292017dfd+45',
243             'link_class': 'tag',
244             'name': 'bar_tag'
245         }}).execute()
246
247         time.sleep(1)
248
249         d4 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
250         d4.sort()
251         self.assertEqual(['ea10d51bcf88862dbcc36eb292017dfd+45', 'fa7aeb5140e2848d39b416daeef4ffc5+45'], d4)
252
253         self.api.links().delete(uuid=l['uuid']).execute()
254
255         time.sleep(1)
256
257         d5 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
258         d5.sort()
259         self.assertEqual(['fa7aeb5140e2848d39b416daeef4ffc5+45'], d5)
260
261
262 class FuseSharedTest(MountTestBase):
263     def runTest(self):
264         operations = fuse.Operations(os.getuid(), os.getgid())
265         e = operations.inodes.add_entry(fuse.SharedDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()['uuid']))
266
267         llfuse.init(operations, self.mounttmp, [])
268         t = threading.Thread(None, lambda: llfuse.main())
269         t.start()
270
271         # wait until the driver is finished initializing
272         operations.initlock.wait()
273
274         # shared_dirs is a list of the directories exposed
275         # by fuse.SharedDirectory (i.e. any object visible
276         # to the current user)
277         shared_dirs = os.listdir(self.mounttmp)
278         shared_dirs.sort()
279         self.assertIn('FUSE User', shared_dirs)
280
281         # fuse_user_objs is a list of the objects owned by the FUSE
282         # test user (which present as files in the 'FUSE User'
283         # directory)
284         fuse_user_objs = os.listdir(os.path.join(self.mounttmp, 'FUSE User'))
285         fuse_user_objs.sort()
286         self.assertEqual(['Empty collection.link',                # permission link on collection
287                           'FUSE Test Project',                    # project owned by user
288                           'collection #1 owned by FUSE',          # collection owned by user
289                           'collection #2 owned by FUSE',          # collection owned by user
290                           'pipeline instance owned by FUSE.pipelineInstance',  # pipeline instance owned by user
291                       ], fuse_user_objs)
292
293         # test_proj_files is a list of the files in the FUSE Test Project.
294         test_proj_files = os.listdir(os.path.join(self.mounttmp, 'FUSE User', 'FUSE Test Project'))
295         test_proj_files.sort()
296         self.assertEqual(['collection in FUSE project',
297                           'pipeline instance in FUSE project.pipelineInstance',
298                           'pipeline template in FUSE project.pipelineTemplate'
299                       ], test_proj_files)
300
301         # Double check that we can open and read objects in this folder as a file,
302         # and that its contents are what we expect.
303         with open(os.path.join(
304                 self.mounttmp,
305                 'FUSE User',
306                 'FUSE Test Project',
307                 'pipeline template in FUSE project.pipelineTemplate')) as f:
308             j = json.load(f)
309             self.assertEqual("pipeline template in FUSE project", j['name'])
310
311
312 class FuseHomeTest(MountTestBase):
313     def runTest(self):
314         operations = fuse.Operations(os.getuid(), os.getgid())
315         e = operations.inodes.add_entry(fuse.ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()))
316
317         llfuse.init(operations, self.mounttmp, [])
318         t = threading.Thread(None, lambda: llfuse.main())
319         t.start()
320
321         # wait until the driver is finished initializing
322         operations.initlock.wait()
323
324         d1 = os.listdir(self.mounttmp)
325         d1.sort()
326         self.assertIn('Unrestricted public data', d1)
327
328         d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
329         d2.sort()
330         self.assertEqual(['GNU General Public License, version 3'], d2)
331
332         d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
333         d3.sort()
334         self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
335
336
337 class FuseUnitTest(unittest.TestCase):
338     def test_sanitize_filename(self):
339         acceptable = [
340             "foo.txt",
341             ".foo",
342             "..foo",
343             "...",
344             "foo...",
345             "foo..",
346             "foo.",
347             "-",
348             "\x01\x02\x03",
349             ]
350         unacceptable = [
351             "f\00",
352             "\00\00",
353             "/foo",
354             "foo/",
355             "//",
356             ]
357         for f in acceptable:
358             self.assertEqual(f, fuse.sanitize_filename(f))
359         for f in unacceptable:
360             self.assertNotEqual(f, fuse.sanitize_filename(f))
361             # The sanitized filename should be the same length, though.
362             self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
363         # Special cases
364         self.assertEqual("_", fuse.sanitize_filename(""))
365         self.assertEqual("_", fuse.sanitize_filename("."))
366         self.assertEqual("__", fuse.sanitize_filename(".."))
367         self.assertEqual("__", fuse.sanitize_filename(".."))