Test suite & CI/CD#
cotainr comes with an extensive test suite which is run automatically via continuous integration when code is committed to the cotainr GitHub repository: DeiC-HPC/cotainr
The test suite#
The cotainr test suite is implemented using pytest and uses the pytest-cov plugin for reporting test coverage. In order to run the test suite locally, you need to setup a development environment containing the cotainr dependencies as well as the Python test dependencies declared in the cotainr pyproject.toml file. Either install the cotainr dependencies manually or consider using the containerized development environment. The Python test dependencies may be installed using uv:
$ uv sync --group tests
Alternatively, you can also install the (default) dev group which contains the full development environment, including the tests group.
Once the development environment has been installed, simply run pytest from the repository root directory, e.g. using uv:
$ uv run pytest
Pytest marks#
The following custom pytest marks are implemented:
pytest.mark.endtoend(): end-to-end tests of workflows using cotainr (may take a long time to run)pytest.mark.singularity_integration(): integration tests that require and use singularity/apptainer (may take a long time to run)pytest.mark.conda_integration(): integration tests that installs and manipulates a conda environment (may take a long time to run)
All tests are run by default when running pytest. You may select/deselect end-to-end and integration tests using the -m option to pytest, e.g. to only run the unittest:
$ pytest -m "not endtoend and not singularity_integration and not conda_integration"
The tests folder hierarchy and naming conventions#
All tests are placed in the cotainr/tests folder. This tests folder acts as a sub-package in cotainr. The cotainr.tests sub-package contains a sub-package for each module in cotainr (structured in the same way as the modules in cotainr). Each such test sub-package contains a module for each class/function in the corresponding cotainr module. If the test module relates to a class, it contains, for each method in that class, one test class with any number of tests cases (implemented as methods). If the test module relates to a function, it contains one test class for that function and, optionally, one test class for any private “helper” function to that function. Here are a few examples to illustrate all of this:
All tests of the
cotainr.pack.CondaInstallclass are placed in cotainr/tests/pack/test_conda_install.py which acts as a python module reachable viacotainr.tests.pack.test_conda_install.The tests of the
cotainr.pack.CondaInstall.add_environment()method are implemented in the classcotainr.tests.pack.test_conda_install.TestAddEnvironment.The tests of the
cotainr.util.stream_subprocess()function are implemented in the classcotainr.tests.util.test_stream_subprocess.TestStreamSubprocess. Specifically, this class implements test cases (methods) liketest_completed_process()ortest_check_returncode().The module
cotainr.tests.util.test_stream_subprocessalso includes a class,Test_PrintAndCaptureStream, implementing the tests of the private “helper” functioncotainr.utils._print_and_capture_stream().
In addition to the modules implementing the tests of the functions and classes in cotainr, the sub-packages in cotainr.tests may also include “special” modules implementing test fixtures and stubs:
patches.py: Contains all (monkey)patch fixtures related to that sub-package, e.g. the patch fixture
cotainr.tests.util.patches.patch_disable_stream_subprocess(). All patch fixtures are prefixed with patch_.data.py: Contains all test data fixtures related to that sub-package, e.g. the data fixture
cotainr.tests.container.data.data_cached_ubuntu_sif(). All data fixtures are prefixed with data_.stubs.py: Contains all test stubs related to that sub-package, e.g. the stub
cotainr.tests.cli.stubs.StubValidSubcommand. All stubs are prefixed with Stub.
All general purpose fixtures, which do not belong in one of the sub-package specific fixture modules listed above, are defined in the tests/conftest.py module.
Finally, the cotainr.tests.test_end_to_end module contains all workflow end-to-end test using cotainr.
Imports in test modules#
Imports in test modules are based on the following conventions:
Functions and classes, subject to testing, are imported using absolute imports, e.g.
import cotainr.pack.CondaInstallin tests/pack/test_conda_install.py.Sub-package specific fixtures are explicitly imported using relative imports, e.g.
from ..container.data import data_cached_ubuntu_sifin tests/pack/test_conda_install.py.Fixtures defined in tests/conftest.py are not explicitly imported (they are implicitly imported by pytest`). Thus, if a fixture is used, but not imported, in a test module, tests/conftest.py is the only module in which it can (or at least should) be defined.
Continuous Integration (CI)#
Continuous Integration (CI) is handled via GitHub Actions in the cotainr GitHub repository DeiC-HPC/cotainr.
Single sourced dependency matrix#
The versions of the cotainr dependencies used in the CI pipelines are single sourced from the matrix.json file. In addition to the cotainr dependencies, the matrix also defines the Github Runners and the corresponding architectures on which we test cotainr. For details of the runners see the github hosted runner overview. When running the CI test matrix, we differentiate between the following (meta)versions of dependencies:
minimum: The minimum supported version of the dependency.
latest: The newest tested version of the dependency.
relevant: Selected versions based on specific HPC systems, we prioritize to support.
all: All supported versions of the dependency, i.e. minimum, latest, and relevant versions.
In order to extract parts of the full test matrix, we use the Container-inputs.yml workflow. This is a reusable workflow that extracts parts of the matrix and returns them as outputs along with some other generally usable variables. All other workflows use this reusable workflow to get the relevant parts of the matrix for their specific use case. That way we ensure that the test matrix is single sourced and avoid code duplication in the workflows.
The containers inputs are used as a reusable workflow in favor of a composite action. While composite actions allow for including another full workflow as a step in a workflow job, for our CI workflows, we need the container inputs before the job is run to define the runs-on, strategy and container configuration for the job. Hence, the container inputs can’t be included as a step, but has to run as a separate pre-processing job, i.e. as a reusable workflow.
CI workflows#
The following CI workflows are implemented:
CI_pull_requests.yml: Runs the unit tests, integration tests, and end-to-end tests on pull requests (not draft pull requests) to the main branch. All Python versions and all SingularityCE as well as all Apptainer versions are tested. Additionally, lint and formatting checks (as described in the style guide) as well as a build of the cotainr wheel and sdist packages, a build of HTML documentation, and checks for any broken documentation hyperlinks are also run. Runs on both x86_64 and ARM64 architectures (except for the build and lint parts that only run on x86_64) in the containerized development environment.
CI_push.yml: Runs the unit tests on pushes to all branches. Restricted to minimum and latest Python versions as well as minimum and latest SingularityCE and Apptainer versions. Lint and formatting checks (as described in the style guide) are also run. Runs on both x86_64 and ARM64 architectures in the containerized development environment.
CI_build_docker_images.yml: Runs the automated process for building the containers used in the containerized development environment whenever changes are made to the files defining the development environment on the main branch or development branches starting with docker_dev_env.
The test suite in the CI on Pull Requests is very thorough, and so it is only launched for pull requests that are not in draft mode. Additionally, it is launched the moment when a pull request is taken out of draft mode. On development where end-to-end and singularity integration testing are critical, the test suite should be run locally through the containerized development environment.
Scheduled tests#
In order to proactively monitor for external changes that may break cotainr, we additionally run the CI_pull_requests.yml workflow as a scheduled GitHub actions (weekly, every Tuesday night).
Continuous Delivery (CD)#
Continuous Delivery (CD) is handled partly via GitHub Actions, partly via the a Read the Docs webhook integration to the cotainr GitHub repository: DeiC-HPC/cotainr.
CD workflows#
The following GitHub CD workflow is implemented:
CD_release.yml: Creates GitHub and PyPI releases when new tags following the versioning scheme are committed to the main branch.
The PyPI release process goes as follows:
Build the Python wheel.
Publish to TestPyPI index.
In a clean environment, download and install from TestPyPI and run basic CLI functionality.
Publish to PyPI and create a GitHub release with source archives.
The GitHub release does not have deployment protection rules as it can easily be undone by first removing the release through the GitHub UI and then remove the tag if something goes wrong.
The TestPyPI and PyPI index locations are both implemented as GitHub environments attached to the DeiC-HPC account. These environments have deployment protection rules which require review from a member of the HPC-developers team before the action is executed. This ensures protection against accidental tag pushes which is needed since releases on TestPyPI and PyPI are not supposed to be removed at any point in time.
The release workflow runs in the containerized development environment on a x86_64 architecture and makes use of the single sourced dependency matrix.
Read the Docs continuous documentation#
The HTML documentation served at http://cotainr.readthedocs.io is automatically built by Read the Docs based on the configuration in the .readthedocs.yml configuration file.
# Configuration file for the documentation build hosted on cotainr.readthedocs.io
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
version: 2
build:
os: ubuntu-24.04
tools:
python: "3"
jobs:
# We need to unshallow fetch all branches to determine the correct version number for the documentation build.
post_checkout:
- git fetch --unshallow || true
- git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' || true
- git fetch --all --tags || true
create_environment:
- asdf plugin add uv
- asdf install uv latest
- asdf global uv latest
install:
- UV_PROJECT_ENVIRONMENT=$READTHEDOCS_VIRTUALENV_PATH uv sync --frozen --group docs --link-mode=copy
pre_build:
- UV_PROJECT_ENVIRONMENT=$READTHEDOCS_VIRTUALENV_PATH uv run make -C doc apidoc
- UV_PROJECT_ENVIRONMENT=$READTHEDOCS_VIRTUALENV_PATH uv run make -C doc relnotes
sphinx:
builder: html
configuration: doc/conf.py
fail_on_warning: true
This fully automates the process of building Read the Docs *latest* and *stable* versions of the HTML documentation served at http://cotainr.readthedocs.io. Additionally, the “build pull requests” feature is enabled. A pull request documentation build is linked in the list of checks for the GitHub pull request.
The Read the Docs automated documentation release process is implemented in the “CalVer releases” automation rule in the cotainr` Read the Docs project. Essentially any git-tag matching the described versioning-scheme will be picked up by Read the Docs, built, and made available as well as activated as stable and latest.