From 630f7b4e188d71a208a3b6648f7e447a34f17826 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Jun 17 2019 14:47:23 +0000 Subject: Allow to run tests with PostgreSQL inside container This patch introduces a new environment variable in order to allow running tests with different database backend. This is useful particularly for running tests inside container. With this change, it is possible run tests in following combinations: * Python 2 and SQLite * Python 3 and SQLite * Python 2 and PostgreSQL * Python 3 and PostgreSQL Package python-psycopg2 is installed in both Dockerfile-tests and Dockerfile-tests-py3 in case of running tests with PostgreSQL. A new script contrib/run-unittests.sh is added to make it easy to run tests. An example: contrib/run-unittests.sh --py3 --with-pgsql that runs tests with Python 3 and PostgreSQL. Signed-off-by: Chenxiong Qi --- diff --git a/.cico-pr.pipeline b/.cico-pr.pipeline index 8cef077..14edbed 100644 --- a/.cico-pr.pipeline +++ b/.cico-pr.pipeline @@ -65,8 +65,8 @@ node('factory2'){ stage('Run Test Suites') { timeout(20) { - onmyduffynode 'docker run -v ~/fm-orchestrator:/src:Z quay.io/factory2/mbs-test-centos' - onmyduffynode 'docker run -v ~/fm-orchestrator:/src:Z quay.io/factory2/mbs-test-fedora' + onmyduffynode 'contrib/run-unittests.sh' + onmyduffynode 'contrib/run-unittests.sh --py3' } } diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ac48b1c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.env/ +.pytest_cache/ +.tox/ +.vagrant/ +.vscode/ diff --git a/conf/config.py b/conf/config.py index a9e0f3c..583bfdd 100644 --- a/conf/config.py +++ b/conf/config.py @@ -1,4 +1,4 @@ -from os import path +from os import environ, path # FIXME: workaround for this moment till confdir, dbdir (installdir etc.) are # declared properly somewhere/somehow @@ -98,7 +98,7 @@ class TestConfiguration(BaseConfiguration): BUILD_LOGS_NAME_FORMAT = "build-{id}.log" LOG_BACKEND = "console" LOG_LEVEL = "debug" - SQLALCHEMY_DATABASE_URI = "sqlite://" + SQLALCHEMY_DATABASE_URI = environ.get("DATABASE_URI", "sqlite://") DEBUG = True MESSAGING = "in_memory" PDC_URL = "https://pdc.fedoraproject.org/rest_api/v1" diff --git a/contrib/run-unittests.sh b/contrib/run-unittests.sh new file mode 100755 index 0000000..443ef17 --- /dev/null +++ b/contrib/run-unittests.sh @@ -0,0 +1,67 @@ +#!/bin/bash +# +# Run MBS unit tests matrix +# | SQLite | PostgreSQL +# ------------------------------ +# py2 | x | x +# py3 | x | x +# +# Command line options: +# --py3: run tests inside mbs-test-fedora container with Python 3. If not +# set, tests will run in mbs-test-centos with Python 2 by default. +# --with-pgsql: run tests with PostgreSQL, otherwise SQLite is used. +# +# Please note that, both of them can have arbitrary value as long as one of +# them is set. So, generally, it works by just setting to 1 or yes for +# simplicity. + +enable_py3= +with_pgsql= + +for arg in "$@"; do + case $arg in + --py3) enable_py3=1 ;; + --with-pgsql) with_pgsql=1 ;; + esac +done + +image_ns=quay.io/factory2 +postgres_image="postgres:9.5.17" +db_container_name="mbs-test-db" +source_dir="$(realpath "$(dirname "$0")/..")" +volume_mount="${source_dir}:/src:Z" +db_name=mbstest +db_password=mbstest +pgdb_uri="postgresql+psycopg2://postgres:${db_password}@db/${db_name}" +db_bg_container= + +if [ -n "$enable_py3" ]; then + test_image="${image_ns}/mbs-test-fedora" +else + test_image="${image_ns}/mbs-test-centos" +fi + +container_opts=(--rm -i -t -v "${volume_mount}" --name mbs-test) + +if [ -n "$with_pgsql" ]; then + container_opts+=(--link "${db_container_name}":db -e "DATABASE_URI=$pgdb_uri") + + # Database will be generated automatically by postgres container during launch. + # Setting this password makes it possible to get into database container + # and check the data. + db_bg_container=$( + docker run --rm --name $db_container_name \ + -e POSTGRES_PASSWORD=$db_password \ + -e POSTGRES_DB=$db_name \ + -d \ + $postgres_image + ) +fi + +(cd "$source_dir" && docker run "${container_opts[@]}" $test_image) + +rv=$? + +[ -n "$db_bg_container" ] && docker stop "$db_bg_container" +exit $rv + diff --git a/docker/Dockerfile-tests b/docker/Dockerfile-tests index fdeb4d5..a25a21e 100644 --- a/docker/Dockerfile-tests +++ b/docker/Dockerfile-tests @@ -38,6 +38,7 @@ RUN yum -y install \ python-solv \ python-sqlalchemy \ python2-pungi \ + python-psycopg2 \ # Test-only dependencies python-flake8 \ python-mock \ diff --git a/docker/Dockerfile-tests-py3 b/docker/Dockerfile-tests-py3 index c2db2db..efc2c1e 100644 --- a/docker/Dockerfile-tests-py3 +++ b/docker/Dockerfile-tests-py3 @@ -28,6 +28,7 @@ RUN dnf -y install \ python3-solv \ python3-sqlalchemy \ python3-pungi \ + python3-psycopg2 \ # Test-only dependencies python3-pytest \ python3-flake8 \ diff --git a/docs/CONTRIBUTING.rst b/docs/CONTRIBUTING.rst index 827400c..e2e7d8a 100644 --- a/docs/CONTRIBUTING.rst +++ b/docs/CONTRIBUTING.rst @@ -1,28 +1,33 @@ Running Tests ============= -Since MBS requires Python dependencies that aren't available using PyPi (e.g. libsolv bindings), -there are container images (based on CentOS and Fedora) that can be used to run the code analysis and unit tests. +Since MBS requires Python dependencies that aren't available using PyPi (e.g. +libsolv bindings), there are container images (based on CentOS and Fedora) that +can be used to run the code analysis and unit tests. -To run the tests, you must first install `podman` with:: +* ``docker/Dockerfile-tests`` is based on ``centos:7``, inside which tests run + with Python 2. - $ sudo dnf install podman +* ``docker/Dockerfile-tests-py3`` is based on ``fedora:29``, inside which tests + run with Python 3. -From the repo root, run the tests with:: +Both of these images are available from Quay.io under `factory2 organization`_ +and named ``mbs-test-centos`` and ``mbs-test-fedora`` individually. Refer to +section "Updating test images in Quay" to learn how to manage these images. - $ podman run -t --rm -v $PWD:/src:Z quay.io/factory2/mbs-test-centos +.. _factory2: https://quay.io/organization/factory2 -To run the tests with Python 3 use the image based on Fedora:: +To run the tests, just simply run: ``contrib/run-unittests.sh`` - $ podman run -t --rm -v $PWD:/src:Z quay.io/factory2/mbs-test-fedora +By default, this script runs tests inside container ``mbs-test-centos`` +with Python 2 and SQLite database. -If you need to build the container image locally use:: +There are options to change the tests enviornment: - $ podman build -t mbs-test-centos -f docker/Dockerfile-tests . +* ``--py3``: run tests with Python 3. +* ``--with-pgsql``: run tests with PostgreSQL database. -or:: - - $ podman build -t mbs-test-fedora -f docker/Dockerfile-tests-py3 . +For example, ``contrib/run-unittests.sh --py3 --with-pgsql``. Style Guide =========== @@ -184,7 +189,19 @@ Historical Names of Module Build Service Updating test images in Quay ============================ -The Quay web UI can be used to update the images used for testing: +The docker images inside which to run tests could be built locally or via Quay +web UI. + +For building locally, use ``podman build`` or ``docker build``. For example +with ``podman``:: + + $ podman build -t quay.io/factory2/mbs-test-centos -f docker/Dockerfile-tests . + +or:: + + $ podman build -t quay.io/factory2/mbs-test-fedora -f docker/Dockerfile-tests-py3 . + +To update the images used for testing via Quay web UI: * https://quay.io/repository/factory2/mbs-test-centos * https://quay.io/repository/factory2/mbs-test-fedora diff --git a/tox.ini b/tox.ini index c421aa1..03c86a7 100644 --- a/tox.ini +++ b/tox.ini @@ -19,6 +19,8 @@ exclude = [testenv] usedevelop = true sitepackages = true +# Allow to switch database backend for running tests. +passenv = DATABASE_URI whitelist_externals = flake8 py.test-3