2 # $PostgreSQL: pgsql/src/test/regress/pg_regress.sh,v 1.61 2005/11/01 15:09:11 adunstan Exp $
6 TMPFILE=$TMPDIR/pg_regress.$$
9 PostgreSQL regression test driver
11 Usage: $me [options...] [extra tests...]
14 --dbname=DB use database DB (default \`regression')
15 --debug turn on debug mode in programs that are run
16 --inputdir=DIR take input files from DIR (default \`.')
17 --load-language=lang load the named language before running the
18 tests; can appear multiple times
19 --max-connections=N maximum number of concurrent connections
20 (default is 0 meaning unlimited)
21 --multibyte=ENCODING use ENCODING as the multibyte encoding, and
22 also run a test by the same name
23 --outputdir=DIR place output files in DIR (default \`.')
24 --schedule=FILE use test ordering schedule from FILE
25 (may be used multiple times to concatenate)
26 --temp-install[=DIR] create a temporary installation (in DIR)
27 --no-locale use C locale
29 Options for \`temp-install' mode:
30 --top-builddir=DIR (relative) path to top level build directory
31 --temp-port=PORT port number to start temp postmaster on
33 Options for using an existing installation:
34 --host=HOST use postmaster running on HOST
35 --port=PORT use postmaster running at PORT
36 --user=USER connect as USER
38 The exit status is 0 if all tests passed, 1 if some tests failed, and 2
39 if the tests could not be run for some reason.
41 Report bugs to <pgsql-bugs@postgresql.org>."
45 _dashes='==============' # 14
47 _msg=`echo "$1$_spaces" | cut -c 1-38`
48 echo "$_dashes $_msg $_dashes"
53 # Unset locale settings
56 unset LC_COLLATE LC_CTYPE LC_MONETARY LC_MESSAGES LC_NUMERIC LC_TIME LC_ALL LANG LANGUAGE
58 # On Windows the default locale may not be English, so force it
59 case $host_platform in
60 *-*-cygwin*|*-*-mingw32*)
68 # Check for echo -n vs echo \c
71 if echo '\c' | grep c >/dev/null 2>&1; then
81 # Initialize default settings
90 host_platform='@host_tuple@'
91 enable_shared='@enable_shared@'
94 if [ "$GCC" = yes ]; then
118 # Parse command line options
128 echo "pg_regress (PostgreSQL @VERSION@)"
131 dbname=`expr "x$1" : "x--dbname=\(.*\)"`
137 inputdir=`expr "x$1" : "x--inputdir=\(.*\)"`
140 lang=`expr "x$1" : "x--load-language=\(.*\)"`
141 load_langs="$load_langs $lang"
145 multibyte=`expr "x$1" : "x--multibyte=\(.*\)"`
151 temp_install=./tmp_check
154 temp_install=`expr "x$1" : "x--temp-install=\(.*\)"`
157 maxconnections=`expr "x$1" : "x--max-connections=\(.*\)"`
160 outputdir=`expr "x$1" : "x--outputdir=\(.*\)"`
163 foo=`expr "x$1" : "x--schedule=\(.*\)"`
164 schedule="$schedule $foo"
167 top_builddir=`expr "x$1" : "x--top-builddir=\(.*\)"`
170 temp_port=`expr "x$1" : "x--temp-port=\(.*\)"`
173 PGHOST=`expr "x$1" : "x--host=\(.*\)"`
178 PGPORT=`expr "x$1" : "x--port=\(.*\)"`
182 PGUSER=`expr "x$1" : "x--user=\(.*\)"`
186 echo "$me: invalid argument $1" 1>&2
189 extra_tests="$extra_tests $1"
195 # warn of Cygwin likely failure if maxconnections = 0
196 # and we are running parallel tests
199 case $host_platform in
203 if [ $maxconnections -eq 0 ] ; then
204 echo Using unlimited parallel connections is likely to fail or hang on Cygwin.
205 echo Try \"$me --max-connections=n\" or \"gmake MAX_CONNECTIONS=n check\"
206 echo with n = 5 or 10 if this happens.
216 # On some platforms we can't use Unix sockets.
218 case $host_platform in
219 *-*-cygwin* | *-*-mingw32* | *-*-qnx* | *beos*)
227 # Set up diff to ignore horizontal white space differences.
230 case $host_platform in
231 *-*-qnx* | *-*-sco3.2v5*)
239 # Set backend timezone and datestyle explicitly
241 # To pass the horology test in its current form, the postmaster must be
242 # started with PGDATESTYLE=ISO, while the frontend must be started with
243 # PGDATESTYLE=Postgres. We set the postmaster values here and change
244 # to the frontend settings after the postmaster has been started.
247 PGTZ='PST8PDT'; export PGTZ
248 PGDATESTYLE='ISO, MDY'; export PGDATESTYLE
252 # Exit trap to remove temp file and shut down postmaster
255 # Note: There are some stupid shells (even among recent ones) that
256 # ignore the argument to exit (as in `exit 1') if there is an exit
257 # trap. The trap (and thus the shell script) will then always exit
258 # with the result of the last shell command before the `exit'. Hence
259 # we have to write `(exit x); exit' below this point.
263 if [ -n "$postmaster_pid" ]; then
264 kill -2 "$postmaster_pid"
265 wait "$postmaster_pid"
268 rm -f "$TMPFILE" && exit $savestatus
271 trap 'exit_trap $?' 0
275 echo; echo "caught signal"
276 if [ -n "$postmaster_pid" ]; then
277 echo "signalling fast shutdown to postmaster with pid $postmaster_pid"
278 kill -2 "$postmaster_pid"
279 wait "$postmaster_pid"
282 (exit $savestatus); exit
285 trap 'sig_trap $?' 1 2 13 15
290 # Scan resultmap file to find which platform-specific expected files to use.
291 # The format of each line of the file is
292 # testname/hostplatformpattern=substitutefile
293 # where the hostplatformpattern is evaluated per the rules of expr(1),
294 # namely, it is a standard regular expression with an implicit ^ at the start.
295 # What hostplatformpattern will be matched against is the config.guess output
296 # followed by either ':gcc' or ':cc' (independent of the actual name of the
297 # compiler executable).
299 # The tempfile hackery is needed because some shells will run the loop
300 # inside a subshell, whereupon shell variables set therein aren't seen
301 # outside the loop :-(
304 cat /dev/null >$TMPFILE
305 if [ -f "$inputdir/resultmap" ]
309 HOSTPAT=`expr "$LINE" : '.*/\(.*\)='`
310 if [ `expr "$host_platform:$compiler" : "$HOSTPAT"` -ne 0 ]
312 # remove hostnamepattern from line so that there are no shell
313 # wildcards in SUBSTLIST; else later 'for' could expand them!
314 TESTNAME=`expr "$LINE" : '\(.*\)/'`
315 SUBST=`echo "$LINE" | sed 's/^.*=//'`
316 echo "$TESTNAME=$SUBST" >> $TMPFILE
318 done <"$inputdir/resultmap"
320 SUBSTLIST=`cat $TMPFILE`
324 LOGDIR=$outputdir/log
326 if [ x"$temp_install" != x"" ]
328 if echo x"$temp_install" | grep -v '^x/' >/dev/null 2>&1; then
329 temp_install="`pwd`/$temp_install"
332 bindir=$temp_install/install/$bindir
333 libdir=$temp_install/install/$libdir
334 datadir=$temp_install/install/$datadir
335 PGDATA=$temp_install/data
337 if [ "$unix_sockets" = no ]; then
346 # since Makefile isn't very bright, check for out-of-range temp_port
347 if [ "$temp_port" -ge 1024 -a "$temp_port" -le 65535 ] ; then
354 # Get rid of environment stuff that might cause psql to misbehave
355 # while contacting our temp installation
356 unset PGDATABASE PGUSER PGSERVICE PGSSLMODE PGREQUIRESSL PGCONNECT_TIMEOUT
359 # Set up shared library paths, needed by psql and pg_encoding
360 # (if you run multibyte). LD_LIBRARY_PATH covers many platforms.
361 # DYLD_LIBRARY_PATH works on Darwin, and maybe other Mach-based systems.
362 # Feel free to account for others as well.
365 if [ -n "$LD_LIBRARY_PATH" ]; then
366 LD_LIBRARY_PATH="$libdir:$LD_LIBRARY_PATH"
368 LD_LIBRARY_PATH=$libdir
370 export LD_LIBRARY_PATH
372 if [ -n "$DYLD_LIBRARY_PATH" ]; then
373 DYLD_LIBRARY_PATH="$libdir:$DYLD_LIBRARY_PATH"
375 DYLD_LIBRARY_PATH=$libdir
377 export DYLD_LIBRARY_PATH
380 # Windows needs shared libraries in PATH. (Only those linked into
381 # executables, not dlopen'ed ones)
383 case $host_platform in
384 *-*-cygwin*|*-*-mingw32*)
390 if [ -d "$temp_install" ]; then
391 message "removing existing temp installation"
392 rm -rf "$temp_install"
395 message "creating temporary installation"
396 if [ ! -d "$LOGDIR" ]; then
397 mkdir -p "$LOGDIR" || { (exit 2); exit; }
399 $GMAKE -C "$top_builddir" DESTDIR="$temp_install/install" install with_perl=no with_python=no >"$LOGDIR/install.log" 2>&1
404 echo "$me: installation failed"
405 echo "Examine $LOGDIR/install.log for the reason."
410 message "initializing database system"
411 [ "$debug" = yes ] && initdb_options="--debug"
412 [ "$nolocale" = yes ] && initdb_options="$initdb_options --no-locale"
413 "$bindir/initdb" -D "$PGDATA" -L "$datadir" --noclean $initdb_options >"$LOGDIR/initdb.log" 2>&1
418 echo "$me: initdb failed"
419 echo "Examine $LOGDIR/initdb.log for the reason."
429 message "starting postmaster"
430 [ "$debug" = yes ] && postmaster_options="$postmaster_options -d 5"
431 if [ "$unix_sockets" = no ]; then
432 postmaster_options="$postmaster_options -c listen_addresses=$hostname"
434 postmaster_options="$postmaster_options -c listen_addresses="
436 "$bindir/postmaster" -D "$PGDATA" -F $postmaster_options >"$LOGDIR/postmaster.log" 2>&1 &
439 # Wait till postmaster is able to accept connections (normally only
440 # a second or so, but Cygwin is reportedly *much* slower). Don't
441 # wait forever, however.
444 until "$bindir/psql" -X $psql_options postgres </dev/null 2>/dev/null
451 if kill -0 $postmaster_pid >/dev/null 2>&1
460 if kill -0 $postmaster_pid >/dev/null 2>&1
462 echo "running on port $PGPORT with pid $postmaster_pid"
465 echo "$me: postmaster did not start"
466 echo "Examine $LOGDIR/postmaster.log for the reason."
471 else # not temp-install
474 # Windows needs shared libraries in PATH. (Only those linked into
475 # executables, not dlopen'ed ones)
477 case $host_platform in
478 *-*-cygwin*|*-*-mingw32*)
484 if [ -n "$PGPORT" ]; then
485 port_info="port $PGPORT"
487 port_info="default port"
490 if [ -n "$PGHOST" ]; then
491 echo "(using postmaster on $PGHOST, $port_info)"
493 if [ "$unix_sockets" = no ]; then
494 echo "(using postmaster on localhost, $port_info)"
496 echo "(using postmaster on Unix socket, $port_info)"
500 message "dropping database \"$dbname\""
501 "$bindir/dropdb" $psql_options "$dbname"
502 # errors can be ignored
507 # Set up SQL shell for the test.
510 PSQL="$bindir/psql -a -q -X $psql_options"
514 # Set frontend timezone and datestyle explicitly
517 PGTZ='PST8PDT'; export PGTZ
518 PGDATESTYLE='Postgres, MDY'; export PGDATESTYLE
522 # Set up multibyte environment
525 if [ -n "$multibyte" ]; then
526 PGCLIENTENCODING=$multibyte
527 export PGCLIENTENCODING
528 encoding_opt="-E $multibyte"
530 unset PGCLIENTENCODING
535 # Create the regression database
536 # We use template0 so that any installation-local cruft in template1
537 # will not mess up the tests.
540 message "creating database \"$dbname\""
541 "$bindir/createdb" $encoding_opt $psql_options --template template0 "$dbname"
542 if [ $? -ne 0 ]; then
543 echo "$me: createdb failed"
547 "$bindir/psql" -q -X $psql_options -c "\
548 alter database \"$dbname\" set lc_messages to 'C';
549 alter database \"$dbname\" set lc_monetary to 'C';
550 alter database \"$dbname\" set lc_numeric to 'C';
551 alter database \"$dbname\" set lc_time to 'C';" "$dbname"
552 if [ $? -ne 0 ]; then
553 echo "$me: could not set database default locales"
559 # Remove regressuser* and regressgroup* user accounts.
562 message "dropping regression test user accounts"
563 "$bindir/psql" -q -X $psql_options -c 'DROP GROUP regressgroup1; DROP GROUP regressgroup2; DROP USER regressuser1, regressuser2, regressuser3, regressuser4;' $dbname 2>/dev/null
564 if [ $? -eq 2 ]; then
565 echo "$me: could not drop user accounts"
571 # Install any requested PL languages
574 if [ "$enable_shared" = yes ]; then
575 for lang in xyzzy $load_langs ; do
576 if [ "$lang" != "xyzzy" ]; then
577 message "installing $lang"
578 "$bindir/createlang" $psql_options $lang $dbname
579 if [ $? -ne 0 ] && [ $? -ne 2 ]; then
580 echo "$me: createlang $lang failed"
592 message "running regression test queries"
594 if [ ! -d "$outputdir/results" ]; then
595 mkdir -p "$outputdir/results" || { (exit 2); exit; }
597 result_summary_file=$outputdir/regression.out
598 diff_file=$outputdir/regression.diffs
600 cat /dev/null >"$result_summary_file"
601 cat /dev/null >"$diff_file"
605 [ "$enable_shared" != yes ] && echo "ignore: plpgsql"
606 cat $schedule </dev/null
607 for x in $extra_tests; do
610 ) | sed 's/[ ]*#.*//g' | \
615 [ -z "$line" ] && continue
619 if [ x"$1" = x"ignore:" ]; then
621 ignore_list="$ignore_list $@"
623 elif [ x"$1" != x"test:" ]; then
624 echo "$me:$schedule:$lno: syntax error"
634 if [ $# -eq 1 ]; then
636 formatted=`echo $1 | awk '{printf "%-20.20s", $1;}'`
637 $ECHO_N "test $formatted ... $ECHO_C"
638 ( $PSQL -d "$dbname" <"$inputdir/sql/$1.sql" >"$outputdir/results/$1.out" 2>&1 )&
641 # Start a parallel group
642 $ECHO_N "parallel group ($# tests): $ECHO_C"
643 if [ $maxconnections -gt 0 ] ; then
645 test $# -gt $maxconnections && $ECHO_N "(in groups of $maxconnections) $ECHO_C"
649 $PSQL -d "$dbname" <"$inputdir/sql/$name.sql" >"$outputdir/results/$name.out" 2>&1
650 $ECHO_N " $name$ECHO_C"
652 if [ $maxconnections -gt 0 ] ; then
653 connnum=`expr \( $connnum + 1 \) % $maxconnections`
654 test $connnum -eq 0 && wait
663 # (We do not want to run the diffs immediately after each test,
664 # because they would certainly get corrupted if run in parallel
669 if [ $# -ne 1 ]; then
670 formatted=`echo "$name" | awk '{printf "%-20.20s", $1;}'`
671 $ECHO_N " $formatted ... $ECHO_C"
674 # Check list extracted from resultmap to see if we should compare
675 # to a system-specific expected file.
676 # There shouldn't be multiple matches, but take the last if there are.
678 EXPECTED="$inputdir/expected/${name}"
679 for LINE in $SUBSTLIST
681 if [ `expr "$LINE" : "$name="` -ne 0 ]
683 SUBST=`echo "$LINE" | sed 's/^.*=//'`
684 EXPECTED="$inputdir/expected/${SUBST}"
688 # If there are multiple equally valid result files, loop to get the right one.
689 # If none match, diff against the closest one.
694 for thisfile in $EXPECTED.out ${EXPECTED}_[0-9].out; do
695 [ ! -r "$thisfile" ] && continue
696 diff $DIFFFLAGS $thisfile $outputdir/results/${name}.out >/dev/null 2>&1
700 1) thisdiff=`diff $DIFFFLAGS $thisfile $outputdir/results/${name}.out | wc -l`
701 if [ -z "$bestdiff" ] || [ "$thisdiff" -lt "$bestdiff" ]; then
702 bestdiff=$thisdiff; bestfile=$thisfile
709 # Now print the result.
715 ( diff $DIFFFLAGS -C3 $bestfile $outputdir/results/${name}.out
717 echo "======================================================================"
718 echo ) >> "$diff_file"
719 if echo " $ignore_list " | grep " $name " >/dev/null 2>&1 ; then
720 echo "failed (ignored)"
731 done | tee "$result_summary_file" 2>&1
739 if [ -n "$postmaster_pid" ]; then
740 message "shutting down postmaster"
741 "$bindir/pg_ctl" -s -D "$PGDATA" stop
742 wait "$postmaster_pid"
753 count_total=`cat "$result_summary_file" | grep '\.\.\.' | wc -l | sed 's/ //g'`
754 count_ok=`cat "$result_summary_file" | grep '\.\.\. ok' | wc -l | sed 's/ //g'`
755 count_failed=`cat "$result_summary_file" | grep '\.\.\. FAILED' | wc -l | sed 's/ //g'`
756 count_ignored=`cat "$result_summary_file" | grep '\.\.\. failed (ignored)' | wc -l | sed 's/ //g'`
759 if [ $count_total -eq $count_ok ]; then
760 msg="All $count_total tests passed."
762 elif [ $count_failed -eq 0 ]; then
763 msg="$count_ok of $count_total tests passed, $count_ignored failed test(s) ignored."
765 elif [ $count_ignored -eq 0 ]; then
766 msg="$count_failed of $count_total tests failed."
769 msg="`expr $count_failed + $count_ignored` of $count_total tests failed, $count_ignored of these failures ignored."
773 dashes=`echo " $msg " | sed 's/./=/g'`
779 if [ -s "$diff_file" ]; then
780 echo "The differences that caused some tests to fail can be viewed in the"
781 echo "file \`$diff_file'. A copy of the test summary that you see"
782 echo "above is saved in the file \`$result_summary_file'."
785 rm -f "$diff_file" "$result_summary_file"