Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run tests in a docker container #342

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/test-docker-image-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Docker Image Build for Tests

on:
workflow_dispatch:
push:
paths:
- "tests/Dockerfile"
- "tests/run.sh"
- "tests/requirements.txt"
- ".github/workflows/test-docker-image-build.yml"

env:
IMAGE_NAME: "pitop/pt-miniscreen-test-runner"
PLATFORMS: "linux/amd64"

jobs:
build-push-docker-hub:
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
version: latest
install: true

- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch

- name: Build and push
id: docker_build_push
uses: docker/build-push-action@v2
with:
context: tests
file: tests/Dockerfile
platforms: ${{ env.PLATFORMS }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Show image digest
run: echo ${{ steps.docker_build_push.outputs.digest }}
20 changes: 7 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@ on:
branches:
- master

env:
IMAGE_NAME: "pitop/pt-miniscreen-test-runner"

jobs:
test:
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Install dependencies
run: |
sudo apt update
sudo apt install python3-pip -y
pip3 install -r tests/requirements.txt

- name: Run tests
run: |
pytest --verbose --cov-report term-missing --cov=pt_miniscreen
coverage xml
docker run \
--rm \
--volume "$PWD":/src \
${{ env.IMAGE_NAME }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ repos:
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies: [types-all]
additional_dependencies: [types-setuptools]
exclude: ^legacy_widgets
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ profile = black
max-line-length = 150

[tool:pytest]
addopts = -n auto --dist=loadfile
addopts = -n auto --dist=loadfile --reruns 2 --reruns-delay 1
26 changes: 26 additions & 0 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM debian:bookworm

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update

RUN apt install -y \
python3 \
python3-pip \
libfreetype6-dev \
libjpeg-dev \
zlib1g-dev

RUN rm -f /usr/lib/python3.11/EXTERNALLY-MANAGED

COPY requirements.txt /tmp/requirements.txt

RUN pip3 install -r /tmp/requirements.txt --extra-index-url=https://packagecloud.io/pi-top/pypi/pypi/simple

RUN rm -f /tmp/requirements.txt

COPY scripts/run.sh /

WORKDIR /src

ENTRYPOINT [ "/run.sh" ]
42 changes: 42 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## Tests

We recommend running tests in a docker image. Most of the tests produce images that are compared with
a base snapshot, but sometimes results might be a bit different from machine to machine (e.g.: image might be shifted 1 pixel).

### Using pi-top test runner image

```
$ docker run \
--rm \
--volume "$PWD":/src \
pitop/pt-miniscreen-test-runner:latest
```

### Building the image

Build the image by running:

```
$ docker build -t pt-miniscreen-test-runner tests
```

Then, run the tests with:

```
$ docker run \
--rm \
--volume "$PWD":/src \
pt-miniscreen-test-runner
```

### Updating base images

Override the entrypoint to access the image using `bash`. Then use `pytest --snapshot-update` to update the snapshots in the tests folder.

```
$ docker run \
--rm \
--volume "$PWD":/src \
--entrypoint bash \
pitop/pt-miniscreen-test-runner:latest
```
17 changes: 17 additions & 0 deletions tests/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

if [ ! -d "/src" ]; then
echo "Error: /src directory not found, make sure to mount the project root directory to /src"
exit 1
fi

cd /src

# Install new dependencies
pip3 install -r tests/requirements.txt --extra-index-url=https://packagecloud.io/pi-top/pypi/pypi/simple

# Run the tests
pytest --verbose --cov-report term-missing --cov=pt_miniscreen || exit 1

# Generate the coverage report
coverage xml
Loading