Merge branch 'master' into wtsi-hgi-8087-arv-cli-request-body-from-file
[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         self.llfuse_thread = None
40
41     # This is a copy of Mount's method.  TODO: Refactor MountTestBase
42     # to use a Mount instead of copying its code.
43     def _llfuse_main(self):
44         try:
45             llfuse.main()
46         except:
47             llfuse.close(unmount=False)
48             raise
49         llfuse.close()
50
51     def make_mount(self, root_class, **root_kwargs):
52         self.operations = fuse.Operations(
53             os.getuid(), os.getgid(),
54             api_client=self.api,
55             enable_write=True)
56         self.operations.inodes.add_entry(root_class(
57             llfuse.ROOT_INODE, self.operations.inodes, self.api, 0, **root_kwargs))
58         llfuse.init(self.operations, self.mounttmp, [])
59         self.llfuse_thread = threading.Thread(None, lambda: self._llfuse_main())
60         self.llfuse_thread.daemon = True
61         self.llfuse_thread.start()
62         # wait until the driver is finished initializing
63         self.operations.initlock.wait()
64         return self.operations.inodes[llfuse.ROOT_INODE]
65
66     def tearDown(self):
67         self.pool.terminate()
68         self.pool.join()
69         del self.pool
70
71         if self.llfuse_thread:
72             subprocess.call(["fusermount", "-u", "-z", self.mounttmp])
73             self.llfuse_thread.join(timeout=1)
74             if self.llfuse_thread.is_alive():
75                 logger.warning("MountTestBase.tearDown():"
76                                " llfuse thread still alive 1s after umount"
77                                " -- abandoning and exiting anyway")
78
79         os.rmdir(self.mounttmp)
80         if self.keeptmp:
81             shutil.rmtree(self.keeptmp)
82             os.environ.pop('KEEP_LOCAL_STORE')
83         run_test_server.reset()
84
85     def assertDirContents(self, subdir, expect_content):
86         path = self.mounttmp
87         if subdir:
88             path = os.path.join(path, subdir)
89         self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(path)))