Fix tests and option parsing of plugins.

This commit is contained in:
redxef 2024-02-22 14:22:20 +01:00
parent bd409be6f2
commit e58c2eb875
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
6 changed files with 68 additions and 5 deletions

View file

@ -135,7 +135,15 @@ while getopts "$short_options" arg; do
for loc in $TARBACK_PLUGIN_SEARCH_PATH; do for loc in $TARBACK_PLUGIN_SEARCH_PATH; do
if [ -e "$loc/tarback/$p.sh" ]; then if [ -e "$loc/tarback/$p.sh" ]; then
# shellcheck disable=SC1090 # disable cannot follow source # shellcheck disable=SC1090 # disable cannot follow source
old_short_options="$short_options"
old_arg="$arg"
old_OPTIND="$OPTIND"
old_OPTARG="$OPTARG"
. "$loc/tarback/$p.sh" . "$loc/tarback/$p.sh"
short_options="$old_short_options"
arg="$old_arg"
OPTIND="$old_OPTIND"
OPTARG="$old_OPTARG"
sourced=true sourced=true
break break
fi fi

View file

@ -3,7 +3,8 @@
_tarback_plugin_docker_use_container= _tarback_plugin_docker_use_container=
short_options='c:' short_options='c:'
while getopts "$short_options" arg; do OPTIND=1
while [ "${TARBACK_DOCKER_ARGS:-}" != '' ] && getopts "$short_options" arg ${TARBACK_DOCKER_ARGS:-}; do
case "$arg" in case "$arg" in
c) c)
_tarback_plugin_docker_use_container="$OPTARG" _tarback_plugin_docker_use_container="$OPTARG"

View file

@ -6,7 +6,8 @@ _tarback_plugin_postgres_user=
_tarback_plugin_postgres_password= _tarback_plugin_postgres_password=
short_options='h:p:U:w:' short_options='h:p:U:w:'
while getopts "$short_options" arg; do OPTIND=1
while [ "${TARBACK_POSTGRES_ARGS:-}" != '' ] && getopts "$short_options" arg ${TARBACK_POSTGRES_ARGS:-}; do
case "$arg" in case "$arg" in
h) h)
_tarback_plugin_postgres_host="$OPTARG" _tarback_plugin_postgres_host="$OPTARG"

View file

@ -19,7 +19,8 @@ _tarback_plugin_ssh_transform_ssh_argument() {
} }
short_options='s:' short_options='s:'
while getopts "$short_options" arg; do OPTIND=1
while [ "${TARBACK_SSH_ARGS:-}" != '' ] && getopts "$short_options" arg ${TARBACK_SSH_ARGS:-}; do
case "$arg" in case "$arg" in
s) s)
TARBACK_REMOTE="$OPTARG" TARBACK_REMOTE="$OPTARG"

View file

@ -23,11 +23,11 @@ EOF
d="$(mktemp -d)" d="$(mktemp -d)"
./tarback.sh -P postgres -h 10.1.0.100 -U postgres -p 5432 -w password123 create postgres "$d/dump" 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 PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
insert into a(i) values (6), (7) insert into a(i) values (6), (7)
EOF EOF
./tarback.sh -P postgres -h 10.1.0.100 -U postgres -p 5432 -w password123 restore "$d/dump" postgres 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 out="$(PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
select i from a; select i from a;

52
tests/test-005-ssh-postgres.sh Executable file
View file

@ -0,0 +1,52 @@
#!/bin/sh
set -eu
. ./tests/common.sh
export TARBACK_REMOTE='ssh localhost'
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 ssh,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 ssh,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"