|
12 | 12 | # language governing permissions and limitations under the License.
|
13 | 13 | from botocore.exceptions import ClientError
|
14 | 14 | from botocore.stub import Stubber
|
| 15 | +from s3transfer.copies import CopySubmissionTask |
15 | 16 | from s3transfer.manager import TransferConfig, TransferManager
|
16 | 17 | from s3transfer.utils import MIN_UPLOAD_CHUNKSIZE
|
17 | 18 |
|
@@ -275,7 +276,12 @@ def test_copy_maps_extra_args_to_head_object(self):
|
275 | 276 |
|
276 | 277 | def test_allowed_copy_params_are_valid(self):
|
277 | 278 | op_model = self.client.meta.service_model.operation_model('CopyObject')
|
278 |
| - for allowed_upload_arg in self._manager.ALLOWED_COPY_ARGS: |
| 279 | + allowed_copy_arg = [ |
| 280 | + arg |
| 281 | + for arg in self._manager.ALLOWED_COPY_ARGS |
| 282 | + if arg not in CopySubmissionTask.COPY_OBJECT_ARGS_BLOCKLIST |
| 283 | + ] |
| 284 | + for allowed_upload_arg in allowed_copy_arg: |
279 | 285 | self.assertIn(allowed_upload_arg, op_model.input_shape.members)
|
280 | 286 |
|
281 | 287 | def test_copy_with_tagging(self):
|
@@ -700,3 +706,216 @@ def test_mp_copy_with_tagging_directive(self):
|
700 | 706 | )
|
701 | 707 | future.result()
|
702 | 708 | self.stubber.assert_no_pending_responses()
|
| 709 | + |
| 710 | + def test_copy_with_no_overwrite_flag_when_small_object_exists_at_target( |
| 711 | + self, |
| 712 | + ): |
| 713 | + # Set up IfNoneMatch in extra_args |
| 714 | + self.extra_args['IfNoneMatch'] = '*' |
| 715 | + # Setting up the size of object |
| 716 | + small_content_size = 5 |
| 717 | + self.content = b'0' * small_content_size |
| 718 | + # Add head object response with small content size |
| 719 | + head_response = self.create_stubbed_responses()[0] |
| 720 | + head_response['service_response'] = { |
| 721 | + 'ContentLength': small_content_size |
| 722 | + } |
| 723 | + self.stubber.add_response(**head_response) |
| 724 | + # Should use multipart upload |
| 725 | + # Add create_multipart_upload response |
| 726 | + self.stubber.add_response( |
| 727 | + 'create_multipart_upload', |
| 728 | + service_response={'UploadId': self.multipart_id}, |
| 729 | + expected_params={ |
| 730 | + 'Bucket': self.bucket, |
| 731 | + 'Key': self.key, |
| 732 | + }, |
| 733 | + ) |
| 734 | + # Add upload_part_copy response |
| 735 | + self.stubber.add_response( |
| 736 | + 'upload_part_copy', |
| 737 | + {'CopyPartResult': {'ETag': 'etag-1'}}, |
| 738 | + { |
| 739 | + 'Bucket': self.bucket, |
| 740 | + 'Key': self.key, |
| 741 | + 'CopySource': self.copy_source, |
| 742 | + 'UploadId': self.multipart_id, |
| 743 | + 'PartNumber': 1, |
| 744 | + 'CopySourceRange': f'bytes=0-{small_content_size-1}', |
| 745 | + }, |
| 746 | + ) |
| 747 | + # Mock a PreconditionFailed error for complete_multipart_upload |
| 748 | + self.stubber.add_client_error( |
| 749 | + method='complete_multipart_upload', |
| 750 | + service_error_code='PreconditionFailed', |
| 751 | + service_message='The condition specified in the conditional header(s) was not met', |
| 752 | + http_status_code=412, |
| 753 | + expected_params={ |
| 754 | + 'Bucket': self.bucket, |
| 755 | + 'Key': self.key, |
| 756 | + 'UploadId': self.multipart_id, |
| 757 | + 'MultipartUpload': { |
| 758 | + 'Parts': [{'ETag': 'etag-1', 'PartNumber': 1}] |
| 759 | + }, |
| 760 | + 'IfNoneMatch': '*', |
| 761 | + }, |
| 762 | + ) |
| 763 | + # Add abort_multipart_upload response |
| 764 | + self.stubber.add_response( |
| 765 | + 'abort_multipart_upload', |
| 766 | + service_response={}, |
| 767 | + expected_params={ |
| 768 | + 'Bucket': self.bucket, |
| 769 | + 'Key': self.key, |
| 770 | + 'UploadId': self.multipart_id, |
| 771 | + }, |
| 772 | + ) |
| 773 | + call_kwargs = self.create_call_kwargs() |
| 774 | + call_kwargs['extra_args'] = self.extra_args |
| 775 | + future = self.manager.copy(**call_kwargs) |
| 776 | + with self.assertRaises(ClientError) as context: |
| 777 | + future.result() |
| 778 | + self.assertEqual( |
| 779 | + context.exception.response['Error']['Code'], 'PreconditionFailed' |
| 780 | + ) |
| 781 | + self.stubber.assert_no_pending_responses() |
| 782 | + |
| 783 | + def test_copy_with_no_overwrite_flag_when_small_object_not_exists_at_target( |
| 784 | + self, |
| 785 | + ): |
| 786 | + # Set up IfNoneMatch in extra_args |
| 787 | + self.extra_args['IfNoneMatch'] = '*' |
| 788 | + # Setting up the size of object |
| 789 | + small_content_size = 5 |
| 790 | + self.content = b'0' * small_content_size |
| 791 | + # Add head object response with small content size |
| 792 | + head_response = self.create_stubbed_responses()[0] |
| 793 | + head_response['service_response'] = { |
| 794 | + 'ContentLength': small_content_size |
| 795 | + } |
| 796 | + self.stubber.add_response(**head_response) |
| 797 | + # Should use multipart copy |
| 798 | + # Add create_multipart_upload response |
| 799 | + self.stubber.add_response( |
| 800 | + 'create_multipart_upload', |
| 801 | + service_response={'UploadId': self.multipart_id}, |
| 802 | + expected_params={ |
| 803 | + 'Bucket': self.bucket, |
| 804 | + 'Key': self.key, |
| 805 | + }, |
| 806 | + ) |
| 807 | + # Add upload_part_copy response |
| 808 | + self.stubber.add_response( |
| 809 | + 'upload_part_copy', |
| 810 | + {'CopyPartResult': {'ETag': 'etag-1'}}, |
| 811 | + { |
| 812 | + 'Bucket': self.bucket, |
| 813 | + 'Key': self.key, |
| 814 | + 'CopySource': self.copy_source, |
| 815 | + 'UploadId': self.multipart_id, |
| 816 | + 'PartNumber': 1, |
| 817 | + 'CopySourceRange': f'bytes=0-{small_content_size-1}', |
| 818 | + }, |
| 819 | + ) |
| 820 | + self.stubber.add_response( |
| 821 | + 'complete_multipart_upload', |
| 822 | + service_response={}, |
| 823 | + expected_params={ |
| 824 | + 'Bucket': self.bucket, |
| 825 | + 'Key': self.key, |
| 826 | + 'UploadId': self.multipart_id, |
| 827 | + 'MultipartUpload': { |
| 828 | + 'Parts': [{'ETag': 'etag-1', 'PartNumber': 1}] |
| 829 | + }, |
| 830 | + 'IfNoneMatch': '*', |
| 831 | + }, |
| 832 | + ) |
| 833 | + call_kwargs = self.create_call_kwargs() |
| 834 | + call_kwargs['extra_args'] = self.extra_args |
| 835 | + future = self.manager.copy(**call_kwargs) |
| 836 | + future.result() |
| 837 | + self.stubber.assert_no_pending_responses() |
| 838 | + |
| 839 | + def test_copy_with_no_overwrite_flag_when_large_object_exists_at_target( |
| 840 | + self, |
| 841 | + ): |
| 842 | + # Set up IfNoneMatch in extra_args |
| 843 | + self.extra_args['IfNoneMatch'] = '*' |
| 844 | + # Add head object response |
| 845 | + self.add_get_head_response_with_default_expected_params() |
| 846 | + # Should use multipart upload |
| 847 | + self.add_create_multipart_response_with_default_expected_params() |
| 848 | + self.add_upload_part_copy_responses_with_default_expected_params() |
| 849 | + # Mock a PreconditionFailed error for complete_multipart_upload |
| 850 | + self.stubber.add_client_error( |
| 851 | + method='complete_multipart_upload', |
| 852 | + service_error_code='PreconditionFailed', |
| 853 | + service_message='The condition specified in the conditional header(s) was not met', |
| 854 | + http_status_code=412, |
| 855 | + expected_params={ |
| 856 | + 'Bucket': self.bucket, |
| 857 | + 'Key': self.key, |
| 858 | + 'UploadId': self.multipart_id, |
| 859 | + 'MultipartUpload': { |
| 860 | + 'Parts': [ |
| 861 | + {'ETag': 'etag-1', 'PartNumber': 1}, |
| 862 | + {'ETag': 'etag-2', 'PartNumber': 2}, |
| 863 | + {'ETag': 'etag-3', 'PartNumber': 3}, |
| 864 | + ] |
| 865 | + }, |
| 866 | + 'IfNoneMatch': '*', |
| 867 | + }, |
| 868 | + ) |
| 869 | + # Add abort_multipart_upload response |
| 870 | + self.stubber.add_response( |
| 871 | + 'abort_multipart_upload', |
| 872 | + service_response={}, |
| 873 | + expected_params={ |
| 874 | + 'Bucket': self.bucket, |
| 875 | + 'Key': self.key, |
| 876 | + 'UploadId': self.multipart_id, |
| 877 | + }, |
| 878 | + ) |
| 879 | + call_kwargs = self.create_call_kwargs() |
| 880 | + call_kwargs['extra_args'] = self.extra_args |
| 881 | + future = self.manager.copy(**call_kwargs) |
| 882 | + with self.assertRaises(ClientError) as context: |
| 883 | + future.result() |
| 884 | + self.assertEqual( |
| 885 | + context.exception.response['Error']['Code'], 'PreconditionFailed' |
| 886 | + ) |
| 887 | + self.stubber.assert_no_pending_responses() |
| 888 | + |
| 889 | + def test_copy_with_no_overwrite_flag_when_large_object_not_exists_at_target( |
| 890 | + self, |
| 891 | + ): |
| 892 | + # Set up IfNoneMatch in extra_args |
| 893 | + self.extra_args['IfNoneMatch'] = '*' |
| 894 | + # Add head object response |
| 895 | + self.add_get_head_response_with_default_expected_params() |
| 896 | + # Should use multipart upload |
| 897 | + self.add_create_multipart_response_with_default_expected_params() |
| 898 | + self.add_upload_part_copy_responses_with_default_expected_params() |
| 899 | + # Add successful complete_multipart_upload response |
| 900 | + self.stubber.add_response( |
| 901 | + 'complete_multipart_upload', |
| 902 | + service_response={}, |
| 903 | + expected_params={ |
| 904 | + 'Bucket': self.bucket, |
| 905 | + 'Key': self.key, |
| 906 | + 'UploadId': self.multipart_id, |
| 907 | + 'MultipartUpload': { |
| 908 | + 'Parts': [ |
| 909 | + {'ETag': 'etag-1', 'PartNumber': 1}, |
| 910 | + {'ETag': 'etag-2', 'PartNumber': 2}, |
| 911 | + {'ETag': 'etag-3', 'PartNumber': 3}, |
| 912 | + ] |
| 913 | + }, |
| 914 | + 'IfNoneMatch': '*', |
| 915 | + }, |
| 916 | + ) |
| 917 | + call_kwargs = self.create_call_kwargs() |
| 918 | + call_kwargs['extra_args'] = self.extra_args |
| 919 | + future = self.manager.copy(**call_kwargs) |
| 920 | + future.result() |
| 921 | + self.stubber.assert_no_pending_responses() |
0 commit comments