From 994c58197e876458c2f4cf4cbf95240a938910ec Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Fri, 4 Jul 2014 14:12:11 +0900 Subject: [PATCH 2/3] Extract generic bash initialization process from pg_upgrade Such initialization is useful as well for some other utilities and makes test settings consistent. --- contrib/pg_upgrade/test.sh | 47 +++-------------------------------- src/test/shell/init_env.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 43 deletions(-) create mode 100644 src/test/shell/init_env.sh diff --git a/contrib/pg_upgrade/test.sh b/contrib/pg_upgrade/test.sh index 7bbd2c7..2e1c61a 100644 --- a/contrib/pg_upgrade/test.sh +++ b/contrib/pg_upgrade/test.sh @@ -9,24 +9,14 @@ # Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group # Portions Copyright (c) 1994, Regents of the University of California -set -e - -: ${MAKE=make} - -# Guard against parallel make issues (see comments in pg_regress.c) -unset MAKEFLAGS -unset MAKELEVEL - -# Establish how the server will listen for connections -testhost=`uname -s` +# Initialize test +. ../../src/test/shell/init_env.sh case $testhost in MINGW*) - LISTEN_ADDRESSES="localhost" PGHOST=""; unset PGHOST ;; *) - LISTEN_ADDRESSES="" # Select a socket directory. The algorithm is from the "configure" # script; the outcome mimics pg_regress.c:make_temp_sockdir(). PGHOST=$PG_REGRESS_SOCK_DIR @@ -102,37 +92,8 @@ logdir=$PWD/log rm -rf "$logdir" mkdir "$logdir" -# Clear out any environment vars that might cause libpq to connect to -# the wrong postmaster (cf pg_regress.c) -# -# Some shells, such as NetBSD's, return non-zero from unset if the variable -# is already unset. Since we are operating under 'set -e', this causes the -# script to fail. To guard against this, set them all to an empty string first. -PGDATABASE=""; unset PGDATABASE -PGUSER=""; unset PGUSER -PGSERVICE=""; unset PGSERVICE -PGSSLMODE=""; unset PGSSLMODE -PGREQUIRESSL=""; unset PGREQUIRESSL -PGCONNECT_TIMEOUT=""; unset PGCONNECT_TIMEOUT -PGHOSTADDR=""; unset PGHOSTADDR - -# Select a non-conflicting port number, similarly to pg_regress.c -PG_VERSION_NUM=`grep '#define PG_VERSION_NUM' "$newsrc"/src/include/pg_config.h | awk '{print $3}'` -PGPORT=`expr $PG_VERSION_NUM % 16384 + 49152` -export PGPORT - -i=0 -while psql -X postgres /dev/null -do - i=`expr $i + 1` - if [ $i -eq 16 ] - then - echo port $PGPORT apparently in use - exit 1 - fi - PGPORT=`expr $PGPORT + 1` - export PGPORT -done +# Get a port to run the tests +pg_get_test_port "$newsrc" # buildfarm may try to override port via EXTRA_REGRESS_OPTS ... EXTRA_REGRESS_OPTS="$EXTRA_REGRESS_OPTS --port=$PGPORT" diff --git a/src/test/shell/init_env.sh b/src/test/shell/init_env.sh new file mode 100644 index 0000000..e4d6cdb --- /dev/null +++ b/src/test/shell/init_env.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +# src/test/shell/init.sh +# +# Utility initializing environment for tests to be conducted in shell. +# The initialization done here is made to be platform-proof. + +set -e + +: ${MAKE=make} + +# Guard against parallel make issues (see comments in pg_regress.c) +unset MAKEFLAGS +unset MAKELEVEL + +# Set listen_addresses desirably +testhost=`uname -s` +case $testhost in + MINGW*) LISTEN_ADDRESSES="localhost" ;; + *) LISTEN_ADDRESSES="" ;; +esac + +# Clear out any environment vars that might cause libpq to connect to +# the wrong postmaster (cf pg_regress.c) +# +# Some shells, such as NetBSD's, return nonzero from unset if the variable +# is already unset. Since we are operating under 'set e', this causes the +# script to fail. To guard against this, set them all to an empty string first. +PGDATABASE=""; unset PGDATABASE +PGUSER=""; unset PGUSER +PGSERVICE=""; unset PGSERVICE +PGSSLMODE=""; unset PGSSLMODE +PGREQUIRESSL=""; unset PGREQUIRESSL +PGCONNECT_TIMEOUT=""; unset PGCONNECT_TIMEOUT +PGHOST=""; unset PGHOST +PGHOSTADDR=""; unset PGHOSTADDR + +# Select a non-conflicting port number, similarly to pg_regress.c, and +# save its value in PGPORT. Caller should either save or use this value +# for the tests. +pg_get_test_port() +{ + PG_ROOT_DIR=$1 + PG_VERSION_NUM=`grep '#define PG_VERSION_NUM' "$PG_ROOT_DIR"/src/include/pg_config.h | awk '{print $3}'` + PGPORT=`expr $PG_VERSION_NUM % 16384 + 49152` + PGPORT_START=$PGPORT + export PGPORT + + i=0 + while psql -X postgres /dev/null + do + i=`expr $i + 1` + if [ $i -eq 16 ] + then + echo "Ports from $PGPORT_START to $PGPORT are apparently in use" + exit 1 + fi + PGPORT=`expr $PGPORT + 1` + export PGPORT + done +} -- 2.0.1