# Copyright (C) 2012, 2018 The Meme Factory, Inc. http://www.meme.com/
#
# This file is part of Gombe-MI.
#
# Gombe-MI is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gombe-MI. If not, see .
#
# Run a test by invoking make.
#
# Syntax: run_test psql_args test
#
# Input:
# psql_args Args for psql
# test Test operation to perform.
#
# Environment Variables:
# RUN_TEST_SOCK Output, path to socket used to feed psql
# THIS_TEST Output, name of current test
#
# Karl O. Pinc
#
# Bugs:
# Does not do an awesome job of capturing psql errors.
#
export PSQL="./psql --host /var/run/postgresql"
export psql_args="$1"
export test_op="$2"
export tsock_dir=/tmp/$0_sockdir_$$
export RUN_TEST_SOCK=$tsock_dir/$0_sock_$$
export tfile=/tmp/$0_$$
exec_sql () {
socat -u STDIN UNIX-SENDTO:$RUN_TEST_SOCK
}
cleanup () {
# shutdown db connection
[ -S $RUN_TEST_SOCK ] && printf '\\q\n' | exec_sql 2>/dev/null
rm -rf $tsock_dir $tfile
}
trap cleanup 0 1 2
# Get rid of the leading foo_ prefix.
export THIS_TEST=$(awk -v to="$test_op" \
'BEGIN {print substr(to, index(to, "_") + 1);
exit; }')
export THIS_OP=${test_op%_${THIS_TEST}}
if [ ! -e sql/$THIS_TEST ] ; then
printf '%s: No such test\n' $THIS_TEST >&2
exit 1
fi
export RUN_TEST_SOCK=$RUN_TEST_SOCK
mkdir $tsock_dir
socat -u UNIX-RECV:$RUN_TEST_SOCK,mode=0600 \
EXEC:"'$PSQL $psql_args'" \
> $tfile &
# Wait for the socket to be available.
# Do this in a loop in case something else messes with the directory.
# Use -t 1 to break out of race condition where creation happens
# between the [] and inotifywait execution.
while [ ! -e $RUN_TEST_SOCK ] ; do
inotifywait -qq -t 1 -e create $tsock_dir
done;
# Initialize psql environment.
# Capture the error stream, never show context for errors.
printf '\\pset estream query\n\\set SHOW_CONTEXT never\n' | exec_sql
# Run the test, and it's prerequsites.
make -s -R -f run_test.mkf "$test_op" 4>&2 2>/dev/null
test_error=$?
if [ -e loaded/begin ] \
&& [ $test_error -eq 0 ] \
&& [ ! -e loaded/commit ] \
&& [ $THIS_TEST != 'commit' ] ; then
# Still no commit. (Must not raise an error in a transaction but
# still require a transaction.) Test for successful commit.
if [ "$THIS_OP" = 'show' ] ; then
# We want to see what the commit does.
printf ' --------------------------------------------------------------\n'
sh &2
echo exiting psql error
exit $psql_error
fi
exit $test_error