forked from longtao.wu/droboticflow
73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
import json
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
|
SCRIPT = ROOT / "scripts" / "verify_dcloud_operator_contracts.py"
|
|
|
|
|
|
def write_text(path: pathlib.Path, text: str) -> None:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(text, encoding="utf-8")
|
|
|
|
|
|
def write_source_root(tmp_path: pathlib.Path, *, include_generation_tasks_route: bool = True) -> pathlib.Path:
|
|
source_root = tmp_path / "d-cloud"
|
|
|
|
annotation = source_root / "robo_annotation_backend"
|
|
write_text(
|
|
annotation / "pegasus/api/auth.py",
|
|
'from fastapi import APIRouter\nrouter = APIRouter()\n@router.get("/me")\nasync def me():\n pass\n',
|
|
)
|
|
write_text(
|
|
annotation / "pegasus/deps.py",
|
|
'Header(alias="d-user-id")\nHeader(alias="org-symbol")\nHeader(alias="sourceapp")\n',
|
|
)
|
|
write_text(annotation / "docs/apis.md", "GET | `/me` | current user\n")
|
|
|
|
generation = source_root / "data_generation"
|
|
write_text(generation / "initialize/server.go", 'apiGroup := r.Group("/generationApi")\n')
|
|
tasks_route = 'imgGenerationGroup.POST("/tasks", h.ListTasks)\n' if include_generation_tasks_route else ""
|
|
write_text(generation / "api/img_generation.go", 'imgGenerationGroup := apiGroup.Group("/imgGeneration")\n' + tasks_route)
|
|
write_text(generation / "common/middleware/request.go", 'c.GetHeader("d-user-id")\nc.GetHeader("sourceapp")\n')
|
|
|
|
return source_root
|
|
|
|
|
|
def run_script(source_root: pathlib.Path) -> subprocess.CompletedProcess:
|
|
env = os.environ.copy()
|
|
env["PYTHONUNBUFFERED"] = "1"
|
|
return subprocess.run(
|
|
["python3", str(SCRIPT), "--source-root", str(source_root), "--json"],
|
|
cwd=ROOT,
|
|
env=env,
|
|
text=True,
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
|
|
|
|
def test_dcloud_operator_contracts_pass_with_matching_source_tree(tmp_path):
|
|
source_root = write_source_root(tmp_path)
|
|
|
|
result = run_script(source_root)
|
|
|
|
assert result.returncode == 0, result.stderr + result.stdout
|
|
payload = json.loads(result.stdout)
|
|
assert payload["status"] == "passed"
|
|
assert {item["slug"] for item in payload["contracts"]} == {"dcloud_embodied_annotation", "dcloud_data_generation"}
|
|
|
|
|
|
def test_dcloud_operator_contracts_fail_when_source_route_drifts(tmp_path):
|
|
source_root = write_source_root(tmp_path, include_generation_tasks_route=False)
|
|
|
|
result = run_script(source_root)
|
|
|
|
assert result.returncode == 1
|
|
payload = json.loads(result.stdout)
|
|
generation = next(item for item in payload["contracts"] if item["slug"] == "dcloud_data_generation")
|
|
failed = [item for item in generation["checks"] if item["status"] == "failed"]
|
|
assert any("POST /imgGeneration/tasks" in item["name"] for item in failed)
|