121 lines
4.5 KiB
Shell
121 lines
4.5 KiB
Shell
# Cases for ./agent-memory-cap.nix. Each case writes a cap file (or none), runs
|
|
# both generators and the boot check with a clean environment and compares what
|
|
# they produced, then sources the login-shell snippet under each POSIX shell
|
|
# and compares what a child process sees.
|
|
set -uo pipefail
|
|
|
|
failures=0
|
|
count=0
|
|
|
|
fail() {
|
|
echo "FAILED: $case: $*" >&2
|
|
failures=$((failures + 1))
|
|
}
|
|
|
|
dir=$(mktemp -d)
|
|
cap=$dir/memory-max-bytes
|
|
sed "s|/run/hive/memory-max-bytes|$cap|g" "$ENV_GENERATOR" >"$dir/env-generator"
|
|
sed "s|/run/hive/memory-max-bytes|$cap|g" "$UNIT_GENERATOR" >"$dir/unit-generator"
|
|
sed "s|/run/hive/memory-max-bytes|$cap|g" "$CHECK" >"$dir/check"
|
|
chmod +x "$dir/env-generator" "$dir/unit-generator" "$dir/check"
|
|
sed "s|$ENV_GENERATOR|$dir/env-generator|g" "$PROFILE" >"$dir/profile"
|
|
envbin=$(command -v env)
|
|
|
|
case="setup"
|
|
for g in env-generator unit-generator check; do
|
|
if grep -q '/run/hive/memory-max-bytes' "$dir/$g"; then fail "$g still names /run/hive after the rewrite"; fi
|
|
if ! grep -qF "$cap" "$dir/$g"; then fail "the rewrite did not reach $g's read"; fi
|
|
done
|
|
if ! grep -qF "$dir/env-generator" "$dir/profile"; then fail "the snippet does not call the rewritten generator"; fi
|
|
if ! printf '%s\n' "$EXTRA_INIT" | grep -qxF '. /etc/hyperhive/memory-cap.sh'; then
|
|
fail "/etc/set-environment does not source the snippet"
|
|
fi
|
|
|
|
# check <description> <expected BUN_JSC_forceRAMSize or ""> <expected MemoryHigh or "">
|
|
# <why the boot check warns, or "">
|
|
check() {
|
|
case=$1
|
|
count=$((count + 1))
|
|
local want_env=${2:+BUN_JSC_forceRAMSize=$2} want_high=$3 want_why=${4:-} got
|
|
got=$(env -i "$dir/env-generator")
|
|
if [ "$got" != "$want_env" ]; then
|
|
fail "environment generator printed:"$'\n'"$got"$'\n'"wanted:"$'\n'"$want_env"
|
|
fi
|
|
|
|
local out=$dir/generator-out
|
|
rm -rf "$out"
|
|
mkdir -p "$out/normal" "$out/early" "$out/late"
|
|
env -i "$dir/unit-generator" "$out/normal" "$out/early" "$out/late"
|
|
if [ -z "$want_high" ]; then
|
|
got=$(find "$out" -type f)
|
|
if [ -n "$got" ]; then fail "unit generator wrote:"$'\n'"$got"; fi
|
|
else
|
|
# The subagent daemon and the bash daemon each get one, and nothing else is written.
|
|
local unit
|
|
for unit in hive-subagent-daemon hive-bash-daemon; do
|
|
got=$(cat "$out/normal/$unit.service.d/50-memory-high.conf" 2>&1)
|
|
if [ "$got" != "[Service]"$'\n'"MemoryHigh=$want_high" ]; then
|
|
fail "unit generator's drop-in for $unit:"$'\n'"$got"
|
|
fi
|
|
done
|
|
got=$(find "$out" -type f | wc -l)
|
|
if [ "$got" -ne 2 ]; then fail "unit generator wrote $got files, wanted 2"; fi
|
|
fi
|
|
|
|
# The warning goes to stderr with the journal's `<4>` priority prefix, and
|
|
# nothing goes anywhere for a byte count or `infinity`.
|
|
got=$(env -i "$dir/check" 2>&1)
|
|
local want_warn=
|
|
if [ -n "$want_why" ]; then
|
|
want_warn="<4>$cap $want_why: no BUN_JSC_forceRAMSize heap ceiling and no MemoryHigh= on hive-subagent-daemon or hive-bash-daemon; the host's MemoryMax= on the container still applies."
|
|
fi
|
|
if [ "$got" != "$want_warn" ]; then
|
|
fail "boot check printed:"$'\n'"$got"$'\n'"wanted:"$'\n'"$want_warn"
|
|
fi
|
|
|
|
local sh
|
|
for sh in $SHELLS; do
|
|
# Only what the snippet exported reaches the child's environment. No PATH:
|
|
# the snippet must not need one.
|
|
got=$(env -i "$sh" -c ". '$dir/profile'; exec '$envbin'" | grep -E '^(BUN_|LD_PRELOAD=|__hyperhive)')
|
|
if [ "$got" != "$want_env" ]; then
|
|
fail "$sh: a child of the sourcing shell saw:"$'\n'"$got"
|
|
fi
|
|
done
|
|
}
|
|
|
|
printf '8589934592\n' >"$cap"
|
|
check "an 8G cap" 6442450944 5726623061
|
|
|
|
printf '4294967296' >"$cap"
|
|
check "a line without a newline is kept" 3221225472 2863311530
|
|
|
|
printf '4294967296\nLD_PRELOAD=/x\n' >"$cap"
|
|
check "only the first line is read" 3221225472 2863311530
|
|
|
|
printf 'infinity\n' >"$cap"
|
|
check "infinity sets nothing and warns of nothing" "" "" ""
|
|
|
|
printf 'infinity' >"$cap"
|
|
check "infinity without a newline" "" "" ""
|
|
|
|
printf 'infinityx\n' >"$cap"
|
|
check "a word that only starts with infinity" "" "" "holds neither a byte count nor infinity"
|
|
|
|
printf '8G\n' >"$cap"
|
|
check "a size with a suffix is ignored" "" "" "holds neither a byte count nor infinity"
|
|
|
|
printf '0123\n' >"$cap"
|
|
check "a leading zero is ignored" "" "" "holds neither a byte count nor infinity"
|
|
|
|
printf '1234567890123456\n' >"$cap"
|
|
check "sixteen digits are ignored" "" "" "holds neither a byte count nor infinity"
|
|
|
|
: >"$cap"
|
|
check "an empty file" "" "" "holds neither a byte count nor infinity"
|
|
|
|
rm -f "$cap"
|
|
check "no file" "" "" "is missing"
|
|
|
|
echo "$count cases, $failures failed"
|
|
[ "$failures" -eq 0 ]
|