From: Peter Eisentraut Date: Tue, 26 Dec 2017 21:49:36 +0000 (-0500) Subject: Fix psql invocation in tests X-Git-Tag: pgbouncer_1_9_0~38 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4f48ae3c7c7a3ae7aab4594420b0646ae27d4c7d;p=pgbouncer Fix psql invocation in tests Always run psql with -X, otherwise some tests fail if there is something in .psqlrc producing output. Also fix some potential portability problems in option syntax: Put non-option arguments last, don't mix options with and without arguments. --- diff --git a/test/test.sh b/test/test.sh index ab3ee55..0845225 100755 --- a/test/test.sh +++ b/test/test.sh @@ -82,16 +82,16 @@ pgctl start sleep 5 echo "Creating databases" -psql -p $PG_PORT -l |grep p0 > /dev/null || { - psql -p $PG_PORT -c "create user bouncer" template1 +psql -X -p $PG_PORT -l | grep p0 > /dev/null || { + psql -X -o /dev/null -p $PG_PORT -c "create user bouncer" template1 createdb -p $PG_PORT p0 createdb -p $PG_PORT p1 createdb -p $PG_PORT p3 } -psql -p $PG_PORT -d p0 -c "select * from pg_user" | grep pswcheck > /dev/null || { - psql -p $PG_PORT p0 -c "create user pswcheck with superuser createdb password 'pgbouncer-check';" || return 1 - psql -p $PG_PORT p0 -c "create user someuser with password 'anypasswd';" || return 1 +psql -X -p $PG_PORT -d p0 -c "select * from pg_user" | grep pswcheck > /dev/null || { + psql -X -o /dev/null -p $PG_PORT -c "create user pswcheck with superuser createdb password 'pgbouncer-check';" p0 || return 1 + psql -X -o /dev/null -p $PG_PORT -c "create user someuser with password 'anypasswd';" p0 || return 1 } echo "Starting bouncer" @@ -153,7 +153,7 @@ die() { } admin() { - psql -h /tmp -U pgbouncer pgbouncer -c "$@;" || die "Cannot contact bouncer!" + psql -X -h /tmp -U pgbouncer -d pgbouncer -c "$@;" || die "Cannot contact bouncer!" } runtest() { @@ -175,35 +175,35 @@ runtest() { # server_lifetime test_server_lifetime() { admin "set server_lifetime=2" - psql -c "select now()" p0 + psql -X -c "select now()" p0 sleep 3 - rc=`psql -p $PG_PORT -tAqc "select count(1) from pg_stat_activity where usename='bouncer' and datname='p0'" p0` - psql -c "select now()" p0 + rc=`psql -X -p $PG_PORT -tAqc "select count(1) from pg_stat_activity where usename='bouncer' and datname='p0'" p0` + psql -X -c "select now()" p0 return $rc } # server_idle_timeout test_server_idle_timeout() { admin "set server_idle_timeout=2" - psql -c "select now()" p0 + psql -X -c "select now()" p0 sleep 3 - rc=`psql -p $PG_PORT -tAqc "select count(1) from pg_stat_activity where usename='bouncer' and datname='p0'" p0` - psql -c "select now()" p0 + rc=`psql -X -p $PG_PORT -tAq -c "select count(1) from pg_stat_activity where usename='bouncer' and datname='p0'" p0` + psql -X -c "select now()" p0 return $rc } # query_timeout test_query_timeout() { admin "set query_timeout=3" - psql -c "select pg_sleep(5)" p0 && return 1 + psql -X -c "select pg_sleep(5)" p0 && return 1 return 0 } # client_idle_timeout test_client_idle_timeout() { admin "set client_idle_timeout=2" - psql --set ON_ERROR_STOP=1 p0 <<-PSQL_EOF + psql -X --set ON_ERROR_STOP=1 p0 <<-PSQL_EOF select now(); \! sleep 3 select now(); @@ -219,7 +219,7 @@ test_server_login_retry() { (pgctl -m fast stop; sleep 3; pgctl start) & sleep 1 - psql -c "select now()" p0 + psql -X -c "select now()" p0 rc=$? wait return $rc @@ -234,7 +234,7 @@ test_server_connect_timeout_establish() { sleep 2 admin "set query_timeout=3" admin "set server_connect_timeout=2" - psql -c "select now()" p2 + psql -X -c "select now()" p2 # client will always see query_timeout, need to grep for connect timeout grep "closing because: connect timeout" $BOUNCER_LOG rc=$? @@ -249,7 +249,7 @@ test_server_connect_timeout_reject() { admin "set query_timeout=5" admin "set server_connect_timeout=3" fw_drop_port $PG_PORT - psql -c "select now()" p0 + psql -X -c "select now()" p0 fw_reset # client will always see query_timeout, need to grep for connect timeout grep "closing because: connect failed" $BOUNCER_LOG @@ -263,10 +263,10 @@ test_server_check_delay() { admin "set server_login_retry=3" admin "set query_timeout=10" - psql p0 -c "select now()" + psql -X -c "select now()" p0 fw_reject_port $PG_PORT sleep 3 - psql -tAq p0 -c "select 1" >$LOGDIR/test.tmp & + psql -X -tAq -c "select 1" p0 >$LOGDIR/test.tmp & sleep 1 fw_reset echo `date` rules flushed @@ -282,23 +282,23 @@ test_max_client_conn() { admin "show config" for i in {1..4}; do - psql p1 -c "select now() as sleeping from pg_sleep(3);" & + psql -X -c "select now() as sleeping from pg_sleep(3);" p1 & done # last conn allowed - psql p1 -c "select now() as last_conn" || return 1 + psql -X -c "select now() as last_conn" p1 || return 1 # exhaust it - psql p1 -c "select now() as sleeping from pg_sleep(3);" & + psql -X -c "select now() as sleeping from pg_sleep(3);" p1 & sleep 1 # shouldn't be allowed - psql p1 -c "select now() as exhausted" && return 1 + psql -X -c "select now() as exhausted" p1 && return 1 # should be ok echo 'waiting for clients to complete ...' wait - psql p1 -c "select now() as ok" || return 1 + psql -X -c "select now() as ok" p1 || return 1 return 0 } @@ -308,10 +308,10 @@ test_pool_size() { docount() { for i in {1..10}; do - psql $1 -c "select pg_sleep(0.5)" & + psql -X -c "select pg_sleep(0.5)" $1 & done wait - cnt=`psql -tAqc "select count(1) from pg_stat_activity where usename='bouncer' and datname='$1'" $1` + cnt=`psql -X -tAq -c "select count(1) from pg_stat_activity where usename='bouncer' and datname='$1'" $1` echo $cnt } @@ -329,7 +329,7 @@ test_online_restart() { echo "`date` attempt $i" for j in {1..5}; do - psql -c "select now() as sleeping from pg_sleep(2)" p1 & + psql -X -c "select now() as sleeping from pg_sleep(2)" p1 & done pid1=`cat $BOUNCER_PID` @@ -347,7 +347,7 @@ test_online_restart() { test_pause_resume() { rm -f $LOGDIR/test.tmp for i in {1..50}; do - psql -tAq p0 -c 'select 1 from pg_sleep(0.1)' >>$LOGDIR/test.tmp + psql -X -tAq -c 'select 1 from pg_sleep(0.1)' p0 >>$LOGDIR/test.tmp done & for i in {1..5}; do @@ -365,11 +365,11 @@ test_pause_resume() { test_suspend_resume() { rm -f $LOGDIR/test.tmp for i in {1..50}; do - psql -tAq p0 -c 'select 1 from pg_sleep(0.1)' >>$LOGDIR/test.tmp + psql -X -tAq -c 'select 1 from pg_sleep(0.1)' p0 >>$LOGDIR/test.tmp done & for i in {1..5}; do - psql -h /tmp -p $BOUNCER_PORT pgbouncer -U pgbouncer <<-PSQL_EOF + psql -X -h /tmp -p $BOUNCER_PORT -d pgbouncer -U pgbouncer <<-PSQL_EOF suspend; \! sleep 1 resume; @@ -384,12 +384,12 @@ test_suspend_resume() { # test pause/resume test_enable_disable() { rm -f $LOGDIR/test.tmp - psql -tAq p0 -c "select 'enabled 1'" >>$LOGDIR/test.tmp 2>&1 + psql -X -tAq -c "select 'enabled 1'" >>$LOGDIR/test.tmp p0 2>&1 admin "disable p0" - psql -tAq p0 -c "select 'disabled 1'" >>$LOGDIR/test.tmp 2>&1 + psql -X -tAq -c "select 'disabled 1'" >>$LOGDIR/test.tmp p0 2>&1 admin "enable p0" - psql -tAq p0 -c "select 'enabled 2'" >>$LOGDIR/test.tmp 2>&1 + psql -X -tAq -c "select 'enabled 2'" >>$LOGDIR/test.tmp p0 2>&1 grep -q "enabled 1" $LOGDIR/test.tmp || return 1 grep -q "enabled 2" $LOGDIR/test.tmp || return 1 @@ -402,30 +402,30 @@ test_enable_disable() { test_database_restart() { admin "set server_login_retry=1" - psql p0 -c "select now() as p0_before_restart" + psql -X -c "select now() as p0_before_restart" p0 pgctl -m fast restart echo `date` restart 1 - psql p0 -c "select now() as p0_after_restart" || return 1 + psql -X -c "select now() as p0_after_restart" p0 || return 1 # do with some more clients for i in {1..5}; do - psql p0 -c "select pg_sleep($i)" & - psql p1 -c "select pg_sleep($i)" & + psql -X -c "select pg_sleep($i)" p0 & + psql -X -c "select pg_sleep($i)" p1 & done pgctl -m fast restart echo `date` restart 2 wait - psql p0 -c "select now() as p0_after_restart" || return 1 + psql -X -c "select now() as p0_after_restart" p0 || return 1 } # test connect string change test_database_change() { admin "set server_lifetime=2" - db1=`psql -tAq p1 -c "select current_database()"` + db1=`psql -X -tAq -c "select current_database()" p1` cp test.ini test.ini.bak sed '/^p1 =/s/dbname=p1/dbname=p0/g' test.ini >test2.ini @@ -434,7 +434,7 @@ test_database_change() { kill -HUP `cat $BOUNCER_PID` sleep 3 - db2=`psql -tAq p1 -c "select current_database()"` + db2=`psql -X -tAq -c "select current_database()" p1` echo "db1=$db1 db2=$db2" cp test.ini.bak test.ini @@ -449,15 +449,15 @@ test_database_change() { # test connect string change test_auth_user() { admin "set auth_type='md5'" - curuser=`psql -d "dbname=authdb user=someuser password=anypasswd" -tAq -c "select current_user;"` + curuser=`psql -X -d "dbname=authdb user=someuser password=anypasswd" -tAq -c "select current_user;"` echo "curuser=$curuser" test "$curuser" = "someuser" || return 1 - curuser2=`psql -d "dbname=authdb user=nouser password=anypasswd" -tAq -c "select current_user;"` + curuser2=`psql -X -d "dbname=authdb user=nouser password=anypasswd" -tAq -c "select current_user;"` echo "curuser2=$curuser2" test "$curuser2" = "" || return 1 - curuser2=`psql -d "dbname=authdb user=someuser password=badpasswd" -tAq -c "select current_user;"` + curuser2=`psql -X -d "dbname=authdb user=someuser password=badpasswd" -tAq -c "select current_user;"` echo "curuser2=$curuser2" test "$curuser2" = "" || return 1