Files
moloapp/example/check_locust_coverage.py
T

106 lines
3.8 KiB
Python
Raw Normal View History

"""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"
# 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"}
# Whole areas that talk to (or front) paid Google APIs. Routes under these
# prefixes are never load tested and never required to be covered.
EXCLUDED_PREFIXES = ("/voice", "/google", "/agent", "/ai", "/embedded/voice.js",
"/embedded/agent_", "/embedded/ai_")
def is_excluded(route: str) -> bool:
return route in EXCLUDED or route.startswith(EXCLUDED_PREFIXES)
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] = []
for r in sorted(r for r in routes if not is_excluded(r)):
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}")
leaked = sorted(u for u in urls if is_excluded(u))
for r in leaked:
print("FORBIDDEN (costs money):", r)
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)
if leaked:
print(f"FAIL: {len(leaked)} paid Google routes in locust")
return 1
if missing or missing_methods:
print(f"FAIL: {len(missing)} missing routes, {len(missing_methods)} missing methods")
return 1
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)}")
return 0
if __name__ == "__main__":
sys.exit(main())