testsAndMisc/linux_configuration/tests/test_usage_monitoring_installer_efficiency.sh
Krzysztof kuhy Rudnicki 26965bad32 Split install_usage_monitoring.sh into an entry plus five libs
The 290-line installer was 40 over the 250-line cap. Its five banner-marked
steps move into a sibling lib/, leaving a 53-line entry that owns log()/die(),
the globals that cross between steps, and the call order.

Proven by execution, not inspection: meta/scripts/trace_shell_split.sh
--prefix was captured at HEAD in place (tracing from a detached worktree
changes REPO_DIR, which is interpolated into a generated file, and fakes a
diff), then re-run after the split. The traces are identical -- same exit
status, same ordered stubbed calls, and all five generated-file hashes
unchanged, including usage-report-catchup.sh at sha=9f4803ab157c5527.

REPO_DIR is computed once in the entry and read as a global rather than
recomputed inside a lib one directory deeper; recomputing it would have
pointed the generated catch-up script at the wrong repo while still exiting 0.

test_usage_monitoring_installer_efficiency.sh greps the installer's source
text for the pmon heredoc, so it follows that heredoc to lib/nvidia_pmon.sh.
Its extraction anchor also had to become whitespace-insensitive: the
pre-commit hook runs `shfmt -w` on staged shell files and rewrites
`cat > "x" << 'SCRIPT'` to `cat >"x" <<'SCRIPT'`, which silently broke
extraction. All five property assertions are unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 07:26:56 +02:00

59 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# Regression tests for nvidia-pmon logger installer template efficiency.
set -euo pipefail
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
REPO_DIR=$(cd -- "$SCRIPT_DIR/.." && pwd)
INSTALLER="$REPO_DIR/scripts/periodic_background/system-maintenance/bin/lib/nvidia_pmon.sh"
fail() {
printf 'FAIL: %s\n' "$1" >&2
exit 1
}
logger_template=$(
awk '
/cat >[[:space:]]*"\$HOME\/.local\/bin\/nvidia-pmon-logger\.sh"[[:space:]]*<<[[:space:]]*'\''SCRIPT'\''/ {capture=1; next}
capture && /^SCRIPT$/ {capture=0; exit}
capture {print}
' "$INSTALLER"
)
[[ -n $logger_template ]] || fail 'could not extract nvidia-pmon-logger template from installer'
printf 'Checking pmon logger template avoids read -t busy-loop pattern...\n'
! grep -q 'read -r -t' <<<"$logger_template" ||
fail 'logger template must not use read -t as sleep surrogate'
# The template used to poll with `sleep 60` until the day changed. It now
# sleeps exactly once, until the next midnight, and blocks on `wait` for the
# pmon child — strictly fewer wakeups. Assert that property rather than the
# old literal, so an accidental return to polling still fails the test.
printf 'Checking pmon logger template sleeps until rollover rather than polling...\n'
# Literal search pattern: the $ must reach grep as text, not be expanded
# by this shell.
# shellcheck disable=SC2016
grep -q 'sleep "\$(seconds_until_next_day)"' <<<"$logger_template" ||
fail 'logger template must sleep until the next day boundary'
grep -q 'seconds_until_next_day()' <<<"$logger_template" ||
fail 'logger template must define seconds_until_next_day'
printf 'Checking pmon logger template blocks on wait instead of spinning...\n'
# Literal search pattern: the $ must reach grep as text, not be expanded
# by this shell.
# shellcheck disable=SC2016
grep -qE '^\s*wait "\$pmon_pid"' <<<"$logger_template" ||
fail 'logger template must block on wait for the pmon child'
printf 'Checking pmon logger template uses fork-free date builtin...\n'
grep -q "printf '%(%Y%m%d)T' -1" <<<"$logger_template" ||
fail 'logger template must use bash printf time builtin for current day'
printf 'Checking pmon logger template avoids external date command...\n'
! grep -q 'date +%Y%m%d' <<<"$logger_template" ||
fail 'logger template must not call external date command in hot path'
printf 'Usage monitoring installer efficiency tests passed.\n'