While we don't have a lot of ports that need database server to be
set up and running, such setup is too tricky to be safely copied
each time.

I propose the following databases/postgresql port module to handle
such cases. It was tested on databases/libpqxx and geo/pgrouting,
giving more or less sane results. I've added diffs for those two
ports as well, to allow seeing how it fits. The port-modules(5)
tweaks will follow if this will go in.

The patch test-runner.pl could be smaller, I just tried to use
upstream-friendly form. Didn't try to send it upstream yet, though.

Okays, rejections and other comments are welcome.

--
WBR,
  Vadim Zhukov


Index: databases/postgresql/postgresql.port.mk
===================================================================
RCS file: databases/postgresql/postgresql.port.mk
diff -N databases/postgresql/postgresql.port.mk
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ databases/postgresql/postgresql.port.mk     19 Dec 2014 22:08:49 -0000
@@ -0,0 +1,43 @@
+# $OpenBSD$ 
+#
+# Helps testing PostgreSQL-based software, no B/L/R-DEPS here.
+
+MODPOSTGRESQL_TEST_CMD ?= \
+       ${MAKE_PROGRAM} ${ALL_TEST_FLAGS} -f ${MAKE_FILE} ${TEST_TARGET}
+
+MODPOSTGRESQL_TEST_PGHOST ?=   ${WRKDIR}
+_MODPOSTGRESQL_TEST_PGDATA =   ${WRKDIR}/testdb
+
+TEST_DEPENDS +=                databases/postgresql,-server
+TEST_ENV +=            PGDATA=${_MODPOSTGRESQL_TEST_PGDATA} \
+                       PGHOST=${MODPOSTGRESQL_TEST_PGHOST}
+.ifdef MODPOSTGRESQL_TEST_DBNAME
+TEST_ENV +=            PGDATABASE=${MODPOSTGRESQL_TEST_DBNAME}
+.endif
+
+MODPOSTGRESQL_TEST_TARGET = \
+       rm -Rf ${_MODPOSTGRESQL_TEST_PGDATA}; \
+       export ${ALL_TEST_ENV}; \
+       ${LOCALBASE}/bin/initdb -D ${_MODPOSTGRESQL_TEST_PGDATA} \
+           -A trust --locale=C; \
+       ${LOCALBASE}/bin/pg_ctl start -w -D ${_MODPOSTGRESQL_TEST_PGDATA} \
+           -l ${WRKDIR}/pg-test.log \
+           -o "-F -h '' -k ${MODPOSTGRESQL_TEST_PGHOST}";
+.ifdef MODPOSTGRESQL_TEST_DBNAME
+MODPOSTGRESQL_TEST_TARGET += \
+       ${LOCALBASE}/bin/createdb ${MODPOSTGRESQL_TEST_DBNAME} || \
+           (${LOCALBASE}/bin/pg_ctl stop -D ${_MODPOSTGRESQL_TEST_PGDATA} \
+            -m i && exit 1);
+.endif
+MODPOSTGRESQL_TEST_TARGET += \
+       set +e; \
+       cd ${WRKBUILD}; \
+       ${MODPOSTGRESQL_TEST_CMD}; \
+       Q=$$?; \
+       ${LOCALBASE}/bin/pg_ctl stop -D ${_MODPOSTGRESQL_TEST_PGDATA} -m i; \
+       exit $$Q
+
+.if !target(do-test)
+do-test:
+       ${MODPOSTGRESQL_TEST_TARGET}
+.endif
Index: databases/libpqxx/Makefile
===================================================================
RCS file: /cvs/ports/databases/libpqxx/Makefile,v
retrieving revision 1.18
diff -u -p -r1.18 Makefile
--- databases/libpqxx/Makefile  2 Oct 2014 10:16:34 -0000       1.18
+++ databases/libpqxx/Makefile  19 Dec 2014 22:08:49 -0000
@@ -18,7 +18,7 @@ MASTER_SITES= http://pqxx.org/download/s
 
 LIB_DEPENDS=   databases/postgresql
 
-MODULES=       lang/python
+MODULES=       databases/postgresql lang/python
 MODPY_RUNDEP=  No
 MODPY_ADJ_FILES=tools/splitconfig \
                tools/template2mak.py
@@ -29,27 +29,6 @@ SEPARATE_BUILD=      No
 CONFIGURE_STYLE=gnu
 CONFIGURE_ARGS=        ${CONFIGURE_SHARED}
 
