Files

98 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
printf '%s\n' "$*" >> "${FAKE_DOCKER_LOG:?}"
command_line=" $* "
if [[ "$command_line" == *" pg_dump "* ]]; then
case "${FAKE_DOCKER_MODE:-success}" in
dump-fail)
printf 'pg_dump failed\n' >&2
exit 42
;;
dump-partial-fail)
printf '%s\n' '-- incomplete dump --'
printf 'pg_dump failed after output\n' >&2
exit 42
;;
empty)
exit 0
;;
concurrent)
barrier="${FAKE_DOCKER_BARRIER:?}"
mkdir -p "$barrier"
touch "$barrier/$$"
for _ in {1..500}; do
count="$(find "$barrier" -type f | wc -l | tr -d '[:space:]')"
if ((count >= 2)); then
break
fi
sleep 0.01
done
if ((count < 2)); then
printf 'concurrent backup barrier timed out\n' >&2
exit 46
fi
;;
esac
printf '%s\n' '-- fake PostgreSQL dump --' 'CREATE TABLE example(id integer);'
exit 0
fi
if [[ "$command_line" == *" createdb "* ]]; then
exit 0
fi
if [[ "$command_line" == *" dropdb "* ]]; then
exit 0
fi
if [[ "$command_line" == *" psql "* && "$command_line" == *" ON_ERROR_STOP=1 "* ]]; then
cat > "${FAKE_RESTORE_INPUT:?}"
if [[ "${FAKE_DOCKER_MODE:-success}" == "restore-fail" ]]; then
printf 'restore failed\n' >&2
exit 43
fi
if [[ "$command_line" != *" -o /dev/null "* ]]; then
printf '%s\n' 'SET' 'CREATE TABLE'
fi
exit 0
fi
if [[ "$command_line" == *" psql "* && "$command_line" == *" SELECT count(*) FROM public."* ]]; then
database=""
query=""
while (($#)); do
case "$1" in
-d)
database="$2"
shift 2
;;
-c)
query="$2"
shift 2
;;
*)
shift
;;
esac
done
table="${query#*public.}"
table="${table%%;*}"
case "$table" in
download_jobs) count=3 ;;
subscriptions) count=2 ;;
known_episodes) count=5 ;;
schema_migrations) count=3 ;;
*) printf 'unexpected table: %s\n' "$table" >&2; exit 44 ;;
esac
if [[ "${FAKE_DOCKER_MODE:-success}" == "count-mismatch" && "$database" == "${RESTORE_DB:-tver_restore_test}" && "$table" == "known_episodes" ]]; then
count=4
fi
printf '%s\n' "$count"
exit 0
fi
printf 'unexpected docker invocation: %s\n' "$*" >&2
exit 45