From 3971ee6d1fccf831395f396a81afbd65c21b605d Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 00:07:23 +0100 Subject: [PATCH 1/9] Testing CI with Travis --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1aa25416..09744c20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,11 @@ language: python +services: + - postgresql + +addons: + postgresql: 9.4 + python: - 2.6 - 2.7 From 0be783c4546c2bbebc67040b212db38ab872d59c Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 00:12:07 +0100 Subject: [PATCH 2/9] Disable email notification Mmm... it seems it's going to be a long night... --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 09744c20..9587a78f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,3 +17,7 @@ install: - python setup.py install script: make check + + +notifications: + email: false From b3cd125d2757872e9337d8df3d8e286345a67450 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 00:18:09 +0100 Subject: [PATCH 3/9] Create the hstore extension in the trevis db --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 9587a78f..027763e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ python: before_script: - psql -c 'create database psycopg2_test;' -U postgres + - psql -c 'create extension hstore;' -U postgres install: - python setup.py install From a478ba9a4785eac4839f0c4f65e90f6557d42c65 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 00:18:22 +0100 Subject: [PATCH 4/9] Fixed tests failing on Python 2.6 --- tests/test_connection.py | 2 +- tests/test_module.py | 4 ++-- tests/test_quote.py | 10 ++++++---- tests/test_replication.py | 4 ---- tests/testutils.py | 3 +++ 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index 8744488d..833751b9 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -465,7 +465,7 @@ class MakeDsnTestCase(ConnectingTestCase): conn = self.connect() d = conn.get_dsn_parameters() self.assertEqual(d['dbname'], dbname) # the only param we can check reliably - self.assertNotIn('password', d) + self.assert_('password' not in d, d) class IsolationLevelsTestCase(ConnectingTestCase): diff --git a/tests/test_module.py b/tests/test_module.py index 1a9a19d4..6a1606d6 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -119,8 +119,8 @@ class ConnectTestCase(unittest.TestCase): def test_int_port_param(self): psycopg2.connect(database='sony', port=6543) dsn = " %s " % self.args[0] - self.assertIn(" dbname=sony ", dsn) - self.assertIn(" port=6543 ", dsn) + self.assert_(" dbname=sony " in dsn, dsn) + self.assert_(" port=6543 " in dsn, dsn) def test_empty_param(self): psycopg2.connect(database='sony', password='') diff --git a/tests/test_quote.py b/tests/test_quote.py index f74fd854..72c9c1e4 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -65,11 +65,13 @@ class QuotingTestCase(ConnectingTestCase): curs = self.conn.cursor() data = 'abcd\x01\x00cdefg' - with self.assertRaises(ValueError) as e: + try: curs.execute("SELECT %s", (data,)) - - self.assertEquals(str(e.exception), - 'A string literal cannot contain NUL (0x00) characters.') + except ValueError as e: + self.assertEquals(str(e), + 'A string literal cannot contain NUL (0x00) characters.') + else: + self.fail("ValueError not raised") def test_binary(self): data = b"""some data with \000\013 binary diff --git a/tests/test_replication.py b/tests/test_replication.py index ca99038a..2ccd4c77 100644 --- a/tests/test_replication.py +++ b/tests/test_replication.py @@ -35,11 +35,7 @@ from testutils import ConnectingTestCase class ReplicationTestCase(ConnectingTestCase): def setUp(self): - if not testconfig.repl_dsn: - self.skipTest("replication tests disabled by default") - super(ReplicationTestCase, self).setUp() - self.slot = testconfig.repl_slot self._slots = [] diff --git a/tests/testutils.py b/tests/testutils.py index d0a34bcf..1dd0c999 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -122,6 +122,9 @@ class ConnectingTestCase(unittest.TestCase): Should raise a skip test if not available, but guard for None on old Python versions. """ + if repl_dsn is None: + return self.skipTest("replication tests disabled by default") + if 'dsn' not in kwargs: kwargs['dsn'] = repl_dsn import psycopg2 From 11ad1005e0b03de7eefe883e890a060611bcaede Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 01:09:57 +0100 Subject: [PATCH 5/9] Added python3 supported versions --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 027763e2..02d96045 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,11 @@ addons: python: - 2.6 - 2.7 + - 3.2 + - 3.3 + - 3.4 + - 3.5 + - 3.6-dev before_script: - psql -c 'create database psycopg2_test;' -U postgres From def22982fb01c6b1411c721ddf0a9f73865d0383 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 02:15:24 +0100 Subject: [PATCH 6/9] Run the tests against all the available server versions --- .travis.yml | 18 +++++++---------- scripts/travis_prepare.sh | 41 +++++++++++++++++++++++++++++++++++++++ scripts/travis_test.sh | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 11 deletions(-) create mode 100755 scripts/travis_prepare.sh create mode 100755 scripts/travis_test.sh diff --git a/.travis.yml b/.travis.yml index 02d96045..d41c801b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,9 @@ +# Travis CI configuration file for psycopg2 + +dist: trusty +sudo: required language: python -services: - - postgresql - -addons: - postgresql: 9.4 - python: - 2.6 - 2.7 @@ -15,14 +13,12 @@ python: - 3.5 - 3.6-dev -before_script: - - psql -c 'create database psycopg2_test;' -U postgres - - psql -c 'create extension hstore;' -U postgres - install: - python setup.py install -script: make check +script: + - sudo scripts/travis_prepare.sh + - scripts/travis_test.sh notifications: diff --git a/scripts/travis_prepare.sh b/scripts/travis_prepare.sh new file mode 100755 index 00000000..86b85bae --- /dev/null +++ b/scripts/travis_prepare.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -e + +# 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. + +set_param () { + # Set a parameter in a postgresql.conf file + version=$1 + param=$2 + value=$3 + + sed -i "s/^\s*#\?\s*$param.*/$param = $value/" \ + "/etc/postgresql/$version/psycopg/postgresql.conf" +} + +create () { + version=$1 + port=$2 + dbname=psycopg2_test_$port + + pg_createcluster -p $port --start-conf manual $version psycopg + set_param "$version" max_prepared_transactions 10 + sed -i "s/local\s*all\s*postgres.*/local all postgres trust/" \ + "/etc/postgresql/$version/psycopg/pg_hba.conf" + pg_ctlcluster "$version" psycopg start + + sudo -u postgres psql -c "create user travis" "port=$port" +} + +# 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 diff --git a/scripts/travis_test.sh b/scripts/travis_test.sh new file mode 100755 index 00000000..3a1bdb28 --- /dev/null +++ b/scripts/travis_test.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# Run the tests in all the databases +# The script is designed for a Trusty environment. + +set -e + +run_test () { + version=$1 + port=$2 + pyver=$(python -c "import sys; print(''.join(map(str,sys.version_info[:2])))") + dbname=psycopg_test_$pyver + + # Create a database for each python version to allow tests to run in parallel + psql -c "create database $dbname" \ + "user=postgres port=$port dbname=postgres" + + psql -c "grant create on database $dbname to travis" \ + "user=postgres port=$port dbname=postgres" + + psql -c "create extension hstore" \ + "user=postgres port=$port dbname=$dbname" + + printf "\n\nRunning tests against PostgreSQL $version\n\n" + export PSYCOPG2_TESTDB=$dbname + export PSYCOPG2_TESTDB_PORT=$port + export PSYCOPG2_TESTDB_USER=travis + make check + + printf "\n\nRunning tests against PostgreSQL $version (green mode)\n\n" + export PSYCOPG2_TEST_GREEN=1 + make check +} + +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 From 6758ce5eefe7122f1e3e68743504a9ca6b33321c Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 04:27:51 +0100 Subject: [PATCH 7/9] Test Python versions in a more relevant order --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index d41c801b..ef056fa1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,13 +5,13 @@ sudo: required language: python python: - - 2.6 - 2.7 - - 3.2 - - 3.3 - - 3.4 - - 3.5 - 3.6-dev + - 3.5 + - 2.6 + - 3.4 + - 3.3 + - 3.2 install: - python setup.py install From 1463bdb86d46d2d729bf0663443d765ac975f100 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 04:28:34 +0100 Subject: [PATCH 8/9] Added build badge to readme --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index 51d2d6b6..f18be564 100644 --- a/README.rst +++ b/README.rst @@ -44,3 +44,8 @@ For any other resource (source code repository, bug tracker, mailing list) please check the `project homepage`__. .. __: http://initd.org/psycopg/ + + +.. image:: https://travis-ci.org/psycopg/psycopg2.svg?branch=master + :target: https://travis-ci.org/psycopg/psycopg2 + :alt: Build Status From feebc8f68991fc1e84ac4fefb9cea0c373eea6db Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 24 Dec 2016 04:42:07 +0100 Subject: [PATCH 9/9] Don't use separate databases for tests I got this wrong: I thought parallel test ran in the same VM; they are isolated instead. --- .travis.yml | 5 ++--- scripts/travis_prepare.sh | 8 +++++--- scripts/travis_test.sh | 13 +------------ 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index ef056fa1..10411637 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,19 +7,18 @@ language: python python: - 2.7 - 3.6-dev - - 3.5 - 2.6 + - 3.5 - 3.4 - 3.3 - 3.2 install: - python setup.py install + - sudo scripts/travis_prepare.sh script: - - sudo scripts/travis_prepare.sh - scripts/travis_test.sh - notifications: email: false diff --git a/scripts/travis_prepare.sh b/scripts/travis_prepare.sh index 86b85bae..f4e86118 100755 --- a/scripts/travis_prepare.sh +++ b/scripts/travis_prepare.sh @@ -20,17 +20,19 @@ set_param () { create () { version=$1 port=$2 - dbname=psycopg2_test_$port + dbname=psycopg2_test pg_createcluster -p $port --start-conf manual $version psycopg set_param "$version" max_prepared_transactions 10 - sed -i "s/local\s*all\s*postgres.*/local all postgres trust/" \ - "/etc/postgresql/$version/psycopg/pg_hba.conf" pg_ctlcluster "$version" psycopg start sudo -u postgres psql -c "create user travis" "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" } + # Would give a permission denied error in the travis build dir cd / diff --git a/scripts/travis_test.sh b/scripts/travis_test.sh index 3a1bdb28..df9413a1 100755 --- a/scripts/travis_test.sh +++ b/scripts/travis_test.sh @@ -8,18 +8,7 @@ set -e run_test () { version=$1 port=$2 - pyver=$(python -c "import sys; print(''.join(map(str,sys.version_info[:2])))") - dbname=psycopg_test_$pyver - - # Create a database for each python version to allow tests to run in parallel - psql -c "create database $dbname" \ - "user=postgres port=$port dbname=postgres" - - psql -c "grant create on database $dbname to travis" \ - "user=postgres port=$port dbname=postgres" - - psql -c "create extension hstore" \ - "user=postgres port=$port dbname=$dbname" + dbname=psycopg2_test printf "\n\nRunning tests against PostgreSQL $version\n\n" export PSYCOPG2_TESTDB=$dbname