Skip to contentSkip to conversation

tests/functional/asset: improve partial-download handling

PATCH Superseded Nicholas Piggin 2 participants 7 messages Mar 12, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover tests/functional/asset: improve partial-download handling

v1 discussion:

https://lore.kernel.org/qemu-devel/[email protected]/T/#md49b293a64207b578600a8c428bccbce3d471e68

Changes since v1:

- Change retry exceeded handling to be a check for no file
- Tidied comments and debug leftover from patch 2
- If downloaded size does not match, the advertised and received sizes are now printed in the error log.

Thanks Thomas and Daniel for review comments.

Thanks,
Nick

Nicholas Piggin (3):
tests/functional/asset: Fail assert fetch when retries are exceeded tests/functional/asset: Verify downloaded size tests/functional/asset: Add AssetError exception class

 roms/skiboot                        |  2 +-
 tests/functional/qemu_test/asset.py | 58 ++++++++++++++++++++++-------
 tests/lcitool/libvirt-ci            |  2 +-
 3 files changed, 46 insertions(+), 16 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v2 1/3 tests/functional/asset: Fail assert fetch when retries are exceeded

Currently the fetch code does not fail gracefully when retry limit is exceeded, it just falls through the loop with no file, which ends up hitting other errors.

Add a check for non-existing file, which indicates the retry limit was exceeded.

---
 tests/functional/qemu_test/asset.py | 3 +++
 1 file changed, 3 insertions(+)
tests/functional/qemu_test/asset.py +3 -0
index f0730695f09..27dd839e705 100644
@@ -138,6 +138,9 @@ def fetch(self):
                 tmp_cache_file.unlink()
                 raise
 
+        if not os.path.exists(tmp_cache_file):
+            raise Exception("Retries exceeded downloading %s", self.url)
+
         try:
             # Set these just for informational purposes
             os.setxattr(str(tmp_cache_file), "user.qemu-asset-url",
Signature · 1 line
2.47.1
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] tests/functional/asset: Fail assert fetch when retries are exceeded

Quoted 11 lines · Nicholas Piggin

Currently the fetch code does not fail gracefully when retry limit is exceeded, it just falls through the loop with no file, which ends up hitting other errors.

Add a check for non-existing file, which indicates the retry limit was exceeded.

---
 tests/functional/qemu_test/asset.py | 3 +++
 1 file changed, 3 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#3 lore raw

PATCH v2 2/3 tests/functional/asset: Verify downloaded size

In reply to Nicholas Piggin

If the server provides a Content-Length header, use that to verify the size of the downloaded file. This catches cases where the connection terminates early, and gives the opportunity to retry. Without this, the checksum will likely mismatch and fail without retry.

---
 tests/functional/qemu_test/asset.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tests/functional/qemu_test/asset.py +14 -0
index 27dd839e705..6bbfb9e1cad 100644
@@ -121,6 +121,20 @@ def fetch(self):
                 with tmp_cache_file.open("xb") as dst:
                     with urllib.request.urlopen(self.url) as resp:
                         copyfileobj(resp, dst)
+                        length_hdr = resp.getheader("Content-Length")
+
+                # Verify downloaded file size against length metadata, if
+                # available.
+                if length_hdr is not None:
+                    length = int(length_hdr)
+                    fsize = tmp_cache_file.stat().st_size
+                    if fsize != length:
+                        self.log.error("Unable to download %s: "
+                                       "connection closed before "
+                                       "transfer complete (%d/%d)",
+                                       self.url, fsize, length)
+                        tmp_cache_file.unlink()
+                        continue
                 break
             except FileExistsError:
                 self.log.debug("%s already exists, "
Signature · 1 line
2.47.1
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] tests/functional/asset: Verify downloaded size

Quoted 9 lines · Nicholas Piggin

If the server provides a Content-Length header, use that to verify the size of the downloaded file. This catches cases where the connection terminates early, and gives the opportunity to retry. Without this, the checksum will likely mismatch and fail without retry.

