-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparity_check.py
More file actions
executable file
·189 lines (158 loc) · 5.65 KB
/
Copy pathparity_check.py
File metadata and controls
executable file
·189 lines (158 loc) · 5.65 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
#!/usr/bin/env python3
"""Cross-language parity check: Python CLI vs Rust binary.
Runs both implementations against the SAME shared mock server (correct
contract, then broken contract) and asserts their JSON reports are identical.
This locks the "provably behaviorally equivalent" claim into CI: any drift in
config parsing, checks, or reporting between schemalock/ and rust/ fails here.
Usage:
python3 scripts/parity_check.py --rust-binary rust/target/debug/schemalock
"""
from __future__ import annotations
import argparse
import difflib
import json
import os
import socket
import subprocess
import sys
import tempfile
import time
import urllib.request
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
CONFIG = REPO_ROOT / "examples" / "escrow_api.yaml"
BASE_URL = "http://127.0.0.1"
def _wait_for_server(port: int, timeout: float = 20.0) -> None:
url = f"{BASE_URL}:{port}/health"
deadline = time.time() + timeout
while time.time() < deadline:
try:
with urllib.request.urlopen(url, timeout=1) as resp:
if resp.status == 200:
return
except OSError:
time.sleep(0.3)
raise RuntimeError(f"mock server on port {port} did not become healthy")
def _free_port() -> int:
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
def _run(cmd, **kwargs) -> subprocess.CompletedProcess:
return subprocess.run(cmd, capture_output=True, text=True, check=False, **kwargs)
def _run_python_cli(
port: int, report_path: Path, extra_args: list[str]
) -> subprocess.CompletedProcess:
return _run(
[
sys.executable,
"-m",
"schemalock.cli",
"test",
"--config",
str(CONFIG),
"--base-url",
f"{BASE_URL}:{port}",
"--json-report",
str(report_path),
*extra_args,
],
cwd=REPO_ROOT,
)
def _run_rust_cli(
binary: Path, port: int, report_path: Path, extra_args: list[str]
) -> subprocess.CompletedProcess:
return _run(
[
str(binary),
"test",
"--config",
str(CONFIG),
"--base-url",
f"{BASE_URL}:{port}",
"--json-report",
str(report_path),
*extra_args,
],
cwd=REPO_ROOT,
)
def _compare(
label: str,
py_report: Path,
rust_report: Path,
py_proc: subprocess.CompletedProcess,
rust_proc: subprocess.CompletedProcess,
) -> bool:
errors = []
if py_proc.returncode != rust_proc.returncode:
errors.append(
f"exit codes differ: python={py_proc.returncode}, rust={rust_proc.returncode}"
)
py_json = json.loads(py_report.read_text())
rust_json = json.loads(rust_report.read_text())
if py_json != rust_json:
errors.append("JSON reports are not identical")
a = json.dumps(py_json, indent=2, sort_keys=True).splitlines()
b = json.dumps(rust_json, indent=2, sort_keys=True).splitlines()
errors.append("\n".join(difflib.unified_diff(a, b, "python", "rust", lineterm="")))
if errors:
print(f"[FAIL] parity mismatch ({label}):", file=sys.stderr)
for error in errors:
print(error, file=sys.stderr)
return False
print(f"[ok] parity matches ({label})")
return True
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--rust-binary", required=True)
args = parser.parse_args()
binary = Path(args.rust_binary)
if not binary.is_file():
print(f"rust binary not found: {binary}", file=sys.stderr)
return 2
ok = True
with tempfile.TemporaryDirectory() as tmp:
for label, env_extra, extra_args in (
("correct contract", None, []),
("broken contract", {"MOCK_BREAK_CONTRACT": "1"}, []),
# --max-response-bytes parity (issue #14): a cap smaller than any
# mock body must produce identical oversized-response errors, and
# a generous cap must match the uncapped baseline exactly.
("bounded oversized", None, ["--max-response-bytes", "10"]),
("bounded within cap", None, ["--max-response-bytes", "10485760"]),
):
port = _free_port()
server_env = dict(os.environ)
if env_extra:
server_env.update(env_extra)
server = subprocess.Popen(
[
sys.executable,
"-m",
"uvicorn",
"examples.mock_server:app",
"--port",
str(port),
],
cwd=REPO_ROOT,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env=server_env,
)
try:
_wait_for_server(port)
slug = label.replace(" ", "_").replace(":", "_")
py_report = Path(tmp) / f"python_{slug}.json"
rust_report = Path(tmp) / f"rust_{slug}.json"
py_proc = _run_python_cli(port, py_report, extra_args)
rust_proc = _run_rust_cli(binary, port, rust_report, extra_args)
ok = _compare(label, py_report, rust_report, py_proc, rust_proc) and ok
finally:
server.terminate()
try:
server.wait(timeout=5)
except subprocess.TimeoutExpired:
server.kill()
server.wait(timeout=5)
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main())