Skip to content

Commit 20a33de

Browse files
authored
Finish making PG tests use a dynamic server address (#3277)
# Description of Changes Finish the work started in #3227, which switched a hardcoded `127.0.0.1` to a dynamic server address. # API and ABI breaking changes None. # Expected complexity level and risk 1 # Testing - [x] Smoketests still pass - [ ] `Internal Tests` pass under this PR (they don't on master) --------- Co-authored-by: Zeke Foppa <[email protected]>
1 parent 94809dc commit 20a33de

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

smoketests/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,22 @@ def get_server_address(self):
301301
server_config = next((c for c in config['server_configs'] if c['nickname'] == server_name), None)
302302
if server_config is None:
303303
raise Exception(f"Unable to find server in config with nickname {server_name}")
304-
host = server_config['host']
304+
address = server_config['host']
305+
host = address
306+
port = None
307+
if ":" in host:
308+
host, port = host.split(":", 1)
305309
protocol = server_config['protocol']
306310

307-
return dict(host=host, protocol=protocol, token=token)
311+
return dict(address=address, host=host, port=port, protocol=protocol, token=token)
308312

309313
# Make an HTTP call with `method` to `path`.
310314
#
311315
# If the response is 200, return the body.
312316
# Otherwise, throw an `Exception` constructed with two arguments, the response object and the body.
313317
def api_call(self, method, path, body=None, headers={}):
314318
server = self.get_server_address()
315-
host = server["host"]
319+
host = server["address"]
316320
protocol = server["protocol"]
317321
token = server["token"]
318322
conn = None

smoketests/tests/pg_wire.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,6 @@
44
import tomllib
55
import psycopg2
66

7-
8-
def psql(identity: str, sql: str) -> str:
9-
"""Call `psql` and execute the given SQL statement."""
10-
result = subprocess.run(
11-
["psql", "-h", "127.0.0.1", "-p", "5432", "-U", "postgres", "-d", "quickstart", "--quiet", "-c", sql],
12-
encoding="utf8",
13-
env={**os.environ, "PGPASSWORD": identity},
14-
stdout=subprocess.PIPE,
15-
stderr=subprocess.PIPE,
16-
text=True,
17-
)
18-
19-
if result.stderr:
20-
raise Exception(result.stderr.strip())
21-
return result.stdout.strip()
22-
23-
24-
def connect_db(identity: str):
25-
"""Connect to the database using `psycopg2`."""
26-
conn = psycopg2.connect(host="127.0.0.1", port=5432, user="postgres", password=identity, dbname="quickstart")
27-
conn.set_session(autocommit=True) # Disable automic transaction
28-
return conn
29-
30-
317
class SqlFormat(Smoketest):
328
AUTOPUBLISH = False
339
MODULE_CODE = """
@@ -168,9 +144,32 @@ class SqlFormat(Smoketest):
168144
}
169145
"""
170146

147+
def psql(self, identity: str, sql: str) -> str:
148+
"""Call `psql` and execute the given SQL statement."""
149+
server = self.get_server_address()
150+
result = subprocess.run(
151+
["psql", "-h", server["host"], "-p", "5432", "-U", "postgres", "-d", "quickstart", "--quiet", "-c", sql],
152+
encoding="utf8",
153+
env={**os.environ, "PGPASSWORD": identity},
154+
stdout=subprocess.PIPE,
155+
stderr=subprocess.PIPE,
156+
text=True,
157+
)
158+
159+
if result.stderr:
160+
raise Exception(result.stderr.strip())
161+
return result.stdout.strip()
162+
163+
def connect_db(self, identity: str):
164+
"""Connect to the database using `psycopg2`."""
165+
server = self.get_server_address()
166+
conn = psycopg2.connect(host=server["host"], port=5432, user="postgres", password=identity, dbname="quickstart")
167+
conn.set_session(autocommit=True) # Disable automic transaction
168+
return conn
169+
171170
def assertSql(self, token: str, sql: str, expected):
172171
self.maxDiff = None
173-
sql_out = psql(token, sql)
172+
sql_out = self.psql(token, sql)
174173
sql_out = "\n".join([line.rstrip() for line in sql_out.splitlines()])
175174
expected = "\n".join([line.rstrip() for line in expected.splitlines()])
176175
print(sql_out)
@@ -242,7 +241,7 @@ def test_sql_conn(self):
242241
self.publish_module("quickstart", clear=True)
243242
self.call("test")
244243

245-
conn = connect_db(token)
244+
conn = self.connect_db(token)
246245
# Check prepared statements (faked by `psycopg2`)
247246
with conn.cursor() as cur:
248247
cur.execute("select * from t_uints where u8 = %s and u16 = %s", (105, 1050))
@@ -262,20 +261,20 @@ def test_failures(self):
262261
self.publish_module("quickstart", clear=True)
263262

264263
# Empty query
265-
sql_out = psql(token, "")
264+
sql_out = self.psql(token, "")
266265
self.assertEqual(sql_out, "")
267266

268267
# Connection fails with invalid token
269268
with self.assertRaises(Exception) as cm:
270-
psql("invalid_token", "SELECT * FROM t_uints")
269+
self.psql("invalid_token", "SELECT * FROM t_uints")
271270
self.assertIn("Invalid token", str(cm.exception))
272271

273272
# Returns error for unsupported `sql` statements
274273
with self.assertRaises(Exception) as cm:
275-
psql(token, "SELECT CASE a WHEN 1 THEN 'one' ELSE 'other' END FROM t_uints")
274+
self.psql(token, "SELECT CASE a WHEN 1 THEN 'one' ELSE 'other' END FROM t_uints")
276275
self.assertIn("Unsupported", str(cm.exception))
277276

278277
# And prepared statements
279278
with self.assertRaises(Exception) as cm:
280-
psql(token, "SELECT * FROM t_uints where u8 = $1")
279+
self.psql(token, "SELECT * FROM t_uints where u8 = $1")
281280
self.assertIn("Unsupported", str(cm.exception))

smoketests/tests/quickstart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def _test_quickstart(self):
125125
main = main.replace(src, dst)
126126
main += "\n" + self.extra_code
127127
server = self.get_server_address()
128-
host = server["host"]
128+
host = server["address"]
129129
protocol = server["protocol"]
130130
main = main.replace("http://localhost:3000", f"{protocol}://{host}")
131131
_write_file(client_path / self.client_file, main)

0 commit comments

Comments
 (0)