test_copies.py 6.0 KB

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