#!/bin/bash
set -e

source functions.sh

if [ "$1" = "--coverage" ]; then
	COVERAGE_ARG="$1"
	shift
fi

#export PATH=${PATH:+$PATH:}/sbin:/usr/sbin

check_for_cmd pg_config

# Start PostgreSQL process for tests
PGSQL_DATA=`mktemp -d /tmp/CEILO-PGSQL-XXXXX`
PGSQL_PATH=`pg_config --bindir`
PGSQL_PORT=9823
${PGSQL_PATH}/initdb -E UTF8 ${PGSQL_DATA}
LANGUAGE=C ${PGSQL_PATH}/pg_ctl -w -D ${PGSQL_DATA} -o "-N 100 -F -k ${PGSQL_DATA} -p ${PGSQL_PORT}" start
export CEILOMETER_TEST_PGSQL_URL="postgresql:///?host=${PGSQL_DATA}&port=${PGSQL_PORT}&dbname=template1"

# Yield execution to venv command
$*

ret=$?
${PGSQL_PATH}/pg_ctl -w -D ${PGSQL_DATA} -o "-p ${PGSQL_PORT}" stop
rm -rf ${PGSQL_DATA}
exit ${ret}

#!/bin/bash
set -e

source functions.sh

if [ "$1" = "--coverage" ]; then
	COVERAGE_ARG="$1"
	shift
fi

#export PATH=${PATH:+$PATH:}/sbin:/usr/sbin

check_for_cmd pg_config

# Start PostgreSQL process for tests
PGSQL_DATA=`mktemp -d /tmp/CEILO-PGSQL-XXXXX`
trap "clean_exit ${PGSQL_DATA}" EXIT
PGSQL_PATH=`pg_config --bindir`
${PGSQL_PATH}/initdb -E UTF8 ${PGSQL_DATA}
mkfifo ${PGSQL_DATA}/out
${PGSQL_PATH}/postgres -N 100 -F -k ${PGSQL_DATA} -D ${PGSQL_DATA} -p 9823 &> ${PGSQL_DATA}/out &
# Wait for PostgreSQL to start listening to connections
wait_for_line "database system is ready to accept connections" ${PGSQL_DATA}/out
export CEILOMETER_TEST_PGSQL_URL="postgresql:///?host=${PGSQL_DATA}&port=9823&dbname=template1"

# Yield execution to venv command
$*

--- 
+++ 
@@ -14,16 +14,14 @@
 
 # Start PostgreSQL process for tests
 PGSQL_DATA=`mktemp -d /tmp/CEILO-PGSQL-XXXXX`
+trap "clean_exit ${PGSQL_DATA}" EXIT
 PGSQL_PATH=`pg_config --bindir`
-PGSQL_PORT=9823
 ${PGSQL_PATH}/initdb -E UTF8 ${PGSQL_DATA}
-LANGUAGE=C ${PGSQL_PATH}/pg_ctl -w -D ${PGSQL_DATA} -o "-N 100 -F -k ${PGSQL_DATA} -p ${PGSQL_PORT}" start
-export CEILOMETER_TEST_PGSQL_URL="postgresql:///?host=${PGSQL_DATA}&port=${PGSQL_PORT}&dbname=template1"
+mkfifo ${PGSQL_DATA}/out
+${PGSQL_PATH}/postgres -N 100 -F -k ${PGSQL_DATA} -D ${PGSQL_DATA} -p 9823 &> ${PGSQL_DATA}/out &
+# Wait for PostgreSQL to start listening to connections
+wait_for_line "database system is ready to accept connections" ${PGSQL_DATA}/out
+export CEILOMETER_TEST_PGSQL_URL="postgresql:///?host=${PGSQL_DATA}&port=9823&dbname=template1"
 
 # Yield execution to venv command
 $*
-
-ret=$?
-${PGSQL_PATH}/pg_ctl -w -D ${PGSQL_DATA} -o "-p ${PGSQL_PORT}" stop
-rm -rf ${PGSQL_DATA}
-exit ${ret}
Use 'pg_ctl' utility to start and stop database

Greping output for  starting message don't work all the time
because 'postgres' utility translate messages in other language
depending on computer settings

Change-Id: I6f48db92ccc16e8c75d76dd3fadbbeb9930f9cac
