retry on authentication failure in api_client

Due to eventlet threading weirdness, it appears that sometimes a request
that is destined for the compute api fixture ends up on the placement
api fixture. The latter uses a no auth middleware that 401s when it does
not get a token.

The change retries the auth request a few times before failing.

If this has no effect, doing an eventlet.sleep() before the retry might
be something worth trying.

Change-Id: I9ca2022ca956be5ea67f4c40bed80115f2fae29d
Related-Bug: #1705753
This commit is contained in:
Chris Dent 2017-07-21 19:13:43 +01:00
parent e62d931025
commit 068f85dc38
1 changed files with 9 additions and 2 deletions

View File

@ -134,7 +134,7 @@ class TestOpenStackClient(object):
response = requests.request(method, url, data=body, headers=_headers)
return response
def _authenticate(self):
def _authenticate(self, retry_count=0):
if self.auth_result:
return self.auth_result
@ -149,8 +149,15 @@ class TestOpenStackClient(object):
LOG.debug("%(auth_uri)s => code %(http_status)s",
{'auth_uri': auth_uri, 'http_status': http_status})
# NOTE(cdent): This is a workaround for an issue where the placement
# API fixture may respond when a request was supposed to go to the
# compute API fixture. Retry a few times, hoping to hit the right
# fixture.
if http_status == 401:
raise OpenStackApiAuthenticationException(response=response)
if retry_count <= 3:
return self._authenticate(retry_count=retry_count + 1)
else:
raise OpenStackApiAuthenticationException(response=response)
self.auth_result = response.headers
return self.auth_result