interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

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
8set -u
9here=$(cd "$(dirname "$0")/.." && pwd)
10U=$here/tools/aien-supervise.service
11B=$here/out/aien-supervise
12[ -x "$B" ] || { echo "unit.sh: build first (tools/build.sh)" >&2; exit 2; }
13T=$(mktemp -d /tmp/aien-supervise-unit.XXXXXX)
14mkdir -p "$T/.local/bin" "$T/.config/aien-supervise/services"
15cp "$B" "$T/.local/bin/aien-supervise"
16pass=0; fail=0
17ok() { pass=$((pass+1)); echo "PASS $*"; }
18bad() { fail=$((fail+1)); echo "FAIL $*"; }
19body() { sed -n "s/^$1=\/bin\/sh -c '\(.*\)'\$/\1/p" "$U" | sed -e "s#%h#$T#g" -e 's/\$\$/$/g'; }
20st() { HOME=$T "$T/.local/bin/aien-supervise" status | head -1; }
21cleanup() { HOME=$T "$T/.local/bin/aien-supervise" shutdown >/dev/null 2>&1; sleep 2; rm -rf "$T"; }
22trap cleanup EXIT
23
24systemd-analyze --user verify "$U" >"$T/verify.out" 2>&1 && ok "systemd-analyze verify" || bad "verify: $(cat "$T/verify.out")"
25start=$(body ExecStart); stop=$(body ExecStop)
26[ -n "$start" ] && [ -n "$stop" ] && ok "ExecStart and ExecStop are shell bodies" || bad "could not read ExecStart/ExecStop"
27
28printf 'exec /usr/bin/sleep 1000\n' > "$T/.config/aien-supervise/services/sleeper.svc"
29timeout 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")"
31mkdir -p "$T/.local/state"
32/bin/sh -c "$start" &
33main=$!
34sleep 4
35st | grep -q '^daemon running' && ok "ExecStart starts the daemon" || bad "after ExecStart: $(st)"
36want=/sys/fs/cgroup$(sed -n 's/^0:://p' /proc/self/cgroup)
37got=$(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
40svc=$(HOME=$T "$T/.local/bin/aien-supervise" status sleeper | tail -1 | sed -n 's/.* pid=\([0-9]*\) .*/\1/p')
41t0=$(date +%s)
42MAINPID=$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
46t0=$(date +%s)
47MAINPID= 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
50if [ $fail = 0 ]; then v=PASS; else v=FAIL; fi
51echo "UNIT $v cases=$((pass+fail)) fail=$fail"
52[ $fail = 0 ]