|
| 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 DHCPOptions API. |
| 15 | +""" |
| 16 | + |
| 17 | +import pytest |
| 18 | +import time |
| 19 | +import logging |
| 20 | + |
| 21 | +from acktest import tags |
| 22 | +from acktest.resources import random_suffix_name |
| 23 | +from acktest.k8s import resource as k8s |
| 24 | +from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource |
| 25 | +from e2e.replacement_values import REPLACEMENT_VALUES |
| 26 | +from e2e.bootstrap_resources import get_bootstrap_resources |
| 27 | +from e2e.tests.helper import EC2Validator |
| 28 | + |
| 29 | +RESOURCE_PLURAL = "dhcpoptions" |
| 30 | + |
| 31 | +DEFAULT_WAIT_AFTER_SECONDS = 5 |
| 32 | +CREATE_WAIT_AFTER_SECONDS = 10 |
| 33 | +DELETE_WAIT_AFTER_SECONDS = 10 |
| 34 | +MODIFY_WAIT_AFTER_SECONDS = 5 |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def simple_dhcp_options(request,simple_vpc): |
| 38 | + replacements = REPLACEMENT_VALUES.copy() |
| 39 | + resource_name = random_suffix_name("dhcp-opts-test", 24) |
| 40 | + resource_file = "dhcp_options" |
| 41 | + |
| 42 | + replacements["DHCP_OPTIONS_NAME"] = resource_name |
| 43 | + replacements["DHCP_KEY_1"] = "domain-name" |
| 44 | + replacements["DHCP_VAL_1"] = "ack-example.com" |
| 45 | + replacements["DHCP_KEY_2"] = "domain-name-servers" |
| 46 | + replacements["DHCP_VAL_2_1"] = "10.2.5.1" |
| 47 | + replacements["DHCP_VAL_2_2"] = "10.2.5.2" |
| 48 | + |
| 49 | + marker = request.node.get_closest_marker("resource_data") |
| 50 | + if marker is not None: |
| 51 | + data = marker.args[0] |
| 52 | + if 'resource_file' in data: |
| 53 | + resource_file = data['resource_file'] |
| 54 | + if 'create_vpc' in data and data['create_vpc'] is True: |
| 55 | + (_, vpc_cr) = simple_vpc |
| 56 | + vpc_id = vpc_cr["status"]["vpcID"] |
| 57 | + replacements["VPC_ID"] = vpc_id |
| 58 | + if 'dhcp_key_1' in data: |
| 59 | + replacements["DHCP_KEY_1"] = data['dhcp_key_1'] |
| 60 | + if 'tag_key' in data: |
| 61 | + replacements["TAG_KEY"] = data['tag_key'] |
| 62 | + if 'tag_value' in data: |
| 63 | + replacements["TAG_VALUE"] = data['tag_value'] |
| 64 | + |
| 65 | + |
| 66 | + # Load DHCP Options CR |
| 67 | + resource_data = load_ec2_resource( |
| 68 | + resource_file, |
| 69 | + additional_replacements=replacements, |
| 70 | + ) |
| 71 | + |
| 72 | + # Create k8s resource |
| 73 | + ref = k8s.CustomResourceReference( |
| 74 | + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL, |
| 75 | + resource_name, namespace="default", |
| 76 | + ) |
| 77 | + k8s.create_custom_resource(ref, resource_data) |
| 78 | + |
| 79 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 80 | + |
| 81 | + # Get latest DHCP Options CR |
| 82 | + cr = k8s.wait_resource_consumed_by_controller(ref) |
| 83 | + |
| 84 | + assert cr is not None |
| 85 | + assert k8s.get_resource_exists(ref) |
| 86 | + |
| 87 | + yield (ref, cr) |
| 88 | + |
| 89 | + # Try to delete, if doesn't already exist |
| 90 | + try: |
| 91 | + _, deleted = k8s.delete_custom_resource(ref, 3, 10) |
| 92 | + assert deleted |
| 93 | + except: |
| 94 | + pass |
| 95 | + |
| 96 | +@service_marker |
| 97 | +@pytest.mark.canary |
| 98 | +class TestDhcpOptions: |
| 99 | + def test_create_delete(self, ec2_client, simple_dhcp_options): |
| 100 | + (ref, cr) = simple_dhcp_options |
| 101 | + |
| 102 | + resource_id = cr["status"]["dhcpOptionsID"] |
| 103 | + |
| 104 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 105 | + |
| 106 | + # Check DHCP Options exists in AWS |
| 107 | + ec2_validator = EC2Validator(ec2_client) |
| 108 | + ec2_validator.assert_dhcp_options(resource_id) |
| 109 | + |
| 110 | + # Delete k8s resource |
| 111 | + _, deleted = k8s.delete_custom_resource(ref) |
| 112 | + assert deleted is True |
| 113 | + |
| 114 | + time.sleep(DELETE_WAIT_AFTER_SECONDS) |
| 115 | + |
| 116 | + # Check DHCP Options no longer exists in AWS |
| 117 | + ec2_validator.assert_dhcp_options(resource_id, exists=False) |
| 118 | + |
| 119 | + @pytest.mark.resource_data({'dhcp_key_1': 'InvalidValue'}) |
| 120 | + def test_terminal_condition_invalid_parameter_value(self, simple_dhcp_options): |
| 121 | + (ref, _) = simple_dhcp_options |
| 122 | + |
| 123 | + expected_msg = "InvalidParameterValue: Value (InvalidValue) for parameter name is invalid. Unknown DHCP option" |
| 124 | + terminal_condition = k8s.get_resource_condition(ref, "ACK.Terminal") |
| 125 | + # Example condition message: |
| 126 | + # An error occurred (InvalidParameterValue) when calling the CreateDhcpOptions operation: |
| 127 | + # Value (InvalidValue) for parameter value is invalid. |
| 128 | + # Unknown DHCP option |
| 129 | + assert expected_msg in terminal_condition['message'] |
| 130 | + |
| 131 | + @pytest.mark.resource_data({'tag_key': 'initialtagkey', 'tag_value': 'initialtagvalue'}) |
| 132 | + def test_crud_tags(self, ec2_client, simple_dhcp_options): |
| 133 | + (ref, cr) = simple_dhcp_options |
| 134 | + |
| 135 | + resource = k8s.get_resource(ref) |
| 136 | + resource_id = cr["status"]["dhcpOptionsID"] |
| 137 | + |
| 138 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 139 | + |
| 140 | + # Check dhcpOptions exists in AWS |
| 141 | + ec2_validator = EC2Validator(ec2_client) |
| 142 | + ec2_validator.assert_dhcp_options(resource_id) |
| 143 | + |
| 144 | + # Check system and user tags exist for dhcpOptions resource |
| 145 | + dhcp_options = ec2_validator.get_dhcp_options(resource_id) |
| 146 | + user_tags = { |
| 147 | + "initialtagkey": "initialtagvalue" |
| 148 | + } |
| 149 | + tags.assert_ack_system_tags( |
| 150 | + tags=dhcp_options["Tags"], |
| 151 | + ) |
| 152 | + tags.assert_equal_without_ack_tags( |
| 153 | + expected=user_tags, |
| 154 | + actual=dhcp_options["Tags"], |
| 155 | + ) |
| 156 | + |
| 157 | + # Only user tags should be present in Spec |
| 158 | + assert len(resource["spec"]["tags"]) == 1 |
| 159 | + assert resource["spec"]["tags"][0]["key"] == "initialtagkey" |
| 160 | + assert resource["spec"]["tags"][0]["value"] == "initialtagvalue" |
| 161 | + |
| 162 | + # Update tags |
| 163 | + update_tags = [ |
| 164 | + { |
| 165 | + "key": "updatedtagkey", |
| 166 | + "value": "updatedtagvalue", |
| 167 | + } |
| 168 | + ] |
| 169 | + |
| 170 | + # Patch the dhcpOptions, updating the tags with new pair |
| 171 | + updates = { |
| 172 | + "spec": {"tags": update_tags}, |
| 173 | + } |
| 174 | + |
| 175 | + k8s.patch_custom_resource(ref, updates) |
| 176 | + time.sleep(MODIFY_WAIT_AFTER_SECONDS) |
| 177 | + |
| 178 | + # Check resource synced successfully |
| 179 | + assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5) |
| 180 | + |
| 181 | + # Check for updated user tags; system tags should persist |
| 182 | + dhcp_options = ec2_validator.get_dhcp_options(resource_id) |
| 183 | + updated_tags = { |
| 184 | + "updatedtagkey": "updatedtagvalue" |
| 185 | + } |
| 186 | + tags.assert_ack_system_tags( |
| 187 | + tags=dhcp_options["Tags"], |
| 188 | + ) |
| 189 | + tags.assert_equal_without_ack_tags( |
| 190 | + expected=updated_tags, |
| 191 | + actual=dhcp_options["Tags"], |
| 192 | + ) |
| 193 | + |
| 194 | + # Only user tags should be present in Spec |
| 195 | + resource = k8s.get_resource(ref) |
| 196 | + assert len(resource["spec"]["tags"]) == 1 |
| 197 | + assert resource["spec"]["tags"][0]["key"] == "updatedtagkey" |
| 198 | + assert resource["spec"]["tags"][0]["value"] == "updatedtagvalue" |
| 199 | + |
| 200 | + # Patch the dhcpOptions resource, deleting the tags |
| 201 | + updates = { |
| 202 | + "spec": {"tags": []}, |
| 203 | + } |
| 204 | + |
| 205 | + k8s.patch_custom_resource(ref, updates) |
| 206 | + time.sleep(MODIFY_WAIT_AFTER_SECONDS) |
| 207 | + |
| 208 | + # Check resource synced successfully |
| 209 | + assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5) |
| 210 | + |
| 211 | + # Check for removed user tags; system tags should persist |
| 212 | + dhcp_options = ec2_validator.get_dhcp_options(resource_id) |
| 213 | + tags.assert_ack_system_tags( |
| 214 | + tags=dhcp_options["Tags"], |
| 215 | + ) |
| 216 | + tags.assert_equal_without_ack_tags( |
| 217 | + expected=[], |
| 218 | + actual=dhcp_options["Tags"], |
| 219 | + ) |
| 220 | + |
| 221 | + # Check user tags are removed from Spec |
| 222 | + resource = k8s.get_resource(ref) |
| 223 | + assert len(resource["spec"]["tags"]) == 0 |
| 224 | + |
| 225 | + # Delete k8s resource |
| 226 | + _, deleted = k8s.delete_custom_resource(ref) |
| 227 | + assert deleted is True |
| 228 | + |
| 229 | + time.sleep(DELETE_WAIT_AFTER_SECONDS) |
| 230 | + |
| 231 | + # Check dhcpOptions no longer exists in AWS |
| 232 | + ec2_validator.assert_dhcp_options(resource_id, exists=False) |
| 233 | + |
| 234 | + @pytest.mark.resource_data({'create_vpc': True, 'resource_file': 'dhcp_options_vpc_ref'}) |
| 235 | + def test_dhcpoptions_creation_with_vpcref(self,ec2_client, simple_dhcp_options): |
| 236 | + (ref, cr) = simple_dhcp_options |
| 237 | + |
| 238 | + resource_id = cr["status"]["dhcpOptionsID"] |
| 239 | + vpc_id = cr["spec"]["vpc"][0] |
| 240 | + |
| 241 | + # Check DHCP Options exists in AWS |
| 242 | + ec2_validator = EC2Validator(ec2_client) |
| 243 | + ec2_validator.assert_dhcp_options(resource_id) |
| 244 | + |
| 245 | + # Check if DHCP Options gets associated to VPC |
| 246 | + ec2_validator.assert_dhcp_vpc_association(resource_id,vpc_id) |
| 247 | + |
| 248 | + # Delete k8s resource |
| 249 | + _, deleted = k8s.delete_custom_resource(ref) |
| 250 | + assert deleted is True |
| 251 | + |
| 252 | + time.sleep(DELETE_WAIT_AFTER_SECONDS) |
| 253 | + |
| 254 | + # Check DHCP Options no longer exists in AWS |
| 255 | + ec2_validator.assert_dhcp_options(resource_id, exists=False) |
| 256 | + |
0 commit comments