2 # $PostgreSQL: pgsql/src/test/regress/pg_regress.sh,v 1.37 2003/11/29 19:52:14 pgsql Exp $
6 TMPFILE=$TMPDIR/pg_regress.$$
9 PostgreSQL regression test driver
11 Usage: $me [options...] [extra tests...]
14 --debug turn on debug mode in programs that are run
15 --inputdir=DIR take input files from DIR (default \`.')
16 --max-connections=N maximum number of concurrent connections
17 (default is 0 meaning unlimited)
18 --multibyte=ENCODING use ENCODING as the multibyte encoding, and
19 also run a test by the same name
20 --outputdir=DIR place output files in DIR (default \`.')
21 --schedule=FILE use test ordering schedule from FILE
22 (may be used multiple times to concatenate)
23 --temp-install[=DIR] create a temporary installation (in DIR)
25 Options for \`temp-install' mode:
26 --top-builddir=DIR (relative) path to top level build directory
28 Options for using an existing installation:
29 --host=HOST use postmaster running on HOST
30 --port=PORT use postmaster running at PORT
31 --user=USER connect as USER
33 The exit status is 0 if all tests passed, 1 if some tests failed, and 2
34 if the tests could not be run for some reason.
36 Report bugs to <pgsql-bugs@postgresql.org>."
40 _dashes='==============' # 14
42 _msg=`echo "$1$_spaces" | cut -c 1-38`
43 echo "$_dashes $_msg $_dashes"
48 # Unset locale settings
51 unset LC_COLLATE LC_CTYPE LC_MONETARY LC_MESSAGES LC_NUMERIC LC_TIME LC_ALL LANG LANGUAGE
55 # Check for echo -n vs echo \c
58 if echo '\c' | grep c >/dev/null 2>&1; then
68 # Initialize default settings
75 pkglibdir='@pkglibdir@'
78 host_platform='@host_tuple@'
79 enable_shared='@enable_shared@'
82 if [ "$GCC" = yes ]; then
103 # Parse command line options
113 echo "pg_regress (PostgreSQL @VERSION@)"
119 inputdir=`expr "x$1" : "x--inputdir=\(.*\)"`
122 multibyte=`expr "x$1" : "x--multibyte=\(.*\)"`
125 temp_install=./tmp_check
128 temp_install=`expr "x$1" : "x--temp-install=\(.*\)"`
131 maxconnections=`expr "x$1" : "x--max-connections=\(.*\)"`
134 outputdir=`expr "x$1" : "x--outputdir=\(.*\)"`
137 foo=`expr "x$1" : "x--schedule=\(.*\)"`
138 schedule="$schedule $foo"
141 top_builddir=`expr "x$1" : "x--top-builddir=\(.*\)"`
144 PGHOST=`expr "x$1" : "x--host=\(.*\)"`
149 PGPORT=`expr "x$1" : "x--port=\(.*\)"`
153 PGUSER=`expr "x$1" : "x--user=\(.*\)"`
157 echo "$me: invalid argument $1" 1>&2
160 extra_tests="$extra_tests $1"
166 # warn of Cygwin likely failure if maxconnections = 0
167 # and we are running parallel tests
170 case $host_platform in
174 if [ $maxconnections -eq 0 ] ; then
175 echo Using unlimited parallel connections is likely to fail or hang on Cygwin.
176 echo Try \"$me --max-connections=n\" or \"gmake MAX_CONNECTIONS=n check\"
177 echo with n = 5 or 10 if this happens.
187 # When on QNX or BeOS, don't use Unix sockets.
190 case $host_platform in
199 # Set up diff to ignore horizontal white space differences.
202 case $host_platform in
203 *-*-qnx* | *-*-sco3.2v5*)
211 # Set backend timezone and datestyle explicitly
213 # To pass the horology test in its current form, the postmaster must be
214 # started with PGDATESTYLE=ISO, while the frontend must be started with
215 # PGDATESTYLE=Postgres. We set the postmaster values here and change
216 # to the frontend settings after the postmaster has been started.
219 PGTZ='PST8PDT'; export PGTZ
220 PGDATESTYLE='ISO, MDY'; export PGDATESTYLE
224 # Exit trap to remove temp file and shut down postmaster
227 # Note: There are some stupid shells (even among recent ones) that
228 # ignore the argument to exit (as in `exit 1') if there is an exit
229 # trap. The trap (and thus the shell script) will then always exit
230 # with the result of the last shell command before the `exit'. Hence
231 # we have to write `(exit x); exit' below this point.
235 if [ -n "$postmaster_pid" ]; then
236 kill -2 "$postmaster_pid"
237 wait "$postmaster_pid"
240 rm -f "$TMPFILE" && exit $savestatus
245 echo; echo "caught signal"
246 if [ -n "$postmaster_pid" ]; then
247 echo "signalling fast shutdown to postmaster with pid $postmaster_pid"
248 kill -2 "$postmaster_pid"
249 wait "$postmaster_pid"
252 (exit $savestatus); exit
258 # Scan resultmap file to find which platform-specific expected files to use.
259 # The format of each line of the file is
260 # testname/hostplatformpattern=substitutefile
261 # where the hostplatformpattern is evaluated per the rules of expr(1),
262 # namely, it is a standard regular expression with an implicit ^ at the start.
263 # What hostplatformpattern will be matched against is the config.guess output
264 # followed by either ':gcc' or ':cc' (independent of the actual name of the
265 # compiler executable).
267 # The tempfile hackery is needed because some shells will run the loop
268 # inside a subshell, whereupon shell variables set therein aren't seen
269 # outside the loop :-(
272 cat /dev/null >$TMPFILE
273 if [ -f "$inputdir/resultmap" ]
277 HOSTPAT=`expr "$LINE" : '.*/\(.*\)='`
278 if [ `expr "$host_platform:$compiler" : "$HOSTPAT"` -ne 0 ]
280 # remove hostnamepattern from line so that there are no shell
281 # wildcards in SUBSTLIST; else later 'for' could expand them!
282 TESTNAME=`expr "$LINE" : '\(.*\)/'`
283 SUBST=`echo "$LINE" | sed 's/^.*=//'`
284 echo "$TESTNAME=$SUBST" >> $TMPFILE
286 done <"$inputdir/resultmap"
288 SUBSTLIST=`cat $TMPFILE`
292 LOGDIR=$outputdir/log
294 if [ x"$temp_install" != x"" ]
296 if echo x"$temp_install" | grep -v '^x/' >/dev/null 2>&1; then
297 temp_install="`pwd`/$temp_install"
300 bindir=$temp_install/install/$bindir
301 libdir=$temp_install/install/$libdir
302 pkglibdir=$temp_install/install/$pkglibdir
303 datadir=$temp_install/install/$datadir
304 PGDATA=$temp_install/data
306 if [ "$unix_sockets" = no ]; then
317 # Get rid of environment stuff that might cause psql to misbehave
318 # while contacting our temp installation
319 unset PGDATABASE PGUSER PGSERVICE PGSSLMODE PGREQUIRESSL PGCONNECT_TIMEOUT
322 # Set up shared library paths, needed by psql and pg_encoding
323 # (if you run multibyte). LD_LIBRARY_PATH covers many platforms,
324 # feel free to account for others as well.
327 if [ -n "$LD_LIBRARY_PATH" ]; then
328 LD_LIBRARY_PATH="$libdir:$LD_LIBRARY_PATH"
330 LD_LIBRARY_PATH=$libdir
332 export LD_LIBRARY_PATH
335 # Windows needs shared libraries in PATH. (Only those linked into
336 # executables, not dlopen'ed ones)
338 case $host_platform in
345 if [ -d "$temp_install" ]; then
346 message "removing existing temp installation"
347 rm -rf "$temp_install"
350 message "creating temporary installation"
351 if [ ! -d "$LOGDIR" ]; then
352 mkdir -p "$LOGDIR" || { (exit 2); exit; }
354 $GMAKE -C "$top_builddir" DESTDIR="$temp_install/install" install with_perl=no with_python=no >"$LOGDIR/install.log" 2>&1
359 echo "$me: installation failed"
360 echo "Examine $LOGDIR/install.log for the reason."
365 # fix conversion shared objs path
366 conv=$datadir/conversion_create.sql
369 sed -e "s@\$libdir@$pkglibdir@g" $backup > $conv
372 message "initializing database system"
373 [ "$debug" = yes ] && initdb_options='--debug'
374 "$bindir/initdb" -D "$PGDATA" -L "$datadir" --noclean $initdb_options >"$LOGDIR/initdb.log" 2>&1
379 echo "$me: initdb failed"
380 echo "Examine $LOGDIR/initdb.log for the reason."
390 message "starting postmaster"
391 [ "$debug" = yes ] && postmaster_options="$postmaster_options -d 5"
392 [ "$unix_sockets" = no ] && postmaster_options="$postmaster_options -i"
393 "$bindir/postmaster" -D "$PGDATA" -F $postmaster_options >"$LOGDIR/postmaster.log" 2>&1 &
396 # Wait till postmaster is able to accept connections (normally only
397 # a second or so, but Cygwin is reportedly *much* slower). Don't
398 # wait forever, however.
401 until "$bindir/psql" $psql_options template1 </dev/null 2>/dev/null
408 if kill -0 $postmaster_pid >/dev/null 2>&1
417 if kill -0 $postmaster_pid >/dev/null 2>&1
419 echo "running on port $PGPORT with pid $postmaster_pid"
422 echo "$me: postmaster did not start"
423 echo "Examine $LOGDIR/postmaster.log for the reason."
428 else # not temp-install
430 # If Unix sockets are not available, use the local host by default.
431 if [ "$unix_sockets" = no ]; then
437 if [ -n "$PGPORT" ]; then
438 port_info="port $PGPORT"
440 port_info="default port"
443 if [ -n "$PGHOST" ]; then
444 echo "(using postmaster on $PGHOST, $port_info)"
446 echo "(using postmaster on Unix socket, $port_info)"
448 message "dropping database \"$dbname\""
449 "$bindir/dropdb" $psql_options "$dbname"
450 # errors can be ignored
455 # Set up SQL shell for the test.
458 PSQL="$bindir/psql -a -q -X $psql_options"
462 # Set frontend timezone and datestyle explicitly
465 PGTZ='PST8PDT'; export PGTZ
466 PGDATESTYLE='Postgres, MDY'; export PGDATESTYLE
470 # Set up multibyte environment
473 if [ -n "$multibyte" ]; then
474 PGCLIENTENCODING=$multibyte
475 export PGCLIENTENCODING
476 encoding_opt="-E $multibyte"
478 unset PGCLIENTENCODING
483 # Create the regression database
484 # We use template0 so that any installation-local cruft in template1
485 # will not mess up the tests.
488 message "creating database \"$dbname\""
489 "$bindir/createdb" $encoding_opt $psql_options --template template0 "$dbname"
490 if [ $? -ne 0 ]; then
491 echo "$me: createdb failed"
495 "$bindir/psql" $psql_options -c "\
496 alter database \"$dbname\" set lc_messages to 'C';
497 alter database \"$dbname\" set lc_monetary to 'C';
498 alter database \"$dbname\" set lc_numeric to 'C';
499 alter database \"$dbname\" set lc_time to 'C';" "$dbname" 2>/dev/null
500 if [ $? -ne 0 ]; then
501 echo "$me: could not set database default locales"
507 # Remove regressuser* and regressgroup* user accounts.
510 message "dropping regression test user accounts"
511 "$bindir/psql" $psql_options -c 'DROP GROUP regressgroup1; DROP GROUP regressgroup2; DROP USER regressuser1, regressuser2, regressuser3, regressuser4;' $dbname 2>/dev/null
512 if [ $? -eq 2 ]; then
513 echo "$me: could not drop user accounts"
519 # Install the PL/pgSQL language in it
522 if [ "$enable_shared" = yes ]; then
523 message "installing PL/pgSQL"
524 "$bindir/createlang" -L "$pkglibdir" $psql_options plpgsql $dbname
525 if [ $? -ne 0 ] && [ $? -ne 2 ]; then
526 echo "$me: createlang failed"
536 message "running regression test queries"
538 if [ ! -d "$outputdir/results" ]; then
539 mkdir -p "$outputdir/results" || { (exit 2); exit; }
541 result_summary_file=$outputdir/regression.out
542 diff_file=$outputdir/regression.diffs
544 cat /dev/null >"$result_summary_file"
545 cat /dev/null >"$diff_file"
549 [ "$enable_shared" != yes ] && echo "ignore: plpgsql"
550 cat $schedule </dev/null
551 for x in $extra_tests; do
554 ) | sed 's/[ ]*#.*//g' | \
559 [ -z "$line" ] && continue
563 if [ x"$1" = x"ignore:" ]; then
565 ignore_list="$ignore_list $@"
567 elif [ x"$1" != x"test:" ]; then
568 echo "$me:$schedule:$lno: syntax error"
578 if [ $# -eq 1 ]; then
580 formatted=`echo $1 | awk '{printf "%-20.20s", $1;}'`
581 $ECHO_N "test $formatted ... $ECHO_C"
582 $PSQL -d "$dbname" <"$inputdir/sql/$1.sql" >"$outputdir/results/$1.out" 2>&1
584 # Start a parallel group
585 $ECHO_N "parallel group ($# tests): $ECHO_C"
586 if [ $maxconnections -gt 0 ] ; then
588 test $# -gt $maxconnections && $ECHO_N "(in groups of $maxconnections) $ECHO_C"
592 $PSQL -d "$dbname" <"$inputdir/sql/$name.sql" >"$outputdir/results/$name.out" 2>&1
593 $ECHO_N " $name$ECHO_C"
595 if [ $maxconnections -gt 0 ] ; then
596 connnum=`expr \( $connnum + 1 \) % $maxconnections`
597 test $connnum -eq 0 && wait
606 # (We do not want to run the diffs immediately after each test,
607 # because they would certainly get corrupted if run in parallel
612 if [ $# -ne 1 ]; then
613 formatted=`echo "$name" | awk '{printf "%-20.20s", $1;}'`
614 $ECHO_N " $formatted ... $ECHO_C"
617 # Check list extracted from resultmap to see if we should compare
618 # to a system-specific expected file.
619 # There shouldn't be multiple matches, but take the last if there are.
621 EXPECTED="$inputdir/expected/${name}"
622 for LINE in $SUBSTLIST
624 if [ `expr "$LINE" : "$name="` -ne 0 ]
626 SUBST=`echo "$LINE" | sed 's/^.*=//'`
627 EXPECTED="$inputdir/expected/${SUBST}"
631 # If there are multiple equally valid result files, loop to get the right one.
632 # If none match, diff against the closest one.
637 for thisfile in $EXPECTED.out ${EXPECTED}_[0-9].out; do
638 [ ! -r "$thisfile" ] && continue
639 diff $DIFFFLAGS $thisfile $outputdir/results/${name}.out >/dev/null 2>&1
643 1) thisdiff=`diff $DIFFFLAGS $thisfile $outputdir/results/${name}.out | wc -l`
644 if [ -z "$bestdiff" ] || [ "$thisdiff" -lt "$bestdiff" ]; then
645 bestdiff=$thisdiff; bestfile=$thisfile
652 # Now print the result.
658 ( diff $DIFFFLAGS -C3 $bestfile $outputdir/results/${name}.out
660 echo "======================================================================"
661 echo ) >> "$diff_file"
662 if echo " $ignore_list " | grep " $name " >/dev/null 2>&1 ; then
663 echo "failed (ignored)"
674 done | tee "$result_summary_file" 2>&1
682 if [ -n "$postmaster_pid" ]; then
683 message "shutting down postmaster"
684 kill -15 "$postmaster_pid"
685 wait "$postmaster_pid"
696 count_total=`cat "$result_summary_file" | grep '\.\.\.' | wc -l | sed 's/ //g'`
697 count_ok=`cat "$result_summary_file" | grep '\.\.\. ok' | wc -l | sed 's/ //g'`
698 count_failed=`cat "$result_summary_file" | grep '\.\.\. FAILED' | wc -l | sed 's/ //g'`
699 count_ignored=`cat "$result_summary_file" | grep '\.\.\. failed (ignored)' | wc -l | sed 's/ //g'`
702 if [ $count_total -eq $count_ok ]; then
703 msg="All $count_total tests passed."
705 elif [ $count_failed -eq 0 ]; then
706 msg="$count_ok of $count_total tests passed, $count_ignored failed test(s) ignored."
708 elif [ $count_ignored -eq 0 ]; then
709 msg="$count_failed of $count_total tests failed."
712 msg="`expr $count_failed + $count_ignored` of $count_total tests failed, $count_ignored of these failures ignored."
716 dashes=`echo " $msg " | sed 's/./=/g'`
722 if [ -s "$diff_file" ]; then
723 echo "The differences that caused some tests to fail can be viewed in the"
724 echo "file \`$diff_file'. A copy of the test summary that you see"
725 echo "above is saved in the file \`$result_summary_file'."
728 rm -f "$diff_file" "$result_summary_file"