diff options
author | Philippe Mathieu-Daudé <f4bug@amsat.org> | 2021-03-15 23:55:43 +0100 |
---|---|---|
committer | Philippe Mathieu-Daudé <philmd@redhat.com> | 2021-11-08 17:00:22 +0100 |
commit | 0e4b1c9435528c30718a2722d09ff35beaa300ee (patch) | |
tree | 2a743af24292a65cd30d45cd15689294c7fc5a19 /tests/avocado/avocado_qemu | |
parent | 5334df48224f494303b4717b57b2b0cd42bd1c1a (diff) |
tests/avocado: Share useful helpers from virtiofs_submounts test
Move the useful has_cmd()/has_cmds() helpers from the virtiofs
test to the avocado_qemu public class.
Reviewed-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Reviewed-by: Willian Rampazzo <willianr@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20211105143416.148332-5-f4bug@amsat.org>
Diffstat (limited to 'tests/avocado/avocado_qemu')
-rw-r--r-- | tests/avocado/avocado_qemu/__init__.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/avocado/avocado_qemu/__init__.py b/tests/avocado/avocado_qemu/__init__.py index e46b3ecb89..1efc22dabf 100644 --- a/tests/avocado/avocado_qemu/__init__.py +++ b/tests/avocado/avocado_qemu/__init__.py @@ -11,6 +11,7 @@ import logging import os import shutil +import subprocess import sys import tempfile import time @@ -41,6 +42,62 @@ from qemu.utils import (get_info_usernet_hostfwd_port, kvm_available, tcg_available) +def has_cmd(name, args=None): + """ + This function is for use in a @avocado.skipUnless decorator, e.g.: + + @skipUnless(*has_cmd('sudo -n', ('sudo', '-n', 'true'))) + def test_something_that_needs_sudo(self): + ... + """ + + if args is None: + args = ('which', name) + + try: + _, stderr, exitcode = run_cmd(args) + except Exception as e: + exitcode = -1 + stderr = str(e) + + if exitcode != 0: + cmd_line = ' '.join(args) + err = f'{name} required, but "{cmd_line}" failed: {stderr.strip()}' + return (False, err) + else: + return (True, '') + +def has_cmds(*cmds): + """ + This function is for use in a @avocado.skipUnless decorator and + allows checking for the availability of multiple commands, e.g.: + + @skipUnless(*has_cmds(('cmd1', ('cmd1', '--some-parameter')), + 'cmd2', 'cmd3')) + def test_something_that_needs_cmd1_and_cmd2(self): + ... + """ + + for cmd in cmds: + if isinstance(cmd, str): + cmd = (cmd,) + + ok, errstr = has_cmd(*cmd) + if not ok: + return (False, errstr) + + return (True, '') + +def run_cmd(args): + subp = subprocess.Popen(args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True) + stdout, stderr = subp.communicate() + ret = subp.returncode + + return (stdout, stderr, ret) + def is_readable_executable_file(path): return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK) |