forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

gate-local.sh309 lines, 12.2 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# The LOCAL full gate: the "special path" where this machine proves the suite
3# instead of a GitHub runner.
4#
5# Why it exists: the same suite is 565s here and ~55min on a 4-core runner.
6# When you have run it, commit the `Local-Gate:` trailer this script prints
7# (`--amend` writes it for you) and the push carries proof: the workflow's
8# "Decide test gate" step verifies the trailer against HEAD^{tree} and then
9# skips the runner suite, so CI only packs and deploys.
10#
11# Default path (nothing here): push without a trailer and GitHub runs
12# everything, suite included. Nothing is mandatory; this is the fast lane.
13#
14# What it runs, in order, each fatal on failure:
15# 1. cargo clippy --all-targets -- -D warnings (same lint CI runs)
16# 2. cargo nextest run --no-fail-fast (the whole suite, or
17# the --scope filter; CI's env, and DATABASE_URL unset so the harness
18# picks its own per-test databases)
19# 3. cargo test --test doc_sync (README drift gate)
20#
21# On success it writes .git/local-gate/<tree-sha>.ok (inside .git, so it is
22# never committed) holding the tree, the timestamp, the scope, the exact
23# commands and the counts parsed from nextest's summary line. Re-running for
24# the same tree is a no-op that just re-prints the trailer — the stamp IS the
25# proof, so `--force` is the only way to pay for the suite twice.
26#
27# The trailer is printed only for a FULL run: a --scope run is quick
28# iteration, not a claim about the suite.
29#
30# Prerequisites: cargo-nextest, and a reachable Postgres (the suite mints a
31# control+school database pair per test). Both are checked first and reported
32# with the exact command to fix them.
33#
34# Usage:
35# scripts/gate-local.sh # full gate (reuses a stamp if it exists)
36# scripts/gate-local.sh --force # full gate, even if already stamped
37# scripts/gate-local.sh --scope e2e # quick iteration (no trailer)
38# scripts/gate-local.sh --amend # full gate, then append the
39# # trailer to the HEAD commit
40set -euo pipefail
41
42here=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
43cd "$here"
44
45scope=""
46amend=0
47force=0
48while [ $# -gt 0 ]; do
49 case "$1" in
50 --scope)
51 [ $# -ge 2 ] || { echo "--scope needs a nextest filter (e.g. --scope e2e)" >&2; exit 2; }
52 scope=$2
53 shift 2
54 ;;
55 --amend)
56 amend=1
57 shift
58 ;;
59 --force)
60 force=1
61 shift
62 ;;
63 -h|--help)
64 sed -n '/^# Usage:/,/^set -euo/p' "$0" | sed 's/^# \{0,1\}//'
65 exit 0
66 ;;
67 *)
68 echo "unknown argument: $1 (see --help)" >&2
69 exit 2
70 ;;
71 esac
72done
73
74# ── Record what this run is ─────────────────────────────────────────────────
75tree=$(git rev-parse HEAD^{tree})
76head=$(git rev-parse --short HEAD)
77stamp_dir=$(git rev-parse --git-dir)/local-gate
78mkdir -p "$stamp_dir"
79stamp=$stamp_dir/$tree.ok
80log=$stamp_dir/$tree.log
81
82if [ -n "$scope" ]; then
83 scope_label=$scope
84else
85 scope_label=full
86fi
87
88dirty=no
89if [ -n "$(git status --porcelain)" ]; then
90 dirty=yes
91 cat <<'EOF'
92WARNING: the working tree is dirty. The gate will test what is on disk, but a
93 stamp can only describe a committed tree — so NO stamp and no trailer
94 will be written this run. Commit, then re-run to get one.
95EOF
96fi
97
98# The tail both paths share: print (and optionally amend in) the trailer.
99emit_trailer() {
100 if [ "$scope_label" != full ]; then
101 cat <<EOF
102
103== scoped run ($scope_label): no Local-Gate trailer.
104 The trailer is a claim about the FULL suite, so only an unscoped run
105 produces it. Re-run \`scripts/gate-local.sh\` before pushing if you want
106 GitHub to skip its own suite.
107EOF
108 return 0
109 fi
110 local trailer="Local-Gate: $tree (${passed:-0} passed, ${seconds:-0}s)"
111 if [ "$amend" = 1 ]; then
112 if git branch -r --contains HEAD 2>/dev/null | grep -q .; then
113 echo "REFUSING --amend: HEAD is already on a remote branch; amend would rewrite published history." >&2
114 echo "Add the trailer by hand instead:" >&2
115 echo " $trailer" >&2
116 exit 1
117 fi
118 local message
119 message=$(git log -1 --format=%B)
120 if printf '%s\n' "$message" | grep -q '^Local-Gate: '; then
121 message=$(printf '%s\n' "$message" | sed "s|^Local-Gate: .*|$trailer|")
122 else
123 message=$(printf '%s\n' "$message" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}')
124 message="$message
125$trailer"
126 fi
127 printf '%s\n' "$message" | git commit --amend -q --no-verify -F -
128 echo "== appended to $(git rev-parse --short HEAD) (tree unchanged: $(git rev-parse HEAD^{tree}))"
129 fi
130 cat <<EOF
131
132Add this line to the commit you are about to push:
133
134 $trailer
135
136(\`scripts/gate-local.sh --amend\` appends it to HEAD for you — the tree does
137not change, so an amend cannot invalidate the stamp.) The push then carries a
138verified claim: the workflow's "Decide test gate" step compares the trailer's
139tree with HEAD^{tree} and skips the runner suite only when they match.
140EOF
141}
142
143# Already proven on this exact tree: the stamp IS the evidence, so re-running
144# the suite would only burn ten minutes to arrive at the same line.
145if [ "$force" = 0 ] && [ "$dirty" = no ] && [ -f "$stamp" ]; then
146 scope_label=$(sed -n 's/^scope: //p' "$stamp" | tail -1)
147 passed=$(sed -n 's/^passed: //p' "$stamp" | tail -1)
148 seconds=$(sed -n 's/^duration_seconds: //p' "$stamp" | tail -1)
149 echo "== tree $tree already verified on this machine; reusing $stamp (--force to re-run)"
150 cat "$stamp"
151 emit_trailer
152 exit 0
153fi
154
155# ── Prerequisites ───────────────────────────────────────────────────────────
156# Each says what is missing and the exact command that fixes it: a mysterious
157# failure 90 seconds in is worse than not starting.
158command -v cargo-nextest >/dev/null || {
159 cat >&2 <<'EOF'
160cargo-nextest is not installed — the suite runs under nextest, not `cargo test`.
161 cargo install cargo-nextest --locked
162EOF
163 exit 1
164}
165
166# The compose stack's Postgres first (that is the repo's own container, and
167# the one with max_connections=500 the parallel suite needs), then the CI
168# sidecar's name in case it was started locally, then a plain reachable
169# server on the harness's default address.
170pg_container=""
171pg_tool=""
172for candidate in hezarfen_backend_postgres hezarfen_backend_pg; do
173 for tool in podman docker; do
174 if command -v "$tool" >/dev/null &&
175 "$tool" ps --format '{{.Names}}' 2>/dev/null | grep -qx "$candidate"; then
176 pg_container=$candidate
177 pg_tool=$tool
178 break 2
179 fi
180 done
181done
182
183if [ -n "$pg_container" ]; then
184 "$pg_tool" exec "$pg_container" pg_isready -U hezarfen >/dev/null 2>&1 || {
185 echo "Postgres container $pg_container exists but is not answering yet; wait for it (compose healthcheck) and re-run." >&2
186 exit 1
187 }
188 max_conn=$("$pg_tool" exec "$pg_container" psql -U hezarfen -tAc 'show max_connections' 2>/dev/null || echo '?')
189 echo "== Postgres: container $pg_container ($pg_tool), max_connections=$max_conn"
190 if [ "$max_conn" != '?' ] && [ "$max_conn" -lt 500 ] 2>/dev/null; then
191 echo " WARNING: max_connections=$max_conn; the suite mints roughly a dozen control+school database pairs concurrently and the compose stack raises this to 500. Expect connection errors."
192 fi
193elif (exec 3<>/dev/tcp/127.0.0.1/5432) 2>/dev/null; then
194 echo "== Postgres: server answering on 127.0.0.1:5432 (no repo container; connection ceiling not verifiable)"
195else
196 cat >&2 <<'EOF'
197No Postgres for the test harness: no hezarfen_backend_postgres / hezarfen_backend_pg
198container is running and nothing answers on 127.0.0.1:5432.
199
200The suite mints a control+school database pair per test, so it needs the repo's
201own server. Start it with:
202
203 podman compose up -d postgres
204
205(that is the container named hezarfen_backend_postgres, published on
206127.0.0.1:5432 with max_connections=500 — see compose.yaml).
207EOF
208 exit 1
209fi
210
211: > "$log"
212echo "== tree $tree (HEAD $head), scope: $scope_label, log: $log"
213
214# ── The three steps ─────────────────────────────────────────────────────────
215declare -a commands=()
216
217run_step() {
218 local label=$1
219 shift
220 commands+=("$(printf '%q ' "$@")")
221 echo
222 echo "=== $label"
223 echo "+ $(printf '%q ' "$@")"
224 if "$@" 2>&1 | tee -a "$log"; then
225 echo "--- $label: OK"
226 else
227 local rc=$?
228 echo "--- $label: FAILED (exit $rc) — nothing was stamped; fix it and re-run" >&2
229 exit $rc
230 fi
231}
232
233# CI runs the same lint on its own runner before anything is packaged; a
234# warning that only shows up there costs a full round trip.
235run_step "clippy (same as CI's Clippy job)" \
236 cargo clippy --all-targets -- -D warnings
237
238# CI's env, verbatim: no debug info in the test/dev profiles (the binaries
239# blow past a runner's disk), no incremental artifacts, sqlx offline cache.
240# DATABASE_URL is unset on purpose — the harness mints its own databases and a
241# stray value would point every test at one schema.
242if [ -n "$scope" ]; then
243 run_step "nextest (scope: $scope)" \
244 env -u DATABASE_URL \
245 CARGO_PROFILE_TEST_DEBUG=0 CARGO_PROFILE_DEV_DEBUG=0 CARGO_INCREMENTAL=0 \
246 SQLX_OFFLINE=true \
247 cargo nextest run --no-fail-fast --config-file .config/nextest.toml "$scope"
248else
249 run_step "nextest (full suite)" \
250 env -u DATABASE_URL \
251 CARGO_PROFILE_TEST_DEBUG=0 CARGO_PROFILE_DEV_DEBUG=0 CARGO_INCREMENTAL=0 \
252 SQLX_OFFLINE=true \
253 cargo nextest run --no-fail-fast --config-file .config/nextest.toml
254fi
255
256# The README's ## Endpoints table is generated; this is the drift gate. No
257# UPDATE_DOCS: a run that regenerates what it should be checking is not a gate.
258run_step "doc gate (README drift)" \
259 cargo test --test doc_sync
260
261# ── Parse nextest's summary line ────────────────────────────────────────────
262summary=$(grep -E 'Summary \[.*tests run:' "$log" | tail -1 || true)
263if [ -z "$summary" ]; then
264 echo "no nextest summary line in $log — the counts are unknown, so no stamp is written" >&2
265 exit 1
266fi
267# Every parse is `|| true`: "0 failed" / "0 skipped" simply do not appear in
268# the summary line, and a bare `grep -oE` with no match exits 1 — which, under
269# `set -e` + pipefail, would abort a run that passed.
270duration=$(printf '%s\n' "$summary" | grep -oE '\[ *[0-9][0-9.]*s\]' | grep -oE '[0-9][0-9.]*' | head -1 || true)
271total=$(printf '%s\n' "$summary" | grep -oE '[0-9]+ tests run' | grep -oE '[0-9]+' | head -1 || true)
272passed=$(printf '%s\n' "$summary" | grep -oE '[0-9]+ passed' | grep -oE '[0-9]+' | head -1 || true)
273skipped=$(printf '%s\n' "$summary" | grep -oE '[0-9]+ skipped' | grep -oE '[0-9]+' | head -1 || true)
274failed=$(printf '%s\n' "$summary" | grep -oE '[0-9]+ failed' | grep -oE '[0-9]+' | head -1 || true)
275if [ -z "$total" ] || [ -z "$passed" ]; then
276 echo "could not read the counts out of: $summary" >&2
277 exit 1
278fi
279seconds=${duration%%.*}
280counts="$total tests run, $passed passed, ${failed:-0} failed, ${skipped:-0} skipped"
281
282echo
283echo "== $summary"
284echo "== counts: $counts (${seconds}s)"
285
286if [ "$dirty" = yes ]; then
287 echo "== no stamp written (dirty working tree); commit and re-run"
288 exit 0
289fi
290
291# ── The stamp ───────────────────────────────────────────────────────────────
292{
293 echo "tree: $tree"
294 echo "head: $head"
295 echo "time: $(date -Is)"
296 echo "scope: $scope_label"
297 echo "dirty: $dirty"
298 echo "counts: $counts"
299 echo "passed: ${passed:-0}"
300 echo "duration_seconds: ${seconds:-0}"
301 for command in "${commands[@]}"; do
302 echo "command: ${command% }"
303 done
304} > "$stamp"
305
306echo "== stamp: $stamp"
307cat "$stamp"
308
309emit_trailer