platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
233
tests/transport_singbox_e2e.py
Executable file
233
tests/transport_singbox_e2e.py
Executable file
@@ -0,0 +1,233 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from typing import Dict, Optional, Tuple
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
def fail(msg: str) -> int:
|
||||
print(f"[transport_singbox_e2e] ERROR: {msg}")
|
||||
return 1
|
||||
|
||||
|
||||
def request_json(api_url: str, method: str, path: str, payload: Optional[Dict] = None) -> Tuple[int, Dict]:
|
||||
data = None
|
||||
headers = {"Accept": "application/json"}
|
||||
if payload is not None:
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(
|
||||
f"{api_url.rstrip('/')}{path}",
|
||||
data=data,
|
||||
method=method.upper(),
|
||||
headers=headers,
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=20.0) as resp:
|
||||
raw = resp.read().decode("utf-8", errors="replace")
|
||||
status = int(resp.getcode() or 200)
|
||||
except urllib.error.HTTPError as e:
|
||||
raw = e.read().decode("utf-8", errors="replace")
|
||||
status = int(e.code or 500)
|
||||
except Exception:
|
||||
return 0, {}
|
||||
|
||||
try:
|
||||
data_json = json.loads(raw) if raw else {}
|
||||
except Exception:
|
||||
data_json = {}
|
||||
if not isinstance(data_json, dict):
|
||||
data_json = {}
|
||||
return status, data_json
|
||||
|
||||
|
||||
def ensure_client_deleted(api_url: str, client_id: str) -> None:
|
||||
request_json(api_url, "DELETE", f"/api/v1/transport/clients/{client_id}?force=true")
|
||||
|
||||
|
||||
def create_client(api_url: str, payload: Dict) -> Tuple[int, Dict]:
|
||||
return request_json(api_url, "POST", "/api/v1/transport/clients", payload)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
api_url = os.environ.get("API_URL", "http://127.0.0.1:8080").strip()
|
||||
if not api_url:
|
||||
return fail("empty API_URL")
|
||||
|
||||
print(f"[transport_singbox_e2e] API_URL={api_url}")
|
||||
|
||||
status, caps = request_json(api_url, "GET", "/api/v1/transport/capabilities")
|
||||
if status == 404:
|
||||
print("[transport_singbox_e2e] SKIP: transport endpoints are not available on this backend")
|
||||
return 0
|
||||
if status != 200 or not bool(caps.get("ok", False)):
|
||||
return fail(f"capabilities failed status={status} payload={caps}")
|
||||
|
||||
clients_caps = caps.get("clients") or {}
|
||||
if not isinstance(clients_caps, dict) or "singbox" not in clients_caps:
|
||||
return fail(f"singbox capability is missing: {caps}")
|
||||
runtime_modes = caps.get("runtime_modes") or {}
|
||||
if isinstance(runtime_modes, dict) and runtime_modes:
|
||||
if not bool(runtime_modes.get("exec", False)):
|
||||
return fail(f"runtime_modes.exec is not supported: {caps}")
|
||||
else:
|
||||
print("[transport_singbox_e2e] WARN: runtime_modes are not advertised by current backend build")
|
||||
|
||||
ts = int(time.time())
|
||||
pid = os.getpid()
|
||||
|
||||
# Case 1: successful lifecycle on mock runner.
|
||||
client_ok = f"e2e-singbox-ok-{ts}-{pid}"
|
||||
ensure_client_deleted(api_url, client_ok)
|
||||
status, create_ok = create_client(
|
||||
api_url,
|
||||
{
|
||||
"id": client_ok,
|
||||
"name": "E2E Singbox Mock",
|
||||
"kind": "singbox",
|
||||
"enabled": False,
|
||||
"config": {
|
||||
"runner": "mock",
|
||||
"runtime_mode": "exec",
|
||||
"packaging_profile": "bundled",
|
||||
"bin_root": "/opt/selective-vpn/bin",
|
||||
"require_binary": False,
|
||||
"singbox_config_path": "/etc/singbox/e2e.json",
|
||||
},
|
||||
},
|
||||
)
|
||||
if status != 200 or not bool(create_ok.get("ok", False)):
|
||||
return fail(f"create mock singbox failed status={status} payload={create_ok}")
|
||||
try:
|
||||
status, provision = request_json(api_url, "POST", f"/api/v1/transport/clients/{client_ok}/provision")
|
||||
if status == 404:
|
||||
print("[transport_singbox_e2e] SKIP: provision endpoint is not available on current backend build")
|
||||
return 0
|
||||
if status != 200 or not bool(provision.get("ok", False)):
|
||||
return fail(f"provision mock singbox failed status={status} payload={provision}")
|
||||
|
||||
status, start = request_json(api_url, "POST", f"/api/v1/transport/clients/{client_ok}/start")
|
||||
if status != 200 or not bool(start.get("ok", False)):
|
||||
return fail(f"start mock singbox failed status={status} payload={start}")
|
||||
if str(start.get("status_after") or "").strip().lower() != "up":
|
||||
return fail(f"start did not set status_after=up: {start}")
|
||||
|
||||
status, health = request_json(api_url, "GET", f"/api/v1/transport/clients/{client_ok}/health")
|
||||
if status != 200 or not bool(health.get("ok", False)):
|
||||
return fail(f"health mock singbox failed status={status} payload={health}")
|
||||
if str(health.get("status") or "").strip().lower() not in ("up", "degraded"):
|
||||
return fail(f"unexpected health status after start: {health}")
|
||||
|
||||
status, restart = request_json(api_url, "POST", f"/api/v1/transport/clients/{client_ok}/restart")
|
||||
if status != 200 or not bool(restart.get("ok", False)):
|
||||
return fail(f"restart mock singbox failed status={status} payload={restart}")
|
||||
|
||||
status, stop = request_json(api_url, "POST", f"/api/v1/transport/clients/{client_ok}/stop")
|
||||
if status != 200 or not bool(stop.get("ok", False)):
|
||||
return fail(f"stop mock singbox failed status={status} payload={stop}")
|
||||
if str(stop.get("status_after") or "").strip().lower() != "down":
|
||||
return fail(f"stop did not set status_after=down: {stop}")
|
||||
|
||||
status, metrics = request_json(api_url, "GET", f"/api/v1/transport/clients/{client_ok}/metrics")
|
||||
if status == 404:
|
||||
print("[transport_singbox_e2e] WARN: metrics endpoint is not available on current backend build")
|
||||
else:
|
||||
if status != 200 or not bool(metrics.get("ok", False)):
|
||||
return fail(f"metrics mock singbox failed status={status} payload={metrics}")
|
||||
metrics_obj = metrics.get("metrics") or {}
|
||||
if not isinstance(metrics_obj, dict):
|
||||
return fail(f"metrics payload is invalid: {metrics}")
|
||||
if int(metrics_obj.get("state_changes", 0) or 0) < 2:
|
||||
return fail(f"state_changes must be >=2 after lifecycle sequence: {metrics}")
|
||||
print("[transport_singbox_e2e] case1 mock lifecycle: ok")
|
||||
finally:
|
||||
ensure_client_deleted(api_url, client_ok)
|
||||
|
||||
# Case 2: embedded runtime mode must be rejected.
|
||||
client_emb = f"e2e-singbox-embedded-{ts}-{pid}"
|
||||
ensure_client_deleted(api_url, client_emb)
|
||||
status, create_emb = create_client(
|
||||
api_url,
|
||||
{
|
||||
"id": client_emb,
|
||||
"name": "E2E Singbox Embedded",
|
||||
"kind": "singbox",
|
||||
"enabled": False,
|
||||
"config": {
|
||||
"runner": "mock",
|
||||
"runtime_mode": "embedded",
|
||||
},
|
||||
},
|
||||
)
|
||||
if status != 200 or not bool(create_emb.get("ok", False)):
|
||||
return fail(f"create embedded singbox failed status={status} payload={create_emb}")
|
||||
try:
|
||||
status, provision_emb = request_json(api_url, "POST", f"/api/v1/transport/clients/{client_emb}/provision")
|
||||
if status != 200 or bool(provision_emb.get("ok", True)):
|
||||
return fail(f"embedded provision must fail status={status} payload={provision_emb}")
|
||||
if str(provision_emb.get("code") or "").strip() != "TRANSPORT_BACKEND_RUNTIME_MODE_UNSUPPORTED":
|
||||
return fail(f"embedded provision wrong code: {provision_emb}")
|
||||
|
||||
status, start_emb = request_json(api_url, "POST", f"/api/v1/transport/clients/{client_emb}/start")
|
||||
if status != 200 or bool(start_emb.get("ok", True)):
|
||||
return fail(f"embedded start must fail status={status} payload={start_emb}")
|
||||
if str(start_emb.get("code") or "").strip() != "TRANSPORT_BACKEND_RUNTIME_MODE_UNSUPPORTED":
|
||||
return fail(f"embedded start wrong code: {start_emb}")
|
||||
|
||||
status, health_emb = request_json(api_url, "GET", f"/api/v1/transport/clients/{client_emb}/health")
|
||||
if status != 200 or not bool(health_emb.get("ok", False)):
|
||||
return fail(f"embedded health request failed status={status} payload={health_emb}")
|
||||
if str(health_emb.get("code") or "").strip() != "TRANSPORT_BACKEND_RUNTIME_MODE_UNSUPPORTED":
|
||||
return fail(f"embedded health wrong code: {health_emb}")
|
||||
print("[transport_singbox_e2e] case2 runtime_mode=embedded guard: ok")
|
||||
finally:
|
||||
ensure_client_deleted(api_url, client_emb)
|
||||
|
||||
# Case 3: require_binary fail-fast for missing singbox binary.
|
||||
client_req = f"e2e-singbox-requirebin-{ts}-{pid}"
|
||||
ensure_client_deleted(api_url, client_req)
|
||||
status, create_req = create_client(
|
||||
api_url,
|
||||
{
|
||||
"id": client_req,
|
||||
"name": "E2E Singbox RequireBinary",
|
||||
"kind": "singbox",
|
||||
"enabled": False,
|
||||
"config": {
|
||||
"runner": "systemd",
|
||||
"runtime_mode": "exec",
|
||||
"unit": f"{client_req}.service",
|
||||
"packaging_profile": "bundled",
|
||||
"bin_root": "/opt/selective-vpn/bin",
|
||||
"require_binary": True,
|
||||
"singbox_bin": "/tmp/definitely-missing-sing-box-binary",
|
||||
"singbox_config_path": "/etc/singbox/e2e.json",
|
||||
},
|
||||
},
|
||||
)
|
||||
if status != 200 or not bool(create_req.get("ok", False)):
|
||||
return fail(f"create require_binary singbox failed status={status} payload={create_req}")
|
||||
try:
|
||||
status, provision_req = request_json(api_url, "POST", f"/api/v1/transport/clients/{client_req}/provision")
|
||||
if status != 200 or bool(provision_req.get("ok", True)):
|
||||
return fail(f"require_binary provision must fail status={status} payload={provision_req}")
|
||||
if str(provision_req.get("code") or "").strip() != "TRANSPORT_BACKEND_PROVISION_CONFIG_REQUIRED":
|
||||
return fail(f"require_binary provision wrong code: {provision_req}")
|
||||
msg = str(provision_req.get("message") or "").strip().lower()
|
||||
if "required singbox binary not found" not in msg:
|
||||
return fail(f"require_binary provision wrong message: {provision_req}")
|
||||
print("[transport_singbox_e2e] case3 require_binary fail-fast: ok")
|
||||
finally:
|
||||
ensure_client_deleted(api_url, client_req)
|
||||
|
||||
print("[transport_singbox_e2e] passed")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user