Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 708d88b

Browse files
authored
Allow specifying the Postgres database's port when running unit tests with Postgres. (#12376)
1 parent efdbcfd commit 708d88b

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

changelog.d/12376.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow specifying the Postgres database's port when running unit tests with Postgres.

docs/development/contributing_guide.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,10 @@ To do so, [configure Postgres](../postgres.md) and run `trial` with the
206206
following environment variables matching your configuration:
207207

208208
- `SYNAPSE_POSTGRES` to anything nonempty
209-
- `SYNAPSE_POSTGRES_HOST`
210-
- `SYNAPSE_POSTGRES_USER`
211-
- `SYNAPSE_POSTGRES_PASSWORD`
209+
- `SYNAPSE_POSTGRES_HOST` (optional if it's the default: UNIX socket)
210+
- `SYNAPSE_POSTGRES_PORT` (optional if it's the default: 5432)
211+
- `SYNAPSE_POSTGRES_USER` (optional if using a UNIX socket)
212+
- `SYNAPSE_POSTGRES_PASSWORD` (optional if using a UNIX socket)
212213

213214
For example:
214215

@@ -220,6 +221,13 @@ export SYNAPSE_POSTGRES_PASSWORD=mydevenvpassword
220221
trial
221222
```
222223

224+
You don't need to specify the host, user, port or password if your Postgres
225+
server is set to authenticate you over the UNIX socket (i.e. if the `psql` command
226+
works without further arguments).
227+
228+
Your Postgres account needs to be able to create databases.
229+
230+
223231
## Run the integration tests ([Sytest](https://github.com/matrix-org/sytest)).
224232

225233
The integration tests are a more comprehensive suite of tests. They

tests/server.py

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
POSTGRES_BASE_DB,
7777
POSTGRES_HOST,
7878
POSTGRES_PASSWORD,
79+
POSTGRES_PORT,
7980
POSTGRES_USER,
8081
SQLITE_PERSIST_DB,
8182
USE_POSTGRES_FOR_TESTS,
@@ -747,6 +748,7 @@ def setup_test_homeserver(
747748
"host": POSTGRES_HOST,
748749
"password": POSTGRES_PASSWORD,
749750
"user": POSTGRES_USER,
751+
"port": POSTGRES_PORT,
750752
"cp_min": 1,
751753
"cp_max": 5,
752754
},
@@ -786,6 +788,7 @@ def setup_test_homeserver(
786788
database=POSTGRES_BASE_DB,
787789
user=POSTGRES_USER,
788790
host=POSTGRES_HOST,
791+
port=POSTGRES_PORT,
789792
password=POSTGRES_PASSWORD,
790793
)
791794
db_conn.autocommit = True
@@ -833,6 +836,7 @@ def cleanup():
833836
database=POSTGRES_BASE_DB,
834837
user=POSTGRES_USER,
835838
host=POSTGRES_HOST,
839+
port=POSTGRES_PORT,
836840
password=POSTGRES_PASSWORD,
837841
)
838842
db_conn.autocommit = True

tests/utils.py

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
POSTGRES_USER = os.environ.get("SYNAPSE_POSTGRES_USER", None)
3636
POSTGRES_HOST = os.environ.get("SYNAPSE_POSTGRES_HOST", None)
3737
POSTGRES_PASSWORD = os.environ.get("SYNAPSE_POSTGRES_PASSWORD", None)
38+
POSTGRES_PORT = (
39+
int(os.environ["SYNAPSE_POSTGRES_PORT"])
40+
if "SYNAPSE_POSTGRES_PORT" in os.environ
41+
else None
42+
)
3843
POSTGRES_BASE_DB = "_synapse_unit_tests_base_%s" % (os.getpid(),)
3944

4045
# When debugging a specific test, it's occasionally useful to write the
@@ -55,6 +60,7 @@ def setupdb():
5560
db_conn = db_engine.module.connect(
5661
user=POSTGRES_USER,
5762
host=POSTGRES_HOST,
63+
port=POSTGRES_PORT,
5864
password=POSTGRES_PASSWORD,
5965
dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE,
6066
)
@@ -73,6 +79,7 @@ def setupdb():
7379
database=POSTGRES_BASE_DB,
7480
user=POSTGRES_USER,
7581
host=POSTGRES_HOST,
82+
port=POSTGRES_PORT,
7683
password=POSTGRES_PASSWORD,
7784
)
7885
db_conn = LoggingDatabaseConnection(db_conn, db_engine, "tests")
@@ -83,6 +90,7 @@ def _cleanup():
8390
db_conn = db_engine.module.connect(
8491
user=POSTGRES_USER,
8592
host=POSTGRES_HOST,
93+
port=POSTGRES_PORT,
8694
password=POSTGRES_PASSWORD,
8795
dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE,
8896
)

0 commit comments

Comments
 (0)