tarback/tests/test-004-postgres.sh

52 lines
1.2 KiB
Bash
Raw Normal View History

#!/bin/sh
set -eu
. ./tests/common.sh
postgres_container_name="postgres-test-$(uuidgen)"
docker run --name "$postgres_container_name" -p 5432:5432 -e POSTGRES_PASSWORD=password123 -d postgres
trap "docker rm -f '$postgres_container_name'; exit" EXIT
for _ in $(seq 1 10); do
if echo '' | PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres; then
break
fi
sleep 1
done
PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
create table a(
i int
);
insert into a(i) values (1), (2), (3), (4);
EOF
d="$(mktemp -d)"
TARBACK_POSTGRES_ARGS='-h 10.1.0.100 -U postgres -p 5432 -w password123' ./tarback.sh -P postgres create postgres "$d/dump"
PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
insert into a(i) values (6), (7)
EOF
TARBACK_POSTGRES_ARGS='-h 10.1.0.100 -U postgres -p 5432 -w password123' ./tarback.sh -P postgres restore "$d/dump" postgres
out="$(PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
select i from a;
EOF
)"
out="$(echo "$out" | tail -n+3)"
expected_out="$(cat <<EOF
1
2
3
4
(4 rows)
EOF
)"
if [ "$out" != "$expected_out" ]; then
echo "$out" | xxd
echo "$expected_out" | xxd
exit 1
fi
rm -r "$d"