/ db / test /
/db/test/run_test.sh
1 # Copyright (C) 2012, 2018 The Meme Factory, Inc. http://www.meme.com/
2 #
3 # This file is part of Gombe-MI.
4 #
5 # Gombe-MI is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Gombe-MI. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Run a test by invoking make.
19 #
20 # Syntax: run_test psql_args test
21 #
22 # Input:
23 # psql_args Args for psql
24 # test Test operation to perform.
25 #
26 # Environment Variables:
27 # RUN_TEST_SOCK Output, path to socket used to feed psql
28 # THIS_TEST Output, name of current test
29 #
30 # Karl O. Pinc <kop@meme.com>
31 #
32 # Bugs:
33 # Does not do an awesome job of capturing psql errors.
34 #
35
36 export PSQL="./psql --host /var/run/postgresql"
37 export psql_args="$1"
38 export test_op="$2"
39
40 export tsock_dir=/tmp/$0_sockdir_$$
41 export RUN_TEST_SOCK=$tsock_dir/$0_sock_$$
42 export tfile=/tmp/$0_$$
43
44 exec_sql () {
45 socat -u STDIN UNIX-SENDTO:$RUN_TEST_SOCK
46 }
47
48 cleanup () {
49 # shutdown db connection
50 [ -S $RUN_TEST_SOCK ] && printf '\\q\n' | exec_sql 2>/dev/null
51 rm -rf $tsock_dir $tfile
52 }
53 trap cleanup 0 1 2
54
55 # Get rid of the leading foo_ prefix.
56 export THIS_TEST=$(awk -v to="$test_op" \
57 'BEGIN {print substr(to, index(to, "_") + 1);
58 exit; }')
59 export THIS_OP=${test_op%_${THIS_TEST}}
60
61 if [ ! -e sql/$THIS_TEST ] ; then
62 printf '%s: No such test\n' $THIS_TEST >&2
63 exit 1
64 fi
65
66 export RUN_TEST_SOCK=$RUN_TEST_SOCK
67 mkdir $tsock_dir
68 socat -u UNIX-RECV:$RUN_TEST_SOCK,mode=0600 \
69 EXEC:"'$PSQL $psql_args'" \
70 > $tfile &
71
72 # Wait for the socket to be available.
73 # Do this in a loop in case something else messes with the directory.
74 # Use -t 1 to break out of race condition where creation happens
75 # between the [] and inotifywait execution.
76 while [ ! -e $RUN_TEST_SOCK ] ; do
77 inotifywait -qq -t 1 -e create $tsock_dir
78 done;
79
80 # Initialize psql environment.
81 # Capture the error stream, never show context for errors.
82 printf '\\pset estream query\n\\set SHOW_CONTEXT never\n' | exec_sql
83
84 # Run the test, and it's prerequsites.
85 make -s -R -f run_test.mkf "$test_op" 4>&2 2>/dev/null
86 test_error=$?
87
88 if [ -e loaded/begin ] \
89 && [ $test_error -eq 0 ] \
90 && [ ! -e loaded/commit ] \
91 && [ $THIS_TEST != 'commit' ] ; then
92 # Still no commit. (Must not raise an error in a transaction but
93 # still require a transaction.) Test for successful commit.
94 if [ "$THIS_OP" = 'show' ] ; then
95 # We want to see what the commit does.
96 printf ' ------------------------------<showing commit>--------------------------------\n'
97 sh <run_sql.sh run_sql.sh show "$psql_args" sql/commit
98 else
99 sh <run_sql.sh run_sql.sh testcommit "$psql_args" sql/commit
100 fi
101 test_error=$?
102 fi
103
104 # Shut down psql.
105 printf '\\q\n' | exec_sql
106 wait %%
107 psql_error=$?
108
109 if [ $psql_error -ne 0 ] ; then
110 touch $tfile ;# just in case
111 cat $tfile >&2
112 echo exiting psql error
113 exit $psql_error
114 fi
115
116 exit $test_error