/
test__helpers.py
438 lines (348 loc) · 14.6 KB
/
test__helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hashlib
import mock
import pytest
from six.moves import http_client
from google.resumable_media import _helpers
from google.resumable_media import common
def test_do_nothing():
ret_val = _helpers.do_nothing()
assert ret_val is None
class Test_header_required(object):
def _success_helper(self, **kwargs):
name = u"some-header"
value = u"The Right Hand Side"
headers = {name: value, u"other-name": u"other-value"}
response = mock.Mock(headers=headers, spec=["headers"])
result = _helpers.header_required(response, name, _get_headers, **kwargs)
assert result == value
def test_success(self):
self._success_helper()
def test_success_with_callback(self):
callback = mock.Mock(spec=[])
self._success_helper(callback=callback)
callback.assert_not_called()
def _failure_helper(self, **kwargs):
response = mock.Mock(headers={}, spec=["headers"])
name = u"any-name"
with pytest.raises(common.InvalidResponse) as exc_info:
_helpers.header_required(response, name, _get_headers, **kwargs)
error = exc_info.value
assert error.response is response
assert len(error.args) == 2
assert error.args[1] == name
def test_failure(self):
self._failure_helper()
def test_failure_with_callback(self):
callback = mock.Mock(spec=[])
self._failure_helper(callback=callback)
callback.assert_called_once_with()
class Test_require_status_code(object):
@staticmethod
def _get_status_code(response):
return response.status_code
def test_success(self):
status_codes = (http_client.OK, http_client.CREATED)
acceptable = (
http_client.OK,
int(http_client.OK),
http_client.CREATED,
int(http_client.CREATED),
)
for value in acceptable:
response = _make_response(value)
status_code = _helpers.require_status_code(
response, status_codes, self._get_status_code
)
assert value == status_code
def test_success_with_callback(self):
status_codes = (http_client.OK,)
response = _make_response(http_client.OK)
callback = mock.Mock(spec=[])
status_code = _helpers.require_status_code(
response, status_codes, self._get_status_code, callback=callback
)
assert status_code == http_client.OK
callback.assert_not_called()
def test_failure(self):
status_codes = (http_client.CREATED, http_client.NO_CONTENT)
response = _make_response(http_client.OK)
with pytest.raises(common.InvalidResponse) as exc_info:
_helpers.require_status_code(response, status_codes, self._get_status_code)
error = exc_info.value
assert error.response is response
assert len(error.args) == 5
assert error.args[1] == response.status_code
assert error.args[3:] == status_codes
def test_failure_with_callback(self):
status_codes = (http_client.OK,)
response = _make_response(http_client.NOT_FOUND)
callback = mock.Mock(spec=[])
with pytest.raises(common.InvalidResponse) as exc_info:
_helpers.require_status_code(
response, status_codes, self._get_status_code, callback=callback
)
error = exc_info.value
assert error.response is response
assert len(error.args) == 4
assert error.args[1] == response.status_code
assert error.args[3:] == status_codes
callback.assert_called_once_with()
class Test_calculate_retry_wait(object):
@mock.patch(u"random.randint", return_value=125)
def test_past_limit(self, randint_mock):
base_wait, wait_time = _helpers.calculate_retry_wait(70.0, 64.0)
assert base_wait == 64.0
assert wait_time == 64.125
randint_mock.assert_called_once_with(0, 1000)
@mock.patch(u"random.randint", return_value=250)
def test_at_limit(self, randint_mock):
base_wait, wait_time = _helpers.calculate_retry_wait(50.0, 50.0)
assert base_wait == 50.0
assert wait_time == 50.25
randint_mock.assert_called_once_with(0, 1000)
@mock.patch(u"random.randint", return_value=875)
def test_under_limit(self, randint_mock):
base_wait, wait_time = _helpers.calculate_retry_wait(16.0, 33.0)
assert base_wait == 32.0
assert wait_time == 32.875
randint_mock.assert_called_once_with(0, 1000)
class Test_wait_and_retry(object):
def test_success_no_retry(self):
truthy = http_client.OK
assert truthy not in _helpers.RETRYABLE
response = _make_response(truthy)
func = mock.Mock(return_value=response, spec=[])
retry_strategy = common.RetryStrategy()
ret_val = _helpers.wait_and_retry(func, _get_status_code, retry_strategy)
assert ret_val is response
func.assert_called_once_with()
@mock.patch(u"time.sleep")
@mock.patch(u"random.randint")
def test_success_with_retry(self, randint_mock, sleep_mock):
randint_mock.side_effect = [125, 625, 375]
status_codes = (
http_client.INTERNAL_SERVER_ERROR,
http_client.BAD_GATEWAY,
http_client.SERVICE_UNAVAILABLE,
http_client.NOT_FOUND,
)
responses = [_make_response(status_code) for status_code in status_codes]
func = mock.Mock(side_effect=responses, spec=[])
retry_strategy = common.RetryStrategy()
ret_val = _helpers.wait_and_retry(func, _get_status_code, retry_strategy)
assert ret_val == responses[-1]
assert status_codes[-1] not in _helpers.RETRYABLE
assert func.call_count == 4
assert func.mock_calls == [mock.call()] * 4
assert randint_mock.call_count == 3
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 3
assert sleep_mock.call_count == 3
sleep_mock.assert_any_call(1.125)
sleep_mock.assert_any_call(2.625)
sleep_mock.assert_any_call(4.375)
@mock.patch(u"time.sleep")
@mock.patch(u"random.randint")
def test_retry_exceeds_max_cumulative(self, randint_mock, sleep_mock):
randint_mock.side_effect = [875, 0, 375, 500, 500, 250, 125]
status_codes = (
http_client.SERVICE_UNAVAILABLE,
http_client.GATEWAY_TIMEOUT,
common.TOO_MANY_REQUESTS,
http_client.INTERNAL_SERVER_ERROR,
http_client.SERVICE_UNAVAILABLE,
http_client.BAD_GATEWAY,
http_client.GATEWAY_TIMEOUT,
common.TOO_MANY_REQUESTS,
)
responses = [_make_response(status_code) for status_code in status_codes]
func = mock.Mock(side_effect=responses, spec=[])
retry_strategy = common.RetryStrategy(max_cumulative_retry=100.0)
ret_val = _helpers.wait_and_retry(func, _get_status_code, retry_strategy)
assert ret_val == responses[-1]
assert status_codes[-1] in _helpers.RETRYABLE
assert func.call_count == 8
assert func.mock_calls == [mock.call()] * 8
assert randint_mock.call_count == 7
assert randint_mock.mock_calls == [mock.call(0, 1000)] * 7
assert sleep_mock.call_count == 7
sleep_mock.assert_any_call(1.875)
sleep_mock.assert_any_call(2.0)
sleep_mock.assert_any_call(4.375)
sleep_mock.assert_any_call(8.5)
sleep_mock.assert_any_call(16.5)
sleep_mock.assert_any_call(32.25)
sleep_mock.assert_any_call(64.125)
def _make_response(status_code):
return mock.Mock(status_code=status_code, spec=["status_code"])
def _get_status_code(response):
return response.status_code
def _get_headers(response):
return response.headers
@pytest.mark.parametrize("checksum", ["md5", "crc32c", None])
def test__get_checksum_object(checksum):
checksum_object = _helpers._get_checksum_object(checksum)
checksum_types = {
"md5": type(hashlib.md5()),
"crc32c": type(_helpers._get_crc32c_object()),
None: type(None),
}
assert isinstance(checksum_object, checksum_types[checksum])
def test__get_checksum_object_invalid():
with pytest.raises(ValueError):
_helpers._get_checksum_object("invalid")
def test_crc32c_throws_import_error():
try:
import builtins
except ImportError:
import __builtin__ as builtins
orig_import = builtins.__import__
# Raises ImportError for name == "crc32c" or name == "crcmod"
def mock_import(name, globals, locals, fromlist, level=None):
raise ImportError
builtins.__import__ = mock_import
try:
with pytest.raises(ImportError):
_helpers._get_crc32c_object()
finally:
builtins.__import__ = orig_import
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_crc32c_warning_on_slow_crcmod():
try:
import builtins
except ImportError:
import __builtin__ as builtins
orig_import = builtins.__import__
# crcmod.crcmod is the only import.
def mock_import(name, globals, locals, fromlist, level):
crcmod = mock.MagicMock()
crcmod._usingExtension = False
return crcmod
builtins.__import__ = mock_import
try:
assert not _helpers._is_fast_crcmod()
finally:
builtins.__import__ = orig_import
def test__DoNothingHash():
do_nothing_hash = _helpers._DoNothingHash()
return_value = do_nothing_hash.update(b"some data")
assert return_value is None
class Test__get_expected_checksum(object):
@pytest.mark.parametrize("template", [u"crc32c={},md5={}", u"crc32c={}, md5={}"])
@pytest.mark.parametrize("checksum", ["md5", "crc32c"])
@mock.patch("google.resumable_media._helpers._LOGGER")
def test__w_header_present(self, _LOGGER, template, checksum):
checksums = {"md5": u"b2twdXNodGhpc2J1dHRvbg==", "crc32c": u"3q2+7w=="}
header_value = template.format(checksums["crc32c"], checksums["md5"])
headers = {_helpers._HASH_HEADER: header_value}
response = _mock_response(headers=headers)
def _get_headers(response):
return response.headers
url = "https://example.com/"
expected_checksum, checksum_obj = _helpers._get_expected_checksum(
response, _get_headers, url, checksum_type=checksum
)
assert expected_checksum == checksums[checksum]
checksum_types = {
"md5": type(hashlib.md5()),
"crc32c": type(_helpers._get_crc32c_object()),
}
assert isinstance(checksum_obj, checksum_types[checksum])
_LOGGER.info.assert_not_called()
@pytest.mark.parametrize("checksum", ["md5", "crc32c"])
@mock.patch("google.resumable_media._helpers._LOGGER")
def test__w_header_missing(self, _LOGGER, checksum):
headers = {}
response = _mock_response(headers=headers)
def _get_headers(response):
return response.headers
url = "https://example.com/"
expected_checksum, checksum_obj = _helpers._get_expected_checksum(
response, _get_headers, url, checksum_type=checksum
)
assert expected_checksum is None
assert isinstance(checksum_obj, _helpers._DoNothingHash)
expected_msg = _helpers._MISSING_CHECKSUM.format(
url, checksum_type=checksum.upper()
)
_LOGGER.info.assert_called_once_with(expected_msg)
class Test__parse_checksum_header(object):
CRC32C_CHECKSUM = u"3q2+7w=="
MD5_CHECKSUM = u"c2l4dGVlbmJ5dGVzbG9uZw=="
def test_empty_value(self):
header_value = None
response = None
md5_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="md5"
)
assert md5_header is None
crc32c_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="crc32c"
)
assert crc32c_header is None
def test_crc32c_only(self):
header_value = u"crc32c={}".format(self.CRC32C_CHECKSUM)
response = None
md5_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="md5"
)
assert md5_header is None
crc32c_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="crc32c"
)
assert crc32c_header == self.CRC32C_CHECKSUM
def test_md5_only(self):
header_value = u"md5={}".format(self.MD5_CHECKSUM)
response = None
md5_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="md5"
)
assert md5_header == self.MD5_CHECKSUM
crc32c_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="crc32c"
)
assert crc32c_header is None
def test_both_crc32c_and_md5(self):
header_value = u"crc32c={},md5={}".format(
self.CRC32C_CHECKSUM, self.MD5_CHECKSUM
)
response = None
md5_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="md5"
)
assert md5_header == self.MD5_CHECKSUM
crc32c_header = _helpers._parse_checksum_header(
header_value, response, checksum_label="crc32c"
)
assert crc32c_header == self.CRC32C_CHECKSUM
def test_md5_multiple_matches(self):
another_checksum = u"eW91IGRpZCBXQVQgbm93Pw=="
header_value = u"md5={},md5={}".format(self.MD5_CHECKSUM, another_checksum)
response = mock.sentinel.response
with pytest.raises(common.InvalidResponse) as exc_info:
_helpers._parse_checksum_header(
header_value, response, checksum_label="md5"
)
error = exc_info.value
assert error.response is response
assert len(error.args) == 3
assert error.args[1] == header_value
assert error.args[2] == [self.MD5_CHECKSUM, another_checksum]
def _mock_response(headers):
return mock.Mock(
headers=headers,
status_code=200,
spec=["status_code", "headers"],
)