qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>, qemu-devel@nongnu.org
Cc: "Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Alex Bennée" <alex.bennee@linaro.org>
Subject: Re: [PATCH 06/22] tests/functional: introduce some helpful decorators
Date: Mon, 2 Dec 2024 09:27:34 +0100	[thread overview]
Message-ID: <3cc78a57-98c0-4dec-89f3-3b277603ae78@redhat.com> (raw)
In-Reply-To: <20241129173120.761728-7-berrange@redhat.com>

On 29/11/2024 18.31, Daniel P. Berrangé wrote:
> Reduce repeated boilerplate with some helper decorators:
> 
>   @skipIfNotPlatform("x86_64", "aarch64")
> 
>    => Skip unless the build host platform matches
> 
>   @skipIfMissingCommands("mkisofs", "losetup")
> 
>    => Skips unless all listed commands are found in $PATH
> 
>   @skipIfMissingImports("numpy", "cv2")
> 
>    => Skips unless all listed modules can be imported
> 
>   @skipFlakyTest("https://gitlab.com/qemu-project/qemu/-/issues/NNN")
> 
>    => Skips unless env var requests flaky tests with the
>       reason documented in the referenced gitlab bug
> 
>   @skipBigData
> 
>    => Skips unless env var permits tests creating big data files
> 
>   @skipUntrustedTest
> 
>    => Skips unless env var permits tests which are potentially
>       dangerous to the host

That are good ideas! And certainly less error prone than specifying the 
names of the environment variables over and over again.

> diff --git a/tests/functional/qemu_test/__init__.py b/tests/functional/qemu_test/__init__.py
> index 8fddddbe67..7dee3522f2 100644
> --- a/tests/functional/qemu_test/__init__.py
> +++ b/tests/functional/qemu_test/__init__.py
> @@ -13,3 +13,6 @@
>       exec_command, exec_command_and_wait_for_pattern, get_qemu_img, which
>   from .testcase import QemuBaseTest, QemuUserTest, QemuSystemTest
>   from .linuxkernel import LinuxKernelTest
> +from .decorators import skipIfMissingCommands, skipIfNotMachine, \
> +    skipFlakyTest, skipUntrustedTest, skipBigDataTest, \
> +    skipIfMissingImports
> diff --git a/tests/functional/qemu_test/decorators.py b/tests/functional/qemu_test/decorators.py
> new file mode 100644
> index 0000000000..d25fec7b2d
> --- /dev/null
> +++ b/tests/functional/qemu_test/decorators.py
> @@ -0,0 +1,105 @@
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +#
> +# Decorators useful in functional tests
> +
> +import os
> +import platform
> +from unittest import skipUnless
> +
> +from .cmd import which
> +
> +'''
> +Decorator to skip execution of a test if the list
> +of command binaries is not available in $PATH.
> +Example:
> +
> +  @skipIfMissingCommands("mkisofs", "losetup")
> +'''
> +def skipIfMissingCommands(*args):
> +    def has_cmds(cmdlist):
> +        for cmd in cmdlist:
> +            if not which(cmd):
> +                return False
> +        return True
> +
> +    return skipUnless(lambda: has_cmds(args),
> +                      'required commands(s) "%s" not installed' %

s/commands(s)/command(s)/ ?

> +                      ", ".join(args))
> +
> +'''
> +Decorator to skip execution of a test if the current
> +host machine does not match one of the permitted
> +machines.
> +Example
> +
> +  @skipIfNotMachine("x86_64", "aarch64")
> +'''
> +def skipIfNotMachine(*args):
> +    return skipUnless(lambda: platform.machine() in args,
> +                        'not running on required machine(s) "%s"' %

plural sounds strange here (like all machines would be required at the same 
time), I'd maybe say "not running on one of the required machine(s)" ?

> +                        ", ".join(args))
> +
> +'''
> +Decorator to skip execution of flaky tests, unless
> +the $QEMU_TEST_FLAKY_TESTS env var is set. A bug URL

Since it is the "official" documentation of this decorator, I'd maybe rather 
use the full words: "environment variable" instead of "env var"

> +must be provided that documents the observed failure
> +behaviour, so it can be tracked & re-evaluated in future.
> +
> +Historical tests may be providing "None" as the bug_url
> +but this should not be done for new test.
> +
> +Example:
> +
> +  @skipFlakyTest("https://gitlab.com/qemu-project/qemu/-/issues/NNN")
> +'''
> +def skipFlakyTest(bug_url):
> +    if bug_url is None:
> +        bug_url = "FIXME: reproduce flaky test and file bug report or remove"
> +    return skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'),
> +                      f'Test is unstable: {bug_url}')
> +
> +'''
> +Decorator to skip execution of tests which are likely
> +to execute untrusted commands on the host, or commands
> +which process untrusted code, unles the

s/unles/unless/

> +$QEMU_TEST_ALLOW_UNTRUSTED_CODE env var is set.
> +Example:
> +
> +  @skipUntrustedTest()
> +'''
> +def skipUntrustedTest():
> +    return skipUnless(os.getenv('QEMU_TEST_ALLOW_UNTRUSTED_CODE'),
> +                      'Test runs untrusted code / processes untrusted data')
> +
> +'''
> +Decorator to skip execution of tests which need large
> +data storage on the host, unless the
> +$QEMU_TEST_ALLOW_LARGE_STORAGE env var is set

Maybe we should also provide some direction what is meant with large 
storage. I've seen some tests that are skipped since they create a disk file 
with 128 MiB. And others are always executed though they create a disk file 
with 512 MiB or even more. What would be a good recommendation here?
(My gut feeling is maybe ~ 1 GiB? Or better less?)

> +Example:
> +
> +  @skipBigDataTest()
> +'''
> +def skipBigDataTest():
> +    return skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'),
> +                      'Test required large host storage space')

