1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 import arvados_fuse as fuse
10 import multiprocessing
12 import run_test_server
22 logger = logging.getLogger('arvados.arv-mount')
24 class MountTestBase(unittest.TestCase):
25 def setUp(self, api=None, local_store=True):
26 # The underlying C implementation of open() makes a fstat() syscall
27 # with the GIL still held. When the GETATTR message comes back to
28 # llfuse (which in these tests is in the same interpreter process) it
29 # can't acquire the GIL, so it can't service the fstat() call, so it
30 # deadlocks. The workaround is to run some of our test code in a
31 # separate process. Forturnately the multiprocessing module makes this
33 self.pool = multiprocessing.Pool(1)
36 self.keeptmp = tempfile.mkdtemp()
37 os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
40 self.mounttmp = tempfile.mkdtemp()
42 run_test_server.authorize_with("admin")
43 self.api = api if api else arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
44 self.llfuse_thread = None
46 # Workaround for llfuse deadlock bug. See #10805, #8345,
47 # https://bitbucket.org/nikratio/python-llfuse/issues/108
48 llfuse.close = lambda *args: None
50 # This is a copy of Mount's method. TODO: Refactor MountTestBase
51 # to use a Mount instead of copying its code.
52 def _llfuse_main(self):
56 llfuse.close(unmount=False)
60 def make_mount(self, root_class, **root_kwargs):
61 self.operations = fuse.Operations(
62 os.getuid(), os.getgid(),
65 self.operations.inodes.add_entry(root_class(
66 llfuse.ROOT_INODE, self.operations.inodes, self.api, 0, **root_kwargs))
67 llfuse.init(self.operations, self.mounttmp, [])
68 self.llfuse_thread = threading.Thread(None, lambda: self._llfuse_main())
69 self.llfuse_thread.daemon = True
70 self.llfuse_thread.start()
71 # wait until the driver is finished initializing
72 self.operations.initlock.wait()
73 return self.operations.inodes[llfuse.ROOT_INODE]
76 if self.llfuse_thread:
77 if self.operations.events:
78 self.operations.events.close(timeout=10)
79 subprocess.call(["fusermount", "-u", "-z", self.mounttmp])
81 self.llfuse_thread.join(timeout=10)
82 if self.llfuse_thread.is_alive():
83 logger.warning("MountTestBase.tearDown():"
84 " llfuse thread still alive 10s after umount"
85 " -- exiting with SIGKILL")
86 os.kill(os.getpid(), signal.SIGKILL)
87 waited = time.time() - t0
89 logger.warning("MountTestBase.tearDown(): waited %f s for llfuse thread to end", waited)
91 os.rmdir(self.mounttmp)
93 shutil.rmtree(self.keeptmp)
94 os.environ.pop('KEEP_LOCAL_STORE')
95 run_test_server.reset()
99 def assertDirContents(self, subdir, expect_content):
102 path = os.path.join(path, subdir)
103 self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(path)))