Merge branch 'master' into 3889-functional-testing
[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         d1 = os.listdir(self.mounttmp)
254         d1.sort()
255         self.assertIn('Active User', d1)
256
257         d2 = os.listdir(os.path.join(self.mounttmp, 'Active User'))
258         d2.sort()
259         self.assertEqual(['A Project',
260                           "Empty collection",
261                           "Empty collection.link",
262                           "Pipeline Template with Input Parameter with Search.pipelineTemplate",
263                           "Pipeline Template with Jobspec Components.pipelineTemplate",
264                           "collection_expires_in_future",
265                           "collection_with_same_name_in_aproject_and_home_project",
266                           "pipeline_to_merge_params.pipelineInstance",
267                           "pipeline_with_job.pipelineInstance",
268                           "pipeline_with_tagged_collection_input.pipelineInstance"
269                       ], d2)
270
271         d3 = os.listdir(os.path.join(self.mounttmp, 'Active User', 'A Project'))
272         d3.sort()
273         self.assertEqual(["A Subproject",
274                           "Two Part Pipeline Template.pipelineTemplate",
275                           "collection_to_move_around",
276                           "collection_with_same_name_in_aproject_and_home_project",
277                           "zzzzz-4zz18-fy296fx3hot09f7 added sometime"
278                       ], d3)
279
280         with open(os.path.join(self.mounttmp, 'Active User', "A Project", "Two Part Pipeline Template.pipelineTemplate")) as f:
281             j = json.load(f)
282             self.assertEqual("Two Part Pipeline Template", 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)