qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Wainer dos Santos Moschetta <wainersm@redhat.com>
To: "Philippe Mathieu-Daudé" <f4bug@amsat.org>, qemu-devel@nongnu.org
Cc: "Willian Rampazzo" <willianr@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>,
	"Cleber Rosa" <crosa@redhat.com>
Subject: Re: [PATCH v3 4/5] tests/acceptance: Share useful helpers from virtiofs_submounts test
Date: Wed, 17 Mar 2021 11:52:57 -0300	[thread overview]
Message-ID: <cdb9df6d-ab8a-c7c3-bfa8-92dca87e6d6b@redhat.com> (raw)
In-Reply-To: <20210315230838.2973103-5-f4bug@amsat.org>


On 3/15/21 8:08 PM, Philippe Mathieu-Daudé wrote:
> Move the useful has_cmd()/has_cmds() helpers from the virtiofs
> test to the avocado_qemu public class.
>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>   tests/acceptance/avocado_qemu/__init__.py | 57 ++++++++++++++++++++++
>   tests/acceptance/virtiofs_submounts.py    | 59 +----------------------
>   2 files changed, 59 insertions(+), 57 deletions(-)

Reviewed-by: Wainer dos Santos Moschetta <wainersm@redhat.com>


>
> diff --git a/tests/acceptance/avocado_qemu/__init__.py b/tests/acceptance/avocado_qemu/__init__.py
> index b471bee66e0..48c705fe3d2 100644
> --- a/tests/acceptance/avocado_qemu/__init__.py
> +++ b/tests/acceptance/avocado_qemu/__init__.py
> @@ -11,6 +11,7 @@
>   import logging
>   import os
>   import shutil
> +import subprocess
>   import sys
>   import uuid
>   import tempfile
> @@ -45,6 +46,62 @@
>   from qemu.accel import tcg_available
>   from qemu.machine import QEMUMachine
>   
> +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/acceptance/virtiofs_submounts.py b/tests/acceptance/virtiofs_submounts.py
> index 46fa65392a1..201645cd740 100644
> --- a/tests/acceptance/virtiofs_submounts.py
> +++ b/tests/acceptance/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



  reply	other threads:[~2021-03-17 15:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 23:08 [PATCH v3 0/5] tests/acceptance: Add bFLT loader linux-user test Philippe Mathieu-Daudé
2021-03-15 23:08 ` [PATCH v3 1/5] tests/acceptance: Extract QemuBaseTest from Test Philippe Mathieu-Daudé
2021-03-17 13:08   ` Wainer dos Santos Moschetta
2021-03-17 13:26     ` Philippe Mathieu-Daudé
2021-03-17 13:44       ` Wainer dos Santos Moschetta
2021-03-17 14:18         ` Philippe Mathieu-Daudé
2021-03-15 23:08 ` [PATCH v3 2/5] tests/acceptance: Make pick_default_qemu_bin() more generic Philippe Mathieu-Daudé
2021-03-17 13:32   ` Wainer dos Santos Moschetta
2021-03-15 23:08 ` [PATCH v3 3/5] tests/acceptance: Introduce QemuUserTest base class Philippe Mathieu-Daudé
2021-03-17 14:14   ` Wainer dos Santos Moschetta
2021-03-15 23:08 ` [PATCH v3 4/5] tests/acceptance: Share useful helpers from virtiofs_submounts test Philippe Mathieu-Daudé
2021-03-17 14:52   ` Wainer dos Santos Moschetta [this message]
2021-03-15 23:08 ` [PATCH v3 5/5] tests/acceptance: Add bFLT loader linux-user test Philippe Mathieu-Daudé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cdb9df6d-ab8a-c7c3-bfa8-92dca87e6d6b@redhat.com \
    --to=wainersm@redhat.com \
    --cc=crosa@redhat.com \
    --cc=f4bug@amsat.org \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=willianr@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).