testsAndMisc/linux_configuration/fixes/lib/tests/test_arch_perf_fixes.sh
Krzysztof kuhy Rudnicki a201963c83
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Keep NetworkManager-wait-online enabled
Disabling it left network-online.target empty, so reflector, periodic
maintenance and nftables-drift raced the LAN/DNS and failed every boot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 08:28:40 +01:00

125 lines
5.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Tests for lib/arch_perf_fixes.sh: fix_journal, fix_nm_wait_online and
# fix_media_organizer.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=arch_perf_harness.sh
. "${SCRIPT_DIR}/arch_perf_harness.sh"
# shellcheck source=../arch_perf_fixes.sh
. "${FIXES_DIR}/lib/arch_perf_fixes.sh"
dropin="${JOURNALD_CONF_DIR}/size-limit.conf"
unit="${SYSTEMD_UNIT_DIR}/media-organizer.service"
# --- fix_journal ------------------------------------------------------------
# A multi-gigabyte journal is vacuumed AND capped. This only fires because the
# size regex now tolerates journalctl's spaceless "4.2G" output; with the old
# pattern the vacuum was unreachable.
arch_reset
_t_stub journalctl 'echo "Archived and active journals take up 4.2G in the file system."'
fix_journal
_t_contains "$(_t_calls)" "journalctl --vacuum-size=4G" \
"fix_journal: vacuums a multi-gigabyte journal"
_t_contains "$(cat "$dropin" 2>/dev/null)" "SystemMaxUse=4G" \
"fix_journal: writes the permanent size cap"
_t_contains "$(_t_calls)" "systemctl restart systemd-journald" \
"fix_journal: restarts journald after writing the cap"
# A journal already under a gigabyte is left alone, but still capped.
arch_reset
_t_stub journalctl 'echo "Archived and active journals take up 240.0M in the file system."'
out="$(fix_journal 2>&1)"
_t_contains "$out" "already under 4G" \
"fix_journal: reports a journal that is already small enough"
_t_lacks "$(_t_calls)" "journalctl --vacuum-size" \
"fix_journal: does not vacuum a journal measured in megabytes"
_t_contains "$(cat "$dropin" 2>/dev/null)" "SystemMaxUse=4G" \
"fix_journal: still writes the cap for a small journal"
# The cap already exists: it is not rewritten and journald is not restarted.
arch_reset
_t_stub journalctl 'echo "120.0M"'
printf '[Journal]\nSystemMaxUse=4G\n' >"$dropin"
out="$(fix_journal 2>&1)"
_t_contains "$out" "size cap already configured" \
"fix_journal: reports an already-configured size cap"
_t_lacks "$(_t_calls)" "systemctl restart systemd-journald" \
"fix_journal: does not restart journald when the cap already exists"
# --- fix_nm_wait_online -----------------------------------------------------
# Disabling it left network-online.target with no dependencies, so every
# After=network-online.target unit raced DNS at boot and failed (2026-11-20).
arch_reset
_t_stub systemctl 'exit 1'
fix_nm_wait_online
_t_contains "$(_t_calls)" "systemctl enable NetworkManager-wait-online.service" \
"fix_nm_wait_online: enables the wait-online unit when it is disabled"
arch_reset
_t_stub systemctl 'exit 0'
out="$(fix_nm_wait_online 2>&1)"
_t_contains "$out" "already enabled" \
"fix_nm_wait_online: reports a unit that is already enabled"
_t_lacks "$(_t_calls)" "systemctl disable" \
"fix_nm_wait_online: never disables the wait-online unit"
# --- fix_media_organizer ----------------------------------------------------
# The organizer script is nowhere to be found: the fix is skipped. Both
# candidate paths are absolute; the first is overridden into the tmpdir and
# the second is a real home path that does not exist on this machine.
arch_reset
if [[ -f /home/kuhy/src/testsAndMisc/linux_configuration/utils/organize_downloads.sh ]]; then
_t_pass "fix_media_organizer: SKIP not-found case (the second candidate exists here)"
else
out="$(fix_media_organizer 2>&1)"
_t_contains "$out" "not found — skipping" \
"fix_media_organizer: skips when no organize_downloads.sh exists"
_t_lacks "$(_t_calls)" "systemctl daemon-reload" \
"fix_media_organizer: writes no unit when the script is missing"
fi
# The script exists: the unit is written, reloaded and enabled.
arch_reset
printf '#!/bin/bash\necho organize\n' >"$ORGANIZE_SCRIPT_CANDIDATES"
out="$(SUDO_USER="kuhy" fix_media_organizer 2>&1)"
unit_text="$(cat "$unit" 2>/dev/null)"
_t_contains "$unit_text" "Description=Media File Organizer" \
"fix_media_organizer: writes the unit description"
_t_contains "$unit_text" "User=kuhy" \
"fix_media_organizer: runs the unit as the invoking user, not root"
_t_contains "$unit_text" "ExecStart=${ORGANIZE_SCRIPT_CANDIDATES}" \
"fix_media_organizer: points ExecStart at the script it found"
_t_contains "$(_t_calls)" "systemctl daemon-reload" \
"fix_media_organizer: reloads systemd after writing the unit"
_t_contains "$(_t_calls)" "systemctl enable media-organizer.service" \
"fix_media_organizer: enables the unit"
_t_contains "$(_t_calls)" "systemctl stop media-organizer.service" \
"fix_media_organizer: stops the old unit before replacing it"
# An already-correct unit is left alone.
arch_reset
printf '#!/bin/bash\n' >"$ORGANIZE_SCRIPT_CANDIDATES"
printf 'User=kuhy\nExecStart=%s\n' "$ORGANIZE_SCRIPT_CANDIDATES" >"$unit"
out="$(SUDO_USER="kuhy" fix_media_organizer 2>&1)"
_t_contains "$out" "already correctly configured" \
"fix_media_organizer: skips a unit that is already correct"
_t_eq "" "$(_t_calls)" \
"fix_media_organizer: runs no systemctl command when the unit is correct"
# A unit that exists but names the WRONG user is rewritten.
arch_reset
printf '#!/bin/bash\n' >"$ORGANIZE_SCRIPT_CANDIDATES"
printf 'User=someoneelse\nExecStart=%s\n' "$ORGANIZE_SCRIPT_CANDIDATES" >"$unit"
out="$(SUDO_USER="kuhy" fix_media_organizer 2>&1)"
_t_contains "$(cat "$unit")" "User=kuhy" \
"fix_media_organizer: rewrites a unit that names the wrong user"
echo
echo "arch_perf_fixes: ${PASS} passed, ${FAIL} failed"
((FAIL == 0))