test_copies.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright 2016 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 s3transfer.copies import CopyObjectTask, CopyPartTask
  14. from tests import BaseTaskTest, RecordingSubscriber
  15. class BaseCopyTaskTest(BaseTaskTest):
  16. def setUp(self):
  17. super().setUp()
  18. self.bucket = 'mybucket'
  19. self.key = 'mykey'
  20. self.copy_source = {'Bucket': 'mysourcebucket', 'Key': 'mysourcekey'}
  21. self.extra_args = {}
  22. self.callbacks = []
  23. self.size = 5
  24. class TestCopyObjectTask(BaseCopyTaskTest):
  25. def get_copy_task(self, **kwargs):
  26. default_kwargs = {
  27. 'client': self.client,
  28. 'copy_source': self.copy_source,
  29. 'bucket': self.bucket,
  30. 'key': self.key,
  31. 'extra_args': self.extra_args,
  32. 'callbacks': self.callbacks,
  33. 'size': self.size,
  34. }
  35. default_kwargs.update(kwargs)
  36. return self.get_task(CopyObjectTask, main_kwargs=default_kwargs)
  37. def test_main(self):
  38. self.stubber.add_response(
  39. 'copy_object',
  40. service_response={},
  41. expected_params={
  42. 'Bucket': self.bucket,
  43. 'Key': self.key,
  44. 'CopySource': self.copy_source,
  45. },
  46. )
  47. task = self.get_copy_task()
  48. task()
  49. self.stubber.assert_no_pending_responses()
  50. def test_extra_args(self):
  51. self.extra_args['ACL'] = 'private'
  52. self.stubber.add_response(
  53. 'copy_object',
  54. service_response={},
  55. expected_params={
  56. 'Bucket': self.bucket,
  57. 'Key': self.key,
  58. 'CopySource': self.copy_source,
  59. 'ACL': 'private',
  60. },
  61. )
  62. task = self.get_copy_task()
  63. task()
  64. self.stubber.assert_no_pending_responses()
  65. def test_callbacks_invoked(self):
  66. subscriber = RecordingSubscriber()
  67. self.callbacks.append(subscriber.on_progress)
  68. self.stubber.add_response(
  69. 'copy_object',
  70. service_response={},
  71. expected_params={
  72. 'Bucket': self.bucket,
  73. 'Key': self.key,
  74. 'CopySource': self.copy_source,
  75. },
  76. )
  77. task = self.get_copy_task()
  78. task()
  79. self.stubber.assert_no_pending_responses()
  80. self.assertEqual(subscriber.calculate_bytes_seen(), self.size)
  81. class TestCopyPartTask(BaseCopyTaskTest):
  82. def setUp(self):
  83. super().setUp()
  84. self.copy_source_range = 'bytes=5-9'
  85. self.extra_args['CopySourceRange'] = self.copy_source_range
  86. self.upload_id = 'myuploadid'
  87. self.part_number = 1
  88. self.result_etag = 'my-etag'
  89. def get_copy_task(self, **kwargs):
  90. default_kwargs = {
  91. 'client': self.client,
  92. 'copy_source': self.copy_source,
  93. 'bucket': self.bucket,
  94. 'key': self.key,
  95. 'upload_id': self.upload_id,
  96. 'part_number': self.part_number,
  97. 'extra_args': self.extra_args,
  98. 'callbacks': self.callbacks,
  99. 'size': self.size,
  100. }
  101. default_kwargs.update(kwargs)
  102. return self.get_task(CopyPartTask, main_kwargs=default_kwargs)
  103. def test_main(self):
  104. self.stubber.add_response(
  105. 'upload_part_copy',
  106. service_response={'CopyPartResult': {'ETag': self.result_etag}},
  107. expected_params={
  108. 'Bucket': self.bucket,
  109. 'Key': self.key,
  110. 'CopySource': self.copy_source,
  111. 'UploadId': self.upload_id,
  112. 'PartNumber': self.part_number,
  113. 'CopySourceRange': self.copy_source_range,
  114. },
  115. )
  116. task = self.get_copy_task()
  117. self.assertEqual(
  118. task(), {'PartNumber': self.part_number, 'ETag': self.result_etag}
  119. )
  120. self.stubber.assert_no_pending_responses()
  121. def test_extra_args(self):
  122. self.extra_args['RequestPayer'] = 'requester'
  123. self.stubber.add_response(
  124. 'upload_part_copy',
  125. service_response={'CopyPartResult': {'ETag': self.result_etag}},
  126. expected_params={
  127. 'Bucket': self.bucket,
  128. 'Key': self.key,
  129. 'CopySource': self.copy_source,
  130. 'UploadId': self.upload_id,
  131. 'PartNumber': self.part_number,
  132. 'CopySourceRange': self.copy_source_range,
  133. 'RequestPayer': 'requester',
  134. },
  135. )
  136. task = self.get_copy_task()
  137. self.assertEqual(
  138. task(), {'PartNumber': self.part_number, 'ETag': self.result_etag}
  139. )
  140. self.stubber.assert_no_pending_responses()
  141. def test_callbacks_invoked(self):
  142. subscriber = RecordingSubscriber()
  143. self.callbacks.append(subscriber.on_progress)
  144. self.stubber.add_response(
  145. 'upload_part_copy',
  146. service_response={'CopyPartResult': {'ETag': self.result_etag}},
  147. expected_params={
  148. 'Bucket': self.bucket,
  149. 'Key': self.key,
  150. 'CopySource': self.copy_source,
  151. 'UploadId': self.upload_id,
  152. 'PartNumber': self.part_number,
  153. 'CopySourceRange': self.copy_source_range,
  154. },
  155. )
  156. task = self.get_copy_task()
  157. self.assertEqual(
  158. task(), {'PartNumber': self.part_number, 'ETag': self.result_etag}
  159. )
  160. self.stubber.assert_no_pending_responses()
  161. self.assertEqual(subscriber.calculate_bytes_seen(), self.size)