bb14d43c0cc2de2a173aba4d4df26accef40b827
[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         self.testcollection = cw.finish()
71         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
72
73     def runTest(self):
74         # Create the request handler
75         operations = fuse.Operations(os.getuid(), os.getgid())
76         e = operations.inodes.add_entry(fuse.CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.testcollection))
77
78         llfuse.init(operations, self.mounttmp, [])
79         t = threading.Thread(None, lambda: llfuse.main())
80         t.start()
81
82         # wait until the driver is finished initializing
83         operations.initlock.wait()
84
85         # now check some stuff
86         d1 = os.listdir(self.mounttmp)
87         d1.sort()
88         self.assertEqual(['dir1', 'dir2', 'thing1.txt', 'thing2.txt'], d1)
89
90         d2 = os.listdir(os.path.join(self.mounttmp, 'dir1'))
91         d2.sort()
92         self.assertEqual(['thing3.txt', 'thing4.txt'], d2)
93
94         d3 = os.listdir(os.path.join(self.mounttmp, 'dir2'))
95         d3.sort()
96         self.assertEqual(['dir3', 'thing5.txt', 'thing6.txt'], d3)
97
98         d4 = os.listdir(os.path.join(self.mounttmp, 'dir2/dir3'))
99         d4.sort()
100         self.assertEqual(['thing7.txt', 'thing8.txt'], d4)
101
102         files = {'thing1.txt': 'data 1',
103                  'thing2.txt': 'data 2',
104                  'dir1/thing3.txt': 'data 3',
105                  'dir1/thing4.txt': 'data 4',
106                  'dir2/thing5.txt': 'data 5',
107                  'dir2/thing6.txt': 'data 6',
108                  'dir2/dir3/thing7.txt': 'data 7',
109                  'dir2/dir3/thing8.txt': 'data 8'}
110
111         for k, v in files.items():
112             with open(os.path.join(self.mounttmp, k)) as f:
113                 self.assertEqual(v, f.read())
114
115
116 class FuseMagicTest(MountTestBase):
117     def setUp(self):
118         super(FuseMagicTest, self).setUp()
119
120         cw = arvados.CollectionWriter()
121
122         cw.start_new_file('thing1.txt')
123         cw.write("data 1")
124
125         self.testcollection = cw.finish()
126         self.api.collections().create(body={"manifest_text":cw.manifest_text()}).execute()
127
128     def runTest(self):
129         # Create the request handler
130         operations = fuse.Operations(os.getuid(), os.getgid())
131         e = operations.inodes.add_entry(fuse.MagicDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0))
132
133         self.mounttmp = tempfile.mkdtemp()
134
135         llfuse.init(operations, self.mounttmp, [])
136         t = threading.Thread(None, lambda: llfuse.main())
137         t.start()
138
139         # wait until the driver is finished initializing
140         operations.initlock.wait()
141
142         # now check some stuff
143         d1 = os.listdir(self.mounttmp)
144         d1.sort()
145         self.assertEqual(['README'], d1)
146
147         d2 = os.listdir(os.path.join(self.mounttmp, self.testcollection))
148         d2.sort()
149         self.assertEqual(['thing1.txt'], d2)
150
151         d3 = os.listdir(self.mounttmp)
152         d3.sort()
153         self.assertEqual([self.testcollection, 'README'], d3)
154
155         files = {}
156         files[os.path.join(self.mounttmp, self.testcollection, 'thing1.txt')] = 'data 1'
157
158         for k, v in files.items():
159             with open(os.path.join(self.mounttmp, k)) as f:
160                 self.assertEqual(v, f.read())
161
162
163 class FuseTagsTest(MountTestBase):
164     def runTest(self):
165         operations = fuse.Operations(os.getuid(), os.getgid())
166         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0))
167
168         llfuse.init(operations, self.mounttmp, [])
169         t = threading.Thread(None, lambda: llfuse.main())
170         t.start()
171
172         # wait until the driver is finished initializing
173         operations.initlock.wait()
174
175         d1 = os.listdir(self.mounttmp)
176         d1.sort()
177         self.assertEqual(['foo_tag'], d1)
178
179         d2 = os.listdir(os.path.join(self.mounttmp, 'foo_tag'))
180         d2.sort()
181         self.assertEqual(['zzzzz-4zz18-fy296fx3hot09f7'], d2)
182
183         d3 = os.listdir(os.path.join(self.mounttmp, 'foo_tag', 'zzzzz-4zz18-fy296fx3hot09f7'))
184         d3.sort()
185         self.assertEqual(['foo'], d3)
186
187
188 class FuseTagsUpdateTest(MountTestBase):
189     def runRealTest(self):
190         operations = fuse.Operations(os.getuid(), os.getgid())
191         e = operations.inodes.add_entry(fuse.TagsDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, poll_time=1))
192
193         llfuse.init(operations, self.mounttmp, [])
194         t = threading.Thread(None, lambda: llfuse.main())
195         t.start()
196
197         # wait until the driver is finished initializing
198         operations.initlock.wait()
199
200         d1 = os.listdir(self.mounttmp)
201         d1.sort()
202         self.assertEqual(['foo_tag'], d1)
203
204         self.api.links().create(body={'link': {
205             'head_uuid': 'fa7aeb5140e2848d39b416daeef4ffc5+45',
206             'link_class': 'tag',
207             'name': 'bar_tag'
208         }}).execute()
209
210         time.sleep(1)
211
212         d2 = os.listdir(self.mounttmp)
213         d2.sort()
214         self.assertEqual(['bar_tag', 'foo_tag'], d2)
215
216         d3 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
217         d3.sort()
218         self.assertEqual(['fa7aeb5140e2848d39b416daeef4ffc5+45'], d3)
219
220         l = self.api.links().create(body={'link': {
221             'head_uuid': 'ea10d51bcf88862dbcc36eb292017dfd+45',
222             'link_class': 'tag',
223             'name': 'bar_tag'
224         }}).execute()
225
226         time.sleep(1)
227
228         d4 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
229         d4.sort()
230         self.assertEqual(['ea10d51bcf88862dbcc36eb292017dfd+45', 'fa7aeb5140e2848d39b416daeef4ffc5+45'], d4)
231
232         self.api.links().delete(uuid=l['uuid']).execute()
233
234         time.sleep(1)
235
236         d5 = os.listdir(os.path.join(self.mounttmp, 'bar_tag'))
237         d5.sort()
238         self.assertEqual(['fa7aeb5140e2848d39b416daeef4ffc5+45'], d5)
239
240
241 class FuseSharedTest(MountTestBase):
242     def runTest(self):
243         operations = fuse.Operations(os.getuid(), os.getgid())
244         e = operations.inodes.add_entry(fuse.SharedDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()['uuid']))
245
246         llfuse.init(operations, self.mounttmp, [])
247         t = threading.Thread(None, lambda: llfuse.main())
248         t.start()
249
250         # wait until the driver is finished initializing
251         operations.initlock.wait()
252
253         # shared_dirs is a list of the directories exposed
254         # by fuse.SharedDirectory (i.e. any object visible
255         # to the current user)
256         shared_dirs = os.listdir(self.mounttmp)
257         shared_dirs.sort()
258         self.assertIn('FUSE User', shared_dirs)
259
260         # fuse_user_objs is a list of the objects owned by the FUSE
261         # test user (which present as files in the 'FUSE User'
262         # directory)
263         fuse_user_objs = os.listdir(os.path.join(self.mounttmp, 'FUSE User'))
264         fuse_user_objs.sort()
265         self.assertEqual(['Empty collection.link',                # permission link on collection
266                           'FUSE Test Project',                    # project owned by user
267                           'collection #1 owned by FUSE',          # collection owned by user
268                           'collection #2 owned by FUSE',          # collection owned by user
269                           'pipeline instance owned by FUSE.pipelineInstance',  # pipeline instance owned by user
270                       ], fuse_user_objs)
271
272         # test_proj_files is a list of the files in the FUSE Test Project.
273         test_proj_files = os.listdir(os.path.join(self.mounttmp, 'FUSE User', 'FUSE Test Project'))
274         test_proj_files.sort()
275         self.assertEqual(['collection in FUSE project',
276                           'pipeline instance in FUSE project.pipelineInstance',
277                           'pipeline template in FUSE project.pipelineTemplate'
278                       ], test_proj_files)
279
280         # Double check that we can open and read objects in this folder as a file,
281         # and that its contents are what we expect.
282         with open(os.path.join(
283                 self.mounttmp,
284                 'FUSE User',
285                 'FUSE Test Project',
286                 'pipeline template in FUSE project.pipelineTemplate')) as f:
287             j = json.load(f)
288             self.assertEqual("pipeline template in FUSE project", j['name'])
289
290
291 class FuseHomeTest(MountTestBase):
292     def runTest(self):
293         operations = fuse.Operations(os.getuid(), os.getgid())
294         e = operations.inodes.add_entry(fuse.ProjectDirectory(llfuse.ROOT_INODE, operations.inodes, self.api, 0, self.api.users().current().execute()))
295
296         llfuse.init(operations, self.mounttmp, [])
297         t = threading.Thread(None, lambda: llfuse.main())
298         t.start()
299
300         # wait until the driver is finished initializing
301         operations.initlock.wait()
302
303         d1 = os.listdir(self.mounttmp)
304         d1.sort()
305         self.assertIn('Unrestricted public data', d1)
306
307         d2 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data'))
308         d2.sort()
309         self.assertEqual(['GNU General Public License, version 3'], d2)
310
311         d3 = os.listdir(os.path.join(self.mounttmp, 'Unrestricted public data', 'GNU General Public License, version 3'))
312         d3.sort()
313         self.assertEqual(["GNU_General_Public_License,_version_3.pdf"], d3)