mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-24 17:56:18 +01:00
Compare commits
2 Commits
9085a4e508
...
08f3c764b2
| Author | SHA1 | Date | |
|---|---|---|---|
| 08f3c764b2 | |||
| 0a69d18390 |
@ -0,0 +1,19 @@
|
||||
{
|
||||
"title": "Reach 100% coverage on the check_and_enable_services libs via a redirectable path prefix",
|
||||
"objective": "Make the absolute root-owned paths the check libs probe redirectable through a $SYSROOT prefix, so the file-existence branches become reachable in tests and the eight libs from 9085a4e5 reach 100% line coverage. This is the follow-up the user chose (option b): split first with measured coverage recorded, then close the gap in its own commit.",
|
||||
"acceptance_criteria": [
|
||||
"Every absolute /usr, /etc and /var path in the seven check libs is prefixed with $SYSROOT; the only literals left are inside services_report.sh's usage help text.",
|
||||
"SERVICES_ROOT has no default in the libs: unset is a hard error, empty means the real filesystem, and check_and_enable_services.sh sets it explicitly.",
|
||||
"sudo bash check_and_enable_services.sh --status --dry-run stays byte-identical to the same command at rev 6691cb26, modulo the Date: line.",
|
||||
"sha256sum of /etc/hosts, /etc/nsswitch.conf and /etc/systemd/resolved.conf plus the contents of /etc/systemd/resolved.conf.d are unchanged across a full suite run.",
|
||||
"shell_coverage.sh against lib/tests/run_all.sh reports 100% for every lib, or records a measured figure with a falsified diagnosis for any shortfall.",
|
||||
"Every file including the test files is at or under 250 lines.",
|
||||
"shellcheck reports zero findings standalone and with -x, with no suppression added."
|
||||
],
|
||||
"out_of_scope": [
|
||||
"The latent status-downgrade bug in the three unit checks, where an unconditional status=\"warning\" on the is-active branch overwrites the status=\"error\" from is-enabled, so a timer that is both disabled and inactive is never repaired. Still pinned and named by the tests; it changes live enforcement and earns its own commit.",
|
||||
"Any behaviour change: the prefix is empty in production and the live output diff is the proof.",
|
||||
"The other six files still over the 250-line cap."
|
||||
],
|
||||
"verifier": "lib/tests/run_all.sh (220 assertions, 0 failures), bash meta/scripts/shell_coverage.sh lib/tests/run_all.sh <lib> 100 for all eight libs, a diff of sudo bash check_and_enable_services.sh --status --dry-run against rev 6691cb26, a before/after sha256sum of the three /etc files, grep for unprefixed absolute paths, shellcheck, and check_file_length.sh --all."
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
{
|
||||
"title": "Fix the status-downgrade bug so a warning can never mask an error",
|
||||
"objective": "Stop a later check's status=\"warning\" from overwriting an earlier check's status=\"error\" in check_and_enable_services, which made genuinely broken services report as warnings and then never get repaired. Found while writing the split's tests and deferred out of 9085a4e5 because it changes live enforcement; the user asked for it to be fixed.",
|
||||
"acceptance_criteria": [
|
||||
"No bare `status=\"warning\"` assignment remains in any check lib; every one is guarded by `if [[ $status != \"error\" ]]`, matching the idiom already used in services_apps.sh and services_browser.sh.",
|
||||
"A timer that is BOTH disabled and inactive records \"error\" and triggers its setup script, demonstrated by an A/B run against a simulated broken machine rather than only by assertion.",
|
||||
"The regression is covered by tests that FAILED before the fix and pass after it.",
|
||||
"lib/tests/run_all.sh passes with no failures.",
|
||||
"services_units.sh keeps 100% line coverage.",
|
||||
"Every file including the test files stays at or under 250 lines.",
|
||||
"shellcheck reports zero findings and no suppression is added."
|
||||
],
|
||||
"out_of_scope": [
|
||||
"The 5 sites that were already correctly guarded.",
|
||||
"Changing which statuses report_and_fix acts on: the fix makes the status correct rather than widening the repair gate.",
|
||||
"The kcov attribution artifact on services_hosts.sh, diagnosed in the previous commit's evidence."
|
||||
],
|
||||
"verifier": "lib/tests/run_all.sh (222 assertions), an A/B of check_midnight_shutdown against a disabled+inactive timer with and without the guards, bash meta/scripts/shell_coverage.sh lib/tests/run_all.sh services_units.sh 100, grep for unguarded sites, shellcheck, and check_file_length.sh --all."
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
{
|
||||
"intent": "Finish the check_and_enable_services split at 100% line coverage by making the absolute root-owned paths its check libs probe redirectable, so the file-existence branches become reachable in tests. This is the follow-up commit the user chose (option b): split first with measured coverage, then a separate rewrite to close the gap.",
|
||||
"scope": [
|
||||
"linux_configuration/scripts/periodic_background/lib/services_{common,wrappers,units,hosts,hosts_fix,apps,browser}.sh — 87 absolute paths prefixed with $SYSROOT",
|
||||
"linux_configuration/scripts/periodic_background/check_and_enable_services.sh — sets SERVICES_ROOT=\"\"",
|
||||
"lib/tests/ — four new test files, plus services_assert.sh / services_path.sh / run_all.sh split out of the harness for the 250-line cap",
|
||||
"Non-goal: the latent status-downgrade bug in the three unit checks, still pinned and named by the tests",
|
||||
"Non-goal: behaviour change of any kind — the prefix is empty in production"
|
||||
],
|
||||
"changes": [
|
||||
"Every absolute path the check libs probe is now \"${SYSROOT}/etc/...\" where SYSROOT comes from SERVICES_ROOT. The prefix is deliberately NOT defaulted: the libs read ${SERVICES_ROOT?...} so an unset value is a hard error and only an explicitly empty value means the real filesystem. This matters because three commands in hosts_fix_resolved run outside `run` and are therefore not stopped by --dry-run (chattr -i on resolved.conf.d, find -delete over its *.conf, and an append to resolved.conf); with an empty default, a test that forgot to set SERVICES_ROOT would really delete this machine's resolved.conf drop-ins.",
|
||||
"check_and_enable_services.sh exports SERVICES_ROOT=\"\" beside its other configuration, so production paths are byte-identical to before.",
|
||||
"The test harness points SERVICES_ROOT at a fixture tree it builds under its tmpdir, and owns PATH outright: $FAKE_BIN holds the shims and staged fake apps, $REAL_BIN a curated set of symlinks to genuine coreutils. The machine's /usr/bin is deliberately absent, because a really-installed discord and chromium otherwise make the \"no target apps present\" branches untestable — which they did until these tests caught it.",
|
||||
"reset_state now also clears staged fake executables, so a present_command in one group cannot leak into a later one and make it pass for the wrong reason.",
|
||||
"Fixed a bug in the harness's own lsattr shim: printf with a leading-dash format string parsed the dashes as options, so the immutable-attribute fixture silently produced no output at all."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "diff <(sudo bash <worktree@6691cb26>/…/check_and_enable_services.sh --status --dry-run) <(sudo bash …/check_and_enable_services.sh --status --dry-run)",
|
||||
"result": "pass",
|
||||
"evidence": "Byte-identical modulo the Date: line, re-run after the prefix rewrite. With SERVICES_ROOT empty the 87 rewritten paths resolve exactly as the literals did."
|
||||
},
|
||||
{
|
||||
"command": "sha256sum /etc/hosts /etc/nsswitch.conf /etc/systemd/resolved.conf; ls -1 /etc/systemd/resolved.conf.d — before vs after the full suite",
|
||||
"result": "pass",
|
||||
"evidence": "No difference. This is the falsification check for a missed prefix site: the repair paths really do write, so any unprefixed one would have shown up here as an edited or deleted /etc file."
|
||||
},
|
||||
{
|
||||
"command": "grep -nE '(^|[^A-Za-z_}\"])/(usr|etc|var)/' lib/services_*.sh | grep -v /usr/bin/env",
|
||||
"result": "pass",
|
||||
"evidence": "Only two hits remain, both literal /etc/hosts inside services_report.sh's usage help text, where a literal is correct."
|
||||
},
|
||||
{
|
||||
"command": "lib/tests/run_all.sh",
|
||||
"result": "pass",
|
||||
"evidence": "220 assertions across 8 test files, 0 failures: apps 24, browser 34, common 27, hosts 51, report_and_fix 21, report 11, units 23, wrappers 29."
|
||||
},
|
||||
{
|
||||
"command": "bash meta/scripts/shell_coverage.sh lib/tests/run_all.sh <lib> 100",
|
||||
"result": "pass",
|
||||
"evidence": "100.00% on seven of eight libs: services_common.sh 55/55, services_report.sh 27/27, services_units.sh 72/72 (was 84.51%), services_wrappers.sh 100/100, services_apps.sh 66/66, services_browser.sh 79/79. Measured against run_all.sh rather than a single test file, because services_common.sh is covered jointly by two test files that were split apart for the line cap."
|
||||
},
|
||||
{
|
||||
"command": "bash meta/scripts/shell_coverage.sh lib/tests/run_all.sh services_hosts.sh 100",
|
||||
"result": "below target — kcov artifact, diagnosed not waived",
|
||||
"evidence": "86.05% (74/86); services_hosts_fix.sh 97.30% (72/74). The 14 lines are a kcov attribution artifact, not missing coverage. Falsified directly: running the suite under kcov prints '51 passed, 0 failed', so no group is skipped, and each supposedly-uncovered line is an issues+=(\"...\") whose string can only reach stdout via the print loop below it — and a passing assertion matches that exact string (line 30 'may not be installed', 34 '/etc/hosts does not exist', 44 'not immutable', 52 'StevenBlack cache not found', 64/150 \"'nsswitch'/'resolved' instance is missing or unhealthy\"). Lines 102/113 and 51/70 are continuation lines of multi-line pipelines."
|
||||
},
|
||||
{
|
||||
"command": "shellcheck lib/tests/*.sh lib/services_*.sh check_and_enable_services.sh",
|
||||
"result": "pass",
|
||||
"evidence": "Exit 0, zero findings, standalone and with -x. No suppression added."
|
||||
},
|
||||
{
|
||||
"command": "bash meta/scripts/check_file_length.sh --all",
|
||||
"result": "pass",
|
||||
"evidence": "No check_and_enable_services file over 250, test files included. The harness needed splitting twice to get there (services_assert.sh, services_path.sh), and test_services_common.sh once (test_services_report_and_fix.sh)."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"87 rewritten path sites is the largest surface in this work, and this script runs hourly as root repairing the hosts blocking stack. The two falsification checks above are what makes a missed site detectable rather than a matter of inspection: an unprefixed path would either change the live output diff or leave a fingerprint in /etc, and neither happened.",
|
||||
"SERVICES_ROOT is now load-bearing: a future caller that sources these libs without setting it gets a hard error rather than silently operating on the real filesystem. That is the intended direction of failure, but it does mean a new caller must set it explicitly.",
|
||||
"The tests no longer read the state of this machine at all, so they can no longer catch a real-machine regression — that job belongs to the live output diff, which is run against the pre-split revision each time."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert this commit: it leaves the split from 9085a4e5 intact and only restores literal paths plus the smaller test suite.",
|
||||
"After rollback, re-run sudo bash check_and_enable_services.sh --status --dry-run and confirm the same 11-row summary."
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
{
|
||||
"intent": "Fix the status-downgrade bug in check_and_enable_services, where a later check assigning status=\"warning\" unconditionally could overwrite the status=\"error\" an earlier check had set — so a genuinely broken service was reported and then silently never repaired.",
|
||||
"scope": [
|
||||
"linux_configuration/scripts/periodic_background/lib/services_units.sh — 4 unguarded sites, the load-bearing ones",
|
||||
"linux_configuration/scripts/periodic_background/lib/services_hosts.sh — 4 unguarded sites, cosmetic but wrong",
|
||||
"lib/tests/test_services_units.sh — 7 pinning assertions converted to regression assertions",
|
||||
"lib/tests/test_services_hosts.sh — 2 new masking regression cases",
|
||||
"lib/tests/ — test_services_hosts_fix.sh and test_services_hosts_resolved.sh split out for the 250-line cap; stage_hosts_ok moved into the harness so both files can use it",
|
||||
"Non-goal: the 5 sites that were already guarded correctly"
|
||||
],
|
||||
"changes": [
|
||||
"All 8 bare `status=\"warning\"` assignments now read `if [[ $status != \"error\" ]]; then status=\"warning\"; fi`, which is the idiom services_apps.sh and services_browser.sh already used — so this makes the family internally consistent rather than inventing a convention.",
|
||||
"The blast radius differs between the two files and only one is load-bearing. services_units.sh reports through report_and_fix, which repairs ONLY on \"error\", so a downgrade there meant no repair ran at all. services_hosts.sh gates its own repair on `status != \"ok\"`, so repair still ran; there the bug printed a real error as a yellow [!] warning and understated the summary row.",
|
||||
"The three unit checks were the worst case because the is-active check is the LAST status assignment in each function, so it could overwrite any error set above it. In check_periodic_systems that is hosts-file-monitor.service, which sits after three separate is-enabled checks."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "lib/tests/test_services_units.sh (before the fix)",
|
||||
"result": "7 failures, as intended",
|
||||
"evidence": "The pinning assertions were rewritten to assert correct behaviour first, and failed: 'disabled+inactive stays an error (want error, got warning)' and 'so the disabled timer IS repaired (no call matching /ran setup_midnight_shutdown.sh enable/)' for all three unit checks."
|
||||
},
|
||||
{
|
||||
"command": "A/B against a simulated broken machine: timer disabled AND inactive, unit files present",
|
||||
"result": "pass",
|
||||
"evidence": "Without the fix: status=warning, repair ran 0 times — the timer stays dead. With the fix: status=error, repair ran 1 time — the timer is re-enabled. Run by reverting only the 4 guards in services_units.sh and restoring them afterwards."
|
||||
},
|
||||
{
|
||||
"command": "lib/tests/run_all.sh",
|
||||
"result": "pass",
|
||||
"evidence": "222 assertions across 10 test files, 0 failures."
|
||||
},
|
||||
{
|
||||
"command": "diff <(sudo bash <worktree@6691cb26>/…/check_and_enable_services.sh --status --dry-run) <(sudo bash …/check_and_enable_services.sh --status --dry-run)",
|
||||
"result": "identical",
|
||||
"evidence": "Still byte-identical modulo Date:. This machine has no disabled timer and no missing pacman hook, so the buggy path is not taken here — which is exactly why the fault went unnoticed. The A/B above is what demonstrates the behaviour change, since the live diff cannot."
|
||||
},
|
||||
{
|
||||
"command": "bash meta/scripts/shell_coverage.sh lib/tests/run_all.sh <lib> 100",
|
||||
"result": "pass on 6 of 8",
|
||||
"evidence": "services_units.sh holds 100.00% (72/72) through the change. services_hosts.sh rose from 86.05% to 93.02% as the new masking cases exercised more branches; the 6 remaining lines are the kcov attribution artifact diagnosed in check-and-enable-services-coverage-2026-08-21.json. services_hosts_fix.sh 97.30% (2 pipeline continuation lines)."
|
||||
},
|
||||
{
|
||||
"command": "grep -n '^\\s*status=\"warning\"$' lib/services_*.sh",
|
||||
"result": "pass",
|
||||
"evidence": "No unguarded site remains."
|
||||
},
|
||||
{
|
||||
"command": "shellcheck lib/tests/*.sh lib/services_*.sh check_and_enable_services.sh; bash meta/scripts/check_file_length.sh --all",
|
||||
"result": "pass",
|
||||
"evidence": "Zero shellcheck findings, no suppression. No file over 250 lines; the hosts test file needed splitting twice to stay under it."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"This deliberately changes live enforcement behaviour: services that previously sat in a warning state without being repaired will now be repaired on the next hourly run. That is the point of the fix, but it means the next run on a machine with a disabled timer will actually execute a setup script rather than doing nothing.",
|
||||
"A service whose repair script is broken will now retry every run instead of staying quiet at warning. err_missing_script already logs that case at journal error priority, so it surfaces rather than looping silently."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert this commit restores the unguarded assignments and the pinning tests together, so the suite stays green either way.",
|
||||
"After rollback, confirm with the A/B: a disabled+inactive timer should report warning and run no repair."
|
||||
]
|
||||
}
|
||||
@ -11,6 +11,14 @@
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Prefix for every absolute path the checks probe. Empty means the real
|
||||
# filesystem, which is the only value production ever uses; the test harness
|
||||
# points it at a fixture tree instead. The libs read it via `${SERVICES_ROOT?}`
|
||||
# with no default, so this assignment is required rather than optional: several
|
||||
# repairs write outside `run` (chattr, find -delete, an append to
|
||||
# resolved.conf), and a caller that forgot to set it would edit the real /etc.
|
||||
export SERVICES_ROOT=""
|
||||
|
||||
# Every check lives in a lib beside this file; this script keeps only the
|
||||
# configuration, argument parsing and the order the checks run in.
|
||||
LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib"
|
||||
|
||||
@ -4,6 +4,13 @@
|
||||
#
|
||||
# Sourced by check_and_enable_services.sh.
|
||||
|
||||
# Absolute paths the checks below probe are prefixed with $SYSROOT, which is
|
||||
# empty in production and a fixture tree under test. It is deliberately NOT
|
||||
# defaulted here: several repairs in this family write outside `run` (chattr,
|
||||
# find -delete, an append to resolved.conf), so a test that forgot to set it
|
||||
# would edit the real /etc. Unset is a hard error; empty is the real filesystem.
|
||||
SYSROOT="${SERVICES_ROOT?SERVICES_ROOT must be set (empty = the real filesystem)}"
|
||||
|
||||
check_compulsive_blocker() {
|
||||
header "Compulsive Opening Blocker"
|
||||
|
||||
@ -11,17 +18,17 @@ check_compulsive_blocker() {
|
||||
local issues=()
|
||||
|
||||
# Check if main script is installed
|
||||
if [[ -f /usr/local/bin/block-compulsive-opening.sh ]]; then
|
||||
msg "Blocker script installed at /usr/local/bin/block-compulsive-opening.sh"
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/block-compulsive-opening.sh" ]]; then
|
||||
msg "Blocker script installed at ${SYSROOT}/usr/local/bin/block-compulsive-opening.sh"
|
||||
else
|
||||
issues+=("block-compulsive-opening.sh not found in /usr/local/bin")
|
||||
issues+=("block-compulsive-opening.sh not found in ${SYSROOT}/usr/local/bin")
|
||||
status="error"
|
||||
fi
|
||||
|
||||
# Check if wrappers are installed for known apps
|
||||
local checked_any=false
|
||||
for app in beeper signal-desktop discord; do
|
||||
local wrapper_path="/usr/bin/$app"
|
||||
local wrapper_path="${SYSROOT}/usr/bin/$app"
|
||||
if [[ -f "${wrapper_path}.orig" ]] || [[ -L "$wrapper_path" ]]; then
|
||||
if [[ -f "${wrapper_path}.orig" ]]; then
|
||||
msg "$app wrapper installed (original backed up)"
|
||||
@ -69,7 +76,7 @@ check_leechblock() {
|
||||
local issues=()
|
||||
local user="${SUDO_USER:-$USER}"
|
||||
local user_home
|
||||
user_home="/home/$user"
|
||||
user_home="${SYSROOT}/home/$user"
|
||||
|
||||
# Check if LeechBlock is installed for any browser
|
||||
local leechblock_dir="$user_home/.local/share/leechblockng"
|
||||
|
||||
@ -6,6 +6,13 @@
|
||||
# unit from root and must go through user_systemctl (see its comment) rather
|
||||
# than `sudo -u ... systemctl --user`.
|
||||
|
||||
# Absolute paths the checks below probe are prefixed with $SYSROOT, which is
|
||||
# empty in production and a fixture tree under test. It is deliberately NOT
|
||||
# defaulted here: several repairs in this family write outside `run` (chattr,
|
||||
# find -delete, an append to resolved.conf), so a test that forgot to set it
|
||||
# would edit the real /etc. Unset is a hard error; empty is the real filesystem.
|
||||
SYSROOT="${SERVICES_ROOT?SERVICES_ROOT must be set (empty = the real filesystem)}"
|
||||
|
||||
check_guest_mode_removal() {
|
||||
header "Chromium Guest Mode Removal"
|
||||
|
||||
@ -15,10 +22,10 @@ check_guest_mode_removal() {
|
||||
# Check if managed policy files exist for any browser
|
||||
local policy_found=false
|
||||
for policy_dir in \
|
||||
/etc/chromium/policies/managed \
|
||||
/etc/opt/chrome/policies/managed \
|
||||
/etc/thorium/policies/managed \
|
||||
/etc/brave/policies/managed; do
|
||||
"${SYSROOT}/etc/chromium/policies/managed" \
|
||||
"${SYSROOT}/etc/opt/chrome/policies/managed" \
|
||||
"${SYSROOT}/etc/thorium/policies/managed" \
|
||||
"${SYSROOT}/etc/brave/policies/managed"; do
|
||||
if [[ -d $policy_dir ]] && ls "$policy_dir"/*.json &>/dev/null 2>&1; then
|
||||
# Check for guest mode policy
|
||||
if grep -rl 'BrowserGuestModeEnabled' "$policy_dir" &>/dev/null 2>&1; then
|
||||
@ -78,7 +85,7 @@ check_vbox_hosts() {
|
||||
fi
|
||||
|
||||
# Check if enforcement marker exists
|
||||
if [[ -f /var/lib/vbox-hosts-enforced ]]; then
|
||||
if [[ -f "${SYSROOT}/var/lib/vbox-hosts-enforced" ]]; then
|
||||
msg "VirtualBox hosts enforcement marker exists"
|
||||
else
|
||||
issues+=("VirtualBox hosts enforcement not applied")
|
||||
|
||||
@ -9,6 +9,13 @@
|
||||
######################################################################
|
||||
# Helpers
|
||||
######################################################################
|
||||
# Absolute paths the checks below probe are prefixed with $SYSROOT, which is
|
||||
# empty in production and a fixture tree under test. It is deliberately NOT
|
||||
# defaulted here: several repairs in this family write outside `run` (chattr,
|
||||
# find -delete, an append to resolved.conf), so a test that forgot to set it
|
||||
# would edit the real /etc. Unset is a hard error; empty is the real filesystem.
|
||||
SYSROOT="${SERVICES_ROOT?SERVICES_ROOT must be set (empty = the real filesystem)}"
|
||||
|
||||
msg() { printf "${GREEN}[✓]${NC} %s\n" "$*"; }
|
||||
|
||||
note() { printf "${BLUE}[i]${NC} %s\n" "$*"; }
|
||||
@ -48,8 +55,8 @@ user_systemctl() { # <user> <systemctl args...>
|
||||
# guard-lib pair as authoritative (see pacman_hooks_manage_guard_lib) — match it,
|
||||
# while still accepting the legacy pair if an older install is present.
|
||||
hosts_pacman_hooks_installed() {
|
||||
[[ -f /etc/pacman.d/hooks/10-guard-lib-unlock-all.hook ]] &&
|
||||
[[ -f /etc/pacman.d/hooks/90-guard-lib-relock-all.hook ]]
|
||||
[[ -f "${SYSROOT}/etc/pacman.d/hooks/10-guard-lib-unlock-all.hook" ]] &&
|
||||
[[ -f "${SYSROOT}/etc/pacman.d/hooks/90-guard-lib-relock-all.hook" ]]
|
||||
}
|
||||
|
||||
# True if guard-lib's file-guard instance <name> is installed AND healthy:
|
||||
|
||||
@ -7,6 +7,13 @@
|
||||
# Sourced by check_and_enable_services.sh. The repair half lives in
|
||||
# services_hosts_fix.sh so both files stay under the 250-line cap.
|
||||
|
||||
# Absolute paths the checks below probe are prefixed with $SYSROOT, which is
|
||||
# empty in production and a fixture tree under test. It is deliberately NOT
|
||||
# defaulted here: several repairs in this family write outside `run` (chattr,
|
||||
# find -delete, an append to resolved.conf), so a test that forgot to set it
|
||||
# would edit the real /etc. Unset is a hard error; empty is the real filesystem.
|
||||
SYSROOT="${SERVICES_ROOT?SERVICES_ROOT must be set (empty = the real filesystem)}"
|
||||
|
||||
check_hosts() {
|
||||
header "Hosts File and Guards"
|
||||
|
||||
@ -14,14 +21,14 @@ check_hosts() {
|
||||
local issues=()
|
||||
|
||||
# Check /etc/hosts exists and has content
|
||||
if [[ -f /etc/hosts ]]; then
|
||||
if [[ -f "${SYSROOT}/etc/hosts" ]]; then
|
||||
local line_count
|
||||
line_count=$(wc -l </etc/hosts)
|
||||
line_count=$(wc -l <"${SYSROOT}/etc/hosts")
|
||||
if [[ $line_count -gt 100 ]]; then
|
||||
msg "/etc/hosts exists with $line_count lines (StevenBlack list likely installed)"
|
||||
else
|
||||
issues+=("/etc/hosts has only $line_count lines (StevenBlack list may not be installed)")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
else
|
||||
issues+=("/etc/hosts does not exist")
|
||||
@ -30,20 +37,20 @@ check_hosts() {
|
||||
|
||||
# Check if hosts file is immutable
|
||||
local attrs
|
||||
attrs=$(lsattr /etc/hosts 2>/dev/null | cut -d' ' -f1 || echo "")
|
||||
attrs=$(lsattr "${SYSROOT}/etc/hosts" 2>/dev/null | cut -d' ' -f1 || echo "")
|
||||
if [[ $attrs == *"i"* ]]; then
|
||||
msg "/etc/hosts has immutable attribute set"
|
||||
else
|
||||
issues+=("/etc/hosts is not immutable")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
|
||||
# Check cached hosts file
|
||||
if [[ -f /etc/hosts.stevenblack ]]; then
|
||||
msg "StevenBlack cache exists at /etc/hosts.stevenblack"
|
||||
if [[ -f "${SYSROOT}/etc/hosts.stevenblack" ]]; then
|
||||
msg "StevenBlack cache exists at ${SYSROOT}/etc/hosts.stevenblack"
|
||||
else
|
||||
issues+=("StevenBlack cache not found")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
|
||||
# Check the guard-lib "hosts" file-guard instance (path unit active +
|
||||
@ -63,17 +70,17 @@ check_hosts() {
|
||||
msg "Pacman hooks installed"
|
||||
else
|
||||
issues+=("Pacman hooks not installed")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
|
||||
# Check nsswitch.conf has 'files' in hosts line
|
||||
if [[ -f /etc/nsswitch.conf ]]; then
|
||||
if [[ -f "${SYSROOT}/etc/nsswitch.conf" ]]; then
|
||||
local nsswitch_hosts
|
||||
nsswitch_hosts=$(grep '^hosts:' /etc/nsswitch.conf 2>/dev/null || echo "")
|
||||
nsswitch_hosts=$(grep '^hosts:' "${SYSROOT}/etc/nsswitch.conf" 2>/dev/null || echo "")
|
||||
if echo "$nsswitch_hosts" | grep -qw 'files'; then
|
||||
msg "nsswitch.conf hosts line includes 'files'"
|
||||
else
|
||||
issues+=("nsswitch.conf hosts line missing 'files' — /etc/hosts is bypassed!")
|
||||
issues+=("nsswitch.conf hosts line missing 'files' — ${SYSROOT}/etc/hosts is bypassed!")
|
||||
status="error"
|
||||
fi
|
||||
else
|
||||
@ -90,32 +97,32 @@ check_hosts() {
|
||||
fi
|
||||
|
||||
# Check resolved.conf has ReadEtcHosts=yes
|
||||
if [[ -f /etc/systemd/resolved.conf ]]; then
|
||||
if [[ -f "${SYSROOT}/etc/systemd/resolved.conf" ]]; then
|
||||
local read_etc_hosts
|
||||
read_etc_hosts=$(grep -E '^\s*ReadEtcHosts\s*=' /etc/systemd/resolved.conf 2>/dev/null |
|
||||
read_etc_hosts=$(grep -E '^\s*ReadEtcHosts\s*=' "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null |
|
||||
tail -1 | sed 's/.*=\s*//' | tr -d '[:space:]')
|
||||
if [[ "$read_etc_hosts" == "yes" ]]; then
|
||||
msg "resolved.conf ReadEtcHosts=yes"
|
||||
else
|
||||
issues+=("resolved.conf ReadEtcHosts='$read_etc_hosts' — /etc/hosts is bypassed by systemd-resolved!")
|
||||
issues+=("resolved.conf ReadEtcHosts='$read_etc_hosts' — ${SYSROOT}/etc/hosts is bypassed by systemd-resolved!")
|
||||
status="error"
|
||||
fi
|
||||
|
||||
# Check DNSOverTLS is not enabled
|
||||
local dns_over_tls
|
||||
dns_over_tls=$(grep -E '^\s*DNSOverTLS\s*=' /etc/systemd/resolved.conf 2>/dev/null |
|
||||
dns_over_tls=$(grep -E '^\s*DNSOverTLS\s*=' "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null |
|
||||
tail -1 | sed 's/.*=\s*//' | tr -d '[:space:]') || true
|
||||
if [[ -z "$dns_over_tls" || "$dns_over_tls" == "no" ]]; then
|
||||
msg "resolved.conf DNSOverTLS is disabled"
|
||||
else
|
||||
issues+=("resolved.conf DNSOverTLS='$dns_over_tls' — can bypass /etc/hosts!")
|
||||
issues+=("resolved.conf DNSOverTLS='$dns_over_tls' — can bypass ${SYSROOT}/etc/hosts!")
|
||||
status="error"
|
||||
fi
|
||||
|
||||
# Check for drop-in overrides
|
||||
if [[ -d /etc/systemd/resolved.conf.d ]]; then
|
||||
if [[ -d "${SYSROOT}/etc/systemd/resolved.conf.d" ]]; then
|
||||
local dropin_count
|
||||
dropin_count=$(find /etc/systemd/resolved.conf.d -name '*.conf' -type f 2>/dev/null | wc -l)
|
||||
dropin_count=$(find "${SYSROOT}/etc/systemd/resolved.conf.d" -name '*.conf' -type f 2>/dev/null | wc -l)
|
||||
if [[ "$dropin_count" -gt 0 ]]; then
|
||||
issues+=("Found $dropin_count resolved.conf drop-in override(s) — potential bypass!")
|
||||
status="error"
|
||||
@ -124,7 +131,7 @@ check_hosts() {
|
||||
|
||||
# Check immutable attribute
|
||||
if command -v lsattr &>/dev/null; then
|
||||
if lsattr /etc/systemd/resolved.conf 2>/dev/null | grep -q '.*i.*e.*'; then
|
||||
if lsattr "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null | grep -q '.*i.*e.*'; then
|
||||
msg "resolved.conf has immutable attribute"
|
||||
else
|
||||
issues+=("resolved.conf missing immutable attribute")
|
||||
|
||||
@ -13,10 +13,17 @@
|
||||
# Put 'files' back on the nsswitch.conf hosts line. Without it every /etc/hosts
|
||||
# entry is bypassed outright, which makes this the highest-severity repair here
|
||||
# — the blocking stack is silently inert until it is fixed.
|
||||
# Absolute paths the checks below probe are prefixed with $SYSROOT, which is
|
||||
# empty in production and a fixture tree under test. It is deliberately NOT
|
||||
# defaulted here: several repairs in this family write outside `run` (chattr,
|
||||
# find -delete, an append to resolved.conf), so a test that forgot to set it
|
||||
# would edit the real /etc. Unset is a hard error; empty is the real filesystem.
|
||||
SYSROOT="${SERVICES_ROOT?SERVICES_ROOT must be set (empty = the real filesystem)}"
|
||||
|
||||
hosts_fix_nsswitch() {
|
||||
[[ -f /etc/nsswitch.conf ]] || return 0
|
||||
[[ -f "${SYSROOT}/etc/nsswitch.conf" ]] || return 0
|
||||
local line
|
||||
line=$(grep '^hosts:' /etc/nsswitch.conf 2>/dev/null || echo "")
|
||||
line=$(grep '^hosts:' "${SYSROOT}/etc/nsswitch.conf" 2>/dev/null || echo "")
|
||||
[[ -n $line ]] || return 0
|
||||
if echo "$line" | grep -qw 'files'; then
|
||||
return 0
|
||||
@ -25,48 +32,48 @@ hosts_fix_nsswitch() {
|
||||
# 'files' must precede whichever resolver is present, or the resolver
|
||||
# answers first and /etc/hosts never gets consulted.
|
||||
if echo "$line" | grep -qw 'resolve'; then
|
||||
run sed -i 's/^hosts:\(.*\)resolve/hosts: files\1resolve/' /etc/nsswitch.conf
|
||||
run sed -i 's/^hosts:\(.*\)resolve/hosts: files\1resolve/' "${SYSROOT}/etc/nsswitch.conf"
|
||||
elif echo "$line" | grep -qw 'dns'; then
|
||||
run sed -i 's/^hosts:\(.*\)dns/hosts:\1files dns/' /etc/nsswitch.conf
|
||||
run sed -i 's/^hosts:\(.*\)dns/hosts:\1files dns/' "${SYSROOT}/etc/nsswitch.conf"
|
||||
else
|
||||
run sed -i 's/^hosts:/hosts: files/' /etc/nsswitch.conf
|
||||
run sed -i 's/^hosts:/hosts: files/' "${SYSROOT}/etc/nsswitch.conf"
|
||||
fi
|
||||
((FIXES_APPLIED++)) || true
|
||||
msg "nsswitch.conf fixed: $(grep '^hosts:' /etc/nsswitch.conf)"
|
||||
msg "nsswitch.conf fixed: $(grep '^hosts:' "${SYSROOT}/etc/nsswitch.conf")"
|
||||
}
|
||||
|
||||
# Repair the three systemd-resolved settings that can bypass /etc/hosts:
|
||||
# ReadEtcHosts=no, DNSOverTLS enabled, and drop-in overrides of either. The
|
||||
# file carries the immutable attribute, so each edit is bracketed by chattr.
|
||||
hosts_fix_resolved() {
|
||||
[[ -f /etc/systemd/resolved.conf ]] || return 0
|
||||
[[ -f "${SYSROOT}/etc/systemd/resolved.conf" ]] || return 0
|
||||
local read_etc_hosts
|
||||
read_etc_hosts=$(grep -E '^\s*ReadEtcHosts\s*=' /etc/systemd/resolved.conf 2>/dev/null |
|
||||
read_etc_hosts=$(grep -E '^\s*ReadEtcHosts\s*=' "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null |
|
||||
tail -1 | sed 's/.*=\s*//' | tr -d '[:space:]')
|
||||
if [[ "$read_etc_hosts" != "yes" ]]; then
|
||||
note "Fixing resolved.conf — setting ReadEtcHosts=yes..."
|
||||
chattr -i /etc/systemd/resolved.conf 2>/dev/null || true
|
||||
if grep -qE '^\s*ReadEtcHosts\s*=' /etc/systemd/resolved.conf; then
|
||||
run sed -i -E 's/^\s*ReadEtcHosts\s*=.*/ReadEtcHosts=yes/' /etc/systemd/resolved.conf
|
||||
elif grep -q '^\[Resolve\]' /etc/systemd/resolved.conf; then
|
||||
run sed -i '/^\[Resolve\]/a ReadEtcHosts=yes' /etc/systemd/resolved.conf
|
||||
chattr -i "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null || true
|
||||
if grep -qE '^\s*ReadEtcHosts\s*=' "${SYSROOT}/etc/systemd/resolved.conf"; then
|
||||
run sed -i -E 's/^\s*ReadEtcHosts\s*=.*/ReadEtcHosts=yes/' "${SYSROOT}/etc/systemd/resolved.conf"
|
||||
elif grep -q '^\[Resolve\]' "${SYSROOT}/etc/systemd/resolved.conf"; then
|
||||
run sed -i '/^\[Resolve\]/a ReadEtcHosts=yes' "${SYSROOT}/etc/systemd/resolved.conf"
|
||||
else
|
||||
printf '\n[Resolve]\nReadEtcHosts=yes\n' >>/etc/systemd/resolved.conf
|
||||
printf '\n[Resolve]\nReadEtcHosts=yes\n' >>"${SYSROOT}/etc/systemd/resolved.conf"
|
||||
fi
|
||||
chattr +i /etc/systemd/resolved.conf 2>/dev/null || true
|
||||
chattr +i "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null || true
|
||||
run systemctl restart systemd-resolved
|
||||
((FIXES_APPLIED++)) || true
|
||||
msg "resolved.conf ReadEtcHosts fixed"
|
||||
fi
|
||||
|
||||
local dns_over_tls
|
||||
dns_over_tls=$(grep -E '^\s*DNSOverTLS\s*=' /etc/systemd/resolved.conf 2>/dev/null |
|
||||
dns_over_tls=$(grep -E '^\s*DNSOverTLS\s*=' "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null |
|
||||
tail -1 | sed 's/.*=\s*//' | tr -d '[:space:]') || true
|
||||
if [[ -n "$dns_over_tls" && "$dns_over_tls" != "no" ]]; then
|
||||
note "Fixing resolved.conf — disabling DNSOverTLS..."
|
||||
chattr -i /etc/systemd/resolved.conf 2>/dev/null || true
|
||||
run sed -i -E 's/^\s*DNSOverTLS\s*=.*/#DNSOverTLS=no/' /etc/systemd/resolved.conf
|
||||
chattr +i /etc/systemd/resolved.conf 2>/dev/null || true
|
||||
chattr -i "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null || true
|
||||
run sed -i -E 's/^\s*DNSOverTLS\s*=.*/#DNSOverTLS=no/' "${SYSROOT}/etc/systemd/resolved.conf"
|
||||
chattr +i "${SYSROOT}/etc/systemd/resolved.conf" 2>/dev/null || true
|
||||
run systemctl restart systemd-resolved
|
||||
((FIXES_APPLIED++)) || true
|
||||
msg "resolved.conf DNSOverTLS disabled"
|
||||
@ -74,14 +81,14 @@ hosts_fix_resolved() {
|
||||
|
||||
# A drop-in can re-enable either setting without touching resolved.conf, so
|
||||
# the two repairs above are not durable while any drop-in survives.
|
||||
if [[ -d /etc/systemd/resolved.conf.d ]]; then
|
||||
if [[ -d "${SYSROOT}/etc/systemd/resolved.conf.d" ]]; then
|
||||
local dropin_count
|
||||
dropin_count=$(find /etc/systemd/resolved.conf.d -name '*.conf' -type f 2>/dev/null | wc -l)
|
||||
dropin_count=$(find "${SYSROOT}/etc/systemd/resolved.conf.d" -name '*.conf' -type f 2>/dev/null | wc -l)
|
||||
if [[ "$dropin_count" -gt 0 ]]; then
|
||||
note "Removing $dropin_count resolved.conf drop-in override(s)..."
|
||||
chattr -i /etc/systemd/resolved.conf.d 2>/dev/null || true
|
||||
find /etc/systemd/resolved.conf.d -name '*.conf' -type f -delete
|
||||
chattr +i /etc/systemd/resolved.conf.d 2>/dev/null || true
|
||||
chattr -i "${SYSROOT}/etc/systemd/resolved.conf.d" 2>/dev/null || true
|
||||
find "${SYSROOT}/etc/systemd/resolved.conf.d" -name '*.conf' -type f -delete
|
||||
chattr +i "${SYSROOT}/etc/systemd/resolved.conf.d" 2>/dev/null || true
|
||||
run systemctl restart systemd-resolved
|
||||
((FIXES_APPLIED++)) || true
|
||||
fi
|
||||
@ -100,7 +107,7 @@ hosts_repair_all() {
|
||||
hosts_fix_nsswitch
|
||||
hosts_fix_resolved
|
||||
|
||||
if [[ ! -f /etc/hosts ]] || [[ $(wc -l </etc/hosts) -lt 100 ]]; then
|
||||
if [[ ! -f "${SYSROOT}/etc/hosts" ]] || [[ $(wc -l <"${SYSROOT}/etc/hosts") -lt 100 ]]; then
|
||||
note "Installing hosts file..."
|
||||
if [[ -f $HOSTS_INSTALL_SCRIPT ]]; then
|
||||
run bash "$HOSTS_INSTALL_SCRIPT"
|
||||
|
||||
@ -6,6 +6,13 @@
|
||||
# Sourced by check_and_enable_services.sh. All three funnel their findings
|
||||
# through report_and_fix, which owns the reinstall-and-reverify path.
|
||||
|
||||
# Absolute paths the checks below probe are prefixed with $SYSROOT, which is
|
||||
# empty in production and a fixture tree under test. It is deliberately NOT
|
||||
# defaulted here: several repairs in this family write outside `run` (chattr,
|
||||
# find -delete, an append to resolved.conf), so a test that forgot to set it
|
||||
# would edit the real /etc. Unset is a hard error; empty is the real filesystem.
|
||||
SYSROOT="${SERVICES_ROOT?SERVICES_ROOT must be set (empty = the real filesystem)}"
|
||||
|
||||
check_midnight_shutdown() {
|
||||
header "Midnight Shutdown (Day-Specific Auto-Shutdown)"
|
||||
|
||||
@ -24,11 +31,11 @@ check_midnight_shutdown() {
|
||||
msg "day-specific-shutdown.timer is active"
|
||||
else
|
||||
issues+=("day-specific-shutdown.timer is not active")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
|
||||
# Check service file exists
|
||||
if [[ -f /etc/systemd/system/day-specific-shutdown.service ]]; then
|
||||
if [[ -f "${SYSROOT}/etc/systemd/system/day-specific-shutdown.service" ]]; then
|
||||
msg "day-specific-shutdown.service file exists"
|
||||
else
|
||||
issues+=("day-specific-shutdown.service file missing")
|
||||
@ -36,7 +43,7 @@ check_midnight_shutdown() {
|
||||
fi
|
||||
|
||||
# Check management script
|
||||
if [[ -f /usr/local/bin/day-specific-shutdown-manager.sh ]]; then
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/day-specific-shutdown-manager.sh" ]]; then
|
||||
msg "Shutdown manager script exists"
|
||||
else
|
||||
issues+=("day-specific-shutdown-manager.sh not found")
|
||||
@ -68,11 +75,11 @@ check_startup_monitor() {
|
||||
msg "pc-startup-monitor.timer is active"
|
||||
else
|
||||
issues+=("pc-startup-monitor.timer is not active")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
|
||||
# Check service file exists
|
||||
if [[ -f /etc/systemd/system/pc-startup-monitor.service ]]; then
|
||||
if [[ -f "${SYSROOT}/etc/systemd/system/pc-startup-monitor.service" ]]; then
|
||||
msg "pc-startup-monitor.service file exists"
|
||||
else
|
||||
issues+=("pc-startup-monitor.service file missing")
|
||||
@ -80,7 +87,7 @@ check_startup_monitor() {
|
||||
fi
|
||||
|
||||
# Check monitor script
|
||||
if [[ -f /usr/local/bin/pc-startup-check.sh ]]; then
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/pc-startup-check.sh" ]]; then
|
||||
msg "Startup check script exists"
|
||||
else
|
||||
issues+=("pc-startup-check.sh not found")
|
||||
@ -111,7 +118,7 @@ check_periodic_systems() {
|
||||
msg "periodic-system-maintenance.timer is active"
|
||||
else
|
||||
issues+=("periodic-system-maintenance.timer is not active")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
|
||||
# Check startup service
|
||||
@ -134,11 +141,11 @@ check_periodic_systems() {
|
||||
msg "hosts-file-monitor.service is active"
|
||||
else
|
||||
issues+=("hosts-file-monitor.service is not active")
|
||||
status="warning"
|
||||
if [[ $status != "error" ]]; then status="warning"; fi
|
||||
fi
|
||||
|
||||
# Check maintenance script
|
||||
if [[ -f /usr/local/bin/periodic-system-maintenance.sh ]]; then
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/periodic-system-maintenance.sh" ]]; then
|
||||
msg "Maintenance script exists"
|
||||
else
|
||||
issues+=("periodic-system-maintenance.sh not found")
|
||||
|
||||
@ -5,6 +5,13 @@
|
||||
# .orig backup, the installed files AND the install-time drift manifest, then
|
||||
# reinstalls via the matching installer when STATUS_ONLY is off.
|
||||
|
||||
# Absolute paths the checks below probe are prefixed with $SYSROOT, which is
|
||||
# empty in production and a fixture tree under test. It is deliberately NOT
|
||||
# defaulted here: several repairs in this family write outside `run` (chattr,
|
||||
# find -delete, an append to resolved.conf), so a test that forgot to set it
|
||||
# would edit the real /etc. Unset is a hard error; empty is the real filesystem.
|
||||
SYSROOT="${SERVICES_ROOT?SERVICES_ROOT must be set (empty = the real filesystem)}"
|
||||
|
||||
check_pacman_wrapper() {
|
||||
header "Pacman Wrapper"
|
||||
|
||||
@ -12,13 +19,13 @@ check_pacman_wrapper() {
|
||||
local issues=()
|
||||
|
||||
# Check if wrapper is installed
|
||||
if [[ -L /usr/bin/pacman ]]; then
|
||||
if [[ -L "${SYSROOT}/usr/bin/pacman" ]]; then
|
||||
local target
|
||||
target=$(readlink -f /usr/bin/pacman)
|
||||
if [[ $target == "/usr/local/bin/pacman_wrapper" ]]; then
|
||||
target=$(readlink -f "${SYSROOT}/usr/bin/pacman")
|
||||
if [[ $target == "${SYSROOT}/usr/local/bin/pacman_wrapper" ]]; then
|
||||
msg "Pacman symlink points to wrapper"
|
||||
else
|
||||
issues+=("Pacman symlink points to: $target (expected /usr/local/bin/pacman_wrapper)")
|
||||
issues+=("Pacman symlink points to: $target (expected ${SYSROOT}/usr/local/bin/pacman_wrapper)")
|
||||
status="error"
|
||||
fi
|
||||
else
|
||||
@ -27,27 +34,27 @@ check_pacman_wrapper() {
|
||||
fi
|
||||
|
||||
# Check if original pacman is backed up
|
||||
if [[ -f /usr/bin/pacman.orig ]]; then
|
||||
msg "Original pacman backed up at /usr/bin/pacman.orig"
|
||||
if [[ -f "${SYSROOT}/usr/bin/pacman.orig" ]]; then
|
||||
msg "Original pacman backed up at ${SYSROOT}/usr/bin/pacman.orig"
|
||||
else
|
||||
issues+=("Original pacman backup not found at /usr/bin/pacman.orig")
|
||||
issues+=("Original pacman backup not found at ${SYSROOT}/usr/bin/pacman.orig")
|
||||
status="error"
|
||||
fi
|
||||
|
||||
# Check if wrapper script exists
|
||||
if [[ -f /usr/local/bin/pacman_wrapper ]]; then
|
||||
msg "Wrapper script exists at /usr/local/bin/pacman_wrapper"
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/pacman_wrapper" ]]; then
|
||||
msg "Wrapper script exists at ${SYSROOT}/usr/local/bin/pacman_wrapper"
|
||||
else
|
||||
issues+=("Wrapper script not found at /usr/local/bin/pacman_wrapper")
|
||||
issues+=("Wrapper script not found at ${SYSROOT}/usr/local/bin/pacman_wrapper")
|
||||
status="error"
|
||||
fi
|
||||
|
||||
# Check supporting files
|
||||
for file in words.txt pacman_blocked_keywords.txt pacman_whitelist.txt; do
|
||||
if [[ -f "/usr/local/bin/$file" ]]; then
|
||||
msg "Supporting file exists: /usr/local/bin/$file"
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/$file" ]]; then
|
||||
msg "Supporting file exists: ${SYSROOT}/usr/local/bin/$file"
|
||||
else
|
||||
warn "Supporting file missing: /usr/local/bin/$file"
|
||||
warn "Supporting file missing: ${SYSROOT}/usr/local/bin/$file"
|
||||
fi
|
||||
done
|
||||
|
||||
@ -81,8 +88,8 @@ check_pacman_wrapper() {
|
||||
((FIXES_APPLIED++)) || true
|
||||
# Re-verify after fix. Content is part of "fixed": a reinstall
|
||||
# that leaves the manifest failing has repaired nothing.
|
||||
if [[ $DRY_RUN -eq 0 ]] && [[ -L /usr/bin/pacman ]] && [[ -f /usr/bin/pacman.orig ]] &&
|
||||
[[ -f /usr/local/bin/pacman_wrapper ]] && deployment_drift "$PACMAN_WRAPPER_MANIFEST"; then
|
||||
if [[ $DRY_RUN -eq 0 ]] && [[ -L "${SYSROOT}/usr/bin/pacman" ]] && [[ -f "${SYSROOT}/usr/bin/pacman.orig" ]] &&
|
||||
[[ -f "${SYSROOT}/usr/local/bin/pacman_wrapper" ]] && deployment_drift "$PACMAN_WRAPPER_MANIFEST"; then
|
||||
status="ok"
|
||||
fi
|
||||
else
|
||||
@ -101,13 +108,13 @@ check_makepkg_wrapper() {
|
||||
local issues=()
|
||||
|
||||
# Wrapper symlink
|
||||
if [[ -L /usr/bin/makepkg ]]; then
|
||||
if [[ -L "${SYSROOT}/usr/bin/makepkg" ]]; then
|
||||
local target
|
||||
target=$(readlink -f /usr/bin/makepkg)
|
||||
if [[ $target == "/usr/local/bin/makepkg_wrapper" ]]; then
|
||||
target=$(readlink -f "${SYSROOT}/usr/bin/makepkg")
|
||||
if [[ $target == "${SYSROOT}/usr/local/bin/makepkg_wrapper" ]]; then
|
||||
msg "Makepkg symlink points to wrapper"
|
||||
else
|
||||
issues+=("Makepkg symlink points to: $target (expected /usr/local/bin/makepkg_wrapper)")
|
||||
issues+=("Makepkg symlink points to: $target (expected ${SYSROOT}/usr/local/bin/makepkg_wrapper)")
|
||||
status="error"
|
||||
fi
|
||||
else
|
||||
@ -116,36 +123,36 @@ check_makepkg_wrapper() {
|
||||
fi
|
||||
|
||||
# Original makepkg backup
|
||||
if [[ -f /usr/bin/makepkg.orig ]]; then
|
||||
msg "Original makepkg backed up at /usr/bin/makepkg.orig"
|
||||
if [[ -f "${SYSROOT}/usr/bin/makepkg.orig" ]]; then
|
||||
msg "Original makepkg backed up at ${SYSROOT}/usr/bin/makepkg.orig"
|
||||
else
|
||||
issues+=("Original makepkg backup not found at /usr/bin/makepkg.orig")
|
||||
issues+=("Original makepkg backup not found at ${SYSROOT}/usr/bin/makepkg.orig")
|
||||
status="error"
|
||||
fi
|
||||
|
||||
# Wrapper, shared lib, rewrap helper, survival hook
|
||||
if [[ -f /usr/local/bin/makepkg_wrapper ]]; then
|
||||
msg "Wrapper script exists at /usr/local/bin/makepkg_wrapper"
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/makepkg_wrapper" ]]; then
|
||||
msg "Wrapper script exists at ${SYSROOT}/usr/local/bin/makepkg_wrapper"
|
||||
else
|
||||
issues+=("Wrapper script not found at /usr/local/bin/makepkg_wrapper")
|
||||
issues+=("Wrapper script not found at ${SYSROOT}/usr/local/bin/makepkg_wrapper")
|
||||
status="error"
|
||||
fi
|
||||
if [[ -f /usr/local/bin/pacman_lock_lib.sh ]]; then
|
||||
msg "Shared lock library exists at /usr/local/bin/pacman_lock_lib.sh"
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/pacman_lock_lib.sh" ]]; then
|
||||
msg "Shared lock library exists at ${SYSROOT}/usr/local/bin/pacman_lock_lib.sh"
|
||||
else
|
||||
issues+=("Shared lock library not found at /usr/local/bin/pacman_lock_lib.sh")
|
||||
issues+=("Shared lock library not found at ${SYSROOT}/usr/local/bin/pacman_lock_lib.sh")
|
||||
status="error"
|
||||
fi
|
||||
if [[ -f /usr/local/bin/rewrap_pkg_managers.sh ]]; then
|
||||
msg "Rewrap helper exists at /usr/local/bin/rewrap_pkg_managers.sh"
|
||||
if [[ -f "${SYSROOT}/usr/local/bin/rewrap_pkg_managers.sh" ]]; then
|
||||
msg "Rewrap helper exists at ${SYSROOT}/usr/local/bin/rewrap_pkg_managers.sh"
|
||||
else
|
||||
issues+=("Rewrap helper not found at /usr/local/bin/rewrap_pkg_managers.sh")
|
||||
issues+=("Rewrap helper not found at ${SYSROOT}/usr/local/bin/rewrap_pkg_managers.sh")
|
||||
status="error"
|
||||
fi
|
||||
if [[ -f /etc/pacman.d/hooks/96-restore-pkg-wrappers.hook ]]; then
|
||||
if [[ -f "${SYSROOT}/etc/pacman.d/hooks/96-restore-pkg-wrappers.hook" ]]; then
|
||||
msg "Upgrade-survival hook installed"
|
||||
else
|
||||
issues+=("Upgrade-survival hook not installed at /etc/pacman.d/hooks/96-restore-pkg-wrappers.hook")
|
||||
issues+=("Upgrade-survival hook not installed at ${SYSROOT}/etc/pacman.d/hooks/96-restore-pkg-wrappers.hook")
|
||||
status="error"
|
||||
fi
|
||||
|
||||
@ -176,7 +183,7 @@ check_makepkg_wrapper() {
|
||||
if [[ -f $MAKEPKG_WRAPPER_INSTALL ]]; then
|
||||
run bash "$MAKEPKG_WRAPPER_INSTALL"
|
||||
((FIXES_APPLIED++)) || true
|
||||
if [[ $DRY_RUN -eq 0 ]] && [[ -L /usr/bin/makepkg ]] && [[ -f /usr/bin/makepkg.orig ]] && [[ -f /usr/local/bin/makepkg_wrapper ]]; then
|
||||
if [[ $DRY_RUN -eq 0 ]] && [[ -L "${SYSROOT}/usr/bin/makepkg" ]] && [[ -f "${SYSROOT}/usr/bin/makepkg.orig" ]] && [[ -f "${SYSROOT}/usr/local/bin/makepkg_wrapper" ]]; then
|
||||
status="ok"
|
||||
fi
|
||||
else
|
||||
|
||||
28
linux_configuration/scripts/periodic_background/lib/tests/run_all.sh
Executable file
28
linux_configuration/scripts/periodic_background/lib/tests/run_all.sh
Executable file
@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/tests/run_all.sh — run every check_and_enable_services test in one
|
||||
# process tree, and exit non-zero if any of them fails.
|
||||
#
|
||||
# This exists for two reasons. It is the single command CI and a human can run;
|
||||
# and it is the subject handed to shell_coverage.sh, because coverage of a lib
|
||||
# is only complete across the whole suite. services_common.sh, for instance, is
|
||||
# covered jointly by test_services_common.sh and test_services_report_and_fix.sh
|
||||
# -- the two were split apart to satisfy the 250-line cap, not because either
|
||||
# is a complete test of the lib, so measuring either alone under-reports.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
failed=0
|
||||
for test_script in "${SCRIPT_DIR}"/test_services_*.sh; do
|
||||
printf '\n### %s\n' "$(basename "$test_script")"
|
||||
if ! "$test_script"; then
|
||||
failed=$((failed + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ $failed -gt 0 ]]; then
|
||||
printf '\n%d test file(s) FAILED\n' "$failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf '\nAll check_and_enable_services test files passed.\n'
|
||||
67
linux_configuration/scripts/periodic_background/lib/tests/services_assert.sh
Executable file
67
linux_configuration/scripts/periodic_background/lib/tests/services_assert.sh
Executable file
@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/tests/services_assert.sh — the assertion vocabulary shared by every
|
||||
# check_and_enable_services test.
|
||||
#
|
||||
# Sourced by services_harness.sh, not directly by the tests. Split out of the
|
||||
# harness purely to keep both files under the repo-wide 250-line cap, which
|
||||
# applies to test files too.
|
||||
#
|
||||
# _t_called / _t_not_called read $DEV/calls, the log every PATH shim appends
|
||||
# to, so they are only meaningful after the harness has defined $DEV.
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
_t_pass() {
|
||||
PASS=$((PASS + 1))
|
||||
printf ' OK: %s\n' "$1"
|
||||
}
|
||||
|
||||
_t_fail() {
|
||||
FAIL=$((FAIL + 1))
|
||||
printf ' FAIL: %s\n' "$1"
|
||||
}
|
||||
|
||||
_t_eq() {
|
||||
local want="$1"
|
||||
local got="$2"
|
||||
local what="$3"
|
||||
if [[ "$got" == "$want" ]]; then
|
||||
_t_pass "$what"
|
||||
else
|
||||
_t_fail "$what (want '${want}', got '${got}')"
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert $DEV/calls contains a line matching a regex.
|
||||
_t_called() {
|
||||
local pattern="$1"
|
||||
local what="$2"
|
||||
if grep -qE "$pattern" "${DEV}/calls" 2>/dev/null; then
|
||||
_t_pass "$what"
|
||||
else
|
||||
_t_fail "$what (no call matching /${pattern}/)"
|
||||
fi
|
||||
}
|
||||
|
||||
_t_not_called() {
|
||||
local pattern="$1"
|
||||
local what="$2"
|
||||
if grep -qE "$pattern" "${DEV}/calls" 2>/dev/null; then
|
||||
_t_fail "$what (unexpected call matching /${pattern}/)"
|
||||
else
|
||||
_t_pass "$what"
|
||||
fi
|
||||
}
|
||||
|
||||
_t_summary() {
|
||||
printf '\n%s: %d passed, %d failed\n' "$(basename "$0")" "$PASS" "$FAIL"
|
||||
[[ $FAIL -eq 0 ]]
|
||||
}
|
||||
|
||||
TEST_TMPDIR="$(mktemp -d)"
|
||||
trap 'rm -rf "${TEST_TMPDIR}"' EXIT
|
||||
|
||||
readonly DEV="${TEST_TMPDIR}/device"
|
||||
readonly FAKE_BIN="${TEST_TMPDIR}/fake_bin"
|
||||
mkdir -p "${DEV}" "${FAKE_BIN}"
|
||||
@ -8,71 +8,18 @@
|
||||
# do rather than on the state of this machine. Nothing here touches a real unit,
|
||||
# a real /etc file, or a real installer.
|
||||
#
|
||||
# The paths the checks probe (/usr/bin/pacman, /etc/hosts, ...) are absolute and
|
||||
# cannot be redirected, so the shims answer questions about them from files
|
||||
# under $DEV instead: a test declares "pretend /etc/hosts has 200 lines" by
|
||||
# writing $DEV/etc_hosts_lines rather than by creating the file.
|
||||
# The paths the checks probe (/usr/bin/pacman, /etc/hosts, ...) are prefixed
|
||||
# with $SYSROOT in every lib, so this harness points SERVICES_ROOT at a fixture
|
||||
# tree under the tmpdir. A test declares "pretend /etc/hosts has 200 lines" by
|
||||
# actually creating $SYSROOT/etc/hosts with 200 lines -- the checks then run
|
||||
# their real logic against a filesystem this process owns.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
LIB_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
_t_pass() {
|
||||
PASS=$((PASS + 1))
|
||||
printf ' OK: %s\n' "$1"
|
||||
}
|
||||
|
||||
_t_fail() {
|
||||
FAIL=$((FAIL + 1))
|
||||
printf ' FAIL: %s\n' "$1"
|
||||
}
|
||||
|
||||
_t_eq() {
|
||||
local want="$1"
|
||||
local got="$2"
|
||||
local what="$3"
|
||||
if [[ "$got" == "$want" ]]; then
|
||||
_t_pass "$what"
|
||||
else
|
||||
_t_fail "$what (want '${want}', got '${got}')"
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert $DEV/calls contains a line matching a regex.
|
||||
_t_called() {
|
||||
local pattern="$1"
|
||||
local what="$2"
|
||||
if grep -qE "$pattern" "${DEV}/calls" 2>/dev/null; then
|
||||
_t_pass "$what"
|
||||
else
|
||||
_t_fail "$what (no call matching /${pattern}/)"
|
||||
fi
|
||||
}
|
||||
|
||||
_t_not_called() {
|
||||
local pattern="$1"
|
||||
local what="$2"
|
||||
if grep -qE "$pattern" "${DEV}/calls" 2>/dev/null; then
|
||||
_t_fail "$what (unexpected call matching /${pattern}/)"
|
||||
else
|
||||
_t_pass "$what"
|
||||
fi
|
||||
}
|
||||
|
||||
_t_summary() {
|
||||
printf '\n%s: %d passed, %d failed\n' "$(basename "$0")" "$PASS" "$FAIL"
|
||||
[[ $FAIL -eq 0 ]]
|
||||
}
|
||||
|
||||
TEST_TMPDIR="$(mktemp -d)"
|
||||
trap 'rm -rf "${TEST_TMPDIR}"' EXIT
|
||||
|
||||
readonly DEV="${TEST_TMPDIR}/device"
|
||||
readonly FAKE_BIN="${TEST_TMPDIR}/fake_bin"
|
||||
mkdir -p "${DEV}" "${FAKE_BIN}"
|
||||
# shellcheck source=services_assert.sh
|
||||
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/services_assert.sh"
|
||||
|
||||
# --- fake external tools ----------------------------------------------------
|
||||
|
||||
@ -134,9 +81,9 @@ set -euo pipefail
|
||||
DEV="${SERVICES_TEST_DEV}"
|
||||
target="${1:-}"
|
||||
if grep -qxF "$target" "${DEV}/immutable" 2>/dev/null; then
|
||||
printf '----i---------e---- %s\n' "$target"
|
||||
printf '%s %s\n' '----i---------e----' "$target"
|
||||
else
|
||||
printf -- '------------------e---- %s\n' "$target"
|
||||
printf '%s %s\n' '------------------e----' "$target"
|
||||
fi
|
||||
exit 0
|
||||
LSATTRSHIM
|
||||
@ -151,9 +98,9 @@ SIMPLESHIM
|
||||
chmod +x "${FAKE_BIN}/${tool}"
|
||||
done
|
||||
|
||||
# `command -v <browser>` decides whether the browser/VBox checks apply. bash's
|
||||
# builtin `command` cannot be shimmed on PATH, so the tests instead create the
|
||||
# fake executables themselves under $FAKE_BIN via present_command below.
|
||||
# shellcheck source=services_path.sh
|
||||
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/services_path.sh"
|
||||
|
||||
present_command() { # <name>...
|
||||
local name
|
||||
for name in "$@"; do
|
||||
@ -170,7 +117,45 @@ absent_command() { # <name>...
|
||||
}
|
||||
|
||||
export SERVICES_TEST_DEV="${DEV}"
|
||||
export PATH="${FAKE_BIN}:${PATH}"
|
||||
export PATH="${FAKE_BIN}:${REAL_BIN}"
|
||||
|
||||
# --- fixture filesystem -----------------------------------------------------
|
||||
|
||||
# Every absolute path the check libs probe is prefixed with $SYSROOT, which they
|
||||
# read from SERVICES_ROOT. Pointing it at the tmpdir is what makes the
|
||||
# file-existence branches reachable: the checks run their real logic, against a
|
||||
# tree this process created. SERVICES_ROOT is deliberately un-defaulted in the
|
||||
# libs, so a test file that forgets this line dies instead of touching /etc.
|
||||
readonly SYSROOT_DIR="${TEST_TMPDIR}/sysroot"
|
||||
export SERVICES_ROOT="${SYSROOT_DIR}"
|
||||
|
||||
mkdir -p \
|
||||
"${SYSROOT_DIR}/usr/bin" \
|
||||
"${SYSROOT_DIR}/usr/local/bin" \
|
||||
"${SYSROOT_DIR}/etc/systemd/system" \
|
||||
"${SYSROOT_DIR}/etc/pacman.d/hooks" \
|
||||
"${SYSROOT_DIR}/var/lib"
|
||||
|
||||
# Create a file under the fixture root, making its parent as needed.
|
||||
# `sysfile etc/hosts 200` writes 200 filler lines, which is how the
|
||||
# StevenBlack-list line-count check is driven.
|
||||
sysfile() { # <relative-path> [line-count]
|
||||
local target="${SYSROOT_DIR}/$1"
|
||||
mkdir -p "$(dirname "$target")"
|
||||
if [[ -n "${2:-}" ]]; then
|
||||
seq 1 "$2" >"$target"
|
||||
else
|
||||
: >"$target"
|
||||
fi
|
||||
}
|
||||
|
||||
# Remove a file or directory from the fixture root.
|
||||
sysrm() { # <relative-path>...
|
||||
local rel
|
||||
for rel in "$@"; do
|
||||
rm -rf "${SYSROOT_DIR:?}/${rel}"
|
||||
done
|
||||
}
|
||||
|
||||
# --- globals the libs read --------------------------------------------------
|
||||
|
||||
@ -223,10 +208,30 @@ make_installer() { # <path>
|
||||
# shellcheck source=../services_common.sh
|
||||
. "${LIB_DIR}/services_common.sh"
|
||||
|
||||
# Stage a fully healthy blocking stack: a long hosts file, the guard-lib trio
|
||||
# active, both pacman hooks, and a resolved.conf that reads /etc/hosts.
|
||||
stage_hosts_ok() {
|
||||
sysfile etc/hosts 200
|
||||
sysfile etc/hosts.stevenblack 200
|
||||
printf '%s\n' "${SERVICES_ROOT}/etc/hosts" >"${DEV}/immutable"
|
||||
printf 'hosts: files resolve dns\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
printf '[Resolve]\nReadEtcHosts=yes\n' >"${SERVICES_ROOT}/etc/systemd/resolved.conf"
|
||||
printf '%s\n' hosts nsswitch resolved >"${DEV}/guard_healthy"
|
||||
sysfile etc/pacman.d/hooks/10-guard-lib-unlock-all.hook
|
||||
sysfile etc/pacman.d/hooks/90-guard-lib-relock-all.hook
|
||||
}
|
||||
|
||||
# reset_state — return every mutable global and every $DEV fact to "nothing has
|
||||
# happened yet", so each test group starts from a known machine.
|
||||
reset_state() {
|
||||
rm -f "${DEV:?}"/* 2>/dev/null || true
|
||||
rm -rf "${SYSROOT_DIR:?}"
|
||||
mkdir -p \
|
||||
"${SYSROOT_DIR}/usr/bin" \
|
||||
"${SYSROOT_DIR}/usr/local/bin" \
|
||||
"${SYSROOT_DIR}/etc/systemd/system" \
|
||||
"${SYSROOT_DIR}/etc/pacman.d/hooks" \
|
||||
"${SYSROOT_DIR}/var/lib"
|
||||
: >"${DEV}/calls"
|
||||
DRY_RUN=0
|
||||
STATUS_ONLY=0
|
||||
@ -234,5 +239,12 @@ reset_state() {
|
||||
FIXES_APPLIED=0
|
||||
SERVICE_STATUS=()
|
||||
MISSING_SCRIPTS=()
|
||||
# Staged fake executables are part of the machine a test describes, so they
|
||||
# reset too. Without this a `present_command discord` leaks into every later
|
||||
# group, which makes a forgetful test pass for the wrong reason.
|
||||
rm -f "${FAKE_BIN:?}"/beeper "${FAKE_BIN}"/signal-desktop "${FAKE_BIN}"/discord \
|
||||
"${FAKE_BIN}"/thorium-browser "${FAKE_BIN}"/chromium \
|
||||
"${FAKE_BIN}"/google-chrome "${FAKE_BIN}"/brave-browser \
|
||||
"${FAKE_BIN}"/VBoxManage
|
||||
}
|
||||
reset_state
|
||||
|
||||
33
linux_configuration/scripts/periodic_background/lib/tests/services_path.sh
Executable file
33
linux_configuration/scripts/periodic_background/lib/tests/services_path.sh
Executable file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/tests/services_path.sh — build the PATH the tests run under.
|
||||
#
|
||||
# Sourced by services_harness.sh once $FAKE_BIN and $TEST_TMPDIR exist. Split
|
||||
# out of the harness to keep both under the repo-wide 250-line cap.
|
||||
|
||||
# `command -v <app>` decides whether the app/browser/VBox checks apply, and it
|
||||
# is a bash builtin that cannot be shimmed. The tests control it by owning PATH:
|
||||
# $FAKE_BIN holds the shims and the staged fake apps, and $REAL_BIN is a
|
||||
# curated directory of symlinks to the genuine coreutils the shims and the
|
||||
# checks themselves need (grep, sed, tail, ...).
|
||||
#
|
||||
# Crucially the machine's own /usr/bin is NOT on PATH, so a really-installed
|
||||
# discord or chromium cannot make "no target apps present" untestable -- which
|
||||
# it did, until these tests caught it. Every symlink is created from an
|
||||
# explicit list and the loop fails loudly on a tool it cannot find, because a
|
||||
# silently-missing tool shows up much later as a `set -e` abort mid-suite with
|
||||
# no message, which cost a debugging round the first time.
|
||||
readonly REAL_BIN="${TEST_TMPDIR}/real_bin"
|
||||
mkdir -p "${REAL_BIN}"
|
||||
|
||||
for real in bash sh env grep egrep awk sed find wc cut tr basename dirname \
|
||||
seq sha256sum cat ls mkdir rmdir rm chmod chown ln readlink realpath \
|
||||
stat sort head tail uniq id getent date mktemp touch cp mv diff xargs \
|
||||
tee expr; do
|
||||
real_path="$(PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" \
|
||||
command -v "$real" 2>/dev/null)" || {
|
||||
printf 'harness: required tool not found on the system: %s\n' "$real" >&2
|
||||
exit 1
|
||||
}
|
||||
ln -sf "$real_path" "${REAL_BIN}/${real}"
|
||||
done
|
||||
unset real real_path
|
||||
143
linux_configuration/scripts/periodic_background/lib/tests/test_services_apps.sh
Executable file
143
linux_configuration/scripts/periodic_background/lib/tests/test_services_apps.sh
Executable file
@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for lib/services_apps.sh — the messaging-app launch wrappers and the
|
||||
# LeechBlock browser extension check.
|
||||
#
|
||||
# check_compulsive_blocker has a three-way branch per app: a wrapper is
|
||||
# installed (.orig backup present), the app is installed but unwrapped, or the
|
||||
# app is absent entirely. `command -v` decides the second vs third case, so the
|
||||
# tests create and remove fake executables on PATH via present_command /
|
||||
# absent_command rather than shimming `command`, which is a bash builtin.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=services_harness.sh
|
||||
. "${SCRIPT_DIR}/services_harness.sh"
|
||||
# shellcheck source=../services_apps.sh
|
||||
. "${SCRIPT_DIR}/../services_apps.sh"
|
||||
|
||||
_t_called_in() { # <haystack> <needle> <what>
|
||||
if [[ "$1" == *"$2"* ]]; then
|
||||
_t_pass "$3"
|
||||
else
|
||||
_t_fail "$3 (missing '$2')"
|
||||
fi
|
||||
}
|
||||
|
||||
TEST_USER="${SUDO_USER:-$USER}"
|
||||
|
||||
echo "== check_compulsive_blocker: no target apps present =="
|
||||
reset_state
|
||||
absent_command beeper signal-desktop discord
|
||||
sysfile usr/local/bin/block-compulsive-opening.sh
|
||||
check_compulsive_blocker >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "No target apps" "an absent app set is reported as not applicable"
|
||||
_t_eq "ok" "$(get_service_status "compulsive_blocker")" "with the blocker installed and no apps, status is ok"
|
||||
|
||||
echo "== check_compulsive_blocker: blocker script missing is an error =="
|
||||
reset_state
|
||||
absent_command beeper signal-desktop discord
|
||||
make_installer "$COMPULSIVE_BLOCK_SCRIPT"
|
||||
check_compulsive_blocker >/dev/null
|
||||
_t_called 'ran block_compulsive_opening.sh install' "a missing blocker script runs the installer with 'install'"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
|
||||
echo "== check_compulsive_blocker: an installed but unwrapped app warns =="
|
||||
reset_state
|
||||
sysfile usr/local/bin/block-compulsive-opening.sh
|
||||
present_command discord
|
||||
absent_command beeper signal-desktop
|
||||
check_compulsive_blocker >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "discord is installed but wrapper not applied" "the unwrapped app is named"
|
||||
_t_eq "warning" "$(get_service_status "compulsive_blocker")" "an unwrapped app is a warning, not an error"
|
||||
_t_not_called 'ran block_compulsive_opening' "a warning does not trigger the installer"
|
||||
|
||||
echo "== check_compulsive_blocker: a wrapped app is accepted =="
|
||||
reset_state
|
||||
sysfile usr/local/bin/block-compulsive-opening.sh
|
||||
present_command discord
|
||||
absent_command beeper signal-desktop
|
||||
sysfile usr/bin/discord.orig
|
||||
check_compulsive_blocker >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "discord wrapper installed" "a backed-up original counts as wrapped"
|
||||
_t_eq "ok" "$(get_service_status "compulsive_blocker")" "a wrapped app leaves status ok"
|
||||
|
||||
echo "== check_compulsive_blocker: a symlinked wrapper without .orig is skipped =="
|
||||
reset_state
|
||||
sysfile usr/local/bin/block-compulsive-opening.sh
|
||||
present_command discord
|
||||
absent_command beeper signal-desktop
|
||||
# A symlink at /usr/bin/<app> with no .orig beside it takes the outer branch but
|
||||
# not the inner one -- neither "wrapped" nor "unwrapped", so nothing is said.
|
||||
ln -sf "${SERVICES_ROOT}/usr/local/bin/block-compulsive-opening.sh" "${SERVICES_ROOT}/usr/bin/discord"
|
||||
check_compulsive_blocker >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "compulsive_blocker")" "a symlink without .orig leaves status ok"
|
||||
|
||||
echo "== check_compulsive_blocker: an unwrapped app plus a missing blocker is an error =="
|
||||
reset_state
|
||||
make_installer "$COMPULSIVE_BLOCK_SCRIPT"
|
||||
present_command discord
|
||||
absent_command beeper signal-desktop
|
||||
check_compulsive_blocker >/dev/null
|
||||
# The error from the missing blocker must not be downgraded by the app warning.
|
||||
_t_eq "error" "$(get_service_status "compulsive_blocker")" "an error is not downgraded by a later warning"
|
||||
_t_called 'ran block_compulsive_opening' "the installer still runs"
|
||||
|
||||
echo "== check_compulsive_blocker: --status and a missing installer =="
|
||||
reset_state
|
||||
absent_command beeper signal-desktop discord
|
||||
make_installer "$COMPULSIVE_BLOCK_SCRIPT"
|
||||
STATUS_ONLY=1
|
||||
check_compulsive_blocker >/dev/null
|
||||
_t_not_called 'ran block_compulsive_opening' "--status never installs"
|
||||
|
||||
reset_state
|
||||
absent_command beeper signal-desktop discord
|
||||
rm -f "$COMPULSIVE_BLOCK_SCRIPT"
|
||||
check_compulsive_blocker >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "a missing installer is recorded"
|
||||
|
||||
echo "== check_leechblock: installed with a desktop entry =="
|
||||
reset_state
|
||||
sysfile "home/${TEST_USER}/.local/share/leechblockng/manifest.json"
|
||||
sysfile "home/${TEST_USER}/.local/share/applications/thorium-leechblock.desktop"
|
||||
check_leechblock >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "LeechBlock desktop entry found" "the desktop entry is found"
|
||||
_t_eq "ok" "$(get_service_status "leechblock")" "a full install records ok"
|
||||
|
||||
echo "== check_leechblock: the capitalised glob is matched too =="
|
||||
reset_state
|
||||
sysfile "home/${TEST_USER}/.local/share/leechblockng/manifest.json"
|
||||
sysfile "home/${TEST_USER}/.local/share/applications/LeechBlock-thorium.desktop"
|
||||
check_leechblock >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "leechblock")" "a LeechBlock-cased entry also counts"
|
||||
|
||||
echo "== check_leechblock: installed but no desktop entry warns =="
|
||||
reset_state
|
||||
sysfile "home/${TEST_USER}/.local/share/leechblockng/manifest.json"
|
||||
mkdir -p "${SERVICES_ROOT}/home/${TEST_USER}/.local/share/applications"
|
||||
check_leechblock >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "No LeechBlock desktop entries found" "the missing entry is reported"
|
||||
_t_eq "warning" "$(get_service_status "leechblock")" "a missing desktop entry is only a warning"
|
||||
_t_not_called 'ran install_leechblock' "a warning does not trigger the installer"
|
||||
|
||||
echo "== check_leechblock: not installed at all runs the installer as the user =="
|
||||
reset_state
|
||||
make_installer "$LEECHBLOCK_SCRIPT"
|
||||
check_leechblock >/dev/null
|
||||
_t_called "sudo -u ${TEST_USER} bash" "the installer runs as the invoking user, not root"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
|
||||
echo "== check_leechblock: --status and a missing installer =="
|
||||
reset_state
|
||||
make_installer "$LEECHBLOCK_SCRIPT"
|
||||
STATUS_ONLY=1
|
||||
check_leechblock >/dev/null
|
||||
_t_not_called 'sudo -u' "--status never installs"
|
||||
|
||||
reset_state
|
||||
rm -f "$LEECHBLOCK_SCRIPT"
|
||||
check_leechblock >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "a missing leechblock installer is recorded"
|
||||
|
||||
_t_summary
|
||||
@ -0,0 +1,183 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for lib/services_browser.sh — the Chromium guest-mode policy check, the
|
||||
# VirtualBox hosts-enforcement check, and the per-user workout locker service.
|
||||
#
|
||||
# All three are gated on something being present: a browser on PATH, VBoxManage
|
||||
# on PATH, a user unit on the user bus. The harness owns PATH outright and the
|
||||
# systemctl shim answers for the user bus, so every gate is drivable here.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=services_harness.sh
|
||||
. "${SCRIPT_DIR}/services_harness.sh"
|
||||
# shellcheck source=../services_browser.sh
|
||||
. "${SCRIPT_DIR}/../services_browser.sh"
|
||||
|
||||
_t_called_in() { # <haystack> <needle> <what>
|
||||
if [[ "$1" == *"$2"* ]]; then
|
||||
_t_pass "$3"
|
||||
else
|
||||
_t_fail "$3 (missing '$2')"
|
||||
fi
|
||||
}
|
||||
|
||||
TEST_USER="${SUDO_USER:-$USER}"
|
||||
BROWSERS=(thorium-browser chromium google-chrome brave-browser)
|
||||
|
||||
# Write a managed-policy JSON carrying the guest-mode key into one browser's
|
||||
# policy directory.
|
||||
stage_policy() { # <policy-dir-relative>
|
||||
sysfile "$1/guest.json"
|
||||
printf '{"BrowserGuestModeEnabled": false}\n' >"${SERVICES_ROOT}/$1/guest.json"
|
||||
}
|
||||
|
||||
echo "== check_guest_mode_removal: no browsers installed is not applicable =="
|
||||
reset_state
|
||||
absent_command "${BROWSERS[@]}"
|
||||
check_guest_mode_removal >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "No Chromium-based browsers detected" "an absent browser set is skipped"
|
||||
_t_eq "ok" "$(get_service_status "guest_mode_removal")" "no browsers leaves status ok"
|
||||
|
||||
echo "== check_guest_mode_removal: a browser with no policy is an error =="
|
||||
reset_state
|
||||
make_installer "$REMOVE_GUEST_MODE_SCRIPT"
|
||||
absent_command "${BROWSERS[@]}"
|
||||
present_command chromium
|
||||
check_guest_mode_removal >/dev/null
|
||||
_t_called 'ran remove_guest_mode' "an unpoliced browser runs the removal script"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
|
||||
echo "== check_guest_mode_removal: each browser's policy dir is honoured =="
|
||||
for dir in etc/chromium/policies/managed etc/opt/chrome/policies/managed \
|
||||
etc/thorium/policies/managed etc/brave/policies/managed; do
|
||||
reset_state
|
||||
make_installer "$REMOVE_GUEST_MODE_SCRIPT"
|
||||
absent_command "${BROWSERS[@]}"
|
||||
present_command chromium
|
||||
stage_policy "$dir"
|
||||
check_guest_mode_removal >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "guest_mode_removal")" "a policy in ${dir%%/policies*} satisfies the check"
|
||||
_t_not_called 'ran remove_guest_mode' "and runs no removal script"
|
||||
done
|
||||
|
||||
echo "== check_guest_mode_removal: a policy dir without the guest key does not count =="
|
||||
reset_state
|
||||
make_installer "$REMOVE_GUEST_MODE_SCRIPT"
|
||||
absent_command "${BROWSERS[@]}"
|
||||
present_command chromium
|
||||
sysfile etc/chromium/policies/managed/unrelated.json
|
||||
printf '{"SomeOtherPolicy": true}\n' >"${SERVICES_ROOT}/etc/chromium/policies/managed/unrelated.json"
|
||||
check_guest_mode_removal >/dev/null
|
||||
_t_called 'ran remove_guest_mode' "a policy file without the guest-mode key is not enough"
|
||||
|
||||
echo "== check_guest_mode_removal: --status and a missing script =="
|
||||
reset_state
|
||||
make_installer "$REMOVE_GUEST_MODE_SCRIPT"
|
||||
absent_command "${BROWSERS[@]}"
|
||||
present_command chromium
|
||||
STATUS_ONLY=1
|
||||
check_guest_mode_removal >/dev/null
|
||||
_t_not_called 'ran remove_guest_mode' "--status never repairs"
|
||||
|
||||
reset_state
|
||||
rm -f "$REMOVE_GUEST_MODE_SCRIPT"
|
||||
absent_command "${BROWSERS[@]}"
|
||||
present_command chromium
|
||||
check_guest_mode_removal >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "a missing removal script is recorded"
|
||||
|
||||
echo "== check_vbox_hosts: VirtualBox absent is n/a, not a pass =="
|
||||
reset_state
|
||||
absent_command VBoxManage
|
||||
check_vbox_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "not applicable" "an absent VirtualBox is reported as not applicable"
|
||||
_t_eq "n/a" "$(get_service_status "vbox_hosts")" "the status is n/a rather than ok"
|
||||
|
||||
echo "== check_vbox_hosts: installed with the marker present =="
|
||||
reset_state
|
||||
present_command VBoxManage
|
||||
sysfile var/lib/vbox-hosts-enforced
|
||||
check_vbox_hosts >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "vbox_hosts")" "an enforced install records ok"
|
||||
|
||||
echo "== check_vbox_hosts: installed without the marker is repaired =="
|
||||
reset_state
|
||||
make_installer "$VBOX_HOSTS_SCRIPT"
|
||||
present_command VBoxManage
|
||||
check_vbox_hosts >/dev/null
|
||||
_t_called 'ran enforce_vbox_hosts' "a missing marker runs the enforcement script"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
|
||||
echo "== check_vbox_hosts: --status and a missing script =="
|
||||
reset_state
|
||||
make_installer "$VBOX_HOSTS_SCRIPT"
|
||||
present_command VBoxManage
|
||||
STATUS_ONLY=1
|
||||
check_vbox_hosts >/dev/null
|
||||
_t_not_called 'ran enforce_vbox_hosts' "--status never repairs"
|
||||
|
||||
reset_state
|
||||
rm -f "$VBOX_HOSTS_SCRIPT"
|
||||
present_command VBoxManage
|
||||
check_vbox_hosts >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "a missing enforcement script is recorded"
|
||||
|
||||
echo "== check_workout_locker: fully healthy =="
|
||||
reset_state
|
||||
: >"$WORKOUT_LOCKER_SCRIPT"
|
||||
printf 'user:workout-locker.service\n' >"${DEV}/enabled"
|
||||
printf 'user:workout-locker.service\n' >"${DEV}/active"
|
||||
check_workout_locker >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "workout_locker")" "an enabled and active user service records ok"
|
||||
_t_called 'systemctl --user --machine=' "the user bus is queried via --machine"
|
||||
|
||||
echo "== check_workout_locker: enabled but inactive is only a warning =="
|
||||
reset_state
|
||||
: >"$WORKOUT_LOCKER_SCRIPT"
|
||||
make_installer "$WORKOUT_LOCKER_INSTALL_SCRIPT"
|
||||
printf 'user:workout-locker.service\n' >"${DEV}/enabled"
|
||||
: >"${DEV}/active"
|
||||
check_workout_locker >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "expected at login time" "an inactive service is explained, not alarmed about"
|
||||
_t_eq "warning" "$(get_service_status "workout_locker")" "inactive alone is a warning"
|
||||
_t_not_called 'sudo -u' "a warning does not trigger the installer"
|
||||
|
||||
echo "== check_workout_locker: not enabled is repaired and re-verified =="
|
||||
reset_state
|
||||
: >"$WORKOUT_LOCKER_SCRIPT"
|
||||
make_installer "$WORKOUT_LOCKER_INSTALL_SCRIPT"
|
||||
: >"${DEV}/enabled"
|
||||
: >"${DEV}/active"
|
||||
check_workout_locker >/dev/null
|
||||
_t_called "sudo -u ${TEST_USER} bash" "the installer runs as the invoking user"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
_t_eq "error" "$(get_service_status "workout_locker")" "a fix that does not enable the unit stays an error"
|
||||
|
||||
echo "== check_workout_locker: a missing screen_lock.py is an error =="
|
||||
reset_state
|
||||
rm -f "$WORKOUT_LOCKER_SCRIPT"
|
||||
make_installer "$WORKOUT_LOCKER_INSTALL_SCRIPT"
|
||||
printf 'user:workout-locker.service\n' >"${DEV}/enabled"
|
||||
printf 'user:workout-locker.service\n' >"${DEV}/active"
|
||||
check_workout_locker >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "screen_lock.py not found" "the missing script is named"
|
||||
_t_called "sudo -u ${TEST_USER} bash" "it triggers a reinstall"
|
||||
|
||||
echo "== check_workout_locker: --status and a missing installer =="
|
||||
reset_state
|
||||
: >"$WORKOUT_LOCKER_SCRIPT"
|
||||
make_installer "$WORKOUT_LOCKER_INSTALL_SCRIPT"
|
||||
STATUS_ONLY=1
|
||||
: >"${DEV}/enabled"
|
||||
check_workout_locker >/dev/null
|
||||
_t_not_called 'sudo -u' "--status never installs"
|
||||
|
||||
reset_state
|
||||
: >"$WORKOUT_LOCKER_SCRIPT"
|
||||
rm -f "$WORKOUT_LOCKER_INSTALL_SCRIPT"
|
||||
: >"${DEV}/enabled"
|
||||
check_workout_locker >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "a missing locker installer is recorded"
|
||||
|
||||
_t_summary
|
||||
@ -103,100 +103,6 @@ rc=0
|
||||
deployment_drift "${TEST_TMPDIR}/absent.sha256" || rc=$?
|
||||
_t_eq "2" "$rc" "a missing manifest is 'unverifiable', not a pass"
|
||||
|
||||
echo "== report_and_fix: an ok service is recorded and left alone =="
|
||||
reset_state
|
||||
declare -a issues=()
|
||||
status="ok"
|
||||
report_and_fix issues status "startup_monitor" "fixing..." "$STARTUP_MONITOR_SCRIPT" "" >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "startup_monitor")" "an ok status is recorded"
|
||||
_t_eq "0" "$ISSUES_FOUND" "an ok service counts no issues"
|
||||
_t_not_called 'ran setup_pc' "an ok service runs no installer"
|
||||
|
||||
echo "== report_and_fix: a warning is reported but never repaired =="
|
||||
reset_state
|
||||
make_installer "$STARTUP_MONITOR_SCRIPT"
|
||||
issues=("timer is not active")
|
||||
status="warning"
|
||||
# Captured to a file rather than `$(...)`: report_and_fix mutates ISSUES_FOUND
|
||||
# and SERVICE_STATUS, and a command substitution would strand both in a
|
||||
# subshell, making the assertions below silently test nothing.
|
||||
report_and_fix issues status "startup_monitor" "fixing..." \
|
||||
"$STARTUP_MONITOR_SCRIPT" "" >"${TEST_TMPDIR}/out.txt"
|
||||
out="$(cat "${TEST_TMPDIR}/out.txt")"
|
||||
_t_called_in "$out" "timer is not active" "the warning text is printed"
|
||||
_t_eq "1" "$ISSUES_FOUND" "a warning counts as an issue"
|
||||
_t_not_called 'ran setup_pc' "a warning does not trigger the installer"
|
||||
_t_eq "warning" "$(get_service_status "startup_monitor")" "the warning status is recorded"
|
||||
|
||||
echo "== report_and_fix: --status reports errors without repairing =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
STATUS_ONLY=1
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." "$PERIODIC_SYSTEM_SCRIPT" "" >/dev/null
|
||||
_t_not_called 'ran setup_periodic' "--status never runs an installer"
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "the error status survives --status"
|
||||
|
||||
echo "== report_and_fix: an error runs the installer and re-verifies =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
printf 'periodic-system-maintenance.timer\n' >"${DEV}/enabled"
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." \
|
||||
"$PERIODIC_SYSTEM_SCRIPT" "periodic-system-maintenance.timer" >/dev/null
|
||||
_t_called 'ran setup_periodic_system' "the installer ran"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
_t_eq "ok" "$(get_service_status "periodic_systems")" "a unit enabled after the fix is promoted to ok"
|
||||
|
||||
echo "== report_and_fix: a fix that does not take leaves the error standing =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
: >"${DEV}/enabled" # the installer ran but the unit is still not enabled
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." \
|
||||
"$PERIODIC_SYSTEM_SCRIPT" "periodic-system-maintenance.timer" >/dev/null
|
||||
_t_called 'ran setup_periodic_system' "the installer ran"
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "an unverified fix does not become ok"
|
||||
|
||||
echo "== report_and_fix: dry-run skips the re-verify =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
printf 'periodic-system-maintenance.timer\n' >"${DEV}/enabled"
|
||||
DRY_RUN=1
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." \
|
||||
"$PERIODIC_SYSTEM_SCRIPT" "periodic-system-maintenance.timer" >/dev/null
|
||||
_t_not_called 'ran setup_periodic_system' "dry-run does not really install"
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "dry-run never claims a fix landed"
|
||||
|
||||
echo "== report_and_fix: a missing installer is a broken-self-repair error =="
|
||||
reset_state
|
||||
rm -f "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "midnight_shutdown" "fixing..." \
|
||||
"$MIDNIGHT_SHUTDOWN_SCRIPT" "day-specific-shutdown.timer" >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "the missing installer is recorded"
|
||||
_t_called 'logger .*MISSING REPAIR SCRIPT' "the missing installer is logged"
|
||||
|
||||
echo "== report_and_fix: extra arguments reach the installer =="
|
||||
reset_state
|
||||
make_installer "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "midnight_shutdown" "fixing..." \
|
||||
"$MIDNIGHT_SHUTDOWN_SCRIPT" "day-specific-shutdown.timer" enable >/dev/null
|
||||
_t_called 'ran setup_midnight_shutdown.sh enable' "the trailing args are forwarded"
|
||||
# report_and_fix rewrites `status` through its nameref; reading it back both
|
||||
# asserts that and gives shellcheck the read it cannot infer through the
|
||||
# nameref (SC2034). The unit is not in $DEV/enabled, so the fix cannot verify.
|
||||
_t_eq "error" "$status" "an unverified fix leaves the caller's status at error"
|
||||
_t_eq "1" "${#issues[@]}" "the caller's issue list is left intact"
|
||||
|
||||
echo "== require_root =="
|
||||
reset_state
|
||||
# require_root ends in `exec sudo`, which would replace this test process, so
|
||||
@ -226,22 +132,27 @@ fi
|
||||
|
||||
echo "== hosts_pacman_hooks_installed =="
|
||||
reset_state
|
||||
# The two hook paths are absolute and root-owned, so this asserts the real
|
||||
# machine's answer rather than a fixture: whichever way it goes, the function
|
||||
# must agree with the filesystem it is reading.
|
||||
if [[ -f /etc/pacman.d/hooks/10-guard-lib-unlock-all.hook &&
|
||||
-f /etc/pacman.d/hooks/90-guard-lib-relock-all.hook ]]; then
|
||||
if hosts_pacman_hooks_installed; then
|
||||
_t_pass "reports the guard-lib hook pair as installed when both exist"
|
||||
else
|
||||
_t_fail "reports the guard-lib hook pair as installed when both exist"
|
||||
fi
|
||||
# Both hooks present.
|
||||
sysfile etc/pacman.d/hooks/10-guard-lib-unlock-all.hook
|
||||
sysfile etc/pacman.d/hooks/90-guard-lib-relock-all.hook
|
||||
if hosts_pacman_hooks_installed; then
|
||||
_t_pass "both guard-lib hooks present reads as installed"
|
||||
else
|
||||
if hosts_pacman_hooks_installed; then
|
||||
_t_fail "reports the hook pair as absent when either is missing"
|
||||
else
|
||||
_t_pass "reports the hook pair as absent when either is missing"
|
||||
fi
|
||||
_t_fail "both guard-lib hooks present reads as installed"
|
||||
fi
|
||||
# Only the unlock hook: a half-installed pair must not read as installed, or
|
||||
# pacman upgrades would relock nothing and leave the guards open.
|
||||
sysrm etc/pacman.d/hooks/90-guard-lib-relock-all.hook
|
||||
if hosts_pacman_hooks_installed; then
|
||||
_t_fail "a missing relock hook reads as not installed"
|
||||
else
|
||||
_t_pass "a missing relock hook reads as not installed"
|
||||
fi
|
||||
sysrm etc/pacman.d/hooks/10-guard-lib-unlock-all.hook
|
||||
if hosts_pacman_hooks_installed; then
|
||||
_t_fail "neither hook present reads as not installed"
|
||||
else
|
||||
_t_pass "neither hook present reads as not installed"
|
||||
fi
|
||||
|
||||
_t_summary
|
||||
|
||||
223
linux_configuration/scripts/periodic_background/lib/tests/test_services_hosts.sh
Executable file
223
linux_configuration/scripts/periodic_background/lib/tests/test_services_hosts.sh
Executable file
@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for lib/services_hosts.sh and lib/services_hosts_fix.sh — the /etc/hosts
|
||||
# blocking stack: the hosts file, its immutable attribute, the three guard-lib
|
||||
# instances, the pacman hook pair, and the systemd-resolved settings that can
|
||||
# silently bypass /etc/hosts.
|
||||
#
|
||||
# This is the highest-stakes check in the family, so the repair half gets its
|
||||
# own cases: nsswitch losing 'files', ReadEtcHosts turning off, DNSOverTLS
|
||||
# turning on, and drop-in overrides appearing. Each of those is a real bypass
|
||||
# that leaves every blocked domain resolvable while the check still finds a
|
||||
# hosts file sitting there.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=services_harness.sh
|
||||
. "${SCRIPT_DIR}/services_harness.sh"
|
||||
# shellcheck source=../services_hosts_fix.sh
|
||||
. "${SCRIPT_DIR}/../services_hosts_fix.sh"
|
||||
# shellcheck source=../services_hosts.sh
|
||||
. "${SCRIPT_DIR}/../services_hosts.sh"
|
||||
|
||||
_t_called_in() { # <haystack> <needle> <what>
|
||||
if [[ "$1" == *"$2"* ]]; then
|
||||
_t_pass "$3"
|
||||
else
|
||||
_t_fail "$3 (missing '$2')"
|
||||
fi
|
||||
}
|
||||
|
||||
echo "== check_hosts: a fully healthy stack records ok =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
out="$(cat "${TEST_TMPDIR}/out.txt")"
|
||||
_t_called_in "$out" "immutable attribute set" "the immutable attribute is recognised"
|
||||
_t_called_in "$out" "guard-lib 'hosts' instance is active" "the hosts guard is recognised"
|
||||
_t_called_in "$out" "Pacman hooks installed" "the hook pair is recognised"
|
||||
_t_eq "ok" "$(get_service_status "hosts")" "a healthy stack records ok"
|
||||
_t_not_called 'ran migrate_hosts_guard' "a healthy stack runs no migration"
|
||||
|
||||
echo "== check_hosts: a short hosts file warns =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
sysfile etc/hosts 10
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "may not be installed" "a short hosts file is reported"
|
||||
_t_called 'ran hosts_install' "a short hosts file is reinstalled"
|
||||
|
||||
echo "== check_hosts: a missing hosts file is an error =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
sysrm etc/hosts
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "/etc/hosts does not exist" "the missing file is reported"
|
||||
_t_called 'ran hosts_install' "it is reinstalled"
|
||||
|
||||
echo "== check_hosts: a mutable hosts file warns =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
: >"${DEV}/immutable"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "not immutable" "a mutable hosts file is reported"
|
||||
|
||||
echo "== check_hosts: a missing StevenBlack cache warns =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
sysrm etc/hosts.stevenblack
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "StevenBlack cache not found" "the missing cache is reported"
|
||||
|
||||
echo "== check_hosts: nsswitch without 'files' is an error and is repaired =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
# Without 'files' the resolver answers first and every /etc/hosts entry is
|
||||
# bypassed -- the whole blocking stack is inert while looking installed.
|
||||
printf 'hosts: resolve dns\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "/etc/hosts is bypassed" "the bypass is called out"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "adding 'files'" "the repair announces itself"
|
||||
_t_called_in "$(grep '^hosts:' "${SERVICES_ROOT}/etc/nsswitch.conf")" "files" "'files' is put back on the hosts line"
|
||||
|
||||
echo "== check_hosts: a missing nsswitch.conf is an error =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
sysrm etc/nsswitch.conf
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "/etc/nsswitch.conf does not exist" "the missing file is reported"
|
||||
|
||||
echo "== check_hosts: an unhealthy guard-lib instance is repaired =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
printf '%s\n' hosts resolved >"${DEV}/guard_healthy"
|
||||
printf 'nsswitch\n' >"${DEV}/guard_degraded"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "'nsswitch' instance is missing or unhealthy" "the degraded instance is named"
|
||||
_t_called 'ran migrate_hosts_guard' "the guard-lib migration is re-run"
|
||||
|
||||
echo "== check_hosts: missing pacman hooks trigger the migration =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
sysrm etc/pacman.d/hooks/90-guard-lib-relock-all.hook
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "Pacman hooks not installed" "the missing hook is reported"
|
||||
_t_called 'ran migrate_hosts_guard' "the migration is re-run to reinstate them"
|
||||
|
||||
echo "== check_hosts: a repaired stack is promoted back to ok =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
# Only the resolver config is wrong; the guard-lib trio and hooks stay healthy,
|
||||
# so the post-repair re-verify passes and the row goes back to ok.
|
||||
printf 'hosts: resolve dns\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
check_hosts >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "hosts")" "a stack that verifies after repair is promoted to ok"
|
||||
|
||||
echo "== check_hosts: --status reports without repairing =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
STATUS_ONLY=1
|
||||
printf 'hosts: resolve dns\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
check_hosts >/dev/null
|
||||
_t_called_in "$(grep '^hosts:' "${SERVICES_ROOT}/etc/nsswitch.conf")" "resolve" "--status leaves nsswitch untouched"
|
||||
if grep -q 'files' "${SERVICES_ROOT}/etc/nsswitch.conf"; then
|
||||
_t_fail "--status applies no repair"
|
||||
else
|
||||
_t_pass "--status applies no repair"
|
||||
fi
|
||||
_t_not_called 'ran migrate_hosts_guard' "--status runs no migration"
|
||||
|
||||
echo "== check_hosts: --dry-run never claims a repair landed =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
DRY_RUN=1
|
||||
printf 'hosts: resolve dns\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "DRY-RUN:" "the repair is announced, not performed"
|
||||
# The re-verify is skipped under --dry-run, so the row must NOT be promoted:
|
||||
# nothing was actually repaired.
|
||||
_t_eq "error" "$(get_service_status "hosts")" "a dry run never promotes the row to ok"
|
||||
|
||||
echo "== check_hosts: missing repair scripts are recorded =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
rm -f "$HOSTS_INSTALL_SCRIPT" "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
sysrm etc/hosts
|
||||
printf '%s\n' hosts resolved >"${DEV}/guard_healthy"
|
||||
printf 'nsswitch\n' >"${DEV}/guard_degraded"
|
||||
check_hosts >/dev/null
|
||||
_t_eq "2" "${#MISSING_SCRIPTS[@]}" "both missing repair scripts are recorded"
|
||||
|
||||
echo "== check_hosts: resolved.conf immutable attribute is recognised =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
printf '%s\n' "${SERVICES_ROOT}/etc/hosts" \
|
||||
"${SERVICES_ROOT}/etc/systemd/resolved.conf" >"${DEV}/immutable"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "resolved.conf has immutable attribute" "the resolved.conf attribute is recognised"
|
||||
|
||||
echo "== check_hosts: each guard-lib instance failing alone is reported =="
|
||||
for inst in hosts nsswitch resolved; do
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
# Everything healthy except this one instance.
|
||||
printf '%s\n' hosts nsswitch resolved | grep -vx "$inst" >"${DEV}/guard_healthy"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "'${inst}' instance is missing or unhealthy" \
|
||||
"a failing '${inst}' guard is named"
|
||||
_t_called 'ran migrate_hosts_guard' "and the migration is re-run for '${inst}'"
|
||||
done
|
||||
|
||||
echo "== check_hosts: a warning must never mask an error =="
|
||||
# Regression test for the status-downgrade bug: several checks assigned
|
||||
# status="warning" unconditionally, so a check running LATER in the function
|
||||
# could overwrite the status="error" an earlier one had set. Because
|
||||
# report_and_fix (and check_hosts' own repair gate) only act on "error", the
|
||||
# downgraded fault was reported and then silently never repaired.
|
||||
#
|
||||
# Here the guard-lib 'hosts' instance is broken (an error) AND the pacman hook
|
||||
# pair is missing (a warning, and checked afterwards). The row must stay
|
||||
# "error" and the repair must run.
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
printf '%s\n' nsswitch resolved >"${DEV}/guard_healthy"
|
||||
sysrm etc/pacman.d/hooks/10-guard-lib-unlock-all.hook
|
||||
sysrm etc/pacman.d/hooks/90-guard-lib-relock-all.hook
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called 'ran migrate_hosts_guard' "a broken guard is still repaired when a later check warns"
|
||||
|
||||
# The same shape with the short-hosts-file warning, which is checked FIRST and
|
||||
# so cannot mask anything -- but the immutable/cache warnings after it can.
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
printf '%s\n' nsswitch resolved >"${DEV}/guard_healthy"
|
||||
: >"${DEV}/immutable"
|
||||
sysrm etc/hosts.stevenblack
|
||||
check_hosts >/dev/null
|
||||
_t_called 'ran migrate_hosts_guard' "a broken guard survives the immutable and cache warnings"
|
||||
|
||||
_t_summary
|
||||
@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for the repair helpers in lib/services_hosts_fix.sh, called directly
|
||||
# rather than through check_hosts.
|
||||
#
|
||||
# Split out of test_services_hosts.sh for the repo-wide 250-line cap. These are
|
||||
# the unit-level cases: each resolver shape hosts_fix_nsswitch has to rewrite,
|
||||
# each resolved.conf shape hosts_fix_resolved has to repair, and the contract
|
||||
# that hosts_repair_all reports failure under --dry-run rather than claiming a
|
||||
# repair that never happened.
|
||||
#
|
||||
# The helpers are called via `if` rather than bare: when resolved.conf carries
|
||||
# no ReadEtcHosts line, the leading grep in the assignment pipeline exits 1 and
|
||||
# `set -o pipefail` makes the whole assignment non-zero, so a bare call would
|
||||
# abort the suite under `set -e`. check_hosts only ever calls them from inside
|
||||
# an `if`, which suppresses `set -e` for the callee, so this is a property of
|
||||
# calling them bare and not a defect.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=services_harness.sh
|
||||
. "${SCRIPT_DIR}/services_harness.sh"
|
||||
# shellcheck source=../services_hosts_fix.sh
|
||||
. "${SCRIPT_DIR}/../services_hosts_fix.sh"
|
||||
|
||||
_t_called_in() { # <haystack> <needle> <what>
|
||||
if [[ "$1" == *"$2"* ]]; then
|
||||
_t_pass "$3"
|
||||
else
|
||||
_t_fail "$3 (missing '$2')"
|
||||
fi
|
||||
}
|
||||
|
||||
echo "== hosts_fix_nsswitch: each resolver shape =="
|
||||
reset_state
|
||||
printf 'hosts: mymachines dns\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
if hosts_fix_nsswitch >/dev/null; then :; fi
|
||||
_t_called_in "$(cat "${SERVICES_ROOT}/etc/nsswitch.conf")" "files dns" "'files' is inserted before dns"
|
||||
|
||||
reset_state
|
||||
printf 'hosts: mymachines myhostname\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
if hosts_fix_nsswitch >/dev/null; then :; fi
|
||||
_t_called_in "$(cat "${SERVICES_ROOT}/etc/nsswitch.conf")" "hosts: files" "'files' is prepended when no resolver is named"
|
||||
|
||||
reset_state
|
||||
printf 'hosts: files resolve\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
if hosts_fix_nsswitch >/dev/null; then :; fi
|
||||
_t_eq "hosts: files resolve" "$(cat "${SERVICES_ROOT}/etc/nsswitch.conf")" "an already-correct line is left alone"
|
||||
|
||||
reset_state
|
||||
sysrm etc/nsswitch.conf
|
||||
if hosts_fix_nsswitch >/dev/null; then :; fi
|
||||
_t_pass "a missing nsswitch.conf is a no-op rather than an error"
|
||||
|
||||
reset_state
|
||||
printf 'passwd: files\n' >"${SERVICES_ROOT}/etc/nsswitch.conf"
|
||||
if hosts_fix_nsswitch >/dev/null; then :; fi
|
||||
_t_pass "an nsswitch.conf with no hosts line at all is a no-op"
|
||||
|
||||
echo "== hosts_fix_resolved: adds the setting when the file lacks it =="
|
||||
# Called through `if` rather than bare. When resolved.conf carries no
|
||||
# ReadEtcHosts line at all, the leading `grep` in the assignment pipeline exits
|
||||
# 1; with `set -o pipefail` that makes the whole assignment non-zero, so a bare
|
||||
# call aborts the test under `set -e`. Production never hits this because
|
||||
# check_hosts only ever calls it from inside an `if`, which suppresses `set -e`
|
||||
# for the callee -- so this is a property of calling it bare, not a defect.
|
||||
reset_state
|
||||
printf '[Resolve]\n' >"${SERVICES_ROOT}/etc/systemd/resolved.conf"
|
||||
if hosts_fix_resolved >/dev/null; then :; fi
|
||||
_t_called_in "$(cat "${SERVICES_ROOT}/etc/systemd/resolved.conf")" "ReadEtcHosts=yes" "the setting is inserted under [Resolve]"
|
||||
|
||||
reset_state
|
||||
printf '# no section here\n' >"${SERVICES_ROOT}/etc/systemd/resolved.conf"
|
||||
if hosts_fix_resolved >/dev/null; then :; fi
|
||||
_t_called_in "$(cat "${SERVICES_ROOT}/etc/systemd/resolved.conf")" "[Resolve]" "a whole [Resolve] section is appended when absent"
|
||||
|
||||
reset_state
|
||||
sysrm etc/systemd/resolved.conf
|
||||
if hosts_fix_resolved >/dev/null; then :; fi
|
||||
_t_pass "a missing resolved.conf is a no-op rather than an error"
|
||||
|
||||
echo "== hosts_repair_all: dry-run refuses to claim success =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
DRY_RUN=1
|
||||
if hosts_repair_all >/dev/null; then
|
||||
_t_fail "hosts_repair_all returns non-zero under --dry-run"
|
||||
else
|
||||
_t_pass "hosts_repair_all returns non-zero under --dry-run"
|
||||
fi
|
||||
|
||||
echo "== hosts_repair_all: a healthy machine verifies after repair =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
if hosts_repair_all >/dev/null; then
|
||||
_t_pass "hosts_repair_all returns zero when the post-repair check passes"
|
||||
else
|
||||
_t_fail "hosts_repair_all returns zero when the post-repair check passes"
|
||||
fi
|
||||
|
||||
_t_summary
|
||||
@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for the systemd-resolved half of check_hosts (lib/services_hosts.sh).
|
||||
#
|
||||
# Split out of test_services_hosts.sh for the repo-wide 250-line cap. Every
|
||||
# case here is a way systemd-resolved can answer a query WITHOUT consulting
|
||||
# /etc/hosts, which leaves the whole blocking stack inert while a correct,
|
||||
# immutable, fully-populated hosts file sits on disk: ReadEtcHosts turned off,
|
||||
# DNSOverTLS turned on, or either re-enabled by a drop-in override.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=services_harness.sh
|
||||
. "${SCRIPT_DIR}/services_harness.sh"
|
||||
# shellcheck source=../services_hosts_fix.sh
|
||||
. "${SCRIPT_DIR}/../services_hosts_fix.sh"
|
||||
# shellcheck source=../services_hosts.sh
|
||||
. "${SCRIPT_DIR}/../services_hosts.sh"
|
||||
|
||||
_t_called_in() { # <haystack> <needle> <what>
|
||||
if [[ "$1" == *"$2"* ]]; then
|
||||
_t_pass "$3"
|
||||
else
|
||||
_t_fail "$3 (missing '$2')"
|
||||
fi
|
||||
}
|
||||
|
||||
echo "== check_hosts: ReadEtcHosts=no is an error and is repaired =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
printf '[Resolve]\nReadEtcHosts=no\n' >"${SERVICES_ROOT}/etc/systemd/resolved.conf"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "bypassed by systemd-resolved" "the resolved bypass is called out"
|
||||
_t_called_in "$(cat "${SERVICES_ROOT}/etc/systemd/resolved.conf")" "ReadEtcHosts=yes" "the setting is repaired"
|
||||
_t_called 'systemctl restart systemd-resolved' "systemd-resolved is restarted so the change takes"
|
||||
|
||||
echo "== check_hosts: DNSOverTLS enabled is an error and is repaired =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
printf '[Resolve]\nReadEtcHosts=yes\nDNSOverTLS=yes\n' >"${SERVICES_ROOT}/etc/systemd/resolved.conf"
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
# The message interpolates $SYSROOT, so match on the stable part of it.
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "DNSOverTLS='yes'" "DNSOverTLS is called out as a bypass"
|
||||
_t_called_in "$(cat "${SERVICES_ROOT}/etc/systemd/resolved.conf")" "#DNSOverTLS=no" "it is commented out"
|
||||
|
||||
echo "== check_hosts: drop-in overrides are an error and are removed =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
make_installer "$HOSTS_INSTALL_SCRIPT"
|
||||
make_installer "$GUARD_LIB_MIGRATE_SCRIPT"
|
||||
# A drop-in can re-enable either bypass without touching resolved.conf, so the
|
||||
# repairs above are not durable while one survives.
|
||||
sysfile etc/systemd/resolved.conf.d/99-override.conf
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "drop-in override" "the drop-in is called out"
|
||||
if [[ -f "${SERVICES_ROOT}/etc/systemd/resolved.conf.d/99-override.conf" ]]; then
|
||||
_t_fail "the drop-in is deleted"
|
||||
else
|
||||
_t_pass "the drop-in is deleted"
|
||||
fi
|
||||
|
||||
echo "== check_hosts: a missing resolved.conf warns =="
|
||||
reset_state
|
||||
stage_hosts_ok
|
||||
sysrm etc/systemd/resolved.conf
|
||||
check_hosts >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "resolved.conf does not exist" "the missing file is reported"
|
||||
|
||||
_t_summary
|
||||
@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for report_and_fix in lib/services_common.sh — the shared
|
||||
# report-then-repair path every unit check funnels through.
|
||||
#
|
||||
# Split out of test_services_common.sh to keep both under the repo-wide
|
||||
# 250-line cap. The branching it covers: status ok vs warning vs error,
|
||||
# STATUS_ONLY on/off, DRY_RUN on/off, the installer being present or missing,
|
||||
# and the post-fix re-verify that can promote a status back to "ok".
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=services_harness.sh
|
||||
. "${SCRIPT_DIR}/services_harness.sh"
|
||||
|
||||
_t_called_in() { # <haystack> <needle> <what>
|
||||
if [[ "$1" == *"$2"* ]]; then
|
||||
_t_pass "$3"
|
||||
else
|
||||
_t_fail "$3 (missing '$2')"
|
||||
fi
|
||||
}
|
||||
|
||||
declare -a issues=()
|
||||
status="ok"
|
||||
|
||||
echo "== report_and_fix: an ok service is recorded and left alone =="
|
||||
reset_state
|
||||
declare -a issues=()
|
||||
status="ok"
|
||||
report_and_fix issues status "startup_monitor" "fixing..." "$STARTUP_MONITOR_SCRIPT" "" >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "startup_monitor")" "an ok status is recorded"
|
||||
_t_eq "0" "$ISSUES_FOUND" "an ok service counts no issues"
|
||||
_t_not_called 'ran setup_pc' "an ok service runs no installer"
|
||||
|
||||
echo "== report_and_fix: a warning is reported but never repaired =="
|
||||
reset_state
|
||||
make_installer "$STARTUP_MONITOR_SCRIPT"
|
||||
issues=("timer is not active")
|
||||
status="warning"
|
||||
# Captured to a file rather than `$(...)`: report_and_fix mutates ISSUES_FOUND
|
||||
# and SERVICE_STATUS, and a command substitution would strand both in a
|
||||
# subshell, making the assertions below silently test nothing.
|
||||
report_and_fix issues status "startup_monitor" "fixing..." \
|
||||
"$STARTUP_MONITOR_SCRIPT" "" >"${TEST_TMPDIR}/out.txt"
|
||||
out="$(cat "${TEST_TMPDIR}/out.txt")"
|
||||
_t_called_in "$out" "timer is not active" "the warning text is printed"
|
||||
_t_eq "1" "$ISSUES_FOUND" "a warning counts as an issue"
|
||||
_t_not_called 'ran setup_pc' "a warning does not trigger the installer"
|
||||
_t_eq "warning" "$(get_service_status "startup_monitor")" "the warning status is recorded"
|
||||
|
||||
echo "== report_and_fix: --status reports errors without repairing =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
STATUS_ONLY=1
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." "$PERIODIC_SYSTEM_SCRIPT" "" >/dev/null
|
||||
_t_not_called 'ran setup_periodic' "--status never runs an installer"
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "the error status survives --status"
|
||||
|
||||
echo "== report_and_fix: an error runs the installer and re-verifies =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
printf 'periodic-system-maintenance.timer\n' >"${DEV}/enabled"
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." \
|
||||
"$PERIODIC_SYSTEM_SCRIPT" "periodic-system-maintenance.timer" >/dev/null
|
||||
_t_called 'ran setup_periodic_system' "the installer ran"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
_t_eq "ok" "$(get_service_status "periodic_systems")" "a unit enabled after the fix is promoted to ok"
|
||||
|
||||
echo "== report_and_fix: a fix that does not take leaves the error standing =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
: >"${DEV}/enabled" # the installer ran but the unit is still not enabled
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." \
|
||||
"$PERIODIC_SYSTEM_SCRIPT" "periodic-system-maintenance.timer" >/dev/null
|
||||
_t_called 'ran setup_periodic_system' "the installer ran"
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "an unverified fix does not become ok"
|
||||
|
||||
echo "== report_and_fix: dry-run skips the re-verify =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
printf 'periodic-system-maintenance.timer\n' >"${DEV}/enabled"
|
||||
DRY_RUN=1
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "periodic_systems" "fixing..." \
|
||||
"$PERIODIC_SYSTEM_SCRIPT" "periodic-system-maintenance.timer" >/dev/null
|
||||
_t_not_called 'ran setup_periodic_system' "dry-run does not really install"
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "dry-run never claims a fix landed"
|
||||
|
||||
echo "== report_and_fix: a missing installer is a broken-self-repair error =="
|
||||
reset_state
|
||||
rm -f "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "midnight_shutdown" "fixing..." \
|
||||
"$MIDNIGHT_SHUTDOWN_SCRIPT" "day-specific-shutdown.timer" >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "the missing installer is recorded"
|
||||
_t_called 'logger .*MISSING REPAIR SCRIPT' "the missing installer is logged"
|
||||
|
||||
echo "== report_and_fix: extra arguments reach the installer =="
|
||||
reset_state
|
||||
make_installer "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
issues=("timer is not enabled")
|
||||
status="error"
|
||||
report_and_fix issues status "midnight_shutdown" "fixing..." \
|
||||
"$MIDNIGHT_SHUTDOWN_SCRIPT" "day-specific-shutdown.timer" enable >/dev/null
|
||||
_t_called 'ran setup_midnight_shutdown.sh enable' "the trailing args are forwarded"
|
||||
# report_and_fix rewrites `status` through its nameref; reading it back both
|
||||
# asserts that and gives shellcheck the read it cannot infer through the
|
||||
# nameref (SC2034). The unit is not in $DEV/enabled, so the fix cannot verify.
|
||||
_t_eq "error" "$status" "an unverified fix leaves the caller's status at error"
|
||||
_t_eq "1" "${#issues[@]}" "the caller's issue list is left intact"
|
||||
|
||||
_t_summary
|
||||
@ -1,11 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for lib/services_units.sh — the three systemd timer/service checks.
|
||||
#
|
||||
# The unit-state half of each check is fully drivable through the systemctl
|
||||
# shim ($DEV/enabled, $DEV/active). The file-existence half probes absolute
|
||||
# root-owned paths (/etc/systemd/system/*.service, /usr/local/bin/*.sh) that no
|
||||
# shim can redirect, so those branches take whichever way this machine is
|
||||
# actually configured; the assertions below are written to hold either way.
|
||||
# Unit state comes from the systemctl shim ($DEV/enabled, $DEV/active); the
|
||||
# service files and manager scripts are real files under $SYSROOT, staged with
|
||||
# sysfile. Both halves of every branch are therefore reachable without touching
|
||||
# the machine this runs on.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
@ -15,53 +14,103 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# shellcheck source=../services_units.sh
|
||||
. "${SCRIPT_DIR}/../services_units.sh"
|
||||
|
||||
echo "== check_midnight_shutdown: unit enabled and active =="
|
||||
# Stage the on-disk half of a healthy midnight-shutdown install.
|
||||
stage_midnight_files() {
|
||||
sysfile etc/systemd/system/day-specific-shutdown.service
|
||||
sysfile usr/local/bin/day-specific-shutdown-manager.sh
|
||||
}
|
||||
|
||||
echo "== check_midnight_shutdown: fully healthy records ok =="
|
||||
reset_state
|
||||
make_installer "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
stage_midnight_files
|
||||
printf 'day-specific-shutdown.timer\n' >"${DEV}/enabled"
|
||||
printf 'day-specific-shutdown.timer\n' >"${DEV}/active"
|
||||
check_midnight_shutdown >/dev/null
|
||||
_t_called 'systemctl is-enabled day-specific-shutdown.timer' "the timer's enabled state is queried"
|
||||
_t_called 'systemctl is-active day-specific-shutdown.timer' "the timer's active state is queried"
|
||||
_t_eq "ok" "$(get_service_status "midnight_shutdown")" "a fully healthy install records ok"
|
||||
_t_not_called 'ran setup_midnight_shutdown' "a healthy install runs no setup script"
|
||||
|
||||
echo "== check_midnight_shutdown: a missing service file is an error and is repaired =="
|
||||
reset_state
|
||||
make_installer "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
sysfile usr/local/bin/day-specific-shutdown-manager.sh
|
||||
printf 'day-specific-shutdown.timer\n' >"${DEV}/enabled"
|
||||
printf 'day-specific-shutdown.timer\n' >"${DEV}/active"
|
||||
check_midnight_shutdown >/dev/null
|
||||
_t_called 'ran setup_midnight_shutdown.sh enable' "a missing unit file runs the setup script with 'enable'"
|
||||
_t_eq "1" "$FIXES_APPLIED" "the fix is counted"
|
||||
|
||||
echo "== check_midnight_shutdown: a missing manager script is an error =="
|
||||
reset_state
|
||||
make_installer "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
sysfile etc/systemd/system/day-specific-shutdown.service
|
||||
printf 'day-specific-shutdown.timer\n' >"${DEV}/enabled"
|
||||
printf 'day-specific-shutdown.timer\n' >"${DEV}/active"
|
||||
check_midnight_shutdown >/dev/null
|
||||
_t_called 'ran setup_midnight_shutdown' "a missing manager script runs the setup script"
|
||||
|
||||
echo "== check_midnight_shutdown: a disabled timer is downgraded to warning =="
|
||||
reset_state
|
||||
make_installer "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
stage_midnight_files
|
||||
: >"${DEV}/enabled"
|
||||
: >"${DEV}/active"
|
||||
check_midnight_shutdown >/dev/null
|
||||
# LATENT BUG, pinned deliberately rather than fixed here: the is-active check
|
||||
# assigns status="warning" unconditionally, so it overwrites the "error" the
|
||||
# is-enabled check set immediately above. A timer that is BOTH disabled and
|
||||
# inactive therefore ends up "warning", and report_and_fix only repairs on
|
||||
# "error" -- so the timer is never re-enabled. Identical in the pre-split file
|
||||
# (verified against rev 6691cb26), so this split neither caused nor hides it.
|
||||
# Fixing it changes enforcement behaviour and belongs in its own commit.
|
||||
_t_eq "warning" "$(get_service_status "midnight_shutdown")" "disabled+inactive is (wrongly) only a warning"
|
||||
_t_not_called 'ran setup_midnight_shutdown' "so the disabled timer is never repaired"
|
||||
# Regression test. The is-active check used to assign status="warning"
|
||||
# unconditionally, overwriting the "error" the is-enabled check set just above;
|
||||
# since report_and_fix only repairs on "error", a timer that was BOTH disabled
|
||||
# and inactive -- the normal shape of a genuinely broken timer -- was reported
|
||||
# and then never re-enabled.
|
||||
_t_eq "error" "$(get_service_status "midnight_shutdown")" "disabled+inactive stays an error"
|
||||
_t_called 'ran setup_midnight_shutdown.sh enable' "so the disabled timer IS repaired"
|
||||
|
||||
echo "== check_midnight_shutdown: --status reports without repairing =="
|
||||
reset_state
|
||||
make_installer "$MIDNIGHT_SHUTDOWN_SCRIPT"
|
||||
stage_midnight_files
|
||||
STATUS_ONLY=1
|
||||
: >"${DEV}/enabled"
|
||||
check_midnight_shutdown >/dev/null
|
||||
_t_not_called 'ran setup_midnight_shutdown' "--status never runs the setup script"
|
||||
_t_eq "warning" "$(get_service_status "midnight_shutdown")" "the verdict is still recorded"
|
||||
_t_eq "error" "$(get_service_status "midnight_shutdown")" "the error is still recorded"
|
||||
|
||||
echo "== check_startup_monitor: timer disabled triggers the installer =="
|
||||
echo "== check_startup_monitor: fully healthy records ok =="
|
||||
reset_state
|
||||
make_installer "$STARTUP_MONITOR_SCRIPT"
|
||||
sysfile etc/systemd/system/pc-startup-monitor.service
|
||||
sysfile usr/local/bin/pc-startup-check.sh
|
||||
printf 'pc-startup-monitor.timer\n' >"${DEV}/enabled"
|
||||
printf 'pc-startup-monitor.timer\n' >"${DEV}/active"
|
||||
check_startup_monitor >/dev/null
|
||||
_t_eq "ok" "$(get_service_status "startup_monitor")" "a fully healthy install records ok"
|
||||
|
||||
echo "== check_startup_monitor: missing files are errors and are repaired =="
|
||||
reset_state
|
||||
make_installer "$STARTUP_MONITOR_SCRIPT"
|
||||
printf 'pc-startup-monitor.timer\n' >"${DEV}/enabled"
|
||||
printf 'pc-startup-monitor.timer\n' >"${DEV}/active"
|
||||
check_startup_monitor >/dev/null
|
||||
_t_called 'ran setup_pc_startup_monitor' "missing files run the setup script"
|
||||
|
||||
echo "== check_startup_monitor: timer disabled is downgraded to warning =="
|
||||
reset_state
|
||||
make_installer "$STARTUP_MONITOR_SCRIPT"
|
||||
sysfile etc/systemd/system/pc-startup-monitor.service
|
||||
sysfile usr/local/bin/pc-startup-check.sh
|
||||
: >"${DEV}/enabled"
|
||||
: >"${DEV}/active"
|
||||
check_startup_monitor >/dev/null
|
||||
# Same latent downgrade as check_midnight_shutdown above.
|
||||
_t_eq "warning" "$(get_service_status "startup_monitor")" "disabled+inactive is (wrongly) only a warning"
|
||||
_t_not_called 'ran setup_pc_startup_monitor' "so the disabled timer is never repaired"
|
||||
# Same regression as check_midnight_shutdown above.
|
||||
_t_eq "error" "$(get_service_status "startup_monitor")" "disabled+inactive stays an error"
|
||||
_t_called 'ran setup_pc_startup_monitor' "so the disabled timer IS repaired"
|
||||
|
||||
echo "== check_startup_monitor: enabled but inactive is a warning, not a fix =="
|
||||
reset_state
|
||||
make_installer "$STARTUP_MONITOR_SCRIPT"
|
||||
sysfile etc/systemd/system/pc-startup-monitor.service
|
||||
sysfile usr/local/bin/pc-startup-check.sh
|
||||
printf 'pc-startup-monitor.timer\n' >"${DEV}/enabled"
|
||||
: >"${DEV}/active"
|
||||
check_startup_monitor >/dev/null
|
||||
@ -77,27 +126,35 @@ printf '%s\n' \
|
||||
printf '%s\n' \
|
||||
'periodic-system-maintenance.timer' \
|
||||
'hosts-file-monitor.service' >"${DEV}/active"
|
||||
sysfile usr/local/bin/periodic-system-maintenance.sh
|
||||
check_periodic_systems >/dev/null
|
||||
# The maintenance script probe is an absolute path this test cannot stage, so
|
||||
# the run is "ok" only on a machine that really has it. Assert the invariant
|
||||
# that holds either way: no repair is attempted unless something reported bad.
|
||||
if [[ -f /usr/local/bin/periodic-system-maintenance.sh ]]; then
|
||||
_t_eq "ok" "$(get_service_status "periodic_systems")" "a fully healthy stack records ok"
|
||||
_t_not_called 'ran setup_periodic_system' "a healthy stack runs no installer"
|
||||
else
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "a missing maintenance script is an error"
|
||||
fi
|
||||
_t_eq "ok" "$(get_service_status "periodic_systems")" "a fully healthy stack records ok"
|
||||
_t_not_called 'ran setup_periodic_system' "a healthy stack runs no installer"
|
||||
|
||||
echo "== check_periodic_systems: a missing maintenance script is repaired =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
printf '%s\n' \
|
||||
'periodic-system-maintenance.timer' \
|
||||
'periodic-system-startup.service' \
|
||||
'hosts-file-monitor.service' >"${DEV}/enabled"
|
||||
printf '%s\n' \
|
||||
'periodic-system-maintenance.timer' \
|
||||
'hosts-file-monitor.service' >"${DEV}/active"
|
||||
check_periodic_systems >/dev/null
|
||||
_t_called 'ran setup_periodic_system' "a missing maintenance script runs the setup script"
|
||||
|
||||
echo "== check_periodic_systems: a disabled unit is repaired =="
|
||||
reset_state
|
||||
make_installer "$PERIODIC_SYSTEM_SCRIPT"
|
||||
sysfile usr/local/bin/periodic-system-maintenance.sh
|
||||
: >"${DEV}/enabled"
|
||||
: >"${DEV}/active"
|
||||
check_periodic_systems >/dev/null
|
||||
# hosts-file-monitor.service's is-active check is the last status assignment
|
||||
# here, and it too downgrades unconditionally -- same bug, same pinning.
|
||||
_t_eq "warning" "$(get_service_status "periodic_systems")" "a fully disabled stack is (wrongly) only a warning"
|
||||
_t_not_called 'ran setup_periodic_system' "so the disabled stack is never repaired"
|
||||
# hosts-file-monitor.service's is-active check is the LAST status assignment in
|
||||
# this function, so before the fix it downgraded everything above it.
|
||||
_t_eq "error" "$(get_service_status "periodic_systems")" "a fully disabled stack stays an error"
|
||||
_t_called 'ran setup_periodic_system' "so the disabled stack IS repaired"
|
||||
|
||||
echo "== check_periodic_systems: startup service disabled alone still repairs =="
|
||||
reset_state
|
||||
@ -108,6 +165,7 @@ printf '%s\n' \
|
||||
printf '%s\n' \
|
||||
'periodic-system-maintenance.timer' \
|
||||
'hosts-file-monitor.service' >"${DEV}/active"
|
||||
sysfile usr/local/bin/periodic-system-maintenance.sh
|
||||
check_periodic_systems >/dev/null
|
||||
# This one DOES repair: hosts-file-monitor is active, so the final downgrade
|
||||
# never fires and the "error" from the disabled startup service survives.
|
||||
|
||||
@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for lib/services_wrappers.sh — the pacman and makepkg wrapper checks.
|
||||
#
|
||||
# Both checks assert four independent things: the /usr/bin symlink points at
|
||||
# the wrapper, the .orig backup exists, the installed files exist, and the
|
||||
# install-time drift manifest still verifies. The last one is the reason these
|
||||
# checks exist at all — on 2026-08-03 a week-stale wrapper passed every
|
||||
# existence check while the manifest would have caught it — so drift gets a
|
||||
# case for each of its three outcomes (verified, drifted, no manifest).
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# shellcheck source=services_harness.sh
|
||||
. "${SCRIPT_DIR}/services_harness.sh"
|
||||
# shellcheck source=../services_wrappers.sh
|
||||
. "${SCRIPT_DIR}/../services_wrappers.sh"
|
||||
|
||||
# Stage a fully healthy pacman wrapper install inside the fixture root, with a
|
||||
# drift manifest that verifies.
|
||||
stage_pacman_ok() {
|
||||
sysfile usr/local/bin/pacman_wrapper
|
||||
sysfile usr/bin/pacman.orig
|
||||
ln -sf "${SERVICES_ROOT}/usr/local/bin/pacman_wrapper" "${SERVICES_ROOT}/usr/bin/pacman"
|
||||
sysfile usr/local/bin/words.txt
|
||||
sysfile usr/local/bin/pacman_blocked_keywords.txt
|
||||
sysfile usr/local/bin/pacman_whitelist.txt
|
||||
(cd "${SERVICES_ROOT}" && sha256sum usr/local/bin/pacman_wrapper >"$PACMAN_WRAPPER_MANIFEST")
|
||||
}
|
||||
|
||||
# Same for makepkg. The makepkg check additionally wants a shared lock library,
|
||||
# a rewrap helper and the upgrade-survival pacman hook.
|
||||
stage_makepkg_ok() {
|
||||
sysfile usr/local/bin/makepkg_wrapper
|
||||
sysfile usr/bin/makepkg.orig
|
||||
ln -sf "${SERVICES_ROOT}/usr/local/bin/makepkg_wrapper" "${SERVICES_ROOT}/usr/bin/makepkg"
|
||||
sysfile usr/local/bin/pacman_lock_lib.sh
|
||||
sysfile usr/local/bin/rewrap_pkg_managers.sh
|
||||
sysfile etc/pacman.d/hooks/96-restore-pkg-wrappers.hook
|
||||
(cd "${SERVICES_ROOT}" && sha256sum usr/local/bin/makepkg_wrapper >"$MAKEPKG_WRAPPER_MANIFEST")
|
||||
}
|
||||
|
||||
# check_pacman_wrapper runs its manifest through `sha256sum -c`, whose paths are
|
||||
# relative to the cwd, so every call is made from the fixture root.
|
||||
#
|
||||
# Deliberately NOT `(cd ... && check)`: the checks mutate SERVICE_STATUS,
|
||||
# ISSUES_FOUND and MISSING_SCRIPTS, and a subshell would strand every one of
|
||||
# those, leaving the assertions below silently testing nothing.
|
||||
run_pacman_check() {
|
||||
local prev="$PWD"
|
||||
cd "${SERVICES_ROOT}" || return 1
|
||||
check_pacman_wrapper
|
||||
local rc=$?
|
||||
cd "$prev" || return 1
|
||||
return $rc
|
||||
}
|
||||
|
||||
run_makepkg_check() {
|
||||
local prev="$PWD"
|
||||
cd "${SERVICES_ROOT}" || return 1
|
||||
check_makepkg_wrapper
|
||||
local rc=$?
|
||||
cd "$prev" || return 1
|
||||
return $rc
|
||||
}
|
||||
|
||||
echo "== check_pacman_wrapper: a healthy install records ok =="
|
||||
reset_state
|
||||
stage_pacman_ok
|
||||
run_pacman_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in() { # <haystack> <needle> <what>
|
||||
if [[ "$1" == *"$2"* ]]; then
|
||||
_t_pass "$3"
|
||||
else
|
||||
_t_fail "$3 (missing '$2')"
|
||||
fi
|
||||
}
|
||||
out="$(cat "${TEST_TMPDIR}/out.txt")"
|
||||
_t_called_in "$out" "Pacman symlink points to wrapper" "the symlink is accepted"
|
||||
_t_called_in "$out" "no drift" "a verifying manifest reports no drift"
|
||||
_t_not_called 'ran install_pacman_wrapper' "a healthy install runs no installer"
|
||||
|
||||
echo "== check_pacman_wrapper: pacman not a symlink =="
|
||||
reset_state
|
||||
make_installer "$PACMAN_WRAPPER_INSTALL"
|
||||
stage_pacman_ok
|
||||
rm -f "${SERVICES_ROOT}/usr/bin/pacman"
|
||||
sysfile usr/bin/pacman # a real file, not a link
|
||||
run_pacman_check >/dev/null
|
||||
_t_called 'ran install_pacman_wrapper' "a missing wrapper symlink runs the installer"
|
||||
|
||||
echo "== check_pacman_wrapper: symlink points somewhere else =="
|
||||
reset_state
|
||||
make_installer "$PACMAN_WRAPPER_INSTALL"
|
||||
stage_pacman_ok
|
||||
sysfile usr/local/bin/impostor
|
||||
ln -sf "${SERVICES_ROOT}/usr/local/bin/impostor" "${SERVICES_ROOT}/usr/bin/pacman"
|
||||
run_pacman_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "expected" "a wrong symlink target names the expected path"
|
||||
_t_called 'ran install_pacman_wrapper' "a wrong symlink target runs the installer"
|
||||
|
||||
echo "== check_pacman_wrapper: missing .orig backup =="
|
||||
reset_state
|
||||
make_installer "$PACMAN_WRAPPER_INSTALL"
|
||||
stage_pacman_ok
|
||||
sysrm usr/bin/pacman.orig
|
||||
run_pacman_check >/dev/null
|
||||
_t_called 'ran install_pacman_wrapper' "a missing .orig backup runs the installer"
|
||||
|
||||
echo "== check_pacman_wrapper: missing supporting files only warn =="
|
||||
reset_state
|
||||
stage_pacman_ok
|
||||
sysrm usr/local/bin/words.txt usr/local/bin/pacman_whitelist.txt
|
||||
run_pacman_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "Supporting file missing" "a missing supporting file is reported"
|
||||
_t_not_called 'ran install_pacman_wrapper' "but it does not trigger a reinstall"
|
||||
|
||||
echo "== check_pacman_wrapper: content drift is an error even when files exist =="
|
||||
reset_state
|
||||
make_installer "$PACMAN_WRAPPER_INSTALL"
|
||||
stage_pacman_ok
|
||||
# Every existence check still passes; only the content changed. This is the
|
||||
# exact shape of the 2026-08-03 stale-wrapper incident.
|
||||
printf 'tampered\n' >"${SERVICES_ROOT}/usr/local/bin/pacman_wrapper"
|
||||
run_pacman_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "stale or tampered" "drift is reported despite every file being present"
|
||||
_t_called 'ran install_pacman_wrapper' "drift triggers a reinstall"
|
||||
|
||||
echo "== check_pacman_wrapper: an absent manifest is unverifiable, not a pass =="
|
||||
reset_state
|
||||
make_installer "$PACMAN_WRAPPER_INSTALL"
|
||||
stage_pacman_ok
|
||||
rm -f "$PACMAN_WRAPPER_MANIFEST"
|
||||
run_pacman_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "unverifiable" "a missing manifest is called out"
|
||||
_t_called 'ran install_pacman_wrapper' "a missing manifest triggers a reinstall"
|
||||
|
||||
echo "== check_pacman_wrapper: --status reports without repairing =="
|
||||
reset_state
|
||||
make_installer "$PACMAN_WRAPPER_INSTALL"
|
||||
STATUS_ONLY=1
|
||||
run_pacman_check >/dev/null
|
||||
_t_not_called 'ran install_pacman_wrapper' "--status never installs"
|
||||
_t_eq "error" "$(get_service_status "pacman_wrapper")" "the error is still recorded"
|
||||
|
||||
echo "== check_pacman_wrapper: a missing installer is a broken-self-repair error =="
|
||||
reset_state
|
||||
rm -f "$PACMAN_WRAPPER_INSTALL"
|
||||
run_pacman_check >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "the missing installer is recorded"
|
||||
|
||||
echo "== check_makepkg_wrapper: a healthy install records ok =="
|
||||
reset_state
|
||||
stage_makepkg_ok
|
||||
run_makepkg_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "Makepkg symlink points to wrapper" "the symlink is accepted"
|
||||
_t_eq "ok" "$(get_service_status "makepkg_wrapper")" "a healthy install records ok"
|
||||
|
||||
echo "== check_makepkg_wrapper: each missing piece triggers a reinstall =="
|
||||
for missing in usr/bin/makepkg.orig usr/local/bin/pacman_lock_lib.sh \
|
||||
usr/local/bin/rewrap_pkg_managers.sh etc/pacman.d/hooks/96-restore-pkg-wrappers.hook; do
|
||||
reset_state
|
||||
make_installer "$MAKEPKG_WRAPPER_INSTALL"
|
||||
stage_makepkg_ok
|
||||
sysrm "$missing"
|
||||
run_makepkg_check >/dev/null
|
||||
_t_called 'ran install_makepkg_wrapper' "a missing ${missing##*/} runs the installer"
|
||||
done
|
||||
|
||||
echo "== check_makepkg_wrapper: not a symlink =="
|
||||
reset_state
|
||||
make_installer "$MAKEPKG_WRAPPER_INSTALL"
|
||||
stage_makepkg_ok
|
||||
rm -f "${SERVICES_ROOT}/usr/bin/makepkg"
|
||||
sysfile usr/bin/makepkg
|
||||
run_makepkg_check >/dev/null
|
||||
_t_called 'ran install_makepkg_wrapper' "a missing wrapper symlink runs the installer"
|
||||
|
||||
echo "== check_makepkg_wrapper: symlink points somewhere else =="
|
||||
reset_state
|
||||
make_installer "$MAKEPKG_WRAPPER_INSTALL"
|
||||
stage_makepkg_ok
|
||||
sysfile usr/local/bin/impostor
|
||||
ln -sf "${SERVICES_ROOT}/usr/local/bin/impostor" "${SERVICES_ROOT}/usr/bin/makepkg"
|
||||
run_makepkg_check >/dev/null
|
||||
_t_called 'ran install_makepkg_wrapper' "a wrong symlink target runs the installer"
|
||||
|
||||
echo "== check_makepkg_wrapper: wrapper file missing =="
|
||||
reset_state
|
||||
make_installer "$MAKEPKG_WRAPPER_INSTALL"
|
||||
stage_makepkg_ok
|
||||
sysrm usr/local/bin/makepkg_wrapper
|
||||
run_makepkg_check >/dev/null
|
||||
_t_called 'ran install_makepkg_wrapper' "a missing wrapper script runs the installer"
|
||||
|
||||
echo "== check_makepkg_wrapper: drift and absent manifest =="
|
||||
reset_state
|
||||
make_installer "$MAKEPKG_WRAPPER_INSTALL"
|
||||
stage_makepkg_ok
|
||||
printf 'tampered\n' >"${SERVICES_ROOT}/usr/local/bin/makepkg_wrapper"
|
||||
run_makepkg_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "stale or tampered" "makepkg drift is reported"
|
||||
|
||||
reset_state
|
||||
make_installer "$MAKEPKG_WRAPPER_INSTALL"
|
||||
stage_makepkg_ok
|
||||
rm -f "$MAKEPKG_WRAPPER_MANIFEST"
|
||||
run_makepkg_check >"${TEST_TMPDIR}/out.txt"
|
||||
_t_called_in "$(cat "${TEST_TMPDIR}/out.txt")" "unverifiable" "an absent makepkg manifest is called out"
|
||||
|
||||
echo "== check_makepkg_wrapper: --status and a missing installer =="
|
||||
reset_state
|
||||
make_installer "$MAKEPKG_WRAPPER_INSTALL"
|
||||
STATUS_ONLY=1
|
||||
run_makepkg_check >/dev/null
|
||||
_t_not_called 'ran install_makepkg_wrapper' "--status never installs"
|
||||
|
||||
reset_state
|
||||
rm -f "$MAKEPKG_WRAPPER_INSTALL"
|
||||
run_makepkg_check >/dev/null
|
||||
_t_eq "1" "${#MISSING_SCRIPTS[@]}" "the missing makepkg installer is recorded"
|
||||
|
||||
_t_summary
|
||||
Loading…
Reference in New Issue
Block a user