diff --git a/tests/integration/local/start_lambda/start_lambda_api_integ_base.py b/tests/integration/local/start_lambda/start_lambda_api_integ_base.py index 2cfcdb570aa..61a93b40ba4 100644 --- a/tests/integration/local/start_lambda/start_lambda_api_integ_base.py +++ b/tests/integration/local/start_lambda/start_lambda_api_integ_base.py @@ -8,6 +8,9 @@ import logging from pathlib import Path +import boto3 +from botocore import UNSIGNED +from botocore.config import Config from docker.errors import APIError from psutil import NoSuchProcess @@ -225,6 +228,17 @@ def tearDownClass(cls): except Exception as ex: LOG.error("Failed to clean up SAM CLI containers", exc_info=ex) + def get_local_lambda_client(self): + url = "http://127.0.0.1:{}".format(self.port) + return boto3.client( + "lambda", + endpoint_url=url, + region_name="us-east-1", + use_ssl=False, + verify=False, + config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), + ) + class WatchWarmContainersIntegBaseClass(StartLambdaIntegBaseClass): temp_path: Optional[str] = None diff --git a/tests/integration/local/start_lambda/test_start_lambda.py b/tests/integration/local/start_lambda/test_start_lambda.py index 1cfd59f4e27..c853eb3c219 100644 --- a/tests/integration/local/start_lambda/test_start_lambda.py +++ b/tests/integration/local/start_lambda/test_start_lambda.py @@ -11,11 +11,7 @@ import pytest import random -import boto3 -from botocore import UNSIGNED -from botocore.config import Config from botocore.exceptions import ClientError -from docker.errors import APIError from samcli.commands.local.cli_common.invoke_context import ContainersInitializationMode from tests.testing_utils import IS_WINDOWS, get_sam_command, kill_process @@ -27,15 +23,7 @@ class TestParallelRequests(StartLambdaIntegBaseClass): template_path = "/testdata/invoke/template.yml" def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") @@ -66,15 +54,7 @@ class TestLambdaServiceErrorCases(StartLambdaIntegBaseClass): template_path = "/testdata/invoke/template.yml" def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") @@ -120,15 +100,7 @@ class TestLambdaServiceWithInlineCode(StartLambdaIntegBaseClass): template_path = "/testdata/invoke/template-inlinecode.yaml" def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") @@ -167,15 +139,7 @@ class TestLambdaService(StartLambdaIntegBaseClass): parent_path = "" def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @parameterized.expand([("False"), ("True")]) @pytest.mark.flaky(reruns=3) @@ -331,15 +295,7 @@ def test_invoke_with_function_timeout_using_lookup_value(self, use_full_path): class TestWarmContainersBaseClass(StartLambdaIntegBaseClass): def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() def count_running_containers(self): """Count containers created by this test using Docker client directly.""" @@ -568,15 +524,7 @@ class TestImagePackageType(StartLambdaIntegBaseClass): parameter_overrides = {"ImageUri": f"helloworldfunction:{tag}"} def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -598,15 +546,7 @@ class TestImagePackageTypeWithEagerWarmContainersMode(StartLambdaIntegBaseClass) parameter_overrides = {"ImageUri": f"helloworldfunction:{tag}"} def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -628,15 +568,7 @@ class TestImagePackageTypeWithEagerLazyContainersMode(StartLambdaIntegBaseClass) parameter_overrides = {"ImageUri": f"helloworldfunction:{tag}"} def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -681,15 +613,7 @@ def handler(event, context): container_mode = ContainersInitializationMode.EAGER.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -774,15 +698,7 @@ def handler2(event, context): container_mode = ContainersInitializationMode.EAGER.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -877,15 +793,7 @@ def handler2(event, context): container_mode = ContainersInitializationMode.EAGER.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -969,15 +877,7 @@ def handler(event, context): container_mode = ContainersInitializationMode.EAGER.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1047,15 +947,7 @@ def handler(event, context): parameter_overrides = {"ImageUri": f"helloworldfunction:{tag}"} def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1153,15 +1045,7 @@ def handler(event, context): parameter_overrides = {"ImageUri": f"helloworldfunction:{tag}"} def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1219,15 +1103,7 @@ def handler(event, context): container_mode = ContainersInitializationMode.LAZY.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1296,15 +1172,7 @@ def handler(event, context): parameter_overrides = {"ImageUri": f"helloworldfunction:{tag}"} def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1390,15 +1258,7 @@ def handler2(event, context): container_mode = ContainersInitializationMode.LAZY.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1493,15 +1353,7 @@ def handler2(event, context): container_mode = ContainersInitializationMode.LAZY.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1585,15 +1437,7 @@ def handler(event, context): container_mode = ContainersInitializationMode.LAZY.value def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1691,15 +1535,7 @@ def handler(event, context): parameter_overrides = {"ImageUri": f"helloworldfunction:{tag}"} def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") @@ -1739,15 +1575,7 @@ class TestLambdaServiceWithCustomInvokeImages(StartLambdaIntegBaseClass): ] def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") @@ -1766,15 +1594,7 @@ class TestFunctionNameFilteringWithFilter(StartLambdaIntegBaseClass): function_logical_ids = ["EchoEventFunction", "HelloWorldServerlessFunction"] def setUp(self): - self.url = f"http://127.0.0.1:{self.port}" - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") @@ -1820,8 +1640,12 @@ def test_invoke_filtered_function_with_eager_containers(self): self.assertEqual(json.loads(response.get("body")), {"hello": "world"}) -class TestFunctionNameFilteringWarmContainersLazy(TestWarmContainersBaseClass): - """Test function filtering with LAZY warm containers""" +class TestFunctionNameFilteringWarmContainersLazyNoContainers(TestWarmContainersBaseClass): + """ + Test function filtering with LAZY warm containers. + This class and the next one have a similar set up, but they need to be separate + otherwise the containers count would be mixed up. + """ template_path = "/testdata/start_api/template-warm-containers.yaml" container_mode = ContainersInitializationMode.LAZY.value @@ -1835,6 +1659,16 @@ def test_no_containers_before_invoke_with_lazy(self): """Test that no containers are initialized before invocation in LAZY mode""" self.assertEqual(self.count_running_containers(), 0) + +class TestFunctionNameFilteringWarmContainersLazy(TestWarmContainersBaseClass): + """Test function filtering with LAZY warm containers""" + + template_path = "/testdata/start_api/template-warm-containers.yaml" + container_mode = ContainersInitializationMode.LAZY.value + mode_env_variable = str(uuid.uuid4()) + parameter_overrides = {"ModeEnvVariable": mode_env_variable} + function_logical_ids = ["HelloWorldFunction"] + @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=600, method="thread") def test_container_created_on_demand_for_filtered_function(self): @@ -1884,15 +1718,7 @@ class TestCapacityProviderFunction(StartLambdaIntegBaseClass): template_path = "/testdata/invoke/template-capacity-provider.yml" def setUp(self): - self.url = "http://127.0.0.1:{}".format(self.port) - self.lambda_client = boto3.client( - "lambda", - endpoint_url=self.url, - region_name="us-east-1", - use_ssl=False, - verify=False, - config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}), - ) + self.lambda_client = self.get_local_lambda_client() @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread")