Skip to content

Commit

Permalink
test: Refactored end-to-end tests (#113)
Browse files Browse the repository at this point in the history
* Refactored end-to-end tests to reduce duplicated code.
  • Loading branch information
mbfreder authored Mar 7, 2024
1 parent d5d7505 commit 1a32012
Showing 1 changed file with 46 additions and 101 deletions.
147 changes: 46 additions & 101 deletions test/integration/local_lambda/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,34 @@ def tagged_name(self, name, architecture):

def get_tag(self, architecture):
return "" if architecture == "" else str(f"-{architecture}")

def run_command(self, cmd):
Popen(cmd.split(" ")).communicate()

def sleep_1s(self):
time.sleep(SLEEP_TIME)

def invoke_function(self, port):
return requests.post(
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):
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"
Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)

r = self.create_container_and_invoke_function(cmd, port)

self.assertEqual(b'"4=4"', r.content)

@parameterized.expand([("x86_64", "8001"), ("arm64", "9001"), ("", "9051")])
Expand All @@ -94,20 +108,13 @@ def test_two_invokes(self, arch, port):

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"

Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
r = self.create_container_and_invoke_function(cmd, port)

self.assertEqual(b'"My lambda ran succesfully"', r.content)

# Make sure we can invoke the function twice
r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
r = self.invoke_function(port)

self.assertEqual(b'"My lambda ran succesfully"', r.content)

@parameterized.expand([("x86_64", "8002"), ("arm64", "9002"), ("", "9052")])
Expand All @@ -116,29 +123,18 @@ def test_lambda_function_arn_exists(self, arch, port):

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"

Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
r = self.create_container_and_invoke_function(cmd, port)

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):
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"
Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)

r = self.create_container_and_invoke_function(cmd, port)

self.assertEqual(b'"My lambda ran succesfully"', r.content)

@parameterized.expand([("x86_64", "8004"), ("arm64", "9004"), ("", "9054")])
Expand All @@ -147,14 +143,8 @@ def test_timeout_invoke(self, arch, port):

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"

Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
r = self.create_container_and_invoke_function(cmd, port)

self.assertEqual(b"Task timed out after 1.00 seconds", r.content)

@parameterized.expand([("x86_64", "8005"), ("arm64", "9005"), ("", "9055")])
Expand All @@ -163,14 +153,8 @@ def test_exception_returned(self, arch, port):

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"

Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
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,
Expand All @@ -182,15 +166,8 @@ def test_context_get_remaining_time_in_three_seconds(self, arch, port):

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"

Popen(cmd.split(' ')).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)

r = self.create_container_and_invoke_function(cmd, 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)
Expand All @@ -201,15 +178,8 @@ def test_context_get_remaining_time_in_ten_seconds(self, arch, port):

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"

Popen(cmd.split(' ')).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)

r = self.create_container_and_invoke_function(cmd, 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)
Expand All @@ -220,14 +190,7 @@ def test_context_get_remaining_time_in_default_deadline(self, arch, port):

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"

Popen(cmd.split(' ')).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
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)
Expand All @@ -239,14 +202,8 @@ def test_invoke_with_pre_runtime_api_runtime(self, arch, port):

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"

Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
r = self.create_container_and_invoke_function(cmd, port)

self.assertEqual(b'"My lambda ran succesfully"', r.content)

@parameterized.expand([("x86_64", "8010"), ("arm64", "9010"), ("", "9060")])
Expand All @@ -255,14 +212,8 @@ def test_function_name_is_overriden(self, arch, port):

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"

Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
r = self.create_container_and_invoke_function(cmd, port)

self.assertEqual(b'"My lambda ran succesfully"', r.content)

@parameterized.expand([("x86_64", "8011"), ("arm64", "9011"), ("", "9061")])
Expand All @@ -272,14 +223,8 @@ def test_port_override(self, arch, port):
# 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"

Popen(cmd.split(" ")).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post(
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
)
r = self.create_container_and_invoke_function(cmd, port)

self.assertEqual(b'"My lambda ran succesfully"', r.content)


Expand Down

0 comments on commit 1a32012

Please sign in to comment.