tests/functional: add common deb_extract helper

This mirrors the existing archive_extract, cpio_extract and zip_extract
helpers

Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-ID: <20241217155953.3950506-20-berrange@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2024-12-17 15:59:40 +00:00 committed by Thomas Huth
parent 379ee839f9
commit 512fe088b1
2 changed files with 17 additions and 9 deletions

View file

@ -12,6 +12,8 @@ import subprocess
import tarfile import tarfile
import zipfile import zipfile
from .cmd import run_cmd
def tar_extract(archive, dest_dir, member=None): def tar_extract(archive, dest_dir, member=None):
with tarfile.open(archive) as tf: with tarfile.open(archive) as tf:
@ -37,3 +39,14 @@ def zip_extract(archive, dest_dir, member=None):
zf.extract(member=member, path=dest_dir) zf.extract(member=member, path=dest_dir)
else: else:
zf.extractall(path=dest_dir) zf.extractall(path=dest_dir)
def deb_extract(archive, dest_dir, member=None):
cwd = os.getcwd()
os.chdir(dest_dir)
try:
(stdout, stderr, ret) = run_cmd(['ar', 't', archive])
file_path = stdout.split()[2]
run_cmd(['ar', 'x', archive, file_path])
tar_extract(file_path, dest_dir, member)
finally:
os.chdir(cwd)

View file

@ -6,8 +6,9 @@
import os import os
from .testcase import QemuSystemTest from .testcase import QemuSystemTest
from .cmd import run_cmd, wait_for_console_pattern from .cmd import wait_for_console_pattern
from .utils import archive_extract from .archive import deb_extract
class LinuxKernelTest(QemuSystemTest): class LinuxKernelTest(QemuSystemTest):
KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 ' KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
@ -37,13 +38,7 @@ class LinuxKernelTest(QemuSystemTest):
:param path: path within the deb archive of the file to be extracted :param path: path within the deb archive of the file to be extracted
:returns: path of the extracted file :returns: path of the extracted file
""" """
cwd = os.getcwd() deb_extract(deb_path, self.workdir, member="." + path)
os.chdir(self.workdir)
(stdout, stderr, ret) = run_cmd(['ar', 't', deb_path])
file_path = stdout.split()[2]
run_cmd(['ar', 'x', deb_path, file_path])
archive_extract(file_path, self.workdir)
os.chdir(cwd)
# Return complete path to extracted file. Because callers to # Return complete path to extracted file. Because callers to
# extract_from_deb() specify 'path' with a leading slash, it is # extract_from_deb() specify 'path' with a leading slash, it is
# necessary to use os.path.relpath() as otherwise scratch_file() # necessary to use os.path.relpath() as otherwise scratch_file()