From ddf0623d2606d8052636fb9368a64409e7b7f9b7 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> Date: Mon, 30 May 2022 12:37:43 -0700 Subject: [PATCH 1/4] feat: remove the created intermediate containers while building the Image type lambda functions --- samcli/lib/build/app_builder.py | 2 ++ tests/unit/lib/build_module/test_app_builder.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/samcli/lib/build/app_builder.py b/samcli/lib/build/app_builder.py index e91fdf9caf..028cf8953c 100644 --- a/samcli/lib/build/app_builder.py +++ b/samcli/lib/build/app_builder.py @@ -413,6 +413,8 @@ def _build_lambda_image(self, function_name: str, metadata: Dict, architecture: "buildargs": docker_build_args, "decode": True, "platform": get_docker_platform(architecture), + "rm": True, + "forcerm": True, } if docker_build_target: build_args["target"] = cast(str, docker_build_target) diff --git a/tests/unit/lib/build_module/test_app_builder.py b/tests/unit/lib/build_module/test_app_builder.py index 1ba6c97661..bb127b213c 100644 --- a/tests/unit/lib/build_module/test_app_builder.py +++ b/tests/unit/lib/build_module/test_app_builder.py @@ -1143,6 +1143,8 @@ def test_can_build_image_function_under_debug(self, mock_os): buildargs={"a": "b", "SAM_BUILD_MODE": "debug"}, decode=True, platform="linux/amd64", + rm=True, + forcerm=True, ), ) @@ -1171,6 +1173,8 @@ def test_can_build_image_function_under_debug_with_target(self, mock_os): decode=True, target="stage", platform="linux/amd64", + rm=True, + forcerm=True, ), ) From cb3dd391ca0da2740438d874838e01dd130c54b5 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> Date: Mon, 30 May 2022 13:17:09 -0700 Subject: [PATCH 2/4] adding integration test case --- .../integration/buildcmd/build_integ_base.py | 7 ++++ tests/integration/buildcmd/test_build_cmd.py | 40 ++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/tests/integration/buildcmd/build_integ_base.py b/tests/integration/buildcmd/build_integ_base.py index e4c5740821..4aecb7233c 100644 --- a/tests/integration/buildcmd/build_integ_base.py +++ b/tests/integration/buildcmd/build_integ_base.py @@ -136,6 +136,13 @@ def verify_docker_container_cleanedup(self, runtime): ) self.assertFalse(bool(samcli_containers), "Build containers have not been removed") + def get_number_of_created_containers(self): + if IS_WINDOWS: + time.sleep(1) + docker_client = docker.from_env() + containers = docker_client.containers.list(all=True) + return len(containers) + def verify_pulled_image(self, runtime, architecture=X86_64): docker_client = docker.from_env() image_name = f"{LambdaBuildContainer._IMAGE_URI_PREFIX}-{runtime}" diff --git a/tests/integration/buildcmd/test_build_cmd.py b/tests/integration/buildcmd/test_build_cmd.py index 2b858f2354..5ca7ca4dba 100644 --- a/tests/integration/buildcmd/test_build_cmd.py +++ b/tests/integration/buildcmd/test_build_cmd.py @@ -46,11 +46,7 @@ SKIP_SAR_TESTS = RUNNING_ON_CI and RUNNING_TEST_FOR_MASTER_ON_CI and not RUN_BY_CANARY -@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_Images(BuildIntegBase): template = "template_image.yaml" @@ -87,6 +83,40 @@ def test_with_default_requirements(self, runtime, use_container): ) + @pytest.mark.flaky(reruns=3) + def test_intermediate_container_deleted(self): + _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}", + ) + + expected = {"pi": "3.14"} + 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), From 5d0c685c1fcc5631ff1eff9544fa2add919de421 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> Date: Mon, 30 May 2022 13:54:09 -0700 Subject: [PATCH 3/4] apply black --- tests/integration/buildcmd/test_build_cmd.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/integration/buildcmd/test_build_cmd.py b/tests/integration/buildcmd/test_build_cmd.py index 5ca7ca4dba..58cbb8b86e 100644 --- a/tests/integration/buildcmd/test_build_cmd.py +++ b/tests/integration/buildcmd/test_build_cmd.py @@ -46,7 +46,6 @@ SKIP_SAR_TESTS = RUNNING_ON_CI and RUNNING_TEST_FOR_MASTER_ON_CI and not RUN_BY_CANARY - class TestBuildCommand_PythonFunctions_Images(BuildIntegBase): template = "template_image.yaml" @@ -82,7 +81,6 @@ def test_with_default_requirements(self, runtime, use_container): self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected ) - @pytest.mark.flaky(reruns=3) def test_intermediate_container_deleted(self): _tag = f"{random.randint(1, 100)}" @@ -113,8 +111,9 @@ def test_intermediate_container_deleted(self): 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") + self.assertEqual( + _num_of_containers_before_build, _num_of_containers_after_build, "Intermediate containers are not removed" + ) @skipIf( From fcfb92fdd9d95bf58dc9a5811e04d9fa9496da37 Mon Sep 17 00:00:00 2001 From: Mohamed Elasmar <71043312+moelasmar@users.noreply.github.com> Date: Mon, 6 Jun 2022 14:02:05 -0700 Subject: [PATCH 4/4] apply PR comments --- samcli/lib/build/app_builder.py | 1 - tests/integration/buildcmd/test_build_cmd.py | 5 +++++ tests/unit/lib/build_module/test_app_builder.py | 2 -- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/samcli/lib/build/app_builder.py b/samcli/lib/build/app_builder.py index 028cf8953c..bbd756350c 100644 --- a/samcli/lib/build/app_builder.py +++ b/samcli/lib/build/app_builder.py @@ -414,7 +414,6 @@ def _build_lambda_image(self, function_name: str, metadata: Dict, architecture: "decode": True, "platform": get_docker_platform(architecture), "rm": True, - "forcerm": True, } if docker_build_target: build_args["target"] = cast(str, docker_build_target) diff --git a/tests/integration/buildcmd/test_build_cmd.py b/tests/integration/buildcmd/test_build_cmd.py index c40d68e327..d21cb9c600 100644 --- a/tests/integration/buildcmd/test_build_cmd.py +++ b/tests/integration/buildcmd/test_build_cmd.py @@ -46,6 +46,11 @@ SKIP_SAR_TESTS = RUNNING_ON_CI and RUNNING_TEST_FOR_MASTER_ON_CI and not RUN_BY_CANARY +@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_Images(BuildIntegBase): template = "template_image.yaml" diff --git a/tests/unit/lib/build_module/test_app_builder.py b/tests/unit/lib/build_module/test_app_builder.py index dad5ab7dc1..42d1590051 100644 --- a/tests/unit/lib/build_module/test_app_builder.py +++ b/tests/unit/lib/build_module/test_app_builder.py @@ -1146,7 +1146,6 @@ def test_can_build_image_function_under_debug(self, mock_os): decode=True, platform="linux/amd64", rm=True, - forcerm=True, ), ) @@ -1176,7 +1175,6 @@ def test_can_build_image_function_under_debug_with_target(self, mock_os): target="stage", platform="linux/amd64", rm=True, - forcerm=True, ), )