Finish postgres plugin and write tests for it.

This commit is contained in:
redxef 2024-02-22 13:39:54 +01:00
parent 47781e8bcf
commit bd409be6f2
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
3 changed files with 63 additions and 4 deletions

View file

@ -3,8 +3,9 @@
_tarback_plugin_postgres_host= _tarback_plugin_postgres_host=
_tarback_plugin_postgres_port= _tarback_plugin_postgres_port=
_tarback_plugin_postgres_user= _tarback_plugin_postgres_user=
_tarback_plugin_postgres_password=
short_options='h:p:U:' short_options='h:p:U:w:'
while getopts "$short_options" arg; do while getopts "$short_options" arg; do
case "$arg" in case "$arg" in
h) h)
@ -16,6 +17,9 @@ while getopts "$short_options" arg; do
U) U)
_tarback_plugin_postgres_user="$OPTARG" _tarback_plugin_postgres_user="$OPTARG"
;; ;;
w)
_tarback_plugin_postgres_password="$OPTARG"
;;
esac esac
done done
@ -23,10 +27,11 @@ _tarback_plugin_postgres_command=
[ -n "$_tarback_plugin_postgres_host" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -h '$_tarback_plugin_postgres_host'" [ -n "$_tarback_plugin_postgres_host" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -h '$_tarback_plugin_postgres_host'"
[ -n "$_tarback_plugin_postgres_port" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -p '$_tarback_plugin_postgres_port'" [ -n "$_tarback_plugin_postgres_port" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -p '$_tarback_plugin_postgres_port'"
[ -n "$_tarback_plugin_postgres_user" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -U '$_tarback_plugin_postgres_user'" [ -n "$_tarback_plugin_postgres_user" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -U '$_tarback_plugin_postgres_user'"
_tarback_plugin_postgres_create_command="pg_dump $_tarback_plugin_postgres_command "'"$1"' _tarback_plugin_postgres_create_command="PGPASSWORD='$_tarback_plugin_postgres_password' pg_dump --clean $_tarback_plugin_postgres_command "'"$1"'
_tarback_plugin_postgres_extract_command="psql $_tarback_plugin_postgres_command" _tarback_plugin_postgres_extract_command="PGPASSWORD='$_tarback_plugin_postgres_password' psql $_tarback_plugin_postgres_command"
TARBACK_TAR_CREATE_COMMAND="$_tarback_plugin_postgres_create_command" TARBACK_TAR_CREATE_COMMAND="$_tarback_plugin_postgres_create_command"
TARBACK_TAR_EXTRACT_COMMAND="$_tarback_plugin_postgres_extract_command" TARBACK_TAR_EXTRACT_COMMAND="$_tarback_plugin_postgres_extract_command"
TARBACK_EXTENSION='xz' TARBACK_EXTENSION='xz'
TARBACK_TAR_CREATE_USE_ALT_COMMAND=false TARBACK_TAR_CREATE_USE_ALT_COMMAND=false
TARBACK_TAR_EXTRACT_USE_ALT_COMMAND=false TARBACK_TAR_EXTRACT_USE_ALT_COMMAND=false

View file

@ -6,7 +6,9 @@ early_return="${TARBACK_TEST_EARLY_RETURN:-false}"
failed=0 failed=0
for f in ./tests/test-*.sh; do for f in ./tests/test-*.sh; do
printf '%s' "Running test $f ... " printf '%s' "Running test $f ... "
if "$f"; then if "$f" 1>"$f.stdout.log" 2>"$f.stderr.log"; then
rm "$f.stdout.log"
rm "$f.stderr.log"
echo "succeeded" echo "succeeded"
else else
failed=$((failed+1)) failed=$((failed+1))
@ -15,6 +17,7 @@ for f in ./tests/test-*.sh; do
break break
fi fi
fi fi
find ./tests/ -type f -size 0 -delete
done done
if [ $failed -ne 0 ]; then if [ $failed -ne 0 ]; then

51
tests/test-004-postgres.sh Executable file
View file

@ -0,0 +1,51 @@
#!/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.sh -P postgres -h 10.1.0.100 -U postgres -p 5432 -w password123 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.sh -P postgres -h 10.1.0.100 -U postgres -p 5432 -w password123 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"