forked from kfzteile24/postgresql-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proxy.py
More file actions
334 lines (296 loc) · 10.3 KB
/
Copy pathtest_proxy.py
File metadata and controls
334 lines (296 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import contextlib
import os
import shutil
import socket
import ssl
import subprocess
import tempfile
import threading
import time
import psycopg2
import pytest
from postgresql_proxy import config_schema as cfg
from postgresql_proxy.proxy import Proxy
def _get_free_tcp_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("127.0.0.1", 0))
return sock.getsockname()[1]
def _wait_for_listen_port(host: str, port: int, timeout: float = 5.0) -> None:
deadline = time.time() + timeout
while time.time() < deadline:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(0.2)
if sock.connect_ex((host, port)) == 0:
return
time.sleep(0.05)
raise TimeoutError(f"Proxy did not start listening on {host}:{port} in {timeout}s")
def _build_dump_like_sql(table_count: int = 12, rows_per_table: int = 100) -> str:
chunks = ["BEGIN;"]
for table_idx in range(table_count):
table_name = f"e2e_batch_{table_idx}"
chunks.append(f"DROP TABLE IF EXISTS {table_name};")
chunks.append(f"CREATE TABLE {table_name} (id INTEGER, payload TEXT);")
chunks.append(f"COPY {table_name} (id, payload) FROM STDIN;")
for row_idx in range(rows_per_table):
chunks.append(f"{row_idx}\trow_{table_idx}_{row_idx}")
chunks.append("\\.")
chunks.append(f"SELECT COUNT(*) FROM {table_name};")
chunks.append("SELECT 'BATCH_OK';")
chunks.append("COMMIT;")
return "\n".join(chunks) + "\n"
def _run_psql_file(
postgres_settings, port: int, sql_file_path: str, timeout_sec: int = 60
):
cmd = [
"psql",
"-X",
"-q",
"-tA",
"-v",
"ON_ERROR_STOP=1",
"-h",
"127.0.0.1",
"-p",
str(port),
"-U",
postgres_settings["user"],
"-d",
postgres_settings["dbname"],
"-f",
sql_file_path,
]
env = {
**os.environ,
"PGPASSWORD": postgres_settings["password"],
"PGSSLMODE": "require",
}
return subprocess.run(
cmd,
env=env,
capture_output=True,
text=True,
timeout=timeout_sec,
check=False,
)
@contextlib.contextmanager
def _temporary_server_cert_pair():
if shutil.which("openssl") is None:
pytest.fail("openssl is required for SSL E2E tests but was not found in PATH")
with tempfile.TemporaryDirectory(prefix="proxy-e2e-cert-") as tmp_dir:
cert_path = os.path.join(tmp_dir, "server.crt")
key_path = os.path.join(tmp_dir, "server.key")
result = subprocess.run(
[
"openssl",
"req",
"-x509",
"-newkey",
"rsa:2048",
"-sha256",
"-days",
"1",
"-nodes",
"-subj",
"/CN=localhost",
"-keyout",
key_path,
"-out",
cert_path,
],
capture_output=True,
text=True,
check=False,
)
if result.returncode != 0:
err_tail = "\n".join((result.stderr or "").splitlines()[-20:])
pytest.fail(
f"Failed to generate temporary TLS cert/key for E2E tests (rc={result.returncode}): {err_tail}"
)
yield cert_path, key_path
@contextlib.contextmanager
def _run_proxy(postgres_settings, ssl_context: ssl.SSLContext | None = None):
proxy_port = _get_free_tcp_port()
instance = cfg.InstanceSettings(
{
"listen": {"name": "proxy", "host": "127.0.0.1", "port": proxy_port},
"redirect": {
"name": "postgres",
"host": postgres_settings["host"],
"port": postgres_settings["port"],
},
# Keep interceptors active with default no-op behavior.
"intercept": {"commands": {}, "responses": {}},
}
)
if not hasattr(instance.intercept.responses, "parameter_status"):
instance.intercept.responses.parameter_status = []
proxy = Proxy(instance, plugins={}, debug=True, ssl_context=ssl_context)
thread = threading.Thread(
target=proxy.listen, kwargs={"max_connections": 32}, daemon=True
)
thread.start()
_wait_for_listen_port("127.0.0.1", proxy_port)
try:
yield proxy_port
finally:
proxy.stop()
# Wake selector.select(timeout=1) so shutdown is immediate.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as wake_sock:
wake_sock.settimeout(0.2)
wake_sock.connect_ex(("127.0.0.1", proxy_port))
thread.join(timeout=4)
assert not thread.is_alive(), "Proxy thread did not stop cleanly"
@pytest.fixture()
def plain_proxy_port(postgres_settings):
with _run_proxy(postgres_settings) as proxy_port:
yield proxy_port
@pytest.fixture()
def ssl_proxy_port(postgres_settings):
with _temporary_server_cert_pair() as (cert_path, key_path):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile=cert_path, keyfile=key_path)
with _run_proxy(postgres_settings, ssl_context=ssl_context) as proxy_port:
yield proxy_port
@pytest.mark.timeout(20)
def test_connect_query_without_ssl(postgres_settings, plain_proxy_port):
with psycopg2.connect(
host="127.0.0.1",
port=plain_proxy_port,
user=postgres_settings["user"],
password=postgres_settings["password"],
dbname=postgres_settings["dbname"],
sslmode="disable",
connect_timeout=3,
) as conn:
with conn.cursor() as cur:
cur.execute("SELECT 1")
assert cur.fetchone() == (1,)
@pytest.mark.timeout(20)
def test_connect_query_with_ssl(postgres_settings, ssl_proxy_port):
with psycopg2.connect(
host="127.0.0.1",
port=ssl_proxy_port,
user=postgres_settings["user"],
password=postgres_settings["password"],
dbname=postgres_settings["dbname"],
sslmode="require",
connect_timeout=3,
) as conn:
with conn.cursor() as cur:
cur.execute("SELECT 1")
assert cur.fetchone() == (1,)
@pytest.mark.timeout(60)
def test_repeated_connect_query_smoke_no_hang(postgres_settings, plain_proxy_port):
for i in range(20):
with psycopg2.connect(
host="127.0.0.1",
port=plain_proxy_port,
user=postgres_settings["user"],
password=postgres_settings["password"],
dbname=postgres_settings["dbname"],
sslmode="disable",
connect_timeout=3,
) as conn:
with conn.cursor() as cur:
cur.execute("SELECT %s", (i,))
assert cur.fetchone() == (i,)
@pytest.mark.timeout(60)
@pytest.mark.parametrize("sslmode", ["disable", "require"])
@pytest.mark.parametrize(
["sql", "expected"],
[
pytest.param(
"SELECT 1",
[(1,)],
id="tiny-1B",
),
pytest.param(
"SELECT repeat('x', 1024)",
[("x" * 1024,)],
id="small-1KB",
),
pytest.param(
"SELECT repeat('x', 102400)",
[("x" * 102400,)],
id="medium-100KB",
),
pytest.param(
"SELECT repeat('x', 1048576)",
[("x" * 1048576,)],
id="large-1MB",
),
pytest.param(
"SELECT repeat('x', 10485760)",
[("x" * 10485760,)],
id="xlarge-10MB",
),
pytest.param(
"SELECT i FROM generate_series(1, 10000) AS t(i)",
[(i,) for i in range(1, 10001)],
id="rows-10k",
),
pytest.param(
"SELECT i FROM generate_series(1, 100000) AS t(i)",
[(i,) for i in range(1, 100001)],
id="rows-100k",
),
]
)
def test_various_payload_sizes(
postgres_settings,
plain_proxy_port,
ssl_proxy_port,
sslmode,
sql,
expected,
):
with psycopg2.connect(
host="127.0.0.1",
port=plain_proxy_port if sslmode == "disable" else ssl_proxy_port,
user=postgres_settings["user"],
password=postgres_settings["password"],
dbname=postgres_settings["dbname"],
sslmode=sslmode,
connect_timeout=3,
) as conn:
with conn.cursor() as cur:
cur.execute(sql)
assert cur.fetchall() == expected
@pytest.mark.timeout(60)
def test_psql_ssl_file_batch_stress_no_hang(postgres_settings, ssl_proxy_port):
if shutil.which("psql") is None:
pytest.fail("psql is required for this test but was not found in PATH")
with tempfile.NamedTemporaryFile("w", suffix=".sql", delete=True) as tmp_file:
sql_content = _build_dump_like_sql(table_count=24, rows_per_table=300)
tmp_file.write(sql_content)
tmp_file.flush()
sql_file_path = tmp_file.name
for run_idx in range(3):
started = time.time()
try:
result = _run_psql_file(
postgres_settings,
port=ssl_proxy_port,
sql_file_path=sql_file_path,
timeout_sec=60,
)
except subprocess.TimeoutExpired as err:
pytest.fail(
"psql -f batch timed out over SSL via proxy "
f"(run={run_idx + 1}, timeout={err.timeout}s)"
)
elapsed = time.time() - started
if result.returncode != 0:
out_tail = "\n".join((result.stdout or "").splitlines()[-20:])
err_tail = "\n".join((result.stderr or "").splitlines()[-20:])
pytest.fail(
"psql -f batch failed over SSL via proxy "
f"(run={run_idx + 1}, rc={result.returncode}, {elapsed=:.2f}s) "
f"stdout_tail={out_tail} stderr_tail={err_tail}"
)
if "BATCH_OK" not in (result.stdout or ""):
out_tail = "\n".join((result.stdout or "").splitlines()[-20:])
pytest.fail(
"psql -f batch succeeded but expected marker missing "
f"(run={run_idx + 1}, {elapsed=:.2f}s) stdout_tail={out_tail}"
)