Merge branch '8784-dir-listings'
[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 import arvados
6 import arvados_fuse as fuse
7 import arvados.safeapi
8 import llfuse
9 import logging
10 import multiprocessing
11 import os
12 import run_test_server
13 import shutil
14 import signal
15 import subprocess
16 import sys
17 import tempfile
18 import threading
19 import time
20 import unittest
21
22 logger = logging.getLogger('arvados.arv-mount')
23
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
32         # relatively easy.
33         self.pool = multiprocessing.Pool(1)
34
35         if local_store:
36             self.keeptmp = tempfile.mkdtemp()
37             os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
38         else:
39             self.keeptmp = None
40         self.mounttmp = tempfile.mkdtemp()
41         run_test_server.run()
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
45
46     # This is a copy of Mount's method.  TODO: Refactor MountTestBase
47     # to use a Mount instead of copying its code.
48     def _llfuse_main(self):
49         try:
50             llfuse.main()
51         except:
52             llfuse.close(unmount=False)
53             raise
54         llfuse.close()
55
56     def make_mount(self, root_class, **root_kwargs):
57         self.operations = fuse.Operations(
58             os.getuid(), os.getgid(),
59             api_client=self.api,
60             enable_write=True)
61         self.operations.inodes.add_entry(root_class(
62             llfuse.ROOT_INODE, self.operations.inodes, self.api, 0, **root_kwargs))
63         llfuse.init(self.operations, self.mounttmp, [])
64         self.llfuse_thread = threading.Thread(None, lambda: self._llfuse_main())
65         self.llfuse_thread.daemon = True
66         self.llfuse_thread.start()
67         # wait until the driver is finished initializing
68         self.operations.initlock.wait()
69         return self.operations.inodes[llfuse.ROOT_INODE]
70
71     def tearDown(self):
72         if self.llfuse_thread:
73             if self.operations.events:
74                 self.operations.events.close(timeout=10)
75             subprocess.call(["fusermount", "-u", "-z", self.mounttmp])
76             t0 = time.time()
77             self.llfuse_thread.join(timeout=10)
78             if self.llfuse_thread.is_alive():
79                 logger.warning("MountTestBase.tearDown():"
80                                " llfuse thread still alive 10s after umount"
81                                " -- exiting with SIGKILL")
82                 os.kill(os.getpid(), signal.SIGKILL)
83             waited = time.time() - t0
84             if waited > 0.1:
85                 logger.warning("MountTestBase.tearDown(): waited %f s for llfuse thread to end", waited)
86
87         os.rmdir(self.mounttmp)
88         if self.keeptmp:
89             shutil.rmtree(self.keeptmp)
90             os.environ.pop('KEEP_LOCAL_STORE')
91         run_test_server.reset()
92         self.pool.close()
93         self.pool.join()
94
95     def assertDirContents(self, subdir, expect_content):
96         path = self.mounttmp
97         if subdir:
98             path = os.path.join(path, subdir)
99         self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(path)))