testsAndMisc/linux_configuration/fixes/lib/tests/arch_perf_harness.sh
Krzysztof kuhy Rudnicki 2d54765f6f
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Read the load average through LOADAVG_FILE so the probe tests are host-independent
The "flags nothing on a quiet machine" case read the real /proc/loadavg and
went red whenever a game was running on the host (22/16 during a BeamNG
session), and the "flags a load at/above thread count" case had to SKIP
whenever the host was idle. Same override pattern as JOURNALD_CONF_DIR: the
harness writes a fixture, both cases now assert deterministically, and a
missing file degrades to 0 as before.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SS8M5wk88nJTXxDWz61KKk
2026-09-14 21:55:06 +02:00

111 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# lib/tests/arch_perf_harness.sh — shared setup for the arch performance lib
# tests (arch_perf_report.sh, arch_perf_probes.sh).
#
# Sourced, not executed. Builds on lib_test_core.sh and adds the pieces these
# libs read from their entry script, diagnose_arch_performance.sh:
#
# * REPORT_FILE, FINDINGS, ACTIONS and the APPLY_SAFE_FIXES flag;
# * add_finding, add_action and run_and_log, redefined verbatim from the
# entry script -- they live there rather than in any lib, the same
# arrangement pacman_hook_stall_harness.sh handles with log_size;
# * the real log_*/has_cmd helpers, via lib/common.sh.
#
# REPORT_FILE points into the throwaway tmpdir, so the probes' report writes
# never leave the sandbox.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FIXES_DIR="$(cd "${SCRIPT_DIR}/../.." && pwd)"
REPO_ROOT="$(cd "${FIXES_DIR}/../.." && pwd)"
# shellcheck source=lib_test_core.sh
. "${SCRIPT_DIR}/lib_test_core.sh"
# shellcheck source=../../../lib/common.sh
. "${REPO_ROOT}/linux_configuration/lib/common.sh"
# --- globals and helpers owned by diagnose_arch_performance.sh --------------
# These four are read by the libs under test, never by this harness, so
# static analysis cannot see the use; export makes the intent explicit.
export REPORT_FILE="${TEST_TMPDIR}/report.log"
# arch_perf_fixes.sh writes a journald drop-in and a systemd unit, and probes
# two absolute home paths for organize_downloads.sh. All three are behind
# overrides defaulting to the real locations, because run_all.sh runs UN-jailed
# in ci_mirror.sh and in CI, where a bind mount would protect nothing.
export JOURNALD_CONF_DIR="${TEST_TMPDIR}/journald.conf.d"
export SYSTEMD_UNIT_DIR="${TEST_TMPDIR}/systemd_units"
export ORGANIZE_SCRIPT_CANDIDATES="${TEST_TMPDIR}/organize_downloads.sh"
# collect_basics reads the 1-minute load from here; a fixture, not the real
# /proc/loadavg, or the "quiet machine" case depends on nothing else running.
export LOADAVG_FILE="${TEST_TMPDIR}/loadavg"
export APPLY_SAFE_FIXES="false"
export INSTALL_TOOLS="false"
declare -a FINDINGS=()
declare -a ACTIONS=()
# Verbatim from diagnose_arch_performance.sh.
add_finding() {
FINDINGS+=("$1")
log_warn "$1"
}
add_action() {
ACTIONS+=("$1")
log_info "$1"
}
run_and_log() {
local header="$1"
shift
{
echo
echo "=== $header ==="
"$@" 2>&1 || true
} >>"$REPORT_FILE"
}
# --- external tool stubs ----------------------------------------------------
_arch_default_stubs() {
local tool
for tool in nvidia-smi lspci journalctl systemctl sysctl lsblk sensors \
free uptime ps top swapon findmnt pacman systemd-analyze; do
_t_stub "$tool" 'exit 0'
done
}
# arch_reset — start a test group from "nothing has happened yet".
arch_reset() {
_t_reset_calls
FINDINGS=()
ACTIONS=()
APPLY_SAFE_FIXES="false"
INSTALL_TOOLS="false"
export APPLY_SAFE_FIXES INSTALL_TOOLS
: >"$REPORT_FILE"
rm -rf "${JOURNALD_CONF_DIR}" "${SYSTEMD_UNIT_DIR}"
mkdir -p "${JOURNALD_CONF_DIR}" "${SYSTEMD_UNIT_DIR}"
rm -f "${ORGANIZE_SCRIPT_CANDIDATES}"
echo "0.10 0.20 0.30 1/100 1" >"${LOADAVG_FILE}"
_t_full_path
_arch_default_stubs
}
# _t_report — the report file's contents this round.
_t_report() {
cat "$REPORT_FILE" 2>/dev/null || true
}
# _t_findings / _t_actions — the recorded findings and actions, one per line.
_t_findings() {
printf '%s\n' "${FINDINGS[@]:-}"
}
_t_actions() {
printf '%s\n' "${ACTIONS[@]:-}"
}
arch_reset