Skip to content

Commit

Permalink
Conditionally import ZoneInfo; use matrix strategy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ColeDCrawford committed Jun 3, 2024
1 parent 5db7fb5 commit fda0dfe
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/ci-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions catchpy/consumer/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 5 additions & 19 deletions test.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit fda0dfe

Please sign in to comment.