From 6dd398b69381576487f53a0a1ccce7547d77a511 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 11 Apr 2019 19:19:59 +0200 Subject: [PATCH 1/7] tests: add support for pg_virtualenv --- Jenkinsfile | 14 ++++++++++++-- tests/conftest.py | 15 +++++++++++++-- tox.ini | 4 ++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6e57c3a..32c737f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,10 +2,19 @@ pipeline { agent any + options { disableConcurrentBuilds() } + environment { + TMPDIR = "/tmp/$BUILD_TAG" + } stages { stage('Unit Tests') { steps { - sh 'tox -rv' + sh """ +mkdir ${env.TMPDIR} +virtualenv venv +./venv/bin/pip install tox +PGPORT=`python -c 'import struct; import socket; s=socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'` pg_virtualenv -o fsync=off ./venv/bin/tox -rv +""" } post { always { @@ -35,7 +44,8 @@ pipeline { utils.mail_notify(currentBuild, env, 'admin+jenkins-wcs-olap@entrouvert.com') } } - success { + cleanup { + sh "rm -rf ${env.TMPDIR}" cleanWs() } } diff --git a/tests/conftest.py b/tests/conftest.py index e78674b..1b8fd70 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,7 +22,18 @@ class Database(object): def __init__(self): self.db_name = 'db%s' % random.getrandbits(20) self.dsn = 'dbname=%s' % self.db_name - with closing(psycopg2.connect('')) as conn: + self.connect_kwargs = { + 'dbname': 'postgres' + } + for variable, key in [ + ('PGHOST', 'host'), + ('PGUSER', 'user'), + ('PGPASSWORD', 'password'), + ('PGPORT', 'port'), + ('PGDATABASE', 'dbname')]: + if variable in os.environ: + self.connect_kwargs[key] = os.environ[variable] + with closing(psycopg2.connect(**self.connect_kwargs)) as conn: conn.set_isolation_level(0) with conn.cursor() as cursor: cursor.execute('CREATE DATABASE %s' % self.db_name) @@ -31,7 +42,7 @@ class Database(object): return closing(psycopg2.connect(self.dsn)) def delete(self): - with closing(psycopg2.connect('')) as conn: + with closing(psycopg2.connect(**self.connect_kwargs)) as conn: conn.set_isolation_level(0) with conn.cursor() as cursor: cursor.execute('DROP DATABASE IF EXISTS %s' % self.db_name) diff --git a/tox.ini b/tox.ini index b288dbf..11352dd 100644 --- a/tox.ini +++ b/tox.ini @@ -13,6 +13,10 @@ basepython = python2 setenv = coverage: COVERAGE=--junit-xml=junit.xml --cov=wcs_olap --cov-report xml --cov-report html WCSCTL=wcs/wcsctl.py + PGPORT={env:PGPORT:} + PGHOST={env:PGHOST:} + PGUSER={env:PGUSER:} + PGPASSWORD={env:PGPASSWORD:} deps = coverage pytest -- 2.23.0