Matrix Docker Ansible eploy
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

138 rader
5.0 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. """A stand-in homeserver for Molecule scenarios.
  5. Most components in this playbook talk to a homeserver while starting up and
  6. exit if it is unreachable, so a scenario cannot get them running without one.
  7. Standing up a real Synapse for every role would dominate the run time and drag
  8. in Postgres, and the scenarios are not testing Synapse - they are testing that
  9. the role's configuration reaches the component and that it starts.
  10. So this answers the handful of endpoints components touch during startup, with
  11. the blandest plausible response in each case. It is deliberately permissive: an
  12. unknown path returns `{}` with a 200 rather than a 404, because the goal is to
  13. get the component past its startup checks, not to model the Matrix spec.
  14. What it is NOT: an authentication check, a room state machine, or anything a
  15. scenario should assert *about*. Assert on what the role rendered and on what the
  16. component reports about itself. If a scenario starts needing this stub to behave
  17. like a real homeserver, that scenario has outgrown what these tests are for.
  18. """
  19. import json
  20. import os
  21. import re
  22. import sys
  23. from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
  24. SERVER_NAME = os.environ.get("STUB_SERVER_NAME", "molecule.local")
  25. PORT = int(os.environ.get("STUB_PORT", "8008"))
  26. # Rooms reported as already joined. Components that resolve a room mapping at
  27. # startup (matrix-alertmanager-receiver, for one) fail if the rooms they were
  28. # configured with are missing, so a scenario passes its own room IDs in.
  29. JOINED_ROOMS = [r for r in os.environ.get("STUB_JOINED_ROOMS", "").split(",") if r]
  30. USER_ID = os.environ.get("STUB_USER_ID", f"@stub:{SERVER_NAME}")
  31. class Handler(BaseHTTPRequestHandler):
  32. protocol_version = "HTTP/1.1"
  33. def _send(self, payload, status=200):
  34. body = json.dumps(payload).encode()
  35. self.send_response(status)
  36. self.send_header("Content-Type", "application/json")
  37. self.send_header("Content-Length", str(len(body)))
  38. self.end_headers()
  39. self.wfile.write(body)
  40. def _route(self):
  41. path = self.path.split("?", 1)[0]
  42. if path.endswith("/joined_rooms"):
  43. return {"joined_rooms": JOINED_ROOMS}
  44. if path.endswith("/whoami"):
  45. return {"user_id": USER_ID, "device_id": "STUBDEVICE"}
  46. if path.endswith("/versions"):
  47. return {
  48. "versions": ["v1.1", "v1.2", "v1.3", "v1.4", "v1.5", "v1.6"],
  49. "unstable_features": {},
  50. }
  51. if path.endswith("/capabilities"):
  52. return {"capabilities": {}}
  53. if path.endswith("/_matrix/client/v3/login"):
  54. return {
  55. "user_id": USER_ID,
  56. "access_token": "stub_access_token",
  57. "device_id": "STUBDEVICE",
  58. "home_server": SERVER_NAME,
  59. }
  60. if path.endswith("/createRoom"):
  61. return {"room_id": f"!stub-room:{SERVER_NAME}"}
  62. if re.search(r"/rooms/[^/]+/join$", path) or path.endswith("/join"):
  63. return {"room_id": f"!stub-room:{SERVER_NAME}"}
  64. if "/send/" in path or "/state/" in path:
  65. return {"event_id": f"$stub-event:{SERVER_NAME}"}
  66. if path.endswith("/register"):
  67. return {
  68. "user_id": USER_ID,
  69. "access_token": "stub_access_token",
  70. "device_id": "STUBDEVICE",
  71. "home_server": SERVER_NAME,
  72. }
  73. if path.endswith("/profile") or "/profile/" in path:
  74. return {"displayname": "stub"}
  75. if path.startswith("/_matrix/key/"):
  76. return {"server_name": SERVER_NAME, "verify_keys": {}, "old_verify_keys": {}}
  77. if path.startswith("/.well-known/matrix/client"):
  78. return {"m.homeserver": {"base_url": f"http://{SERVER_NAME}:{PORT}"}}
  79. if path.startswith("/.well-known/matrix/server"):
  80. return {"m.server": f"{SERVER_NAME}:{PORT}"}
  81. if path.endswith("/health") or path.endswith("/_matrix/federation/v1/version"):
  82. return {"server": {"name": "molecule-stub", "version": "0"}}
  83. # Anything unrecognised: an empty object, so a component doing a startup
  84. # probe of an endpoint not listed here still gets past it.
  85. return {}
  86. def do_GET(self):
  87. self._send(self._route())
  88. def do_POST(self):
  89. length = int(self.headers.get("Content-Length") or 0)
  90. if length:
  91. self.rfile.read(length)
  92. self._send(self._route())
  93. def do_PUT(self):
  94. self.do_POST()
  95. def do_DELETE(self):
  96. self._send({})
  97. def log_message(self, fmt, *args):
  98. # Quiet by default; STUB_VERBOSE=1 when a scenario will not start and you
  99. # need to see what the component is actually asking for.
  100. if os.environ.get("STUB_VERBOSE"):
  101. sys.stderr.write("stub: " + (fmt % args) + "\n")
  102. if __name__ == "__main__":
  103. ThreadingHTTPServer(("0.0.0.0", PORT), Handler).serve_forever()