ci: follow-ups after moving Molecule to the molecule-controller image #506
No files matched your search
@@ -70,9 +70,18 @@ USER_AGENT = "kamino-next-image-version"
|
||||
|
||||
|
||||
class RegistryError(Exception):
|
||||
def __init__(self, url, status, text, challenge=""):
|
||||
def __init__(self, url, status, text, challenge="", codes=()):
|
||||
super().__init__(f"GET {url}: HTTP {status}: {text}")
|
||||
self.status, self.challenge = status, challenge
|
||||
self.status, self.challenge, self.codes = status, challenge, codes
|
||||
|
||||
|
||||
def _error_codes(raw):
|
||||
"""The OCI error codes of an error body (`{"errors": [{"code": ...}]}`), or () for any other body."""
|
||||
try:
|
||||
errors = json.loads(raw).get("errors") or []
|
||||
return tuple(e.get("code") for e in errors if isinstance(e, dict))
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
return ()
|
||||
|
||||
|
||||
def _readable(raw):
|
||||
@@ -92,7 +101,9 @@ def _fetch(url, headers):
|
||||
except urllib.error.HTTPError as exc:
|
||||
raw = exc.read(20000).decode("utf-8", errors="replace")
|
||||
challenge = exc.headers.get("WWW-Authenticate", "")
|
||||
raise RegistryError(url, exc.code, _readable(raw), challenge) from exc
|
||||
raise RegistryError(
|
||||
url, exc.code, _readable(raw), challenge, _error_codes(raw)
|
||||
) from exc
|
||||
|
||||
|
||||
class Registry:
|
||||
@@ -139,7 +150,8 @@ class Registry:
|
||||
body, headers = self._get(path)
|
||||
except RegistryError as exc:
|
||||
# A repository not pushed yet is 404 NAME_UNKNOWN (OCI distribution spec): no tags.
|
||||
if exc.status == 404 and not tags:
|
||||
# Any other 404, such as a wrong registry path, must fail rather than restart at .0.
|
||||
if exc.status == 404 and "NAME_UNKNOWN" in exc.codes and not tags:
|
||||
return []
|
||||
raise
|
||||
tags += body.get("tags") or []
|
||||
|
||||
@@ -105,6 +105,7 @@ class FakeRegistry(http.server.BaseHTTPRequestHandler):
|
||||
no_realm = False
|
||||
empty_token = False
|
||||
unknown = False # the repository does not exist yet
|
||||
not_found = None # a 404 body without NAME_UNKNOWN
|
||||
agents = None # a fresh list per test, set in setUp
|
||||
|
||||
def do_GET(self):
|
||||
@@ -128,6 +129,8 @@ class FakeRegistry(http.server.BaseHTTPRequestHandler):
|
||||
b"{}",
|
||||
extra={"WWW-Authenticate": f'Bearer realm="{realm}",service="reg"'},
|
||||
)
|
||||
elif self.not_found is not None:
|
||||
self.reply(404, self.not_found, "text/plain")
|
||||
elif self.unknown:
|
||||
body = {"errors": [{"code": "NAME_UNKNOWN", "message": "unknown"}]}
|
||||
self.reply(404, json.dumps(body).encode())
|
||||
@@ -155,6 +158,7 @@ class RegistryClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
FakeRegistry.refuse = FakeRegistry.refuse_token = FakeRegistry.no_realm = False
|
||||
FakeRegistry.empty_token = FakeRegistry.unknown = False
|
||||
FakeRegistry.not_found = None
|
||||
FakeRegistry.agents = []
|
||||
server = QuietServer(("127.0.0.1", 0), FakeRegistry)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
@@ -176,6 +180,17 @@ class RegistryClient(unittest.TestCase):
|
||||
niv.next_version("0.1", self.registry.tags(), "c", no_label), "0.1.0"
|
||||
)
|
||||
|
||||
def test_a_404_without_name_unknown_is_a_registry_error(self):
|
||||
for body in (
|
||||
b"404 page not found",
|
||||
b'{"errors":[{"code":"NOT_FOUND","message":"no such route"}]}',
|
||||
):
|
||||
with self.subTest(body=body):
|
||||
FakeRegistry.not_found = body
|
||||
with self.assertRaises(niv.RegistryError) as caught:
|
||||
self.registry.tags()
|
||||
self.assertEqual(caught.exception.status, 404)
|
||||
|
||||
def test_a_missing_manifest_is_still_a_registry_error(self):
|
||||
FakeRegistry.unknown = True
|
||||
with self.assertRaises(niv.RegistryError) as caught:
|
||||
|
||||
Reference in new issue
Block a user