Retry transient transport failures in package compatibility tests

This commit is contained in:
waozi committed 2026-10-26 20:45:31 -03:00
1 parent 0bf3339102
commit 5707266d5e
3 files changed
+88 -5

No files matched your search

+1
View File
@@ -68,6 +68,7 @@ package-release-test: apps $(ZIRAN_BUILD_DIR)/libziran.a
# each new package that declares a host interface it has.
.PHONY: package-compat-test
package-compat-test: $(TARGET) $(APP_BUNDLES) $(INBE_BUNDLE)
python3 tests/package_compat_fetch_test.py
python3 tests/package_compat_test.py $(abspath $(TARGET))
.PHONY: package-version-test
+65
View File
@@ -0,0 +1,65 @@
"""Compatibility downloads tolerate transport outages without hiding bad data."""
import io
import socket
import unittest
from unittest.mock import patch
import urllib.error
from package_compat_test import fetch
class FetchTest(unittest.TestCase):
def test_temporary_dns_recovers_and_payload_is_returned(self):
outage = urllib.error.URLError(socket.gaierror(socket.EAI_AGAIN, "temporary DNS outage"))
with (
patch("package_compat_test.urllib.request.urlopen",
side_effect=[outage, outage, io.BytesIO(b"package")]) as request,
patch("package_compat_test.time.sleep"),
):
self.assertEqual(fetch("https://api.waozi.xyz/package.zx", 7), b"package")
self.assertEqual(request.call_count, 3)
def test_exhausted_dns_outage_is_still_a_failure(self):
outage = urllib.error.URLError(socket.gaierror(socket.EAI_AGAIN, "temporary DNS outage"))
with (
patch("package_compat_test.urllib.request.urlopen", side_effect=outage) as request,
patch("package_compat_test.time.sleep"),
):
with self.assertRaises(urllib.error.URLError):
fetch("https://api.waozi.xyz/package.zx", 7)
self.assertEqual(request.call_count, 4)
def test_absent_package_is_not_retried(self):
absent = urllib.error.HTTPError("https://api.waozi.xyz/package.zx", 404, "not offered", {}, None)
with patch("package_compat_test.urllib.request.urlopen", side_effect=absent) as request:
with self.assertRaises(urllib.error.HTTPError) as failure:
fetch("https://api.waozi.xyz/package.zx", 7)
self.assertEqual(failure.exception.code, 404)
request.assert_called_once()
def test_oversized_payload_is_rejected_without_retry(self):
with patch("package_compat_test.urllib.request.urlopen", return_value=io.BytesIO(b"too large")) as request:
with self.assertRaises(ValueError):
fetch("https://api.waozi.xyz/package.zx", 3)
request.assert_called_once()
def test_permanent_dns_error_is_not_retried(self):
missing = urllib.error.URLError(socket.gaierror(socket.EAI_NONAME, "unknown host"))
with patch("package_compat_test.urllib.request.urlopen", side_effect=missing) as request:
with self.assertRaises(urllib.error.URLError):
fetch("https://api.waozi.xyz/package.zx", 7)
request.assert_called_once()
def test_temporary_server_failure_recovers(self):
unavailable = urllib.error.HTTPError("https://api.waozi.xyz/package.zx", 503, "unavailable", {}, None)
with (
patch("package_compat_test.urllib.request.urlopen",
side_effect=[unavailable, io.BytesIO(b"package")]) as request,
patch("package_compat_test.time.sleep"),
):
self.assertEqual(fetch("https://api.waozi.xyz/package.zx", 7), b"package")
self.assertEqual(request.call_count, 2)
if __name__ == "__main__":
unittest.main()
+22 -5
View File
@@ -20,6 +20,7 @@ import hashlib
import json
import os
from pathlib import Path
import socket
import stat
import subprocess
import sys
@@ -57,11 +58,27 @@ def fetch(url, limit):
headers["Authorization"] = f"Bearer {token}"
headers["Accept"] = "application/vnd.github+json"
request = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(request, timeout=60) as response:
data = response.read(limit + 1)
if len(data) > limit:
raise ValueError(f"{url} exceeds {limit} bytes")
return data
for attempt in range(4):
try:
with urllib.request.urlopen(request, timeout=60) as response:
data = response.read(limit + 1)
if len(data) > limit:
raise ValueError(f"{url} exceeds {limit} bytes")
return data
except urllib.error.HTTPError as error:
if error.code not in (408, 429, 500, 502, 503, 504) or attempt == 3:
raise
except urllib.error.URLError as error:
reason = error.reason
temporary_dns = isinstance(reason, socket.gaierror) and reason.errno == socket.EAI_AGAIN
if not (temporary_dns or isinstance(reason, (TimeoutError, ConnectionError))) or attempt == 3:
raise
except (TimeoutError, ConnectionError):
if attempt == 3:
raise
destination = urllib.parse.urlsplit(url)
print(f"Temporary package transport failure for {destination.hostname}{destination.path}; retrying")
time.sleep(2 ** attempt)
def host_range(ledger):