From 4ae82e29264f5c2571a7f06cc6f21d20ce2468ce Mon Sep 17 00:00:00 2001 From: Josef Skladanka Date: Mar 27 2019 12:25:48 +0000 Subject: Use tox-docker to test with postgresql backend --- diff --git a/README.md b/README.md index b9db2f3..744b3fb 100644 --- a/README.md +++ b/README.md @@ -80,10 +80,19 @@ And run as usual. ## Running test suite -You can run the test suite with the following command (with virtualenv -active):: +You can run the test suite with the following command:: - $ pytest + $ tox + +Note, that in order for some of the tests to work properly, tox is configured to spin-up PostgreSQL in a docker container using the +``tox-docker`` plugin, which needs to be installed separately. The best option probably is:: + + $ pip install --user tox-docker + $ pip3 install --user tox-docker + +Should you, for some reason avoid docker, you could run the following command (with virtualenv active):: + + $ NO_CAN_HAS_POSTGRES=sadly pytest ## Deployment diff --git a/testing/functest_api_v20.py b/testing/functest_api_v20.py index 1e5cba9..0a4e10b 100644 --- a/testing/functest_api_v20.py +++ b/testing/functest_api_v20.py @@ -22,6 +22,7 @@ import datetime import os import tempfile import copy +import time import resultsdb import resultsdb.cli @@ -48,8 +49,12 @@ class TestFuncApiV20(): def setup_class(cls): cls.dbfile = tempfile.NamedTemporaryFile(delete=False) cls.dbfile.close() - resultsdb.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s' % cls.dbfile.name - #resultsdb.app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://resultsdb:resultsdb@localhost:5432/resultsdb' + postgres_port = os.getenv('POSTGRES_5432_TCP', None) + if postgres_port: + time.sleep(1) # for some weird reason, docker container is 'up' before postgres is ready + resultsdb.app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://resultsdb:resultsdb@localhost:%s/resultsdb' % postgres_port + else: + resultsdb.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s' % cls.dbfile.name resultsdb.app.config['MESSAGE_BUS_PUBLISH'] = True resultsdb.app.config['MESSAGE_BUS_PLUGIN'] = 'dummy' @@ -859,9 +864,11 @@ class TestFuncApiV20(): assert data['data'][1]['outcome'] == "FAILED" def test_get_results_latest_distinct_on(self): - print("=============== HINT ===============\nThis test requires PostgreSQL, because DISTINCT ON does work differently in SQLite") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite""" if os.getenv('NO_CAN_HAS_POSTGRES', None): return + if resultsdb.app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite'): + raise Exception("This test requires PostgreSQL to work properly. You can disable it by setting NO_CAN_HAS_POSTGRES env variable to any non-empty value") self.helper_create_testcase() @@ -880,9 +887,11 @@ class TestFuncApiV20(): assert data['data'][0]['data']['scenario'][0] == 'scenario2' def test_get_results_latest_distinct_on_more_specific_cases_1(self): - print("=============== HINT ===============\nThis test requires PostgreSQL, because DISTINCT ON does work differently in SQLite") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite""" if os.getenv('NO_CAN_HAS_POSTGRES', None): return + if resultsdb.app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite'): + raise Exception("This test requires PostgreSQL to work properly. You can disable it by setting NO_CAN_HAS_POSTGRES env variable to any non-empty value") ''' | id | testcase | scenario | @@ -903,9 +912,11 @@ class TestFuncApiV20(): assert len(data['data']) == 4 def test_get_results_latest_distinct_on_more_specific_cases_2(self): - print("=============== HINT ===============\nThis test requires PostgreSQL, because DISTINCT ON does work differently in SQLite") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite""" if os.getenv('NO_CAN_HAS_POSTGRES', None): return + if resultsdb.app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite'): + raise Exception("This test requires PostgreSQL to work properly. You can disable it by setting NO_CAN_HAS_POSTGRES env variable to any non-empty value") ''' | id | testcase | scenario | @@ -928,9 +939,11 @@ class TestFuncApiV20(): assert len(data['data']) == 5 def test_get_results_latest_distinct_on_more_specific_cases_2(self): - print("=============== HINT ===============\nThis test requires PostgreSQL, because DISTINCT ON does work differently in SQLite") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite""" if os.getenv('NO_CAN_HAS_POSTGRES', None): return + if resultsdb.app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite'): + raise Exception("This test requires PostgreSQL to work properly. You can disable it by setting NO_CAN_HAS_POSTGRES env variable to any non-empty value") ''' | id | testcase | scenario | @@ -959,9 +972,11 @@ class TestFuncApiV20(): assert tc_1s[1]['outcome'] == 'FAILED' def test_get_results_latest_distinct_on_with_scenario_not_defined(self): - print("=============== HINT ===============\nThis test requires PostgreSQL, because DISTINCT ON does work differently in SQLite") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite""" if os.getenv('NO_CAN_HAS_POSTGRES', None): return + if resultsdb.app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite'): + raise Exception("This test requires PostgreSQL to work properly. You can disable it by setting NO_CAN_HAS_POSTGRES env variable to any non-empty value") self.helper_create_testcase() self.helper_create_result(outcome="PASSED", testcase=self.ref_testcase_name) diff --git a/tox.ini b/tox.ini index 93ee516..002049c 100644 --- a/tox.ini +++ b/tox.ini @@ -18,8 +18,14 @@ addopts=--functional -p no:warnings testing/ --cov resultsdb --cov-report=term-m [tox] envlist = py27,py36,py37 +requires = tox-docker [testenv] +docker = postgres:latest +dockerenv = + POSTGRES_USER=resultsdb + POSTGRES_DB=resultsdb + POSTGRES_PASWORD=resultsdb deps = -rrequirements.txt commands = python -m pytest {posargs} # setup.py has from utils import... @@ -27,4 +33,6 @@ setenv = PYTHONPATH = {toxinidir} # needs hawkey, koji sitepackages = False # tests read HOME -passenv = HOME +passenv = + HOME + NO_CAN_HAS_POSTGRES