Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions samcli/commands/local/lib/sam_function_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ValueError:
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"),
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/local/invoke/test_integrations_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +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")])
@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
Expand Down
13 changes: 12 additions & 1 deletion tests/integration/testdata/invoke/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Parameters:
MyRuntimeVersion:
Type: String

StringValueTimeout:
Default: "5"
Type: Number

Resources:
HelloWorldServerlessFunction:
Type: AWS::Serverless::Function
Expand Down Expand Up @@ -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
27 changes: 22 additions & 5 deletions tests/unit/commands/local/lib/test_sam_function_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -326,7 +343,7 @@ def test_must_convert(self):
"Code": {"Bucket": "bucket"},
"Runtime": "myruntime",
"MemorySize": "mymemorysize",
"Timeout": "mytimeout",
"Timeout": "30",
"Handler": "myhandler",
"Environment": "myenvironment",
"Role": "myrole",
Expand All @@ -337,7 +354,7 @@ def test_must_convert(self):
name="myname",
runtime="myruntime",
memory="mymemorysize",
timeout="mytimeout",
timeout="30",
handler="myhandler",
codeuri=".",
environment="myenvironment",
Expand Down