Skip to content

Commit

Permalink
Use docker compose setup for replicatable testing (#24)
Browse files Browse the repository at this point in the history
* Use docker compose setup for replicatable testing

* Update readme, add additional assert
  • Loading branch information
MatthewCane authored Oct 22, 2024
1 parent 057f261 commit 2e34488
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 22 deletions.
18 changes: 6 additions & 12 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@ jobs:
test:
name: Pytest
runs-on: ubuntu-latest
# This does not work because you can't pass
# commands into the container to start the
# server! Massive limitation in Actions.
# services:
# ntfy:
# image: binwiederhier/ntfy
# ports:
# - 80:80
# env:
# NTFY_ATTACHMENT_CACHE_DIR: /cache
# NTFY_BASE_URL: http://localhost
steps:
- uses: actions/checkout@v4

- uses: citizensadvice/python-poetry-setup-action@v1

- name: Install dependencies
run: poetry install --with dev


- name: Setup Docker Compose
uses: KengoTODA/actions-setup-docker-compose@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Start test containers
uses: hoverkraft-tech/[email protected]
with:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ __pytest__
.coverage
__pycache__
dist/
site/
site/
.DS_Store
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ These tools are also run in the CI pipeline and must pass before merging.

This project is aiming for 95% code coverage. Any added features must include comprihensive tests.

#### Setup Steps
#### Testing Steps

1. Start the test docker container with `docker-compose -f tests/assets/test_containers.yml up`
2. Run the tests with `poetry run pytest --cov`

The tests will sent messages to the `python_ntfy_testing` topic so you will need to view the web interface and subcribe to that topic to see the test messages.
1. Make sure you have `docker` and `docker-compose` installed
2. Run the tests with `poetry run pytest --cov` or use the VSCode testing extension
Binary file modified tests/assets/auth.db
Binary file not shown.
10 changes: 8 additions & 2 deletions tests/assets/test_containers.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
version: '3.8'

services:
ntfy-without-auth:
image: binwiederhier/ntfy:latest
command: serve
healthcheck:
test: ["CMD-SHELL", "curl", "-f", "http://localhost/v1/health"]
interval: 1s
retries: 3
ports:
- "8080:80"
environment:
Expand All @@ -13,6 +15,10 @@ services:
ntfy-with-auth:
image: binwiederhier/ntfy:latest
command: serve
healthcheck:
test: ["CMD-SHELL", "curl", "-f", "http://localhost/v1/health"]
interval: 1s
retries: 3
ports:
- "8081:80"
environment:
Expand Down
40 changes: 39 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from pytest import fixture
import subprocess
from pathlib import Path
from time import sleep
from typing import Generator

from pytest import fixture, mark


@fixture
Expand Down Expand Up @@ -30,3 +35,36 @@ def no_auth(monkeypatch) -> None:
monkeypatch.delenv("NTFY_USER", raising=False)
monkeypatch.delenv("NTFY_PASSWORD", raising=False)
monkeypatch.delenv("NTFY_USER", raising=False)


@fixture(scope="session", autouse=True)
def docker_compose_up() -> Generator:
"""Fixture to start up docker compose before tests and tear it down after."""
compose_file = Path("tests/assets/test_containers.yml").resolve()

# Start up docker compose
subprocess.run( # noqa: S603
["docker-compose", "-f", str(compose_file), "up", "-d"], # noqa: S607
check=True,
capture_output=True,
)

sleep(0.5)

# Run the tests
yield

# Tear down the containers
subprocess.run(["docker-compose", "-f", str(compose_file), "down"], check=True) # noqa: S607, S603


def pytest_configure(config) -> None:
config.addinivalue_line(
"markers", "requires_docker: mark test as requiring docker services"
)


def pytest_collection_modifyitems(session, config, items) -> None:
"""Automatically mark all test items as requiring docker."""
for item in items:
item.add_marker(mark.requires_docker)
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
topic = "python_ntfy_testing"


def random_string(length: int = 10) -> str:
def random_string(length: int = 32) -> str:
return "".join([random.choice(string.ascii_lowercase) for _ in range(length)]) # noqa: S311
1 change: 1 addition & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ def test_init_set_topic(localhost_server_no_auth, no_auth) -> None:
ntfy.set_topic("test2")
assert ntfy._topic == "test2"
assert ntfy.url == "/".join([environ["NTFY_SERVER"], "test2"])
assert ntfy.get_topic() == "test2"

0 comments on commit 2e34488

Please sign in to comment.