From 0a946cd4e5092e46fd009376fc982d5db2704cc6 Mon Sep 17 00:00:00 2001 From: Radovan Fuchs Date: Wed, 3 Sep 2025 10:46:51 +0200 Subject: [PATCH] add health check after restarting container --- tests/e2e/utils/utils.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/tests/e2e/utils/utils.py b/tests/e2e/utils/utils.py index 2ace9b4c..5f189ff9 100644 --- a/tests/e2e/utils/utils.py +++ b/tests/e2e/utils/utils.py @@ -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, @@ -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) # Clean up backup file if cleanup and os.path.exists(backup_file):