s/required/requires/ ?
(the other decorators use present tense, too)

> +'''
> +Decorator to skip execution of a test if the list
> +of python imports is not available.
> +Example:
> +
> +  @skipIfMissingImports("numpy", "cv2")
> +'''
> +def skipIfMissingImports(*args):
> +    def has_imports(importlist):
> +        for impname in importlist:
> +            try:
> +                import impname
> +            except ImportError:
> +                return False
> +        return True
> +
> +    return skipUnless(lambda: has_imports(args),
> +                      'required imports(s) "%s" not installed' %

s/imports(s)/import(s)/ ?

> +                      ", ".join(args))

  Thomas



  reply	other threads:[~2024-12-02  8:28 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-29 17:30 [PATCH 00/22 for 10.0] tests/functional: various improvements wrt assets/scratch files Daniel P. Berrangé
2024-11-29 17:30 ` [PATCH 01/22] tests/functional: increase timeouts for arm sx1 test Daniel P. Berrangé
2024-11-30  9:55   ` Thomas Huth
2024-11-29 17:31 ` [PATCH 02/22] tests/functional: remove unused system imports Daniel P. Berrangé
2024-11-30  9:59   ` Thomas Huth
2024-12-02  9:22     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 03/22] tests/functional: remove duplicated 'qemu_test' import statements Daniel P. Berrangé
2024-11-30 10:09   ` Thomas Huth
2024-12-02 11:40     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 04/22] tests/functional: remove pointless with statement Daniel P. Berrangé
2024-11-30 10:10   ` Thomas Huth
2024-11-29 17:31 ` [PATCH 05/22] tests/functional: remove duplicated 'which' function impl Daniel P. Berrangé
2024-11-30 10:16   ` Thomas Huth
2024-12-02 11:44     ` Daniel P. Berrangé
2024-12-02 12:45       ` Thomas Huth
2024-11-30 15:08   ` Richard Henderson
2024-12-02 11:45     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 06/22] tests/functional: introduce some helpful decorators Daniel P. Berrangé
2024-12-02  8:27   ` Thomas Huth [this message]
2024-12-02 11:49     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 07/22] tests/functional: switch to new test skip decorators Daniel P. Berrangé
2024-12-02  8:57   ` Thomas Huth
2024-12-02 11:51     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 08/22] tests/functional: add helpers for building file paths Daniel P. Berrangé
2024-12-02  9:19   ` Thomas Huth
2024-12-03 13:53     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 09/22] tests/functional: switch over to using self.log_file(...) Daniel P. Berrangé
2024-12-02  9:22   ` Thomas Huth
2024-11-29 17:31 ` [PATCH 10/22] tests/functional: switch over to using self.build_file(...) Daniel P. Berrangé
2024-12-02  9:26   ` Thomas Huth
2024-12-02 12:00     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 11/22] tests/functional: switch over to using self.data_file(...) Daniel P. Berrangé
2024-12-02  9:32   ` Thomas Huth
2024-12-03  5:39     ` Ani Sinha
2024-12-03  8:11       ` Daniel P. Berrangé
2024-12-03  8:50         ` Thomas Huth
2024-12-03  9:05           ` Ani Sinha
2024-11-29 17:31 ` [PATCH 12/22] tests/functional: switch over to using self.scratch_file() Daniel P. Berrangé
2024-12-02  9:56   ` Thomas Huth
2024-12-02 12:03     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 13/22] tests/functional: switch over to using self.socket_dir(...) Daniel P. Berrangé
2024-12-02  9:59   ` Thomas Huth
2024-11-29 17:31 ` [PATCH 14/22] tests/functional: remove redundant 'rmtree' call Daniel P. Berrangé
2024-11-30 10:32   ` Thomas Huth
2024-11-29 17:31 ` [PATCH 15/22] tests/functional: add common zip_extract helper Daniel P. Berrangé
2024-12-02 10:04   ` Thomas Huth
2024-12-02 12:04     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 16/22] tests/functional: add common deb_extract helper Daniel P. Berrangé
2024-12-02 10:14   ` Thomas Huth
2024-12-02 12:08     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 17/22] tests/functional: generalize archive_extract Daniel P. Berrangé
2024-12-02 10:20   ` Thomas Huth
2024-12-02 12:11     ` Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 18/22] tests/functional: add 'archive_extract' to QemuBaseTest Daniel P. Berrangé
2024-12-02 10:30   ` Thomas Huth
2024-12-02 12:13     ` Daniel P. Berrangé
2024-12-02 12:52       ` Thomas Huth
2024-12-02 13:28         ` Daniel P. Berrangé
2024-12-06 13:10         ` Thomas Huth
2024-11-29 17:31 ` [PATCH 19/22] tests/functional: convert tests to new archive_extract helper Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 20/22] tests/functional: generalize uncompress Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 21/22] tests/functional: add 'uncompress' to QemuBaseTest Daniel P. Berrangé
2024-11-29 17:31 ` [PATCH 22/22] tests/functional: convert tests to new uncompress helper Daniel P. Berrangé

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=3cc78a57-98c0-4dec-89f3-3b277603ae78@redhat.com \
    --to=thuth@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=berrange@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /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).