test.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. """Start GUI, hit /cmd_list_army and /cmd_list_nodes, validate anchors."""
  3. import sys, subprocess, time, socket, signal, os
  4. err = []
  5. proc = subprocess.Popen(
  6. [sys.executable, "ufonet", "--gui"],
  7. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
  8. preexec_fn=os.setsid,
  9. )
  10. def http_get(path, port=9999, timeout=5):
  11. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12. s.settimeout(timeout)
  13. s.connect(("127.0.0.1", port))
  14. s.sendall(("GET " + path + " HTTP/1.0\r\n\r\n").encode())
  15. chunks = []
  16. while True:
  17. try:
  18. data = s.recv(65536)
  19. if not data: break
  20. chunks.append(data)
  21. except socket.timeout:
  22. break
  23. s.close()
  24. return b"".join(chunks).decode("utf-8", errors="replace")
  25. try:
  26. ready = False
  27. for _ in range(30):
  28. try:
  29. s = socket.socket()
  30. s.settimeout(0.5)
  31. s.connect(("127.0.0.1", 9999))
  32. s.close()
  33. ready = True
  34. break
  35. except Exception:
  36. time.sleep(0.5)
  37. if not ready:
  38. err.append("GUI didn't open port 9999 within ~15s")
  39. else:
  40. army = http_get("/cmd_list_army")
  41. nodes = http_get("/cmd_list_nodes")
  42. sections = ["UCAVs", "Aliens", "Droids", "Zombies", "XML-RPCs", "NTPs", "DNSs", "SNMPs"]
  43. for s in sections:
  44. if f"<u>{s}:</u>" not in army:
  45. err.append(f"army: section {s} missing")
  46. if army.count("<a href=") < len(sections):
  47. err.append(f"army: too few anchors ({army.count('<a href=')})")
  48. if "Total Nodes" not in nodes:
  49. err.append("nodes: 'Total Nodes' header missing")
  50. finally:
  51. try:
  52. os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
  53. except Exception:
  54. pass
  55. proc.wait(timeout=5)
  56. print(f"army_anchors={army.count('<a href=') if 'army' in dir() else 'n/a'}")
  57. print(f"nodes_anchors={nodes.count('<a href=') if 'nodes' in dir() else 'n/a'}")
  58. for e in err:
  59. print("FAIL:", e)
  60. sys.exit(0 if not err else 1)