testsAndMisc/linux_configuration/fixes/lib/tests/test_arch_hardware.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

121 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# lib/tests/test_arch_hardware.sh — arch_hardware (fstrim/nvidia) — tests for tweak_fstrim and tweak_nvidia_gpu.
#
# Split from the mitigation/journal/ananicy cases in
# test_arch_hardware_boot.sh to hold every file under the 250-line cap.
# fstrim.timer, an NVIDIA card, a boot loader, a journal size. Every case
# therefore stubs the probe and, where the lib writes, points it at the
# throwaway tmpdir through the harness's overrides.
#
# 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=arch_desktop_harness.sh
. "${SCRIPT_DIR}/arch_desktop_harness.sh"
# shellcheck source=../arch_hardware.sh
. "${FIXES_DIR}/lib/arch_hardware.sh"
printf '\n-- tweak_fstrim --\n'
# Case 1: already enabled -> skip.
arch_desktop_reset
_t_stub systemctl 'exit 0'
_t_run tweak_fstrim
_t_eq "0" "$?" "tweak_fstrim: returns 0 when already enabled"
_t_contains "$out" "already enabled" "tweak_fstrim: reports the skip"
_t_lacks "$(_t_calls)" "systemctl enable --now fstrim.timer" \
"tweak_fstrim: does not re-enable"
# Case 2: not enabled -> enable it.
arch_desktop_reset
_t_stub_stdin systemctl <<'STUB'
[[ $1 == is-enabled ]] && exit 1
exit 0
STUB
_t_run tweak_fstrim
_t_contains "$(_t_calls)" "systemctl enable --now fstrim.timer" \
"tweak_fstrim: enables the timer when it is off"
printf '\n-- tweak_nvidia_gpu --\n'
# Case 3: no NVIDIA card at all.
arch_desktop_reset
_t_unstub nvidia-smi
_t_hide nvidia-smi
_t_run tweak_nvidia_gpu
_t_eq "0" "$?" "tweak_nvidia_gpu: returns 0 without nvidia-smi"
_t_contains "$out" "nvidia-smi not found" "tweak_nvidia_gpu: reports the skip"
_t_full_path
# Case 4: persistence mode already on -> not re-enabled, but the unit is
# still installed.
arch_desktop_reset
_t_stub_stdin nvidia-smi <<'STUB'
[[ $1 == --query-gpu=persistence_mode ]] && { echo "Enabled"; exit 0; }
exit 0
STUB
_t_unstub nvidia-persistenced
_t_hide nvidia-persistenced
_t_run tweak_nvidia_gpu
_t_contains "$out" "already enabled" "tweak_nvidia_gpu: reports persistence already on"
_t_lacks "$(_t_calls)" "nvidia-smi -pm 1" "tweak_nvidia_gpu: does not re-enable persistence"
_t_contains "$(cat "${SYSTEMD_UNIT_DIR}/nvidia-performance.service")" \
"ExecStart=/usr/bin/nvidia-smi -pm 1" "tweak_nvidia_gpu: writes the unit"
_t_contains "$(_t_calls)" "systemctl daemon-reload" "tweak_nvidia_gpu: reloads systemd"
_t_full_path
# Case 5: persistence mode off -> turned on.
arch_desktop_reset
_t_stub_stdin nvidia-smi <<'STUB'
[[ $1 == --query-gpu=persistence_mode ]] && { echo "Disabled"; exit 0; }
exit 0
STUB
_t_unstub nvidia-persistenced
_t_hide nvidia-persistenced
_t_run tweak_nvidia_gpu
_t_contains "$(_t_calls)" "nvidia-smi -pm 1" "tweak_nvidia_gpu: enables persistence mode"
_t_contains "$out" "persistence mode enabled" "tweak_nvidia_gpu: logs the change"
_t_full_path
# Case 6: an existing unit file is not rewritten.
arch_desktop_reset
_t_stub nvidia-smi 'exit 0'
printf 'CUSTOM UNIT\n' >"${SYSTEMD_UNIT_DIR}/nvidia-performance.service"
_t_unstub nvidia-persistenced
_t_hide nvidia-persistenced
_t_run tweak_nvidia_gpu
_t_eq "CUSTOM UNIT" "$(cat "${SYSTEMD_UNIT_DIR}/nvidia-performance.service")" \
"tweak_nvidia_gpu: leaves an existing unit alone"
_t_full_path
# Case 7: nvidia-persistenced present but inactive -> enabled AND started.
arch_desktop_reset
_t_stub nvidia-smi 'exit 0'
_t_stub nvidia-persistenced 'exit 0'
_t_stub_stdin systemctl <<'STUB'
[[ $1 == is-active ]] && exit 1
exit 0
STUB
_t_run tweak_nvidia_gpu
_t_contains "$(_t_calls)" "systemctl enable nvidia-persistenced.service" \
"tweak_nvidia_gpu: enables nvidia-persistenced"
_t_contains "$(_t_calls)" "systemctl start nvidia-persistenced.service" \
"tweak_nvidia_gpu: starts an inactive nvidia-persistenced"
# Case 8: nvidia-persistenced already active -> enabled but NOT started.
arch_desktop_reset
_t_stub nvidia-smi 'exit 0'
_t_stub nvidia-persistenced 'exit 0'
_t_stub systemctl 'exit 0'
_t_run tweak_nvidia_gpu
_t_lacks "$(_t_calls)" "systemctl start nvidia-persistenced.service" \
"tweak_nvidia_gpu: does not start an already-active persistenced"
printf '\narch_hardware (fstrim/nvidia): %d passed, %d failed\n' "$PASS" "$FAIL"
[[ $FAIL -eq 0 ]]