Skip to content

Commit

Permalink
Add base for Python tests
Browse files Browse the repository at this point in the history
As discussed in #8 (comment)
  • Loading branch information
joao-p-marques authored and yajo committed Jul 8, 2021
1 parent c210bd3 commit 23c2c03
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .copier-answers.image-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ main_branches:
project_name: docker-whitelist
project_owner: Tecnativa
push_to_ghcr: true
pytest: false
pytest: true
python_versions:
- "3.9"
44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,56 @@ name: Build, Test & Deploy
tags:
- "v*"
workflow_dispatch:
inputs:
pytest_addopts:
description:
Extra options for pytest; use -vv for full details; see
https://docs.pytest.org/en/latest/example/simple.html#how-to-change-command-line-options-defaults
required: false

env:
LANG: "en_US.utf-8"
LC_ALL: "en_US.utf-8"
PIP_CACHE_DIR: ${{ github.workspace }}/.cache.~/pip
PIPX_HOME: ${{ github.workspace }}/.cache.~/pipx
POETRY_CACHE_DIR: ${{ github.workspace }}/.cache.~/pypoetry
POETRY_VIRTUALENVS_IN_PROJECT: "true"
PYTEST_ADDOPTS: ${{ github.event.inputs.pytest_addopts }}
PYTHONIOENCODING: "UTF-8"

jobs:
build-test:
runs-on: ubuntu-20.04
strategy:
matrix:
python:
- 3.9
steps:
# Prepare environment
- uses: actions/checkout@v2
# Set up and run tests
- name: Install python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Generate cache key CACHE
run:
echo "CACHE=${{ secrets.CACHE_DATE }} ${{ runner.os }} $(python -VV |
sha256sum | cut -d' ' -f1) ${{ hashFiles('pyproject.toml') }} ${{
hashFiles('poetry.lock') }}" >> $GITHUB_ENV
- uses: actions/cache@v2
with:
path: |
.cache.~
.venv
~/.local/bin
key: venv ${{ env.CACHE }}
- run: pip install poetry
- name: Patch $PATH
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
- run: poetry install
# Run tests
- run: poetry run pytest --prebuild
build-push:
runs-on: ubuntu-20.04
services:
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": ["pytest"],
"python.pythonPath": ".venv/bin/python",
"python.testing.pytestEnabled": true
}
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,45 @@ networks:
```

And voilà! `app` has fonts, but nothing more. ✋👮

## Development

All the dependencies you need to develop this project (apart from Docker itself) are
managed with [poetry](https://python-poetry.org/).

To set up your development environment, run:

```bash
pip install pipx # If you don't have pipx installed
pipx install poetry # Install poetry itself
poetry install # Install the python dependencies and setup the development environment
```

### Testing

To run the tests locally, add `--prebuild` to autobuild the image before testing:

```sh
poetry run pytest --prebuild
```

By default, the image that the tests use (and optionally prebuild) is named
`test:docker-whitelist`. If you prefer, you can build it separately before testing, and
remove the `--prebuild` flag, to run the tests with that image you built:

```sh
docker image build -t test:docker-whitelist .
poetry run pytest
```

If you want to use a different image, pass the `--image` command line argument with the
name you want:

```sh
# To build it automatically
poetry run pytest --prebuild --image my_custom_image
# To prebuild it separately
docker image build -t my_custom_image .
poetry run pytest --image my_custom_image
```
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

[tool.poetry]
name = "docker-whitelist"
version = "0.0.0"
description = ""
authors = ["Tecnativa"]

[tool.poetry.dependencies]
python = "^3.9"
plumbum = "^1.6.9"

[tool.poetry.dev-dependencies]
black = {version = "^20.8b1", allow-prereleases = true}
flake8 = "^3.8.4"
plumbum = "^1.6.9"
pytest-xdist = "^2.1.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = -n auto -ra
41 changes: 41 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging
from pathlib import Path

import pytest
from plumbum.cmd import docker

_logger = logging.getLogger(__name__)


def pytest_addoption(parser):
"""Allow prebuilding image for local testing."""
parser.addoption(
"--prebuild", action="store_true", help="Build local image before testing"
)
parser.addoption(
"--image",
action="store",
default="test:docker-whitelist",
help="Specify testing image name",
)


@pytest.fixture(scope="session")
def image(request):
"""Get image name. Builds it if needed."""
image = request.config.getoption("--image")
if request.config.getoption("--prebuild"):
build = docker["image", "build", "-t", image, Path(__file__).parent.parent]
retcode, stdout, stderr = build.run()
_logger.log(
# Pytest prints warnings if a test fails, so this is a warning if
# the build succeeded, to allow debugging the build logs
logging.ERROR if retcode else logging.WARNING,
"Build logs for COMMAND: %s\nEXIT CODE:%d\nSTDOUT:%s\nSTDERR:%s",
build.bound_command(),
retcode,
stdout,
stderr,
)
assert not retcode, "Image build failed"
return image

0 comments on commit 23c2c03

Please sign in to comment.