|
| 1 | +""" |
| 2 | +Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +""" |
| 4 | + |
| 5 | +import http.server |
| 6 | +import json |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import threading |
| 11 | +import unittest |
| 12 | + |
| 13 | +INVOCATION_ID_HEADER = "Lambda-Runtime-Invocation-Id" |
| 14 | +REQUEST_ID = "request-id-1" |
| 15 | + |
| 16 | +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 17 | +CLIENT_PROBE = os.path.join( |
| 18 | + os.path.dirname(os.path.abspath(__file__)), "invocation_id_probe.py" |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class StubRapidHandler(http.server.BaseHTTPRequestHandler): |
| 23 | + """Minimal stand-in for the RAPID /next, /response and /error endpoints. |
| 24 | +
|
| 25 | + Each request's raw header block is appended to the server's `received` list |
| 26 | + so that tests can assert on what actually went over the wire rather than on |
| 27 | + what the Python layer believed it was sending. |
| 28 | + """ |
| 29 | + |
| 30 | + protocol_version = "HTTP/1.1" |
| 31 | + |
| 32 | + def log_message(self, format, *args): |
| 33 | + pass # keep the test output clean |
| 34 | + |
| 35 | + def _record(self): |
| 36 | + self.server.received.append((self.path, self.headers)) |
| 37 | + |
| 38 | + def do_GET(self): |
| 39 | + self._record() |
| 40 | + body = b"{}" |
| 41 | + self.send_response(200) |
| 42 | + self.send_header("Lambda-Runtime-Aws-Request-Id", REQUEST_ID) |
| 43 | + self.send_header("Lambda-Runtime-Invoked-Function-Arn", "arn:aws:lambda:::f") |
| 44 | + self.send_header("Lambda-Runtime-Deadline-Ms", "1735689600000") |
| 45 | + self.send_header("Content-Type", "application/json") |
| 46 | + if self.server.invocation_id is not None: |
| 47 | + self.send_header(INVOCATION_ID_HEADER, self.server.invocation_id) |
| 48 | + self.send_header("Content-Length", str(len(body))) |
| 49 | + self.end_headers() |
| 50 | + self.wfile.write(body) |
| 51 | + |
| 52 | + def do_POST(self): |
| 53 | + self._record() |
| 54 | + length = int(self.headers.get("Content-Length") or 0) |
| 55 | + if length: |
| 56 | + self.rfile.read(length) |
| 57 | + self.send_response(202) |
| 58 | + self.send_header("Content-Length", "0") |
| 59 | + self.end_headers() |
| 60 | + |
| 61 | + |
| 62 | +class StubRapid(http.server.ThreadingHTTPServer): |
| 63 | + |
| 64 | + def __init__(self): |
| 65 | + super().__init__(("127.0.0.1", 0), StubRapidHandler) |
| 66 | + self.invocation_id = None |
| 67 | + self.received = [] |
| 68 | + |
| 69 | + @property |
| 70 | + def endpoint(self): |
| 71 | + return "{}:{}".format(*self.server_address) |
| 72 | + |
| 73 | + |
| 74 | +class TestInvocationIdOnTheWire(unittest.TestCase): |
| 75 | + """End-to-end coverage of the Lambda-Runtime-Invocation-Id echo. |
| 76 | +
|
| 77 | + These tests drive the compiled extension against a real socket, so they |
| 78 | + cover the header-emitting C++ code in aws-lambda-cpp that the mocked unit |
| 79 | + tests in test_lambda_runtime_client.py cannot reach. If |
| 80 | + aws-lambda-cpp-add-invocation-id.patch is ever dropped from the vendored |
| 81 | + tarball, these are the tests that fail. |
| 82 | + """ |
| 83 | + |
| 84 | + def _run_client(self, invocation_id, mode="response"): |
| 85 | + """Serve one invocation to a real client and return its POST headers.""" |
| 86 | + server = StubRapid() |
| 87 | + server.invocation_id = invocation_id |
| 88 | + thread = threading.Thread(target=server.serve_forever, daemon=True) |
| 89 | + thread.start() |
| 90 | + self.addCleanup(thread.join, 5) |
| 91 | + self.addCleanup(server.shutdown) |
| 92 | + self.addCleanup(server.server_close) |
| 93 | + |
| 94 | + env = dict( |
| 95 | + os.environ, |
| 96 | + AWS_LAMBDA_RUNTIME_API=server.endpoint, |
| 97 | + PYTHONPATH=os.pathsep.join( |
| 98 | + [REPO_ROOT] |
| 99 | + + ([os.environ["PYTHONPATH"]] if os.environ.get("PYTHONPATH") else []) |
| 100 | + ), |
| 101 | + ) |
| 102 | + completed = subprocess.run( |
| 103 | + [sys.executable, CLIENT_PROBE, mode], |
| 104 | + env=env, |
| 105 | + cwd=REPO_ROOT, |
| 106 | + capture_output=True, |
| 107 | + text=True, |
| 108 | + timeout=30, |
| 109 | + ) |
| 110 | + self.assertEqual( |
| 111 | + 0, |
| 112 | + completed.returncode, |
| 113 | + f"client failed:\nstdout={completed.stdout}\nstderr={completed.stderr}", |
| 114 | + ) |
| 115 | + |
| 116 | + posted = [h for path, h in server.received if path.endswith("/" + mode)] |
| 117 | + self.assertEqual(1, len(posted), f"expected exactly one POST to /{mode}") |
| 118 | + return json.loads(completed.stdout), posted[0] |
| 119 | + |
| 120 | + def test_invocation_id_is_echoed_on_response(self): |
| 121 | + seen, posted = self._run_client("inv-uuid-round-trip") |
| 122 | + |
| 123 | + self.assertEqual("inv-uuid-round-trip", seen["next_invocation_id"]) |
| 124 | + # get_all, not get: a duplicated header must be visible, not normalized. |
| 125 | + self.assertEqual(["inv-uuid-round-trip"], posted.get_all(INVOCATION_ID_HEADER)) |
| 126 | + |
| 127 | + def test_invocation_id_is_echoed_on_error(self): |
| 128 | + seen, posted = self._run_client("inv-uuid-error-path", mode="error") |
| 129 | + |
| 130 | + self.assertEqual("inv-uuid-error-path", seen["next_invocation_id"]) |
| 131 | + self.assertEqual(["inv-uuid-error-path"], posted.get_all(INVOCATION_ID_HEADER)) |
| 132 | + |
| 133 | + def test_no_header_is_sent_when_rapid_did_not_send_one(self): |
| 134 | + seen, posted = self._run_client(None) |
| 135 | + |
| 136 | + self.assertIsNone(seen["next_invocation_id"]) |
| 137 | + self.assertIsNone(posted.get_all(INVOCATION_ID_HEADER)) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + unittest.main() |
0 commit comments