interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / ops / test-store-post-update.sh

77 lines · 4726 bytes Raw Blame History
1#!/usr/bin/env bash
2# test-store-post-update.sh: the store hook in a scratch home (its own ~/.gitconfig, git store, sync,
3# spark-cleanup and disk-alarm stand-ins). Real pushes; nothing outside the scratch folder is read or written.
4# Usage: bash tools/ops/test-store-post-update.sh exit 0 all pass, 1 any fail.
5set -u
6here=$(cd "$(dirname "$0")" && pwd)
7T=$(mktemp -d "${TMPDIR:-/tmp}/test-store-hook.XXXXXX")
8trap 'rm -rf "$T"' EXIT
9fails=0
10ok() { echo "PASS $1"; }
11bad() { echo "FAIL $1"; fails=$((fails + 1)); }
12
13H="$T/home"
14mkdir -p "$H/.local/bin" "$H/.local/lib/store-hooks" "$H/.local/lib/bazinga-git" "$H/.config/git" "$H/git/nest" "$H/elsewhere"
15cp "$here/store-post-update" "$H/.local/lib/store-hooks/post-update"
16# the install, as README "Store hook" gives it
17printf '[includeIf "gitdir:~/git/"]\n\tpath = .config/git/store.inc\n[user]\n\tname = t\n\temail = t@t\n[init]\n\tdefaultBranch = main\n' > "$H/.gitconfig"
18printf '[core]\n\thooksPath = %s/.local/lib/store-hooks\n' "$H" > "$H/.config/git/store.inc"
19# stand-ins: each records its arguments, every GIT_ variable it can see, and its working folder
20for f in spark-cleanup disk-alarm; do
21 printf '#!/bin/sh\n[ -f "%s/%s.fail" ] && exit 9\nprintf "%%s|%%s|%%s\\n" "$*" "$(env | grep -c "^GIT_")" "$(pwd)" >> "%s/%s.calls"\n' \
22 "$T" "$f" "$T" "$f" > "$H/.local/bin/$f"
23done
24printf '#!/bin/sh\n[ -f "%s/sync.fail" ] && exit 9\necho "$1" >> "%s/sync.calls"\n' "$T" "$T" > "$H/.local/lib/bazinga-git/sync.sh"
25chmod +x "$H/.local/bin/"* "$H/.local/lib/bazinga-git/sync.sh" "$H/.local/lib/store-hooks/post-update"
26
27export HOME="$H" GIT_CONFIG_NOSYSTEM=1 GIT_TERMINAL_PROMPT=0
28unset XDG_CONFIG_HOME GIT_DIR GIT_WORK_TREE
29git init -q --bare "$H/git/alpha.git"
30git init -q --bare "$H/git/nest/beta.git"
31git init -q --bare "$H/elsewhere/gamma.git"
32git init -q "$T/w"
33git -C "$T/w" commit -q --allow-empty -m one
34n=0
35push() { n=$((n + 1)); git -C "$T/w" commit -q --allow-empty -m "c$n"; git -C "$T/w" push -q "$1" HEAD:main 2> "$T/push.err"; }
36# the detached steps finish within a moment; wait for the expected count of lines, at most 10 s
37lines() { local i=0; while [ "$i" -lt 100 ]; do [ "$(cat "$T/$1.calls" 2>/dev/null | wc -l)" -ge "$2" ] && break; sleep 0.1; i=$((i + 1)); done
38 cat "$T/$1.calls" 2>/dev/null | wc -l; }
39
40# 1. a landing on a store repository: sync, spark-cleanup --landed NAME, then disk-alarm --quiet; no GIT_ variable leaks
41push "$H/git/alpha.git"; r=$?
42[ "$r" = 0 ] && [ "$(lines spark-cleanup 1)" = 1 ] && [ "$(lines disk-alarm 1)" = 1 ] &&
43 grep -qx 'alpha' "$T/sync.calls" && grep -qx -- '--landed alpha|0|/' "$T/spark-cleanup.calls" &&
44 grep -qx -- '--quiet|0|/' "$T/disk-alarm.calls" && ok "landing runs sync, spark-cleanup --landed, disk-alarm" ||
45 bad "landing (push $r): $(cat "$T"/*.calls 2>/dev/null | tr '\n' ' ')"
46
47# 2. a repository in a nested store folder, created after the install, gets the hook with no step of its own
48push "$H/git/nest/beta.git"
49[ "$(lines spark-cleanup 2)" = 2 ] && grep -qx -- '--landed beta|0|/' "$T/spark-cleanup.calls" &&
50 ok "nested store repository" || bad "nested store repository: $(cat "$T/spark-cleanup.calls" | tr '\n' ' ')"
51
52# 3. negative control: a bare repository outside the store runs none of it
53push "$H/elsewhere/gamma.git"; sleep 1
54[ "$(lines spark-cleanup 2)" = 2 ] && ! grep -qx gamma "$T/sync.calls" && ok "repository outside the store: no hook" ||
55 bad "repository outside the store ran the hook"
56
57# 4. every step failing still lands the push (exit 0), and the steps after a failed one still run
58touch "$T/sync.fail" "$T/spark-cleanup.fail"
59push "$H/git/alpha.git"; r=$?
60[ "$r" = 0 ] && [ "$(lines disk-alarm 3)" = 3 ] && ok "failing steps never fail the push" || bad "failing steps (push $r, alarms $(lines disk-alarm 3))"
61rm -f "$T/sync.fail" "$T/spark-cleanup.fail"
62
63# 5. the push does not wait for a slow step
64printf '#!/bin/sh\nsleep 5\n' > "$H/.local/bin/spark-cleanup"
65s=$(date +%s); push "$H/git/alpha.git"; e=$(( $(date +%s) - s ))
66[ "$e" -lt 4 ] && ok "push does not wait for the clean-up (${e} s)" || bad "push waited ${e} s"
67
68# 6. mutant: the same hook without its GIT_ variable reset must fail case 1's check
69grep -v '^for v in ' "$here/store-post-update" > "$H/.local/lib/store-hooks/post-update"
70for f in spark-cleanup; do
71 printf '#!/bin/sh\nprintf "%%s|%%s\\n" "$*" "$(env | grep -c "^GIT_")" >> "%s/mut.calls"\n' "$T" > "$H/.local/bin/$f"
72done
73push "$H/git/alpha.git"; lines mut 1 > /dev/null
74grep -qx -- '--landed alpha|0' "$T/mut.calls" && bad "mutant without the GIT_ reset passed" || ok "mutant without the GIT_ reset caught"
75
76[ "$fails" = 0 ] && echo "TEST PASS store-post-update" || echo "TEST FAIL store-post-update fails=$fails"
77[ "$fails" = 0 ]