|
| 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 Vpc Endpoint Service Configuraion Adoption. |
| 15 | +""" |
| 16 | + |
| 17 | +# Default to us-west-2 since that's where prow is deployed |
| 18 | +import logging |
| 19 | +from os import environ |
| 20 | +import time |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from e2e import service_marker |
| 25 | +from e2e import CRD_GROUP, CRD_VERSION, load_ec2_resource |
| 26 | +from e2e.bootstrap_resources import get_bootstrap_resources |
| 27 | +from e2e.replacement_values import REPLACEMENT_VALUES |
| 28 | +from acktest.resources import random_suffix_name |
| 29 | +from acktest.k8s import resource as k8s |
| 30 | + |
| 31 | +from e2e.tests.helper import EC2Validator |
| 32 | + |
| 33 | + |
| 34 | +REGION = "us-west-2" if environ.get('AWS_DEFAULT_REGION') is None else environ.get('AWS_DEFAULT_REGION') |
| 35 | +RESOURCE_PLURAL = "vpcendpointserviceconfigurations" |
| 36 | + |
| 37 | +CREATE_WAIT_AFTER_SECONDS = 10 |
| 38 | +DELETE_WAIT_AFTER_SECONDS = 10 |
| 39 | +MODIFY_WAIT_AFTER_SECONDS = 5 |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def vpc_endpoint_service_adoption(): |
| 43 | + replacements = REPLACEMENT_VALUES.copy() |
| 44 | + resource_name = random_suffix_name("vpc-es-adoption", 32) |
| 45 | + service_id = get_bootstrap_resources().AdoptedVpcEndpointService.service_id |
| 46 | + replacements["VPC_ADOPTION_NAME"] = resource_name |
| 47 | + replacements["ADOPTION_POLICY"] = "adopt" |
| 48 | + replacements["ADOPTION_FIELDS"] = f"{{\\\"serviceId\\\": \\\"{service_id}\\\"}}" |
| 49 | + |
| 50 | + resource_data = load_ec2_resource( |
| 51 | + "vpc_endpoint_service_adoption", |
| 52 | + additional_replacements=replacements, |
| 53 | + ) |
| 54 | + logging.debug(resource_data) |
| 55 | + |
| 56 | + ref = k8s.CustomResourceReference( |
| 57 | + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL, |
| 58 | + resource_name, namespace="default", |
| 59 | + ) |
| 60 | + |
| 61 | + k8s.create_custom_resource(ref, resource_data) |
| 62 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 63 | + |
| 64 | + cr = k8s.wait_resource_consumed_by_controller(ref) |
| 65 | + assert cr is not None |
| 66 | + assert k8s.get_resource_exists(ref) |
| 67 | + |
| 68 | + yield (ref, cr) |
| 69 | + |
| 70 | + _, deleted = k8s.delete_custom_resource(ref, DELETE_WAIT_AFTER_SECONDS) |
| 71 | + assert deleted |
| 72 | + |
| 73 | +@service_marker |
| 74 | +@pytest.mark.canary |
| 75 | +class TestVpcAdoption: |
| 76 | + |
| 77 | + def test_vpc_endpoint_service_configuration_adopt_update(self, ec2_client, vpc_endpoint_service_adoption): |
| 78 | + (ref, cr) = vpc_endpoint_service_adoption |
| 79 | + |
| 80 | + assert cr is not None |
| 81 | + assert 'status' in cr |
| 82 | + assert 'serviceID' in cr['status'] |
| 83 | + resource_id = cr['status']['serviceID'] |
| 84 | + |
| 85 | + assert 'spec' in cr |
| 86 | + assert 'tags' in cr['spec'] |
| 87 | + |
| 88 | + # Check VPC Endpoint Service exists in AWS |
| 89 | + ec2_validator = EC2Validator(ec2_client) |
| 90 | + ec2_validator.assert_vpc_endpoint_service_configuration(resource_id) |
| 91 | + |
| 92 | + endpoint_service_config = ec2_validator.get_vpc_endpoint_service_configuration(resource_id) |
| 93 | + assert len(endpoint_service_config['Tags']) == 1 |
| 94 | + current_tag = endpoint_service_config['Tags'][0] |
| 95 | + new_tag = {'Key': 'TestName', 'Value': 'test-value'} |
| 96 | + |
| 97 | + updates = { |
| 98 | + "spec": {"tags": [current_tag, new_tag]} |
| 99 | + } |
| 100 | + k8s.patch_custom_resource(ref, updates) |
| 101 | + time.sleep(MODIFY_WAIT_AFTER_SECONDS) |
| 102 | + |
| 103 | + assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5) |
| 104 | + |
| 105 | + endpoint_service_config = ec2_validator.get_vpc_endpoint_service_configuration(resource_id) |
| 106 | + assert len(endpoint_service_config['Tags']) == 2 |
| 107 | + assert endpoint_service_config['Tags'][0] == current_tag |
| 108 | + assert endpoint_service_config['Tags'][1] == new_tag |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments