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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
|
# Copyright 2013 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 argparse
import datetime
import ntpath
import os
import time
import pytest
from botocore.hooks import HierarchicalEmitter
from dateutil.tz import tzlocal
from s3transfer.compat import seekable
from awscli.compat import StringIO
from awscli.customizations.exceptions import ParamValidationError
from awscli.customizations.s3.utils import (
AppendFilter,
BucketLister,
NonSeekableStream,
RequestParamsMapper,
S3PathResolver,
SetFileUtimeError,
StablePriorityQueue,
StdoutBytesWriter,
block_unsupported_resources,
create_warning,
find_bucket_key,
get_file_stat,
guess_content_type,
human_readable_size,
human_readable_to_int,
relative_path,
set_file_utime,
)
from awscli.testutils import mock, temporary_file, unittest
@pytest.fixture
def s3control_client():
client = mock.MagicMock()
client.get_access_point.return_value = {"Bucket": "mybucket"}
client.list_multi_region_access_points.return_value = {
"AccessPoints": [
{"Alias": "myalias.mrap", "Regions": [{"Bucket": "mybucket"}]}
]
}
return client
@pytest.fixture
def sts_client():
client = mock.MagicMock()
client.get_caller_identity.return_value = {"Account": "123456789012"}
return client
@pytest.fixture
def s3_path_resolver(s3control_client, sts_client):
return S3PathResolver(s3control_client, sts_client)
@pytest.mark.parametrize(
'bytes_int, expected',
(
(1, '1 Byte'),
(10, '10 Bytes'),
(1000, '1000 Bytes'),
(1024, '1.0 KiB'),
(1024**2, '1.0 MiB'),
(1024**3, '1.0 GiB'),
(1024**4, '1.0 TiB'),
(1024**5, '1.0 PiB'),
(1024**6, '1.0 EiB'),
(1024**2 - 1, '1.0 MiB'),
(1024**3 - 1, '1.0 GiB'),
),
)
def test_human_readable_size(bytes_int, expected):
assert human_readable_size(bytes_int) == expected
@pytest.mark.parametrize(
'size_str, expected',
(
("1", 1),
("1024", 1024),
("1KB", 1024),
("1kb", 1024),
("1MB", 1024**2),
("1GB", 1024**3),
("1TB", 1024**4),
("1KiB", 1024),
("1kib", 1024),
("1MiB", 1024**2),
("1GiB", 1024**3),
("1TiB", 1024**4),
),
)
def test_convert_human_readable_to_int(size_str, expected):
assert human_readable_to_int(size_str) == expected
class AppendFilterTest(unittest.TestCase):
def test_call(self):
parser = argparse.ArgumentParser()
parser.add_argument(
'--include', action=AppendFilter, nargs=1, dest='path'
)
parser.add_argument(
'--exclude', action=AppendFilter, nargs=1, dest='path'
)
parsed_args = parser.parse_args(['--include', 'a', '--exclude', 'b'])
self.assertEqual(
parsed_args.path, [['--include', 'a'], ['--exclude', 'b']]
)
class TestFindBucketKey(unittest.TestCase):
def test_unicode(self):
s3_path = '\u1234' + '/' + '\u5678'
bucket, key = find_bucket_key(s3_path)
self.assertEqual(bucket, '\u1234')
self.assertEqual(key, '\u5678')
def test_bucket(self):
bucket, key = find_bucket_key('bucket')
self.assertEqual(bucket, 'bucket')
self.assertEqual(key, '')
def test_bucket_with_slash(self):
bucket, key = find_bucket_key('bucket/')
self.assertEqual(bucket, 'bucket')
self.assertEqual(key, '')
def test_bucket_with_key(self):
bucket, key = find_bucket_key('bucket/key')
self.assertEqual(bucket, 'bucket')
self.assertEqual(key, 'key')
def test_bucket_with_key_and_prefix(self):
bucket, key = find_bucket_key('bucket/prefix/key')
self.assertEqual(bucket, 'bucket')
self.assertEqual(key, 'prefix/key')
def test_accesspoint_arn(self):
bucket, key = find_bucket_key(
'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint'
)
self.assertEqual(
bucket, 'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint'
)
self.assertEqual(key, '')
def test_accesspoint_arn_with_slash(self):
bucket, key = find_bucket_key(
'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint/'
)
self.assertEqual(
bucket, 'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint'
)
self.assertEqual(key, '')
def test_accesspoint_arn_with_key(self):
bucket, key = find_bucket_key(
'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint/key'
)
self.assertEqual(
bucket, 'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint'
)
self.assertEqual(key, 'key')
def test_accesspoint_arn_with_key_and_prefix(self):
bucket, key = find_bucket_key(
'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint/pre/key'
)
self.assertEqual(
bucket, 'arn:aws:s3:us-west-2:123456789012:accesspoint/endpoint'
)
self.assertEqual(key, 'pre/key')
def test_outpost_arn_with_colon(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint'
),
)
self.assertEqual(key, '')
def test_outpost_arn_with_colon_and_key(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint/key'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint'
),
)
self.assertEqual(key, 'key')
def test_outpost_arn_with_colon_and_key_with_colon_in_name(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint/key:name'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint'
),
)
self.assertEqual(key, 'key:name')
def test_outpost_arn_with_colon_and_key_with_slash_in_name(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint/key/name'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint'
),
)
self.assertEqual(key, 'key/name')
def test_outpost_arn_with_colon_and_key_with_slash_and_colon_in_name(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint/prefix/key:name'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-12334:'
'accesspoint:my-accesspoint'
),
)
self.assertEqual(key, 'prefix/key:name')
def test_outpost_arn_with_slash(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint'
),
)
self.assertEqual(key, '')
def test_outpost_arn_with_slash_and_key(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint/key'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint'
),
)
self.assertEqual(key, 'key')
def test_outpost_arn_with_slash_and_key_with_colon_in_name(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint/key:name'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint'
),
)
self.assertEqual(key, 'key:name')
def test_outpost_arn_with_slash_and_key_with_slash_in_name(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint/key/name'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint'
),
)
self.assertEqual(key, 'key/name')
def test_outpost_arn_with_slash_and_key_with_slash_and_colon_in_name(self):
bucket, key = find_bucket_key(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint/prefix/key:name'
)
self.assertEqual(
bucket,
(
'arn:aws:s3-outposts:us-west-2:123456789012:outpost/op-12334/'
'accesspoint/my-accesspoint'
),
)
self.assertEqual(key, 'prefix/key:name')
class TestBlockUnsupportedResources(unittest.TestCase):
def test_object_lambda_arn_with_colon_raises_exception(self):
with self.assertRaisesRegex(
ParamValidationError, 'Use s3api commands instead'
):
block_unsupported_resources(
'arn:aws:s3-object-lambda:us-west-2:123456789012:'
'accesspoint:my-accesspoint'
)
def test_object_lambda_arn_with_slash_raises_exception(self):
with self.assertRaisesRegex(
ParamValidationError, 'Use s3api commands instead'
):
block_unsupported_resources(
'arn:aws:s3-object-lambda:us-west-2:123456789012:'
'accesspoint/my-accesspoint'
)
def test_outpost_bucket_arn_with_colon_raises_exception(self):
with self.assertRaisesRegex(
ParamValidationError, 'Use s3control commands instead'
):
block_unsupported_resources(
'arn:aws:s3-outposts:us-west-2:123456789012:'
'outpost/op-0a12345678abcdefg:bucket/bucket-foo'
)
def test_outpost_bucket_arn_with_slash_raises_exception(self):
with self.assertRaisesRegex(
ParamValidationError, 'Use s3control commands instead'
):
block_unsupported_resources(
'arn:aws:s3-outposts:us-west-2:123456789012:'
'outpost/op-0a12345678abcdefg/bucket/bucket-foo'
)
class TestCreateWarning(unittest.TestCase):
def test_create_warning(self):
path = '/foo/'
error_message = 'There was an error'
warning_message = create_warning(path, error_message)
self.assertEqual(
warning_message.message,
'warning: Skipping file /foo/. There was an error',
)
self.assertFalse(warning_message.error)
self.assertTrue(warning_message.warning)
class TestGuessContentType(unittest.TestCase):
def test_guess_content_type(self):
self.assertEqual(guess_content_type('foo.txt'), 'text/plain')
def test_guess_content_type_with_no_valid_matches(self):
self.assertEqual(guess_content_type('no-extension'), None)
def test_guess_content_type_with_unicode_error_returns_no_match(self):
with mock.patch('mimetypes.guess_type') as guess_type_patch:
# This should throw a UnicodeDecodeError.
guess_type_patch.side_effect = lambda x: b'\xe2'.decode('ascii')
self.assertEqual(guess_content_type('foo.txt'), None)
class TestRelativePath(unittest.TestCase):
def test_relpath_normal(self):
self.assertEqual(
relative_path('/tmp/foo/bar', '/tmp/foo'), '.' + os.sep + 'bar'
)
# We need to patch out relpath with the ntpath version so
# we can simulate testing drives on windows.
@mock.patch('os.path.relpath', ntpath.relpath)
def test_relpath_with_error(self):
# Just want to check we don't get an exception raised,
# which is what was happening previously.
self.assertIn(r'foo\bar', relative_path(r'c:\foo\bar'))
class TestStablePriorityQueue(unittest.TestCase):
def test_fifo_order_of_same_priorities(self):
a = mock.Mock()
a.PRIORITY = 5
b = mock.Mock()
b.PRIORITY = 5
c = mock.Mock()
c.PRIORITY = 1
q = StablePriorityQueue(maxsize=10, max_priority=20)
q.put(a)
q.put(b)
q.put(c)
# First we should get c because it's the lowest priority.
# We're using assertIs because we want the *exact* object.
self.assertIs(q.get(), c)
# Then a and b are the same priority, but we should get
# a first because it was inserted first.
self.assertIs(q.get(), a)
self.assertIs(q.get(), b)
def test_queue_length(self):
a = mock.Mock()
a.PRIORITY = 5
q = StablePriorityQueue(maxsize=10, max_priority=20)
self.assertEqual(q.qsize(), 0)
q.put(a)
self.assertEqual(q.qsize(), 1)
q.get()
self.assertEqual(q.qsize(), 0)
def test_insert_max_priority_capped(self):
q = StablePriorityQueue(maxsize=10, max_priority=20)
a = mock.Mock()
a.PRIORITY = 100
q.put(a)
self.assertIs(q.get(), a)
def test_priority_attr_is_missing(self):
# If priority attr is missing, we should add it
# to the lowest priority.
q = StablePriorityQueue(maxsize=10, max_priority=20)
a = object()
b = mock.Mock()
b.PRIORITY = 5
q.put(a)
q.put(b)
self.assertIs(q.get(), b)
self.assertIs(q.get(), a)
class TestBucketList(unittest.TestCase):
def setUp(self):
self.client = mock.Mock()
self.emitter = HierarchicalEmitter()
self.client.meta.events = self.emitter
self.date_parser = mock.Mock()
self.date_parser.return_value = mock.sentinel.now
self.responses = []
def fake_paginate(self, *args, **kwargs):
for response in self.responses:
self.emitter.emit('after-call.s3.ListObjectsV2', parsed=response)
return self.responses
def test_list_objects(self):
now = mock.sentinel.now
self.client.get_paginator.return_value.paginate = self.fake_paginate
individual_response_elements = [
{
'LastModified': '2014-02-27T04:20:38.000Z',
'Key': 'a',
'Size': 1,
},
{
'LastModified': '2014-02-27T04:20:38.000Z',
'Key': 'b',
'Size': 2,
},
{
'LastModified': '2014-02-27T04:20:38.000Z',
'Key': 'c',
'Size': 3,
},
]
self.responses = [
{'Contents': individual_response_elements[0:2]},
{'Contents': [individual_response_elements[2]]},
]
lister = BucketLister(self.client, self.date_parser)
objects = list(lister.list_objects(bucket='foo'))
self.assertEqual(
objects,
[
('foo/a', individual_response_elements[0]),
('foo/b', individual_response_elements[1]),
('foo/c', individual_response_elements[2]),
],
)
for individual_response in individual_response_elements:
self.assertEqual(individual_response['LastModified'], now)
def test_list_objects_passes_in_extra_args(self):
self.client.get_paginator.return_value.paginate.return_value = [
{
'Contents': [
{
'LastModified': '2014-02-27T04:20:38.000Z',
'Key': 'mykey',
'Size': 3,
}
]
}
]
lister = BucketLister(self.client, self.date_parser)
list(
lister.list_objects(
bucket='mybucket', extra_args={'RequestPayer': 'requester'}
)
)
self.client.get_paginator.return_value.paginate.assert_called_with(
Bucket='mybucket',
PaginationConfig={'PageSize': None},
RequestPayer='requester',
)
class TestGetFileStat(unittest.TestCase):
def test_get_file_stat(self):
now = datetime.datetime.now(tzlocal())
epoch_now = time.mktime(now.timetuple())
with temporary_file('w') as f:
f.write('foo')
f.flush()
os.utime(f.name, (epoch_now, epoch_now))
size, update_time = get_file_stat(f.name)
self.assertEqual(size, 3)
self.assertEqual(time.mktime(update_time.timetuple()), epoch_now)
def test_error_message(self):
with mock.patch('os.stat', mock.Mock(side_effect=OSError('msg'))):
with self.assertRaisesRegex(ValueError, r'myfilename\.txt'):
get_file_stat('myfilename.txt')
def assert_handles_fromtimestamp_error(self, error):
patch_attribute = 'awscli.customizations.s3.utils.datetime'
with mock.patch(patch_attribute) as datetime_mock:
with temporary_file('w') as temp_file:
temp_file.write('foo')
temp_file.flush()
datetime_mock.fromtimestamp.side_effect = error
size, update_time = get_file_stat(temp_file.name)
self.assertIsNone(update_time)
def test_returns_epoch_on_invalid_timestamp(self):
self.assert_handles_fromtimestamp_error(ValueError())
def test_returns_epoch_on_invalid_timestamp_os_error(self):
self.assert_handles_fromtimestamp_error(OSError())
def test_returns_epoch_on_invalid_timestamp_overflow_error(self):
self.assert_handles_fromtimestamp_error(OverflowError())
class TestSetsFileUtime(unittest.TestCase):
def test_successfully_sets_utime(self):
now = datetime.datetime.now(tzlocal())
epoch_now = time.mktime(now.timetuple())
with temporary_file('w') as f:
set_file_utime(f.name, epoch_now)
_, update_time = get_file_stat(f.name)
self.assertEqual(time.mktime(update_time.timetuple()), epoch_now)
def test_throws_more_relevant_error_when_errno_1(self):
now = datetime.datetime.now(tzlocal())
epoch_now = time.mktime(now.timetuple())
with mock.patch('os.utime') as utime_mock:
utime_mock.side_effect = OSError(1, '')
with self.assertRaises(SetFileUtimeError):
set_file_utime('not_real_file', epoch_now)
def test_passes_through_other_os_errors(self):
now = datetime.datetime.now(tzlocal())
epoch_now = time.mktime(now.timetuple())
with mock.patch('os.utime') as utime_mock:
utime_mock.side_effect = OSError(2, '')
with self.assertRaises(OSError):
set_file_utime('not_real_file', epoch_now)
class TestRequestParamsMapperSSE(unittest.TestCase):
def setUp(self):
self.cli_params = {
'sse': 'AES256',
'sse_kms_key_id': 'my-kms-key',
'sse_c': 'AES256',
'sse_c_key': 'my-sse-c-key',
'sse_c_copy_source': 'AES256',
'sse_c_copy_source_key': 'my-sse-c-copy-source-key',
}
def test_head_object(self):
params = {}
RequestParamsMapper.map_head_object_params(params, self.cli_params)
self.assertEqual(
params,
{
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': 'my-sse-c-key',
},
)
def test_put_object(self):
params = {}
RequestParamsMapper.map_put_object_params(params, self.cli_params)
self.assertEqual(
params,
{
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': 'my-sse-c-key',
'SSEKMSKeyId': 'my-kms-key',
'ServerSideEncryption': 'AES256',
},
)
def test_get_object(self):
params = {}
RequestParamsMapper.map_get_object_params(params, self.cli_params)
self.assertEqual(
params,
{
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': 'my-sse-c-key',
},
)
def test_copy_object(self):
params = {}
RequestParamsMapper.map_copy_object_params(params, self.cli_params)
self.assertEqual(
params,
{
'CopySourceSSECustomerAlgorithm': 'AES256',
'CopySourceSSECustomerKey': 'my-sse-c-copy-source-key',
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': 'my-sse-c-key',
'SSEKMSKeyId': 'my-kms-key',
'ServerSideEncryption': 'AES256',
},
)
def test_create_multipart_upload(self):
params = {}
RequestParamsMapper.map_create_multipart_upload_params(
params, self.cli_params
)
self.assertEqual(
params,
{
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': 'my-sse-c-key',
'SSEKMSKeyId': 'my-kms-key',
'ServerSideEncryption': 'AES256',
},
)
def test_upload_part(self):
params = {}
RequestParamsMapper.map_upload_part_params(params, self.cli_params)
self.assertEqual(
params,
{
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': 'my-sse-c-key',
},
)
def test_upload_part_copy(self):
params = {}
RequestParamsMapper.map_upload_part_copy_params(
params, self.cli_params
)
self.assertEqual(
params,
{
'CopySourceSSECustomerAlgorithm': 'AES256',
'CopySourceSSECustomerKey': 'my-sse-c-copy-source-key',
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': 'my-sse-c-key',
},
)
class TestRequestParamsMapperChecksumAlgorithm:
@pytest.fixture
def cli_params(self):
return {'checksum_algorithm': 'CRC64NVME'}
@pytest.fixture
def cli_params_no_algorithm(self):
return {}
def test_put_object(self, cli_params):
request_params = {}
RequestParamsMapper.map_put_object_params(request_params, cli_params)
assert request_params == {'ChecksumAlgorithm': 'CRC64NVME'}
def test_put_object_no_checksum(self, cli_params_no_algorithm):
request_params = {}
RequestParamsMapper.map_put_object_params(
request_params, cli_params_no_algorithm
)
assert 'ChecksumAlgorithm' not in request_params
def test_copy_object(self, cli_params):
request_params = {}
RequestParamsMapper.map_copy_object_params(request_params, cli_params)
assert request_params == {'ChecksumAlgorithm': 'CRC64NVME'}
def test_copy_object_no_checksum(self, cli_params_no_algorithm):
request_params = {}
RequestParamsMapper.map_put_object_params(
request_params, cli_params_no_algorithm
)
assert 'ChecksumAlgorithm' not in request_params
class TestRequestParamsMapperChecksumMode:
@pytest.fixture
def cli_params(self):
return {'checksum_mode': 'ENABLED'}
@pytest.fixture
def cli_params_no_checksum(self):
return {}
def test_get_object(self, cli_params):
request_params = {}
RequestParamsMapper.map_get_object_params(request_params, cli_params)
assert request_params == {'ChecksumMode': 'ENABLED'}
def test_get_object_no_checksums(self, cli_params_no_checksum):
request_params = {}
RequestParamsMapper.map_get_object_params(
request_params, cli_params_no_checksum
)
assert 'ChecksumMode' not in request_params
class TestRequestParamsMapperRequestPayer(unittest.TestCase):
def setUp(self):
self.cli_params = {'request_payer': 'requester'}
def test_head_object(self):
params = {}
RequestParamsMapper.map_head_object_params(params, self.cli_params)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_put_object(self):
params = {}
RequestParamsMapper.map_put_object_params(params, self.cli_params)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_get_object(self):
params = {}
RequestParamsMapper.map_get_object_params(params, self.cli_params)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_copy_object(self):
params = {}
RequestParamsMapper.map_copy_object_params(params, self.cli_params)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_create_multipart_upload(self):
params = {}
RequestParamsMapper.map_create_multipart_upload_params(
params, self.cli_params
)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_upload_part(self):
params = {}
RequestParamsMapper.map_upload_part_params(params, self.cli_params)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_upload_part_copy(self):
params = {}
RequestParamsMapper.map_upload_part_copy_params(
params, self.cli_params
)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_delete_object(self):
params = {}
RequestParamsMapper.map_delete_object_params(params, self.cli_params)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_list_objects(self):
params = {}
RequestParamsMapper.map_list_objects_v2_params(params, self.cli_params)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_map_get_object_tagging_params(self):
params = {}
RequestParamsMapper.map_get_object_tagging_params(
params, self.cli_params
)
self.assertEqual(params, {'RequestPayer': 'requester'})
def test_map_put_object_tagging_params(self):
params = {}
RequestParamsMapper.map_put_object_tagging_params(
params, self.cli_params
)
self.assertEqual(params, {'RequestPayer': 'requester'})
class TestBytesPrint(unittest.TestCase):
def setUp(self):
self.stdout = mock.Mock()
self.stdout.buffer = self.stdout
def test_stdout_wrapper(self):
wrapper = StdoutBytesWriter(self.stdout)
wrapper.write(b'foo')
self.assertTrue(self.stdout.write.called)
self.assertEqual(self.stdout.write.call_args[0][0], b'foo')
class TestNonSeekableStream(unittest.TestCase):
def test_can_make_stream_unseekable(self):
fileobj = StringIO('foobar')
self.assertTrue(seekable(fileobj))
nonseekable_fileobj = NonSeekableStream(fileobj)
self.assertFalse(seekable(nonseekable_fileobj))
self.assertEqual(nonseekable_fileobj.read(), 'foobar')
def test_can_specify_amount_for_nonseekable_stream(self):
nonseekable_fileobj = NonSeekableStream(StringIO('foobar'))
self.assertEqual(nonseekable_fileobj.read(3), 'foo')
class TestS3PathResolver:
_BASE_ACCESSPOINT_ARN = (
"s3://arn:aws:s3:us-west-2:123456789012:accesspoint/myaccesspoint"
)
_BASE_OUTPOST_ACCESSPOINT_ARN = (
"s3://arn:aws:s3-outposts:us-east-1:123456789012:outpost"
"/op-foo/accesspoint/myaccesspoint"
)
_BASE_ACCESSPOINT_ALIAS = "s3://myaccesspoint-foobar12345-s3alias"
_BASE_OUTPOST_ACCESSPOINT_ALIAS = "s3://myaccesspoint-foobar12345--op-s3"
_BASE_MRAP_ARN = "s3://arn:aws:s3::123456789012:accesspoint/myalias.mrap"
@pytest.mark.parametrize(
"path,resolved",
[
(_BASE_ACCESSPOINT_ARN, "s3://mybucket/"),
(f"{_BASE_ACCESSPOINT_ARN}/", "s3://mybucket/"),
(f"{_BASE_ACCESSPOINT_ARN}/mykey", "s3://mybucket/mykey"),
(f"{_BASE_ACCESSPOINT_ARN}/myprefix/", "s3://mybucket/myprefix/"),
(
f"{_BASE_ACCESSPOINT_ARN}/myprefix/mykey",
"s3://mybucket/myprefix/mykey",
),
],
)
def test_resolves_accesspoint_arn(
self, path, resolved, s3_path_resolver, s3control_client
):
resolved_paths = s3_path_resolver.resolve_underlying_s3_paths(path)
assert resolved_paths == [resolved]
s3control_client.get_access_point.assert_called_with(
AccountId="123456789012", Name="myaccesspoint"
)
@pytest.mark.parametrize(
"path,resolved",
[
(_BASE_OUTPOST_ACCESSPOINT_ARN, "s3://mybucket/"),
(f"{_BASE_OUTPOST_ACCESSPOINT_ARN}/", "s3://mybucket/"),
(f"{_BASE_OUTPOST_ACCESSPOINT_ARN}/mykey", "s3://mybucket/mykey"),
(
f"{_BASE_OUTPOST_ACCESSPOINT_ARN}/myprefix/",
"s3://mybucket/myprefix/",
),
(
f"{_BASE_OUTPOST_ACCESSPOINT_ARN}/myprefix/mykey",
"s3://mybucket/myprefix/mykey",
),
],
)
def test_resolves_outpost_accesspoint_arn(
self, path, resolved, s3_path_resolver, s3control_client
):
resolved_paths = s3_path_resolver.resolve_underlying_s3_paths(path)
assert resolved_paths == [resolved]
s3control_client.get_access_point.assert_called_with(
AccountId="123456789012",
Name=(
"arn:aws:s3-outposts:us-east-1:123456789012:outpost"
"/op-foo/accesspoint/myaccesspoint"
),
)
@pytest.mark.parametrize(
"path,resolved",
[
(_BASE_ACCESSPOINT_ALIAS, "s3://mybucket/"),
(f"{_BASE_ACCESSPOINT_ALIAS}/", "s3://mybucket/"),
(f"{_BASE_ACCESSPOINT_ALIAS}/mykey", "s3://mybucket/mykey"),
(
f"{_BASE_ACCESSPOINT_ALIAS}/myprefix/",
"s3://mybucket/myprefix/",
),
(
f"{_BASE_ACCESSPOINT_ALIAS}/myprefix/mykey",
"s3://mybucket/myprefix/mykey",
),
],
)
def test_resolves_accesspoint_alias(
self, path, resolved, s3_path_resolver, s3control_client, sts_client
):
resolved_paths = s3_path_resolver.resolve_underlying_s3_paths(path)
assert resolved_paths == [resolved]
sts_client.get_caller_identity.assert_called_once_with()
s3control_client.get_access_point.assert_called_with(
AccountId="123456789012", Name="myaccesspoint-foobar12345-s3alias"
)
@pytest.mark.parametrize(
"path",
[
(_BASE_OUTPOST_ACCESSPOINT_ALIAS),
(f"{_BASE_OUTPOST_ACCESSPOINT_ALIAS}/"),
(f"{_BASE_OUTPOST_ACCESSPOINT_ALIAS}/mykey"),
(f"{_BASE_OUTPOST_ACCESSPOINT_ALIAS}/myprefix/"),
(f"{_BASE_OUTPOST_ACCESSPOINT_ALIAS}/myprefix/mykey"),
],
)
def test_outpost_accesspoint_alias_raises_exception(
self, path, s3_path_resolver
):
with pytest.raises(ParamValidationError) as e:
s3_path_resolver.resolve_underlying_s3_paths(path)
assert "Can't resolve underlying bucket name" in str(e.value)
@pytest.mark.parametrize(
"path,resolved",
[
(_BASE_MRAP_ARN, "s3://mybucket/"),
(f"{_BASE_MRAP_ARN}/", "s3://mybucket/"),
(f"{_BASE_MRAP_ARN}/mykey", "s3://mybucket/mykey"),
(f"{_BASE_MRAP_ARN}/myprefix/", "s3://mybucket/myprefix/"),
(
f"{_BASE_MRAP_ARN}/myprefix/mykey",
"s3://mybucket/myprefix/mykey",
),
],
)
def test_resolves_mrap_arn(
self, path, resolved, s3_path_resolver, s3control_client
):
resolved_paths = s3_path_resolver.resolve_underlying_s3_paths(path)
assert resolved_paths == [resolved]
s3control_client.list_multi_region_access_points.assert_called_with(
AccountId="123456789012"
)
@pytest.mark.parametrize(
"path,resolved,name",
[
(
f"{_BASE_ACCESSPOINT_ARN}-s3alias/mykey",
"s3://mybucket/mykey",
"myaccesspoint-s3alias",
),
(
f"{_BASE_OUTPOST_ACCESSPOINT_ARN}--op-s3/mykey",
"s3://mybucket/mykey",
f"{_BASE_OUTPOST_ACCESSPOINT_ARN[5:]}--op-s3",
),
],
)
def test_alias_suffixes_dont_match_accesspoint_arns(
self, path, resolved, name, s3_path_resolver, s3control_client
):
resolved_paths = s3_path_resolver.resolve_underlying_s3_paths(path)
assert resolved_paths == [resolved]
s3control_client.get_access_point.assert_called_with(
AccountId="123456789012", Name=name
)
@pytest.mark.parametrize(
"path,expected_has_underlying_s3_path",
[
(_BASE_ACCESSPOINT_ARN, True),
(f"{_BASE_ACCESSPOINT_ARN}/mykey", True),
(f"{_BASE_ACCESSPOINT_ARN}/myprefix/mykey", True),
(_BASE_ACCESSPOINT_ALIAS, True),
(_BASE_OUTPOST_ACCESSPOINT_ARN, True),
(_BASE_OUTPOST_ACCESSPOINT_ALIAS, True),
(_BASE_MRAP_ARN, True),
("s3://mybucket/", False),
("s3://mybucket/mykey", False),
("s3://mybucket/myprefix/mykey", False),
],
)
def test_has_underlying_s3_path(
self, path, expected_has_underlying_s3_path
):
has_underlying_s3_path = S3PathResolver.has_underlying_s3_path(path)
assert has_underlying_s3_path == expected_has_underlying_s3_path
|