]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
vstart_runner.py: add methods for negative testing a cmd 33279/head
authorRishabh Dave <ridave@redhat.com>
Thu, 13 Feb 2020 13:12:02 +0000 (18:42 +0530)
committerRishabh Dave <ridave@redhat.com>
Thu, 19 Mar 2020 20:41:58 +0000 (02:11 +0530)
Methods like run_shell effectively conduct positive test on the given
command. Add methods that runs given command expecting failure and then
verifies return value and error message with given one. Rewrite testcmd,
testcmd_as_user and testcmd_as_root to create these new methods for
negative testing since run_shell, run_as_user and run_as_root is
equivalent of running positive test.

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

index 3511515fda5be233a95a8a6e5426efc43934c67b..0911452e5316d9bb10da7e5df1e0330d03bbe8b7 100644 (file)
@@ -233,15 +233,45 @@ class CephFSMount(object):
                                       stdout=BytesIO(), stderr=BytesIO(),
                                       check_status=check_status, cwd=cwd)
 
-    def testcmd(self, args, wait=True, stdin=None, cwd=None, omit_sudo=True):
-        return self.run_shell(args=args, wait=wait, stdin=stdin, cwd=cwd)
-
-    def testcmd_as_user(self, args, user, wait=True, stdin=None, cwd=None):
-        return self.run_as_user(args=args, user=user, wait=wait, stdin=stdin,
-                                cwd=cwd)
-
-    def testcmd_as_root(self, args, wait=True, stdin=None, cwd=None):
-        return self.run_as_root(args=args, wait=wait, stdin=stdin, cwd=cwd)
+    def _verify(self, proc, retval=None, errmsg=None):
+        if retval:
+            msg = ('expected return value: {}\nreceived return value: '
+                   '{}\n'.format(retval, proc.returncode))
+            assert proc.returncode == retval, msg
+
+        if errmsg:
+            stderr = proc.stderr.getvalue().lower()
+            msg = ('didn\'t find given string in stderr -\nexpected string: '
+                   '{}\nreceived error message: {}\nnote: received error '
+                   'message is converted to lowercase'.format(errmsg, stderr))
+            assert errmsg in stderr, msg
+
+    def negtestcmd(self, args, retval=None, errmsg=None, stdin=None,
+                   cwd=None, wait=True):
+        """
+        Conduct a negative test for the given command.
+
+        retval and errmsg are parameters to confirm the cause of command
+        failure.
+        """
+        proc = self.run_shell(args=args, wait=wait, stdin=stdin, cwd=cwd,
+                              check_status=False)
+        self._verify(proc, retval, errmsg)
+        return proc
+
+    def negtestcmd_as_user(self, args, user, retval=None, errmsg=None,
+                           stdin=None, cwd=None, wait=True):
+        proc = self.run_as_user(args=args, user=user, wait=wait, stdin=stdin,
+                                cwd=cwd, check_status=False)
+        self._verify(proc, retval, errmsg)
+        return proc
+
+    def negtestcmd_as_root(self, args, retval=None, errmsg=None, stdin=None,
+                           cwd=None, wait=True):
+        proc = self.run_as_root(args=args, wait=wait, stdin=stdin, cwd=cwd,
+                                check_status=False)
+        self._verify(proc, retval, errmsg)
+        return proc
 
     def open_no_data(self, basename):
         """