|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +# not use this file except in compliance with the License. A copy of the |
| 5 | +# 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 distributed |
| 10 | +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +# express or implied. See the License for the specific language governing |
| 12 | +# permissions and limitations under the License. |
| 13 | + |
| 14 | +"""Integration tests for the deletion policy annotation on Bucket. |
| 15 | +""" |
| 16 | + |
| 17 | +from enum import Enum |
| 18 | +import pytest |
| 19 | +import time |
| 20 | +import logging |
| 21 | +import itertools |
| 22 | +from typing import TYPE_CHECKING, Generator, List, NamedTuple, Tuple |
| 23 | + |
| 24 | +from acktest.resources import random_suffix_name |
| 25 | +from acktest.k8s import resource as k8s |
| 26 | +from acktest import adoption as adoption |
| 27 | +from acktest import tags as tags |
| 28 | +from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_s3_resource |
| 29 | +from e2e.tests.test_bucket import Bucket, bucket_exists, create_bucket, delete_bucket |
| 30 | +from e2e.replacement_values import REPLACEMENT_VALUES |
| 31 | + |
| 32 | +CREATE_WAIT_AFTER_SECONDS = 10 |
| 33 | +MODIFY_WAIT_AFTER_SECONDS = 10 |
| 34 | +DELETE_WAIT_AFTER_SECONDS = 10 |
| 35 | + |
| 36 | +class AdoptionPolicy(str, Enum): |
| 37 | + NONE = "" |
| 38 | + ADOPT = "adopt" |
| 39 | + ADOPT_OR_CREATE = "adopt-or-create" |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(scope="function") |
| 43 | +def adoption_policy_adopt_bucket(s3_client): |
| 44 | + replacements = REPLACEMENT_VALUES.copy() |
| 45 | + bucket_name = replacements["REPLICATION_BUCKET_NAME"] |
| 46 | + replacements["BUCKET_NAME"] = bucket_name |
| 47 | + replacements["ADOPTION_POLICY"] = AdoptionPolicy.ADOPT |
| 48 | + replacements["ADOPTION_FIELDS"] = f'{{\"name\": \"{bucket_name}\"}}' |
| 49 | + |
| 50 | + resource_data = load_s3_resource( |
| 51 | + "bucket_adoption_policy", |
| 52 | + additional_replacements=replacements, |
| 53 | + ) |
| 54 | + |
| 55 | + # Create k8s resource |
| 56 | + ref = k8s.CustomResourceReference( |
| 57 | + CRD_GROUP, CRD_VERSION, "buckets", |
| 58 | + bucket_name, namespace="default") |
| 59 | + k8s.create_custom_resource(ref, resource_data) |
| 60 | + |
| 61 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 62 | + cr = k8s.wait_resource_consumed_by_controller(ref) |
| 63 | + |
| 64 | + assert cr is not None |
| 65 | + assert k8s.get_resource_exists(ref) |
| 66 | + |
| 67 | + yield (ref, cr) |
| 68 | + |
| 69 | + _, deleted = k8s.delete_custom_resource(ref, DELETE_WAIT_AFTER_SECONDS) |
| 70 | + assert deleted |
| 71 | + |
| 72 | + |
| 73 | +class TestAdoptionPolicyBucket: |
| 74 | + @pytest.mark.parametrize() |
| 75 | + def test_deletion_policy( |
| 76 | + self, adoption_policy_adopt_bucket, s3_client |
| 77 | + ): |
| 78 | + (ref, cr) = adoption_policy_adopt_bucket |
| 79 | + |
| 80 | + # Spec will be added by controller |
| 81 | + assert 'spec' in cr |
| 82 | + assert 'name' in cr['spec'] |
| 83 | + bucket_name = cr['spec']['name'] |
| 84 | + assert k8s.assert_synced(ref) |
| 85 | + # Check that the bucket exists in S3 |
| 86 | + assert bucket_exists(s3_client, bucket_name) |
| 87 | + |
0 commit comments