tests/functional: drop 'has_cmd' and 'has_cmds' helpers
The 'which' helper is simpler, not depending on the external 'which' binary, and is sufficient for test needs. Reviewed-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com> Message-ID: <20241217155953.3950506-10-berrange@redhat.com> Signed-off-by: Thomas Huth <thuth@redhat.com>
This commit is contained in:
parent
3d5938607e
commit
9132fff802
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
from .asset import Asset
|
from .asset import Asset
|
||||||
from .config import BUILD_DIR
|
from .config import BUILD_DIR
|
||||||
from .cmd import has_cmd, has_cmds, run_cmd, is_readable_executable_file, \
|
from .cmd import run_cmd, is_readable_executable_file, \
|
||||||
interrupt_interactive_console_until_pattern, wait_for_console_pattern, \
|
interrupt_interactive_console_until_pattern, wait_for_console_pattern, \
|
||||||
exec_command, exec_command_and_wait_for_pattern, get_qemu_img, which
|
exec_command, exec_command_and_wait_for_pattern, get_qemu_img, which
|
||||||
from .testcase import QemuBaseTest, QemuUserTest, QemuSystemTest
|
from .testcase import QemuBaseTest, QemuUserTest, QemuSystemTest
|
||||||
|
@ -29,52 +29,6 @@ def which(tool):
|
|||||||
return p
|
return p
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def has_cmd(name, args=None):
|
|
||||||
"""
|
|
||||||
This function is for use in a @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 @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):
|
def run_cmd(args):
|
||||||
subp = subprocess.Popen(args,
|
subp = subprocess.Popen(args,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
@ -254,7 +208,7 @@ def get_qemu_img(test):
|
|||||||
qemu_img = os.path.join(BUILD_DIR, 'qemu-img')
|
qemu_img = os.path.join(BUILD_DIR, 'qemu-img')
|
||||||
if os.path.exists(qemu_img):
|
if os.path.exists(qemu_img):
|
||||||
return qemu_img
|
return qemu_img
|
||||||
(has_system_qemu_img, errmsg) = has_cmd('qemu-img')
|
qemu_img = which('qemu-img')
|
||||||
if has_system_qemu_img:
|
if qemu_img is not None:
|
||||||
return 'qemu-img'
|
return qemu_img
|
||||||
test.skipTest(errmsg)
|
test.skipTest(f"qemu-img not found in {BUILD_DIR} or '$PATH'")
|
||||||
|
@ -15,7 +15,7 @@ import stat
|
|||||||
from qemu_test import QemuSystemTest
|
from qemu_test import QemuSystemTest
|
||||||
from qemu_test import exec_command_and_wait_for_pattern
|
from qemu_test import exec_command_and_wait_for_pattern
|
||||||
from qemu_test import wait_for_console_pattern
|
from qemu_test import wait_for_console_pattern
|
||||||
from qemu_test import has_cmd, run_cmd, get_qemu_img
|
from qemu_test import which, run_cmd, get_qemu_img
|
||||||
|
|
||||||
class TuxRunBaselineTest(QemuSystemTest):
|
class TuxRunBaselineTest(QemuSystemTest):
|
||||||
|
|
||||||
@ -38,10 +38,8 @@ class TuxRunBaselineTest(QemuSystemTest):
|
|||||||
super().setUp()
|
super().setUp()
|
||||||
|
|
||||||
# We need zstd for all the tuxrun tests
|
# We need zstd for all the tuxrun tests
|
||||||
(has_zstd, msg) = has_cmd('zstd')
|
if which('zstd') is None:
|
||||||
if has_zstd is False:
|
self.skipTest("zstd not found in $PATH")
|
||||||
self.skipTest(msg)
|
|
||||||
self.zstd = 'zstd'
|
|
||||||
|
|
||||||
# Pre-init TuxRun specific settings: Most machines work with
|
# Pre-init TuxRun specific settings: Most machines work with
|
||||||
# reasonable defaults but we sometimes need to tweak the
|
# reasonable defaults but we sometimes need to tweak the
|
||||||
@ -78,7 +76,7 @@ class TuxRunBaselineTest(QemuSystemTest):
|
|||||||
|
|
||||||
disk_image = self.workdir + "/rootfs.ext4"
|
disk_image = self.workdir + "/rootfs.ext4"
|
||||||
|
|
||||||
run_cmd([self.zstd, "-f", "-d", disk_image_zst,
|
run_cmd(['zstd', "-f", "-d", disk_image_zst,
|
||||||
"-o", disk_image])
|
"-o", disk_image])
|
||||||
# zstd copies source archive permissions for the output
|
# zstd copies source archive permissions for the output
|
||||||
# file, so must make this writable for QEMU
|
# file, so must make this writable for QEMU
|
||||||
|
Loading…
x
Reference in New Issue
Block a user