Merge branch '8178-keepstore-trash-interface' of git.curoverse.com:arvados into 8178...
[arvados.git] / services / fuse / tests / mount_test_base.py
1 import arvados
2 import arvados.safeapi
3 import arvados_fuse as fuse
4 import llfuse
5 import os
6 import shutil
7 import subprocess
8 import sys
9 import tempfile
10 import threading
11 import time
12 import unittest
13 import logging
14 import multiprocessing
15 import run_test_server
16
17 logger = logging.getLogger('arvados.arv-mount')
18
19 class MountTestBase(unittest.TestCase):
20     def setUp(self, api=None, local_store=True):
21         # The underlying C implementation of open() makes a fstat() syscall
22         # with the GIL still held.  When the GETATTR message comes back to
23         # llfuse (which in these tests is in the same interpreter process) it
24         # can't acquire the GIL, so it can't service the fstat() call, so it
25         # deadlocks.  The workaround is to run some of our test code in a
26         # separate process.  Forturnately the multiprocessing module makes this
27         # relatively easy.
28         self.pool = multiprocessing.Pool(1)
29
30         if local_store:
31             self.keeptmp = tempfile.mkdtemp()
32             os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
33         else:
34             self.keeptmp = None
35         self.mounttmp = tempfile.mkdtemp()
36         run_test_server.run()
37         run_test_server.authorize_with("admin")
38         self.api = api if api else arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
39
40     def make_mount(self, root_class, **root_kwargs):
41         self.operations = fuse.Operations(
42             os.getuid(), os.getgid(),
43             api_client=self.api,
44             enable_write=True)
45         self.operations.inodes.add_entry(root_class(
46             llfuse.ROOT_INODE, self.operations.inodes, self.api, 0, **root_kwargs))
47         llfuse.init(self.operations, self.mounttmp, [])
48         threading.Thread(None, llfuse.main).start()
49         # wait until the driver is finished initializing
50         self.operations.initlock.wait()
51         return self.operations.inodes[llfuse.ROOT_INODE]
52
53     def tearDown(self):
54         self.pool.terminate()
55         self.pool.join()
56         del self.pool
57
58         # llfuse.close is buggy, so use fusermount instead.
59         #llfuse.close(unmount=True)
60
61         count = 0
62         success = 1
63         while (count < 9 and success != 0):
64           success = subprocess.call(["fusermount", "-u", self.mounttmp])
65           time.sleep(0.1)
66           count += 1
67
68         self.operations.destroy()
69
70         os.rmdir(self.mounttmp)
71         if self.keeptmp:
72             shutil.rmtree(self.keeptmp)
73             os.environ.pop('KEEP_LOCAL_STORE')
74         run_test_server.reset()
75
76     def assertDirContents(self, subdir, expect_content):
77         path = self.mounttmp
78         if subdir:
79             path = os.path.join(path, subdir)
80         self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(path)))