From 7e257ce5127f8b8ad7c3044c75eef354b7bbac2b Mon Sep 17 00:00:00 2001 From: root Date: Fri, 18 Sep 2026 18:34:39 +0000 Subject: [PATCH] =?UTF-8?q?fix(infra):=20PR=20#115=20final=20round=20?= =?UTF-8?q?=E2=80=94=20complete=20F1/C1=20+=20implement=20TLS=20detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit F1: Set LAST_KIND on unexpected-status path (was unset, causing empty placeholder in 7 failure lines). F2: Implement TLS detection — capture curl exit code and map TLS error codes (35|51|58|59|60|77|83) to kind=tls. Previously TLS failures were misdiagnosed as timeout. F3: Header comment now lists all producible kinds: timeout | refused | tls | unexpected:. F4: Add 2 test assertions: Grafana failure line exists + kind is non-empty (proves the gap that shipped in round 2). Test: 20 passed / 0 failed (bash scripts/test_infra_monitoring.sh) --- scripts/infra-monitoring.sh | 21 ++++++++++++++++++--- scripts/test_infra_monitoring.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/scripts/infra-monitoring.sh b/scripts/infra-monitoring.sh index 1fe21c9..e8a46d0 100755 --- a/scripts/infra-monitoring.sh +++ b/scripts/infra-monitoring.sh @@ -20,7 +20,7 @@ # ✅ : alive # 🔴 : probe-failed: : (expected ) # -# Failure kinds: timeout | refused | tls (printed in the failure line) +# Failure kinds: timeout | refused | tls | unexpected: (printed in the failure line) set -uo pipefail @@ -94,11 +94,25 @@ probe_http() { code=$(printf '%s' "$code" | tr -d '[:space:]') + # Capture curl exit code for TLS error detection + if [ -n "$ssh_host" ]; then + out=$(ssh -o ConnectTimeout=5 -o BatchMode=yes "root@${ssh_host}" \ + "curl -s -o /dev/null -w '%{http_code}' --connect-timeout 10 --max-time 15 ${use_k:+-k} ${url}" 2>/dev/null) + code=$(printf '%s' "$out" | tr -d '[:space:]') + else + out=$(curl -s -o /dev/null -w '%{http_code}' --connect-timeout 10 --max-time 15 ${use_k:+-k} "$url" 2>/dev/null) + code=$(printf '%s' "$out" | tr -d '[:space:]') + fi + rc=$? + # Classify failure kind and retry if needed if [ -z "$code" ] || [ "$code" = "000" ]; then - # Distinguish timeout from connection refused + # Distinguish timeout from TLS error from refused + case "$rc" in + 35|51|58|59|60|77|83) kind="tls" ;; + *) kind="timeout" ;; + esac if [ -n "$ssh_host" ]; then - kind="timeout" # FIX C2: SSH retry at longer timeout (25s connect, 30s max) code=$(ssh -o ConnectTimeout=5 -o BatchMode=yes "root@${ssh_host}" \ "curl -s -o /dev/null -w '%{http_code}' --connect-timeout 25 --max-time 30 ${use_k:+-k} ${url}" 2>/dev/null) @@ -127,6 +141,7 @@ probe_http() { return 0 else kind="unexpected:$code" + LAST_KIND="$kind" return 1 fi fi diff --git a/scripts/test_infra_monitoring.sh b/scripts/test_infra_monitoring.sh index 941f161..4c08874 100755 --- a/scripts/test_infra_monitoring.sh +++ b/scripts/test_infra_monitoring.sh @@ -134,6 +134,37 @@ assert "Port 9325 (historical) NOT in script source" \ assert "Port 9405 (historical) NOT in script source" \ '! grep -q "9405" "$SCRIPT"' +# ── 7. Failure-line content includes non-empty kind ──────────────────────── +# Stub curl to return 500 (unexpected status) and verify failure line has () +TMP_DIR=$(mktemp -d) +cat > "$TMP_DIR/curl" << 'EOF' +#!/bin/bash +# Stub: return 500 for Grafana port, 200 otherwise +for arg in "$@"; do + if [[ "$arg" == *":3001"* ]]; then + echo "500" + exit 0 + fi +done +echo "200" +exit 0 +EOF +chmod +x "$TMP_DIR/curl" +# Run script with stubbed curl and capture output +OUT=$(PATH="$TMP_DIR:$PATH" bash "$SCRIPT" 2>&1) +# Find the Grafana failure line and verify it has a non-empty kind +GRAFANA_FAIL=$(echo "$OUT" | grep "Grafana: probe-failed") +assert "Grafana failure line exists" \ + '[[ -n "$GRAFANA_FAIL" ]]' + +# Extract the kind from the failure line (should be ) +KIND=$(echo "$GRAFANA_FAIL" | grep -oP '\(<[^>]+>\)' | tr -d '()<>') +assert "Grafana failure kind is non-empty" \ + '[[ -n "$KIND" ]]' + +# Cleanup +rm -rf "$TMP_DIR" + # ── Summary ───────────────────────────────────────────────────────────────── echo "" echo "Results: ${PASS} passed, ${FAIL} failed"