|
| 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 Lambda version API. |
| 15 | +""" |
| 16 | + |
| 17 | +import pytest |
| 18 | +import time |
| 19 | +import logging |
| 20 | + |
| 21 | +from acktest.resources import random_suffix_name |
| 22 | +from acktest.aws.identity import get_region |
| 23 | +from acktest.k8s import resource as k8s |
| 24 | + |
| 25 | +from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_lambda_resource |
| 26 | +from e2e.replacement_values import REPLACEMENT_VALUES |
| 27 | +from e2e.bootstrap_resources import get_bootstrap_resources |
| 28 | +from e2e.service_bootstrap import LAMBDA_FUNCTION_FILE_ZIP |
| 29 | +from e2e.tests.helper import LambdaValidator |
| 30 | + |
| 31 | +RESOURCE_PLURAL = "versions" |
| 32 | + |
| 33 | +CREATE_WAIT_AFTER_SECONDS = 10 |
| 34 | +UPDATE_WAIT_AFTER_SECONDS = 10 |
| 35 | +DELETE_WAIT_AFTER_SECONDS = 10 |
| 36 | + |
| 37 | +@pytest.fixture(scope="module") |
| 38 | +def lambda_function(): |
| 39 | + resource_name = random_suffix_name("lambda-function", 24) |
| 40 | + resources = get_bootstrap_resources() |
| 41 | + |
| 42 | + replacements = REPLACEMENT_VALUES.copy() |
| 43 | + replacements["FUNCTION_NAME"] = resource_name |
| 44 | + replacements["BUCKET_NAME"] = resources.FunctionsBucket.name |
| 45 | + replacements["LAMBDA_ROLE"] = resources.EICRole.arn |
| 46 | + replacements["LAMBDA_FILE_NAME"] = LAMBDA_FUNCTION_FILE_ZIP |
| 47 | + replacements["RESERVED_CONCURRENT_EXECUTIONS"] = "3" |
| 48 | + replacements["CODE_SIGNING_CONFIG_ARN"] = "" |
| 49 | + replacements["AWS_REGION"] = get_region() |
| 50 | + |
| 51 | + # Load function CR |
| 52 | + resource_data = load_lambda_resource( |
| 53 | + "function", |
| 54 | + additional_replacements=replacements, |
| 55 | + ) |
| 56 | + logging.debug(resource_data) |
| 57 | + |
| 58 | + # Create k8s resource |
| 59 | + function_reference = k8s.CustomResourceReference( |
| 60 | + CRD_GROUP, CRD_VERSION, "functions", |
| 61 | + resource_name, namespace="default", |
| 62 | + ) |
| 63 | + |
| 64 | + # Create lambda function |
| 65 | + k8s.create_custom_resource(function_reference, resource_data) |
| 66 | + function_resource = k8s.wait_resource_consumed_by_controller(function_reference) |
| 67 | + |
| 68 | + assert function_resource is not None |
| 69 | + assert k8s.get_resource_exists(function_reference) |
| 70 | + |
| 71 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 72 | + |
| 73 | + yield (function_reference, function_resource) |
| 74 | + |
| 75 | + _, deleted = k8s.delete_custom_resource(function_reference) |
| 76 | + assert deleted |
| 77 | + |
| 78 | +@service_marker |
| 79 | +@pytest.mark.canary |
| 80 | +class TestVersion: |
| 81 | + def test_smoke(self, lambda_client, lambda_function): |
| 82 | + (_, function_resource) = lambda_function |
| 83 | + lambda_function_name = function_resource["spec"]["name"] |
| 84 | + |
| 85 | + resource_name = random_suffix_name("lambda-version", 24) |
| 86 | + |
| 87 | + replacements = REPLACEMENT_VALUES.copy() |
| 88 | + replacements["AWS_REGION"] = get_region() |
| 89 | + replacements["FUNCTION_NAME"] = lambda_function_name |
| 90 | + replacements["VERSION_NAME"] = resource_name |
| 91 | + |
| 92 | + # Load Lambda CR |
| 93 | + resource_data = load_lambda_resource( |
| 94 | + "version", |
| 95 | + additional_replacements=replacements, |
| 96 | + ) |
| 97 | + logging.debug(resource_data) |
| 98 | + |
| 99 | + # Create k8s resource |
| 100 | + ref = k8s.CustomResourceReference( |
| 101 | + CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL, |
| 102 | + resource_name, namespace="default", |
| 103 | + ) |
| 104 | + k8s.create_custom_resource(ref, resource_data) |
| 105 | + cr = k8s.wait_resource_consumed_by_controller(ref) |
| 106 | + |
| 107 | + assert cr is not None |
| 108 | + assert k8s.get_resource_exists(ref) |
| 109 | + |
| 110 | + time.sleep(CREATE_WAIT_AFTER_SECONDS) |
| 111 | + |
| 112 | + cr = k8s.wait_resource_consumed_by_controller(ref) |
| 113 | + |
| 114 | + lambda_validator = LambdaValidator(lambda_client) |
| 115 | + |
| 116 | + version_number = cr['status']['version'] |
| 117 | + |
| 118 | + # Check version exists |
| 119 | + assert lambda_validator.version_exists(lambda_function_name, version_number) |
| 120 | + |
| 121 | + # Delete k8s resource |
| 122 | + _, deleted = k8s.delete_custom_resource(ref) |
| 123 | + assert deleted is True |
| 124 | + |
| 125 | + time.sleep(DELETE_WAIT_AFTER_SECONDS) |
| 126 | + |
| 127 | + # Check function version doesn't exist |
| 128 | + assert not lambda_validator.version_exists(lambda_function_name, version_number) |
0 commit comments