Files
noahandClaude Opus 4.8 4be4750aae
source / check (pull_request) Skipped
verify / verify (pull_request) Successful in 10m8s
verify / verify-windows (pull_request) Failing after 5m42s
test(report-relay): seed the label cache instead of a live in-test fetch
v5/v6 still failed in CI on the labels assertion. Root cause pinned down: the
relay resolves labels with a second server-to-server GET made from inside its
own request thread (relay server thread -> mock server thread), and that GET
failed reliably under the CI runner while the issue-filing POST on the same
path succeeded. Gitea.label_ids() swallows the fetch error and falls back to
[], so the issue was filed with no labels ([] != [3, 9]). Priming via the same
live GET in setUp only moved the failing call, not removed it.

Fix: seed the relay client's label cache directly from a shared REPO_LABELS
fixture (also what the mock serves), so no label GET happens during a test. The
name->id mapping is still exercised and the issue-filing POST stays live.
Verified 20/20 locally.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B1W6qKwKxq3oUPDKWicM6i
2026-11-16 21:40:17 -06:00

209 lines
9.2 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Tests for the report relay: python3 tools/report-relay/test_relay.py"""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import pathlib
import sys
import threading
import time
import unittest
import urllib.error
import urllib.request
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
import relay # noqa: E402
TOKEN = "test-token-123"
# The repository's labels, as the real Gitea /labels endpoint would return them.
# The mock serves these, and setUp seeds the relay client's label cache from the
# same list so label resolution is deterministic (see RelayTests.setUp).
REPO_LABELS = [{"name": "bug", "id": 1}, {"name": "ui", "id": 2},
{"name": "feature", "id": 3}, {"name": "from-app", "id": 9}]
class FakeGitea:
"""Records issue creations; serves the repository's labels."""
def __init__(self, fail=False):
self.created = []
self.fail = fail
outer = self
class H(BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def _json(self, status, body):
data = json.dumps(body).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def do_GET(self):
assert self.headers["Authorization"] == f"token {TOKEN}"
if self.path.startswith("/api/v1/repos/blueflare/alkane-toolkit/labels"):
return self._json(200, REPO_LABELS)
self._json(404, {})
def do_POST(self):
assert self.headers["Authorization"] == f"token {TOKEN}"
if outer.fail:
return self._json(500, {"message": "boom"})
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
outer.created.append((self.path, body))
n = len(outer.created)
self._json(201, {"number": n,
"html_url": f"https://gitea.example/blueflare/alkane-toolkit/issues/{n}"})
self.server = ThreadingHTTPServer(("127.0.0.1", 0), H)
threading.Thread(target=self.server.serve_forever, daemon=True).start()
self.url = f"http://127.0.0.1:{self.server.server_address[1]}"
# Block until the serve_forever thread is actually handling requests, so
# the relay's first label fetch cannot race server startup. The socket
# listens from __init__, but under load the daemon thread can be starved
# past the relay's HTTP timeout, which silently drops labels to [] and
# made the first test flaky in CI.
probe = urllib.request.Request(
self.url + "/api/v1/repos/blueflare/alkane-toolkit/labels",
headers={"Authorization": f"token {TOKEN}"})
deadline = time.monotonic() + 5
while True:
try:
urllib.request.urlopen(probe, timeout=1).read()
break
except Exception:
if time.monotonic() >= deadline:
raise
time.sleep(0.02)
def close(self):
self.server.shutdown()
self.server.server_close()
class RelayTests(unittest.TestCase):
def setUp(self):
self.gitea = FakeGitea()
self.now = [1000.0]
self.limiter = relay.RateLimiter(2, 3, clock=lambda: self.now[0])
self.logs = []
client = relay.Gitea(self.gitea.url, "blueflare/alkane-toolkit", TOKEN)
# Seed the relay client's label cache directly from REPO_LABELS instead
# of letting a request handler fetch it live. Gitea.label_ids() caches
# for 10 minutes and silently falls back to [] on any fetch error; in CI
# that first live GET -- a second server-to-server hop made from inside
# the relay's own request thread -- failed reliably and dropped the
# labels to [], while the issue-filing POST on the same path still
# worked. Seeding the cache keeps label resolution deterministic (the
# name->id mapping is still exercised) and leaves the issue POST live.
client.labels = {label["name"]: label["id"] for label in REPO_LABELS}
client.labels_at = time.monotonic()
r = relay.Relay(client, self.limiter, log=self.logs.append)
self.server = ThreadingHTTPServer(("127.0.0.1", 0), relay.make_handler(r))
threading.Thread(target=self.server.serve_forever, daemon=True).start()
self.url = f"http://127.0.0.1:{self.server.server_address[1]}"
def tearDown(self):
self.server.shutdown()
self.server.server_close()
self.gitea.close()
def post(self, body, ip="198.51.100.7", path="/v1/report", raw=None):
data = raw if raw is not None else json.dumps(body).encode()
req = urllib.request.Request(self.url + path, data=data, method="POST",
headers={"Content-Type": "application/json",
"X-Real-IP": ip})
try:
with urllib.request.urlopen(req, timeout=10) as resp:
return resp.status, json.load(resp), resp.headers
except urllib.error.HTTPError as e:
return e.code, json.load(e), e.headers
def report(self, **over):
body = {"kind": "bug", "text": "Table is blank on macOS\nSteps: open the app.",
"app_version": "0.110.0", "platform": "macos-aarch64", "os": "macOS 15.1",
"appearance": "dark", "language": "en", "window": "1280x800", "log": ""}
body.update(over)
return body
def test_files_an_issue_with_title_body_and_labels(self):
status, body, _ = self.post(self.report(kind="feature", log="12:00 scan done"))
self.assertEqual(status, 201)
self.assertEqual(body["number"], 1)
self.assertTrue(body["url"].endswith("/issues/1"))
path, issue = self.gitea.created[0]
self.assertEqual(path, "/api/v1/repos/blueflare/alkane-toolkit/issues")
self.assertEqual(issue["title"], "Table is blank on macOS")
self.assertEqual(issue["labels"], [3, 9])
self.assertIn("**Type:** Feature request", issue["body"])
self.assertIn("| OS | macOS 15.1 |", issue["body"])
self.assertIn("Steps: open the app.", issue["body"])
self.assertIn("<details><summary>Recent activity log</summary>", issue["body"])
self.assertIn("filed #1", self.logs[-1])
self.assertNotIn("blank", " ".join(self.logs), "report text is never logged")
def test_scrubs_the_log_and_neutralises_mentions(self):
log = ("set pool url=stratum+tcp://w:hunter2@pool:3333 password=hunter2\n"
"Authorization: Bearer abc.def\n``` fence ```")
status, _, _ = self.post(self.report(text="@admin please look", log=log))
self.assertEqual(status, 201)
_, issue = self.gitea.created[0]
self.assertNotIn("hunter2", issue["body"])
self.assertNotIn("abc.def", issue["body"])
self.assertNotIn("@admin", issue["body"])
self.assertNotIn("@admin", issue["title"])
self.assertIn("````\n", issue["body"], "fence longer than the log's own")
def test_rejects_bad_reports(self):
cases = [
(self.report(text=" "), 422),
(self.report(kind="hack"), 422),
(self.report(text="x" * 8001), 413),
(self.report(log="y" * (16 * 1024 + 1)), 413),
(self.report(os="z" * 121), 422),
(self.report(os=5), 422),
([1, 2], 400),
]
for body, want in cases:
status, answer, _ = self.post(body)
self.assertEqual(status, want, body if not isinstance(body, dict) else answer)
self.assertIn("error", answer)
status, _, _ = self.post(None, raw=b"{not json")
self.assertEqual(status, 400)
status, _, _ = self.post(None, raw=b"x" * (relay.MAX_BODY + 1))
self.assertEqual(status, 413)
status, _, _ = self.post(self.report(), path="/v1/other")
self.assertEqual(status, 404)
self.assertEqual(self.gitea.created, [])
def test_rate_limits_per_ip_and_in_all(self):
for _ in range(2):
self.assertEqual(self.post(self.report())[0], 201)
status, body, headers = self.post(self.report())
self.assertEqual(status, 429)
self.assertGreater(int(headers["Retry-After"]), 0)
self.assertEqual(self.post(self.report(), ip="203.0.113.9")[0], 201)
self.assertEqual(self.post(self.report(), ip="203.0.113.10")[0], 429, "global cap of 3")
self.now[0] += relay.HOUR
self.assertEqual(self.post(self.report())[0], 201, "window slides")
def test_tracker_failure_is_reported(self):
self.gitea.fail = True
status, body, _ = self.post(self.report())
self.assertEqual(status, 502)
self.assertIn("check the issue list", body["error"])
def test_title_is_cut(self):
title = relay.issue_title({"text": "\n\n" + "w" * 300})
self.assertEqual(len(title), relay.MAX_TITLE_CHARS)
self.assertTrue(title.endswith("..."))
if __name__ == "__main__":
unittest.main()