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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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 fnmatch
import io
import threading
import time
from concurrent.futures import Future
from botocore.session import Session
from s3transfer.subscribers import BaseSubscriber
from __tests__ import (
HAS_CRT,
FileCreator,
NonSeekableReader,
NonSeekableWriter,
mock,
requires_crt,
unittest,
)
if HAS_CRT:
import awscrt
import s3transfer.crt
class submitThread(threading.Thread):
def __init__(self, transfer_manager, futures, callargs):
threading.Thread.__init__(self)
self._transfer_manager = transfer_manager
self._futures = futures
self._callargs = callargs
def run(self):
self._futures.append(self._transfer_manager.download(*self._callargs))
class RecordingSubscriber(BaseSubscriber):
def __init__(self):
self.on_queued_called = False
self.on_done_called = False
self.bytes_transferred = 0
self.on_queued_future = None
self.on_done_future = None
def on_queued(self, future, **kwargs):
self.on_queued_called = True
self.on_queued_future = future
def on_done(self, future, **kwargs):
self.on_done_called = True
self.on_done_future = future
@requires_crt
class TestCRTTransferManager(unittest.TestCase):
def setUp(self):
self.region = 'us-west-2'
self.bucket = "test_bucket"
self.key = "test_key"
self.expected_content = b'my content'
self.expected_download_content = b'new content'
self.files = FileCreator()
self.filename = self.files.create_file(
'myfile', self.expected_content, mode='wb'
)
self.expected_path = "/" + self.bucket + "/" + self.key
self.expected_host = "s3.%s.amazonaws.com" % (self.region)
self.s3_request = mock.Mock(awscrt.s3.S3Request)
self.s3_crt_client = mock.Mock(awscrt.s3.S3Client)
self.s3_crt_client.make_request.side_effect = (
self._simulate_make_request_side_effect
)
self.session = Session()
self.session.set_config_variable('region', self.region)
self.request_serializer = s3transfer.crt.BotocoreCRTRequestSerializer(
self.session
)
self.transfer_manager = s3transfer.crt.CRTTransferManager(
crt_s3_client=self.s3_crt_client,
crt_request_serializer=self.request_serializer,
)
self.record_subscriber = RecordingSubscriber()
def tearDown(self):
self.files.remove_all()
def _assert_expected_crt_http_request(
self,
crt_http_request,
expected_http_method='GET',
expected_host=None,
expected_path=None,
expected_body_content=None,
expected_content_length=None,
expected_missing_headers=None,
):
if expected_host is None:
expected_host = self.expected_host
if expected_path is None:
expected_path = self.expected_path
self.assertEqual(crt_http_request.method, expected_http_method)
self.assertEqual(crt_http_request.headers.get("host"), expected_host)
self.assertEqual(crt_http_request.path, expected_path)
if expected_body_content is not None:
# Note: The underlying CRT awscrt.io.InputStream does not expose
# a public read method so we have to reach into the private,
# underlying stream to determine the content. We should update
# to use a public interface if a public interface is ever exposed.
self.assertEqual(
crt_http_request.body_stream._stream.read(),
expected_body_content,
)
if expected_content_length is not None:
self.assertEqual(
crt_http_request.headers.get('Content-Length'),
str(expected_content_length),
)
if expected_missing_headers is not None:
header_names = [
header[0].lower() for header in crt_http_request.headers
]
for expected_missing_header in expected_missing_headers:
self.assertNotIn(expected_missing_header.lower(), header_names)
def _assert_subscribers_called(self, expected_future=None):
self.assertTrue(self.record_subscriber.on_queued_called)
self.assertTrue(self.record_subscriber.on_done_called)
if expected_future:
self.assertIs(
self.record_subscriber.on_queued_future, expected_future
)
self.assertIs(
self.record_subscriber.on_done_future, expected_future
)
def _get_expected_upload_checksum_config(self, **overrides):
checksum_config_kwargs = {
'algorithm': awscrt.s3.S3ChecksumAlgorithm.CRC32,
'location': awscrt.s3.S3ChecksumLocation.TRAILER,
}
checksum_config_kwargs.update(overrides)
return awscrt.s3.S3ChecksumConfig(**checksum_config_kwargs)
def _get_expected_download_checksum_config(self, **overrides):
checksum_config_kwargs = {
'validate_response': True,
}
checksum_config_kwargs.update(overrides)
return awscrt.s3.S3ChecksumConfig(**checksum_config_kwargs)
def _invoke_done_callbacks(self, **kwargs):
callargs = self.s3_crt_client.make_request.call_args
callargs_kwargs = callargs[1]
on_done = callargs_kwargs["on_done"]
on_done(error=None)
def _simulate_file_download(self, recv_filepath):
self.files.create_file(
recv_filepath, self.expected_download_content, mode='wb'
)
def _simulate_on_body_download(self, on_body_callback):
on_body_callback(chunk=self.expected_download_content, offset=0)
def _simulate_make_request_side_effect(self, **kwargs):
if kwargs.get('recv_filepath'):
self._simulate_file_download(kwargs['recv_filepath'])
if kwargs.get('on_body'):
self._simulate_on_body_download(kwargs['on_body'])
self._invoke_done_callbacks()
return self.s3_request
def test_upload(self):
future = self.transfer_manager.upload(
self.filename, self.bucket, self.key, {}, [self.record_subscriber]
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.PUT_OBJECT,
'send_filepath': self.filename,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'checksum_config': self._get_expected_upload_checksum_config(),
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='PUT',
expected_content_length=len(self.expected_content),
expected_missing_headers=['Content-MD5'],
)
self._assert_subscribers_called(future)
def test_upload_from_seekable_stream(self):
with open(self.filename, 'rb') as f:
future = self.transfer_manager.upload(
f, self.bucket, self.key, {}, [self.record_subscriber]
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.PUT_OBJECT,
'send_filepath': None,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'checksum_config': self._get_expected_upload_checksum_config(),
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='PUT',
expected_body_content=self.expected_content,
expected_content_length=len(self.expected_content),
expected_missing_headers=['Content-MD5'],
)
self._assert_subscribers_called(future)
def test_upload_from_nonseekable_stream(self):
nonseekable_stream = NonSeekableReader(self.expected_content)
future = self.transfer_manager.upload(
nonseekable_stream,
self.bucket,
self.key,
{},
[self.record_subscriber],
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.PUT_OBJECT,
'send_filepath': None,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'checksum_config': self._get_expected_upload_checksum_config(),
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='PUT',
expected_body_content=self.expected_content,
expected_missing_headers=[
'Content-MD5',
'Content-Length',
'Transfer-Encoding',
],
)
self._assert_subscribers_called(future)
def test_upload_override_checksum_algorithm(self):
future = self.transfer_manager.upload(
self.filename,
self.bucket,
self.key,
{'ChecksumAlgorithm': 'CRC32C'},
[self.record_subscriber],
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.PUT_OBJECT,
'send_filepath': self.filename,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'checksum_config': self._get_expected_upload_checksum_config(
algorithm=awscrt.s3.S3ChecksumAlgorithm.CRC32C
),
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='PUT',
expected_content_length=len(self.expected_content),
expected_missing_headers=[
'Content-MD5',
'x-amz-sdk-checksum-algorithm',
'X-Amz-Trailer',
],
)
self._assert_subscribers_called(future)
def test_upload_override_checksum_algorithm_accepts_lowercase(self):
future = self.transfer_manager.upload(
self.filename,
self.bucket,
self.key,
{'ChecksumAlgorithm': 'crc32c'},
[self.record_subscriber],
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.PUT_OBJECT,
'send_filepath': self.filename,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'checksum_config': self._get_expected_upload_checksum_config(
algorithm=awscrt.s3.S3ChecksumAlgorithm.CRC32C
),
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='PUT',
expected_content_length=len(self.expected_content),
expected_missing_headers=[
'Content-MD5',
'x-amz-sdk-checksum-algorithm',
'X-Amz-Trailer',
],
)
self._assert_subscribers_called(future)
def test_upload_throws_error_for_unsupported_checksum(self):
with self.assertRaisesRegex(
ValueError, 'ChecksumAlgorithm: UNSUPPORTED not supported'
):
self.transfer_manager.upload(
self.filename,
self.bucket,
self.key,
{'ChecksumAlgorithm': 'UNSUPPORTED'},
[self.record_subscriber],
)
def test_download(self):
future = self.transfer_manager.download(
self.bucket, self.key, self.filename, {}, [self.record_subscriber]
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.GET_OBJECT,
'recv_filepath': mock.ANY,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'on_body': None,
'checksum_config': self._get_expected_download_checksum_config(),
},
)
# the recv_filepath will be set to a temporary file path with some
# random suffix
self.assertTrue(
fnmatch.fnmatch(
callargs_kwargs["recv_filepath"],
f'{self.filename}.*',
)
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='GET',
expected_content_length=0,
)
self._assert_subscribers_called(future)
with open(self.filename, 'rb') as f:
# Check the fake response overwrites the file because of download
self.assertEqual(f.read(), self.expected_download_content)
def test_download_to_seekable_stream(self):
with open(self.filename, 'wb') as f:
future = self.transfer_manager.download(
self.bucket, self.key, f, {}, [self.record_subscriber]
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.GET_OBJECT,
'recv_filepath': None,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'on_body': mock.ANY,
'checksum_config': self._get_expected_download_checksum_config(),
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='GET',
expected_content_length=0,
)
self._assert_subscribers_called(future)
with open(self.filename, 'rb') as f:
# Check the fake response overwrites the file because of download
self.assertEqual(f.read(), self.expected_download_content)
def test_download_to_nonseekable_stream(self):
underlying_stream = io.BytesIO()
nonseekable_stream = NonSeekableWriter(underlying_stream)
future = self.transfer_manager.download(
self.bucket,
self.key,
nonseekable_stream,
{},
[self.record_subscriber],
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.GET_OBJECT,
'recv_filepath': None,
'on_progress': mock.ANY,
'on_done': mock.ANY,
'on_body': mock.ANY,
'checksum_config': self._get_expected_download_checksum_config(),
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='GET',
expected_content_length=0,
)
self._assert_subscribers_called(future)
self.assertEqual(
underlying_stream.getvalue(), self.expected_download_content
)
def test_delete(self):
future = self.transfer_manager.delete(
self.bucket, self.key, {}, [self.record_subscriber]
)
future.result()
callargs_kwargs = self.s3_crt_client.make_request.call_args[1]
self.assertEqual(
callargs_kwargs,
{
'request': mock.ANY,
'type': awscrt.s3.S3RequestType.DEFAULT,
'on_progress': mock.ANY,
'on_done': mock.ANY,
},
)
self._assert_expected_crt_http_request(
callargs_kwargs["request"],
expected_http_method='DELETE',
expected_content_length=0,
)
self._assert_subscribers_called(future)
def test_blocks_when_max_requests_processes_reached(self):
self.s3_crt_client.make_request.return_value = self.s3_request
# We simulate blocking by not invoking the on_done callbacks for
# all of the requests we send. The default side effect invokes all
# callbacks so we need to unset the side effect to avoid on_done from
# being called in the child threads.
self.s3_crt_client.make_request.side_effect = None
futures = []
callargs = (self.bucket, self.key, self.filename, {}, [])
max_request_processes = 128 # the hard coded max processes
all_concurrent = max_request_processes + 1
threads = []
for i in range(0, all_concurrent):
thread = submitThread(self.transfer_manager, futures, callargs)
thread.start()
threads.append(thread)
# Sleep until the expected max requests has been reached
while len(futures) < max_request_processes:
time.sleep(0.05)
self.assertLessEqual(
self.s3_crt_client.make_request.call_count, max_request_processes
)
# Release lock
callargs = self.s3_crt_client.make_request.call_args
callargs_kwargs = callargs[1]
on_done = callargs_kwargs["on_done"]
on_done(error=None)
for thread in threads:
thread.join()
self.assertEqual(
self.s3_crt_client.make_request.call_count, all_concurrent
)
def _cancel_function(self):
self.cancel_called = True
self.s3_request.finished_future.set_exception(
awscrt.exceptions.from_code(0)
)
self._invoke_done_callbacks()
def test_cancel(self):
self.s3_request.finished_future = Future()
self.cancel_called = False
self.s3_request.cancel = self._cancel_function
try:
with self.transfer_manager:
future = self.transfer_manager.upload(
self.filename, self.bucket, self.key, {}, []
)
raise KeyboardInterrupt()
except KeyboardInterrupt:
pass
with self.assertRaises(awscrt.exceptions.AwsCrtError):
future.result()
self.assertTrue(self.cancel_called)
def test_serializer_error_handling(self):
class SerializationException(Exception):
pass
class ExceptionRaisingSerializer(
s3transfer.crt.BaseCRTRequestSerializer
):
def serialize_http_request(self, transfer_type, future):
raise SerializationException()
not_impl_serializer = ExceptionRaisingSerializer()
transfer_manager = s3transfer.crt.CRTTransferManager(
crt_s3_client=self.s3_crt_client,
crt_request_serializer=not_impl_serializer,
)
future = transfer_manager.upload(
self.filename, self.bucket, self.key, {}, []
)
with self.assertRaises(SerializationException):
future.result()
def test_crt_s3_client_error_handling(self):
self.s3_crt_client.make_request.side_effect = (
awscrt.exceptions.from_code(0)
)
future = self.transfer_manager.upload(
self.filename, self.bucket, self.key, {}, []
)
with self.assertRaises(awscrt.exceptions.AwsCrtError):
future.result()
|