Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MemorySize validation for function build #6679

Closed
wants to merge 4 commits into from
Closed
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
10 changes: 10 additions & 0 deletions samcli/commands/local/cli_common/invoke_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Reads CLI arguments and performs necessary preparation to be able to run the function
"""

import errno
import json
import logging
Expand All @@ -14,6 +15,7 @@
from samcli.commands.local.cli_common.user_exceptions import DebugContextException, InvokeContextException
from samcli.commands.local.lib.debug_context import DebugContext
from samcli.commands.local.lib.local_lambda import LocalLambdaRunner
from samcli.commands.validate.validate import do_cli as run_sam_validate
from samcli.lib.providers.provider import Function, Stack
from samcli.lib.providers.sam_function_provider import RefreshableSamFunctionProvider, SamFunctionProvider
from samcli.lib.providers.sam_stack_provider import SamLocalStackProvider
Expand Down Expand Up @@ -99,6 +101,7 @@ def __init__(
container_host_interface: Optional[str] = None,
add_host: Optional[dict] = None,
invoke_images: Optional[str] = None,
ctx: Optional[dict] = None,
) -> None:
"""
Initialize the context
Expand Down Expand Up @@ -153,6 +156,8 @@ def __init__(
Optional. Docker extra hosts support from --add-host parameters
invoke_images dict
Optional. A dictionary that defines the custom invoke image URI of each function
ctx dict
Optional. Command invoke context used to invoke commands before invoking `sam local` logic
"""
self._template_file = template_file
self._function_identifier = function_identifier
Expand Down Expand Up @@ -209,6 +214,7 @@ def __init__(
self._lambda_runtimes: Optional[Dict[ContainersMode, LambdaRuntime]] = None

self._local_lambda_runner: Optional[LocalLambdaRunner] = None
self._ctx = ctx

def __enter__(self) -> "InvokeContext":
"""
Expand All @@ -219,6 +225,10 @@ def __enter__(self) -> "InvokeContext":

self._stacks = self._get_stacks()

if self._ctx:
LOG.info("Validating template %s\n", self._template_file)
run_sam_validate(ctx=self._ctx, template=self._template_file, lint=True)

_function_providers_class: Dict[ContainersMode, Type[SamFunctionProvider]] = {
ContainersMode.WARM: RefreshableSamFunctionProvider,
ContainersMode.COLD: SamFunctionProvider,
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def do_cli( # pylint: disable=R0914
container_host_interface=container_host_interface,
add_host=add_host,
invoke_images=processed_invoke_images,
ctx=ctx,
) as context:
# Invoke the function
context.local_lambda_runner.invoke(
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def do_cli( # pylint: disable=R0914
container_host_interface=container_host_interface,
invoke_images=processed_invoke_images,
add_host=add_host,
ctx=ctx,
) as invoke_context:
ssl_context = (ssl_cert_file, ssl_key_file) if ssl_cert_file else None
service = LocalApiService(
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/start_lambda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def do_cli( # pylint: disable=R0914
container_host_interface=container_host_interface,
add_host=add_host,
invoke_images=processed_invoke_images,
ctx=ctx,
) as invoke_context:
service = LocalLambdaService(lambda_invoke_context=invoke_context, port=port, host=host)
service.start()
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/local/invoke/test_integrations_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,3 +1253,17 @@ def test_function_exception(self):
self.assertEqual(result.process.returncode, 0)
for line in stack_trace_lines:
self.assertIn(line, stderr)


class TestRunValidateBeforeInvoke(InvokeIntegBase):
template = Path("template-invalid-memorysize.yaml")

def test_validation_exception(self):
command_list = InvokeIntegBase.get_command_list(
function_to_invoke="HelloWorldFunction",
template_path=self.template_path,
parameter_overrides={"MemorySize": "1"},
)

result = run_command(command_list)
self.assertIn("E3034 Value has to be between 128 and 10240", result.stdout.decode("utf-8"))
7 changes: 7 additions & 0 deletions tests/integration/local/start_api/test_start_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
from ..invoke.layer_utils import LayerUtils


# class TestInvalidTempate(StartApiIntegBaseClass):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove the lines if they are not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, didn't mean to commit this

# template_path = "/testdata/start_api/invalid-template.yaml"

# def test(self):
# self.assertIsNotNone(self.start_api_process.poll())


@parameterized_class(
("template_path",),
[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.9
Handler: handler
CodeUri: .
Timeout: 600
MemorySize: 1
2 changes: 2 additions & 0 deletions tests/unit/commands/local/invoke/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def test_cli_must_setup_context_and_invoke(self, get_event_mock, InvokeContextMo
container_host_interface=self.container_host_interface,
add_host=self.add_host,
invoke_images={None: "amazon/aws-sam-cli-emulation-image-python3.9"},
ctx=self.ctx_mock,
)

context_mock.local_lambda_runner.invoke.assert_called_with(
Expand Down Expand Up @@ -152,6 +153,7 @@ def test_cli_must_invoke_with_no_event(self, get_event_mock, InvokeContextMock):
container_host_interface=self.container_host_interface,
add_host=self.add_host,
invoke_images={None: "amazon/aws-sam-cli-emulation-image-python3.9"},
ctx=self.ctx_mock,
)

get_event_mock.assert_not_called()
Expand Down
1 change: 1 addition & 0 deletions tests/unit/commands/local/start_api/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def test_cli_must_setup_context_and_start_service(self, local_api_service_mock,
container_host_interface=self.container_host_interface,
add_host=self.add_host,
invoke_images={},
ctx=self.ctx_mock,
)

local_api_service_mock.assert_called_with(
Expand Down
1 change: 1 addition & 0 deletions tests/unit/commands/local/start_lambda/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def test_cli_must_setup_context_and_start_service(self, local_lambda_service_moc
container_host_interface=self.container_host_interface,
add_host=self.add_host,
invoke_images={},
ctx=self.ctx_mock,
)

local_lambda_service_mock.assert_called_with(lambda_invoke_context=context_mock, port=self.port, host=self.host)
Expand Down
Loading