|
12 | 12 | import subprocess
|
13 | 13 | import time
|
14 | 14 | from typing import Dict, List, Optional, Union
|
| 15 | +from urllib.parse import urlparse |
15 | 16 |
|
16 | 17 |
|
17 | 18 | class BaseDb(object):
|
@@ -241,3 +242,48 @@ def stop(self):
|
241 | 242 | self.proc.wait()
|
242 | 243 | shutil.rmtree(self.pgdir)
|
243 | 244 | drop_unused_port(self.port)
|
| 245 | + |
| 246 | + |
| 247 | +class SystemPostgresDbProvider(PostgresDbProvider): |
| 248 | + """A DB provider that uses an externally controlled postgres instance. |
| 249 | +
|
| 250 | + Spinning postgres instances up and down is costly. We are keeping |
| 251 | + tests separate by assigning them random names, so we can share a |
| 252 | + single DB cluster. This provider does just that: it talks to an |
| 253 | + externally managed cluster, creates and deletes DBs on demand, but |
| 254 | + does not manage the cluster's lifecycle. |
| 255 | +
|
| 256 | + The external cluster to talk to can be specified via the |
| 257 | + `CLN_TEST_POSTGRES_DSN` environment variable. |
| 258 | +
|
| 259 | + Please make sure that the user specified in the DSN has the |
| 260 | + permission to create new DBs. |
| 261 | +
|
| 262 | + Since tests, may end up interrupted, and may not clean up the |
| 263 | + databases they created, be aware that over time your cluster may |
| 264 | + accumulate quite a few databases. This mode is mostly intended for |
| 265 | + CI where a throwaway postgre cluster can be spun up and tested |
| 266 | + against. |
| 267 | +
|
| 268 | + """ |
| 269 | + |
| 270 | + def __init__(self, directory): |
| 271 | + self.dsn = os.environ.get("CLN_TEST_POSTGRES_DSN") |
| 272 | + self.conn = None |
| 273 | + parts = urlparse(self.dsn) |
| 274 | + |
| 275 | + self.hostname = parts.hostname |
| 276 | + self.username = parts.username |
| 277 | + self.port = parts.port |
| 278 | + self.dbname = parts.path |
| 279 | + |
| 280 | + def stop(self): |
| 281 | + pass |
| 282 | + |
| 283 | + def start(self): |
| 284 | + self.conn = psycopg2.connect(self.dsn) |
| 285 | + cur = self.conn.cursor() |
| 286 | + cur.execute("SELECT 1") |
| 287 | + cur.close() |
| 288 | + # Required for CREATE DATABASE to work |
| 289 | + self.conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) |
0 commit comments