| 1 |
import pytest |
| 2 |
import requests |
| 3 |
|
| 4 |
import globus_sdk |
| 5 |
from globus_sdk.testing import RegisteredResponse |
| 6 |
from globus_sdk.transport import RequestCallerInfo, RetryConfig |
| 7 |
|
| 8 |
|
| 9 |
@pytest.mark.parametrize("error_status", [500, 429, 502, 503, 504]) |
| 10 |
def test_retry_on_transient_error(client, mocksleep, error_status): |
| 11 |
RegisteredResponse( |
| 12 |
path="https://foo.api.globus.org/bar", status=error_status, body="Uh-oh!" |
| 13 |
).add() |
| 14 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 15 |
|
| 16 |
# no sign of an error in the client |
| 17 |
res = client.get("/bar") |
| 18 |
assert res.http_status == 200 |
| 19 |
assert res["baz"] == 1 |
| 20 |
|
| 21 |
# there was a sleep (retry was triggered) |
| 22 |
mocksleep.assert_called_once() |
| 23 |
|
| 24 |
|
| 25 |
def test_retry_disabled_via_tune(client, mocksleep): |
| 26 |
RegisteredResponse( |
| 27 |
path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!" |
| 28 |
).add() |
| 29 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 30 |
|
| 31 |
# the error is seen by the client (automatic retry does not hide it) |
| 32 |
with pytest.raises(globus_sdk.GlobusAPIError) as excinfo: |
| 33 |
with client.retry_config.tune(max_retries=0): |
| 34 |
client.get("/bar") |
| 35 |
assert excinfo.value.http_status == 500 |
| 36 |
|
| 37 |
# there was no sleep (retry was not triggered) |
| 38 |
mocksleep.assert_not_called() |
| 39 |
|
| 40 |
|
| 41 |
def test_retry_disabled_via_init_param(client_class, mocksleep): |
| 42 |
RegisteredResponse( |
| 43 |
path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!" |
| 44 |
).add() |
| 45 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 46 |
client = client_class(retry_config=RetryConfig(max_retries=0)) |
| 47 |
|
| 48 |
# the error is seen by the client (automatic retry does not hide it) |
| 49 |
with pytest.raises(globus_sdk.GlobusAPIError) as excinfo: |
| 50 |
client.get("/bar") |
| 51 |
assert excinfo.value.http_status == 500 |
| 52 |
|
| 53 |
# there was no sleep (retry was not triggered) |
| 54 |
mocksleep.assert_not_called() |
| 55 |
|
| 56 |
|
| 57 |
def test_retry_disabled_via_init_param_but_enabled_via_tune(client_class, mocksleep): |
| 58 |
RegisteredResponse( |
| 59 |
path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!" |
| 60 |
).add() |
| 61 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 62 |
client = client_class(retry_config=RetryConfig(max_retries=0)) |
| 63 |
|
| 64 |
# no sign of an error in the client if we "turn it back on" |
| 65 |
with client.retry_config.tune(max_retries=1): |
| 66 |
res = client.get("/bar") |
| 67 |
assert res.http_status == 200 |
| 68 |
assert res["baz"] == 1 |
| 69 |
|
| 70 |
# there was a sleep (retry was triggered) |
| 71 |
mocksleep.assert_called_once() |
| 72 |
|
| 73 |
|
| 74 |
def test_retry_on_network_error(client, mocksleep): |
| 75 |
# set the response to be a requests NetworkError -- responses will raise the |
| 76 |
# exception when the call is made |
| 77 |
RegisteredResponse( |
| 78 |
path="https://foo.api.globus.org/bar", |
| 79 |
body=requests.ConnectionError("foo-err"), |
| 80 |
).add() |
| 81 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 82 |
|
| 83 |
# no sign of an error in the client |
| 84 |
res = client.get("/bar") |
| 85 |
assert res.http_status == 200 |
| 86 |
assert res["baz"] == 1 |
| 87 |
|
| 88 |
# there was a sleep (retry was triggered) |
| 89 |
mocksleep.assert_called_once() |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.parametrize("num_errors,expect_err", [(5, False), (6, True), (7, True)]) |
| 93 |
def test_retry_limit(client, mocksleep, num_errors, expect_err): |
| 94 |
# N errors followed by a success |
| 95 |
for _i in range(num_errors): |
| 96 |
RegisteredResponse( |
| 97 |
path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!" |
| 98 |
).add() |
| 99 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 100 |
|
| 101 |
if expect_err: |
| 102 |
with pytest.raises(globus_sdk.GlobusAPIError): |
| 103 |
client.get("/bar") |
| 104 |
else: |
| 105 |
# no sign of an error in the client |
| 106 |
res = client.get("/bar") |
| 107 |
assert res.http_status == 200 |
| 108 |
assert res["baz"] == 1 |
| 109 |
|
| 110 |
# default num retries = 5 |
| 111 |
assert mocksleep.call_count == min(num_errors, 5) |
| 112 |
|
| 113 |
|
| 114 |
def test_transport_retry_limit(client, mocksleep): |
| 115 |
# this limit is a safety to protect against a bad policy causing infinite retries |
| 116 |
client.retry_config.max_retries = 2 |
| 117 |
|
| 118 |
for _i in range(3): |
| 119 |
RegisteredResponse( |
| 120 |
path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!" |
| 121 |
).add() |
| 122 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 123 |
|
| 124 |
with pytest.raises(globus_sdk.GlobusAPIError): |
| 125 |
client.get("/bar") |
| 126 |
|
| 127 |
assert mocksleep.call_count == 2 |
| 128 |
|
| 129 |
|
| 130 |
def test_bad_max_retries_causes_error(client): |
| 131 |
# this test exploits the fact that we loop to (max_retries + 1) in order |
| 132 |
# to ensure that no requests are ever sent |
| 133 |
# the transport should throw an error in this case, since it doesn't have a response |
| 134 |
# value to return |
| 135 |
client.retry_config.max_retries = -1 |
| 136 |
|
| 137 |
with pytest.raises(ValueError): |
| 138 |
client.get("/bar") |
| 139 |
|
| 140 |
|
| 141 |
def test_persistent_connection_error(client): |
| 142 |
for _i in range(6): |
| 143 |
RegisteredResponse( |
| 144 |
path="https://foo.api.globus.org/bar", |
| 145 |
body=requests.ConnectionError("foo-err"), |
| 146 |
).add() |
| 147 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 148 |
|
| 149 |
with pytest.raises(globus_sdk.GlobusConnectionError): |
| 150 |
client.get("/bar") |
| 151 |
|
| 152 |
|
| 153 |
def test_no_retry_401_no_authorizer(client): |
| 154 |
RegisteredResponse( |
| 155 |
path="https://foo.api.globus.org/bar", status=401, body="Unauthorized" |
| 156 |
).add() |
| 157 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 158 |
|
| 159 |
# error gets raised in client (no retry) |
| 160 |
with pytest.raises(globus_sdk.GlobusAPIError) as excinfo: |
| 161 |
client.get("/bar") |
| 162 |
assert excinfo.value.http_status == 401 |
| 163 |
|
| 164 |
|
| 165 |
def test_retry_with_authorizer(client): |
| 166 |
RegisteredResponse( |
| 167 |
path="https://foo.api.globus.org/bar", status=401, body="Unauthorized" |
| 168 |
).add() |
| 169 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 170 |
|
| 171 |
# an authorizer class which does nothing but claims to support handling of |
| 172 |
# unauthorized errors |
| 173 |
dummy_authz_calls = [] |
| 174 |
|
| 175 |
class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer): |
| 176 |
def get_authorization_header(self): |
| 177 |
dummy_authz_calls.append("set_authz") |
| 178 |
return "foo" |
| 179 |
|
| 180 |
def handle_missing_authorization(self): |
| 181 |
dummy_authz_calls.append("handle_missing") |
| 182 |
return True |
| 183 |
|
| 184 |
authorizer = DummyAuthorizer() |
| 185 |
client.authorizer = authorizer |
| 186 |
|
| 187 |
# no sign of an error in the client |
| 188 |
res = client.get("/bar") |
| 189 |
assert res.http_status == 200 |
| 190 |
assert res["baz"] == 1 |
| 191 |
|
| 192 |
# ensure that setting authz was called twice (once for each request) |
| 193 |
# and that between the two calls, handle_missing_authorization was called once |
| 194 |
assert dummy_authz_calls == ["set_authz", "handle_missing", "set_authz"] |
| 195 |
|
| 196 |
|
| 197 |
def test_no_retry_with_authorizer_no_handler(client): |
| 198 |
RegisteredResponse( |
| 199 |
path="https://foo.api.globus.org/bar", status=401, body="Unauthorized" |
| 200 |
).add() |
| 201 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 202 |
|
| 203 |
# an authorizer class which does nothing and does not claim to handle |
| 204 |
# unauthorized errors |
| 205 |
dummy_authz_calls = [] |
| 206 |
|
| 207 |
class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer): |
| 208 |
def get_authorization_header(self): |
| 209 |
dummy_authz_calls.append("set_authz") |
| 210 |
return "foo" |
| 211 |
|
| 212 |
def handle_missing_authorization(self): |
| 213 |
dummy_authz_calls.append("handle_missing") |
| 214 |
return False |
| 215 |
|
| 216 |
authorizer = DummyAuthorizer() |
| 217 |
client.authorizer = authorizer |
| 218 |
|
| 219 |
# error gets raised in client (no retry) |
| 220 |
with pytest.raises(globus_sdk.GlobusAPIError) as excinfo: |
| 221 |
client.get("/bar") |
| 222 |
assert excinfo.value.http_status == 401 |
| 223 |
|
| 224 |
# only two calls, single setting of authz and a call to ask about handling the error |
| 225 |
assert dummy_authz_calls == ["set_authz", "handle_missing"] |
| 226 |
|
| 227 |
|
| 228 |
def test_retry_with_authorizer_persistent_401(client): |
| 229 |
RegisteredResponse( |
| 230 |
path="https://foo.api.globus.org/bar", status=401, body="Unauthorized" |
| 231 |
).add() |
| 232 |
RegisteredResponse( |
| 233 |
path="https://foo.api.globus.org/bar", status=401, body="Unauthorized" |
| 234 |
).add() |
| 235 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 236 |
|
| 237 |
# an authorizer class which does nothing but claims to support handling of |
| 238 |
# unauthorized errors |
| 239 |
dummy_authz_calls = [] |
| 240 |
|
| 241 |
class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer): |
| 242 |
def get_authorization_header(self): |
| 243 |
dummy_authz_calls.append("set_authz") |
| 244 |
return "foo" |
| 245 |
|
| 246 |
def handle_missing_authorization(self): |
| 247 |
dummy_authz_calls.append("handle_missing") |
| 248 |
return True |
| 249 |
|
| 250 |
authorizer = DummyAuthorizer() |
| 251 |
client.authorizer = authorizer |
| 252 |
|
| 253 |
# the error gets raised in this case because it persists -- the authorizer only gets |
| 254 |
# one chance to resolve the issue |
| 255 |
with pytest.raises(globus_sdk.GlobusAPIError) as excinfo: |
| 256 |
client.get("/bar") |
| 257 |
assert excinfo.value.http_status == 401 |
| 258 |
|
| 259 |
# ensure that setting authz was called twice (once for each request) |
| 260 |
# and that between the two calls, handle_missing_authorization was called once |
| 261 |
# but the handler should not be called a second time because the 401 repeated |
| 262 |
assert dummy_authz_calls == ["set_authz", "handle_missing", "set_authz"] |
| 263 |
|
| 264 |
|
| 265 |
def test_transport_caller_info_with_retry(client): |
| 266 |
RegisteredResponse( |
| 267 |
path="https://foo.api.globus.org/bar", status=401, body="Unauthorized" |
| 268 |
).add() |
| 269 |
RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add() |
| 270 |
|
| 271 |
dummy_authz_calls = [] |
| 272 |
|
| 273 |
class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer): |
| 274 |
def get_authorization_header(self): |
| 275 |
dummy_authz_calls.append("set_authz") |
| 276 |
return "foo" |
| 277 |
|
| 278 |
def handle_missing_authorization(self): |
| 279 |
dummy_authz_calls.append("handle_missing") |
| 280 |
return True |
| 281 |
|
| 282 |
authorizer = DummyAuthorizer() |
| 283 |
caller_info = RequestCallerInfo( |
| 284 |
retry_config=client.retry_config, authorizer=authorizer |
| 285 |
) |
| 286 |
|
| 287 |
# Test direct transport usage with caller_info |
| 288 |
response = client.transport.request( |
| 289 |
"GET", "https://foo.api.globus.org/bar", caller_info=caller_info |
| 290 |
) |
| 291 |
|
| 292 |
assert response.status_code == 200 |
| 293 |
# Verify that the authorizer was used for both authorization and retry handling |
| 294 |
assert dummy_authz_calls == ["set_authz", "handle_missing", "set_authz"] |