From fda0dfe1fadaf2206b02140d0144390df02b996c Mon Sep 17 00:00:00 2001 From: Cole Crawford <16374762+ColeDCrawford@users.noreply.github.com> Date: Mon, 3 Jun 2024 11:30:55 -0400 Subject: [PATCH] Conditionally import ZoneInfo; use matrix strategy Instead of using tox, use a build argument to control the Python Docker base image to take advantage of GHA parallel / matrix strategy. We end up with multiple containers but they should build faster and be easier to debug than having to manage multiple versions of Python and PATH in one image. --- .github/workflows/ci-pytest.yml | 16 +++++++++++---- catchpy/consumer/tests/test_middleware.py | 6 ++++-- docker-compose-test.yml | 2 ++ test.Dockerfile | 24 +++++------------------ 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci-pytest.yml b/.github/workflows/ci-pytest.yml index 3a9b5b5..80048f2 100644 --- a/.github/workflows/ci-pytest.yml +++ b/.github/workflows/ci-pytest.yml @@ -11,17 +11,25 @@ env: jobs: tests: runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 + - name: Build Docker images run: | - docker compose -f docker-compose-test.yml build + PYTHON_VERSION=${{ matrix.python-version }} docker compose -f docker-compose-test.yml build + - name: Run Docker Compose containers run: | - docker compose -f docker-compose-test.yml up -d + PYTHON_VERSION=${{ matrix.python-version }} docker compose -f docker-compose-test.yml up -d + - name: Run Pytest unit tests within Compose run: | - docker compose -f docker-compose-test.yml exec web bash -c "tox" + docker compose -f docker-compose-test.yml exec web pytest + - name: Stop Docker Compose containers if: always() run: docker compose -f docker-compose.yml down \ No newline at end of file diff --git a/catchpy/consumer/tests/test_middleware.py b/catchpy/consumer/tests/test_middleware.py index 65edfdc..19f9803 100644 --- a/catchpy/consumer/tests/test_middleware.py +++ b/catchpy/consumer/tests/test_middleware.py @@ -1,9 +1,11 @@ from datetime import datetime, timedelta -from zoneinfo import ZoneInfo - import pytest from django.http import HttpResponse from django.test import RequestFactory +try: + from zoneinfo import ZoneInfo +except ImportError: + from backports.zoneinfo import ZoneInfo from ..catchjwt import decode_token, encode_catchjwt, validate_token from ..jwt_middleware import ( diff --git a/docker-compose-test.yml b/docker-compose-test.yml index a7f26dd..b8de7be 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -12,6 +12,8 @@ services: build: context: . dockerfile: test.Dockerfile + args: + PYTHON_VERSION: ${PYTHON_VERSION} image: hx/catchpy:test command: ["./wait-for-it.sh", "db:5432", "--", "python", "manage.py", "runserver", "0.0.0.0:8000"] volumes: diff --git a/test.Dockerfile b/test.Dockerfile index 54c4cc5..1216bd0 100644 --- a/test.Dockerfile +++ b/test.Dockerfile @@ -1,27 +1,13 @@ -FROM python:3.11 +# pass in Python version as build arg to allow for tests to be run on multiple versions of Python +ARG PYTHON_VERSION=3.11 +FROM python:${PYTHON_VERSION} +ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1 - -RUN apt-get update # Include fortune library for quote generation for text annotations -RUN apt-get install fortune-mod -y +RUN apt-get update && apt-get install -y fortune-mod ENV PATH "$PATH:/usr/games" -# Install all other versions of Python we want to test with tox -RUN git clone https://github.com/pyenv/pyenv /root/.pyenv -RUN for PYTHON_VERSION in 3.8.19 3.9.19 3.10.14 3.11.9 3.12.3; do \ - set -ex \ - && /root/.pyenv/bin/pyenv install ${PYTHON_VERSION} \ - && /root/.pyenv/versions/${PYTHON_VERSION}/bin/python -m pip install --upgrade pip \ - ; done - -# Add to PATH, in order of lowest precedence to highest. -ENV PATH /root/.pyenv/versions/3.8.19/bin:${PATH} -ENV PATH /root/.pyenv/versions/3.9.19/bin:${PATH} -ENV PATH /root/.pyenv/versions/3.10.14/bin:${PATH} -ENV PATH /root/.pyenv/versions/3.12.3/bin:${PATH} -ENV PATH /root/.pyenv/versions/3.11.9/bin:${PATH} - RUN mkdir /code WORKDIR /code ADD . /code