diff --git a/.cico-pr.pipeline b/.cico-pr.pipeline index 8cef077d..14edbed4 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 00000000..ac48b1cb --- /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 a9e0f3c3..583bfdd0 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 00000000..443ef17f --- /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 fdeb4d5c..a25a21e8 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 c2db2db4..efc2c1e9 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 827400cb..e2e7d8a8 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 c421aa1d..03c86a79 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