1 # Copyright (C) 2012 The Meme Factory, Inc. http://www.meme.com/
3 # This file is part of Gombe-MI.
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.
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.
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/>.
18 # Load data, possibly testing.
21 # run_sql [ prereq | test | testcommit | save | show ] psql_args file
23 # Env vars: THIS_TEST Name of test.
24 # RUN_TEST_SOCK Socket to pipe test sql into.
26 # Karl O. Pinc <kop@meme.com>
29 # prereq: Loads the data into the db and compares with results/$file
30 # to see if it succeeds.
31 # test: Load data into db, compare with results/$file and report result.
32 # testcommit: Run a commit for the test and report (final) result.
33 # save: Save the result as a successful run in results/$file.
34 # show: Print output of load on screen.
37 # Loads data into the db.
38 # Touches loaded/$file to let make know the load prerequsites are
40 # Actions prereq and test print to the screen. "test" leaves status on the
44 # Has a race condition. The initial inotifywait might, possibly,
45 # not establish it's wait until after the psql process has
46 # finished and closed the file. This is unlikely since we establish
47 # the wait before sending any sql, but possible. It all depends
48 # on the OS scheduler.
54 export file=$(basename $path)
56 export tfile=/tmp/$0_$$
57 export sigfile=/tmp/$0_sig_$$
60 socat -u STDIN UNIX-SENDTO:$RUN_TEST_SOCK
69 # if [ -e filters/$file ] ; then
70 # if [ ! -x filters/$file ] ; then
73 rm -rf $tfile $sigfile ;# also ends inotifywait process, if any
77 # Make the file which notes sql completion exist so we can wait on it.
79 inotifywait -qq -e close $sigfile &
80 ## Get out of the run queue to avoid the race condition.
81 # (Be sure to note in the README that this requires GNU sleep.)
85 if [ "$action" = "prereq" ] \
86 || [ "$action" = "test" ] \
87 || [ "$action" = "testcommit" ] ; then
88 printf '%-25s ........... ' "$THIS_TEST"
93 if [ ! -e results/$file ] ; then
94 printf 'fail (no saved results)\n'
99 if [ ! -e results/$file ] ; then
100 printf 'fail (prereq %s, no saved results)\n' $file
106 # Run the commands and capture the result.
107 printf '\\o %s\n' $tfile | exec_sql
108 # show what's executing
109 #printf '\n%s\n' $path
112 if [ -n "$INCLUDE_COMMIT" ] \
113 && [ "$file" = "$THIS_TEST" ] \
114 && [ "$THIS_TEST" != 'commit' ] ; then
115 # The test is finished, but requires the transaction be commited.
116 # (Capture the output of the transaction commit in the test output.)
117 cat sql/commit | exec_sql
121 printf '\\o\n' | exec_sql
123 # Write a file with content when we're done.
124 # With this we can be sure that we're really finished.
125 # (We can't just wait on closure of our data file because it
126 # might be somebody else that closed it.)
127 printf '\\o %s\n' "$sigfile" | exec_sql
128 printf "select 'done' as data;\n" | exec_sql
129 printf '\\o\n' | exec_sql
130 # Wait on the inotifywait for sql completion.
132 # Loop, in case it was somebody else that closed the file.
133 # (It would be nice to look for specific content, but
134 # if there's an aborted transaction then we'll get
135 # an error instead of our content.)
136 while [ -e $sigfile ] && [ ! -s $sigfile ] ; do
137 # Wait some more for file to close, but poll
138 # to break out of race condition. The
139 # file might have closed while we tested for done-ness.
140 inotifywait -t 1 -qq -e close $sigfile
143 cmp -s results/$file $tfile
145 if [ $result -ne 0 ] ; then
151 # This with -v for debugging when things go wonky and prerequsites
152 # don't load as prerequsites but load file when tested independenly.
153 # (Having to do this is a sign that you're running in a transaction
154 # and need to commit and start a new one.)
156 printf 'fail (prereq %s)\n' $file
164 if [ -e loaded/commit ] || [ ! -e loaded/begin ] ; then
165 # All done with transaction related stuff, test is done.
173 printf 'pass (%s)' $file
178 if [ "$action" = 'show' ] ; then
183 if [ "$action" = 'save' -a "$file" = "$THIS_TEST" ] ; then
184 mv $tfile results/$file