Skip to content

Commit

Permalink
Merge branch 'test-dinosaurs'
Browse files Browse the repository at this point in the history
  • Loading branch information
dvarrazzo committed Feb 7, 2017
2 parents 705dda2 + 7b11b95 commit 3ff350c
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 116 deletions.
131 changes: 101 additions & 30 deletions scripts/travis_prepare.sh
Original file line number Diff line number Diff line change
@@ -1,60 +1,131 @@
#!/bin/bash

set -e
set -e -x

# Prepare the test databases in Travis CI.
#
# The script should be run with sudo.
# The script is not idempotent: it assumes the machine in a clean state
# and is designed for a sudo-enabled Trusty environment.
#
# The variables TEST_PAST, TEST_FUTURE, DONT_TEST_PRESENT can be used to test
# against unsupported Postgres versions and skip tests with supported ones.
#
# The variables can be set in the travis configuration
# (https://travis-ci.org/psycopg/psycopg2/settings)

set_param () {
# Set a parameter in a postgresql.conf file
version=$1
param=$2
value=$3
param=$1
value=$2

sed -i "s/^\s*#\?\s*$param.*/$param = $value/" \
"/etc/postgresql/$version/psycopg/postgresql.conf"
sed -i "s/^\s*#\?\s*$param.*/$param = $value/" "$DATADIR/postgresql.conf"
}

create () {
version=$1
port=$2
dbname=psycopg2_test
export VERSION=$1
export PACKAGE=${2:-$VERSION}

pg_createcluster -p $port --start-conf manual $version psycopg
# Version as number: 9.6 -> 906
export VERNUM=$(( $(echo $VERSION \
| sed 's/\(.\+\)\.\(.\+\)/100 * \1 + \2/') ))

# Port number: 9.6 -> 50906
export PORT=$(( 50000 + $VERNUM ))

export DATADIR="/var/lib/postgresql/$PACKAGE/psycopg"
export PGDIR="/usr/lib/postgresql/$PACKAGE"
export PGBIN="$PGDIR/bin"

# install postgres versions not available on the image
if (( "$VERNUM" < 902 || "$VERNUM" > 906 )); then
wget -O - http://initd.org/psycopg/tarballs/postgresql/postgresql-${PACKAGE}.tar.bz2 \
| sudo tar xjf - -C /usr/lib/postgresql
fi

sudo -u postgres "$PGBIN/initdb" -D "$DATADIR"

set_param port "$PORT"
if (( "$VERNUM" >= 800 )); then
set_param listen_addresses "'*'"
else
set_param tcpip_socket true
fi

# for two-phase commit testing
set_param "$version" max_prepared_transactions 10
if (( "$VERNUM" >= 801 )); then set_param max_prepared_transactions 10; fi

# for replication testing
set_param "$version" max_wal_senders 5
set_param "$version" max_replication_slots 5
if [ "$version" == "9.2" -o "$version" == "9.3" ]
then
set_param "$version" wal_level hot_standby
if (( "$VERNUM" >= 900 )); then set_param max_wal_senders 5; fi
if (( "$VERNUM" >= 904 )); then set_param max_replication_slots 5; fi

if (( "$VERNUM" >= 904 )); then
set_param wal_level logical
elif (( "$VERNUM" >= 900 )); then
set_param wal_level hot_standby
fi

if (( "$VERNUM" >= 900 )); then
echo "host replication travis 0.0.0.0/0 trust" >> "$DATADIR/pg_hba.conf"
fi

# start the server, wait for start
sudo -u postgres "$PGBIN/pg_ctl" -w -l /dev/null -D "$DATADIR" start

# create the test database
DBNAME=psycopg2_test
CONNINFO="user=postgres host=localhost port=$PORT dbname=template1"

if (( "$VERNUM" >= 901 )); then
psql -c "create user travis createdb createrole replication" "$CONNINFO"
elif (( "$VERNUM" >= 801 )); then
psql -c "create user travis createdb createrole" "$CONNINFO"
else
set_param "$version" wal_level logical
psql -c "create user travis createdb createuser" "$CONNINFO"
fi

echo "local replication travis trust" \
>> "/etc/postgresql/$version/psycopg/pg_hba.conf"
psql -c "create database $DBNAME with owner travis" "$CONNINFO"

# configure global objects on the test database
CONNINFO="user=postgres host=localhost port=$PORT dbname=$DBNAME"

pg_ctlcluster "$version" psycopg start
if (( "$VERNUM" >= 901 )); then
psql -c "create extension hstore" "$CONNINFO"
elif (( "$VERNUM" >= 803 )); then
psql -f "$PGDIR/share/contrib/hstore.sql" "$CONNINFO"
fi

sudo -u postgres psql -c "create user travis replication" "port=$port"
sudo -u postgres psql -c "create database $dbname" "port=$port"
sudo -u postgres psql -c "grant create on database $dbname to travis" "port=$port"
sudo -u postgres psql -c "create extension hstore" "port=$port dbname=$dbname"
if (( "$VERNUM" == 901 )); then
psql -c "create extension json" "$CONNINFO"
fi
}


