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
2 changes: 2 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ python-dateutil~=2.6, <2.8.1
requests==2.22.0
serverlessrepo==0.1.9
aws_lambda_builders==0.5.0
# https://github.com/mhammond/pywin32/issues/1439
pywin32 < 226; sys_platform == 'win32'
13 changes: 2 additions & 11 deletions samcli/commands/local/lib/sam_function_provider.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""
Class that provides functions from a given SAM template
"""

import ast
import logging

from samcli.commands.local.cli_common.user_exceptions import InvalidLayerVersionArn, InvalidSamTemplateException
from samcli.commands.local.cli_common.user_exceptions import InvalidLayerVersionArn
from .exceptions import InvalidLayerReference
from .provider import FunctionProvider, Function, LayerVersion
from .sam_base_provider import SamBaseProvider
Expand Down Expand Up @@ -123,18 +121,11 @@ 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=timeout,
timeout=resource_properties.get("Timeout"),
handler=resource_properties.get("Handler"),
codeuri=codeuri,
environment=resource_properties.get("Environment"),
Expand Down
14 changes: 14 additions & 0 deletions samcli/local/lambdafn/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Lambda Function configuration data required by the runtime
"""
import ast

from samcli.commands.local.cli_common.user_exceptions import InvalidSamTemplateException
from .env_vars import EnvironmentVariables


Expand Down Expand Up @@ -44,8 +46,20 @@ def __init__(self, name, runtime, handler, code_abs_path, layers, memory=None, t
self.code_abs_path = code_abs_path
self.layers = layers
self.memory = memory or self._DEFAULT_MEMORY

self.timeout = timeout or self._DEFAULT_TIMEOUT_SECONDS

if not isinstance(self.timeout, int):
try:
self.timeout = ast.literal_eval(self.timeout)

# Guard against float values like "3.2"
if not isinstance(self.timeout, int):
raise InvalidSamTemplateException("Invalid Number for Timeout: {}".format(self.timeout))

except (ValueError, SyntaxError, TypeError):
raise InvalidSamTemplateException("Invalid Number for Timeout: {}".format(self.timeout))

if not env_vars:
env_vars = EnvironmentVariables(self.memory, self.timeout, self.handler)

Expand Down
19 changes: 1 addition & 18 deletions tests/unit/commands/local/lib/test_sam_function_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def test_must_convert(self):
name="myname",
runtime="myruntime",
memory="mymemorysize",
timeout=30,
timeout="30",
handler="myhandler",
codeuri="/usr/local",
environment="myenvironment",
Expand All @@ -267,23 +267,6 @@ 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
62 changes: 62 additions & 0 deletions tests/unit/local/lambdafn/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from unittest import TestCase
from unittest.mock import Mock

from parameterized import parameterized

from samcli.local.lambdafn.config import FunctionConfig
from samcli.commands.local.cli_common.user_exceptions import InvalidSamTemplateException


class TestFunctionConfig(TestCase):
Expand Down Expand Up @@ -59,3 +62,62 @@ def test_init_without_optional_values(self):
self.assertEqual(config.env_vars.handler, self.handler)
self.assertEqual(config.env_vars.memory, self.DEFAULT_MEMORY)
self.assertEqual(config.env_vars.timeout, self.DEFAULT_TIMEOUT)

def test_init_with_timeout_of_int_string(self):
config = FunctionConfig(
self.name,
self.runtime,
self.handler,
self.code_path,
self.layers,
memory=self.memory,
timeout="34",
env_vars=self.env_vars_mock,
)

self.assertEqual(config.name, self.name)
self.assertEqual(config.runtime, self.runtime)
self.assertEqual(config.handler, self.handler)
self.assertEqual(config.code_abs_path, self.code_path)
self.assertEqual(config.layers, self.layers)
self.assertEqual(config.memory, self.memory)
self.assertEqual(config.timeout, 34)
self.assertEqual(config.env_vars, self.env_vars_mock)

self.assertEqual(self.env_vars_mock.handler, self.handler)
self.assertEqual(self.env_vars_mock.memory, self.memory)
self.assertEqual(self.env_vars_mock.timeout, 34)


class TestFunctionConfigInvalidTimeouts(TestCase):
def setUp(self):
self.name = "name"
self.runtime = "runtime"
self.handler = "handler"
self.code_path = "codepath"
self.memory = 1234
self.env_vars_mock = Mock()
self.layers = ["layer1"]

@parameterized.expand(
[
("none int string",),
({"dictionary": "is not a string either"},),
("/local/lambda/timeout",),
("3.2",),
("4.2",),
("0.123",),
]
)
def test_init_with_invalid_timeout_values(self, timeout):
with self.assertRaises(InvalidSamTemplateException):
FunctionConfig(
self.name,
self.runtime,
self.handler,
self.code_path,
self.layers,
memory=self.memory,
timeout=timeout,
env_vars=self.env_vars_mock,
)