Skip to content

Commit b7fedbd

Browse files
KandarpAjvaliaKandarp Ajvalia
andauthored
fix: failing DNS integration tests by using long-running functions (#8889)
* fix: fix failing DNS integration tests by using long-running functions The DNS container inspection tests were failing because Lambda containers exit immediately after the handler completes, making inspection impossible. Changes: - test_invoke_dns.py: Use WriteToStdoutFunction instead of HelloWorldServerlessFunction to avoid KeyError when no_event=True is passed - test_start_api_dns.py: Use threading with /sleepfortenseconds endpoint to keep container alive during inspection - test_start_lambda_dns.py: Use threading with HelloWorldSleepFunction to keep container alive during inspection All tests now use long-running functions (10+ seconds) with async threading pattern, allowing enough time to inspect running containers before they exit. Fixes tests that were failing after #8878 was merged. * fix: black formatting --------- Co-authored-by: Kandarp Ajvalia <ajvalia@amazon.com>
1 parent b32bf16 commit b7fedbd

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

tests/integration/local/invoke/test_invoke_dns.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_invoke_accepts_multiple_dns_servers(self):
9090
and executes successfully
9191
"""
9292
command_list = self.get_command_list(
93-
function_to_invoke="HelloWorldServerlessFunction",
93+
function_to_invoke="WriteToStdoutFunction",
9494
template_path=self.template_path,
9595
no_event=True,
9696
)
@@ -104,4 +104,4 @@ def test_invoke_accepts_multiple_dns_servers(self):
104104
self.assertEqual(return_code, 0, f"Command failed with stderr: {stderr}")
105105

106106
output = stdout.decode("utf-8")
107-
self.assertIn("Hello World", output)
107+
self.assertIn("wrote to stdout", output)

tests/integration/local/start_api/test_start_api_dns.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Integration tests for DNS option in sam local start-api
33
"""
44

5+
import time
6+
import threading
57
import requests
68
import pytest
79
from tests.integration.local.start_api.start_api_integ_base import StartApiIntegBaseClass
@@ -44,8 +46,19 @@ def test_dns_configured_in_container(self):
4446
Test that DNS servers are actually configured in the Docker container.
4547
This inspects the container configuration to verify DNS was set correctly.
4648
"""
47-
response = requests.get(f"http://127.0.0.1:{self.port}/anyandall", timeout=300)
48-
self.assertEqual(response.status_code, 200)
49+
# Start async request to keep container alive during inspection
50+
response_holder = {}
51+
52+
def make_request():
53+
response_holder["response"] = requests.get(
54+
f"http://127.0.0.1:{self.port}/sleepfortenseconds/function0", timeout=300
55+
)
56+
57+
request_thread = threading.Thread(target=make_request)
58+
request_thread.start()
59+
60+
# Wait for container to start and be in sleep phase
61+
time.sleep(7)
4962

5063
sam_containers = self.docker_client.containers.list(
5164
all=False, filters={"label": "sam.cli.container.type=lambda"}
@@ -76,5 +89,9 @@ def test_dns_configured_in_container(self):
7689

7790
self.assertTrue(
7891
dns_verified,
79-
f"Could not verify DNS configuration in any container. " f"Checked {len(sam_containers)} containers",
92+
f"Could not verify DNS configuration in any container. Checked {len(sam_containers)} containers",
8093
)
94+
95+
# Wait for request to complete
96+
request_thread.join()
97+
self.assertEqual(response_holder["response"].status_code, 200)

tests/integration/local/start_lambda/test_start_lambda_dns.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Integration tests for DNS option in sam local start-lambda
33
"""
44

5+
import time
6+
import threading
57
import pytest
68
from tests.integration.local.start_lambda.start_lambda_api_integ_base import StartLambdaIntegBaseClass
79

@@ -78,8 +80,19 @@ def test_dns_configured_in_container(self):
7880
Test that DNS servers are actually configured in the Docker container.
7981
This inspects the container configuration to verify DNS was set correctly.
8082
"""
81-
response = self.lambda_client.invoke(FunctionName="EchoEventFunction", Payload='{"key": "value"}')
82-
self.assertEqual(response.get("StatusCode"), 200)
83+
# Start async invocation to keep container alive during inspection
84+
response_holder = {}
85+
86+
def invoke_function():
87+
response_holder["response"] = self.lambda_client.invoke(
88+
FunctionName="HelloWorldSleepFunction", Payload="{}"
89+
)
90+
91+
invoke_thread = threading.Thread(target=invoke_function)
92+
invoke_thread.start()
93+
94+
# Wait for container to start and be in sleep phase
95+
time.sleep(5)
8396

8497
sam_containers = self.docker_client.containers.list(
8598
all=False, filters={"label": "sam.cli.container.type=lambda"}
@@ -113,5 +126,9 @@ def test_dns_configured_in_container(self):
113126

114127
self.assertTrue(
115128
dns_verified,
116-
f"Could not verify DNS configuration in any container. " f"Checked {len(sam_containers)} containers",
129+
f"Could not verify DNS configuration in any container. Checked {len(sam_containers)} containers",
117130
)
131+
132+
# Wait for invocation to complete
133+
invoke_thread.join()
134+
self.assertEqual(response_holder["response"].get("StatusCode"), 200)

0 commit comments

Comments
 (0)