-# Regression tests start up and use a real database
-TEST_DEPENDS =         databases/postgresql,-server
-PGDATA =               ${WRKDIR}/testdb
-PGDATABASE =           pqxx-test
-TEST_ENV =             PGDATABASE=${PGDATABASE} \
-                       PGHOST=${WRKDIR}
-
-# Make sure that server is always stopped after starting
-do-test:
-       rm -Rf ${PGDATA}
-       ${LOCALBASE}/bin/initdb -D ${PGDATA} -A trust --locale=C
-       ${LOCALBASE}/bin/pg_ctl start -w -D ${PGDATA} -l ${WRKDIR}/pg.log \
-               -o "-F -h '' -k ${WRKDIR}"
-       ${TEST_ENV} ${LOCALBASE}/bin/createdb ${PGDATABASE} || \
-           (${LOCALBASE}/bin/pg_ctl stop -D ${PGDATA} -m i; exit 1)
-       cd ${WRKBUILD}; \
-           set +e; \
-           ${SETENV} ${ALL_TEST_ENV} ${MAKE_PROGRAM} ${ALL_TEST_FLAGS} \
-               -f ${MAKE_FILE} ${TEST_TARGET}; \
-           Q=$$?; \
-           ${LOCALBASE}/bin/pg_ctl stop -D ${PGDATA} -m i; \
-           exit $$Q
+MODPOSTGRESQL_TEST_DBNAME =    pqxx-test
 
 .include <bsd.port.mk>
Index: geo/pgrouting/Makefile
===================================================================
RCS file: /cvs/ports/geo/pgrouting/Makefile,v
retrieving revision 1.2
diff -u -p -r1.2 Makefile
--- geo/pgrouting/Makefile      19 Dec 2014 21:16:55 -0000      1.2
+++ geo/pgrouting/Makefile      19 Dec 2014 22:08:49 -0000
@@ -20,20 +20,17 @@ MAINTAINER =        Landry Breuil <landry@openb
 # GPLv2
 PERMIT_PACKAGE_CDROM=  Yes
 
-MODULES =      devel/cmake
+MODULES =      databases/postgresql devel/cmake
 
 LIB_DEPENDS =  math/cgal
 RUN_DEPENDS =  geo/postgis
 BUILD_DEPENDS =        databases/postgresql,-server
 TEST_DEPENDS = ${BUILD_PKGPATH}
+TEST_ENV =     LD_PRELOAD=libpthread.so
 WANTLIB +=     CGAL boost_system-mt boost_thread-mt gmp m stdc++
 
-do-test:
-       regdir=$$(mktemp -t -d pgrouting-regress.XXXXXX) && \
-       LC_CTYPE=C initdb -D $${regdir} && \
-       LD_PRELOAD=libpthread.so pg_ctl -D $${regdir} -o '-p 12341' -l 
$${regdir}/log start && \
-       cd ${WRKSRC} && perl tools/test-runner.pl -pgport 12341 || \
-       pg_ctl -D $${regdir} -o '-p 12341'  stop -m fast
-       #not all tests pass - ensure we stop the server
+MODPOSTGRESQL_TEST_CMD = \
+       cd ${WRKSRC}; \
+       ./tools/test-runner.pl
 
 .include <bsd.port.mk>
Index: geo/pgrouting/patches/patch-tools_test-runner_pl
===================================================================
RCS file: /cvs/ports/geo/pgrouting/patches/patch-tools_test-runner_pl,v
retrieving revision 1.1
diff -u -p -r1.1 patch-tools_test-runner_pl
--- geo/pgrouting/patches/patch-tools_test-runner_pl    19 Dec 2014 21:16:55 
-0000      1.1
+++ geo/pgrouting/patches/patch-tools_test-runner_pl    19 Dec 2014 22:08:49 
-0000
@@ -1,13 +1,162 @@
 $OpenBSD: patch-tools_test-runner_pl,v 1.1 2014/12/19 21:16:55 landry Exp $
---- tools/test-runner.pl.orig  Fri Dec 19 21:33:45 2014
-+++ tools/test-runner.pl       Fri Dec 19 21:55:37 2014
-@@ -20,7 +20,8 @@ my $DRYRUN = 0;
+Allow Postgres utilities to use their preferred defaults, allowing to
+run tests in any environment supported by Postgres.
+--- tools/test-runner.pl.orig  Sat Dec 20 00:10:42 2014
++++ tools/test-runner.pl       Sat Dec 20 00:32:46 2014
+@@ -20,14 +20,16 @@ my $DRYRUN = 0;
  my $DEBUG = 0;
  
  my $DBNAME = "pgr_test__db__test";
 -my $DBUSER = 'postgres';
