Files
tidewall/test/server/test_health_server.gd
claudeandClaude Opus 5.5 8eb6e587fb N7: Dedicated server packaging, health endpoint and graceful shutdown
- packaging/server/tidewall-server: a wrapper that turns SIGTERM and SIGINT
  into a clean stop through the server's new --stop-file, since the engine
  can't catch signals. The server tells everyone it shut down, logs its
  statistics and exits 0; the wrapper kills it only after 15 s.
- --config=PATH reads options from a file (packaging/server/server.cfg);
  options after it on the command line win.
- --health-port answers GET /health with JSON (phase, players, tick, uptime)
  on loopback by default. HealthServer is non-blocking and bounded.
- packaging/docker: a debian:13 image that checks the engine against
  tools/engine.sha512, imports the game at build time, runs as an unprivileged
  user, and works with a read-only root and a small /tmp. compose.yaml is
  project and container tidewall-server, read-only, no capabilities.
- packaging/systemd: a hardened unit with a dynamic user.
- make server-package-check: compose up, healthy, non-root, read-only, a real
  headless client joins over UDP into the drop, then SIGTERM stops it cleanly.

This also covers H7 (graceful shutdown on SIGTERM).

Closes #38
Closes #70

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 22:07:24 -05:00

62 lines
2.2 KiB
GDScript

extends TestCase
## Issue N7: the dedicated server's health endpoint answers GET /health with JSON,
## refuses everything else, and drops requests that never finish.
var health: HealthServer
func before_each() -> void:
health = HealthServer.new()
health.report = func() -> Dictionary: return {"phase": "lobby", "players": 2}
assert_eq(health.listen(0), OK)
func after_each() -> void:
health.close()
## Sends `request` and returns everything the server says before it closes.
func _ask(request: String, limit_msec: int = 3000) -> String:
var peer: StreamPeerTCP = StreamPeerTCP.new()
assert_eq(peer.connect_to_host("127.0.0.1", health.port()), OK)
var reply: PackedByteArray = PackedByteArray()
var sent: bool = false
var started: int = Time.get_ticks_msec()
while Time.get_ticks_msec() - started < limit_msec:
health.poll()
peer.poll()
var status: int = peer.get_status()
if status == StreamPeerTCP.STATUS_CONNECTED:
if not sent and not request.is_empty():
peer.put_data(request.to_ascii_buffer())
sent = true
var available: int = peer.get_available_bytes()
if available > 0:
reply.append_array(peer.get_data(available)[1])
elif status != StreamPeerTCP.STATUS_CONNECTING:
break
OS.delay_msec(5)
peer.disconnect_from_host()
return reply.get_string_from_utf8()
func test_health_answers_with_json() -> void:
var reply: String = _ask("GET /health HTTP/1.1\r\nHost: x\r\n\r\n")
assert_true(reply.begins_with("HTTP/1.0 200 OK\r\n"), reply)
var body: Variant = JSON.parse_string(reply.get_slice("\r\n\r\n", 1))
assert_eq(body, {"status": "ok", "phase": "lobby", "players": 2.0})
assert_eq(health.answered.get(200, 0), 1)
func test_other_paths_and_methods_are_not_found() -> void:
assert_true(_ask("GET /admin HTTP/1.0\r\n\r\n").begins_with("HTTP/1.0 404"))
assert_true(_ask("POST /health HTTP/1.0\r\n\r\n").begins_with("HTTP/1.0 404"))
assert_eq(health.answered.get(404, 0), 2)
func test_unfinished_and_oversized_requests_are_dropped() -> void:
assert_eq(_ask("GET /health HTTP/1.0\r\n", 2600), "", "never finished: closed without an answer")
assert_eq(_ask("GET /" + "a".repeat(HealthServer.MAX_REQUEST_BYTES) + " HTTP/1.0\r\n\r\n"), "")
assert_eq(health.answered.size(), 0)