From dd8c1002d4bec3952c0196c972be82e96b021ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Wed, 23 Nov 2022 10:26:28 +0100 Subject: [PATCH] Report errors from CTS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the service returns a status code indicating a user error, report that and do not retry. Signed-off-by: Lubomír Sedlář --- pungi/compose.py | 6 ++++++ tests/test_compose.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pungi/compose.py b/pungi/compose.py index ab62bbfa..dcc3b4d6 100644 --- a/pungi/compose.py +++ b/pungi/compose.py @@ -61,6 +61,12 @@ except ImportError: def retry_request(method, url, data=None, auth=None): request_method = getattr(requests, method) rv = request_method(url, json=data, auth=auth) + if rv.status_code >= 400 and rv.status_code < 500: + try: + error = rv.json()["message"] + except ValueError: + error = rv.text + raise RuntimeError("CTS responded with %d: %s" % (rv.status_code, error)) rv.raise_for_status() return rv diff --git a/tests/test_compose.py b/tests/test_compose.py index 44748520..a0070488 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -628,6 +628,7 @@ class ComposeTestCase(unittest.TestCase): ci_copy = dict(self.ci_json) ci_copy["header"]["version"] = "1.2" mocked_response = mock.MagicMock() + mocked_response.status_code = 200 mocked_response.text = json.dumps(self.ci_json) mocked_requests.post.return_value = mocked_response @@ -827,3 +828,17 @@ class RetryRequestTest(unittest.TestCase): ], ) self.assertEqual(rv.status_code, 200) + + @mock.patch("pungi.compose.requests") + def test_no_retry_on_client_error(self, mocked_requests): + mocked_requests.post.side_effect = [ + mock.Mock(status_code=400, json=lambda: {"message": "You made a mistake"}), + ] + url = "http://locahost/api/1/composes/" + with self.assertRaises(RuntimeError): + retry_request("post", url) + + self.assertEqual( + mocked_requests.mock_calls, + [mock.call.post(url, json=None, auth=None)], + )