]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
qa/cephfs: refactor KernelMount.mount method
authorRishabh Dave <ridave@redhat.com>
Wed, 14 Oct 2020 11:21:36 +0000 (16:51 +0530)
committerRishabh Dave <ridave@redhat.com>
Fri, 5 Mar 2021 03:23:26 +0000 (08:53 +0530)
Make KernelMount.mount() such that it can LocalKernelMount can reuse it
instead of duplicating it. For this, move subtasks required for mounting
Ceph FS (like creating mountpoint directory) to a different method, so
that these methods can be overriden in LocalKernelMount as per
requirement.

Signed-off-by: Rishabh Dave <ridave@redhat.com>
qa/tasks/cephfs/kernel_mount.py

index fc6e97dc50a6948aa64f4215b996788ad9a76af0..c4cd1591fb5b3c69ab6b45a0af6882ceb0983ee2 100644 (file)
@@ -29,6 +29,8 @@ class KernelMount(CephFSMount):
         self.rbytes = config.get('rbytes', False)
         self.inst = None
         self.addr = None
+        self._mount_bin = ['adjust-ulimits', 'ceph-coverage', self.test_dir +\
+                           '/archive/coverage', '/bin/mount', '-t', 'ceph']
 
     def mount(self, mntopts=[], check_status=True, **kwargs):
         self.update_attrs(**kwargs)
@@ -39,33 +41,43 @@ class KernelMount(CephFSMount):
         if not self.cephfs_mntpt:
             self.cephfs_mntpt = '/'
 
-        stderr = StringIO()
-        try:
-            self.client_remote.run(args=['mkdir', '-p', self.hostfs_mntpt],
-                                   timeout=(5*60), stderr=stderr)
-        except CommandFailedError:
-            if 'file exists' not in stderr.getvalue().lower():
-                raise
+        self._create_mntpt()
 
         retval = self._run_mount_cmd(mntopts, check_status)
         if retval:
             return retval
 
+        self._set_filemode_on_mntpt()
+
+        self.mounted = True
+
+    def _create_mntpt(self):
         stderr = StringIO()
         try:
-            self.client_remote.run(
-                args=['sudo', 'chmod', '1777', self.hostfs_mntpt],
-                stderr=stderr, timeout=(5*60))
+            self.client_remote.run(args=['mkdir', '-p', self.hostfs_mntpt],
+                                   timeout=(5*60), stderr=stderr)
         except CommandFailedError:
-            # the client does not have write permissions in the caps it holds
-            # for the Ceph FS that was just mounted.
-            if 'permission denied' in stderr.getvalue().lower():
-                pass
+            if 'file exists' not in stderr.getvalue().lower():
+                raise
 
+    def _run_mount_cmd(self, mntopts, check_status):
+        mount_cmd = self._get_mount_cmd(mntopts)
+        mountcmd_stdout, mountcmd_stderr = StringIO(), StringIO()
 
-        self.mounted = True
+        try:
+            self.client_remote.run(args=mount_cmd, timeout=(30*60),
+                                   stdout=mountcmd_stdout,
+                                   stderr=mountcmd_stderr, omit_sudo=False)
+        except CommandFailedError as e:
+            log.info('mount command failed')
+            if check_status:
+                raise
+            else:
+                return (e, mountcmd_stdout.getvalue(),
+                        mountcmd_stderr.getvalue())
+        log.info('mount command passed')
 
-    def _run_mount_cmd(self, mntopts, check_status):
+    def _get_mount_cmd(self, mntopts):
         opts = 'norequire_active_mds'
         if self.client_id:
             opts += ',name=' + self.client_id
@@ -82,27 +94,28 @@ class KernelMount(CephFSMount):
         if mntopts:
             opts += ',' + ','.join(mntopts)
 
+        mount_cmd = ['sudo'] + self._nsenter_args
         mount_dev = ':' + self.cephfs_mntpt
-        prefix = ['sudo', 'adjust-ulimits', 'ceph-coverage',
-                  self.test_dir + '/archive/coverage',
-                  'nsenter',
-                  '--net=/var/run/netns/{0}'.format(self.netns_name)]
-        cmdargs = prefix + ['/bin/mount', '-t', 'ceph', mount_dev,
-                            self.hostfs_mntpt, '-v', '-o', opts]
+        mount_cmd += self._mount_bin + [mount_dev, self.hostfs_mntpt, '-v',
+                                        '-o', opts]
 
-        mountcmd_stdout, mountcmd_stderr = StringIO(), StringIO()
+        return mount_cmd
+
+    @property
+    def _nsenter_args(self):
+        return ['nsenter', f'--net=/var/run/netns/{self.netns_name}']
+
+    def _set_filemode_on_mntpt(self):
+        stderr = StringIO()
         try:
-            self.client_remote.run(args=cmdargs, timeout=(30*60),
-                                   stdout=mountcmd_stdout,
-                                   stderr=mountcmd_stderr)
-        except CommandFailedError as e:
-            log.info('mount command failed')
-            if check_status:
-                raise
-            else:
-                return (e, mountcmd_stdout.getvalue(),
-                        mountcmd_stderr.getvalue())
-        log.info('mount command passed')
+            self.client_remote.run(
+                args=['sudo', 'chmod', '1777', self.hostfs_mntpt],
+                stderr=stderr, timeout=(5*60))
+        except CommandFailedError:
+            # the client does not have write permissions in the caps it holds
+            # for the Ceph FS that was just mounted.
+            if 'permission denied' in stderr.getvalue().lower():
+                pass
 
     def umount(self, force=False):
         if not self.is_mounted():