# Would give a permission denied error in the travis build dir
cd /

create 9.6 54396
create 9.5 54395
create 9.4 54394
create 9.3 54393
create 9.2 54392
# Postgres versions supported by Travis CI
if [[ -z "$DONT_TEST_PRESENT" ]]; then
create 9.6
create 9.5
create 9.4
create 9.3
create 9.2
fi

# Unsupported postgres versions that we still support
# Images built by https://github.com/psycopg/psycopg2-wheels/tree/build-dinosaurs
if [[ -n "$TEST_PAST" ]]; then
create 7.4
create 8.0
create 8.1
create 8.2
create 8.3
create 8.4
create 9.0
create 9.1
fi

# Postgres built from master
if [[ -n "$TEST_FUTURE" ]]; then
create 10.0 10-master
fi
70 changes: 56 additions & 14 deletions scripts/travis_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,71 @@

# Run the tests in all the databases
# The script is designed for a Trusty environment.
#
# The variables TEST_PAST, TEST_FUTURE, DONT_TEST_PRESENT can be used to test
# against unsupported Postgres versions and skip tests with supported ones.
#
# The variables TEST_VERBOSE enables verbose test log.
#
# The variables can be set in the travis configuration
# (https://travis-ci.org/psycopg/psycopg2/settings)

set -e
set -e -x

run_test () {
version=$1
port=$2
dbname=psycopg2_test
VERSION=$1
DBNAME=psycopg2_test
if [[ -n "$TEST_VERBOSE" ]]; then
VERBOSE=--verbose
else
VERBOSE=
fi

printf "\n\nRunning tests against PostgreSQL $version\n\n"
export PSYCOPG2_TESTDB=$dbname
# Port number: 9.6 -> 50906
port=$(( 50000 + $(echo $VERSION \
| sed 's/\(.\+\)\.\(.\+\)/100 * \1 + \2/') ))

printf "\n\nRunning tests against PostgreSQL $VERSION (port $port)\n\n"
export PSYCOPG2_TESTDB=$DBNAME
export PSYCOPG2_TESTDB_HOST=localhost
export PSYCOPG2_TESTDB_PORT=$port
export PSYCOPG2_TESTDB_USER=travis
export PSYCOPG2_TEST_REPL_DSN=
unset PSYCOPG2_TEST_GREEN
python -c "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')"
python -c \
"from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" \
$VERBOSE

printf "\n\nRunning tests against PostgreSQL $version (green mode)\n\n"
printf "\n\nRunning tests against PostgreSQL $VERSION (green mode)\n\n"
export PSYCOPG2_TEST_GREEN=1
python -c "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')"
python -c \
"from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" \
$VERBOSE
}

run_test 9.6 54396
run_test 9.5 54395
run_test 9.4 54394
run_test 9.3 54393
run_test 9.2 54392
# Postgres versions supported by Travis CI
if [[ -z "$DONT_TEST_PRESENT" ]]; then
run_test 9.6
run_test 9.5
run_test 9.4
run_test 9.3
run_test 9.2
fi

# Unsupported postgres versions that we still support
# Images built by https://github.com/psycopg/psycopg2-wheels/tree/build-dinosaurs
if [[ -n "$TEST_PAST" ]]; then
run_test 7.4
run_test 8.0
run_test 8.1
run_test 8.2
run_test 8.3
run_test 8.4
run_test 9.0
run_test 9.1
fi

# Postgres built from master
if [[ -n "$TEST_FUTURE" ]]; then
run_test 10.0
fi
18 changes: 11 additions & 7 deletions tests/test_async_keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.

import time

import psycopg2
from psycopg2 import extras

from testconfig import dsn
from testutils import (ConnectingTestCase, unittest, skip_before_postgres,
assertDsnEqual)
from testutils import ConnectingTestCase, unittest, skip_before_postgres, slow

