#!/usr/bin/env bash # Red-first contract harness for RM-61 / #1000. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" VERIFIER="$SCRIPT_DIR/verify-terminal-green.py" TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT write_fixture() { local file="$1" pipeline_status="$2" postgres_state="$3" postgres_exit="$4" postgres_error="$5" test_state="$6" python3 - "$file" "$pipeline_status" "$postgres_state" "$postgres_exit" "$postgres_error" "$test_state" <<'PY' import json, sys path, pipeline_status, pg_state, pg_exit, pg_error, test_state = sys.argv[1:] steps = [ {"name": "clone", "type": "clone", "state": "success", "exit_code": 0, "error": None}, {"name": "ci-postgres", "type": "service", "state": pg_state, "exit_code": int(pg_exit), "error": pg_error or None}, {"name": "test", "type": "commands", "state": test_state, "exit_code": 0 if test_state == "success" else 1, "error": None}, ] json.dump({ "number": 9999, "status": pipeline_status, "commit": "a" * 40, "workflows": [{"name": "ci", "state": pipeline_status, "children": steps}], }, open(path, "w")) PY } expect_exit() { local expected="$1" label="$2" file="$3" shift 3 set +e output=$(python3 "$VERIFIER" "$file" "$@" 2>&1) actual=$? set -e if [[ "$actual" -ne "$expected" ]]; then printf 'FAIL %s: expected exit %s, got %s\n%s\n' "$label" "$expected" "$actual" "$output" >&2 exit 1 fi printf 'PASS %s\n' "$label" printf '%s' "$output" } # Ordinary terminal green. write_fixture "$TMP/green.json" success success 0 '' success out=$(expect_exit 0 green "$TMP/green.json") grep -q '"total_steps": 3' <<<"$out" grep -q '"exempted_steps": 0' <<<"$out" # Exact, named #1000 teardown artifact: the only permitted non-success child. artifact='pods "wp-svc-01kyxzjhdf6w81swsnbfzh85z9-ci-postgres" not found' write_fixture "$TMP/artifact.json" success failure 0 "$artifact" success out=$(expect_exit 0 exact-artifact "$TMP/artifact.json") grep -q '"exemption_id": "WP-K8S-1000-CI-POSTGRES-TEARDOWN"' <<<"$out" grep -q '"exempted_steps": 1' <<<"$out" # Negative controls: both real PostgreSQL failures must remain red. write_fixture "$TMP/startup.json" failure failure 1 '' failure expect_exit 1 startup-failure "$TMP/startup.json" >/dev/null write_fixture "$TMP/crash.json" failure failure 137 '' failure expect_exit 1 post-readiness-crash "$TMP/crash.json" >/dev/null # The exemption is signature-scoped, not step-scoped. write_fixture "$TMP/wrong-error.json" success failure 0 'connection refused' success expect_exit 1 other-postgres-error "$TMP/wrong-error.json" >/dev/null write_fixture "$TMP/wrong-pod.json" success failure 0 'pods "other-ci-postgres" not found' success expect_exit 1 wrong-pod-signature "$TMP/wrong-pod.json" >/dev/null write_fixture "$TMP/nonzero-artifact.json" success failure 137 "$artifact" success expect_exit 1 nonzero-with-artifact-text "$TMP/nonzero-artifact.json" >/dev/null # Exact artifact cannot mask any independent failure or non-success pipeline. write_fixture "$TMP/artifact-plus-failure.json" failure failure 0 "$artifact" failure expect_exit 1 artifact-plus-real-failure "$TMP/artifact-plus-failure.json" >/dev/null write_fixture "$TMP/skipped.json" success success 0 '' skipped expect_exit 1 skipped-step "$TMP/skipped.json" >/dev/null printf 'terminal-green contract harness: PASS (9 cases)\n'