All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Thomas Huth <thuth@redhat.com>
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	qemu-devel@nongnu.org,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Ani Sinha" <anisinha@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"John Snow" <jsnow@redhat.com>, "Cédric Le Goater" <clg@kaod.org>,
	"Fabiano Rosas" <farosas@suse.de>
Subject: Re: [PATCH 05/11] tests/functional: Implement fetch_asset() method for downloading assets
Date: Tue, 16 Jul 2024 19:17:54 +0100	[thread overview]
Message-ID: <Zpa5Un6oAdVstI-r@redhat.com> (raw)
In-Reply-To: <20240716112614.1755692-6-thuth@redhat.com>

On Tue, Jul 16, 2024 at 01:26:08PM +0200, Thomas Huth wrote:
> In the new python test framework, we cannot use the fetch_asset()
> function from Avocado anymore, so we have to provide our own
> implementation now instead. Thus add such a function based on the
> urllib python module for this purpose.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  tests/functional/qemu_test/__init__.py | 36 ++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)
> 
> diff --git a/tests/functional/qemu_test/__init__.py b/tests/functional/qemu_test/__init__.py
> index e73705d40a..77c3b14d34 100644
> --- a/tests/functional/qemu_test/__init__.py
> +++ b/tests/functional/qemu_test/__init__.py
> @@ -11,6 +11,8 @@
>  # This work is licensed under the terms of the GNU GPL, version 2 or
>  # later.  See the COPYING file in the top-level directory.
>  
> +import hashlib
> +import urllib.request
>  import logging
>  import os
>  import pycotap
> @@ -23,6 +25,7 @@
>  import unittest
>  
>  from pathlib import Path
> +from shutil import copyfileobj
>  from qemu.machine import QEMUMachine
>  from qemu.utils import (get_info_usernet_hostfwd_port, kvm_available,
>                          tcg_available)
> @@ -215,6 +218,39 @@ def setUp(self, bin_prefix):
>          if not os.path.exists(self.workdir):
>              os.makedirs(self.workdir)
>  
> +    def check_hash(self, file_name, expected_hash):
> +        if not expected_hash:
> +            return True
> +        if len(expected_hash) == 40:
> +            sum_prog = 'sha1sum'
> +        elif len(expected_hash) == 64:
> +            sum_prog = 'sha256sum'
> +        elif len(expected_hash) == 128:
> +            sum_prog = 'sha512sum'
> +        else:
> +            raise Exception("unknown hash type")
> +        checksum = subprocess.check_output([sum_prog, file_name]).split()[0]
> +        return expected_hash == checksum.decode("utf-8")
> +
> +    def fetch_asset(self, url, asset_hash):
> +        cache_dir = os.path.expanduser("~/.cache/qemu/download")
> +        if not os.path.exists(cache_dir):
> +            os.makedirs(cache_dir)
> +        fname = os.path.join(cache_dir,
> +                             hashlib.sha256(url.encode("utf-8")).hexdigest())
> +        if os.path.exists(fname) and self.check_hash(fname, asset_hash):
> +            return fname
> +        self.log.info("Downloading %s to %s...", url, fname)
> +        dl_fname = fname + ".download"
> +        with urllib.request.urlopen(url) as src:
> +            with open(dl_fname, "wb+") as dst:
> +                copyfileobj(src, dst)

For cleanliness we should probably wrap this and delete the file on
error, eg

   try:
     with open(dl_fname,....) as dst
        ....
   except:
     os.remove(dl_fname)
     raise

> +        if not self.check_hash(dl_fname, asset_hash):
> +            os.remove(dl_fname)
> +            raise Exception("Hash of " + url + " does not match")
> +        os.rename(dl_fname, fname)
> +        return fname
> +
>      def main():
>          tr = pycotap.TAPTestRunner(message_log = pycotap.LogMode.LogToError,
>                                     test_output_log = pycotap.LogMode.LogToError)
> -- 
> 2.45.2
> 

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 :|



  reply	other threads:[~2024-07-16 18:18 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-16 11:26 [PATCH v1 00/11] Convert avocado tests to normal Python unittests Thomas Huth
2024-07-16 11:26 ` [PATCH 01/11] tests/functional: Add base classes for the upcoming pytest-based tests Thomas Huth
2024-07-16 11:26 ` [PATCH 02/11] tests/functional: Convert simple avocado tests into standalone python tests Thomas Huth
2024-07-16 18:14   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 03/11] tests/functional: Convert avocado tests that just need a small adjustment Thomas Huth
2024-07-16 18:15   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 04/11] tests/functional: Add python-based tests to the meson build system Thomas Huth
2024-07-16 15:15   ` Fabiano Rosas
2024-07-17  9:20     ` Thomas Huth
2024-07-16 16:55   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 05/11] tests/functional: Implement fetch_asset() method for downloading assets Thomas Huth
2024-07-16 18:17   ` Daniel P. Berrangé [this message]
2024-07-16 11:26 ` [PATCH 06/11] tests/functional: Convert some tests that download files via fetch_asset() Thomas Huth
2024-07-16 18:19   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 07/11] tests/functional: Add a function for extracting files from an archive Thomas Huth
2024-07-16 18:20   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 08/11] tests/functional: Convert some avocado tests that needed avocado.utils.archive Thomas Huth
2024-07-16 18:20   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 09/11] tests/functional: Set up logging Thomas Huth
2024-07-16 18:22   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 10/11] tests/functional: Convert the s390x avocado tests into standalone tests Thomas Huth
2024-07-16 18:24   ` Daniel P. Berrangé
2024-07-16 11:26 ` [PATCH 11/11] gitlab-ci: Add "check-functional" to the build tests Thomas Huth
2024-07-16 18:25   ` Daniel P. Berrangé
2024-07-16 16:51 ` [PATCH v1 00/11] Convert avocado tests to normal Python unittests Daniel P. Berrangé
2024-07-16 17:03   ` Thomas Huth
2024-07-17  8:04     ` Thomas Huth
2024-07-17  8:37       ` Daniel P. Berrangé
2024-07-17  8:53         ` Thomas Huth
2024-07-16 17:57 ` Paolo Bonzini
2024-07-24 11:43   ` Thomas Huth
2024-07-25 14:21 ` Cleber Rosa
2024-07-26 10:07   ` Thomas Huth
2024-07-26 13:56     ` Cleber Rosa
2024-07-29 12:44   ` Daniel P. Berrangé
2024-07-29 14:01     ` Cleber Rosa

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=Zpa5Un6oAdVstI-r@redhat.com \
    --to=berrange@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=anisinha@redhat.com \
    --cc=clg@kaod.org \
    --cc=farosas@suse.de \
    --cc=jsnow@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=thuth@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.