6219: first draft at fuse performance testing.
[arvados.git] / services / fuse / tests / mount_test_base.py
1 import arvados
2 import arvados.safeapi
3 import arvados_fuse as fuse
4 import glob
5 import json
6 import llfuse
7 import os
8 import shutil
9 import subprocess
10 import sys
11 import tempfile
12 import threading
13 import time
14 import unittest
15 import logging
16 import multiprocessing
17 import run_test_server
18
19 logger = logging.getLogger('arvados.arv-mount')
20
21 class MountTestBase(unittest.TestCase):
22     def setUp(self):
23         # The underlying C implementation of open() makes a fstat() syscall
24         # with the GIL still held.  When the GETATTR message comes back to
25         # llfuse (which in these tests is in the same interpreter process) it
26         # can't acquire the GIL, so it can't service the fstat() call, so it
27         # deadlocks.  The workaround is to run some of our test code in a
28         # separate process.  Forturnately the multiprocessing module makes this
29         # relatively easy.
30         self.pool = multiprocessing.Pool(1)
31
32         self.keeptmp = tempfile.mkdtemp()
33         os.environ['KEEP_LOCAL_STORE'] = self.keeptmp
34         self.mounttmp = tempfile.mkdtemp()
35         run_test_server.run()
36         run_test_server.authorize_with("admin")
37         self.api = arvados.safeapi.ThreadSafeApiCache(arvados.config.settings())
38
39     def make_mount(self, root_class, **root_kwargs):
40         self.operations = fuse.Operations(os.getuid(), os.getgid(), enable_write=True)
41         self.operations.inodes.add_entry(root_class(
42             llfuse.ROOT_INODE, self.operations.inodes, self.api, 0, **root_kwargs))
43         llfuse.init(self.operations, self.mounttmp, [])
44         threading.Thread(None, llfuse.main).start()
45         # wait until the driver is finished initializing
46         self.operations.initlock.wait()
47         return self.operations.inodes[llfuse.ROOT_INODE]
48
49     def tearDown(self):
50         self.pool.terminate()
51         self.pool.join()
52         del self.pool
53
54         # llfuse.close is buggy, so use fusermount instead.
55         #llfuse.close(unmount=True)
56
57         count = 0
58         success = 1
59         while (count < 9 and success != 0):
60           success = subprocess.call(["fusermount", "-u", self.mounttmp])
61           time.sleep(0.1)
62           count += 1
63
64         self.operations.destroy()
65
66         os.rmdir(self.mounttmp)
67         shutil.rmtree(self.keeptmp)
68         run_test_server.reset()
69
70     def assertDirContents(self, subdir, expect_content):
71         path = self.mounttmp
72         if subdir:
73             path = os.path.join(path, subdir)
74         self.assertEqual(sorted(expect_content), sorted(llfuse.listdir(path)))