test_crt.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://aws.amazon.com/apache2.0/
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. from botocore.credentials import CredentialResolver, ReadOnlyCredentials
  14. from botocore.session import Session
  15. from s3transfer.exceptions import TransferNotDoneError
  16. from s3transfer.utils import CallArgs
  17. from tests import HAS_CRT, FileCreator, mock, requires_crt, unittest
  18. if HAS_CRT:
  19. import awscrt.s3
  20. import s3transfer.crt
  21. class CustomFutureException(Exception):
  22. pass
  23. @requires_crt
  24. class TestBotocoreCRTRequestSerializer(unittest.TestCase):
  25. def setUp(self):
  26. self.region = 'us-west-2'
  27. self.session = Session()
  28. self.session.set_config_variable('region', self.region)
  29. self.request_serializer = s3transfer.crt.BotocoreCRTRequestSerializer(
  30. self.session
  31. )
  32. self.bucket = "test_bucket"
  33. self.key = "test_key"
  34. self.files = FileCreator()
  35. self.filename = self.files.create_file('myfile', 'my content')
  36. self.expected_path = "/" + self.bucket + "/" + self.key
  37. self.expected_host = "s3.%s.amazonaws.com" % (self.region)
  38. def tearDown(self):
  39. self.files.remove_all()
  40. def test_upload_request(self):
  41. callargs = CallArgs(
  42. bucket=self.bucket,
  43. key=self.key,
  44. fileobj=self.filename,
  45. extra_args={},
  46. subscribers=[],
  47. )
  48. coordinator = s3transfer.crt.CRTTransferCoordinator()
  49. future = s3transfer.crt.CRTTransferFuture(
  50. s3transfer.crt.CRTTransferMeta(call_args=callargs), coordinator
  51. )
  52. crt_request = self.request_serializer.serialize_http_request(
  53. "put_object", future
  54. )
  55. self.assertEqual("PUT", crt_request.method)
  56. self.assertEqual(self.expected_path, crt_request.path)
  57. self.assertEqual(self.expected_host, crt_request.headers.get("host"))
  58. self.assertIsNone(crt_request.headers.get("Authorization"))
  59. def test_download_request(self):
  60. callargs = CallArgs(
  61. bucket=self.bucket,
  62. key=self.key,
  63. fileobj=self.filename,
  64. extra_args={},
  65. subscribers=[],
  66. )
  67. coordinator = s3transfer.crt.CRTTransferCoordinator()
  68. future = s3transfer.crt.CRTTransferFuture(
  69. s3transfer.crt.CRTTransferMeta(call_args=callargs), coordinator
  70. )
  71. crt_request = self.request_serializer.serialize_http_request(
  72. "get_object", future
  73. )
  74. self.assertEqual("GET", crt_request.method)
  75. self.assertEqual(self.expected_path, crt_request.path)
  76. self.assertEqual(self.expected_host, crt_request.headers.get("host"))
  77. self.assertIsNone(crt_request.headers.get("Authorization"))
  78. def test_delete_request(self):
  79. callargs = CallArgs(
  80. bucket=self.bucket, key=self.key, extra_args={}, subscribers=[]
  81. )
  82. coordinator = s3transfer.crt.CRTTransferCoordinator()
  83. future = s3transfer.crt.CRTTransferFuture(
  84. s3transfer.crt.CRTTransferMeta(call_args=callargs), coordinator
  85. )
  86. crt_request = self.request_serializer.serialize_http_request(
  87. "delete_object", future
  88. )
  89. self.assertEqual("DELETE", crt_request.method)
  90. self.assertEqual(self.expected_path, crt_request.path)
  91. self.assertEqual(self.expected_host, crt_request.headers.get("host"))
  92. self.assertIsNone(crt_request.headers.get("Authorization"))
  93. @requires_crt
  94. class TestCRTCredentialProviderAdapter(unittest.TestCase):
  95. def setUp(self):
  96. self.botocore_credential_provider = mock.Mock(CredentialResolver)
  97. self.access_key = "access_key"
  98. self.secret_key = "secret_key"
  99. self.token = "token"
  100. self.botocore_credential_provider.load_credentials.return_value.get_frozen_credentials.return_value = ReadOnlyCredentials(
  101. self.access_key, self.secret_key, self.token
  102. )
  103. def _call_adapter_and_check(self, credentails_provider_adapter):
  104. credentials = credentails_provider_adapter()
  105. self.assertEqual(credentials.access_key_id, self.access_key)
  106. self.assertEqual(credentials.secret_access_key, self.secret_key)
  107. self.assertEqual(credentials.session_token, self.token)
  108. def test_fetch_crt_credentials_successfully(self):
  109. credentails_provider_adapter = (
  110. s3transfer.crt.CRTCredentialProviderAdapter(
  111. self.botocore_credential_provider
  112. )
  113. )
  114. self._call_adapter_and_check(credentails_provider_adapter)
  115. def test_load_credentials_once(self):
  116. credentails_provider_adapter = (
  117. s3transfer.crt.CRTCredentialProviderAdapter(
  118. self.botocore_credential_provider
  119. )
  120. )
  121. called_times = 5
  122. for i in range(called_times):
  123. self._call_adapter_and_check(credentails_provider_adapter)
  124. # Assert that the load_credentails of botocore credential provider
  125. # will only be called once
  126. self.assertEqual(
  127. self.botocore_credential_provider.load_credentials.call_count, 1
  128. )
  129. @requires_crt
  130. class TestCRTTransferFuture(unittest.TestCase):
  131. def setUp(self):
  132. self.mock_s3_request = mock.Mock(awscrt.s3.S3RequestType)
  133. self.mock_crt_future = mock.Mock(awscrt.s3.Future)
  134. self.mock_s3_request.finished_future = self.mock_crt_future
  135. self.coordinator = s3transfer.crt.CRTTransferCoordinator()
  136. self.coordinator.set_s3_request(self.mock_s3_request)
  137. self.future = s3transfer.crt.CRTTransferFuture(
  138. coordinator=self.coordinator
  139. )
  140. def test_set_exception(self):
  141. self.future.set_exception(CustomFutureException())
  142. with self.assertRaises(CustomFutureException):
  143. self.future.result()
  144. def test_set_exception_raises_error_when_not_done(self):
  145. self.mock_crt_future.done.return_value = False
  146. with self.assertRaises(TransferNotDoneError):
  147. self.future.set_exception(CustomFutureException())
  148. def test_set_exception_can_override_previous_exception(self):
  149. self.future.set_exception(Exception())
  150. self.future.set_exception(CustomFutureException())
  151. with self.assertRaises(CustomFutureException):
  152. self.future.result()