Merge branch '21541-arv-mount-keyerror-rebase' refs #21541
[arvados.git] / services / fuse / tests / test_command_args.py
index 581f2462468c4197bec042826a58995826b07870..b08ab19335758be4c7ee4f72b91b4f3e26d04cea 100644 (file)
@@ -4,6 +4,7 @@
 
 from __future__ import absolute_import
 from __future__ import print_function
+from six import assertRegex
 import arvados
 import arvados_fuse
 import arvados_fuse.command
@@ -19,6 +20,7 @@ from . import run_test_server
 import sys
 import tempfile
 import unittest
+import resource
 
 def noexit(func):
     """If argparse or arvados_fuse tries to exit, fail the test instead"""
@@ -82,13 +84,13 @@ class MountArgsTest(unittest.TestCase):
 
         e = self.check_ent_type(arvados_fuse.StringFile, 'README')
         readme = e.readfrom(0, -1).decode()
-        self.assertRegexpMatches(readme, r'active-user@arvados\.local')
-        self.assertRegexpMatches(readme, r'\n$')
+        assertRegex(self, readme, r'active-user@arvados\.local')
+        assertRegex(self, readme, r'\n$')
 
         e = self.check_ent_type(arvados_fuse.StringFile, 'by_id', 'README')
         txt = e.readfrom(0, -1).decode()
-        self.assertRegexpMatches(txt, r'portable data hash')
-        self.assertRegexpMatches(txt, r'\n$')
+        assertRegex(self, txt, r'portable data hash')
+        assertRegex(self, txt, r'\n$')
 
     @noexit
     def test_by_id(self):
@@ -188,8 +190,8 @@ class MountArgsTest(unittest.TestCase):
 
     def test_version_argument(self):
         # The argparse version action prints to stderr in Python 2 and stdout
-        # in Python 3.4 and up. Write both to the same stream so the test can pass 
-        # in both cases. 
+        # in Python 3.4 and up. Write both to the same stream so the test can pass
+        # in both cases.
         origerr = sys.stderr
         origout = sys.stdout
         sys.stderr = io.StringIO()
@@ -197,7 +199,7 @@ class MountArgsTest(unittest.TestCase):
 
         with self.assertRaises(SystemExit):
             args = arvados_fuse.command.ArgumentParser().parse_args(['--version'])
-        self.assertRegexpMatches(sys.stdout.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+")
+        assertRegex(self, sys.stdout.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+")
         sys.stderr.close()
         sys.stderr = origerr
         sys.stdout = origout
@@ -260,6 +262,50 @@ class MountArgsTest(unittest.TestCase):
                         '--foreground', self.mntdir])
                     arvados_fuse.command.Mount(args)
 
+    @noexit
+    @mock.patch('resource.setrlimit')
+    @mock.patch('resource.getrlimit')
+    def test_default_file_cache(self, getrlimit, setrlimit):
+        args = arvados_fuse.command.ArgumentParser().parse_args([
+            '--foreground', self.mntdir])
+        self.assertEqual(args.mode, None)
+        getrlimit.return_value = (1024, 1048576)
+        self.mnt = arvados_fuse.command.Mount(args)
+        setrlimit.assert_called_with(resource.RLIMIT_NOFILE, (10240, 1048576))
+
+    @noexit
+    @mock.patch('resource.setrlimit')
+    @mock.patch('resource.getrlimit')
+    def test_small_file_cache(self, getrlimit, setrlimit):
+        args = arvados_fuse.command.ArgumentParser().parse_args([
+            '--foreground', '--file-cache=256000000', self.mntdir])
+        self.assertEqual(args.mode, None)
+        getrlimit.return_value = (1024, 1048576)
+        self.mnt = arvados_fuse.command.Mount(args)
+        setrlimit.assert_not_called()
+
+    @noexit
+    @mock.patch('resource.setrlimit')
+    @mock.patch('resource.getrlimit')
+    def test_large_file_cache(self, getrlimit, setrlimit):
+        args = arvados_fuse.command.ArgumentParser().parse_args([
+            '--foreground', '--file-cache=256000000000', self.mntdir])
+        self.assertEqual(args.mode, None)
+        getrlimit.return_value = (1024, 1048576)
+        self.mnt = arvados_fuse.command.Mount(args)
+        setrlimit.assert_called_with(resource.RLIMIT_NOFILE, (30517, 1048576))
+
+    @noexit
+    @mock.patch('resource.setrlimit')
+    @mock.patch('resource.getrlimit')
+    def test_file_cache_hard_limit(self, getrlimit, setrlimit):
+        args = arvados_fuse.command.ArgumentParser().parse_args([
+            '--foreground', '--file-cache=256000000000', self.mntdir])
+        self.assertEqual(args.mode, None)
+        getrlimit.return_value = (1024, 2048)
+        self.mnt = arvados_fuse.command.Mount(args)
+        setrlimit.assert_called_with(resource.RLIMIT_NOFILE, (2048, 2048))
+
 class MountErrorTest(unittest.TestCase):
     def setUp(self):
         self.mntdir = tempfile.mkdtemp()
@@ -291,7 +337,7 @@ class MountErrorTest(unittest.TestCase):
 
     def test_bogus_host(self):
         arvados.config._settings["ARVADOS_API_HOST"] = "100::"
-        with self.assertRaises(SystemExit) as ex:
+        with self.assertRaises(SystemExit) as ex, mock.patch('time.sleep'):
             args = arvados_fuse.command.ArgumentParser().parse_args([self.mntdir])
             arvados_fuse.command.Mount(args, logger=self.logger).run()
         self.assertEqual(1, ex.exception.code)