16470: Adds ArvadosModel test exposing a bug with audit logs.
[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         self.operations = fuse.Operations(
61             os.getuid(), os.getgid(),
62             api_client=self.api,
63             enable_write=True)
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]
73
74     def tearDown(self):
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])
79             t0 = time.time()
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
87             if waited > 0.1:
88                 logger.warning("MountTestBase.tearDown(): waited %f s for llfuse thread to end", waited)
89
90         os.rmdir(self.mounttmp)
91         if self.keeptmp:
92             shutil.rmtree(self.keeptmp)
93             os.environ.pop('KEEP_LOCAL_STORE')
94         run_test_server.reset()
95
96     def assertDirContents(self, subdir, expect_content):
97         path = self.mounttmp
98         if subdir:
99             path = os.path.join(path, subdir)
100         self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(str(path))))