[PATCH v5 18/19] tests/functional: add skipUnlessPasswordlessSudo() decorator

Vladimir Sementsov-Ogievskiy posted 19 patches 1 week, 2 days ago
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Jason Wang <jasowang@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, Markus Armbruster <armbru@redhat.com>, Michael Roth <michael.roth@amd.com>, Eric Blake <eblake@redhat.com>, Thomas Huth <thuth@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
There is a newer version of this series
[PATCH v5 18/19] tests/functional: add skipUnlessPasswordlessSudo() decorator
Posted by Vladimir Sementsov-Ogievskiy 1 week, 2 days ago
To be used in the next commit: that would be a test for TAP
networking, and it will need to setup TAP device.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
---
 tests/functional/qemu_test/decorators.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/tests/functional/qemu_test/decorators.py b/tests/functional/qemu_test/decorators.py
index c0d1567b14..4b332804ef 100644
--- a/tests/functional/qemu_test/decorators.py
+++ b/tests/functional/qemu_test/decorators.py
@@ -6,6 +6,7 @@
 import os
 import platform
 import resource
+import subprocess
 from unittest import skipIf, skipUnless
 
 from .cmd import which
@@ -149,3 +150,18 @@ def skipLockedMemoryTest(locked_memory):
         ulimit_memory == resource.RLIM_INFINITY or ulimit_memory >= locked_memory * 1024,
         f'Test required {locked_memory} kB of available locked memory',
     )
+
+'''
+Decorator to skip execution of a test if passwordless
+sudo command is not available.
+'''
+def skipUnlessPasswordlessSudo():
+    proc = subprocess.run(["sudo", "-n", "/bin/true"],
+                          stdin=subprocess.PIPE,
+                          stdout=subprocess.PIPE,
+                          stderr=subprocess.STDOUT,
+                          universal_newlines=True,
+                          check=False)
+
+    return skipUnless(proc.returncode == 0,
+                      f'requires password-less sudo access: {proc.stdout}')
-- 
2.48.1


Re: [PATCH v5 18/19] tests/functional: add skipUnlessPasswordlessSudo() decorator
Posted by Thomas Huth 1 week, 2 days ago
On 19/09/2025 11.55, Vladimir Sementsov-Ogievskiy wrote:
> To be used in the next commit: that would be a test for TAP
> networking, and it will need to setup TAP device.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   tests/functional/qemu_test/decorators.py | 16 ++++++++++++++++
>   1 file changed, 16 insertions(+)
> 
> diff --git a/tests/functional/qemu_test/decorators.py b/tests/functional/qemu_test/decorators.py
> index c0d1567b14..4b332804ef 100644
> --- a/tests/functional/qemu_test/decorators.py
> +++ b/tests/functional/qemu_test/decorators.py
> @@ -6,6 +6,7 @@
>   import os
>   import platform
>   import resource
> +import subprocess
>   from unittest import skipIf, skipUnless
>   
>   from .cmd import which
> @@ -149,3 +150,18 @@ def skipLockedMemoryTest(locked_memory):
>           ulimit_memory == resource.RLIM_INFINITY or ulimit_memory >= locked_memory * 1024,
>           f'Test required {locked_memory} kB of available locked memory',
>       )
> +
> +'''
> +Decorator to skip execution of a test if passwordless
> +sudo command is not available.
> +'''
> +def skipUnlessPasswordlessSudo():
> +    proc = subprocess.run(["sudo", "-n", "/bin/true"],
> +                          stdin=subprocess.PIPE,
> +                          stdout=subprocess.PIPE,
> +                          stderr=subprocess.STDOUT,
> +                          universal_newlines=True,
> +                          check=False)
> +
> +    return skipUnless(proc.returncode == 0,
> +                      f'requires password-less sudo access: {proc.stdout}')

I'd maybe rather just call it "skipWithoutSudo" ... but anyway:

Reviewed-by: Thomas Huth <thuth@redhat.com>