Skip to content

Commit a1298c0

Browse files
authored
Auto start/stop postgres w/ tox (ansible#632)
1 parent 0140edb commit a1298c0

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
python-version: ${{ matrix.tests.python-version }}
4949

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

5353
- name: Run tox
5454
run: |

docker-compose.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
---
22
services:
33
postgres:
4-
image: "postgres:15"
4+
build:
5+
context: tools/dev_postgres
6+
dockerfile: Dockerfile
57
container_name: dab_postgres
6-
environment:
7-
POSTGRES_DB: dab_db
8-
POSTGRES_USER: dab
9-
POSTGRES_PASSWORD: dabing
10-
healthcheck:
11-
test: ["CMD", "pg_isready", "-U", "dab", "-d", "dab_db"]
12-
interval: 10s
13-
timeout: 5s
14-
retries: 5
158
ports:
169
- "55432:5432"
1710

docs/testing.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ All tests for django-ansible-base are located in `test_app/tests`. The directory
66

77
## Running tests
88

9+
### Requirements
10+
Ensure you have the needed python dependencies installed (either in your system or a virtual env):
11+
```
12+
pip install -r requirements/requirements_dev.txt
13+
```
14+
15+
### Run
916
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:
1017
```
1118
tox -e 311
@@ -14,11 +21,8 @@ tox -e 311
1421
### Test database
1522

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

20-
If all goes according to plan, tox should automatically kill the container after the tests run.
21-
However, if you stop tox early or otherwise need to manually kill the container, you can run `make stop-postgres`.
25+
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**.
2226

2327
## Checking code coverage locally
2428

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ legacy_tox_ini = """
109109
-r{toxinidir}/requirements/requirements_all.txt
110110
-r{toxinidir}/requirements/requirements_dev.txt
111111
allowlist_externals = sh
112-
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}'
112+
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}
113+
docker = db
113114
114115
[testenv:check]
115116
deps =
@@ -123,6 +124,11 @@ legacy_tox_ini = """
123124
setenv =
124125
DJANGO_SETTINGS_MODULE = test_app.sqlite3settings
125126
127+
[docker:db]
128+
dockerfile = {toxinidir}/tools/dev_postgres/Dockerfile
129+
expose =
130+
DB_PORT=5432/tcp
131+
126132
[testenv:flake8]
127133
deps =
128134
flake8

requirements/requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ djangorestframework
77
django-split-settings
88
ipython
99
tox
10+
tox-docker
1011
typeguard
1112
pytest
1213
pytest-asyncio

tools/dev_postgres/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM postgres:15
2+
3+
ENV POSTGRES_DB=dab_db
4+
ENV POSTGRES_USER=dab
5+
ENV POSTGRES_PASSWORD=dabing
6+
7+
EXPOSE 5432
8+
9+
HEALTHCHECK --interval=10s --timeout=5s --retries=5 CMD ["pg_isready", "-U", "$${POSTGRES_USER}", "-d", "$${POSTGRES_DB}"]

0 commit comments

Comments
 (0)