From 2a1a2fed7e3cb9decf4a2ee5432490c70d779f81 Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Thu, 11 Apr 2024 17:59:09 +0000 Subject: [PATCH 1/5] refactor test cases --- Makefile | 29 +- .../local_lambda/test_end_to_end.py | 254 +++++++++--------- test/integration/testdata/Dockerfile-allinone | 3 +- 3 files changed, 159 insertions(+), 127 deletions(-) diff --git a/Makefile b/Makefile index 6d36ae2..8dc3d49 100644 --- a/Makefile +++ b/Makefile @@ -39,9 +39,32 @@ integ-tests-and-compile: tests integ-tests-with-docker: tests-with-docker make compile-with-docker-all make integ-tests - -integ-tests: + +prep-python: python3 -m venv .venv .venv/bin/pip install --upgrade pip .venv/bin/pip install requests parameterized - .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + +integ-tests: + make prep-python + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + TEST_ARCH=x86_64 TEST_PORT=8002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + TEST_ARCH=arm64 TEST_PORT=9002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + TEST_ARCH="" TEST_PORT=9052 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + +integ-tests-with-docker-x86-64: + make ARCH=x86_64 compile-with-docker + make prep-python + TEST_ARCH=x86_64 TEST_PORT=8002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + +integ-tests-with-docker-arm64: + make ARCH=arm64 compile-with-docker + make prep-python + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + TEST_ARCH="arm64" TEST_PORT=9002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + +integ-tests-with-docker-old: + make ARCH=old compile-with-docker + make prep-python + TEST_ARCH="" TEST_PORT=9052 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + \ No newline at end of file diff --git a/test/integration/local_lambda/test_end_to_end.py b/test/integration/local_lambda/test_end_to_end.py index fd7f735..4046a31 100644 --- a/test/integration/local_lambda/test_end_to_end.py +++ b/test/integration/local_lambda/test_end_to_end.py @@ -5,8 +5,9 @@ from unittest import TestCase, main from pathlib import Path import time - +import os import requests +from contextlib import contextmanager from parameterized import parameterized SLEEP_TIME = 2 @@ -14,7 +15,10 @@ ARCHS = ["x86_64", "arm64", ""] + class TestEndToEnd(TestCase): + ARCH = os.environ.get('TEST_ARCH', "") + PORT = os.environ.get('TEST_PORT', 8002) @classmethod def setUpClass(cls): testdata_path = Path(__file__).resolve().parents[1].joinpath("testdata") @@ -23,48 +27,30 @@ def setUpClass(cls): cls.path_to_binary = Path().resolve().joinpath("bin") # build image - for arch in ARCHS: - image_name = cls.image_name if arch == "" else f"{cls.image_name}-{arch}" - architecture = arch if arch == "arm64" else "amd64" - build_cmd = [ - "docker", - "build", - "--platform", - f"linux/{architecture}", - "-t", - image_name, - "-f", - str(dockerfile_path), - str(testdata_path), - ] - Popen(build_cmd).communicate() + image_name = cls.image_name if cls.ARCH == "" else f"{cls.image_name}-{cls.ARCH}" + architecture = cls.ARCH if cls.ARCH == "arm64" else "amd64" + docker_arch = cls.ARCH if cls.ARCH == "arm64" else "x86_64" + + build_cmd = [ + "docker", + "build", + "--platform", + f"linux/{architecture}", + "-t", + image_name, + "-f", + str(dockerfile_path), + str(testdata_path), + "--build-arg", + f"IMAGE_ARCH={docker_arch}", + ] + print (build_cmd) + Popen(build_cmd).communicate() @classmethod def tearDownClass(cls): - images_to_delete = [ - "envvarcheck", - "twoinvokes", - "arnexists", - "customname", - "timeout", - "exception", - "remaining_time_in_three_seconds", - "remaining_time_in_ten_seconds", - "remaining_time_in_default_deadline", - "pre-runtime-api", - "assert-overwritten", - "port_override" - ] - - for image in images_to_delete: - for arch in ARCHS: - arch_tag = "" if arch == "" else f"-{arch}" - cmd = f"docker rm -f {image}{arch_tag}" - Popen(cmd.split(" ")).communicate() - - for arch in ARCHS: - arch_tag = "" if arch == "" else f"-{arch}" - Popen(f"docker rmi {cls.image_name}{arch_tag}".split(" ")).communicate() + arch_tag = "" if cls.ARCH == "" else f"-{cls.ARCH}" + Popen(f"docker rmi {cls.image_name}{arch_tag}".split(" ")).communicate() def tagged_name(self, name, architecture): tag = self.get_tag(architecture) @@ -84,148 +70,170 @@ def invoke_function(self, port): f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={} ) - def create_container_and_invoke_function(self, cmd, port): - self.run_command(cmd) - - # sleep 1s to give enough time for the endpoint to be up to curl - self.sleep_1s() - - return self.invoke_function(port) - - @parameterized.expand([("x86_64", "8000"), ("arm64", "9000"), ("", "9050")]) - def test_env_var_with_equal_sign(self, arch, port): + @contextmanager + def create_container(self, cmd, image): + try: + platform = "x86_64" if self.ARCH == "" else self.ARCH + cmd_full = f"docker run --platform linux/{platform} {cmd}" + self.run_command(cmd_full) + + # sleep 1s to give enough time for the endpoint to be up to curl + self.sleep_1s() + yield + except Exception as e: + print(f"An error occurred while executing cmd: {cmd_full}. error: {e}") + raise e + finally: + self.run_command(f"docker stop {image}") + self.run_command(f"docker rm -f {image}") + + + def test_env_var_with_equal_sign(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("envvarcheck", arch) - - cmd = f"docker run --name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b'"4=4"', r.content) + self.assertEqual(b'"4=4"', r.content) + - @parameterized.expand([("x86_64", "8001"), ("arm64", "9001"), ("", "9051")]) - def test_two_invokes(self, arch, port): + def test_two_invokes(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("twoinvokes", arch) - cmd = f"docker run --name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b'"My lambda ran succesfully"', r.content) + self.assertEqual(b'"My lambda ran succesfully"', r.content) + + # Make sure we can invoke the function twice + r = self.invoke_function(port) + + self.assertEqual(b'"My lambda ran succesfully"', r.content) - # Make sure we can invoke the function twice - r = self.invoke_function(port) - - self.assertEqual(b'"My lambda ran succesfully"', r.content) - @parameterized.expand([("x86_64", "8002"), ("arm64", "9002"), ("", "9052")]) - def test_lambda_function_arn_exists(self, arch, port): + def test_lambda_function_arn_exists(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("arnexists", arch) - cmd = f"docker run --name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b'"My lambda ran succesfully"', r.content) + self.assertEqual(b'"My lambda ran succesfully"', r.content) - @parameterized.expand([("x86_64", "8003"), ("arm64", "9003"), ("", "9053")]) - def test_lambda_function_arn_exists_with_defining_custom_name(self, arch, port): + + def test_lambda_function_arn_exists_with_defining_custom_name(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("customname", arch) - cmd = f"docker run --name {image} --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" + cmd = f"--name {image} --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b'"My lambda ran succesfully"', r.content) + self.assertEqual(b'"My lambda ran succesfully"', r.content) + - @parameterized.expand([("x86_64", "8004"), ("arm64", "9004"), ("", "9054")]) - def test_timeout_invoke(self, arch, port): + def test_timeout_invoke(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("timeout", arch) - cmd = f"docker run --name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b"Task timed out after 1.00 seconds", r.content) + self.assertEqual(b"Task timed out after 1.00 seconds", r.content) - @parameterized.expand([("x86_64", "8005"), ("arm64", "9005"), ("", "9055")]) - def test_exception_returned(self, arch, port): + + def test_exception_returned(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("exception", arch) - cmd = f"docker run --name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler" + + with self.create_container(cmd, image): + r = self.invoke_function(port) + + # ignore request_id in python3.12 lambda + result = r.json() + self.assertEqual(result["errorMessage"],"Raising an exception") + self.assertEqual(result["errorType"],"Exception") + self.assertEqual(result["stackTrace"],[" File \"/var/task/main.py\", line 13, in exception_handler\n raise Exception(\"Raising an exception\")\n"]) - r = self.create_container_and_invoke_function(cmd, port) - - self.assertEqual( - b'{"errorMessage": "Raising an exception", "errorType": "Exception", "stackTrace": [" File \\"/var/task/main.py\\", line 13, in exception_handler\\n raise Exception(\\"Raising an exception\\")\\n"]}', - r.content, - ) - @parameterized.expand([("x86_64", "8006"), ("arm64", "9006"), ("", "9056")]) - def test_context_get_remaining_time_in_three_seconds(self, arch, port): + def test_context_get_remaining_time_in_three_seconds(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("remaining_time_in_three_seconds", arch) - cmd = f"docker run --name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=3 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=3 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - # Execution time is not decided, 1.0s ~ 3.0s is a good estimation - self.assertLess(int(r.content), 3000) - self.assertGreater(int(r.content), 1000) + # Execution time is not decided, 1.0s ~ 3.0s is a good estimation + self.assertLess(int(r.content), 3000) + self.assertGreater(int(r.content), 1000) - @parameterized.expand([("x86_64", "8007"), ("arm64", "9007"), ("", "9057")]) - def test_context_get_remaining_time_in_ten_seconds(self, arch, port): + + def test_context_get_remaining_time_in_ten_seconds(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("remaining_time_in_ten_seconds", arch) - cmd = f"docker run --name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=10 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=10 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - # Execution time is not decided, 8.0s ~ 10.0s is a good estimation - self.assertLess(int(r.content), 10000) - self.assertGreater(int(r.content), 8000) + # Execution time is not decided, 8.0s ~ 10.0s is a good estimation + self.assertLess(int(r.content), 10000) + self.assertGreater(int(r.content), 8000) + - @parameterized.expand([("x86_64", "8008"), ("arm64", "9008"), ("", "9058")]) - def test_context_get_remaining_time_in_default_deadline(self, arch, port): + def test_context_get_remaining_time_in_default_deadline(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("remaining_time_in_default_deadline", arch) - cmd = f"docker run --name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + + with self.create_container(cmd, image): + r = self.invoke_function(port) - r = self.create_container_and_invoke_function(cmd, port) + # Executation time is not decided, 298.0s ~ 300.0s is a good estimation + self.assertLess(int(r.content), 300000) + self.assertGreater(int(r.content), 298000) - # Executation time is not decided, 298.0s ~ 300.0s is a good estimation - self.assertLess(int(r.content), 300000) - self.assertGreater(int(r.content), 298000) - @parameterized.expand([("x86_64", "8009"), ("arm64", "9009"), ("", "9059")]) - def test_invoke_with_pre_runtime_api_runtime(self, arch, port): + def test_invoke_with_pre_runtime_api_runtime(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("pre-runtime-api", arch) - cmd = f"docker run --name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b'"My lambda ran succesfully"', r.content) + self.assertEqual(b'"My lambda ran succesfully"', r.content) - @parameterized.expand([("x86_64", "8010"), ("arm64", "9010"), ("", "9060")]) - def test_function_name_is_overriden(self, arch, port): + + def test_function_name_is_overriden(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("assert-overwritten", arch) - cmd = f"docker run --name {image} -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b'"My lambda ran succesfully"', r.content) + self.assertEqual(b'"My lambda ran succesfully"', r.content) + - @parameterized.expand([("x86_64", "8011"), ("arm64", "9011"), ("", "9061")]) - def test_port_override(self, arch, port): + def test_port_override(self, arch=ARCH, port=PORT): image, rie, image_name = self.tagged_name("port_override", arch) # Use port 8081 inside the container instead of 8080 - cmd = f"docker run --name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8081 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler --runtime-interface-emulator-address 0.0.0.0:8081" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8081 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler --runtime-interface-emulator-address 0.0.0.0:8081" - r = self.create_container_and_invoke_function(cmd, port) + with self.create_container(cmd, image): + r = self.invoke_function(port) - self.assertEqual(b'"My lambda ran succesfully"', r.content) + self.assertEqual(b'"My lambda ran succesfully"', r.content) + if __name__ == "__main__": diff --git a/test/integration/testdata/Dockerfile-allinone b/test/integration/testdata/Dockerfile-allinone index b804e5c..1d28406 100644 --- a/test/integration/testdata/Dockerfile-allinone +++ b/test/integration/testdata/Dockerfile-allinone @@ -1,4 +1,5 @@ -FROM public.ecr.aws/lambda/python:3.8 +ARG IMAGE_ARCH +FROM public.ecr.aws/lambda/python:3.12-$IMAGE_ARCH WORKDIR /var/task COPY ./ ./ From 2a71f855e02383504209031d023af24f59533008 Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Thu, 11 Apr 2024 18:10:14 +0000 Subject: [PATCH 2/5] gh action --- .github/workflows/integ-tests.yml | 36 +++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/.github/workflows/integ-tests.yml b/.github/workflows/integ-tests.yml index cb2f9dc..73e8fc2 100644 --- a/.github/workflows/integ-tests.yml +++ b/.github/workflows/integ-tests.yml @@ -6,7 +6,37 @@ on: - develop jobs: - integ-tests: + go-tests: + runs-on: ubuntu-latest + environment: + name: prod + steps: + - uses: actions/checkout@v4 + - name: run go tests + run: make tests-with-docker + integ-tests-x86: + runs-on: ubuntu-latest + environment: + name: prod + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: run integration tests + run: make integ-tests-with-docker-x86-64 + integ-tests-arm64: + runs-on: ubuntu-latest + environment: + name: prod + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: run integration tests + run: make integ-tests-with-docker-arm64 + integ-tests-old: runs-on: ubuntu-latest environment: name: prod @@ -15,7 +45,5 @@ jobs: - uses: actions/setup-python@v5 with: python-version: '3.11' - - name: allows us to build arm64 images - run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - name: run integration tests - run: make integ-tests-with-docker \ No newline at end of file + run: make integ-tests-with-docker-old \ No newline at end of file From d78aea83377271ca49dbc378bdc19dd768b243f5 Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Wed, 17 Apr 2024 00:45:31 +0000 Subject: [PATCH 3/5] removing unecessary params, optimize make file --- Makefile | 15 +- .../local_lambda/test_end_to_end.py | 131 +++++++++--------- 2 files changed, 74 insertions(+), 72 deletions(-) diff --git a/Makefile b/Makefile index 8dc3d49..0e55009 100644 --- a/Makefile +++ b/Makefile @@ -45,26 +45,29 @@ prep-python: .venv/bin/pip install --upgrade pip .venv/bin/pip install requests parameterized +exec-python-e2e-test: + TEST_ARCH=${TEST_ARCH} TEST_PORT=${TEST_PORT} .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + integ-tests: make prep-python docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - TEST_ARCH=x86_64 TEST_PORT=8002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py - TEST_ARCH=arm64 TEST_PORT=9002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py - TEST_ARCH="" TEST_PORT=9052 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + make TEST_ARCH=x86_64 TEST_PORT=8002 exec-python-e2e-test + make TEST_ARCH=arm64 TEST_PORT=9002 exec-python-e2e-test + make TEST_ARCH="" TEST_PORT=9052 exec-python-e2e-test integ-tests-with-docker-x86-64: make ARCH=x86_64 compile-with-docker make prep-python - TEST_ARCH=x86_64 TEST_PORT=8002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + make TEST_ARCH=x86_64 TEST_PORT=8002 exec-python-e2e-test integ-tests-with-docker-arm64: make ARCH=arm64 compile-with-docker make prep-python docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - TEST_ARCH="arm64" TEST_PORT=9002 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + make TEST_ARCH=arm64 TEST_PORT=9002 exec-python-e2e-test integ-tests-with-docker-old: make ARCH=old compile-with-docker make prep-python - TEST_ARCH="" TEST_PORT=9052 .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + make TEST_ARCH="" TEST_PORT=9052 exec-python-e2e-test \ No newline at end of file diff --git a/test/integration/local_lambda/test_end_to_end.py b/test/integration/local_lambda/test_end_to_end.py index 4046a31..47a9b2e 100644 --- a/test/integration/local_lambda/test_end_to_end.py +++ b/test/integration/local_lambda/test_end_to_end.py @@ -10,7 +10,7 @@ from contextlib import contextmanager from parameterized import parameterized -SLEEP_TIME = 2 +SLEEP_TIME = 1.5 DEFAULT_1P_ENTRYPOINT = "/lambda-entrypoint.sh" ARCHS = ["x86_64", "arm64", ""] @@ -23,11 +23,11 @@ class TestEndToEnd(TestCase): def setUpClass(cls): testdata_path = Path(__file__).resolve().parents[1].joinpath("testdata") dockerfile_path = testdata_path.joinpath("Dockerfile-allinone") - cls.image_name = "aws-lambda-local:testing" cls.path_to_binary = Path().resolve().joinpath("bin") # build image - image_name = cls.image_name if cls.ARCH == "" else f"{cls.image_name}-{cls.ARCH}" + image_name_base = "aws-lambda-local:testing" + cls.image_name = image_name_base if cls.ARCH == "" else f"{image_name_base}-{cls.ARCH}" architecture = cls.ARCH if cls.ARCH == "arm64" else "amd64" docker_arch = cls.ARCH if cls.ARCH == "arm64" else "x86_64" @@ -37,27 +37,25 @@ def setUpClass(cls): "--platform", f"linux/{architecture}", "-t", - image_name, + cls.image_name, "-f", str(dockerfile_path), str(testdata_path), "--build-arg", f"IMAGE_ARCH={docker_arch}", ] - print (build_cmd) Popen(build_cmd).communicate() @classmethod def tearDownClass(cls): - arch_tag = "" if cls.ARCH == "" else f"-{cls.ARCH}" - Popen(f"docker rmi {cls.image_name}{arch_tag}".split(" ")).communicate() + Popen(f"docker rmi {cls.image_name}".split(" ")).communicate() - def tagged_name(self, name, architecture): - tag = self.get_tag(architecture) - return (name + tag, "aws-lambda-rie" + tag, self.image_name + tag) + def tagged_name(self, name): + tag = self.get_tag() + return (name + tag, "aws-lambda-rie" + tag, self.image_name) - def get_tag(self, architecture): - return "" if architecture == "" else str(f"-{architecture}") + def get_tag(self): + return "" if self.ARCH == "" else str(f"-{self.ARCH}") def run_command(self, cmd): Popen(cmd.split(" ")).communicate() @@ -65,16 +63,16 @@ def run_command(self, cmd): def sleep_1s(self): time.sleep(SLEEP_TIME) - def invoke_function(self, port): + def invoke_function(self): return requests.post( - f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={} + f"http://localhost:{self.PORT}/2015-03-31/functions/function/invocations", json={} ) @contextmanager - def create_container(self, cmd, image): + def create_container(self, param, image): try: platform = "x86_64" if self.ARCH == "" else self.ARCH - cmd_full = f"docker run --platform linux/{platform} {cmd}" + cmd_full = f"docker run --platform linux/{platform} {param}" self.run_command(cmd_full) # sleep 1s to give enough time for the endpoint to be up to curl @@ -88,149 +86,150 @@ def create_container(self, cmd, image): self.run_command(f"docker rm -f {image}") - def test_env_var_with_equal_sign(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("envvarcheck", arch) - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler" + def test_env_var_with_equal_sign(self): + image, rie, image_name = self.tagged_name("envvarcheck") + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"4=4"', r.content) - def test_two_invokes(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("twoinvokes", arch) + def test_two_invokes(self): + image, rie, image_name = self.tagged_name("twoinvokes") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) # Make sure we can invoke the function twice - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) - def test_lambda_function_arn_exists(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("arnexists", arch) + def test_lambda_function_arn_exists(self): + image, rie, image_name = self.tagged_name("arnexists") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) - def test_lambda_function_arn_exists_with_defining_custom_name(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("customname", arch) + def test_lambda_function_arn_exists_with_defining_custom_name(self): + image, rie, image_name = self.tagged_name("customname") - cmd = f"--name {image} --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" + cmd = f"--name {image} --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) - def test_timeout_invoke(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("timeout", arch) + def test_timeout_invoke(self): + image, rie, image_name = self.tagged_name("timeout") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b"Task timed out after 1.00 seconds", r.content) - def test_exception_returned(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("exception", arch) + def test_exception_returned(self): + image, rie, image_name = self.tagged_name("exception") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() - # ignore request_id in python3.12 lambda + # Except the 3 fields assrted below, there's another field `request_id` included start from python3.12 runtime. + # We should ignore asserting the field `request_id` for it is in a UUID like format and chages everytime result = r.json() self.assertEqual(result["errorMessage"],"Raising an exception") self.assertEqual(result["errorType"],"Exception") self.assertEqual(result["stackTrace"],[" File \"/var/task/main.py\", line 13, in exception_handler\n raise Exception(\"Raising an exception\")\n"]) - def test_context_get_remaining_time_in_three_seconds(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("remaining_time_in_three_seconds", arch) + def test_context_get_remaining_time_in_three_seconds(self): + image, rie, image_name = self.tagged_name("remaining_time_in_three_seconds") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=3 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=3 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() # Execution time is not decided, 1.0s ~ 3.0s is a good estimation self.assertLess(int(r.content), 3000) self.assertGreater(int(r.content), 1000) - def test_context_get_remaining_time_in_ten_seconds(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("remaining_time_in_ten_seconds", arch) + def test_context_get_remaining_time_in_ten_seconds(self): + image, rie, image_name = self.tagged_name("remaining_time_in_ten_seconds") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=10 -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=10 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() # Execution time is not decided, 8.0s ~ 10.0s is a good estimation self.assertLess(int(r.content), 10000) self.assertGreater(int(r.content), 8000) - def test_context_get_remaining_time_in_default_deadline(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("remaining_time_in_default_deadline", arch) + def test_context_get_remaining_time_in_default_deadline(self): + image, rie, image_name = self.tagged_name("remaining_time_in_default_deadline") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() # Executation time is not decided, 298.0s ~ 300.0s is a good estimation self.assertLess(int(r.content), 300000) self.assertGreater(int(r.content), 298000) - def test_invoke_with_pre_runtime_api_runtime(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("pre-runtime-api", arch) + def test_invoke_with_pre_runtime_api_runtime(self): + image, rie, image_name = self.tagged_name("pre-runtime-api") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) - def test_function_name_is_overriden(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("assert-overwritten", arch) + def test_function_name_is_overriden(self): + image, rie, image_name = self.tagged_name("assert-overwritten") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten" + cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) - def test_port_override(self, arch=ARCH, port=PORT): - image, rie, image_name = self.tagged_name("port_override", arch) + def test_port_override(self): + image, rie, image_name = self.tagged_name("port_override") # Use port 8081 inside the container instead of 8080 - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {port}:8081 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler --runtime-interface-emulator-address 0.0.0.0:8081" + cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8081 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler --runtime-interface-emulator-address 0.0.0.0:8081" with self.create_container(cmd, image): - r = self.invoke_function(port) + r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) From 6e0c33e94dc735a26ccafbbd417e2d260ad482ce Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Fri, 19 Apr 2024 17:16:10 +0000 Subject: [PATCH 4/5] fix nit, naming --- .github/workflows/integ-tests.yml | 8 +-- Makefile | 2 +- .../local_lambda/test_end_to_end.py | 52 +++++++++---------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/.github/workflows/integ-tests.yml b/.github/workflows/integ-tests.yml index 73e8fc2..7fddc95 100644 --- a/.github/workflows/integ-tests.yml +++ b/.github/workflows/integ-tests.yml @@ -9,7 +9,7 @@ jobs: go-tests: runs-on: ubuntu-latest environment: - name: prod + name: integ-tests steps: - uses: actions/checkout@v4 - name: run go tests @@ -17,7 +17,7 @@ jobs: integ-tests-x86: runs-on: ubuntu-latest environment: - name: prod + name: integ-tests steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 @@ -28,7 +28,7 @@ jobs: integ-tests-arm64: runs-on: ubuntu-latest environment: - name: prod + name: integ-tests steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 @@ -39,7 +39,7 @@ jobs: integ-tests-old: runs-on: ubuntu-latest environment: - name: prod + name: integ-tests steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 diff --git a/Makefile b/Makefile index 0e55009..f7a714e 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ prep-python: .venv/bin/pip install requests parameterized exec-python-e2e-test: - TEST_ARCH=${TEST_ARCH} TEST_PORT=${TEST_PORT} .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py + .venv/bin/python3 test/integration/local_lambda/test_end_to_end.py integ-tests: make prep-python diff --git a/test/integration/local_lambda/test_end_to_end.py b/test/integration/local_lambda/test_end_to_end.py index 47a9b2e..6e741f4 100644 --- a/test/integration/local_lambda/test_end_to_end.py +++ b/test/integration/local_lambda/test_end_to_end.py @@ -99,9 +99,9 @@ def test_env_var_with_equal_sign(self): def test_two_invokes(self): image, rie, image_name = self.tagged_name("twoinvokes") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) @@ -115,9 +115,9 @@ def test_two_invokes(self): def test_lambda_function_arn_exists(self): image, rie, image_name = self.tagged_name("arnexists") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) @@ -126,9 +126,9 @@ def test_lambda_function_arn_exists(self): def test_lambda_function_arn_exists_with_defining_custom_name(self): image, rie, image_name = self.tagged_name("customname") - cmd = f"--name {image} --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" + params = f"--name {image} --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) @@ -137,9 +137,9 @@ def test_lambda_function_arn_exists_with_defining_custom_name(self): def test_timeout_invoke(self): image, rie, image_name = self.tagged_name("timeout") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler" + params = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b"Task timed out after 1.00 seconds", r.content) @@ -148,25 +148,25 @@ def test_timeout_invoke(self): def test_exception_returned(self): image, rie, image_name = self.tagged_name("exception") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler" + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() # Except the 3 fields assrted below, there's another field `request_id` included start from python3.12 runtime. - # We should ignore asserting the field `request_id` for it is in a UUID like format and chages everytime + # We should ignore asserting the field `request_id` for it is in a UUID like format and changes everytime result = r.json() - self.assertEqual(result["errorMessage"],"Raising an exception") - self.assertEqual(result["errorType"],"Exception") - self.assertEqual(result["stackTrace"],[" File \"/var/task/main.py\", line 13, in exception_handler\n raise Exception(\"Raising an exception\")\n"]) + self.assertEqual(result["errorMessage"], "Raising an exception") + self.assertEqual(result["errorType"], "Exception") + self.assertEqual(result["stackTrace"], [" File \"/var/task/main.py\", line 13, in exception_handler\n raise Exception(\"Raising an exception\")\n"]) def test_context_get_remaining_time_in_three_seconds(self): image, rie, image_name = self.tagged_name("remaining_time_in_three_seconds") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=3 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + params = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=3 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() # Execution time is not decided, 1.0s ~ 3.0s is a good estimation @@ -177,9 +177,9 @@ def test_context_get_remaining_time_in_three_seconds(self): def test_context_get_remaining_time_in_ten_seconds(self): image, rie, image_name = self.tagged_name("remaining_time_in_ten_seconds") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=10 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + params = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=10 -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() # Execution time is not decided, 8.0s ~ 10.0s is a good estimation @@ -190,9 +190,9 @@ def test_context_get_remaining_time_in_ten_seconds(self): def test_context_get_remaining_time_in_default_deadline(self): image, rie, image_name = self.tagged_name("remaining_time_in_default_deadline") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_remaining_time_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() # Executation time is not decided, 298.0s ~ 300.0s is a good estimation @@ -203,9 +203,9 @@ def test_context_get_remaining_time_in_default_deadline(self): def test_invoke_with_pre_runtime_api_runtime(self): image, rie, image_name = self.tagged_name("pre-runtime-api") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) @@ -214,9 +214,9 @@ def test_invoke_with_pre_runtime_api_runtime(self): def test_function_name_is_overriden(self): image, rie, image_name = self.tagged_name("assert-overwritten") - cmd = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten" + params = f"--name {image} -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) @@ -226,9 +226,9 @@ def test_port_override(self): image, rie, image_name = self.tagged_name("port_override") # Use port 8081 inside the container instead of 8080 - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8081 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler --runtime-interface-emulator-address 0.0.0.0:8081" + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8081 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler --runtime-interface-emulator-address 0.0.0.0:8081" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b'"My lambda ran succesfully"', r.content) From 0d94f517cf5138b2b75f3e2004c880806fd92e64 Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Fri, 19 Apr 2024 18:39:21 +0000 Subject: [PATCH 5/5] missed var change --- test/integration/local_lambda/test_end_to_end.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/local_lambda/test_end_to_end.py b/test/integration/local_lambda/test_end_to_end.py index 6e741f4..7c5486f 100644 --- a/test/integration/local_lambda/test_end_to_end.py +++ b/test/integration/local_lambda/test_end_to_end.py @@ -88,9 +88,9 @@ def create_container(self, param, image): def test_env_var_with_equal_sign(self): image, rie, image_name = self.tagged_name("envvarcheck") - cmd = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler" + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler" - with self.create_container(cmd, image): + with self.create_container(params, image): r = self.invoke_function() self.assertEqual(b'"4=4"', r.content)