testsAndMisc/linux_configuration/fixes/lib/tests/test_ubuntu_perf_fixes.sh
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

143 lines
6.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# lib/tests/test_ubuntu_perf_fixes.sh — tests for ubuntu_perf_fixes.sh's
# fix_sysctl_tuning and fix_nvidia_persistence.
#
# fix_earlyoom and fix_failed_sssd live in test_ubuntu_perf_fixes_oom.sh,
# split out to hold every file under the 250-line cap.
#
# Calls go through _t_run rather than `out="$(...)"`: command substitution
# forks a subshell, and kcov does not register a lib whose first execution
# happens inside one.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=ubuntu_perf_harness.sh
. "${SCRIPT_DIR}/ubuntu_perf_harness.sh"
# shellcheck source=../ubuntu_perf_fixes.sh
. "${FIXES_DIR}/lib/ubuntu_perf_fixes.sh"
printf '\n-- fix_sysctl_tuning --\n'
# Case 1: the drop-in already exists -> skip without touching anything.
ubuntu_reset
: >"${SYSCTL_DROPIN_DIR}/99-performance-tuning.conf"
_t_run fix_sysctl_tuning
_t_eq "0" "$?" "fix_sysctl_tuning: returns 0 when already applied"
_t_contains "$out" "already applied" "fix_sysctl_tuning: reports the skip"
_t_eq "" "$(_t_undo)" "fix_sysctl_tuning: records no undo when skipping"
_t_lacks "$(_t_calls)" "sysctl --system" "fix_sysctl_tuning: does not reload when skipping"
# Case 2: a fresh machine -> drop-in written, reloaded, undo recorded with the
# values that were live BEFORE the change.
ubuntu_reset
_t_stub_stdin sysctl <<'STUB'
case "$2" in
vm.swappiness) echo 60 ;;
vm.vfs_cache_pressure) echo 100 ;;
vm.dirty_ratio) echo 20 ;;
vm.dirty_background_ratio) echo 10 ;;
esac
exit 0
STUB
_t_run fix_sysctl_tuning
dropin="$(_t_sysctl_file)"
_t_contains "$dropin" "vm.swappiness = 10" "fix_sysctl_tuning: writes swappiness"
_t_contains "$dropin" "vm.vfs_cache_pressure = 50" "fix_sysctl_tuning: writes cache pressure"
_t_contains "$dropin" "vm.dirty_ratio = 15" "fix_sysctl_tuning: writes the dirty ratio"
_t_contains "$(_t_calls)" "sysctl --system" "fix_sysctl_tuning: applies immediately"
undo="$(_t_undo)"
_t_contains "$undo" "rm -f /etc/sysctl.d/99-performance-tuning.conf" \
"fix_sysctl_tuning: the undo removes the real drop-in, not the sandbox one"
_t_contains "$undo" "vm.swappiness=60" "fix_sysctl_tuning: undo restores the prior swappiness"
_t_contains "$undo" "vm.dirty_ratio=20" "fix_sysctl_tuning: undo restores the prior dirty ratio"
# Case 3: sysctl cannot report the current values -> documented defaults are
# used for the undo rather than an empty string, which would produce an undo
# script that sets nothing.
ubuntu_reset
_t_stub sysctl 'exit 1'
_t_run fix_sysctl_tuning
undo="$(_t_undo)"
_t_contains "$undo" "vm.swappiness=60" "fix_sysctl_tuning: falls back to the default swappiness"
_t_contains "$undo" "vm.vfs_cache_pressure=100" "fix_sysctl_tuning: falls back for cache pressure"
_t_contains "$undo" "vm.dirty_background_ratio=10" \
"fix_sysctl_tuning: falls back for the background ratio"
printf '\n-- fix_nvidia_persistence --\n'
# Case 4: persistence mode already enabled -> skip.
ubuntu_reset
_t_stub_stdin nvidia-smi <<'STUB'
[[ $1 == -q ]] && { echo " Persistence Mode : Enabled"; exit 0; }
exit 0
STUB
_t_run fix_nvidia_persistence
_t_eq "0" "$?" "fix_nvidia_persistence: returns 0 when persistence is on"
_t_contains "$out" "already enabled" "fix_nvidia_persistence: reports the skip"
_t_eq "" "$(_t_undo)" "fix_nvidia_persistence: records no undo when skipping"
# Case 5: the helper service already exists AND is enabled, but persistence is
# off this boot -> start it, do not rewrite the unit.
ubuntu_reset
_t_stub nvidia-smi 'exit 0'
printf 'EXISTING UNIT\n' >"${SYSTEMD_UNIT_DIR}/nvidia-persistence-mode.service"
_t_run fix_nvidia_persistence
_t_contains "$out" "helper already configured" \
"fix_nvidia_persistence: reports the helper is set up"
_t_contains "$(_t_calls)" "start nvidia-persistence-mode.service" \
"fix_nvidia_persistence: starts the helper for this boot"
_t_eq "EXISTING UNIT" "$(_t_unit nvidia-persistence-mode.service)" \
"fix_nvidia_persistence: does not rewrite an existing unit"
# Case 6: nvidia-persistenced present -> the helper unit is created, enabled,
# and the undo removes it.
ubuntu_reset
_t_stub nvidia-smi 'exit 0'
_t_stub nvidia-persistenced 'exit 0'
_t_stub_stdin systemctl <<'STUB'
[[ $1 == is-enabled ]] && exit 1
exit 0
STUB
_t_run fix_nvidia_persistence
unit="$(_t_unit nvidia-persistence-mode.service)"
_t_contains "$unit" "Description=Enable NVIDIA Persistence Mode" \
"fix_nvidia_persistence: writes the helper unit"
_t_contains "$unit" "Requires=nvidia-persistenced.service" \
"fix_nvidia_persistence: the helper requires the daemon"
calls="$(_t_calls)"
_t_contains "$calls" "start nvidia-persistenced.service" \
"fix_nvidia_persistence: makes sure the daemon is running first"
_t_contains "$calls" "daemon-reload" "fix_nvidia_persistence: reloads systemd"
_t_contains "$calls" "enable --now nvidia-persistence-mode.service" \
"fix_nvidia_persistence: enables the helper"
_t_contains "$(_t_undo)" "rm -f /etc/systemd/system/nvidia-persistence-mode.service" \
"fix_nvidia_persistence: undo removes the real helper unit"
# Case 7: no nvidia-persistenced -> fall back to the standalone service.
ubuntu_reset
_t_stub nvidia-smi 'exit 0'
_t_unstub nvidia-persistenced
_t_hide nvidia-persistenced
_t_stub_stdin systemctl <<'STUB'
[[ $1 == is-enabled ]] && exit 1
exit 0
STUB
_t_run fix_nvidia_persistence
unit="$(_t_unit nvidia-persistence.service)"
_t_contains "$unit" "Description=NVIDIA Persistence Mode" \
"fix_nvidia_persistence: writes the fallback unit"
_t_contains "$unit" "Requires=nvidia.target" \
"fix_nvidia_persistence: the fallback requires nvidia.target, not the daemon"
_t_contains "$(_t_calls)" "enable --now nvidia-persistence.service" \
"fix_nvidia_persistence: enables the fallback service"
_t_contains "$(_t_undo)" "rm -f /etc/systemd/system/nvidia-persistence.service" \
"fix_nvidia_persistence: undo removes the real fallback unit"
_t_eq "" "$(_t_unit nvidia-persistence-mode.service)" \
"fix_nvidia_persistence: writes no helper unit without the daemon"
_t_full_path
printf '\nubuntu_perf_fixes (sysctl/nvidia): %d passed, %d failed\n' "$PASS" "$FAIL"
[[ $FAIL -eq 0 ]]