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):
61 if 'enable_write' in root_kwargs:
62 enable_write = root_kwargs.pop('enable_write')
63 self.operations = fuse.Operations(
64 os.getuid(), os.getgid(),
66 enable_write=enable_write)
67 self.operations.inodes.add_entry(root_class(
68 llfuse.ROOT_INODE, self.operations.inodes, self.api, 0, enable_write, **root_kwargs))
69 llfuse.init(self.operations, self.mounttmp, [])
70 self.llfuse_thread = threading.Thread(None, lambda: self._llfuse_main())
71 self.llfuse_thread.daemon = True
72 self.llfuse_thread.start()
73 # wait until the driver is finished initializing
74 self.operations.initlock.wait()
75 return self.operations.inodes[llfuse.ROOT_INODE]
78 if self.llfuse_thread:
79 if self.operations.events:
80 self.operations.events.close(timeout=10)
81 subprocess.call(["fusermount", "-u", "-z", self.mounttmp])
83 self.llfuse_thread.join(timeout=10)
84 if self.llfuse_thread.is_alive():
85 logger.warning("MountTestBase.tearDown():"
86 " llfuse thread still alive 10s after umount"
87 " -- exiting with SIGKILL")
88 os.kill(os.getpid(), signal.SIGKILL)
89 waited = time.time() - t0
91 logger.warning("MountTestBase.tearDown(): waited %f s for llfuse thread to end", waited)
93 os.rmdir(self.mounttmp)
95 shutil.rmtree(self.keeptmp)
96 os.environ.pop('KEEP_LOCAL_STORE')
97 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(str(path))))