Skip to content

Instantly share code, notes, and snippets.

@flyinghyrax
Last active December 23, 2024 01:35
Show Gist options
  • Save flyinghyrax/e3bed496cb049e71a5a5893e697f0b20 to your computer and use it in GitHub Desktop.
Save flyinghyrax/e3bed496cb049e71a5a5893e697f0b20 to your computer and use it in GitHub Desktop.
Docker Compose configuration for testing multiple Python versions
name: bandersnatch-test
services:
test-runner:
build:
dockerfile: 'runner.Dockerfile'
environment:
AWS_ACCESS_KEY: 'minioadmin'
AWS_SECRET_ACCESS_KEY: 'minioadmin'
AWS_EC2_METADATA_DISABLED: 'true'
BANDERSNATCH_S3_ENDPOINT_URL: 'http://block-storage:9000'
TOX_WORK_DIR: '/tox'
TOX_ALWAYS_COPY: 'true'
TOX_OVERRIDE: 'tox.envlist=py3.11,py3.12'
volumes:
- type: bind
source: '../bandersnatch'
target: '/bandersnatch'
- type: volume
source: tox-data
target: /tox
networks:
- internal
block-storage:
image: 'minio/minio'
command: 'server /data --console-address ":9001"'
volumes:
- type: volume
source: minio-data
target: /data
# not required unless you want to run S3 tests outside the test-runner service
# ports:
# - "127.0.0.1:9000:9000"
# - "127.0.0.1:9001:9001"
networks:
- internal
volumes:
minio-data:
tox-data:
networks:
internal:
#!/usr/bin/sh
# add deadsnakes for python versions other than the distro shipped
apt-get update
apt-get install --yes 'software-properties-common'
add-apt-repository --yes --ppa 'deadsnakes/ppa'
# install many pythons
apt-get install --yes \
python3.10 python3.10-dev \
python3.11 python3.11-dev \
python3.12 python3.12-dev \
python3-venv python3-pip
# remove downloaded files
apt-get clean
FROM library/ubuntu:24.04
RUN --mount=type=bind,source=install-pythons.sh,destination=/tmp/install-pythons.sh \
["sh", "/tmp/install-pythons.sh" ]
RUN python3 -m venv /venv
ENV PATH=/venv/bin:$PATH
RUN /venv/bin/python -m pip install --upgrade pip setuptools tox
RUN mkdir /bandersnatch
VOLUME /bandersnatch
WORKDIR /bandersnatch
CMD ["tail", "-f", "/dev/null"]

directory placement

Expects to be in a sibling directory to the bandersnatch project root, e.g.:

- projects/
  - bandersnatch/
    - src/
    - pyproject.toml
    - ...
  - bandersnatch-test/
    - docker-compose.yml
    - runner.Dockerfile
    - install-pythons.sh

Technically this is adjustable by editing the bind mount in the volumes configuration for test-runner in the Docker Compose file.

run services

docker compose up -d

Starts 2 services:

  • test-runner: an Ubuntu base image with multiple versions of Python 3 available
  • block-storage: a default minio instance used as an S3-compatible object store when testing the S3 storage backend

executing tests

Running pytest:

docker compose exec test-runner tox run

This runs unit tests for Python 3.11 and 3.12. The Python versions are controlled by this line in the Docker Compose file:

TOX_OVERRIDE: 'tox.envlist=py3.11,py3.12'

You can run specific versions with -e:

docker compose exec test-runner tox run -e py3.11

And pass arguments to pytest by adding them after a --:

docker compose exec test-runner tox run -- -m 'not s3'

Running the integration test script:

docker compose exec -e TOXENV=INTEGRATION test-runner tox exec \
    -e py3.11 \
    -x testenv.allowlist_externals=*.py \
    -x testenv.pass_env+=TOXENV \
    -- ./test_runner.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment