[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 07/23] tests/functional: Implement fetch_asset() method for do
From: |
Thomas Huth |
Subject: |
[PATCH v2 07/23] tests/functional: Implement fetch_asset() method for downloading assets |
Date: |
Wed, 24 Jul 2024 19:52:25 +0200 |
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 | 41 ++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/tests/functional/qemu_test/__init__.py
b/tests/functional/qemu_test/__init__.py
index fc98222c52..40a81c3927 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 kvm_available, tcg_available
@@ -216,6 +219,44 @@ 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):
+ self.log.debug("Using cached assed %s for %s", fname, url)
+ return fname
+ self.log.info("Downloading %s to %s...", url, fname)
+ dl_fname = fname + ".download"
+ with urllib.request.urlopen(url) as src:
+ try:
+ with open(dl_fname, "wb+") as dst:
+ copyfileobj(src, 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
- [PATCH v2 04/23] tests/functional: Prepare the meson build system for the functional tests, (continued)
- [PATCH v2 04/23] tests/functional: Prepare the meson build system for the functional tests, Thomas Huth, 2024/07/24
- [PATCH v2 05/23] tests/functional: Convert simple avocado tests into standalone python tests, Thomas Huth, 2024/07/24
- [PATCH v2 06/23] tests/functional: Convert avocado tests that just need a small adjustment, Thomas Huth, 2024/07/24
- [PATCH v2 07/23] tests/functional: Implement fetch_asset() method for downloading assets,
Thomas Huth <=
- [PATCH v2 08/23] tests/functional: Convert some tests that download files via fetch_asset(), Thomas Huth, 2024/07/24
- [PATCH v2 09/23] tests/functional: Add a function for extracting files from an archive, Thomas Huth, 2024/07/24
- [PATCH v2 10/23] tests/functional: Convert some avocado tests that needed avocado.utils.archive, Thomas Huth, 2024/07/24
- [PATCH v2 11/23] tests/functional: Set up logging, Thomas Huth, 2024/07/24
- [PATCH v2 12/23] tests/functional: Convert the s390x avocado tests into standalone tests, Thomas Huth, 2024/07/24
- [PATCH v2 14/23] tests/functional: Convert the microblaze avocado tests into standalone tests, Thomas Huth, 2024/07/24
- [PATCH v2 13/23] tests/functional: Convert the x86_cpu_model_versions test, Thomas Huth, 2024/07/24