| 1 |
#!/bin/bash |
| 2 |
set -e |
| 3 |
|
| 4 |
# pg_virtualenv does its cluster setup as the invoking user. When |
| 5 |
# autopkgtest runs as root, re-exec as the postgres system user so the |
| 6 |
# cluster is owned by an unprivileged account. We use "su" without the |
| 7 |
# "-"/login flag to preserve the environment (notably AUTOPKGTEST_TMP) |
| 8 |
# and force /bin/sh so we don't depend on the postgres user's shell. |
| 9 |
# Positional args after the username become $0, $1, ... of the -c shell; |
| 10 |
# we pass our script path so it becomes $0 and re-execs via its shebang. |
| 11 |
if [ "$(id -u)" = 0 ]; then |
| 12 |
exec su -s /bin/sh postgres -c 'exec "$0" "$@"' "$0" "$@" |
| 13 |
fi |
| 14 |
|
| 15 |
BARMAN_HOME= |
| 16 |
BARMAN_CONF= |
| 17 |
receiver_pid= |
| 18 |
|
| 19 |
cleanup() { |
| 20 |
echo "Cleaning up..." |
| 21 |
# "barman receive-wal --stop" signals the daemon via its pidfile, |
| 22 |
# which is more reliable than killing our background job (barman |
| 23 |
# forks pg_receivewal, and the child may not be in our process |
| 24 |
# group). |
| 25 |
if [ -n "$BARMAN_CONF" ] && [ -r "$BARMAN_CONF" ]; then |
| 26 |
barman -c "$BARMAN_CONF" receive-wal --stop testdb 2>/dev/null || true |
| 27 |
fi |
| 28 |
if [ -n "$receiver_pid" ]; then |
| 29 |
wait "$receiver_pid" 2>/dev/null || true |
| 30 |
fi |
| 31 |
[ -n "$BARMAN_HOME" ] && rm -rf "$BARMAN_HOME" |
| 32 |
[ -n "$BARMAN_CONF" ] && rm -f "$BARMAN_CONF" |
| 33 |
: |
| 34 |
} |
| 35 |
|
| 36 |
# Poll pg_replication_slots until the given slot is active (i.e. a |
| 37 |
# receiver is connected and streaming) or the timeout expires. |
| 38 |
wait_for_slot_active() { |
| 39 |
local slot="$1" |
| 40 |
local timeout="${2:-30}" |
| 41 |
local elapsed=0 active |
| 42 |
while [ "$elapsed" -lt "$timeout" ]; do |
| 43 |
active=$(psql -AqtX -c \ |
| 44 |
"SELECT active FROM pg_replication_slots WHERE slot_name = '$slot'" \ |
| 45 |
2>/dev/null || true) |
| 46 |
if [ "$active" = "t" ]; then |
| 47 |
return 0 |
| 48 |
fi |
| 49 |
sleep 1 |
| 50 |
elapsed=$((elapsed + 1)) |
| 51 |
done |
| 52 |
return 1 |
| 53 |
} |
| 54 |
|
| 55 |
run_barman_test() { |
| 56 |
BARMAN_HOME=$(mktemp -d) |
| 57 |
BARMAN_CONF=$(mktemp) |
| 58 |
mkdir -p "$BARMAN_HOME/conf.d" |
| 59 |
|
| 60 |
local barman_password="barman_autopkgtest" |
| 61 |
local streaming_password="streaming_autopkgtest" |
| 62 |
|
| 63 |
echo "=== Creating PostgreSQL users ===" |
| 64 |
psql -c "CREATE USER barman SUPERUSER PASSWORD '$barman_password';" |
| 65 |
psql -c "CREATE USER streaming_barman REPLICATION LOGIN PASSWORD '$streaming_password';" |
| 66 |
|
| 67 |
echo "=== Configuring pg_hba for password authentication ===" |
| 68 |
local hba_file |
| 69 |
hba_file=$(psql -AqtX -c "SHOW hba_file") |
| 70 |
printf "host all barman 127.0.0.1/32 scram-sha-256\n" >> "$hba_file" |
| 71 |
printf "host replication streaming_barman 127.0.0.1/32 scram-sha-256\n" >> "$hba_file" |
| 72 |
psql -c "SELECT pg_reload_conf();" |
| 73 |
|
| 74 |
echo "=== Creating barman configuration ===" |
| 75 |
|
| 76 |
cat > "$BARMAN_CONF" <<EOF |
| 77 |
[barman] |
| 78 |
barman_home = ${BARMAN_HOME} |
| 79 |
barman_user = $(whoami) |
| 80 |
log_file = ${BARMAN_HOME}/barman.log |
| 81 |
configuration_files_directory = ${BARMAN_HOME}/conf.d |
| 82 |
|
| 83 |
[testdb] |
| 84 |
description = "Autopkgtest PostgreSQL server" |
| 85 |
conninfo = host=127.0.0.1 port=${PGPORT} user=barman password=${barman_password} dbname=postgres sslmode=disable |
| 86 |
streaming_conninfo = host=127.0.0.1 port=${PGPORT} user=streaming_barman password=${streaming_password} sslmode=disable |
| 87 |
backup_method = postgres |
| 88 |
streaming_archiver = on |
| 89 |
slot_name = barman |
| 90 |
backup_directory = ${BARMAN_HOME}/testdb |
| 91 |
EOF |
| 92 |
|
| 93 |
echo "=== Testing barman connectivity ===" |
| 94 |
barman -c "$BARMAN_CONF" list-servers |
| 95 |
barman -c "$BARMAN_CONF" status testdb || true |
| 96 |
|
| 97 |
echo "=== Creating replication slot ===" |
| 98 |
barman -c "$BARMAN_CONF" receive-wal --create-slot testdb |
| 99 |
|
| 100 |
echo "=== Starting WAL receiver (background) ===" |
| 101 |
barman -c "$BARMAN_CONF" receive-wal testdb & |
| 102 |
receiver_pid=$! |
| 103 |
|
| 104 |
echo "=== Waiting for WAL receiver to connect ===" |
| 105 |
if ! wait_for_slot_active "barman" 30; then |
| 106 |
echo "ERROR: WAL receiver did not connect within 30s" |
| 107 |
exit 1 |
| 108 |
fi |
| 109 |
|
| 110 |
echo "=== Barman check (for diagnostics) ===" |
| 111 |
barman -c "$BARMAN_CONF" check testdb || true |
| 112 |
|
| 113 |
# Run the backup without --wait: --wait blocks until the backup's |
| 114 |
# trailing WAL is archived (moved from streaming/ to wals/), which |
| 115 |
# requires "barman cron" / "barman archive-wal" to run periodically. |
| 116 |
# This test only validates the backup path, so we skip the archive |
| 117 |
# step and verify the backup metadata directly. |
| 118 |
echo "=== Running streaming backup ===" |
| 119 |
barman -c "$BARMAN_CONF" backup testdb |
| 120 |
|
| 121 |
echo "=== Listing backups ===" |
| 122 |
barman -c "$BARMAN_CONF" list-backups testdb |
| 123 |
|
| 124 |
echo "=== Checking backup info ===" |
| 125 |
local backup_id |
| 126 |
backup_id=$(barman -c "$BARMAN_CONF" list-backups testdb --minimal | head -n 1) |
| 127 |
if [ -z "$backup_id" ]; then |
| 128 |
echo "ERROR: No backup found" |
| 129 |
exit 1 |
| 130 |
fi |
| 131 |
barman -c "$BARMAN_CONF" show-backup testdb "$backup_id" |
| 132 |
|
| 133 |
echo "=== All streaming backup tests passed ===" |
| 134 |
} |
| 135 |
|
| 136 |
start_virtualenv() { |
| 137 |
PGVIRTUAL=1 exec pg_virtualenv -t \ |
| 138 |
-o "wal_level=replica" \ |
| 139 |
-o "max_wal_senders=3" \ |
| 140 |
-o "max_replication_slots=3" \ |
| 141 |
-o "listen_addresses=localhost" \ |
| 142 |
-o "log_line_prefix=" \ |
| 143 |
"$0" |
| 144 |
} |
| 145 |
|
| 146 |
trap cleanup EXIT |
| 147 |
|
| 148 |
if [ -z "$PGVIRTUAL" ]; then |
| 149 |
start_virtualenv |
| 150 |
fi |
| 151 |
|
| 152 |
run_barman_test |