[PATCH 16/22] tests/functional: add common deb_extract helper

Daniel P. Berrangé posted 22 patches 3 weeks, 5 days ago
There is a newer version of this series
[PATCH 16/22] tests/functional: add common deb_extract helper
Posted by Daniel P. Berrangé 3 weeks, 5 days ago
This mirrors the existing archive_extract, cpio_extract and zip_extract
helpers

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 tests/functional/qemu_test/linuxkernel.py | 15 +++++----------
 tests/functional/qemu_test/utils.py       | 13 +++++++++++++
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/tests/functional/qemu_test/linuxkernel.py b/tests/functional/qemu_test/linuxkernel.py
index a6525f9dd6..fb6a158d36 100644
--- a/tests/functional/qemu_test/linuxkernel.py
+++ b/tests/functional/qemu_test/linuxkernel.py
@@ -6,8 +6,8 @@
 import os
 
 from .testcase import QemuSystemTest
-from .cmd import run_cmd, wait_for_console_pattern
-from .utils import archive_extract
+from .cmd import wait_for_console_pattern
+from .utils import deb_extract
 
 class LinuxKernelTest(QemuSystemTest):
     KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
@@ -37,16 +37,11 @@ def extract_from_deb(self, deb_path, path):
         :param path: path within the deb archive of the file to be extracted
         :returns: path of the extracted file
         """
-        cwd = os.getcwd()
-        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)
+        relpath = os.path.relpath(path, '/')
+        deb_extract(deb_path, self.workdir, member="." + path)
         # Return complete path to extracted file.  Because callers to
         # extract_from_deb() specify 'path' with a leading slash, it is
         # necessary to use 'relative_to()' to turn it into a relative
         # path for joining to the scratch dir
-        return os.path.normpath(self.scratch_file(os.path.relpath(path, '/')))
+        return os.path.normpath(self.scratch_file(relpath))
 
diff --git a/tests/functional/qemu_test/utils.py b/tests/functional/qemu_test/utils.py
index 41bd1df666..bafe7fb80e 100644
--- a/tests/functional/qemu_test/utils.py
+++ b/tests/functional/qemu_test/utils.py
@@ -15,6 +15,8 @@
 import subprocess
 import tarfile
 
+from .cmd import run_cmd
+
 """
 Round up to next power of 2
 """
@@ -53,6 +55,17 @@ def zip_extract(archive, dest_dir, member=None):
         else:
             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])
+        archive_extract(file_path, dest_dir, member)
+    finally:
+        os.chdir(cwd)
+
 def gzip_uncompress(gz_path, output_path):
     if os.path.exists(output_path):
         return
-- 
2.46.0


Re: [PATCH 16/22] tests/functional: add common deb_extract helper
Posted by Thomas Huth 3 weeks, 3 days ago
On 29/11/2024 18.31, Daniel P. Berrangé wrote:
> This mirrors the existing archive_extract, cpio_extract and zip_extract
> helpers
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   tests/functional/qemu_test/linuxkernel.py | 15 +++++----------
>   tests/functional/qemu_test/utils.py       | 13 +++++++++++++
>   2 files changed, 18 insertions(+), 10 deletions(-)
> 
> diff --git a/tests/functional/qemu_test/linuxkernel.py b/tests/functional/qemu_test/linuxkernel.py
> index a6525f9dd6..fb6a158d36 100644
> --- a/tests/functional/qemu_test/linuxkernel.py
> +++ b/tests/functional/qemu_test/linuxkernel.py
> @@ -6,8 +6,8 @@
>   import os
>   
>   from .testcase import QemuSystemTest
> -from .cmd import run_cmd, wait_for_console_pattern
> -from .utils import archive_extract
> +from .cmd import wait_for_console_pattern
> +from .utils import deb_extract
>   
>   class LinuxKernelTest(QemuSystemTest):
>       KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
> @@ -37,16 +37,11 @@ def extract_from_deb(self, deb_path, path):
>           :param path: path within the deb archive of the file to be extracted
>           :returns: path of the extracted file
>           """
> -        cwd = os.getcwd()
> -        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)
> +        relpath = os.path.relpath(path, '/')
> +        deb_extract(deb_path, self.workdir, member="." + path)
>           # Return complete path to extracted file.  Because callers to
>           # extract_from_deb() specify 'path' with a leading slash, it is
>           # necessary to use 'relative_to()' to turn it into a relative
>           # path for joining to the scratch dir
> -        return os.path.normpath(self.scratch_file(os.path.relpath(path, '/')))
> +        return os.path.normpath(self.scratch_file(relpath))
>   
> diff --git a/tests/functional/qemu_test/utils.py b/tests/functional/qemu_test/utils.py
> index 41bd1df666..bafe7fb80e 100644
> --- a/tests/functional/qemu_test/utils.py
> +++ b/tests/functional/qemu_test/utils.py
> @@ -15,6 +15,8 @@
>   import subprocess
>   import tarfile
>   
> +from .cmd import run_cmd
> +
>   """
>   Round up to next power of 2
>   """
> @@ -53,6 +55,17 @@ def zip_extract(archive, dest_dir, member=None):
>           else:
>               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])
> +        archive_extract(file_path, dest_dir, member)
> +    finally:
> +        os.chdir(cwd)

Not sure whether we really need this ... extracting .deb files is very 
specific to running Linux kernels in the guest, so IMHO it should be 
sufficient to have it in linuxkernel.py.

Anyway, if we add more and more *_extract functions, we should maybe 
consider to move the extraction functions out of utils.py into an archive.py 
file, what do you think?

  Thomas


Re: [PATCH 16/22] tests/functional: add common deb_extract helper
Posted by Daniel P. Berrangé 3 weeks, 3 days ago
On Mon, Dec 02, 2024 at 11:14:16AM +0100, Thomas Huth wrote:
> On 29/11/2024 18.31, Daniel P. Berrangé wrote:
> > This mirrors the existing archive_extract, cpio_extract and zip_extract
> > helpers
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> >   tests/functional/qemu_test/linuxkernel.py | 15 +++++----------
> >   tests/functional/qemu_test/utils.py       | 13 +++++++++++++
> >   2 files changed, 18 insertions(+), 10 deletions(-)
> > 
> > diff --git a/tests/functional/qemu_test/linuxkernel.py b/tests/functional/qemu_test/linuxkernel.py
> > index a6525f9dd6..fb6a158d36 100644
> > --- a/tests/functional/qemu_test/linuxkernel.py
> > +++ b/tests/functional/qemu_test/linuxkernel.py

> > +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])
> > +        archive_extract(file_path, dest_dir, member)
> > +    finally:
> > +        os.chdir(cwd)
> 
> Not sure whether we really need this ... extracting .deb files is very
> specific to running Linux kernels in the guest, so IMHO it should be
> sufficient to have it in linuxkernel.py.

You've probably seen the motivation in later patches now - the desire is
to have a standardized "archive_extract" method that handles all types
of archive, automatically setting the dest_dir to 'workdir'. I didn't
want deb_extract to remain an exception to the pattern, because it is
used quite widely.


> Anyway, if we add more and more *_extract functions, we should maybe
> consider to move the extraction functions out of utils.py into an archive.py
> file, what do you think?

Yeah, good point. I temporarily forgot my general rule that files / dirs
called 'util' end up being a general "dumping ground" for unrelated stuff,
and more specialized files are a better idea.

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|