---
 tests/functional/qemu_test/asset.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: 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 :|
#5 lore raw

PATCH v2 3/3 tests/functional/asset: Add AssetError exception class

In reply to Nicholas Piggin

Assets are uniquely identified by human-readable-ish url, so make an AssetError exception class that prints url with error message.

A property 'transient' is used to capture whether the client may retry or try again later, or if it is a serious and likely permanent error. This is used to retain the existing behaviour of treating HTTP errors other than 404 as 'transient' and not causing precache step to fail. Additionally, partial-downloads and stale asset caches that fail to resolve after the retry limit are now treated as transient and do not cause precache step to fail.

For background: The NetBSD archive is, at the time of writing, failing with short transfer. Retrying the fetch at that position (as wget does) results in a "503 backend unavailable" error. We would like to get that error code directly, but I have not found a way to do that with urllib, so treating the short-copy as a transient failure covers that case (and seems like a reasonable way to handle it in general).

---
 roms/skiboot                        |  2 +-
 tests/functional/qemu_test/asset.py | 43 +++++++++++++++++++----------
 tests/lcitool/libvirt-ci            |  2 +-
 3 files changed, 30 insertions(+), 17 deletions(-)
roms/skiboot +1 -1
index 24a7eb35966..785a5e3070a 160000
@@ -1 +1 @@
-Subproject commit 24a7eb35966d93455520bc2debdd7954314b638b
+Subproject commit 785a5e3070a86e18521e62fe202b87209de30fa2
tests/functional/qemu_test/asset.py +28 -15

84 lines not on this page · raw message

tests/lcitool/libvirt-ci +1 -1
index 18c4bfe02c4..b6a65806bc9 160000
@@ -1 +1 @@
-Subproject commit 18c4bfe02c467e5639bf9a687139735ccd7a3fff
+Subproject commit b6a65806bc9b2b56985f5e97c936b77c7e7a99fc
Signature · 1 line
2.47.1
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] tests/functional/asset: Add AssetError exception class

Quoted 40 lines · Nicholas Piggin

Assets are uniquely identified by human-readable-ish url, so make an AssetError exception class that prints url with error message.

A property 'transient' is used to capture whether the client may retry or try again later, or if it is a serious and likely permanent error. This is used to retain the existing behaviour of treating HTTP errors other than 404 as 'transient' and not causing precache step to fail. Additionally, partial-downloads and stale asset caches that fail to resolve after the retry limit are now treated as transient and do not cause precache step to fail.

For background: The NetBSD archive is, at the time of writing, failing with short transfer. Retrying the fetch at that position (as wget does) results in a "503 backend unavailable" error. We would like to get that error code directly, but I have not found a way to do that with urllib, so treating the short-copy as a transient failure covers that case (and seems like a reasonable way to handle it in general).

---
 roms/skiboot                        |  2 +-
 tests/functional/qemu_test/asset.py | 43 +++++++++++++++++++----------
 tests/lcitool/libvirt-ci            |  2 +-
 3 files changed, 30 insertions(+), 17 deletions(-)
roms/skiboot +1 -1
index 24a7eb35966..785a5e3070a 160000
@@ -1 +1 @@
-Subproject commit 24a7eb35966d93455520bc2debdd7954314b638b
+Subproject commit 785a5e3070a86e18521e62fe202b87209de30fa2
tests/lcitool/libvirt-ci +0 -0
index 18c4bfe02c4..b6a65806bc9 160000

tests/lcitool/libvirt-ci line 1

Quoted 2 lines
tests/lcitool/libvirt-ci +1 -1
@@ -1 +1 @@
-Subproject commit 18c4bfe02c467e5639bf9a687139735ccd7a3fff
+Subproject commit b6a65806bc9b2b56985f5e97c936b77c7e7a99fc

Two accidents here, with those removed

With regards,
Daniel

Signature · 3 lines
|: 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 :|