|
| 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 | + |
| 22 | +from acktest.resources import random_suffix_name |
| 23 | +from acktest.k8s import resource as k8s |
| 24 | +from acktest import adoption as adoption |
| 25 | +from acktest import tags as tags |
| 26 | +from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_s3_resource |
| 27 | +from e2e.tests.test_bucket import bucket_exists, get_bucket |
| 28 | +from e2e.replacement_values import REPLACEMENT_VALUES |
| 29 | + |
| 30 | +CREATE_WAIT_AFTER_SECONDS = 10 |
| 31 | +MODIFY_WAIT_AFTER_SECONDS = 10 |
| 32 | +DELETE_WAIT_AFTER_SECONDS = 10 |
| 33 | + |
| 34 | +class AdoptionPolicy(str, Enum): |
| 35 | + NONE = "" |
| 36 | + ADOPT = "adopt" |
| 37 | + ADOPT_OR_CREATE = "adopt-or-create" |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope="module") |
| 41 | +def adoption_policy_adopt_bucket(s3_client): |
| 42 | + replacements = REPLACEMENT_VALUES.copy() |
| 43 | + bucket_name = replacements["ADOPTION_BUCKET_NAME"] |
| 44 | + replacements["ADOPTION_POLICY"] = AdoptionPolicy.ADOPT |
| 45 | + replacements["ADOPTION_FIELDS"] = f'{{\\\"name\\\": \\\"{bucket_name}\\\"}}' |
| 46 | + |
| 47 | + resource_data = load_s3_resource( |
| 48 | + "bucket_adoption_policy", |
| 49 | + additional_replacements=replacements, |
| 50 | + ) |
| 51 | + |
| 52 | + # Create k8s resource |
| 53 | + ref = k8s.CustomResourceReference( |
| 54 | + CRD_GROUP, CRD_VERSION, "buckets", |
| 55 | + bucket_name, namespace="default") |
| 56 | + k8s.create_custom_resource(ref, resource_data) |
| 57 | + |
| 58 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 59 | + cr = k8s.wait_resource_consumed_by_controller(ref) |
| 60 | + |
| 61 | + assert cr is not None |
| 62 | + assert k8s.get_resource_exists(ref) |
| 63 | + |
| 64 | + yield (ref, cr) |
| 65 | + |
| 66 | + _, deleted = k8s.delete_custom_resource(ref, DELETE_WAIT_AFTER_SECONDS) |
| 67 | + assert deleted |
| 68 | + |
| 69 | +@service_marker |
| 70 | +@pytest.mark.canary |
| 71 | +class TestAdoptionPolicyBucket: |
| 72 | + def test_adoption_policy( |
| 73 | + self, s3_client, adoption_policy_adopt_bucket, s3_resource |
| 74 | + ): |
| 75 | + (ref, cr) = adoption_policy_adopt_bucket |
| 76 | + |
| 77 | + # Spec will be added by controller |
| 78 | + assert 'spec' in cr |
| 79 | + assert 'name' in cr['spec'] |
| 80 | + bucket_name = cr['spec']['name'] |
| 81 | + |
| 82 | + updates = { |
| 83 | + "spec": { |
| 84 | + "versioning": { |
| 85 | + "status": "Suspended" |
| 86 | + }, |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + k8s.patch_custom_resource(ref, updates) |
| 91 | + time.sleep(MODIFY_WAIT_AFTER_SECONDS) |
| 92 | + |
| 93 | + cr = k8s.wait_resource_consumed_by_controller(ref) |
| 94 | + assert cr is not None |
| 95 | + assert 'spec' in cr |
| 96 | + assert 'versioning' in cr['spec'] |
| 97 | + assert 'status' in cr['spec']['versioning'] |
| 98 | + status = cr['spec']['versioning']['status'] |
| 99 | + latest = get_bucket(s3_resource, bucket_name) |
| 100 | + assert latest is not None |
| 101 | + versioning = latest.Versioning() |
| 102 | + assert versioning.status == status |
| 103 | + |
| 104 | + |
0 commit comments