|
| 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 logging |
| 20 | +import itertools |
| 21 | +from typing import TYPE_CHECKING, Generator, List, NamedTuple, Tuple |
| 22 | + |
| 23 | +from acktest.resources import random_suffix_name |
| 24 | +from acktest.k8s import resource as k8s |
| 25 | +from acktest import adoption as adoption |
| 26 | +from acktest import tags as tags |
| 27 | +from e2e import SERVICE_NAME |
| 28 | +from e2e.tests.test_bucket import Bucket, bucket_exists, create_bucket, delete_bucket |
| 29 | + |
| 30 | +DELETION_POLICY_RESOURCE_ANNOTATION_KEY = "services.k8s.aws/deletion-policy" |
| 31 | +DELETION_POLICY_NAMESPACE_ANNOTATION_KEY = ( |
| 32 | + f"{SERVICE_NAME}.services.k8s.aws/deletion-policy" |
| 33 | +) |
| 34 | + |
| 35 | +class DeletionPolicy(str, Enum): |
| 36 | + NONE = "" |
| 37 | + DELETE = "delete" |
| 38 | + RETAIN = "retain" |
| 39 | + |
| 40 | + |
| 41 | +# DeletionPolicyAnnotationTuple represents a tuple of namespace and resource |
| 42 | +# deletion policy annotations. These are used when testing all combinations of |
| 43 | +# each annotation. |
| 44 | +class DeletionPolicyAnnotationTuple(NamedTuple): |
| 45 | + namespace: DeletionPolicy |
| 46 | + resource: DeletionPolicy |
| 47 | + |
| 48 | + |
| 49 | +# Create a matrix of combinations for deletion policy annotations that can be |
| 50 | +# used as a parameter for tests |
| 51 | +DELETION_POLICY_ANNOTATION_COMBINATIONS: List[DeletionPolicyAnnotationTuple] = [ |
| 52 | + DeletionPolicyAnnotationTuple(r[0], r[1]) |
| 53 | + for r in itertools.product([p for p in DeletionPolicy], [p for p in DeletionPolicy]) |
| 54 | +] |
| 55 | + |
| 56 | +# Parameter types are not support by pytest. This adds support for type checking |
| 57 | +# the param type. |
| 58 | +if TYPE_CHECKING: |
| 59 | + |
| 60 | + class DeletionPolicyFixtureRequest: |
| 61 | + param: DeletionPolicyAnnotationTuple |
| 62 | + |
| 63 | +else: |
| 64 | + from typing import Any |
| 65 | + |
| 66 | + DeletionPolicyFixtureRequest = Any |
| 67 | + |
| 68 | + |
| 69 | +def create_deletion_policy_namespace(deletion_policy: DeletionPolicy) -> str: |
| 70 | + namespace_name = random_suffix_name("s3-deletion-policy", 24) |
| 71 | + annotations = {} |
| 72 | + if deletion_policy != DeletionPolicy.NONE: |
| 73 | + annotations[DELETION_POLICY_NAMESPACE_ANNOTATION_KEY] = deletion_policy.value |
| 74 | + |
| 75 | + logging.info(f"Creating namespace {namespace_name}") |
| 76 | + try: |
| 77 | + k8s.create_k8s_namespace(namespace_name, annotations) |
| 78 | + except Exception as ex: |
| 79 | + return pytest.fail("Failed to create namespace") |
| 80 | + |
| 81 | + return namespace_name |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture(scope="function") |
| 85 | +def deletion_policy_namespace_bucket( |
| 86 | + request: DeletionPolicyFixtureRequest, s3_client |
| 87 | +) -> Generator[Tuple[Bucket, DeletionPolicyAnnotationTuple], None, None]: |
| 88 | + bucket_namespace = create_deletion_policy_namespace(request.param.namespace) |
| 89 | + |
| 90 | + bucket = None |
| 91 | + try: |
| 92 | + if request.param.resource == DeletionPolicy.NONE: |
| 93 | + bucket = create_bucket("bucket", namespace=bucket_namespace) |
| 94 | + else: |
| 95 | + bucket = create_bucket( |
| 96 | + "bucket_deletion_policy", |
| 97 | + namespace=bucket_namespace, |
| 98 | + additional_replacements={ |
| 99 | + "DELETION_POLICY": request.param.resource.value |
| 100 | + }, |
| 101 | + ) |
| 102 | + |
| 103 | + assert k8s.get_resource_exists(bucket.ref) |
| 104 | + |
| 105 | + exists = bucket_exists(s3_client, bucket) |
| 106 | + assert exists |
| 107 | + except: |
| 108 | + if bucket is not None: |
| 109 | + delete_bucket(bucket) |
| 110 | + return pytest.fail("Bucket failed to create") |
| 111 | + |
| 112 | + yield (bucket, request.param) |
| 113 | + |
| 114 | + delete_bucket(bucket) |
| 115 | + |
| 116 | + exists = bucket_exists(s3_client, bucket) |
| 117 | + if exists: |
| 118 | + s3_client.delete_bucket(Bucket=bucket.resource_name) |
| 119 | + |
| 120 | + k8s.delete_k8s_namespace(bucket_namespace) |
| 121 | + |
| 122 | + |
| 123 | +class TestDeletionPolicyBucket: |
| 124 | + @pytest.mark.parametrize( |
| 125 | + "deletion_policy_namespace_bucket", |
| 126 | + DELETION_POLICY_ANNOTATION_COMBINATIONS, |
| 127 | + indirect=True, |
| 128 | + ) |
| 129 | + def test_deletion_policy( |
| 130 | + self, s3_client, deletion_policy_namespace_bucket |
| 131 | + ): |
| 132 | + (bucket, deletion_policy_annotations) = deletion_policy_namespace_bucket |
| 133 | + |
| 134 | + delete_bucket(bucket) |
| 135 | + |
| 136 | + exists = bucket_exists(s3_client, bucket) |
| 137 | + |
| 138 | + # Assert in order of precedence (resource > namespace) |
| 139 | + if deletion_policy_annotations.resource == DeletionPolicy.DELETE: |
| 140 | + assert not exists |
| 141 | + elif deletion_policy_annotations.resource == DeletionPolicy.RETAIN: |
| 142 | + assert exists |
| 143 | + elif deletion_policy_annotations.namespace == DeletionPolicy.DELETE: |
| 144 | + assert not exists |
| 145 | + elif deletion_policy_annotations.namespace == DeletionPolicy.RETAIN: |
| 146 | + assert exists |
| 147 | + else: # Neither has an annotation |
| 148 | + assert not exists |
0 commit comments