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
|
# Copyright 2025 OpenStack Foundation
# 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. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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 hashlib
import io
from unittest import mock
import uuid
import boto3
import botocore
from botocore import exceptions as boto_exceptions
from botocore import stub
from oslo_utils import units
from glance_store._drivers import s3
from glance_store import capabilities
from glance_store import exceptions
from glance_store import location
# Constants
FAKE_UUID = str(uuid.uuid4())
FIVE_KB = 5 * units.Ki
S3_CONF = {
's3_store_access_key': 'user',
's3_store_secret_key': 'key',
's3_store_region_name': '',
's3_store_host': 'localhost',
's3_store_bucket': 'glance',
's3_store_large_object_size': 9, # over 9MB is large
's3_store_large_object_chunk_size': 6, # part size is 6MB
}
def format_s3_location(user, key, authurl, bucket, obj):
"""Helper method that returns a S3 store URI given the component pieces."""
scheme = 's3'
if authurl.startswith('https://'):
scheme = 's3+https'
authurl = authurl[len('https://'):]
elif authurl.startswith('http://'):
authurl = authurl[len('http://'):]
authurl = authurl.strip('/')
return "%s://%s:%s@%s/%s/%s" % (scheme, user, key, authurl, bucket, obj)
class TestS3StoreBase(object):
def _test_get(self):
"""Test a "normal" retrieval of an image in chunks."""
bucket, key = 'glance', str(uuid.uuid4())
fixture_object = {
'Body': io.BytesIO(b"*" * 5 * units.Ki),
'ContentLength': 5 * units.Ki
}
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_object',
service_response={},
expected_params={
'Bucket': bucket,
'Key': key
})
stubber.add_response(method='get_object',
service_response=fixture_object,
expected_params={
'Bucket': bucket,
'Key': key
})
with mock.patch.object(boto3.session.Session,
"client") as mock_client:
mock_client.return_value = fake_s3_client
if self.multistore:
loc = location.get_location_from_uri_and_backend(
"s3://user:key@auth_address/%s/%s" % (bucket, key),
self.backend, conf=self.conf)
else:
loc = location.get_location_from_uri(
"s3://user:key@auth_address/%s/%s" % (bucket, key),
conf=self.conf)
(image_s3, image_size) = self.store.get(loc)
self.assertEqual(5 * units.Ki, image_size)
expected_data = b"*" * 5 * units.Ki
data = b""
for chunk in image_s3:
data += chunk
self.assertEqual(expected_data, data)
def _test_get_non_existing(self):
"""Test trying to retrieve a s3 that doesn't exist raises an error."""
bucket, key = 'glance', 'no_exist'
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_client_error(method='head_object',
service_error_code='404',
service_message='''
The specified key does not exist.
''',
expected_params={
'Bucket': bucket,
'Key': key
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
uri = "s3://user:key@auth_address/%s/%s" % (bucket, key)
if self.multistore:
loc = location.get_location_from_uri_and_backend(
uri, self.backend, conf=self.conf)
else:
loc = location.get_location_from_uri(uri, conf=self.conf)
self.assertRaises(exceptions.NotFound, self.store.get, loc)
def _test_add_singlepart(self):
"""Test that we can add an image via the s3 backend."""
expected_image_id = str(uuid.uuid4())
# 5KiB is smaller than WRITE_CHUNKSIZE
expected_s3_size = 5 * units.Ki
expected_s3_contents = b"*" * expected_s3_size
expected_checksum = hashlib.md5(expected_s3_contents,
usedforsecurity=False).hexdigest()
expected_multihash = hashlib.sha256(expected_s3_contents).hexdigest()
backend_conf = getattr(self.conf, self.backend)
expected_location = format_s3_location(
backend_conf.s3_store_access_key,
backend_conf.s3_store_secret_key,
backend_conf.s3_store_host,
backend_conf.s3_store_bucket,
expected_image_id)
image_s3 = io.BytesIO(expected_s3_contents)
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket
})
stubber.add_client_error(
method='head_object', service_error_code='404',
service_message='',
expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id})
stubber.add_response(method='put_object',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id,
'Body': botocore.stub.ANY
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
if self.multistore:
loc, size, checksum, multihash, metadata = \
self.store.add(expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
self.assertEqual(self.backend, metadata["store"])
else:
loc, size, checksum, multihash, _ = \
self.store.add(expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
self.assertEqual(expected_location, loc)
self.assertEqual(expected_s3_size, size)
self.assertEqual(expected_checksum, checksum)
self.assertEqual(expected_multihash, multihash)
def _test_add_singlepart_size_exceeding_max_size(self):
"""Test size validation during add for singlepart upload."""
self.store.WRITE_CHUNKSIZE = 1024
expected_image_id = str(uuid.uuid4())
# Provide a smaller expected size than actual data to trigger
# size mismatch
expected_s3_size = 5 * units.Ki
# 5KB more than expected
actual_s3_contents = b"*" * (expected_s3_size * 2)
image_s3 = io.BytesIO(actual_s3_contents)
backend_conf = getattr(self.conf, self.backend)
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket
})
stubber.add_client_error(
method='head_object', service_error_code='404',
service_message='', expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id})
stubber.add_response(method='put_object',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id,
'Body': botocore.stub.ANY
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
# Expect an exception due to size mismatch
self.assertRaisesRegex(exceptions.Invalid,
'Size exceeds: expected',
self.store.add,
expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
# Verify that the stream's position reflects the number of
# bytes read, which should be exactly at expected_file_size
# plus the last buffer size read.
actual_read_size = image_s3.tell()
expected_read = expected_s3_size + self.store.WRITE_CHUNKSIZE
self.assertEqual(actual_read_size, expected_read,
"The stream was not read only up to the"
" expected size.")
def _test_add_singlepart_write_less_than_declared(self):
"""Test size validation during add for singlepart upload."""
expected_image_id = str(uuid.uuid4())
# Provide a smaller expected size than actual data to trigger
# size mismatch
expected_s3_size = 5 * units.Ki
# 100 bytes smaller than expected
actual_s3_contents = b"*" * (5 * units.Ki - 100)
image_s3 = io.BytesIO(actual_s3_contents)
backend_conf = getattr(self.conf, self.backend)
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket
})
stubber.add_client_error(
method='head_object', service_error_code='404',
service_message='', expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id})
stubber.add_response(method='put_object',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id,
'Body': botocore.stub.ANY
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
# Expect an exception due to size mismatch
self.assertRaisesRegex(exceptions.Invalid,
'Size mismatch: expected',
self.store.add,
expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
# The input buffer should be fully read
# depending on implementation
total_input_size = len(actual_s3_contents)
self.assertEqual(image_s3.tell(), total_input_size)
def _test_add_singlepart_bigger_than_write_chunk(self):
"""Test that we can add a large image via the s3 backend."""
expected_image_id = str(uuid.uuid4())
# 8 MiB is bigger than WRITE_CHUNKSIZE(=5MiB),
# but smaller than s3_store_large_object_size
expected_s3_size = 8 * units.Mi
expected_s3_contents = b"*" * expected_s3_size
expected_checksum = hashlib.md5(expected_s3_contents,
usedforsecurity=False).hexdigest()
expected_multihash = hashlib.sha256(expected_s3_contents).hexdigest()
backend_conf = getattr(self.conf, self.backend)
expected_location = format_s3_location(
backend_conf.s3_store_access_key,
backend_conf.s3_store_secret_key,
backend_conf.s3_store_host,
backend_conf.s3_store_bucket,
expected_image_id)
image_s3 = io.BytesIO(expected_s3_contents)
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket
})
stubber.add_client_error(
method='head_object', service_error_code='404',
service_message='', expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id})
stubber.add_response(method='put_object',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id,
'Body': botocore.stub.ANY
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
if self.multistore:
loc, size, checksum, multihash, metadata = \
self.store.add(
expected_image_id, image_s3, expected_s3_size,
self.hash_algo)
self.assertEqual(self.backend, metadata["store"])
else:
loc, size, checksum, multihash, _ = \
self.store.add(expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
self.assertEqual(expected_location, loc)
self.assertEqual(expected_s3_size, size)
self.assertEqual(expected_checksum, checksum)
self.assertEqual(expected_multihash, multihash)
def _test_add_with_verifier(self):
"""Assert 'verifier.update' is called when verifier is provided."""
expected_image_id = str(uuid.uuid4())
expected_s3_size = 5 * units.Ki
expected_s3_contents = b"*" * expected_s3_size
image_s3 = io.BytesIO(expected_s3_contents)
fake_s3_client = botocore.session.get_session().create_client('s3')
verifier = mock.MagicMock(name='mock_verifier')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket', service_response={})
stubber.add_client_error(method='head_object',
service_error_code='404',
service_message='')
stubber.add_response(method='put_object', service_response={})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
self.store.add(expected_image_id, image_s3,
expected_s3_size, self.hash_algo,
verifier=verifier)
verifier.update.assert_called_with(expected_s3_contents)
def _test_add_multipart(self):
"""Test that we can add an image via the s3 backend."""
expected_image_id = str(uuid.uuid4())
expected_s3_size = 16 * units.Mi
expected_s3_contents = b"*" * expected_s3_size
expected_checksum = hashlib.md5(expected_s3_contents,
usedforsecurity=False).hexdigest()
expected_multihash = hashlib.sha256(expected_s3_contents).hexdigest()
backend_conf = getattr(self.conf, self.backend)
expected_location = format_s3_location(
backend_conf.s3_store_access_key,
backend_conf.s3_store_secret_key,
backend_conf.s3_store_host,
backend_conf.s3_store_bucket,
expected_image_id)
image_s3 = io.BytesIO(expected_s3_contents)
fake_s3_client = botocore.session.get_session().create_client('s3')
num_parts = 3 # image size is 16MB and chunk size is 6MB
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': backend_conf.s3_store_bucket
})
stubber.add_client_error(
method='head_object', service_error_code='404',
service_message='', expected_params={
'Bucket': backend_conf.s3_store_bucket,
'Key': expected_image_id})
stubber.add_response(method='create_multipart_upload',
service_response={
"Bucket": backend_conf.s3_store_bucket,
"Key": expected_image_id,
"UploadId": 'UploadId'
},
expected_params={
"Bucket": backend_conf.s3_store_bucket,
"Key": expected_image_id,
})
parts = []
remaining_image_size = expected_s3_size
large_chunk_size = backend_conf.s3_store_large_object_chunk_size
chunk_size = large_chunk_size * units.Mi
for i in range(num_parts):
part_number = i + 1
stubber.add_response(
method='upload_part',
service_response={'ETag': 'ETag'},
expected_params={
"Bucket": backend_conf.s3_store_bucket,
"Key": expected_image_id,
"Body": botocore.stub.ANY,
'ContentLength': chunk_size,
"PartNumber": part_number,
"UploadId": 'UploadId'
})
parts.append({'ETag': 'ETag', 'PartNumber': part_number})
remaining_image_size -= chunk_size
if remaining_image_size < chunk_size:
chunk_size = remaining_image_size
stubber.add_response(method='complete_multipart_upload',
service_response={
"Bucket": backend_conf.s3_store_bucket,
"Key": expected_image_id,
'ETag': 'ETag'
},
expected_params={
"Bucket": backend_conf.s3_store_bucket,
"Key": expected_image_id,
"MultipartUpload": {
"Parts": parts
},
"UploadId": 'UploadId'
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
if self.multistore:
loc, size, checksum, multihash, metadata = \
self.store.add(expected_image_id, image_s3,
expected_s3_size,
self.hash_algo)
self.assertEqual(self.backend, metadata["store"])
else:
loc, size, checksum, multihash, _ = \
self.store.add(expected_image_id, image_s3,
expected_s3_size,
self.hash_algo)
self.assertEqual(expected_location, loc)
self.assertEqual(expected_s3_size, size)
self.assertEqual(expected_checksum, checksum)
self.assertEqual(expected_multihash, multihash)
def _test_add_multipart_size_exceeding_max_size(self):
"""Test size validation during multipart upload."""
expected_image_id = str(uuid.uuid4())
expected_s3_size = 15 * units.Mi
self.store.WRITE_CHUNKSIZE = 5 * units.Mi
# Actual data is larger than expected to trigger size validation
# failure, 5MB larger
total_size = expected_s3_size + self.store.WRITE_CHUNKSIZE
expected_s3_contents = b"*" * total_size
image_s3 = io.BytesIO(expected_s3_contents)
conf = getattr(self.conf, self.backend)
fake_s3_client = botocore.session.get_session().create_client('s3')
# Patch abort_multipart_upload on the client to a Mock
fake_s3_client.abort_multipart_upload = mock.Mock()
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': conf.s3_store_bucket
})
stubber.add_client_error(method='head_object',
service_error_code='404',
service_message='',
expected_params={
'Bucket': conf.s3_store_bucket,
'Key': expected_image_id
})
stubber.add_response(method='create_multipart_upload',
service_response={
"Bucket": conf.s3_store_bucket,
"Key": expected_image_id,
"UploadId": 'UploadId'
},
expected_params={
"Bucket": conf.s3_store_bucket,
"Key": expected_image_id,
})
stubber.add_response(method='abort_multipart_upload',
service_response={},
expected_params={
'Bucket': conf.s3_store_bucket,
'Key': expected_image_id,
'UploadId': 'UploadId'
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
# Expect an exception due to size mismatch
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
# Verify that the stream's position reflects the number of
# bytes read, which should be exactly at expected_file_size
# plus the last buffer size read.
actual_read_size = image_s3.tell()
self.assertEqual(actual_read_size, total_size,
"The stream was not read only up to the "
"expected size.")
# Assert that abort_multipart_upload was called
fake_s3_client.abort_multipart_upload.assert_called_once_with(
Bucket=conf.s3_store_bucket,
Key=expected_image_id,
UploadId='UploadId'
)
def _test_add_multipart_write_less_than_declared(self):
"""Test size validation during multipart upload."""
expected_image_id = str(uuid.uuid4())
expected_s3_size = 16 * units.Mi
# Actual data is less than expected to trigger size validation
# failure, 1MB less
expected_s3_contents = b"*" * (expected_s3_size - 1 * units.Mi)
image_s3 = io.BytesIO(expected_s3_contents)
conf = getattr(self.conf, self.backend)
fake_s3_client = botocore.session.get_session().create_client('s3')
# Patch abort_multipart_upload on the client to a Mock
fake_s3_client.abort_multipart_upload = mock.Mock()
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': conf.s3_store_bucket
})
stubber.add_client_error(method='head_object',
service_error_code='404',
service_message='',
expected_params={
'Bucket': conf.s3_store_bucket,
'Key': expected_image_id
})
stubber.add_response(method='create_multipart_upload',
service_response={
"Bucket": conf.s3_store_bucket,
"Key": expected_image_id,
"UploadId": 'UploadId'
},
expected_params={
"Bucket": conf.s3_store_bucket,
"Key": expected_image_id,
})
stubber.add_response(method='abort_multipart_upload',
service_response={},
expected_params={
'Bucket': conf.s3_store_bucket,
'Key': expected_image_id,
'UploadId': 'UploadId'
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
# Expect an exception due to size mismatch
self.assertRaisesRegex(exceptions.Invalid,
'Size mismatch: expected',
self.store.add,
expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
# The input buffer should be fully read depending on
# implementation
total_input_size = len(expected_s3_contents)
self.assertEqual(image_s3.tell(), total_input_size)
# Assert that abort_multipart_upload was called
fake_s3_client.abort_multipart_upload.assert_called_once_with(
Bucket=conf.s3_store_bucket,
Key=expected_image_id,
UploadId='UploadId'
)
def _test_add_already_existing(self):
"""Tests that adding an image with an existing identifier
raises an appropriate exception
"""
image_s3 = io.BytesIO(b"never_gonna_make_it")
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket', service_response={})
stubber.add_response(method='head_object', service_response={})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
self.assertRaises(exceptions.Duplicate, self.store.add,
str(uuid.uuid4()), image_s3, 0,
self.hash_algo)
def _test_delete_non_existing(self):
"""Test trying to delete a s3 that doesn't exist raises an error."""
bucket, key = 'glance', 'no_exist'
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_client_error(method='head_object',
service_error_code='404',
service_message='''
The specified key does not exist.
''',
expected_params={
'Bucket': bucket,
'Key': key
})
fake_s3_client.head_bucket = mock.MagicMock()
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
uri = "s3://user:key@auth_address/%s/%s" % (bucket, key)
if self.multistore:
loc = location.get_location_from_uri_and_backend(
uri, self.backend, conf=self.conf)
else:
loc = location.get_location_from_uri(uri, conf=self.conf)
self.assertRaises(exceptions.NotFound, self.store.delete, loc)
def _do_test_get_s3_location(self, host, loc):
"""Helper method to test S3 location derivation"""
self.assertEqual(s3.get_s3_location(host), loc)
self.assertEqual(s3.get_s3_location(host + '/'), loc)
self.assertEqual(s3.get_s3_location(host + ':80'), loc)
self.assertEqual(s3.get_s3_location(host + ':80/'), loc)
self.assertEqual(s3.get_s3_location('http://' + host), loc)
self.assertEqual(s3.get_s3_location('http://' + host + '/'), loc)
self.assertEqual(s3.get_s3_location('http://' + host + ':80'), loc)
self.assertEqual(s3.get_s3_location('http://' + host + ':80/'), loc)
self.assertEqual(s3.get_s3_location('https://' + host), loc)
self.assertEqual(s3.get_s3_location('https://' + host + '/'), loc)
self.assertEqual(s3.get_s3_location('https://' + host + ':80'), loc)
self.assertEqual(s3.get_s3_location('https://' + host + ':80/'), loc)
def _test_get_invalid_bucket_name(self):
"""Test that invalid bucket names raise appropriate exceptions"""
if self.multistore:
self.config(s3_store_bucket_url_format='virtual',
group=self.backend)
invalid_buckets = ['not.dns.compliant', 'aa', 'bucket-']
for bucket in invalid_buckets:
loc = location.get_location_from_uri_and_backend(
"s3+https://user:key@auth_address/%s/key" % bucket,
self.backend, conf=self.conf)
self.assertRaises(boto_exceptions.InvalidDNSNameError,
self.store.get, loc)
else:
self.config(s3_store_bucket_url_format='virtual')
invalid_buckets = ['not.dns.compliant', 'aa', 'bucket-']
for bucket in invalid_buckets:
loc = location.get_location_from_uri(
"s3://user:key@auth_address/%s/key" % bucket,
conf=self.conf)
self.assertRaises(boto_exceptions.InvalidDNSNameError,
self.store.get, loc)
def _test_client_custom_region_name(self):
"""Test a custom s3_store_region_name in config"""
if self.multistore:
mock_loc = mock.MagicMock()
mock_loc.accesskey = 'abcd'
mock_loc.secretkey = 'efgh'
mock_loc.bucket = 'bucket1'
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
self.store._create_s3_client(mock_loc)
mock_client.assert_called_with(
config=mock.ANY,
endpoint_url='https://s3-region1.com',
region_name='custom_region_name',
service_name='s3',
use_ssl=False,
verify='path/to/cert/bundle.pem',
)
else:
self.config(s3_store_host='http://example.com')
self.config(s3_store_region_name='regionOne')
self.config(s3_store_bucket_url_format='path')
self.store.configure()
mock_loc = mock.MagicMock()
mock_loc.accesskey = 'abcd'
mock_loc.secretkey = 'efgh'
mock_loc.bucket = 'bucket1'
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
self.store._create_s3_client(mock_loc)
mock_client.assert_called_with(
config=mock.ANY,
endpoint_url='http://example.com',
region_name='regionOne',
service_name='s3',
use_ssl=False,
verify=None,
)
def _test_client_custom_ca_cert_bundle(self):
"""Test a custom s3_store_cacert in config"""
if self.multistore:
mock_loc = mock.MagicMock()
mock_loc.accesskey = 'abcd'
mock_loc.secretkey = 'efgh'
mock_loc.bucket = 'bucket1'
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
self.store._create_s3_client(mock_loc)
mock_client.assert_called_with(
config=mock.ANY,
endpoint_url='https://s3-region1.com',
region_name='custom_region_name',
service_name='s3',
use_ssl=False,
verify='path/to/cert/bundle.pem',
)
else:
self.config(s3_store_host='http://example.com')
self.config(s3_store_cacert='path/to/cert/bundle.pem')
self.config(s3_store_bucket_url_format='path')
self.store.configure()
mock_loc = mock.MagicMock()
mock_loc.accesskey = 'abcd'
mock_loc.secretkey = 'efgh'
mock_loc.bucket = 'bucket1'
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
self.store._create_s3_client(mock_loc)
mock_client.assert_called_with(
config=mock.ANY,
endpoint_url='http://example.com',
region_name=None,
service_name='s3',
use_ssl=False,
verify='path/to/cert/bundle.pem',
)
def _test_partial_get(self):
"""Test that partial get operations raise appropriate exceptions"""
if self.multistore:
loc = location.get_location_from_uri_and_backend(
"s3+https://user:key@auth_address/glance/%s" % FAKE_UUID,
self.backend, conf=self.conf)
else:
loc = location.get_location_from_uri(
"s3://user:key@auth_address/glance/%s" % FAKE_UUID,
conf=self.conf)
self.assertRaises(exceptions.StoreRandomGetNotSupported,
self.store.get, loc, chunk_size=1)
def _test_get_s3_good_location(self):
"""Test that the s3 location can be derived from the host"""
good_locations = [
('s3.amazonaws.com', ''),
('s3-us-east-1.amazonaws.com', 'us-east-1'),
('s3-us-east-2.amazonaws.com', 'us-east-2'),
('s3-us-west-1.amazonaws.com', 'us-west-1'),
('s3-us-west-2.amazonaws.com', 'us-west-2'),
('s3-ap-east-1.amazonaws.com', 'ap-east-1'),
('s3-ap-south-1.amazonaws.com', 'ap-south-1'),
('s3-ap-northeast-1.amazonaws.com', 'ap-northeast-1'),
('s3-ap-northeast-2.amazonaws.com', 'ap-northeast-2'),
('s3-ap-northeast-3.amazonaws.com', 'ap-northeast-3'),
('s3-ap-southeast-1.amazonaws.com', 'ap-southeast-1'),
('s3-ap-southeast-2.amazonaws.com', 'ap-southeast-2'),
('s3-ca-central-1.amazonaws.com', 'ca-central-1'),
('s3-cn-north-1.amazonaws.com.cn', 'cn-north-1'),
('s3-cn-northwest-1.amazonaws.com.cn', 'cn-northwest-1'),
('s3-eu-central-1.amazonaws.com', 'eu-central-1'),
('s3-eu-west-1.amazonaws.com', 'eu-west-1'),
('s3-eu-west-2.amazonaws.com', 'eu-west-2'),
('s3-eu-west-3.amazonaws.com', 'eu-west-3'),
('s3-eu-north-1.amazonaws.com', 'eu-north-1'),
('s3-sa-east-1.amazonaws.com', 'sa-east-1'),
]
for (url, expected) in good_locations:
self._do_test_get_s3_location(url, expected)
def _test_get_my_object_storage_location(self):
"""Test that the my object storage location convert to ''"""
my_object_storage_locations = [
('my-object-storage.com', ''),
('s3-my-object.jp', ''),
('192.168.100.12', ''),
]
for (url, expected) in my_object_storage_locations:
self._do_test_get_s3_location(url, expected)
def _test_no_access_key(self):
"""Tests that options without access key disables the add method"""
self.assertTrue(self._option_required(
's3_store_access_key'))
def _test_no_secret_key(self):
"""Tests that options without secret key disables the add method"""
self.assertTrue(self._option_required(
's3_store_secret_key'))
def _test_no_host(self):
"""Tests that options without host disables the add method"""
self.assertTrue(self._option_required(
's3_store_host'))
def _test_no_bucket(self):
"""Tests that options without bucket name disables the add method"""
self.assertTrue(self._option_required(
's3_store_bucket'))
def _option_required(self, key):
"""Helper method to test if a configuration option is required"""
conf = S3_CONF.copy()
conf[key] = None
try:
self.config(**conf, group=self.backend)
self.store = s3.Store(self.conf)
self.store.configure()
return not self.store.is_capable(
capabilities.BitMasks.WRITE_ACCESS)
except Exception:
return False
def _test_add_different_backend(self):
"""Test adding to a different backend in multistore mode"""
if not self.multistore:
self.skipTest("This test is only for multistore mode")
# Switch to different backend
self.store = s3.Store(self.conf, backend="s3_region2")
self.store.configure()
self.register_store_backend_schemes(self.store, 's3', 's3_region2')
expected_image_id = str(uuid.uuid4())
expected_s3_size = FIVE_KB
expected_s3_contents = b"*" * expected_s3_size
expected_checksum = hashlib.md5(expected_s3_contents,
usedforsecurity=False).hexdigest()
expected_multihash = hashlib.sha256(expected_s3_contents).hexdigest()
expected_location = format_s3_location(
S3_CONF['s3_store_access_key'],
S3_CONF['s3_store_secret_key'],
'http://s3-region2.com',
S3_CONF['s3_store_bucket'],
expected_image_id)
image_s3 = io.BytesIO(expected_s3_contents)
fake_s3_client = botocore.session.get_session().create_client('s3')
with stub.Stubber(fake_s3_client) as stubber:
stubber.add_response(method='head_bucket',
service_response={},
expected_params={
'Bucket': S3_CONF['s3_store_bucket']
})
stubber.add_client_error(method='head_object',
service_error_code='404',
service_message='',
expected_params={
'Bucket': S3_CONF['s3_store_bucket'],
'Key': expected_image_id
})
stubber.add_response(method='put_object',
service_response={},
expected_params={
'Bucket': S3_CONF['s3_store_bucket'],
'Key': expected_image_id,
'Body': botocore.stub.ANY
})
with mock.patch.object(
boto3.session.Session, "client") as mock_client:
mock_client.return_value = fake_s3_client
loc, size, checksum, multihash, metadata = \
self.store.add(expected_image_id, image_s3,
expected_s3_size,
self.hash_algo)
self.assertEqual("s3_region2", metadata["store"])
self.assertEqual(expected_location, loc)
self.assertEqual(expected_s3_size, size)
self.assertEqual(expected_checksum, checksum)
self.assertEqual(expected_multihash, multihash)
def _test_config_with_data_integrity_protection_disabled(self):
"""Test Config creation when data integrity protection is disabled."""
# Explicitly disable data integrity protection
self.config(s3_store_enable_data_integrity_protection=False,
group=self.backend)
self.store.configure()
# Create a mock location with required attributes
mock_loc = mock.Mock()
mock_loc.accesskey = 'access_key'
mock_loc.secretkey = 'secret_key'
mock_loc.bucket = 'test_bucket'
with mock.patch('botocore.client.Config') as mock_config, \
mock.patch('boto3.session.Session.client'):
mock_config.return_value = mock.Mock()
self.store._create_s3_client(mock_loc)
# When data integrity protection is disabled,
# both checksum options should be 'when_required'
mock_config.assert_called_once()
call_args = mock_config.call_args
self.assertEqual('when_required',
call_args.kwargs['request_checksum_calculation'])
self.assertEqual('when_required',
call_args.kwargs['response_checksum_validation'])
def _test_config_with_data_integrity_protection_enabled(self):
"""Test Config creation when data integrity protection is enabled."""
# Enable data integrity protection and set custom checksum options
self.config(s3_store_enable_data_integrity_protection=True,
s3_store_request_checksum_calculation='when_supported',
s3_store_response_checksum_validation='when_supported',
group=self.backend)
self.store.configure()
# Create a mock location with required attributes
mock_loc = mock.Mock()
mock_loc.accesskey = 'access_key'
mock_loc.secretkey = 'secret_key'
mock_loc.bucket = 'test_bucket'
with mock.patch('botocore.client.Config') as mock_config, \
mock.patch('boto3.session.Session.client'):
mock_config.return_value = mock.Mock()
self.store._create_s3_client(mock_loc)
# When data integrity protection is enabled,
# checksum options should use the configured values
mock_config.assert_called_once()
call_args = mock_config.call_args
self.assertEqual('when_supported',
call_args.kwargs['request_checksum_calculation'])
self.assertEqual('when_supported',
call_args.kwargs['response_checksum_validation'])
def _test_config_fallback_for_old_boto3(self):
"""Test Config fallback when boto3 < 1.36.0 (no checksum support)."""
# Create a mock location with required attributes
mock_loc = mock.Mock()
mock_loc.accesskey = 'access_key'
mock_loc.secretkey = 'secret_key'
mock_loc.bucket = 'test_bucket'
# Simulate old boto3 version by making Config raise TypeError
# when checksum parameters are passed
def config_side_effect(*args, **kwargs):
if 'request_checksum_calculation' in kwargs:
raise TypeError("Unexpected keyword argument")
return mock.Mock()
with mock.patch('botocore.client.Config',
side_effect=config_side_effect) as mock_config, \
mock.patch('boto3.session.Session.client'):
self.store._create_s3_client(mock_loc)
# Should have been called twice: once with checksum params
# (which failed), then without them
self.assertEqual(2, mock_config.call_count)
# First call should have checksum parameters
first_call_kwargs = mock_config.call_args_list[0].kwargs
self.assertIn('request_checksum_calculation',
first_call_kwargs)
self.assertIn('response_checksum_validation',
first_call_kwargs)
# Second call should not have checksum parameters
second_call_kwargs = mock_config.call_args_list[1].kwargs
self.assertNotIn('request_checksum_calculation',
second_call_kwargs)
self.assertNotIn('response_checksum_validation',
second_call_kwargs)
|