211 lines
7.0 KiB
Bash
211 lines
7.0 KiB
Bash
#!/usr/bin/env bats
|
|
# apply, confirm, rollback: systemd timer, reload, verification,
|
|
# rollback.
|
|
|
|
load helpers
|
|
|
|
bats_require_minimum_version 1.5.0
|
|
|
|
setup() {
|
|
setup_env
|
|
cp "$REPO/examples/fence.conf" "$CONF"
|
|
chmod 0644 "$CONF"
|
|
}
|
|
|
|
@test "apply: writes, schedules the rollback, reloads, verifies" {
|
|
run fence apply
|
|
[ "$status" -eq 0 ]
|
|
grep -qF '# BEGIN DOCKER-FENCE' "$RULES"
|
|
grep -q '^systemd-run --unit=docker-fence-rollback --on-active=120s ' "$STUB_LOG"
|
|
grep -q '^ufw reload' "$STUB_LOG"
|
|
# The timer is scheduled before the reload.
|
|
[ "$(grep -n '^systemd-run' "$STUB_LOG" | cut -d: -f1)" -lt "$(grep -n '^ufw reload' "$STUB_LOG" | cut -d: -f1)" ]
|
|
grep -qxF -- '-A DOCKER-USER -j docker-fence' "$KERNEL"
|
|
grep -qxF -- '-A ufw-after-input -i br-+ -p tcp --dport 8080 -j ACCEPT' "$KERNEL"
|
|
[ -f "$BACKUPS/pending-rollback" ]
|
|
[[ "$output" == *'Confirm with "docker-fence confirm" within 120 seconds'* ]]
|
|
grep -q 'apply ok mode=observe' "$FENCE_ROOT/var/log/docker-fence.log"
|
|
}
|
|
|
|
@test "the timer command reads the same configuration and options" {
|
|
fence --mode enforce --set rollback_seconds=45 apply
|
|
grep -q -- '--on-active=45s' "$STUB_LOG"
|
|
mapfile -t cmd <"$STUB_STATE/timer_cmd"
|
|
[ "${cmd[0]}" = "$FENCE" ]
|
|
[ "${cmd[1]}" = rollback ]
|
|
[ "${cmd[2]}" = --auto ]
|
|
[ "${cmd[3]}" = --config ]
|
|
[ "${cmd[4]}" = "$CONF" ]
|
|
[[ " ${cmd[*]} " == *" --set mode=enforce "* ]]
|
|
[[ " ${cmd[*]} " == *" --set rollback_seconds=45 "* ]]
|
|
[ "$(cat "$STUB_STATE/timer_env")" = "FENCE_ROOT=$FENCE_ROOT" ]
|
|
}
|
|
|
|
@test "apply without confirm: the timer restores the exact previous state" {
|
|
local orig
|
|
orig="$(sum_of "$RULES")"
|
|
fence apply
|
|
run fire_timer
|
|
[ "$status" -eq 0 ]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
[ "$(stat -c %a "$RULES")" = 640 ]
|
|
[ ! -e "$BACKUPS/pending-rollback" ]
|
|
# Nothing of the block is left in the kernel.
|
|
! grep -qF -- 'docker-fence' "$KERNEL"
|
|
! grep -qF -- '--dport 8080' "$KERNEL"
|
|
grep -q 'rollback --auto' "$FENCE_ROOT/var/log/docker-fence.log"
|
|
}
|
|
|
|
@test "confirm stops the timer; no rollback afterwards" {
|
|
fence apply
|
|
run fence confirm
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"confirmed"* ]]
|
|
grep -q '^systemctl stop docker-fence-rollback.timer' "$STUB_LOG"
|
|
[ ! -e "$STUB_STATE/timer_cmd" ]
|
|
[ ! -e "$BACKUPS/pending-rollback" ]
|
|
# Even if the timer fired anyway, rollback --auto would do nothing.
|
|
local now
|
|
now="$(sum_of "$RULES")"
|
|
run fence rollback --auto
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"no rollback pending"* ]]
|
|
[ "$(sum_of "$RULES")" = "$now" ]
|
|
}
|
|
|
|
@test "unconfirmed switch observe -> enforce: back to observe mode" {
|
|
fence apply
|
|
fence confirm
|
|
local observe
|
|
observe="$(sum_of "$RULES")"
|
|
fence --mode enforce apply
|
|
grep -qxF -- '-A docker-fence -m conntrack --ctstate DNAT -j docker-fence-deny' "$KERNEL"
|
|
fire_timer
|
|
[ "$(sum_of "$RULES")" = "$observe" ]
|
|
# The block is still there: the kernel is reloaded in observe mode.
|
|
grep -qxF -- '-A DOCKER-USER -j docker-fence' "$KERNEL"
|
|
! grep -qF -- '-j docker-fence-deny' "$KERNEL"
|
|
grep -qF -- 'DOCKER-WOULD-DROP' "$KERNEL"
|
|
}
|
|
|
|
@test "apply refused when a rollback is already pending" {
|
|
fence apply
|
|
run fence --mode enforce apply
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"an automatic rollback is pending"* ]]
|
|
run fence install
|
|
[ "$status" -eq 1 ]
|
|
run fence uninstall
|
|
[ "$status" -eq 1 ]
|
|
}
|
|
|
|
@test "UFW inactive: apply refused, nothing changes" {
|
|
local orig
|
|
orig="$(sum_of "$RULES")"
|
|
STUB_UFW_INACTIVE=1 run fence apply
|
|
[ "$status" -eq 2 ]
|
|
[[ "$output" == *"UFW is inactive"* ]]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
! grep -q '^systemd-run' "$STUB_LOG"
|
|
}
|
|
|
|
@test "missing binary: apply refused (exit code 2)" {
|
|
run fence --set bin_systemd_run=/nonexistent/systemd-run apply
|
|
[ "$status" -eq 2 ]
|
|
[[ "$output" == *"command not found: /nonexistent/systemd-run"* ]]
|
|
}
|
|
|
|
@test "systemd-run fails: file restored, nothing reloaded (exit code 4)" {
|
|
local orig
|
|
orig="$(sum_of "$RULES")"
|
|
STUB_SYSTEMD_RUN_RC=1 run fence apply
|
|
[ "$status" -eq 4 ]
|
|
[[ "$output" == *"cannot schedule the rollback"* ]]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
! grep -q '^ufw reload' "$STUB_LOG"
|
|
[ ! -e "$BACKUPS/pending-rollback" ]
|
|
}
|
|
|
|
@test "ufw reload fails: immediate rollback (exit code 4)" {
|
|
local orig
|
|
orig="$(sum_of "$RULES")"
|
|
STUB_UFW_RELOAD_RC=1 run fence apply
|
|
[ "$status" -eq 4 ]
|
|
[[ "$output" == *'"ufw reload" failed'* ]]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
[ ! -e "$BACKUPS/pending-rollback" ]
|
|
grep -q '^systemctl stop docker-fence-rollback.timer' "$STUB_LOG"
|
|
}
|
|
|
|
@test "block missing from the kernel after reload: immediate rollback (exit code 4)" {
|
|
local orig
|
|
orig="$(sum_of "$RULES")"
|
|
STUB_UFW_RELOAD_NOOP=1 run fence apply
|
|
[ "$status" -eq 4 ]
|
|
[[ "$output" == *"DOCKER-USER does not jump to docker-fence"* ]]
|
|
[[ "$output" == *"rollback done"* ]]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
[ ! -e "$STUB_STATE/timer_cmd" ]
|
|
}
|
|
|
|
@test "container_to_host rule not loaded: immediate rollback (exit code 4)" {
|
|
local orig
|
|
orig="$(sum_of "$RULES")"
|
|
STUB_UFW_DROP_C2H=1 run fence apply
|
|
[ "$status" -eq 4 ]
|
|
[[ "$output" == *"container_to_host rule tcp 8080 is missing"* ]]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
! grep -qF -- '-j docker-fence' "$KERNEL"
|
|
}
|
|
|
|
@test "manual rollback: latest backup, then the given backup" {
|
|
local orig v1
|
|
orig="$(sum_of "$RULES")"
|
|
fence install
|
|
v1="$(sum_of "$RULES")"
|
|
fence --mode enforce install
|
|
run fence rollback
|
|
[ "$status" -eq 0 ]
|
|
[ "$(sum_of "$RULES")" = "$v1" ]
|
|
grep -q '^ufw reload' "$STUB_LOG"
|
|
local first
|
|
first="$(find "$BACKUPS" -name 'after.rules.[0-9]*' | sort | head -n 1)"
|
|
run fence rollback "$first"
|
|
[ "$status" -eq 0 ]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
# Each rollback keeps the state it replaces.
|
|
[ "$(find "$BACKUPS" -name 'after.rules.pre-rollback.[0-9]*' | wc -l)" -eq 2 ]
|
|
}
|
|
|
|
@test "rollback without backup or with a missing file" {
|
|
run fence rollback
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"no backup in"* ]]
|
|
run fence rollback /nonexistent
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"backup missing or unreadable"* ]]
|
|
}
|
|
|
|
@test "confirm with no rollback pending" {
|
|
run fence confirm
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"no rollback pending"* ]]
|
|
}
|
|
|
|
@test "rotation never removes the backup a pending rollback waits for" {
|
|
local f
|
|
for f in messages util config rulesfile; do
|
|
source "$REPO/lib/$f.sh"
|
|
done
|
|
fence_config_reset
|
|
CFG[backup_keep]=1
|
|
mkdir -p "$BACKUPS"
|
|
touch "$BACKUPS/after.rules.20260101-000000-1" "$BACKUPS/after.rules.20260102-000000-1" \
|
|
"$BACKUPS/after.rules.20260103-000000-1"
|
|
printf '%s\n' "$BACKUPS/after.rules.20260101-000000-1" >"$BACKUPS/pending-rollback"
|
|
fence_rotate after.rules
|
|
[ -e "$BACKUPS/after.rules.20260101-000000-1" ]
|
|
[ ! -e "$BACKUPS/after.rules.20260102-000000-1" ]
|
|
[ -e "$BACKUPS/after.rules.20260103-000000-1" ]
|
|
}
|