]> git.ipfire.org Git - thirdparty/qemu.git/commitdiff
tests/functional/asset: Verify downloaded size
authorNicholas Piggin <npiggin@gmail.com>
Wed, 12 Mar 2025 13:00:01 +0000 (23:00 +1000)
committerThomas Huth <thuth@redhat.com>
Wed, 12 Mar 2025 17:20:50 +0000 (18:20 +0100)
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.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Message-ID: <20250312130002.945508-3-npiggin@gmail.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
tests/functional/qemu_test/asset.py

index 27dd839e705f6b31ad96f5541a101be7e79cd4dd..6bbfb9e1cadec98cf072248d259407aa1c55b7ef 100644 (file)
@@ -121,6 +121,20 @@ class Asset:
                 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, "