Skip to content

Commit

Permalink
Add tests to ensure Dockerfile can be a path to a docker file via s…
Browse files Browse the repository at this point in the history
…ubdirectories. This means images can share code more easily.
  • Loading branch information
i18n-tribe committed Sep 15, 2022
1 parent a142c25 commit ec8d936
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/integration/buildcmd/test_build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,90 @@ def test_intermediate_container_deleted(self):
)


@skipIf(
# Hits public ECR pull limitation, move it to canary tests
((not RUN_BY_CANARY) or (IS_WINDOWS and RUNNING_ON_CI) and not CI_OVERRIDE),
"Skip build tests on windows when running in CI unless overridden",
)
class TestBuildCommand_PythonFunctions_ImagesWithSharedCode(BuildIntegBase):
template = "template_images_with_shared_code.yaml"

EXPECTED_FILES_PROJECT_MANIFEST: Set[str] = set()

FUNCTION_LOGICAL_ID_IMAGE = "ImageFunction"

@parameterized.expand(
[
*[(runtime, "feature_phi/Dockerfile", {"phi": "1.62"}) for runtime in ["3.6", "3.7", "3.8", "3.9"]],
*[(runtime, "feature_pi/Dockerfile", {"pi": "3.14"}) for runtime in ["3.6", "3.7", "3.8", "3.9"]],
]
)
@pytest.mark.flaky(reruns=3)
def test_with_default_requirements(self, runtime, dockerfile, expected):
_tag = f"{random.randint(1, 100)}"
overrides = {
"Runtime": runtime,
"Handler": "main.handler",
"DockerFile": dockerfile,
"Tag": _tag,
}
cmdlist = self.get_command_list(use_container=False, parameter_overrides=overrides)

LOG.info("Running Command: ")
LOG.info(cmdlist)
run_command(cmdlist, cwd=self.working_dir)

self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID_IMAGE,
"ImageUri",
f"{self.FUNCTION_LOGICAL_ID_IMAGE.lower()}:{_tag}",
)

self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected
)

@parameterized.expand(
[
("feature_phi/Dockerfile", {"phi": "1.62"}),
("feature_pi/Dockerfile", {"pi": "3.14"}),
]
)
@pytest.mark.flaky(reruns=3)
def test_intermediate_container_deleted(self, dockerfile, expected):
_tag = f"{random.randint(1, 100)}"
overrides = {
"Runtime": "3.9",
"Handler": "main.handler",
"DockerFile": dockerfile,
"Tag": _tag,
}
cmdlist = self.get_command_list(use_container=False, parameter_overrides=overrides)

LOG.info("Running Command: ")
LOG.info(cmdlist)

_num_of_containers_before_build = self.get_number_of_created_containers()
run_command(cmdlist, cwd=self.working_dir)
_num_of_containers_after_build = self.get_number_of_created_containers()

self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID_IMAGE,
"ImageUri",
f"{self.FUNCTION_LOGICAL_ID_IMAGE.lower()}:{_tag}",
)

self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected
)

self.assertEqual(
_num_of_containers_before_build, _num_of_containers_after_build, "Intermediate containers are not removed"
)


@skipIf(
# Hits public ECR pull limitation, move it to canary tests
((not RUN_BY_CANARY) or (IS_WINDOWS and RUNNING_ON_CI) and not CI_OVERRIDE),
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ARG BASE_RUNTIME

FROM public.ecr.aws/lambda/python:$BASE_RUNTIME

ARG FUNCTION_DIR="/var/task"

RUN mkdir -p $FUNCTION_DIR

COPY feature_phi/__init__.py $FUNCTION_DIR
COPY feature_phi/main.py $FUNCTION_DIR

COPY shared_code/ $FUNCTION_DIR/shared_code/

COPY requirements.txt $FUNCTION_DIR

RUN python -m pip install -r $FUNCTION_DIR/requirements.txt -t $FUNCTION_DIR

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from shared_code import numbers


def handler(event, context):
return {"phi": numbers.get_phi_2dp()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ARG BASE_RUNTIME

FROM public.ecr.aws/lambda/python:$BASE_RUNTIME

ARG FUNCTION_DIR="/var/task"

RUN mkdir -p $FUNCTION_DIR

COPY feature_pi/__init__.py $FUNCTION_DIR
COPY feature_pi/main.py $FUNCTION_DIR

COPY shared_code/ $FUNCTION_DIR/shared_code/

COPY requirements.txt $FUNCTION_DIR

RUN python -m pip install -r $FUNCTION_DIR/requirements.txt -t $FUNCTION_DIR

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from shared_code import numbers


def handler(event, context):
return {"pi": numbers.get_pi_2dp()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# These are some hard packages to build. Using them here helps us verify that building works on various platforms

# NOTE: Fixing to <1.20.3 as numpy1.20.3 started to use a new wheel naming convention (PEP 600)
numpy<1.20.3
# `cryptography` has a dependency on `pycparser` which, for some reason doesn't build inside a Docker container.
# Turning this off until we resolve this issue: https://github.com/awslabs/aws-lambda-builders/issues/29
# cryptography~=2.4
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy


def _format_2dp(number):
return "{0:.2f}".format(number)


def get_pi_2dp():
return _format_2dp(numpy.pi)


def get_phi_2dp():
# Golden ratio
return _format_2dp((1 + 5 ** 0.5) / 2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Parameteres:
Runtime:
Type: String
DockerFile:
Type: String
Handler:
Type: String
Tag:
Type: String

Resources:

ImageFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageConfig:
Command:
- !Ref Handler
Timeout: 600
Metadata:
DockerTag: !Ref Tag
DockerContext: ./PythonImagesWithSharedCode
Dockerfile: !Ref DockerFile
DockerBuildArgs:
BASE_RUNTIME: !Ref Runtime

0 comments on commit ec8d936

Please sign in to comment.