2026-10-24 03:10:28 +02:00
|
|
|
"""Reproducible route-vs-locust coverage check. Fails CI on gaps.
|
|
|
|
|
|
|
|
|
|
Usage: python3 example/check_locust_coverage.py
|
|
|
|
|
Exit 0 = full coverage. Exit 1 = missing routes printed.
|
|
|
|
|
"""
|
|
|
|
|
import re
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
|
|
|
APP = HERE / "app"
|
|
|
|
|
LOCUST = HERE / "locustfile.py"
|
|
|
|
|
|
2026-10-24 04:05:41 +02:00
|
|
|
# Routes that call paid Google APIs: never load tested. The check fails if
|
|
|
|
|
# locust references one, and does not require them to be covered.
|
|
|
|
|
EXCLUDED = {"/voice/say", "/voice/hear", "/voice/turn", "/voice/reset"}
|
2026-10-24 04:12:52 +02:00
|
|
|
# Whole areas that talk to (or front) paid Google APIs. Routes under these
|
|
|
|
|
# prefixes are never load tested and never required to be covered.
|
2026-10-24 06:06:06 +02:00
|
|
|
EXCLUDED_PREFIXES = ("/voice", "/google", "/agent", "/ai", "/embedded/voice.js",
|
|
|
|
|
"/embedded/agent_", "/embedded/ai_")
|
2026-10-24 04:12:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_excluded(route: str) -> bool:
|
|
|
|
|
return route in EXCLUDED or route.startswith(EXCLUDED_PREFIXES)
|
2026-10-24 04:05:41 +02:00
|
|
|
|
2026-10-24 03:10:28 +02:00
|
|
|
FILLS = {"name": "locust", "id": "1", "status": "404", "tenant": "acme"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def fill(route: str) -> str:
|
|
|
|
|
out = route
|
|
|
|
|
for k, v in FILLS.items():
|
|
|
|
|
out = out.replace("{" + k + "}", v)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def collect_routes() -> set[str]:
|
|
|
|
|
routes: set[str] = set()
|
|
|
|
|
# 1. Index features (authoritative user-facing list)
|
|
|
|
|
out = subprocess.run(
|
|
|
|
|
["grep", "-rhoE", r'add_feature\("[^"]+", "/[^"]+"\)', str(APP)],
|
|
|
|
|
capture_output=True, text=True, check=True,
|
|
|
|
|
).stdout
|
|
|
|
|
for m in re.findall(r'"/[^"]+"\)', out):
|
|
|
|
|
routes.add(m.strip('")'))
|
|
|
|
|
# 2. Direct router registrations
|
|
|
|
|
out = subprocess.run(
|
|
|
|
|
["grep", "-rhoE", r'\.(get|post|put|delete|patch|options|head)\("/[^"]+"', str(APP)],
|
|
|
|
|
capture_output=True, text=True, check=True,
|
|
|
|
|
).stdout
|
|
|
|
|
for m in re.findall(r'"/[^"]+"', out):
|
|
|
|
|
routes.add(m.strip('"'))
|
|
|
|
|
# 3. Prefix-mounted equivalents (see RoutingDemo.cpp / App.cpp)
|
|
|
|
|
routes |= {"/views/greet", "/routers/nested/deep",
|
|
|
|
|
"/app1/app1/app1/whoami"}
|
|
|
|
|
return routes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def collect_locust_urls() -> tuple[set[str], str]:
|
|
|
|
|
src = LOCUST.read_text()
|
|
|
|
|
urls = set(re.findall(r'"(/[^\"]*)"', src))
|
|
|
|
|
methods = set(re.findall(r"self\.client\.(get|post|put|options|delete|patch|head)\(", src))
|
|
|
|
|
return urls, methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> int:
|
|
|
|
|
routes = collect_routes()
|
|
|
|
|
urls, methods = collect_locust_urls()
|
|
|
|
|
missing: list[str] = []
|
2026-10-24 04:12:52 +02:00
|
|
|
for r in sorted(r for r in routes if not is_excluded(r)):
|
2026-10-24 03:10:28 +02:00
|
|
|
c = fill(r)
|
|
|
|
|
ok = (
|
|
|
|
|
c in urls
|
|
|
|
|
or "/routing" + c in urls
|
|
|
|
|
or (r == "/static/{name}" and "/static/app.css" in urls)
|
|
|
|
|
or (r == "/embedded/{name}" and "/embedded/test.txt" in urls)
|
|
|
|
|
or (r == "/di/resolve/{name}" and "/di/resolve/testsvc" in urls)
|
|
|
|
|
or (r == "/views/greet" and ("/greet" in urls or "/routing/views/greet" in urls))
|
|
|
|
|
or (r == "/routers/nested/deep" and ("/deep" in urls or "/routing/routers/nested/deep" in urls))
|
|
|
|
|
)
|
|
|
|
|
if not ok:
|
|
|
|
|
missing.append(f"{r} -> {c}")
|
2026-10-24 04:12:52 +02:00
|
|
|
leaked = sorted(u for u in urls if is_excluded(u))
|
2026-10-24 04:05:41 +02:00
|
|
|
for r in leaked:
|
|
|
|
|
print("FORBIDDEN (costs money):", r)
|
2026-10-24 03:10:28 +02:00
|
|
|
required_methods = {"get", "post", "put", "options"}
|
|
|
|
|
missing_methods = required_methods - methods
|
|
|
|
|
print(f"routes={len(routes)} urls={len(urls)} methods={sorted(methods)}")
|
|
|
|
|
if missing_methods:
|
|
|
|
|
print("MISSING METHODS:", sorted(missing_methods))
|
|
|
|
|
for m in missing:
|
|
|
|
|
print("MISS", m)
|
2026-10-24 04:05:41 +02:00
|
|
|
if leaked:
|
|
|
|
|
print(f"FAIL: {len(leaked)} paid Google routes in locust")
|
|
|
|
|
return 1
|
2026-10-24 03:10:28 +02:00
|
|
|
if missing or missing_methods:
|
|
|
|
|
print(f"FAIL: {len(missing)} missing routes, {len(missing_methods)} missing methods")
|
|
|
|
|
return 1
|
2026-10-24 04:12:52 +02:00
|
|
|
excluded = sum(1 for r in routes if is_excluded(r))
|
|
|
|
|
print(f"OK: {len(routes) - excluded} routes covered, {excluded} paid Google routes excluded, methods {sorted(methods)}")
|
2026-10-24 03:10:28 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
sys.exit(main())
|