test_control_example.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """control_mock example must default to mock and refuse a real endpoint without the flag."""
  2. from __future__ import annotations
  3. import os
  4. import subprocess
  5. import sys
  6. from pathlib import Path
  7. SDK_ROOT = Path(__file__).resolve().parents[1]
  8. EXAMPLES = SDK_ROOT / "examples"
  9. def _python() -> str:
  10. # Isolated wheel/venv tests must use the interpreter running pytest.
  11. # Do not prefer /data/prod2u/bin/python3: that wrapper can exec a
  12. # different libexec python and fail to open the example script.
  13. return sys.executable
  14. def _env() -> dict[str, str]:
  15. env = os.environ.copy()
  16. if (SDK_ROOT / "hth_sdk").is_dir():
  17. env["PYTHONPATH"] = str(SDK_ROOT) + os.pathsep + env.get("PYTHONPATH", "")
  18. return env
  19. def test_control_mock_runs_without_daemon():
  20. proc = subprocess.run(
  21. [ _python(), str(EXAMPLES / "control_mock.py")],
  22. check=False,
  23. capture_output=True,
  24. text=True,
  25. timeout=20,
  26. env=_env(),
  27. )
  28. assert proc.returncode == 0, proc.stdout + proc.stderr
  29. assert "UNCONFIRMED" in proc.stdout
  30. assert "FULL OPEN" in proc.stdout
  31. def test_control_mock_refuses_real_endpoint_without_flag():
  32. proc = subprocess.run(
  33. [
  34. _python(),
  35. str(EXAMPLES / "control_mock.py"),
  36. "--allow-real-endpoint",
  37. "/run/hth/control.sock",
  38. ],
  39. check=False,
  40. capture_output=True,
  41. text=True,
  42. timeout=10,
  43. env=_env(),
  44. )
  45. assert proc.returncode == 2
  46. assert "refusing real endpoint" in proc.stderr