From 5980ae7465b644d143e4f1aaa37d3310a66d7f5a Mon Sep 17 00:00:00 2001 From: Hunor Csomortáni Date: Sep 11 2019 11:28:31 +0000 Subject: Make tox work without tox-docker This will make tox-docker optional for running the tests. In case tox-docker is not installed, tox ignores the docker configuration for the test environment. When the above happens the POSTGRES_5432_TCP environment variable will be missing. This can be used as a condition for pytest.mark.skipif to skip the tests which need PostgreSQL running. The above should improve developer experience. Signed-off-by: Hunor Csomortáni --- diff --git a/Makefile b/Makefile index 2ec4ed8..28fde3b 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ update-makefile: test: $(VENV) set -e source $(VENV)/bin/activate; - TEST='true' NO_CAN_HAS_POSTGRES='sadly' py.test --cov-report=term-missing --cov $(MODULENAME); + TEST='true' py.test --cov-report=term-missing --cov $(MODULENAME); deactivate .PHONY: test-ci @@ -63,7 +63,7 @@ test: $(VENV) test-ci: $(VENV) set -e source $(VENV)/bin/activate - TEST='true' NO_CAN_HAS_POSTGRES='sadly' py.test --cov-report=xml --cov $(MODULENAME) + TEST='true' py.test --cov-report=xml --cov $(MODULENAME) deactivate .PHONY: pylint diff --git a/README.md b/README.md index 744b3fb..2234eda 100644 --- a/README.md +++ b/README.md @@ -84,15 +84,15 @@ You can run the test suite with the following command:: $ 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:: +Note, that in order for some of the tests to work properly, tox needs to +spin-up PostgreSQL in a docker container using the ``tox-docker`` plugin. This +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 +If `tox-docker` is not present, the tests which require PostgreSQL, are +skipped. ## Deployment diff --git a/resultsdb.spec b/resultsdb.spec index a8bfeb8..fe12ef9 100644 --- a/resultsdb.spec +++ b/resultsdb.spec @@ -37,7 +37,7 @@ ResultsDB is a results store engine for, but not limited to, Fedora QA tools. %setup -q %check -NO_CAN_HAS_POSTGRES='sadly' PYTHONPATH=%{buildroot}%{python3_sitelib}/ pytest-3 +PYTHONPATH=%{buildroot}%{python3_sitelib}/ pytest-3 # This seems to be the only place where we can remove pyco files, see: # https://fedoraproject.org/wiki/Packaging:Python#Byte_compiling diff --git a/testing/functest_api_v20.py b/testing/functest_api_v20.py index 0a4e10b..a36cf65 100644 --- a/testing/functest_api_v20.py +++ b/testing/functest_api_v20.py @@ -23,6 +23,7 @@ import os import tempfile import copy import time +import pytest import resultsdb import resultsdb.cli @@ -863,13 +864,10 @@ class TestFuncApiV20(): assert data['data'][1]['testcase']['name'] == self.ref_testcase_name assert data['data'][1]['outcome'] == "FAILED" + @pytest.mark.skipif(not os.getenv('POSTGRES_5432_TCP'), + reason="Requires PostgreSQL, because DISTINCT ON works differently in SQLite") def test_get_results_latest_distinct_on(self): """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", data={'scenario': 'scenario1'}, testcase=self.ref_testcase_name) @@ -886,21 +884,18 @@ class TestFuncApiV20(): assert len(data['data']) == 1 assert data['data'][0]['data']['scenario'][0] == 'scenario2' + @pytest.mark.skipif(not os.getenv('POSTGRES_5432_TCP'), + reason="Requires PostgreSQL, because DISTINCT ON works differently in SQLite") def test_get_results_latest_distinct_on_more_specific_cases_1(self): - """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") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite - ''' | id | testcase | scenario | |----|----------|----------| | 1 | tc_1 | s_1 | | 2 | tc_2 | s_1 | | 3 | tc_2 | s_2 | | 4 | tc_3 | | - ''' + """ self.helper_create_result(outcome="PASSED", testcase='tc_1', data={'item': 'grub', 'scenario': 's_1'}) self.helper_create_result(outcome="PASSED", testcase='tc_2', data={'item': 'grub', 'scenario': 's_1'}) self.helper_create_result(outcome="PASSED", testcase='tc_2', data={'item': 'grub', 'scenario': 's_2'}) @@ -911,14 +906,11 @@ class TestFuncApiV20(): assert len(data['data']) == 4 + @pytest.mark.skipif(not os.getenv('POSTGRES_5432_TCP'), + reason="Requires PostgreSQL, because DISTINCT ON works differently in SQLite") def test_get_results_latest_distinct_on_more_specific_cases_2(self): - """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") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite - ''' | id | testcase | scenario | |----|----------|----------| | 1 | tc_1 | s_1 | @@ -926,7 +918,7 @@ class TestFuncApiV20(): | 3 | tc_2 | s_2 | | 4 | tc_3 | | | 5 | tc_1 | | - ''' + """ self.helper_create_result(outcome="PASSED", testcase='tc_1', data={'item': 'grub', 'scenario': 's_1'}) self.helper_create_result(outcome="PASSED", testcase='tc_2', data={'item': 'grub', 'scenario': 's_1'}) self.helper_create_result(outcome="PASSED", testcase='tc_2', data={'item': 'grub', 'scenario': 's_2'}) @@ -938,14 +930,11 @@ class TestFuncApiV20(): assert len(data['data']) == 5 + @pytest.mark.skipif(not os.getenv('POSTGRES_5432_TCP'), + reason="Requires PostgreSQL, because DISTINCT ON works differently in SQLite") def test_get_results_latest_distinct_on_more_specific_cases_2(self): - """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") + """This test requires PostgreSQL, because DISTINCT ON does work differently in SQLite - ''' | id | testcase | scenario | |----|----------|----------| | 1 | tc_1 | s_1 | @@ -954,7 +943,7 @@ class TestFuncApiV20(): | 4 | tc_3 | | | 5 | tc_1 | | | 6 | tc_1 | s_1 | - ''' + """ self.helper_create_result(outcome="PASSED", testcase='tc_1', data={'item': 'grub', 'scenario': 's_1'}) self.helper_create_result(outcome="PASSED", testcase='tc_2', data={'item': 'grub', 'scenario': 's_1'}) self.helper_create_result(outcome="PASSED", testcase='tc_2', data={'item': 'grub', 'scenario': 's_2'}) @@ -971,13 +960,10 @@ class TestFuncApiV20(): assert tc_1s[0]['outcome'] == 'INFO' assert tc_1s[1]['outcome'] == 'FAILED' + @pytest.mark.skipif(not os.getenv('POSTGRES_5432_TCP'), + reason="Requires PostgreSQL, because DISTINCT ON works differently in SQLite") def test_get_results_latest_distinct_on_with_scenario_not_defined(self): """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) self.helper_create_result(outcome="FAILED", testcase=self.ref_testcase_name) diff --git a/tox.ini b/tox.ini index 002049c..3173ece 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,6 @@ addopts=--functional -p no:warnings testing/ --cov resultsdb --cov-report=term-m [tox] envlist = py27,py36,py37 -requires = tox-docker [testenv] docker = postgres:latest @@ -35,4 +34,3 @@ sitepackages = False # tests read HOME passenv = HOME - NO_CAN_HAS_POSTGRES