lightweight-server/tests/test-step2-ufw-boot.sh
ochenstarik-ui e5b06793ec
All checks were successful
Bash syntax / syntax (push) Successful in 18s
Bash syntax / syntax (pull_request) Successful in 17s
Reject masked UFW boot service before changing firewall rules
2026-10-23 15:26:10 +07:00

41 lines
1.4 KiB
Bash

#!/usr/bin/env bash
set -Eeuo pipefail
cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
source <(awk '/^\[\[ "\$EUID"/{exit} {print}' ochenstarik-server-2.sh)
test_dir="$(mktemp -d)"
trap 'rm -rf -- "$test_dir"' EXIT
systemctl() {
printf '%s\n' "$*" >> "$test_dir/calls"
case "$*" in
'is-enabled ufw.service') cat "$test_dir/state"; return 1 ;;
'enable ufw.service')
[[ "$scenario" != enable-failed ]] || return 1
[[ "$scenario" == verify-failed ]] || printf enabled > "$test_dir/state"
;;
'is-enabled --quiet ufw.service') [[ "$(cat "$test_dir/state")" == enabled ]] ;;
*) return 99 ;;
esac
}
for state in masked masked-runtime; do
printf '%s' "$state" > "$test_dir/state"
: > "$test_dir/calls"
scenario=masked
if (ensure_ufw_boot_service) > "$test_dir/output" 2>&1; then exit 1; fi
! grep -q '^enable ' "$test_dir/calls"
[[ "$(cat "$test_dir/state")" == "$state" ]]
done
for scenario in disabled enabled enable-failed verify-failed; do
printf '%s' "$scenario" > "$test_dir/state"
: > "$test_dir/calls"
if [[ "$scenario" == *-failed ]]; then
if (ensure_ufw_boot_service) > "$test_dir/output" 2>&1; then exit 1; fi
else
ensure_ufw_boot_service
[[ "$(cat "$test_dir/state")" == enabled ]]
grep -qx 'is-enabled --quiet ufw.service' "$test_dir/calls"
fi
done
printf 'Step 2 UFW boot readiness tests passed.\n'