#!/bin/bash ## Tests for the goal primitives: ensure_file, ensure_link, ensure_block, write_knob. ## Each is exercised through `pave run`, the way goal files use them. # shellcheck source=tests/testlib.bash source "${BASH_SOURCE[0]%/*}/testlib.bash" G="$scratch/goals" T="$scratch/t" mkdir -p "$T" ## converge writes a manual-phase goal file and runs it. ## ## - @stdin the goal file. converge() { goals "$G/manual.bash" pv run "$G" manual } # ---- ensure_file ---- converge <"$T/deep/er/f" pv run "$G" manual assert_has "ensure_file: an extra trailing newline is drift" "pave: changed: rewrote $T/deep/er/f" chmod 600 "$T/deep/er/f" pv run "$G" manual assert_has "ensure_file: mode drift fixed alone" "pave: changed: chmod 640 $T/deep/er/f (was 600)" assert_count "ensure_file: mode drift doesn't rewrite" "rewrote" 0 mkdir "$T/adir" ln -s elsewhere "$T/alink" converge < ../target" assert_eq "ensure_link: exact target text" ../target "$(readlink "$T/links/l")" pv run "$G" manual assert_eq "ensure_link: converged is quiet" "" "$out" ln -sfn /wrong "$T/links/l" pv run "$G" manual assert_has "ensure_link: repoint reported" "pave: changed: repointed $T/links/l -> ../target (was /wrong)" assert_eq "ensure_link: repointed" ../target "$(readlink "$T/links/l")" touch "$T/plainfile" converge <"$B" chmod 751 "$B" converge <>> pave:first >>>\necho one\necho two\n# <<< pave:first <<<\necho existing\n\nexit 0' "$(<"$B")" assert_eq "ensure_block: mode kept" 751 "$(stat -c %a "$B")" snap=$(exact "$B") pv run "$G" manual assert_eq "ensure_block: converged is quiet" "" "$out" assert_eq "ensure_block: converged file byte-identical" "$snap" "$(exact "$B")" converge <>> pave:first >>>\necho changed\n# <<< pave:first <<<\necho existing\n\nexit 0\n# >>> pave:second >>>\necho last\n# <<< pave:second <<<' "$(<"$B")" converge <>> pave:second >>>\necho last\n# <<< pave:second <<<\n# >>> pave:first >>>\necho changed\n# <<< pave:first <<<\necho existing\n\nexit 0' "$(<"$B")" converge <>> pave:b >>>\nx\n# <<< pave:b <<<' "$(<"$T/fresh/rc")" : >"$T/empty" converge <>> pave:b >>>\nx\n# <<< pave:b <<<' "$(<"$T/empty")" printf 'keep\n# >>> pave:b >>>\nhalf a block\n' >"$T/corrupt" converge <>> pave:b >>>\nhalf a block' "$(<"$T/corrupt")" assert_has "ensure_block: refuses an unknown placement" "placement must be top or bottom" # ---- write_knob (against plain files; real sysfs can't be faked) ---- printf 'old\n' >"$T/knob" converge < new (wrote new)" pv run "$G" manual assert_eq "write_knob: unchanged read-back is quiet" "" "$out" converge <"$T/unreadable" chmod 000 "$T/unreadable" converge <