#134 Use tox-docker to test with postgresql backend
Merged 6 years ago by jskladan. Opened 6 years ago by jskladan.

file modified
+12 -3
@@ -80,10 +80,19 @@ 

  

  ## 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

  

file modified
+22 -7
@@ -22,6 +22,7 @@ 

  import os

  import tempfile

  import copy

+ import time

  

  import resultsdb

  import resultsdb.cli
@@ -48,8 +49,12 @@ 

      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 @@ 

          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 @@ 

          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 @@ 

          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 @@ 

          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 @@ 

          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)

file modified
+9 -1
@@ -18,8 +18,14 @@ 

  

  [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 @@ 

  # needs hawkey, koji

  sitepackages = False

  # tests read HOME

- passenv = HOME

+ passenv =

+     HOME

+     NO_CAN_HAS_POSTGRES

We finally got to the point where proper testing can not be done against SQLite, but requires PostgreSQL.

tox-docker is an easy to use tool, that takes care of spinning up (and removing) docker containers. Sadly, the plugin is not (yet?) packaged in Fedora, but this can easily be worked-around with pip install --user (works on my setup, at least :-D), so I don't see any major issue here.

If somebody wants to go about it without docker, using pytest instead of tox is still an option.

rebased onto 4ae82e2

6 years ago

Pull-Request has been merged by jskladan

6 years ago