spark-supervisor
Process supervisor and health monitor for sovereign daemons
Brought by drakestapleton
spark-supervisor / tests / limits.sh
| 1 | #!/bin/sh |
| 2 | # Real cgroup-v2 exercise. No systemd command or unit; no installed supervisor is contacted. |
| 3 | # The existing tmux server must already be inside the user's delegated domain. This is a |
| 4 | # bootstrap route only: it does not grant delegation or remove the distribution PID-1 dependency. |
| 5 | set -eu |
| 6 | here=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) |
| 7 | B=${SUPERVISE_BIN:-$here/out/aien-supervise} |
| 8 | OMC=${OMC:?set OMC to build the resource child} |
| 9 | T=${SUPERVISE_TEST_DIR:-$(mktemp -d /tmp/aien-limits.XXXXXX)} |
| 10 | mkdir -p "$T/user" "$T/root" "$T/state" |
| 11 | export AIEN_SUPERVISE_STATE=$T/state AIEN_SUPERVISE_USERDIR=$T/user AIEN_SUPERVISE_ROOTDIR=$T/root |
| 12 | uid=$(id -u) |
| 13 | D=${AIEN_TEST_DELEGATE:-/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service} |
| 14 | CG=$D/aien-test-$(basename "$T") |
| 15 | S=aien-test-$(basename "$T") |
| 16 | cleanup() { |
| 17 | "$B" shutdown >/dev/null 2>&1 || true |
| 18 | [ ! -f "$T/state/daemon.pid" ] || kill "$(cut -d' ' -f1 "$T/state/daemon.pid")" 2>/dev/null || true |
| 19 | [ ! -e "$CG/cgroup.kill" ] || printf 1 > "$CG/cgroup.kill" 2>/dev/null || true |
| 20 | find "$CG" -depth -mindepth 1 -type d -exec rmdir '{}' \; 2>/dev/null || true |
| 21 | rmdir "$CG" 2>/dev/null || true |
| 22 | } |
| 23 | trap cleanup EXIT INT TERM |
| 24 | if ! command -v tmux >/dev/null || [ ! -w "$D/cgroup.procs" ]; then |
| 25 | echo 'LIMITS NOT_RUN: writable user delegation and a delegated tmux bootstrap are required'; exit 3 |
| 26 | fi |
| 27 | server=$(tmux display-message -p '#{pid}') |
| 28 | placement=$(cat "/proc/$server/cgroup") |
| 29 | case $placement in *"/user@$uid.service/"*) ;; *) echo 'LIMITS NOT_RUN: tmux is outside delegated subtree'; exit 3;; esac |
| 30 | mkdir "$CG" |
| 31 | printf 536870912 > "$CG/memory.max" |
| 32 | printf 0 > "$CG/memory.swap.max" |
| 33 | printf 64 > "$CG/pids.max" |
| 34 | "$OMC" < "$here/tests/load.om" > "$T/load.new" |
| 35 | chmod +x "$T/load.new" |
| 36 | mv "$T/load.new" "$T/load" |
| 37 | printf 'exec /usr/bin/sleep 30\nrestart never\nmemory-max 67108864\ntasks-max 8\ncpu-weight 321\n' > "$T/user/base.svc" |
| 38 | printf 'exec /usr/bin/sleep 30\nrestart never\nafter base\n' > "$T/user/dependent.svc" |
| 39 | printf 'exec %s/load memory 134217728\nrestart never\nmemory-max 67108864\ntasks-max 8\n' "$T" > "$T/user/oom.svc" |
| 40 | printf 'echo crash-output; exit 3\n' > "$T/crash.sh" |
| 41 | printf 'exec /usr/bin/sh %s/crash.sh\nrestart on-failure\nbackoff 1 2\n' "$T" > "$T/user/crash.svc" |
| 42 | printf 'i=0; while [ "$i" -lt 500 ]; do echo "log-$i-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; i=$((i+1)); done; exec /usr/bin/sleep 30\n' > "$T/log.sh" |
| 43 | printf 'exec /usr/bin/sh %s/log.sh\nrestart never\nlog-max 1000\nlog-keep 2\n' "$T" > "$T/user/log.svc" |
| 44 | # New process, new session and private config only. The inherited address-space ceiling also |
| 45 | # contains a broken limit-code mutant before its cgroup operation can run. |
| 46 | printf '#!/bin/sh\nulimit -v 524288\nexport AIEN_SUPERVISE_STATE="%s/state" AIEN_SUPERVISE_USERDIR="%s/user" AIEN_SUPERVISE_ROOTDIR="%s/root" AIEN_SUPERVISE_CGROUP="%s"\nexec "%s" run >"%s/daemon.log" 2>&1\n' "$T" "$T" "$T" "$CG" "$B" "$T" > "$T/launch" |
| 47 | # The sync channel is an event, not a clock-based readiness guess. |
| 48 | tmux new-session -d -s "$S" "sh '$T/launch'" |
| 49 | # Broker client gets connection readiness through the test driver's bounded event wait below. |
| 50 | # inotifywait is a test oracle only; the production path does not invoke it. |
| 51 | python3 "$here/tests/wait.py" "$T/state/limited.sock" exists |
| 52 | set +e |
| 53 | "$B" run-limited --memory 67108864 --pids 8 -- "$T/load" memory 33554432 > "$T/half.out" 2> "$T/half.err" |
| 54 | half=$? |
| 55 | "$B" run-limited --memory 67108864 --pids 8 -- "$T/load" memory 134217728 > "$T/over.out" 2> "$T/over.err" |
| 56 | over=$? |
| 57 | "$B" run-limited --memory 67108864 --pids 8 -- "$T/load" fork > "$T/fork.out" 2> "$T/fork.err" |
| 58 | forks=$? |
| 59 | printf 'pipe-input\n' | "$B" run-limited --memory 67108864 --pids 8 -- /usr/bin/cat > "$T/pipe.out" 2> "$T/pipe.err" |
| 60 | pipe=$? |
| 61 | "$B" run-limited --memory 67108864 --pids 8 -- /usr/bin/sh -c 'printf err >&2; exit 7' > "$T/exit.out" 2> "$T/exit.err" |
| 62 | status=$? |
| 63 | set -e |
| 64 | printf 'half=%s over=%s forks=%s pipe=%s exit=%s\n' "$half" "$over" "$forks" "$pipe" "$status" |
| 65 | fail() { echo "LIMITS FAIL $*"; exit 1; } |
| 66 | # set -e ignores failures before && in a list, so every assertion fails explicitly. |
| 67 | { [ "$half" = 0 ] && grep -qx MEMORY_SURVIVED "$T/half.out"; } || fail half-ceiling |
| 68 | { [ "$over" = 137 ] && grep -qx LIMIT_OOM_KILLED "$T/over.err"; } || fail over-ceiling |
| 69 | { [ "$forks" = 0 ] && grep -qx FORK_HELD "$T/fork.out"; } || fail pids-ceiling |
| 70 | { [ "$pipe" = 0 ] && grep -qx pipe-input "$T/pipe.out"; } || fail pipe |
| 71 | { [ "$status" = 7 ] && [ "$(cat "$T/exit.err")" = err ]; } || fail exit-status |
| 72 | python3 "$here/tests/wait.py" "$T/state/st/oom" contains LIMIT_OOM_KILLED |
| 73 | python3 "$here/tests/wait.py" "$T/daemon.log" contains 'crash backoff next_in_s=2' |
| 74 | [ "$(cat "$CG/svc-base/memory.max")" = 67108864 ] || fail service-memory |
| 75 | [ "$(cat "$CG/svc-base/memory.swap.max")" = 0 ] || fail service-swap |
| 76 | [ "$(cat "$CG/svc-base/pids.max")" = 8 ] || fail service-pids |
| 77 | [ "$(cat "$CG/svc-base/cpu.weight")" = 321 ] || fail service-cpu |
| 78 | [ -f "$T/state/log/log.log.2" ] || fail rotation |
| 79 | "$B" status > "$T/status.out" |
| 80 | "$B" shutdown |
| 81 | # Wait for the daemon to unlink its own pid file. State deletion is the observed event. |
| 82 | python3 "$here/tests/wait.py" "$T/state/daemon.pid" gone |
| 83 | # After shutdown, so the stop events written to the service log are counted too. |
| 84 | for f in "$T"/state/log/log.log*; do [ "$(wc -c < "$f")" -le 1000 ] || fail "rotation-bound $f"; done |
| 85 | sha256sum "$T"/*.out "$T"/*.err "$T/daemon.log" |
| 86 | cat "$T/status.out" "$T/daemon.log" |
| 87 | echo "LIMITS PASS cases=10 dir=$T" |