1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 from __future__ import absolute_import
7 import arvados_fuse as fuse
11 import multiprocessing
13 from . import run_test_server
23 logger = logging.getLogger('arvados.arv-mount')
25 from .integration_test import workerPool
27 class MountTestBase(unittest.TestCase):
28 def setUp(self, api=None, local_store=True):
29 # The underlying C implementation of open() makes a fstat() syscall
30 # with the GIL still held. When the GETATTR message comes back to
31 # llfuse (which in these tests is in the same interpreter process) it
32 # can't acquire the GIL, so it can't service the fstat() call, so it
33 # deadlocks. The workaround is to run some of our test code in a
34 # separate process. Forturnately the multiprocessing module makes this
37 self.pool = workerPool()
39 self.keeptmp = tempfile.mkdtemp()
40 os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
43 self.mounttmp = tempfile.mkdtemp()
45 run_test_server.authorize_with("admin")
46 self.api = api if api else arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
47 self.llfuse_thread = None
49 # This is a copy of Mount's method. TODO: Refactor MountTestBase
50 # to use a Mount instead of copying its code.
51 def _llfuse_main(self):
55 llfuse.close(unmount=False)
59 def make_mount(self, root_class, **root_kwargs):
60 self.operations = fuse.Operations(
61 os.getuid(), os.getgid(),
64 self.operations.inodes.add_entry(root_class(
65 llfuse.ROOT_INODE, self.operations.inodes, self.api, 0, **root_kwargs))
66 llfuse.init(self.operations, self.mounttmp, [])
67 self.llfuse_thread = threading.Thread(None, lambda: self._llfuse_main())
68 self.llfuse_thread.daemon = True
69 self.llfuse_thread.start()
70 # wait until the driver is finished initializing
71 self.operations.initlock.wait()
72 return self.operations.inodes[llfuse.ROOT_INODE]
75 if self.llfuse_thread:
76 if self.operations.events:
77 self.operations.events.close(timeout=10)
78 subprocess.call(["fusermount", "-u", "-z", self.mounttmp])
80 self.llfuse_thread.join(timeout=10)
81 if self.llfuse_thread.is_alive():
82 logger.warning("MountTestBase.tearDown():"
83 " llfuse thread still alive 10s after umount"
84 " -- exiting with SIGKILL")
85 os.kill(os.getpid(), signal.SIGKILL)
86 waited = time.time() - t0
88 logger.warning("MountTestBase.tearDown(): waited %f s for llfuse thread to end", waited)
90 os.rmdir(self.mounttmp)
92 shutil.rmtree(self.keeptmp)
93 os.environ.pop('KEEP_LOCAL_STORE')
94 run_test_server.reset()
96 def assertDirContents(self, subdir, expect_content):
99 path = os.path.join(path, subdir)
100 self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(str(path))))