[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v6 4/6] tests/acceptance: Share useful helpers from virtiofs_subm
From: |
Philippe Mathieu-Daudé |
Subject: |
[PATCH v6 4/6] tests/acceptance: Share useful helpers from virtiofs_submounts test |
Date: |
Fri, 5 Nov 2021 15:34:14 +0100 |
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: <20211102084232.2965062-5-f4bug@amsat.org>
---
tests/avocado/avocado_qemu/__init__.py | 57 +++++++++++++++++++++++++
tests/avocado/virtiofs_submounts.py | 59 +-------------------------
2 files changed, 59 insertions(+), 57 deletions(-)
diff --git a/tests/avocado/avocado_qemu/__init__.py
b/tests/avocado/avocado_qemu/__init__.py
index e46b3ecb89e..1efc22dabfa 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 @@
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)
diff --git a/tests/avocado/virtiofs_submounts.py
b/tests/avocado/virtiofs_submounts.py
index 21ad7d792e7..e6dc32ffd4e 100644
--- a/tests/avocado/virtiofs_submounts.py
+++ b/tests/avocado/virtiofs_submounts.py
@@ -6,67 +6,12 @@
from avocado import skipUnless
from avocado_qemu import LinuxTest, BUILD_DIR
+from avocado_qemu import has_cmds
+from avocado_qemu import run_cmd
from avocado_qemu import wait_for_console_pattern
from avocado.utils import ssh
-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 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, '')
-
-
class VirtiofsSubmountsTest(LinuxTest):
"""
:avocado: tags=arch:x86_64
--
2.31.1
- [PATCH v6 0/6] tests/acceptance: Add bFLT loader linux-user test, Philippe Mathieu-Daudé, 2021/11/05
- [PATCH v6 1/6] tests/acceptance: Extract QemuBaseTest from Test, Philippe Mathieu-Daudé, 2021/11/05
- [PATCH v6 2/6] tests/acceptance: Make pick_default_qemu_bin() more generic, Philippe Mathieu-Daudé, 2021/11/05
- [PATCH v6 3/6] tests/acceptance: Introduce QemuUserTest base class, Philippe Mathieu-Daudé, 2021/11/05
- [PATCH v6 4/6] tests/acceptance: Share useful helpers from virtiofs_submounts test,
Philippe Mathieu-Daudé <=
- [PATCH v6 5/6] tests/acceptance: Add bFLT loader linux-user test, Philippe Mathieu-Daudé, 2021/11/05
- [PATCH v6 6/6] tests/acceptance: Rename avocado_qemu.Test -> QemuSystemTest, Philippe Mathieu-Daudé, 2021/11/05
- Re: [PATCH v6 0/6] tests/acceptance: Add bFLT loader linux-user test, Philippe Mathieu-Daudé, 2021/11/08