3198: Add --file-cache and --directory-cache to arv-mount to specify desired
[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(['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         pipeline_template_path = os.path.join(
263                 self.mounttmp,
264                 'FUSE User',
265                 'FUSE Test Project',
266                 'pipeline template in FUSE project.pipelineTemplate')
267         with open(pipeline_template_path) as f:
268             j = json.load(f)
269             self.assertEqual("pipeline template in FUSE project", j['name'])
270
271         # check mtime on template
272         st = os.stat(pipeline_template_path)
273         self.assertEqual(st.st_mtime, 1397493304)
274
275         # check mtime on collection
276         st = os.stat(os.path.join(
277                 self.mounttmp,
278                 'FUSE User',
279                 'collection #1 owned by FUSE'))
280         self.assertEqual(st.st_mtime, 1391448174)
281
282
283 class FuseHomeTest(MountTestBase):
284     def runTest(self):
285         self.make_mount(fuse.ProjectDirectory,
286                         project_object=self.api.users().current().execute())
287
288         d1 = os.listdir(self.mounttmp)
289         self.assertIn('Unrestricted public data', d1)
290
291         d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
292         public_project = run_test_server.fixture('groups')[
293             'anonymously_accessible_project']
294         found_in = 0
295         found_not_in = 0
296         for name, item in run_test_server.fixture('collections').iteritems():
297             if 'name' not in item:
298                 pass
299             elif item['owner_uuid'] == public_project['uuid']:
300                 self.assertIn(item['name'], d2)
301                 found_in += 1
302             else:
303                 # Artificial assumption here: there is no public
304                 # collection fixture with the same name as a
305                 # non-public collection.
306                 self.assertNotIn(item['name'], d2)
307                 found_not_in += 1
308         self.assertNotEqual(0, found_in)
309         self.assertNotEqual(0, found_not_in)
310
311         d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
312         self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)
313
314
315 class FuseUnitTest(unittest.TestCase):
316     def test_sanitize_filename(self):
317         acceptable = [
318             "foo.txt",
319             ".foo",
320             "..foo",
321             "...",
322             "foo...",
323             "foo..",
324             "foo.",
325             "-",
326             "\x01\x02\x03",
327             ]
328         unacceptable = [
329             "f\00",
330             "\00\00",
331             "/foo",
332             "foo/",
333             "//",
334             ]
335         for f in acceptable:
336             self.assertEqual(f, fuse.sanitize_filename(f))
337         for f in unacceptable:
338             self.assertNotEqual(f, fuse.sanitize_filename(f))
339             # The sanitized filename should be the same length, though.
340             self.assertEqual(len(f), len(fuse.sanitize_filename(f)))
341         # Special cases
342         self.assertEqual("_", fuse.sanitize_filename(""))
343         self.assertEqual("_", fuse.sanitize_filename("."))
344         self.assertEqual("__", fuse.sanitize_filename(".."))