Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions tests/e2e/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ def validate_json(message: Any, schema: Any) -> None:
assert False, "The provided schema is faulty:" + str(e)


def wait_for_container_health(container_name: str, max_attempts: int = 3) -> None:
"""Wait for container to be healthy."""
for attempt in range(max_attempts):
try:
result = subprocess.run(
[
"docker",
"inspect",
"--format={{.State.Health.Status}}",
container_name,
],
capture_output=True,
text=True,
check=True,
timeout=10,
)
if result.stdout.strip() == "healthy":
break
else:
if attempt < max_attempts - 1:
time.sleep(5)
else:
print(
f"{container_name} not healthy after {max_attempts * 5} seconds"
)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
pass

if attempt < max_attempts - 1:
print(f"⏱ Attempt {attempt + 1}/{max_attempts} - waiting...")
time.sleep(5)
else:
print(f"Could not check health status for {container_name}")


def switch_config_and_restart(
original_file: str,
replacement_file: str,
Expand Down Expand Up @@ -76,8 +111,8 @@ def switch_config_and_restart(
print(f"Failed to restart container {container_name}: {e.stderr}")
raise

# Wait for container to be ready
time.sleep(5)
# Wait for container to be healthy
wait_for_container_health(container_name)

Comment on lines +114 to 116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Surface restart failures with a timeout and clearer error reporting.

TimeoutExpired won’t fire without a timeout; also bubble up a concise message.

     # Restart container
     try:
         subprocess.run(
             ["docker", "restart", container_name],
             capture_output=True,
             text=True,
             check=True,
+            timeout=60,
         )
-    except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
-        print(f"Failed to restart container {container_name}: {e.stderr}")
+    except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
+        err = getattr(e, "stderr", None) or str(e)
+        print(f"Failed to restart container {container_name}: {err}")
         raise
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Wait for container to be healthy
wait_for_container_health(container_name)
# Restart container
try:
subprocess.run(
["docker", "restart", container_name],
capture_output=True,
text=True,
check=True,
timeout=60,
)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
err = getattr(e, "stderr", None) or str(e)
print(f"Failed to restart container {container_name}: {err}")
raise
🤖 Prompt for AI Agents
In tests/e2e/utils/utils.py around lines 114 to 116, the call to
wait_for_container_health(container_name) currently can hang indefinitely and
any TimeoutExpired won't be raised because no timeout is provided; update the
call to pass a reasonable timeout (e.g., timeout_seconds or a constant) and wrap
the call in a try/except that catches TimeoutExpired (and optionally
subprocess.TimeoutExpired) and re-raises a concise, informative exception (or
raise RuntimeError) that includes the container name and that the container
failed to become healthy within the timeout; ensure the timeout value is
configurable or clearly documented.

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