Skip to content

Commit 4707088

Browse files
authored
Capture HTTP errors for Quay GET requests (#274)
1 parent a324ba1 commit 4707088

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

config/repository_notification_resetter/notification_resetter/reset_notifications.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ def get_quay_notifications(
5151
},
5252
)
5353

54-
with urlopen(request) as resp:
55-
if resp.status != 200:
56-
# do not fail the job if we can't fetch notifications
57-
# for single repository
58-
LOGGER.warning("Failed to fetch notifications for %s/%s", namespace, name)
59-
json_data = {}
60-
else:
61-
json_data = json.loads(resp.read())
54+
try:
55+
with urlopen(request) as resp:
56+
if resp.status != 200:
57+
# do not fail the job if we can't fetch notifications
58+
# for single repository
59+
LOGGER.warning("Failed to fetch notifications for %s/%s", namespace, name)
60+
json_data = {}
61+
else:
62+
json_data = json.loads(resp.read())
63+
except HTTPError:
64+
LOGGER.warning("Failed to fetch notifications for %s/%s", namespace, name)
65+
json_data = {}
6266

6367
return json_data.get("notifications", [])
6468

config/repository_notification_resetter/notification_resetter/test_reset_notifications.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,50 @@ def test_reset_notification(self, urlopen):
361361
self.assert_make_post_request(request)
362362
self.assert_quay_token_included(request)
363363

364+
@patch.dict(os.environ, {"QUAY_TOKEN": QUAY_TOKEN})
365+
@patch("sys.argv", ["reset_notifications", "--namespace", "sample"])
366+
@patch("reset_notifications.urlopen")
367+
def test_get_notifications_http_error_returns_empty(self, urlopen):
368+
"""When fetching notifications returns 504 (or any HTTPError), job continues with empty list."""
369+
fetch_repos = MagicMock()
370+
response = MagicMock()
371+
response.status = 200
372+
response.read.return_value = json.dumps(
373+
{
374+
"repositories": [
375+
{"namespace": "sample", "name": "hello-image"},
376+
],
377+
}
378+
).encode()
379+
fetch_repos.__enter__.return_value = response
380+
381+
mock_error = HTTPError(
382+
url="http://example.com",
383+
code=504,
384+
msg="Gateway Timeout",
385+
hdrs=None,
386+
fp=io.BytesIO(b""),
387+
)
388+
389+
urlopen.side_effect = [
390+
fetch_repos,
391+
mock_error, # get_quay_notifications raises
392+
]
393+
394+
with self.assertLogs(LOGGER) as logs:
395+
main()
396+
run_log = [
397+
msg
398+
for msg in logs.output
399+
if re.search(
400+
r"Failed to fetch notifications for sample/hello-image",
401+
msg,
402+
)
403+
]
404+
self.assertEqual(1, len(run_log))
405+
406+
self.assertEqual(2, urlopen.call_count)
407+
364408
@patch("reset_notifications.urlopen")
365409
def test_handle_image_repos_pagination(self, urlopen):
366410
first_fetch_rv = MagicMock()

0 commit comments

Comments
 (0)