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 / limits.sh

87 lines · 5450 bytes Raw Blame History
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.
5set -eu
6here=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
7B=${SUPERVISE_BIN:-$here/out/aien-supervise}
8OMC=${OMC:?set OMC to build the resource child}
9T=${SUPERVISE_TEST_DIR:-$(mktemp -d /tmp/aien-limits.XXXXXX)}
10mkdir -p "$T/user" "$T/root" "$T/state"
11export AIEN_SUPERVISE_STATE=$T/state AIEN_SUPERVISE_USERDIR=$T/user AIEN_SUPERVISE_ROOTDIR=$T/root
12uid=$(id -u)
13D=${AIEN_TEST_DELEGATE:-/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service}
14CG=$D/aien-test-$(basename "$T")
15S=aien-test-$(basename "$T")
16cleanup() {
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}
23trap cleanup EXIT INT TERM
24if ! 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
26fi
27server=$(tmux display-message -p '#{pid}')
28placement=$(cat "/proc/$server/cgroup")
29case $placement in *"/user@$uid.service/"*) ;; *) echo 'LIMITS NOT_RUN: tmux is outside delegated subtree'; exit 3;; esac
30mkdir "$CG"
31printf 536870912 > "$CG/memory.max"
32printf 0 > "$CG/memory.swap.max"
33printf 64 > "$CG/pids.max"
34"$OMC" < "$here/tests/load.om" > "$T/load.new"
35chmod +x "$T/load.new"
36mv "$T/load.new" "$T/load"
37printf 'exec /usr/bin/sleep 30\nrestart never\nmemory-max 67108864\ntasks-max 8\ncpu-weight 321\n' > "$T/user/base.svc"
38printf 'exec /usr/bin/sleep 30\nrestart never\nafter base\n' > "$T/user/dependent.svc"
39printf 'exec %s/load memory 134217728\nrestart never\nmemory-max 67108864\ntasks-max 8\n' "$T" > "$T/user/oom.svc"
40printf 'echo crash-output; exit 3\n' > "$T/crash.sh"
41printf 'exec /usr/bin/sh %s/crash.sh\nrestart on-failure\nbackoff 1 2\n' "$T" > "$T/user/crash.svc"
42printf 'i=0; while [ "$i" -lt 500 ]; do echo "log-$i-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; i=$((i+1)); done; exec /usr/bin/sleep 30\n' > "$T/log.sh"
43printf '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.
46printf '#!/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.
48tmux 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.
51python3 "$here/tests/wait.py" "$T/state/limited.sock" exists
52set +e
53"$B" run-limited --memory 67108864 --pids 8 -- "$T/load" memory 33554432 > "$T/half.out" 2> "$T/half.err"
54half=$?
55"$B" run-limited --memory 67108864 --pids 8 -- "$T/load" memory 134217728 > "$T/over.out" 2> "$T/over.err"
56over=$?
57"$B" run-limited --memory 67108864 --pids 8 -- "$T/load" fork > "$T/fork.out" 2> "$T/fork.err"
58forks=$?
59printf 'pipe-input\n' | "$B" run-limited --memory 67108864 --pids 8 -- /usr/bin/cat > "$T/pipe.out" 2> "$T/pipe.err"
60pipe=$?
61"$B" run-limited --memory 67108864 --pids 8 -- /usr/bin/sh -c 'printf err >&2; exit 7' > "$T/exit.out" 2> "$T/exit.err"
62status=$?
63set -e
64printf 'half=%s over=%s forks=%s pipe=%s exit=%s\n' "$half" "$over" "$forks" "$pipe" "$status"
65fail() { 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
72python3 "$here/tests/wait.py" "$T/state/st/oom" contains LIMIT_OOM_KILLED
73python3 "$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.
82python3 "$here/tests/wait.py" "$T/state/daemon.pid" gone
83# After shutdown, so the stop events written to the service log are counted too.
84for f in "$T"/state/log/log.log*; do [ "$(wc -c < "$f")" -le 1000 ] || fail "rotation-bound $f"; done
85sha256sum "$T"/*.out "$T"/*.err "$T/daemon.log"
86cat "$T/status.out" "$T/daemon.log"
87echo "LIMITS PASS cases=10 dir=$T"