from test_replication import ReplicationTestCase, skip_repl_if_green
from psycopg2.extras import LogicalReplicationConnection, StopReplication

Expand Down Expand Up @@ -97,13 +99,15 @@ def setUp(self):
)''')
self.conn.commit()

@slow
@skip_before_postgres(8, 2)
def test_async_cancel(self):
async_conn = psycopg2.connect(dsn, async=True)
self.assertRaises(psycopg2.OperationalError, async_conn.cancel)
extras.wait_select(async_conn)
cur = async_conn.cursor()
cur.execute("select pg_sleep(10000)")
cur.execute("select pg_sleep(10)")
time.sleep(1)
self.assertTrue(async_conn.isexecuting())
async_conn.cancel()
self.assertRaises(psycopg2.extensions.QueryCanceledError,
Expand Down Expand Up @@ -143,23 +147,23 @@ def f(dsn, async=False):
pass

psycopg2.connect(database='foo', host='baz', connection_factory=f)
assertDsnEqual(self, self.args[0], 'dbname=foo host=baz')
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
self.assertEqual(self.args[1], f)
self.assertEqual(self.args[2], False)

psycopg2.connect("dbname=foo host=baz", connection_factory=f)
assertDsnEqual(self, self.args[0], 'dbname=foo host=baz')
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
self.assertEqual(self.args[1], f)
self.assertEqual(self.args[2], False)

def test_async(self):
psycopg2.connect(database='foo', host='baz', async=1)
assertDsnEqual(self, self.args[0], 'dbname=foo host=baz')
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
self.assertEqual(self.args[1], None)
self.assert_(self.args[2])

psycopg2.connect("dbname=foo host=baz", async=True)
assertDsnEqual(self, self.args[0], 'dbname=foo host=baz')
self.assertDsnEqual(self.args[0], 'dbname=foo host=baz')
self.assertEqual(self.args[1], None)
self.assert_(self.args[2])

Expand Down
5 changes: 4 additions & 1 deletion tests/test_cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.

import time
import threading

import psycopg2
Expand Down Expand Up @@ -86,13 +87,15 @@ def canceller(conn):

self.assertEqual(errors, [])

@slow
@skip_before_postgres(8, 2)
def test_async_cancel(self):
async_conn = psycopg2.connect(dsn, async_=True)
self.assertRaises(psycopg2.OperationalError, async_conn.cancel)
extras.wait_select(async_conn)
cur = async_conn.cursor()
cur.execute("select pg_sleep(2)")
cur.execute("select pg_sleep(10)")
time.sleep(1)
self.assertTrue(async_conn.isexecuting())
async_conn.cancel()
self.assertRaises(psycopg2.extensions.QueryCanceledError,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from psycopg2 import extensions as ext

from testutils import (
unittest, assertDsnEqual, decorate_all_tests, skip_if_no_superuser,
unittest, decorate_all_tests, skip_if_no_superuser,
skip_before_postgres, skip_after_postgres, skip_before_libpq,
ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow)

Expand Down Expand Up @@ -413,7 +413,7 @@ def test_params_validation(self):

def test_empty_param(self):
dsn = ext.make_dsn(dbname='sony', password='')
assertDsnEqual(self, dsn, "dbname=sony password=''")
self.assertDsnEqual(dsn, "dbname=sony password=''")

def test_escape(self):
dsn = ext.make_dsn(dbname='hello world')
Expand All @@ -436,10 +436,10 @@ def test_database_is_a_keyword(self):

def test_params_merging(self):
dsn = ext.make_dsn('dbname=foo host=bar', host='baz')
assertDsnEqual(self, dsn, 'dbname=foo host=baz')
self.assertDsnEqual(dsn, 'dbname=foo host=baz')

dsn = ext.make_dsn('dbname=foo', user='postgres')
assertDsnEqual(self, dsn, 'dbname=foo user=postgres')
self.assertDsnEqual(dsn, 'dbname=foo user=postgres')

def test_no_dsn_munging(self):
dsnin = 'dbname=a host=b user=c password=d'
Expand All @@ -453,7 +453,7 @@ def test_url_is_cool(self):
self.assertEqual(dsn, url)

dsn = ext.make_dsn(url, application_name='woot')
assertDsnEqual(self, dsn,
self.assertDsnEqual(dsn,
'dbname=test user=tester password=secret application_name=woot')

self.assertRaises(psycopg2.ProgrammingError,
Expand Down
Loading

0 comments on commit 3ff350c

Please sign in to comment.