Skip to content

Commit

Permalink
Merge pull request #47 from uc-cdis/chore/403
Browse files Browse the repository at this point in the history
chore(403): handle 403 response
  • Loading branch information
philloooo authored Nov 29, 2018
2 parents 09f7571 + e51126c commit 51b88d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cirrus/google_cloud/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def log_backoff_giveup(details):


def _is_handled_exception(e):
if isinstance(e, HttpError):
if e.resp.status == 403:
return True
return False

return isinstance(e, CirrusError)


Expand Down Expand Up @@ -1387,6 +1392,8 @@ def _authed_request(self, method, url, data=""):
else:
raise CirrusError("Unsupported method: " + str(method) + ".")

if response.status_code == 403:
raise GoogleAPIError("Call to {} was forbidden".format(url))
response.raise_for_status()
return response

Expand Down
30 changes: 30 additions & 0 deletions test/test_google_cloud_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from googleapiclient.errors import HttpError
import pytest
from requests import Response
import httplib2

from cirrus.google_cloud import (
COMPUTE_ENGINE_DEFAULT_SERVICE_ACCOUNT,
Expand Down Expand Up @@ -1282,6 +1283,35 @@ def test_handled_exception_no_retry(test_cloud_manager):
assert logger_error.call_count <= 1


def test_handled_exception_403_no_retry(test_cloud_manager):
"""
Test that when a handled exception is raised (e.g. a cirrus error), we
do NOT retry the Google API call
"""
from cirrus.google_cloud.manager import BACKOFF_SETTINGS

response = httplib2.Response({"status": "403", "content-type": "application/json"})
http_error = HttpError(resp=response, content="")
mock_config = {"get.side_effect": http_error}
test_cloud_manager._authed_session.configure_mock(**mock_config)
warn = cirrus.google_cloud.manager.logger.warn
error = cirrus.google_cloud.manager.logger.error
with mock.patch(
"cirrus.google_cloud.manager.logger.warn"
) as logger_warn, mock.patch(
"cirrus.google_cloud.manager.logger.error"
) as logger_error:
# keep the side effect to actually put logs, so you can see the format with `-s`
logger_warn.side_effect = warn
logger_error.side_effect = error
with pytest.raises(HttpError):
test_cloud_manager.get_service_account_type(
account="[email protected]"
)
assert logger_warn.call_count == 0
assert logger_error.call_count == 1


def test_unhandled_exception_retry(test_cloud_manager):
"""
Test that when an unhandled exception is raised,
Expand Down

0 comments on commit 51b88d9

Please sign in to comment.