17417: Merge branch 'main' into 17417-add-arm64
[arvados.git] / services / fuse / tests / mount_test_base.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 from __future__ import absolute_import
6 import arvados
7 import arvados_fuse as fuse
8 import arvados.safeapi
9 import llfuse
10 import logging
11 import multiprocessing
12 import os
13 from . import run_test_server
14 import shutil
15 import signal
16 import subprocess
17 import sys
18 import tempfile
19 import threading
20 import time
21 import unittest
22
23 logger = logging.getLogger('arvados.arv-mount')
24
25 from .integration_test import workerPool
26
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
35         # relatively easy.
36
37         self.pool = workerPool()
38         if local_store:
39             self.keeptmp = tempfile.mkdtemp()
40             os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
41         else:
42             self.keeptmp = None
43         self.mounttmp = tempfile.mkdtemp()
44         run_test_server.run()
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
48
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):
52         try:
53             llfuse.main()
54         except:
55             llfuse.close(unmount=False)
56             raise
57         llfuse.close()
58
59     def make_mount(self, root_class, **root_kwargs):
60         enable_write = True
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(),
65             api_client=self.api,
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]
76
77     def tearDown(self):
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])
82             t0 = time.time()
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
90             if waited > 0.1:
91                 logger.warning("MountTestBase.tearDown(): waited %f s for llfuse thread to end", waited)
92
93         os.rmdir(self.mounttmp)
94         if self.keeptmp:
95             shutil.rmtree(self.keeptmp)
96             os.environ.pop('KEEP_LOCAL_STORE')
97         run_test_server.reset()
98
99     def assertDirContents(self, subdir, expect_content):
100         path = self.mounttmp
101         if subdir:
102             path = os.path.join(path, subdir)
103         self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(str(path))))