Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea01d4c

Browse files
David Robertsonreivilibre
David Robertson
andauthoredSep 24, 2021
Update postgresql testing script (#10906)
- Use sytest:bionic. Sytest:latest is two years old (do we want CI to push out latest at all?) and comes with Python 3.5, which we explictly no longer support. The script now runs under PostgreSQL 10 as a result. - Advertise script in the docs - Move pg testing script to scripts-dev directory - Write to host as the script's exector, not root A few changes to make it speedier to re-run the tests: - Create blank DB in the container, not the script, so we don't have to `initdb` each time - Use a named volume to persist the tox environment, so we don't have to fetch and install a bunch of packages from PyPI each time Co-authored-by: reivilibre <[email protected]>
1 parent 0420d4e commit ea01d4c

File tree

7 files changed

+92
-19
lines changed

7 files changed

+92
-19
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ __pycache__/
4040
/.coverage*
4141
/.mypy_cache/
4242
/.tox
43+
/.tox-pg-container
4344
/build/
4445
/coverage.*
4546
/dist/

‎changelog.d/10906.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update development testing script `test_postgresql.sh` to use a supported Python version and make re-runs quicker.

‎docker/Dockerfile-pgtests

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
# Use the Sytest image that comes with a lot of the build dependencies
22
# pre-installed
3-
FROM matrixdotorg/sytest:latest
3+
FROM matrixdotorg/sytest:bionic
44

55
# The Sytest image doesn't come with python, so install that
66
RUN apt-get update && apt-get -qq install -y python3 python3-dev python3-pip
77

88
# We need tox to run the tests in run_pg_tests.sh
99
RUN python3 -m pip install tox
1010

11-
ADD run_pg_tests.sh /pg_tests.sh
12-
ENTRYPOINT /pg_tests.sh
11+
# Initialise the db
12+
RUN su -c '/usr/lib/postgresql/10/bin/initdb -D /var/lib/postgresql/data -E "UTF-8" --lc-collate="C.UTF-8" --lc-ctype="C.UTF-8" --username=postgres' postgres
13+
14+
# Add a user with our UID and GID so that files get created on the host owned
15+
# by us, not root.
16+
ARG UID
17+
ARG GID
18+
RUN groupadd --gid $GID user
19+
RUN useradd --uid $UID --gid $GID --groups sudo --no-create-home user
20+
21+
# Ensure we can start postgres by sudo-ing as the postgres user.
22+
RUN apt-get update && apt-get -qq install -y sudo
23+
RUN echo "user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
24+
25+
ADD run_pg_tests.sh /run_pg_tests.sh
26+
# Use the "exec form" of ENTRYPOINT (https://docs.docker.com/engine/reference/builder/#entrypoint)
27+
# so that we can `docker run` this container and pass arguments to pg_tests.sh
28+
ENTRYPOINT ["/run_pg_tests.sh"]
29+
30+
USER user

‎docker/run_pg_tests.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ set -e
1010
# Set PGUSER so Synapse's tests know what user to connect to the database with
1111
export PGUSER=postgres
1212

13-
# Initialise & start the database
14-
su -c '/usr/lib/postgresql/9.6/bin/initdb -D /var/lib/postgresql/data -E "UTF-8" --lc-collate="en_US.UTF-8" --lc-ctype="en_US.UTF-8" --username=postgres' postgres
15-
su -c '/usr/lib/postgresql/9.6/bin/pg_ctl -w -D /var/lib/postgresql/data start' postgres
13+
# Start the database
14+
sudo -u postgres /usr/lib/postgresql/10/bin/pg_ctl -w -D /var/lib/postgresql/data start
1615

1716
# Run the tests
1817
cd /src
1918
export TRIAL_FLAGS="-j 4"
20-
tox --workdir=/tmp -e py35-postgres
19+
tox --workdir=./.tox-pg-container -e py36-postgres "$@"

‎docs/development/contributing_guide.md

+47
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,53 @@ To increase the log level for the tests, set `SYNAPSE_TEST_LOG_LEVEL`:
170170
SYNAPSE_TEST_LOG_LEVEL=DEBUG trial tests
171171
```
172172

173+
### Running tests under PostgreSQL
174+
175+
Invoking `trial` as above will use an in-memory SQLite database. This is great for
176+
quick development and testing. However, we recommend using a PostgreSQL database
177+
in production (and indeed, we have some code paths specific to each database).
178+
This means that we need to run our unit tests against PostgreSQL too. Our CI does
179+
this automatically for pull requests and release candidates, but it's sometimes
180+
useful to reproduce this locally.
181+
182+
To do so, [configure Postgres](../postgres.md) and run `trial` with the
183+
following environment variables matching your configuration:
184+
185+
- `SYNAPSE_POSTGRES` to anything nonempty
186+
- `SYNAPSE_POSTGRES_HOST`
187+
- `SYNAPSE_POSTGRES_USER`
188+
- `SYNAPSE_POSTGRES_PASSWORD`
189+
190+
For example:
191+
192+
```shell
193+
export SYNAPSE_POSTGRES=1
194+
export SYNAPSE_POSTGRES_HOST=localhost
195+
export SYNAPSE_POSTGRES_USER=postgres
196+
export SYNAPSE_POSTGRES_PASSWORD=mydevenvpassword
197+
trial
198+
```
199+
200+
#### Prebuilt container
201+
202+
Since configuring PostgreSQL can be fiddly, we can make use of a pre-made
203+
Docker container to set up PostgreSQL and run our tests for us. To do so, run
204+
205+
```shell
206+
scripts-dev/test_postgresql.sh
207+
```
208+
209+
Any extra arguments to the script will be passed to `tox` and then to `trial`,
210+
so we can run a specific test in this container with e.g.
211+
212+
```shell
213+
scripts-dev/test_postgresql.sh tests.replication.test_sharded_event_persister.EventPersisterShardTestCase
214+
```
215+
216+
The container creates a folder in your Synapse checkout called
217+
`.tox-pg-container` and uses this as a tox environment. The output of any
218+
`trial` runs goes into `_trial_temp` in your synapse source directory — the same
219+
as running `trial` directly on your host machine.
173220

174221
## Run the integration tests ([Sytest](https://github.com/matrix-org/sytest)).
175222

‎scripts-dev/test_postgresql.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
# This script builds the Docker image to run the PostgreSQL tests, and then runs
4+
# the tests. It uses a dedicated tox environment so that we don't have to
5+
# rebuild it each time.
6+
7+
# Command line arguments to this script are forwarded to "tox" and then to "trial".
8+
9+
set -e
10+
11+
# Build, and tag
12+
docker build docker/ \
13+
--build-arg "UID=$(id -u)" \
14+
--build-arg "GID=$(id -g)" \
15+
-f docker/Dockerfile-pgtests \
16+
-t synapsepgtests
17+
18+
# Run, mounting the current directory into /src
19+
docker run --rm -it -v "$(pwd):/src" -v synapse-pg-test-tox:/tox synapsepgtests "$@"

‎test_postgresql.sh

-12
This file was deleted.

0 commit comments

Comments
 (0)
This repository has been archived.