Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/spark-supervisor/blob/main/tests/unit.sh # spark-supervisor Process supervisor and health monitor for sovereign daemons Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `spark-supervisor / tests / unit.sh` 52 lines ยท 3367 bytes Raw Blame History 1 #!/bin/sh 2 # unit.sh: the one boot unit, tools/aien-supervise.service, checked without installing it. 3 # systemd-analyze verifies the file (read-only oracle). Then the ExecStart and ExecStop shell bodies are 4 # taken from the file itself (systemd's %h replaced by a scratch home, $$ by $) and run against that home: 5 # the daemon starts with a cgroup root derived from /proc/self/cgroup, ExecStop stops it and returns only 6 # after it has exited, and ExecStop with no daemon returns at once and leaves no request behind. 7 # sh tests/unit.sh prints one line per case and UNIT PASS|FAIL cases=N fail=N 8 set -u 9 here=$(cd "$(dirname "$0")/.." && pwd) 10 U=$here/tools/aien-supervise.service 11 B=$here/out/aien-supervise 12 [ -x "$B" ] || { echo "unit.sh: build first (tools/build.sh)" >&2; exit 2; } 13 T=$(mktemp -d /tmp/aien-supervise-unit.XXXXXX) 14 mkdir -p "$T/.local/bin" "$T/.config/aien-supervise/services" 15 cp "$B" "$T/.local/bin/aien-supervise" 16 pass=0; fail=0 17 ok() { pass=$((pass+1)); echo "PASS $*"; } 18 bad() { fail=$((fail+1)); echo "FAIL $*"; } 19 body() { sed -n "s/^$1=\/bin\/sh -c '\(.*\)'\$/\1/p" "$U" | sed -e "s#%h#$T#g" -e 's/\$\$/$/g'; } 20 st() { HOME=$T "$T/.local/bin/aien-supervise" status | head -1; } 21 cleanup() { HOME=$T "$T/.local/bin/aien-supervise" shutdown >/dev/null 2>&1; sleep 2; rm -rf "$T"; } 22 trap cleanup EXIT 23 24 systemd-analyze --user verify "$U" >"$T/verify.out" 2>&1 && ok "systemd-analyze verify" || bad "verify: $(cat "$T/verify.out")" 25 start=$(body ExecStart); stop=$(body ExecStop) 26 [ -n "$start" ] && [ -n "$stop" ] && ok "ExecStart and ExecStop are shell bodies" || bad "could not read ExecStart/ExecStop" 27 28 printf 'exec /usr/bin/sleep 1000\n' > "$T/.config/aien-supervise/services/sleeper.svc" 29 timeout 10 /bin/sh -c "$start" >/dev/null 2>"$T/nostate.err"; rc=$? 30 [ $rc = 1 ] && grep -q 'cannot write daemon.pid' "$T/nostate.err" && ok "no state directory: daemon refuses loudly (exit 1)" || bad "no state directory: rc=$rc $(cat "$T/nostate.err")" 31 mkdir -p "$T/.local/state" 32 /bin/sh -c "$start" & 33 main=$! 34 sleep 4 35 st | grep -q '^daemon running' && ok "ExecStart starts the daemon" || bad "after ExecStart: $(st)" 36 want=/sys/fs/cgroup$(sed -n 's/^0:://p' /proc/self/cgroup) 37 got=$(tr '\0' '\n' < "/proc/$main/environ" 2>/dev/null | sed -n 's/^AIEN_SUPERVISE_CGROUP=//p') 38 [ "$got" = "$want" ] && ok "cgroup root derived from /proc/self/cgroup" || bad "cgroup root '$got', want '$want'" 39 40 svc=$(HOME=$T "$T/.local/bin/aien-supervise" status sleeper | tail -1 | sed -n 's/.* pid=\([0-9]*\) .*/\1/p') 41 t0=$(date +%s) 42 MAINPID=$main timeout 30 /bin/sh -c "$stop"; rc=$?; t1=$(date +%s) 43 ! kill -0 "$main" 2>/dev/null && [ $rc = 0 ] && ok "ExecStop returns after the daemon exited ($((t1-t0)) s)" || bad "ExecStop rc=$rc, daemon alive=$(kill -0 "$main" 2>/dev/null && echo yes || echo no)" 44 [ -n "$svc" ] && [ "$svc" != 0 ] && ! kill -0 "$svc" 2>/dev/null && ok "ExecStop stopped the service (pid $svc)" || bad "service pid '$svc' still running or unknown" 45 46 t0=$(date +%s) 47 MAINPID= timeout 10 /bin/sh -c "$stop"; rc=$?; t1=$(date +%s) 48 [ $rc = 0 ] && [ $((t1-t0)) -lt 3 ] && [ -z "$(ls -A "$T/.local/state/aien-supervise/ctl" 2>/dev/null)" ] && ok "ExecStop with no daemon: exits 0 at once, no request left" || bad "ExecStop no daemon: rc=$rc $((t1-t0)) s" 49 50 if [ $fail = 0 ]; then v=PASS; else v=FAIL; fi 51 echo "UNIT $v cases=$((pass+fail)) fail=$fail" 52 [ $fail = 0 ]