Skip to content

Commit

Permalink
Auto start/stop postgres w/ tox (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavenst authored Oct 31, 2024
1 parent 0140edb commit a1298c0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
python-version: ${{ matrix.tests.python-version }}

- name: Install tox
run: pip${{ matrix.tests.python-version }} install tox
run: pip${{ matrix.tests.python-version }} install tox tox-docker

- name: Run tox
run: |
Expand Down
13 changes: 3 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
---
services:
postgres:
image: "postgres:15"
build:
context: tools/dev_postgres
dockerfile: Dockerfile
container_name: dab_postgres
environment:
POSTGRES_DB: dab_db
POSTGRES_USER: dab
POSTGRES_PASSWORD: dabing
healthcheck:
test: ["CMD", "pg_isready", "-U", "dab", "-d", "dab_db"]
interval: 10s
timeout: 5s
retries: 5
ports:
- "55432:5432"

Expand Down
12 changes: 8 additions & 4 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All tests for django-ansible-base are located in `test_app/tests`. The directory

## Running tests

### Requirements
Ensure you have the needed python dependencies installed (either in your system or a virtual env):
```
pip install -r requirements/requirements_dev.txt
```

### Run
To run the test suite locally you can use tox. By default, with no arguments, tox will attempt to run the tests in various version of python. To run a specific version you can add the `-e` parameter like:
```
tox -e 311
Expand All @@ -14,11 +21,8 @@ tox -e 311
### Test database

Tests require PostgreSQL running in order to pass.
Running PostgreSQL can be done by running `make postgres` in the root of your django-ansible-base clone.
This will create a detached/daemonized PostgreSQL container called `dab_postgres`.

If all goes according to plan, tox should automatically kill the container after the tests run.
However, if you stop tox early or otherwise need to manually kill the container, you can run `make stop-postgres`.
tox will automatically create/start and stop/destroy the postgres container. If you want the container to remain after test execution, add **--docker-dont-stop=db**.

## Checking code coverage locally

Expand Down
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ legacy_tox_ini = """
-r{toxinidir}/requirements/requirements_all.txt
-r{toxinidir}/requirements/requirements_dev.txt
allowlist_externals = sh
commands = sh -c 'make postgres && pytest -n auto --cov=. --cov-report=xml:coverage.xml --cov-report=html --cov-report=json --cov-branch --junit-xml=django-ansible-base-test-results.xml {env:ANSIBLE_BASE_PYTEST_ARGS} {env:ANSIBLE_BASE_TEST_DIRS:test_app/tests} {posargs}'
commands = pytest -n auto --cov=. --cov-report=xml:coverage.xml --cov-report=html --cov-report=json --cov-branch --junit-xml=django-ansible-base-test-results.xml {env:ANSIBLE_BASE_PYTEST_ARGS} {env:ANSIBLE_BASE_TEST_DIRS:test_app/tests} {posargs}
docker = db
[testenv:check]
deps =
Expand All @@ -123,6 +124,11 @@ legacy_tox_ini = """
setenv =
DJANGO_SETTINGS_MODULE = test_app.sqlite3settings
[docker:db]
dockerfile = {toxinidir}/tools/dev_postgres/Dockerfile
expose =
DB_PORT=5432/tcp
[testenv:flake8]
deps =
flake8
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ djangorestframework
django-split-settings
ipython
tox
tox-docker
typeguard
pytest
pytest-asyncio
Expand Down
9 changes: 9 additions & 0 deletions tools/dev_postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM postgres:15

ENV POSTGRES_DB=dab_db
ENV POSTGRES_USER=dab
ENV POSTGRES_PASSWORD=dabing

EXPOSE 5432

HEALTHCHECK --interval=10s --timeout=5s --retries=5 CMD ["pg_isready", "-U", "$${POSTGRES_USER}", "-d", "$${POSTGRES_DB}"]

0 comments on commit a1298c0

Please sign in to comment.