From f063893efa75dd58a9ef125bbbc4e64f84721dd4 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Sun, 4 Oct 2026 09:40:07 +0300 Subject: * test/python/server_test_case.py (ServerTestCase): probe SQL_URI credentials, not just the port Mirrors the Rust-side fix in 4a2cf5b on the Python suites: the old skip check connected to 127.0.0.1:3306, so tests ran (and failed) whenever a server was listening but rejected the credentials. database_reachable() now performs the real mysql_native_password handshake and skips only when the database is genuinely unusable; the five DB-mapped test modules use it instead of their local port probes. * test/python/test_{groups,group_by_name,group_strains,species,species_by_name}.py (database_reachable): use the shared probe; drop local copies. --- test/python/server_test_case.py | 96 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'test/python/server_test_case.py') diff --git a/test/python/server_test_case.py b/test/python/server_test_case.py index ec243a2..d4bb330 100644 --- a/test/python/server_test_case.py +++ b/test/python/server_test_case.py @@ -3,11 +3,14 @@ real HTTP (Python can only reach the server this way, unlike the Rust integration tests under tests/, which call the router in-process). """ +import hashlib import os import socket +import struct import subprocess import time import unittest +from urllib.parse import urlparse BINARY = os.environ.get("GENENETWORK4_BINARY", "genenetwork4") HOST = "127.0.0.1" @@ -25,6 +28,99 @@ def wait_for_port(host, port, timeout=10): return False +def database_reachable(): + """True only when the configured MariaDB accepts the SQL_URI + credentials. A server may be listening on the port yet still reject + the login (the common local case), which a bare port check cannot + see — so this performs the real mysql_native_password handshake. + Any protocol surprise is treated as "not reachable" (skip), which is + the safe direction: these tests must not fail where there is simply + no usable database (e.g. the Guix build sandbox).""" + uri = os.environ.get( + "SQL_URI", "mysql://webqtlout:webqtlout@127.0.0.1/db_webqtl" + ) + try: + parsed = urlparse(uri) + sock = socket.create_connection( + (parsed.hostname or "127.0.0.1", parsed.port or 3306), timeout=1 + ) + with sock: + scramble = _read_scramble(_read_packet(sock)) + if scramble is None: + return False + sock.sendall(_login_packet(parsed, scramble)) + # OK starts with 0x00, ERR with 0xff (access denied etc.). + return _read_packet(sock)[4] == 0 + except (OSError, struct.error, IndexError): + return False + + +def _read_packet(sock): + header = _read_exact(sock, 4) + length = header[0] | (header[1] << 8) | (header[2] << 16) + return header + _read_exact(sock, length) + + +def _read_exact(sock, count): + data = b"" + while len(data) < count: + chunk = sock.recv(count - len(data)) + if not chunk: + raise ConnectionError("connection closed mid-packet") + data += chunk + return data + + +def _read_scramble(handshake): + """Extract the 20-byte auth plugin data from the server handshake.""" + pos = 1 # protocol version + pos = handshake.index(b"\0", pos) + 1 # server version string + pos += 4 # connection id + scramble = handshake[pos : pos + 8] + pos += 8 + 1 # auth data part 1, filler + capabilities = struct.unpack_from("