[PATCH] tests/functional: Allow Asset() to work with non-HTTP URLs

Peter Maydell posted 1 patch 1 week, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261109161443.724122-1-peter.maydell@linaro.org
Maintainers: Thomas Huth <th.huth+qemu@posteo.eu>, "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>, "Daniel P. Berrangé" <berrange@redhat.com>
tests/functional/qemu_test/asset.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] tests/functional: Allow Asset() to work with non-HTTP URLs
Posted by Peter Maydell 1 week, 6 days ago
The Asset() implementation only works with HTTP URLs because it
assumes that the object it gets back from urllib.request.urlopen()
has a getheader() method.  This is only true for the HTTPResponse
object that HTTP URLs produce, not for the urllib.request.addinfourl
object that file, data and FTP URLs produce.

File URLs are obviously not useful for public QEMU tests, but they
can be helpful for iterating locally on the asset to be used by a
test before putting the final asset image up somewhere public.

Get the Content-Length header with resp.headers.get(), which works on
both object types, and matches how the urllib documentation suggests
getting headers.

Fixes: 7524e1b33679dc13 ("tests/functional/asset: Verify downloaded size")
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 tests/functional/qemu_test/asset.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/functional/qemu_test/asset.py b/tests/functional/qemu_test/asset.py
index 0abd89e0a38..780b995b703 100644
--- a/tests/functional/qemu_test/asset.py
+++ b/tests/functional/qemu_test/asset.py
@@ -139,7 +139,7 @@ def _try_to_fetch(self, tmp_cache_file):
                 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")
+                        length_hdr = resp.headers.get("Content-Length")
 
                 # Verify downloaded file size against length metadata, if
                 # available.
-- 
2.43.0
Re: [PATCH] tests/functional: Allow Asset() to work with non-HTTP URLs
Posted by Thomas Huth 1 week, 6 days ago
Am 09.11.26 um 17:14 schrieb Peter Maydell:
> The Asset() implementation only works with HTTP URLs because it
> assumes that the object it gets back from urllib.request.urlopen()
> has a getheader() method.  This is only true for the HTTPResponse
> object that HTTP URLs produce, not for the urllib.request.addinfourl
> object that file, data and FTP URLs produce.
> 
> File URLs are obviously not useful for public QEMU tests, but they
> can be helpful for iterating locally on the asset to be used by a
> test before putting the final asset image up somewhere public.
> 
> Get the Content-Length header with resp.headers.get(), which works on
> both object types, and matches how the urllib documentation suggests
> getting headers.
> 
> Fixes: 7524e1b33679dc13 ("tests/functional/asset: Verify downloaded size")
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>   tests/functional/qemu_test/asset.py | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tests/functional/qemu_test/asset.py b/tests/functional/qemu_test/asset.py
> index 0abd89e0a38..780b995b703 100644
> --- a/tests/functional/qemu_test/asset.py
> +++ b/tests/functional/qemu_test/asset.py
> @@ -139,7 +139,7 @@ def _try_to_fetch(self, tmp_cache_file):
>                   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")
> +                        length_hdr = resp.headers.get("Content-Length")
>   
>                   # Verify downloaded file size against length metadata, if
>                   # available.

Reviewed-by: Thomas Huth <thuth@redhat.com>