Skip to content

Commit 0a946cd

Browse files
committed
add health check after restarting container
1 parent ddbe64d commit 0a946cd

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

tests/e2e/utils/utils.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,41 @@ def validate_json(message: Any, schema: Any) -> None:
3232
assert False, "The provided schema is faulty:" + str(e)
3333

3434

35+
def wait_for_container_health(container_name: str, max_attempts: int = 3) -> None:
36+
"""Wait for container to be healthy."""
37+
for attempt in range(max_attempts):
38+
try:
39+
result = subprocess.run(
40+
[
41+
"docker",
42+
"inspect",
43+
"--format={{.State.Health.Status}}",
44+
container_name,
45+
],
46+
capture_output=True,
47+
text=True,
48+
check=True,
49+
timeout=10,
50+
)
51+
if result.stdout.strip() == "healthy":
52+
break
53+
else:
54+
if attempt < max_attempts - 1:
55+
time.sleep(5)
56+
else:
57+
print(
58+
f"{container_name} not healthy after {max_attempts * 5} seconds"
59+
)
60+
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
61+
pass
62+
63+
if attempt < max_attempts - 1:
64+
print(f"⏱ Attempt {attempt + 1}/{max_attempts} - waiting...")
65+
time.sleep(5)
66+
else:
67+
print(f"Could not check health status for {container_name}")
68+
69+
3570
def switch_config_and_restart(
3671
original_file: str,
3772
replacement_file: str,
@@ -76,8 +111,8 @@ def switch_config_and_restart(
76111
print(f"Failed to restart container {container_name}: {e.stderr}")
77112
raise
78113

79-
# Wait for container to be ready
80-
time.sleep(5)
114+
# Wait for container to be healthy
115+
wait_for_container_health(container_name)
81116

82117
# Clean up backup file
83118
if cleanup and os.path.exists(backup_file):

0 commit comments

Comments
 (0)