Report errors from CTS

If the service returns a status code indicating a user error, report
that and do not retry.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
(cherry picked from commit dd8c1002d4)
This commit is contained in:
Lubomír Sedlář 2022-11-23 10:26:28 +01:00 committed by soksanichenko
parent 6c1c9d9efd
commit d17e578645
2 changed files with 21 additions and 0 deletions

View File

@ -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

View File

@ -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)],
)