|
1 | 1 | import pathlib |
2 | | -from unittest.mock import patch |
| 2 | +from unittest.mock import MagicMock, patch |
3 | 3 |
|
| 4 | +import botocore.exceptions |
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from awspub import context, exceptions, image |
@@ -494,3 +495,62 @@ def test_image__share_list_filtered(partition, imagename, share_list_expected): |
494 | 495 | ctx = context.Context(curdir / "fixtures/config1.yaml", None) |
495 | 496 | img = image.Image(ctx, imagename) |
496 | 497 | assert img._share_list_filtered(img.conf["share"]) == share_list_expected |
| 498 | + |
| 499 | + |
| 500 | +@patch("awspub.s3.S3.bucket_region", return_value="region1") |
| 501 | +def test_create__should_allow_partial_registration(s3_bucket_mock): |
| 502 | + """ |
| 503 | + Test that the create() method allows a partial upload set |
| 504 | + """ |
| 505 | + with patch("boto3.client") as bclient_mock: |
| 506 | + instance = bclient_mock.return_value |
| 507 | + |
| 508 | + ctx = context.Context(curdir / "fixtures/config1.yaml", None) |
| 509 | + img = image.Image(ctx, "test-image-6") |
| 510 | + img._image_regions = ["region1", "region2"] |
| 511 | + img._image_regions_cached = True |
| 512 | + with patch.object(img, "_get") as get_mock, patch.object(img._snapshot, "copy") as copy_mock: |
| 513 | + copy_mock.return_value = {r: f"snapshot{i}" for i, r in enumerate(img.image_regions)} |
| 514 | + get_mock.return_value = None |
| 515 | + instance.register_image.side_effect = [ |
| 516 | + botocore.exceptions.ClientError( |
| 517 | + { |
| 518 | + "Error": { |
| 519 | + "Code": "OperationNotPermitted", |
| 520 | + "Message": "Intentional permission failure for snapshot0", |
| 521 | + } |
| 522 | + }, |
| 523 | + "awspub Testing", |
| 524 | + ), |
| 525 | + {"ImageId": "id1"}, |
| 526 | + ] |
| 527 | + with pytest.raises(exceptions.IncompleteImageSetException): |
| 528 | + img.create() == {"region2": image._ImageInfo("id1", "snapshot1")} |
| 529 | + # register and create_tags should be called since at least one snapshot made it |
| 530 | + assert instance.register_image.called |
| 531 | + assert instance.create_tags.called |
| 532 | + |
| 533 | + |
| 534 | +def test_register_image__should_return_none_on_permission_failures(): |
| 535 | + instance = MagicMock() |
| 536 | + |
| 537 | + instance.register_image.side_effect = botocore.exceptions.ClientError( |
| 538 | + {"Error": {"Code": "OperationNotPermitted", "Message": "Testing"}}, "Testing" |
| 539 | + ) |
| 540 | + ctx = context.Context(curdir / "fixtures/config1.yaml", None) |
| 541 | + img = image.Image(ctx, "test-image-6") |
| 542 | + snapshot_ids = {"eu-central-1": "my-snapshot"} |
| 543 | + assert img._register_image(snapshot_ids["eu-central-1"], instance) is None |
| 544 | + |
| 545 | + |
| 546 | +def test_register_image__should_raise_on_unhandled_client_error(): |
| 547 | + instance = MagicMock() |
| 548 | + |
| 549 | + instance.register_image.side_effect = botocore.exceptions.ClientError( |
| 550 | + {"Error": {"Code": "UnsupportedOperation", "Message": "Testing"}}, "Testing" |
| 551 | + ) |
| 552 | + ctx = context.Context(curdir / "fixtures/config1.yaml", None) |
| 553 | + img = image.Image(ctx, "test-image-6") |
| 554 | + snapshot_ids = {"eu-central-1": "my-snapshot"} |
| 555 | + with pytest.raises(botocore.exceptions.ClientError): |
| 556 | + img._register_image(snapshot_ids["eu-central-1"], instance) is None |
0 commit comments