Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 16 additions & 4 deletions scripts/ci/wait-for-container-health.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ if ! [[ "$sleep_seconds" =~ ^[0-9]+$ ]] || [ "$sleep_seconds" -lt 1 ]; then
exit 2
fi

# Classify container startup failures without echoing untrusted service logs
# into public Actions output. Raw logs remain job-local for gated inspection.
diagnose_health_failure() {
classifier="$(dirname "$0")/classify-startup-logs.py"
if [ -f "$classifier" ] && command -v python3 >/dev/null 2>&1; then
docker compose -f "$compose_file" --profile "$profile" logs --no-color --tail 200 "$service" 2>/dev/null |
python3 "$classifier" || echo "STARTUP_DIAGNOSTIC stage=classifier_failed"
else
echo "STARTUP_DIAGNOSTIC stage=classifier_unavailable"
fi
}

container_id="$(docker compose -f "$compose_file" --profile "$profile" ps -q "$service" | head -n1)"
if [ -z "${container_id}" ]; then
echo "ERROR: no container found for service=${service} profile=${profile}" >&2
Expand All @@ -27,12 +39,12 @@ for ((i=1; i<=max_attempts; i++)); do
status="$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container_id" 2>/dev/null || echo unknown)"
case "$status" in
healthy)
echo "Service ${service} is healthy (${container_id})"
echo "Service ${service} is healthy"
exit 0
;;
unhealthy)
echo "ERROR: service ${service} became unhealthy (${container_id})" >&2
docker compose -f "$compose_file" --profile "$profile" logs --tail 200 "$service" || true
echo "ERROR: service ${service} became unhealthy" >&2
diagnose_health_failure
exit 1
;;
*)
Expand All @@ -43,5 +55,5 @@ for ((i=1; i<=max_attempts; i++)); do
done

echo "ERROR: service ${service} did not become healthy after ${max_attempts} attempts" >&2
docker compose -f "$compose_file" --profile "$profile" logs --tail 200 "$service" || true
diagnose_health_failure
exit 1
46 changes: 46 additions & 0 deletions tests/test_ci_unit_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,51 @@ def test_generated_api_token_is_not_ever_echoed(self):
self.assertIn('echo -n "$API_TOKEN" > /tmp/winebot_api_token', source)


def test_health_wait_failure_output_is_allowlisted(self):
source = (ROOT / "scripts/ci/wait-for-container-health.sh").read_text(
encoding="utf-8"
)
self.assertEqual(source.count("diagnose_health_failure"), 3)
self.assertIn('python3 "$classifier"', source)
self.assertIn('logs --no-color --tail 200 "$service" 2>/dev/null', source)
self.assertNotIn('logs --tail 200 "$service" || true', source)

def test_health_waiter_never_echoes_sensitive_service_log_content(self):
import os
import subprocess
import tempfile

with tempfile.TemporaryDirectory() as directory:
mock_docker = Path(directory) / "docker"
mock_docker.write_text(
"""#!/bin/sh
case "$1" in
inspect) printf '%s\\n' unhealthy;;
compose)
case " $* " in
*" ps -q "*) printf '%s\\n' mock123;;
*" logs "*) printf '%s\\n' SYNTHETIC_SECRET 'WINEBOT_STARTUP_ERROR stage=services/trace-log-path rc=1';;
*) exit 4;;
esac;;
*) exit 4;;
esac
""",
encoding="utf-8",
)
mock_docker.chmod(0o755)
env = os.environ.copy()
env["PATH"] = directory + os.pathsep + env["PATH"]
result = subprocess.run(
["bash", str(ROOT / "scripts/ci/wait-for-container-health.sh"),
"compose/docker-compose.yml", "interactive",
"winebot-interactive", "1", "1"],
env=env, capture_output=True, text=True, timeout=10, check=False
)
self.assertEqual(result.returncode, 1)
self.assertIn("STARTUP_DIAGNOSTIC stage=failed_trace_log_path",
result.stdout + result.stderr)
self.assertNotIn("SYNTHETIC_SECRET", result.stdout + result.stderr)
self.assertNotIn("mock123", result.stdout + result.stderr)

if __name__ == "__main__":
unittest.main()
Loading