-+my $DBUSER = `whoami`;
-+chomp $DBUSER;
- my $DBHOST = 'localhost';
- my $DBPORT = '5432';
+-my $DBHOST = 'localhost';
+-my $DBPORT = '5432';
++my $DBUSER;
++my $DBHOST;
++my $DBPORT;
  
+ sub Usage {
+     die "Usage: test-runner.pl -pgver vpg -pgisver vpgis -psql 
/path/to/psql\n" .
+         "       -pgver vpg          - postgresql version\n" .
+-        "       -pgport port        - postgresql port to use (default: 
5432)\n" .
++        "       -pghost host        - postgresql host or socket directory to 
use\n" .
++        "       -pgport port        - postgresql port to use\n" .
++        "       -pguser username    - postgresql user role to use\n" .
+         "       -pgisver vpgis      - postgis version\n" .
+         "       -pgrver vpgr        - pgrouting version\n" .
+         "       -psql /path/to/psql - optional path to psql\n" .
+@@ -50,9 +52,15 @@ while (my $a = shift @ARGV) {
+     if ( $a eq '-pgver') {
+         $vpg   = shift @ARGV || Usage();
+     }
++    elsif ($a eq '-pghost') {
++        $DBHOST = shift @ARGV || Usage();
++    }
+     elsif ($a eq '-pgport') {
+         $DBPORT = shift @ARGV || Usage();
+     }
++    elsif ($a eq '-pguser') {
++        $DBUSER = shift @ARGV || Usage();
++    }
+     elsif ($a eq '-pgisver') {
+         $vpgis = shift @ARGV || Usage();
+     }
+@@ -92,8 +100,13 @@ while (my $a = shift @ARGV) {
+     }
+ }
+ 
+-mysystem("dropdb -U $DBUSER -h $DBHOST -p $DBPORT $DBNAME") if $clean;
++my $connopts = "";
++$connopts .= " -U $DBUSER" if defined $DBUSER;
++$connopts .= " -h $DBHOST" if defined $DBHOST;
++$connopts .= " -p $DBPORT" if defined $DBPORT;
+ 
++mysystem("dropdb $connopts $DBNAME") if $clean;
++
+ %main::tests = ();
+ my @cfgs = ();
+ my %stats = (z_pass=>0, z_fail=>0, z_crash=>0);
+@@ -181,7 +194,7 @@ sub run_test {
+ 
+     $res{comment} = $t->{comment} if $t->{comment};
+     for my $x (@{$t->{data}}) {
+-        mysystem("$psql -U $DBUSER -h $DBHOST -p $DBPORT -A -t -q -f 
'$dir/$x' $DBNAME >> $TMP2 2>\&1 ");
++        mysystem("$psql $connopts -A -t -q -f '$dir/$x' $DBNAME >> $TMP2 
2>\&1 ");
+     }
+ 
+     for my $x (@{$t->{tests}}) {
+@@ -192,7 +205,7 @@ sub run_test {
+             $stats{z_fail}++;
+             next;
+         };
+-        open(PSQL, "|$psql -U $DBUSER -h $DBHOST -p $DBPORT -A -t -q $DBNAME 
> $TMP 2>\&1 ") || do {
++        open(PSQL, "|$psql $connopts -A -t -q $DBNAME > $TMP 2>\&1 ") || do {
+             $res{"$dir/$x.test"} = "FAILED: could not open connection to db : 
$!";
+             $stats{z_fail}++;
+             next;
+@@ -257,7 +270,7 @@ sub createTestDB {
+     # first create a database with postgis installed in it
+     if (version_greater_eq($dbver, '9.1') &&
+             -f "$dbshare/extension/postgis.control") {
+-        mysystem("createdb -U $DBUSER -h $DBHOST -p $DBPORT $DBNAME");
++        mysystem("createdb $connopts $DBNAME");
+         die "ERROR: Failed to create database '$DBNAME'!\n"
+             unless dbExists($DBNAME);
+         my $myver = '';
+@@ -270,7 +283,7 @@ sub createTestDB {
+             $encoding = "SET client_encoding TO 'UTF8';";
+         }
+         print "-- Trying to install postgis extension $myver\n" if $DEBUG;
+-        mysystem("$psql -U $DBUSER -h $DBHOST -p $DBPORT -c \"$encoding 
create extension postgis $myver\" $DBNAME");
++        mysystem("$psql $connopts -c \"$encoding create extension postgis 
$myver\" $DBNAME");
+     }
+     else {
+         if ($vpgis && dbExists("template_postgis_$vpgis")) {
+@@ -283,7 +296,7 @@ sub createTestDB {
+             die "ERROR: Could not find an appropriate template_postgis 
database!\n";
+         }
+         print "-- Trying to install postgis from $template\n" if $DEBUG;
+-        mysystem("createdb -U $DBUSER -h $DBHOST -p $DBPORT -T $template 
$DBNAME");
++        mysystem("createdb $connopts -T $template $DBNAME");
+         sleep(2);
+         die "ERROR: Failed to create database '$DBNAME'!\n"
+             if ! dbExists($DBNAME);
+@@ -297,11 +310,11 @@ sub createTestDB {
+             $myver = " VERSION '$vpgr'";
+         }
+         print "-- Trying to install pgrouting extension $myver\n" if $DEBUG;
+-        mysystem("$psql -U $DBUSER -h $DBHOST -p $DBPORT -c \"create 
extension pgrouting $myver\" $DBNAME");
++        mysystem("$psql $connopts -c \"create extension pgrouting $myver\" 
$DBNAME");
+     }
+     elsif ($vpgr && -f "$dbshare/extension/pgrouting--$vpgr.sql") {
+         print "-- Trying to install pgrouting from 
'$dbshare/extension/pgrouting--$vpgr.sql'\n" if $DEBUG;
+-        mysystem("$psql -U $DBUSER -h $DBHOST -p $DBPORT -f 
'$dbshare/extension/pgrouting--$vpgr.sql' $DBNAME");
++        mysystem("$psql $connopts -f 
'$dbshare/extension/pgrouting--$vpgr.sql' $DBNAME");
+     }
+     else {
+         my $find = `find "$dbshare/contrib" -name pgrouting.sql | sort -r -n 
`;
+@@ -309,7 +322,7 @@ sub createTestDB {
+         my $file = shift @found;
+         if ($file && length($file)) {
+             print "-- Trying to install pgrouting from '$file'\n" if $DEBUG;
+-            mysystem("$psql -U $DBUSER -h $DBHOST -p $DBPORT -f '$file' 
$DBNAME");
++            mysystem("$psql $connopts -f '$file' $DBNAME");
+         }
+         else {
+             mysystem("ls -alR $dbshare") if $DEBUG;
+@@ -319,14 +332,14 @@ sub createTestDB {
+ 
+     # now verify that we have pgrouting installed
+ 
+-    my $pgrv = `$psql -U $DBUSER -h $DBHOST -p $DBPORT -c "select 
pgr_version()" $DBNAME`;
++    my $pgrv = `$psql $connopts -c "select pgr_version()" $DBNAME`;
+     die "ERROR: failed to install pgrouting into the database!\n"
+         unless $pgrv;
+ 
+ }
+ 
+ sub dropTestDB {
+-    mysystem("dropdb -U $DBUSER -h $DBHOST -p $DBPORT $DBNAME");
++    mysystem("dropdb $connopts $DBNAME");
+ }
+ 
+ sub version_greater_eq {
+@@ -353,8 +366,8 @@ sub version_greater_eq {
+ 
+ 
+ sub getServerVersion {
+-    my $v = `$psql -U $DBUSER -h $DBHOST -p $DBPORT -q -t -c "select 
version()" postgres`;
+-    print "$psql -U $DBUSER -h $DBHOST -p $DBPORT -q -t -c \"select 
version()\" postgres\n    # RETURNED: $v\n" if $VERBOSE;
++    my $v = `$psql $connopts -q -t -c "select version()" postgres`;
++    print "$psql $connopts -q -t -c \"select version()\" postgres\n    # 
RETURNED: $v\n" if $VERBOSE;
+     if ($v =~ m/PostgreSQL (\d+(\.\d+)?(\.\d+)?)/) {
+         print "    # Got ($1)\n" if $VERBOSE;
+         return $1;
+@@ -365,7 +378,7 @@ sub getServerVersion {
+ sub dbExists {
+     my $dbname = shift;
+ 
+-    my $isdb = `$psql -U $DBUSER -h $DBHOST -p $DBPORT -l | grep $dbname`;
++    my $isdb = `$psql $connopts -l | grep $dbname`;
+     $isdb =~ s/^\s*|\s*$//g;
+     return length($isdb);
+ }

Reply via email to