From ec1c2af43f75f6c84efec35264da76fa0889bd43 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Thu, 13 Feb 2020 18:42:02 +0530 Subject: [PATCH] vstart_runner.py: add methods for negative testing a cmd 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 --- qa/tasks/cephfs/mount.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/qa/tasks/cephfs/mount.py b/qa/tasks/cephfs/mount.py index 03fb38cf60e..07dd82f9aed 100644 --- a/qa/tasks/cephfs/mount.py +++ b/qa/tasks/cephfs/mount.py @@ -238,6 +238,46 @@ class CephFSMount(object): stdout=BytesIO(), stderr=BytesIO(), check_status=check_status, 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): """ A pure metadata operation -- 2.39.5