Files
Linux_post_install/tests/t-systemd-unit.sh
T
Your Name d817c37652
gates / consistency-and-conventions (push) Successful in 26s
fix: stabilization pass — fail-closed auth, ai flag validation, lint/config/security hardening, regression tests
17-point code-level audit executed via Explorer->Architect->Builder->Tester->Reviewer;
Reviewer accepted (APPROVE_WITH_NOTES; 3 block-list items resolved):

- security: telegram sender-owner AND-gate + TELEGRAM_OWNER_ID, matrix
  MATRIX_ROOM_ID fail-closed, gpg --passphrase-fd 3 (no argv secret),
  /dev/tcp positional-arg form (checkport/smb-client/share-lib/NET_PROBE),
  eval deny-by-default + --no-command-execution carried by both chat bridges,
  tty-gated --trust; config/{telegram,matrix}.env reference templates
- ai: all ExecStart flags validated against installed llama.cpp
  (requested->error, default->omit+warn, CONFIG_REQUESTED_FLAGS); single-file
  hf download failure rc=1 + no .hf-meta; LLAMACPP_HOST coherent;
  POS_SUBCMDS + metadata gaps closed
- tooling: lint-conventions Bash-native rewrite (~24-30x faster, rules and
  output byte-identical, :num restored); pos system uninstall covers all 12
  libs + scale-tail + flags dir + systemd user units (|| true) + plugin
  markers; anchored .bash_completion/.bashrc removal replaces sed -i '/pos/d'
- config: canonical load_env_file in lib/config-ui.sh (CRLF strip, env-wins,
  XDG, LOADED_ENV_KEYS); 9 tools migrated; entertainment-lib collapsed to
  wrappers; docker-compose deliberately unmigrated (source semantics)
- tests: first committed regression suite — tests/run-tests.sh zero-dep
  runner + make test; 12 files / 179 checks / 0 skip / ~52s; hard skip
  contract; systemd-analyze verify on generated unit PASS

Verified: make gen idempotent; make check green; make lint 0 FAIL, 0 WARN;
make test green; bash -n clean; git diff --check clean. Audit deliverables +
agent reports + AGENT_TODO Done entry included.
2026-09-06 07:25:44 -04:00

76 lines
3.2 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# t-systemd-unit.sh — generated systemd unit correctness (D5):
# - exactly one ExecStart= line, binary + model paths quoted (spaces safe),
# - each runtime flag emitted exactly once,
# - EnvironmentFile= present with the ai.env path,
# - `systemd-analyze verify` passes on the generated unit (skip if the
# analyzer is unavailable).
# Runs the REAL unit-write path: stub systemctl/curl/llama-server so no
# system service or network is touched.
run_test() {
local sandbox stubs unitdir models
sandbox="$(mksandbox systemd-unit)"
stubs="$sandbox/stubs"
unitdir="$sandbox/userunits"
models="$sandbox/models"
mkdir -p "$stubs" "$unitdir" "$models"
# model path deliberately contains spaces
touch "$models/my model file.gguf"
cat > "$stubs/llama-server" <<'STUB'
#!/usr/bin/env bash
case "$1" in
--version) echo "llama.cpp 1.2.3" ;;
--help)
cat <<'HELP'
usage: llama-server [options]
options:
--host <addr> bind address
--port <port> server port
--n-gpu-layers <n> layers to offload
--ctx-size <n> context size
--threads <n> cpu threads
--mmap memory mapping
HELP
;;
esac
STUB
# systemctl: pretend every operation succeeds silently.
printf '#!/usr/bin/env bash\nexit 0\n' > "$stubs/systemctl"
# curl (health check) returns an "ok" JSON body.
printf '#!/usr/bin/env bash\nprintf "%%s" '"'"'{"status":"ok"}'"'"'\n' > "$stubs/curl"
# nvidia-smi: no GPU → cpu path deterministic.
printf '#!/usr/bin/env bash\nexit 1\n' > "$stubs/nvidia-smi"
chmod +x "$stubs/llama-server" "$stubs/systemctl" "$stubs/curl" "$stubs/nvidia-smi"
local server="$ROOT/bin/pos-ai-server"
local model="$models/my model file.gguf"
# REAL run: writes the unit to USER_SYSTEMD_DIR, waits ~2s for health.
test_run_env PATH="$stubs:/usr/bin:/bin" USER_SYSTEMD_DIR="$unitdir" -- \
timeout 60 "$server" start "$model"
check_rc "real start writes unit and exits 0" 0 "$TR_RC"
check_file_exists "unit file created" "$unitdir/pos-ai-server.service"
local unit="$unitdir/pos-ai-server.service"
local exec_lines unit_exec
exec_lines="$(grep -c '^ExecStart=' "$unit" || true)"
check_eq "exactly one ExecStart= line" 1 "$exec_lines"
unit_exec="$(grep '^ExecStart=' "$unit")"
check_contains "binary path (with spaces) quoted" "\"$stubs/llama-server\"" "$unit_exec"
check_contains "model path (with spaces) double-quoted in unit" "\"$model\"" "$unit_exec"
check_contains "port flag in unit" "--port 8088" "$unit_exec"
check_contains "host flag in unit" "--host 127.0.0.1" "$unit_exec"
check_contains "gpu layers flag in unit" "--n-gpu-layers 0" "$unit_exec"
check_contains "ctx-size flag in unit" "--ctx-size 4096" "$unit_exec"
check_contains "EnvironmentFile ai.env referenced" "EnvironmentFile=-%h/.config/linux_post_install/ai.env" "$(cat "$unit")"
if command -v systemd-analyze >/dev/null 2>&1; then
test_run systemd-analyze verify "$unit"
check_rc "systemd-analyze verify accepts generated unit" 0 "$TR_RC"
else
skip_case "systemd-analyze verify" "systemd-analyze not available"
fi
}