| 1 |
#!/bin/bash |
| 2 |
set -e |
| 3 |
|
| 4 |
# End-to-end test of the Debian systemd integration for receive-wal |
| 5 |
# (debian/patches/systemd-receive-wal-integration.patch + the |
| 6 |
# [email protected] template and polkit rule). It verifies that: |
| 7 |
# |
| 8 |
# 1. "barman cron", run as the unprivileged barman user, starts the |
| 9 |
# per-server unit [email protected] via systemd |
| 10 |
# (exercising the shipped polkit rule), and the receiver connects. |
| 11 |
# 2. "barman receive-wal --stop", run as the barman user, stops the *unit* |
| 12 |
# (detected from the process cgroup) rather than signalling the PID: |
| 13 |
# the receiver must stay down, proving Restart=on-failure did not revive it. |
| 14 |
# 3. Setting streaming_archiver=off makes the next "barman cron" stop the |
| 15 |
# unit. |
| 16 |
# |
| 17 |
# Requires a systemd testbed (isolation-machine) and root. |
| 18 |
|
| 19 |
UNIT="[email protected]" |
| 20 |
BARMAN_CONF=/etc/barman.conf |
| 21 |
BARMAN_CONF_BAK= |
| 22 |
BARMAN_PASSWORD=barman_autopkgtest |
| 23 |
STREAMING_PASSWORD=streaming_autopkgtest |
| 24 |
PGPORT= |
| 25 |
|
| 26 |
as_barman() { runuser -u barman -- "$@"; } |
| 27 |
as_postgres() { runuser -u postgres -- "$@"; } |
| 28 |
run_psql() { as_postgres psql -p "$PGPORT" "$@"; } |
| 29 |
|
| 30 |
CRON_D=/etc/cron.d/barman |
| 31 |
CRON_D_BAK= |
| 32 |
POLKIT_RULE=/usr/share/polkit-1/rules.d/50-barman-receive-wal.rules |
| 33 |
POLKIT_RULE_BAK= |
| 34 |
|
| 35 |
cleanup() { |
| 36 |
echo "=== Cleaning up ===" |
| 37 |
# Stop the receiver whether it is a systemd unit or a forked fallback. |
| 38 |
as_barman barman receive-wal --stop testdb 2>/dev/null || true |
| 39 |
systemctl stop "$UNIT" 2>/dev/null || true |
| 40 |
as_barman barman receive-wal --drop-slot testdb 2>/dev/null || true |
| 41 |
if [ -n "$BARMAN_CONF_BAK" ] && [ -e "$BARMAN_CONF_BAK" ]; then |
| 42 |
mv -f "$BARMAN_CONF_BAK" "$BARMAN_CONF" |
| 43 |
else |
| 44 |
rm -f "$BARMAN_CONF" |
| 45 |
fi |
| 46 |
systemctl unmask barman.timer 2>/dev/null || true |
| 47 |
if [ -n "$CRON_D_BAK" ] && [ -e "$CRON_D_BAK" ]; then |
| 48 |
mv -f "$CRON_D_BAK" "$CRON_D" |
| 49 |
fi |
| 50 |
if [ -n "$POLKIT_RULE_BAK" ] && [ -e "$POLKIT_RULE_BAK" ]; then |
| 51 |
mv -f "$POLKIT_RULE_BAK" "$POLKIT_RULE" |
| 52 |
systemctl reload polkit.service 2>/dev/null \ |
| 53 |
|| systemctl restart polkit.service 2>/dev/null || true |
| 54 |
fi |
| 55 |
} |
| 56 |
trap cleanup EXIT |
| 57 |
|
| 58 |
# This test drives cron by hand and assumes it is the only actor; a background |
| 59 |
# run via barman.timer or /etc/cron.d/barman racing it (re-starting the unit or |
| 60 |
# forking a competing receiver) would make it flaky. Neutralise both, restore on exit. |
| 61 |
echo "=== Disabling background cron drivers (test drives cron by hand) ===" |
| 62 |
systemctl stop barman.timer 2>/dev/null || true |
| 63 |
systemctl mask barman.timer 2>/dev/null || true |
| 64 |
if [ -e "$CRON_D" ]; then |
| 65 |
CRON_D_BAK="${CRON_D}.autopkgtest-bak" |
| 66 |
mv -f "$CRON_D" "$CRON_D_BAK" |
| 67 |
fi |
| 68 |
|
| 69 |
# Wait until "systemctl is-active <unit>" matches the expected state. |
| 70 |
wait_unit_state() { # state timeout |
| 71 |
local want="$1" timeout="${2:-30}" elapsed=0 got |
| 72 |
while [ "$elapsed" -lt "$timeout" ]; do |
| 73 |
got=$(systemctl is-active "$UNIT" 2>/dev/null || true) |
| 74 |
[ "$got" = "$want" ] && return 0 |
| 75 |
# When waiting for the unit to stop, a 'failed' state means the stop |
| 76 |
# did not exit cleanly (e.g. the wrong signal was sent); it will not |
| 77 |
# reach 'inactive' on its own, so fail fast with a clear message |
| 78 |
# instead of waiting out the timeout. |
| 79 |
if [ "$got" = "failed" ] && [ "$want" = "inactive" ]; then |
| 80 |
echo "ERROR: $UNIT entered 'failed' while waiting for it to stop" >&2 |
| 81 |
systemctl status "$UNIT" --no-pager || true |
| 82 |
return 1 |
| 83 |
fi |
| 84 |
sleep 1 |
| 85 |
elapsed=$((elapsed + 1)) |
| 86 |
done |
| 87 |
echo "ERROR: $UNIT did not become '$want' within ${timeout}s (last: '$got')" >&2 |
| 88 |
systemctl status "$UNIT" --no-pager || true |
| 89 |
return 1 |
| 90 |
} |
| 91 |
|
| 92 |
# Wait until the replication slot is active (a receiver is connected). |
| 93 |
wait_for_slot_active() { # timeout |
| 94 |
local timeout="${1:-30}" elapsed=0 active |
| 95 |
while [ "$elapsed" -lt "$timeout" ]; do |
| 96 |
active=$(run_psql -AqtX -c \ |
| 97 |
"SELECT active FROM pg_replication_slots WHERE slot_name = 'barman'" \ |
| 98 |
2>/dev/null || true) |
| 99 |
[ "$active" = "t" ] && return 0 |
| 100 |
sleep 1 |
| 101 |
elapsed=$((elapsed + 1)) |
| 102 |
done |
| 103 |
echo "ERROR: replication slot did not become active within ${timeout}s" >&2 |
| 104 |
return 1 |
| 105 |
} |
| 106 |
|
| 107 |
# --- locate (and if needed start) a PostgreSQL cluster ------------------- |
| 108 |
PGPORT=$(pg_lsclusters -h | awk '$4 == "online" { print $3; exit }') |
| 109 |
if [ -z "$PGPORT" ]; then |
| 110 |
read -r ver cluster <<EOF |
| 111 |
$(pg_lsclusters -h | awk 'NR == 1 { print $1, $2 }') |
| 112 |
EOF |
| 113 |
[ -n "$ver" ] || { echo "ERROR: no PostgreSQL cluster found" >&2; exit 1; } |
| 114 |
pg_ctlcluster "$ver" "$cluster" start |
| 115 |
PGPORT=$(pg_lsclusters -h | awk '$4 == "online" { print $3; exit }') |
| 116 |
fi |
| 117 |
[ -n "$PGPORT" ] || { echo "ERROR: no online PostgreSQL cluster" >&2; exit 1; } |
| 118 |
echo "Using PostgreSQL on port $PGPORT" |
| 119 |
|
| 120 |
# --- PostgreSQL roles and host-based authentication ---------------------- |
| 121 |
echo "=== Creating PostgreSQL roles ===" |
| 122 |
run_psql -c "CREATE USER barman SUPERUSER PASSWORD '$BARMAN_PASSWORD';" |
| 123 |
run_psql -c "CREATE USER streaming_barman REPLICATION LOGIN PASSWORD '$STREAMING_PASSWORD';" |
| 124 |
|
| 125 |
echo "=== Allowing password authentication from localhost ===" |
| 126 |
hba_file=$(run_psql -AqtX -c "SHOW hba_file") |
| 127 |
{ |
| 128 |
printf 'host all barman 127.0.0.1/32 scram-sha-256\n' |
| 129 |
printf 'host replication streaming_barman 127.0.0.1/32 scram-sha-256\n' |
| 130 |
} >> "$hba_file" |
| 131 |
run_psql -c "SELECT pg_reload_conf();" |
| 132 |
|
| 133 |
# --- barman configuration (the path the systemd unit reads) -------------- |
| 134 |
echo "=== Writing $BARMAN_CONF ===" |
| 135 |
if [ -e "$BARMAN_CONF" ]; then |
| 136 |
BARMAN_CONF_BAK="${BARMAN_CONF}.autopkgtest-bak" |
| 137 |
mv -f "$BARMAN_CONF" "$BARMAN_CONF_BAK" |
| 138 |
fi |
| 139 |
cat > "$BARMAN_CONF" <<EOF |
| 140 |
[barman] |
| 141 |
barman_home = /var/lib/barman |
| 142 |
barman_user = barman |
| 143 |
log_file = /var/log/barman/barman.log |
| 144 |
configuration_files_directory = /etc/barman.d |
| 145 |
|
| 146 |
[testdb] |
| 147 |
description = "Autopkgtest systemd integration" |
| 148 |
conninfo = host=127.0.0.1 port=${PGPORT} user=barman password=${BARMAN_PASSWORD} dbname=postgres sslmode=disable |
| 149 |
streaming_conninfo = host=127.0.0.1 port=${PGPORT} user=streaming_barman password=${STREAMING_PASSWORD} sslmode=disable |
| 150 |
backup_method = postgres |
| 151 |
streaming_archiver = on |
| 152 |
slot_name = barman |
| 153 |
EOF |
| 154 |
|
| 155 |
# --- 1. cron starts the unit (via polkit, as the barman user) ----------- |
| 156 |
echo "=== Creating replication slot ===" |
| 157 |
as_barman barman receive-wal --create-slot testdb |
| 158 |
|
| 159 |
echo "=== Running 'barman cron' as the barman user ===" |
| 160 |
as_barman barman -q cron |
| 161 |
|
| 162 |
echo "=== Asserting the systemd unit is active ===" |
| 163 |
wait_unit_state active 30 |
| 164 |
echo "=== Asserting the receiver connected (slot active) ===" |
| 165 |
wait_for_slot_active 30 |
| 166 |
|
| 167 |
# A second cron run must be a harmless no-op (systemctl start is idempotent). |
| 168 |
echo "=== Re-running cron (idempotent) ===" |
| 169 |
as_barman barman -q cron |
| 170 |
wait_unit_state active 5 |
| 171 |
|
| 172 |
# --- 1b. config-switch restarts the receiver via systemd (restart_processes) -- |
| 173 |
# "barman config-switch" (and config-update) call Server.restart_processes(), |
| 174 |
# the second code path that routes receiver startup through systemd: it stops |
| 175 |
# the receiver and starts it again. Exercise it and re-assert the receiver is up. |
| 176 |
echo "=== Restarting via 'barman config-switch --reset' (exercises restart_processes) ===" |
| 177 |
as_barman barman config-switch testdb --reset |
| 178 |
wait_unit_state active 30 |
| 179 |
wait_for_slot_active 30 |
| 180 |
echo " config-switch restarted the receiver under systemd: OK" |
| 181 |
|
| 182 |
# --- 2. receive-wal --stop stops the UNIT, and it stays stopped ---------- |
| 183 |
echo "=== Stopping via 'barman receive-wal --stop' (as barman user) ===" |
| 184 |
as_barman barman receive-wal --stop testdb |
| 185 |
wait_unit_state inactive 15 |
| 186 |
|
| 187 |
echo "=== Asserting the receiver is NOT revived ===" |
| 188 |
# A clean --stop leaves the unit inactive and it must stay that way. If the stop |
| 189 |
# had instead left the receiver in a failed state (e.g. the wrong kill signal), |
| 190 |
# Restart=on-failure would bring it back within RestartSec (10s); confirm it is |
| 191 |
# still inactive after 15s. |
| 192 |
sleep 15 |
| 193 |
if systemctl is-active --quiet "$UNIT"; then |
| 194 |
echo "ERROR: $UNIT came back after --stop (it should have been stopped, not signalled)" >&2 |
| 195 |
systemctl status "$UNIT" --no-pager || true |
| 196 |
exit 1 |
| 197 |
fi |
| 198 |
echo " unit stayed stopped: OK" |
| 199 |
|
| 200 |
# --- 3. disabling streaming_archiver stops the unit via cron ------------- |
| 201 |
echo "=== Restarting receiver, then disabling streaming_archiver ===" |
| 202 |
as_barman barman -q cron |
| 203 |
wait_unit_state active 30 |
| 204 |
# Wait until the receiver has actually connected before flipping the config. |
| 205 |
# The unit is Type=simple, so it reports "active" the instant the process is |
| 206 |
# forked, before "barman receive-wal" has read its configuration; disabling |
| 207 |
# streaming in that window makes the receiver read streaming_archiver=off and |
| 208 |
# exit non-zero, which Restart=on-failure would then crash-loop on. |
| 209 |
wait_for_slot_active 30 |
| 210 |
|
| 211 |
sed -i 's/^streaming_archiver = on/streaming_archiver = off/' "$BARMAN_CONF" |
| 212 |
as_barman barman -q cron |
| 213 |
wait_unit_state inactive 15 |
| 214 |
echo " cron stopped the unit after disabling streaming: OK" |
| 215 |
|
| 216 |
# --- 4. fork-fallback when the unit cannot be managed (no polkit) --------- |
| 217 |
# Move the shipped polkit rule aside and reload polkit so the barman user is no |
| 218 |
# longer authorised to manage the unit, mimicking a host without polkit. Then |
| 219 |
# run cron through the real barman.service oneshot and assert the receiver is |
| 220 |
# started by FORKING (not as a unit) and OUTLIVES the oneshot exit, which is |
| 221 |
# exactly what KillMode=process in barman.service guarantees (Debian #1138696). |
| 222 |
echo "=== Simulating absent polkit authorisation (moving the rule aside) ===" |
| 223 |
sed -i 's/^streaming_archiver = off/streaming_archiver = on/' "$BARMAN_CONF" |
| 224 |
if [ -e "$POLKIT_RULE" ]; then |
| 225 |
POLKIT_RULE_BAK="${POLKIT_RULE}.autopkgtest-bak" |
| 226 |
mv -f "$POLKIT_RULE" "$POLKIT_RULE_BAK" |
| 227 |
systemctl reload polkit.service 2>/dev/null \ |
| 228 |
|| systemctl restart polkit.service 2>/dev/null || true |
| 229 |
fi |
| 230 |
|
| 231 |
echo "=== Running cron via the barman.service oneshot (expect fork fallback) ===" |
| 232 |
# barman.service runs "barman cron" as the barman user; the unit start is now |
| 233 |
# denied, so cron must fall back to forking. systemctl start blocks until the |
| 234 |
# oneshot has finished and its cgroup has been torn down, so a receiver that is |
| 235 |
# streaming afterwards has necessarily survived that teardown. |
| 236 |
systemctl start barman.service |
| 237 |
|
| 238 |
echo "=== Asserting a receiver is streaming, but NOT as the systemd unit ===" |
| 239 |
wait_for_slot_active 30 |
| 240 |
if systemctl is-active --quiet "$UNIT"; then |
| 241 |
echo "ERROR: $UNIT is active; expected the forked fallback, not a unit" >&2 |
| 242 |
systemctl status "$UNIT" --no-pager || true |
| 243 |
exit 1 |
| 244 |
fi |
| 245 |
echo " receiver streaming via forked fallback, unit not active: OK" |
| 246 |
|
| 247 |
echo "=== Asserting the forked receiver outlived the barman.service oneshot ===" |
| 248 |
# Re-check after a short delay: without KillMode=process the control-group |
| 249 |
# teardown of the oneshot would have killed the forked receiver by now. |
| 250 |
sleep 5 |
| 251 |
wait_for_slot_active 5 |
| 252 |
echo " forked receiver survived the oneshot exit (KillMode=process): OK" |
| 253 |
|
| 254 |
echo "=== All systemd integration tests passed ===" |