From 2e34488e61352151241338cbdef9f8f4519a03ae Mon Sep 17 00:00:00 2001 From: Matthew Cane <39704070+MatthewCane@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:07:07 +0100 Subject: [PATCH] Use docker compose setup for replicatable testing (#24) * Use docker compose setup for replicatable testing * Update readme, add additional assert --- .github/workflows/pull_request.yml | 18 +++++-------- .gitignore | 3 ++- .vscode/settings.json | 7 +++++ README.md | 8 +++--- tests/assets/auth.db | Bin 77824 -> 77824 bytes tests/assets/test_containers.yml | 10 ++++++-- tests/conftest.py | 40 ++++++++++++++++++++++++++++- tests/helpers.py | 2 +- tests/test_init.py | 1 + 9 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 3bc993f..44c66bf 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -10,17 +10,6 @@ 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 @@ -28,7 +17,12 @@ jobs: - 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/compose-action@v2.0.2 with: diff --git a/.gitignore b/.gitignore index dc073cb..e95c8e2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ __pytest__ .coverage __pycache__ dist/ -site/ \ No newline at end of file +site/ +.DS_Store \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1be6a53 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/README.md b/README.md index a11b185..e1284fa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tests/assets/auth.db b/tests/assets/auth.db index 2d1f26307bef58cebd5b926946f2ff67e32dde4d..8fd27bd4afeeff82161ac543b9f6aa8686b51779 100644 GIT binary patch delta 27 jcmZp8z|!!5Wr8##_e2?IM(&LXlkPK3Y~B3jK2HDuk5>x# delta 27 jcmZp8z|!!5Wr8##*F+g-My`zslkPK>-`)J>K2HDukdg~D diff --git a/tests/assets/test_containers.yml b/tests/assets/test_containers.yml index 400fec9..e6bb954 100644 --- a/tests/assets/test_containers.yml +++ b/tests/assets/test_containers.yml @@ -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: @@ -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: diff --git a/tests/conftest.py b/tests/conftest.py index e247c51..b4b7c8f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 @@ -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) diff --git a/tests/helpers.py b/tests/helpers.py index 5895cc7..002be95 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -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 diff --git a/tests/test_init.py b/tests/test_init.py index 145a4e0..67843c3 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -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"