Retry in ProxyError during post inspector data

* ProxyError is derived from ConnectionError, but it's necessary
to check the Response object to identify.

- Added ProxyError in retry_if_exception_type
- Updated _post_to_inspector to proper handle ProxyError
- Updated the wait to use wait_exponential instead of wait_fixed.

Closes-Bug: 2045429
Change-Id: Iefe3fe581cd4e7c91a0da708e6f6d0fdaacab6fe
This commit is contained in:
Iury Gregory Melo Ferreira 2023-12-01 18:40:09 -03:00
parent beccfe8c92
commit 801da9ec1f
3 changed files with 42 additions and 5 deletions

View File

@ -134,15 +134,20 @@ def call_inspector(data, failures):
@tenacity.retry( @tenacity.retry(
retry=tenacity.retry_if_exception_type( retry=tenacity.retry_if_exception_type(
requests.exceptions.ConnectionError), (requests.exceptions.ConnectionError,
requests.exceptions.HTTPError)),
stop=tenacity.stop_after_attempt(_RETRY_ATTEMPTS), stop=tenacity.stop_after_attempt(_RETRY_ATTEMPTS),
wait=tenacity.wait_fixed(_RETRY_WAIT), wait=tenacity.wait_exponential(multiplier=1.5,
min=_RETRY_WAIT, max=30),
reraise=True) reraise=True)
def _post_to_inspector(): def _post_to_inspector():
return requests.post( inspector_resp = requests.post(
CONF.inspection_callback_url, data=data, CONF.inspection_callback_url, data=data,
verify=verify, cert=cert, verify=verify, cert=cert, timeout=CONF.http_request_timeout)
timeout=CONF.http_request_timeout) if inspector_resp.status_code >= 500:
raise requests.exceptions.HTTPError(response=inspector_resp)
return inspector_resp
resp = _post_to_inspector() resp = _post_to_inspector()
if resp.status_code >= 400: if resp.status_code >= 400:

View File

@ -204,6 +204,34 @@ class TestCallInspector(base.IronicAgentTest):
data, failures) data, failures)
self.assertEqual(5, mock_post.call_count) self.assertEqual(5, mock_post.call_count)
@mock.patch.object(inspector, '_RETRY_WAIT', 0.01)
@mock.patch.object(inspector, '_RETRY_ATTEMPTS', 3)
def test_inspector_retries_on_50X_error(self, mock_post):
mock_post.side_effect = [mock.Mock(status_code=500),
mock.Mock(status_code=501),
mock.Mock(status_code=502)]
failures = utils.AccumulatedFailures()
data = collections.OrderedDict(data=42)
self.assertRaises(requests.exceptions.HTTPError,
inspector.call_inspector,
data, failures)
self.assertEqual(3, mock_post.call_count)
@mock.patch.object(inspector, '_RETRY_WAIT', 0.01)
@mock.patch.object(inspector, '_RETRY_ATTEMPTS', 2)
def test_inspector_retry_on_50X_and_succeed(self, mock_post):
mock_post.side_effect = [mock.Mock(status_code=503),
mock.Mock(status_code=200)]
failures = utils.AccumulatedFailures()
data = collections.OrderedDict(data=42)
inspector.call_inspector(data, failures)
self.assertEqual(2, mock_post.call_count)
mock_post.assert_called_with('url',
cert=None, verify=True,
data='{"data": 42, "error": null}',
timeout=30)
class BaseDiscoverTest(base.IronicAgentTest): class BaseDiscoverTest(base.IronicAgentTest):
def setUp(self): def setUp(self):

View File

@ -0,0 +1,4 @@
---
fixes:
- |
Fixes the post data to inspector to retry in 50X errors.