From 0c99a66c09b65084b996afc71ee29e9dce999e9f Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Fri, 31 May 2019 09:12:41 +0100 Subject: [PATCH 1/2] fix: Lambda timeout from parameter (#925) --- samcli/commands/local/lib/sam_function_provider.py | 12 ++++++++++-- .../local/invoke/test_integrations_cli.py | 6 +++++- tests/integration/testdata/invoke/template.yml | 13 ++++++++++++- .../local/lib/test_sam_function_provider.py | 8 ++++---- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/samcli/commands/local/lib/sam_function_provider.py b/samcli/commands/local/lib/sam_function_provider.py index 1e8365b2fa..59cd5a9665 100644 --- a/samcli/commands/local/lib/sam_function_provider.py +++ b/samcli/commands/local/lib/sam_function_provider.py @@ -2,9 +2,10 @@ Class that provides functions from a given SAM template """ +import ast import logging -from samcli.commands.local.cli_common.user_exceptions import InvalidLayerVersionArn +from samcli.commands.local.cli_common.user_exceptions import InvalidLayerVersionArn, InvalidSamTemplateException from .exceptions import InvalidLayerReference from .provider import FunctionProvider, Function, LayerVersion from .sam_base_provider import SamBaseProvider @@ -122,11 +123,18 @@ def _convert_sam_function_resource(name, resource_properties, layers): LOG.debug("Found Serverless function with name='%s' and CodeUri='%s'", name, codeuri) + timeout = resource_properties.get("Timeout") + if isinstance(timeout, str): + try: + timeout = ast.literal_eval(timeout) + except SyntaxError: + raise InvalidSamTemplateException('Invalid Number for Timeout: {}'.format(timeout)) + return Function( name=name, runtime=resource_properties.get("Runtime"), memory=resource_properties.get("MemorySize"), - timeout=resource_properties.get("Timeout"), + timeout=timeout, handler=resource_properties.get("Handler"), codeuri=codeuri, environment=resource_properties.get("Environment"), diff --git a/tests/integration/local/invoke/test_integrations_cli.py b/tests/integration/local/invoke/test_integrations_cli.py index d9999d5670..3af5b600fe 100644 --- a/tests/integration/local/invoke/test_integrations_cli.py +++ b/tests/integration/local/invoke/test_integrations_cli.py @@ -74,7 +74,11 @@ def test_invoke_of_lambda_function(self): @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") - @parameterized.expand([("TimeoutFunction"), ("TimeoutFunctionWithParameter")]) + @parameterized.expand([ + ("TimeoutFunction"), + ("TimeoutFunctionWithParameter"), + ("TimeoutFunctionWithStringParameter"), + ]) def test_invoke_with_timeout_set(self, function_name): command_list = self.get_command_list( function_name, template_path=self.template_path, event_path=self.event_path diff --git a/tests/integration/testdata/invoke/template.yml b/tests/integration/testdata/invoke/template.yml index fcb5493e74..f22754f67f 100644 --- a/tests/integration/testdata/invoke/template.yml +++ b/tests/integration/testdata/invoke/template.yml @@ -10,6 +10,10 @@ Parameters: MyRuntimeVersion: Type: String + StringValueTimeout: + Default: "5" + Type: Number + Resources: HelloWorldServerlessFunction: Type: AWS::Serverless::Function @@ -122,4 +126,11 @@ Resources: Timeout: !Ref DefaultTimeout MyRuntimeVersion: !Ref MyRuntimeVersion - + TimeoutFunctionWithStringParameter: + Type: AWS::Serverless::Function + Properties: + Handler: main.sleep_handler + Runtime: python3.6 + CodeUri: . + Timeout: + Ref: StringValueTimeout diff --git a/tests/unit/commands/local/lib/test_sam_function_provider.py b/tests/unit/commands/local/lib/test_sam_function_provider.py index 9b8e679159..65a031d8c6 100644 --- a/tests/unit/commands/local/lib/test_sam_function_provider.py +++ b/tests/unit/commands/local/lib/test_sam_function_provider.py @@ -244,7 +244,7 @@ def test_must_convert(self): "CodeUri": "/usr/local", "Runtime": "myruntime", "MemorySize": "mymemorysize", - "Timeout": "mytimeout", + "Timeout": "30", "Handler": "myhandler", "Environment": "myenvironment", "Role": "myrole", @@ -255,7 +255,7 @@ def test_must_convert(self): name="myname", runtime="myruntime", memory="mymemorysize", - timeout="mytimeout", + timeout=30, handler="myhandler", codeuri="/usr/local", environment="myenvironment", @@ -326,7 +326,7 @@ def test_must_convert(self): "Code": {"Bucket": "bucket"}, "Runtime": "myruntime", "MemorySize": "mymemorysize", - "Timeout": "mytimeout", + "Timeout": "30", "Handler": "myhandler", "Environment": "myenvironment", "Role": "myrole", @@ -337,7 +337,7 @@ def test_must_convert(self): name="myname", runtime="myruntime", memory="mymemorysize", - timeout="mytimeout", + timeout="30", handler="myhandler", codeuri=".", environment="myenvironment", From e5334daec194634b4f30bca8237f4a56e3ad548e Mon Sep 17 00:00:00 2001 From: Jacob Fuss Date: Mon, 28 Oct 2019 11:50:50 -0500 Subject: [PATCH 2/2] update with HEAD of develop and add a unit test --- .../local/lib/sam_function_provider.py | 4 ++-- .../local/invoke/test_integrations_cli.py | 8 +++----- .../local/lib/test_sam_function_provider.py | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/samcli/commands/local/lib/sam_function_provider.py b/samcli/commands/local/lib/sam_function_provider.py index 59cd5a9665..6bc09c9fb7 100644 --- a/samcli/commands/local/lib/sam_function_provider.py +++ b/samcli/commands/local/lib/sam_function_provider.py @@ -127,8 +127,8 @@ def _convert_sam_function_resource(name, resource_properties, layers): if isinstance(timeout, str): try: timeout = ast.literal_eval(timeout) - except SyntaxError: - raise InvalidSamTemplateException('Invalid Number for Timeout: {}'.format(timeout)) + except ValueError: + raise InvalidSamTemplateException("Invalid Number for Timeout: {}".format(timeout)) return Function( name=name, diff --git a/tests/integration/local/invoke/test_integrations_cli.py b/tests/integration/local/invoke/test_integrations_cli.py index 3af5b600fe..9814c71b39 100644 --- a/tests/integration/local/invoke/test_integrations_cli.py +++ b/tests/integration/local/invoke/test_integrations_cli.py @@ -74,11 +74,9 @@ def test_invoke_of_lambda_function(self): @pytest.mark.flaky(reruns=3) @pytest.mark.timeout(timeout=300, method="thread") - @parameterized.expand([ - ("TimeoutFunction"), - ("TimeoutFunctionWithParameter"), - ("TimeoutFunctionWithStringParameter"), - ]) + @parameterized.expand( + [("TimeoutFunction"), ("TimeoutFunctionWithParameter"), ("TimeoutFunctionWithStringParameter")] + ) def test_invoke_with_timeout_set(self, function_name): command_list = self.get_command_list( function_name, template_path=self.template_path, event_path=self.event_path diff --git a/tests/unit/commands/local/lib/test_sam_function_provider.py b/tests/unit/commands/local/lib/test_sam_function_provider.py index 65a031d8c6..97c057dba8 100644 --- a/tests/unit/commands/local/lib/test_sam_function_provider.py +++ b/tests/unit/commands/local/lib/test_sam_function_provider.py @@ -2,7 +2,7 @@ from unittest.mock import patch from parameterized import parameterized -from samcli.commands.local.cli_common.user_exceptions import InvalidLayerVersionArn +from samcli.commands.local.cli_common.user_exceptions import InvalidLayerVersionArn, InvalidSamTemplateException from samcli.commands.local.lib.provider import Function, LayerVersion from samcli.commands.local.lib.sam_function_provider import SamFunctionProvider from samcli.commands.local.lib.exceptions import InvalidLayerReference @@ -267,6 +267,23 @@ def test_must_convert(self): self.assertEqual(expected, result) + def test_must_fail_with_InvalidSamTemplateException(self): + + name = "myname" + properties = { + "CodeUri": "/usr/local", + "Runtime": "myruntime", + "MemorySize": "mymemorysize", + "Timeout": "timeout", + "Handler": "myhandler", + "Environment": "myenvironment", + "Role": "myrole", + "Layers": ["Layer1", "Layer2"], + } + + with self.assertRaises(InvalidSamTemplateException): + SamFunctionProvider._convert_sam_function_resource(name, properties, ["Layer1", "Layer2"]) + def test_must_skip_non_existent_properties(self): name = "myname"