From 024791655f0409f7713915acd0d0c63559ff7a2b Mon Sep 17 00:00:00 2001 From: lucas persson <4030216+luben93@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:16:29 +0100 Subject: [PATCH 1/2] Testcontainers' Mongo, and other exec-based containers, start on Kiri exec start and attach answered 101 to every client. Docker does that only for a request with an Upgrade header, and sends 200 OK with the raw stream as the body otherwise. docker-java (Testcontainers) sends none, and read no ExitCode from the exec. Co-Authored-By: Claude Sonnet 5.5 --- crates/kiri/src/daemon/api.rs | 4 ++-- crates/kiri/src/daemon/http.rs | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/crates/kiri/src/daemon/api.rs b/crates/kiri/src/daemon/api.rs index 03ebb0e5..7456193f 100644 --- a/crates/kiri/src/daemon/api.rs +++ b/crates/kiri/src/daemon/api.rs @@ -2217,7 +2217,7 @@ fn attach(req: &Request, engine: &Arc, key: &str, w: &mut UnixStream, r: return Ok(Next::KeepAlive); } }; - http::upgrade(w)?; + http::upgrade_if_asked(req, w)?; if want_in { forward_stdin(engine.clone(), id, r)?; } @@ -2338,7 +2338,7 @@ fn exec_start(req: &Request, engine: &Arc, id: &str, w: &mut UnixStream, http::no_content(w)?; return Ok(Next::KeepAlive); } - http::upgrade(w)?; + http::upgrade_if_asked(req, w)?; if stdin { forward_stdin(engine.clone(), id.to_owned(), r)?; } diff --git a/crates/kiri/src/daemon/http.rs b/crates/kiri/src/daemon/http.rs index 082afcf8..92d786d4 100644 --- a/crates/kiri/src/daemon/http.rs +++ b/crates/kiri/src/daemon/http.rs @@ -370,6 +370,21 @@ pub fn upgrade(w: &mut impl Write) -> io::Result<()> { w.flush() } +/// Starts the raw stream for Docker's `attach` and `exec` start. As Docker +/// does (it checks that the `Upgrade` header is present, not its value), a +/// client that asked for the upgrade (the docker CLI) gets `101`; one that did +/// not (docker-java, so Testcontainers) gets `200 OK` with the stream as the +/// body. A `101` to a client that never asked for it can end its read before +/// the process's exit code is recorded, and it then sees an exec with no +/// `ExitCode`. +pub fn upgrade_if_asked(req: &Request, w: &mut impl Write) -> io::Result<()> { + if req.header("Upgrade").is_some() { + return upgrade(w); + } + head(w, 200, "Content-Type: application/vnd.docker.raw-stream\r\n")?; + w.flush() +} + /// Docker's multiplexed stream framing: `[stream, 0, 0, 0, len u32 BE] data`. pub fn mux_frame(w: &mut impl Write, stream: u8, data: &[u8]) -> io::Result<()> { let mut header = [0u8; 8]; @@ -434,4 +449,26 @@ mod tests { // An invalid escape stays written, as in query values. assert_eq!(path_of("/images/100%/json"), "/images/100%/json"); } + + /// `attach` and `exec` start answer `101` only to a client that asked to + /// upgrade (the docker CLI). docker-java, so Testcontainers, does not, and + /// gets `200 OK` with the raw stream as the body, as from Docker. + #[test] + fn upgrades_only_when_asked() { + let reply = |headers: Vec<(String, String)>| { + let req = Request { method: "POST".into(), path: "/exec/x/start".into(), query: Vec::new(), headers, body: Vec::new() }; + let mut out = Vec::new(); + upgrade_if_asked(&req, &mut out).unwrap(); + String::from_utf8(out).unwrap() + }; + let asked = reply(vec![("Upgrade".into(), "tcp".into()), ("Connection".into(), "Upgrade".into())]); + assert!(asked.starts_with("HTTP/1.1 101 "), "{asked}"); + assert!(asked.contains("Upgrade: tcp\r\n"), "{asked}"); + let other = reply(vec![("upgrade".into(), "h2c".into())]); + assert!(other.starts_with("HTTP/1.1 101 "), "{other}"); + let plain = reply(Vec::new()); + assert!(plain.starts_with("HTTP/1.1 200 OK\r\n"), "{plain}"); + assert!(plain.contains("Content-Type: application/vnd.docker.raw-stream\r\n"), "{plain}"); + assert!(!plain.contains("Upgrade"), "{plain}"); + } } From e54b8091efc68565c4dace47366e49b0dffc81d4 Mon Sep 17 00:00:00 2001 From: lucas persson <4030216+luben93@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:16:29 +0100 Subject: [PATCH 2/2] e2e: exec start answers 200 without Upgrade and 101 with it --- tests/e2e.sh | 2 +- tests/testcontainers-api.py | 51 ++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/tests/e2e.sh b/tests/e2e.sh index 46888f9c..42a9b29b 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -82,7 +82,7 @@ t version-api "1.47" $D version --format '{{.Server.APIVersion}}' # Engine version (Kiri's own beside it), the image's CMD for "Cmd": [], the # connection kept after a copy, log follow, one timestamp a line. t version-kiri-component "Kiri=" $D version --format '{{range .Server.Components}}{{.Name}}={{.Version}} {{end}}' -for c in version cmd keepalive follow follow-created timestamps; do +for c in version cmd keepalive follow follow-created timestamps exec; do t "testcontainers-$c" "ok $c" python3 "$(dirname "$0")/testcontainers-api.py" "$SOCK" "$c" done # Ryuk mounts the engine's socket and talks to it from inside. diff --git a/tests/testcontainers-api.py b/tests/testcontainers-api.py index 8ca9e933..a56c3f24 100644 --- a/tests/testcontainers-api.py +++ b/tests/testcontainers-api.py @@ -2,7 +2,7 @@ """What Testcontainers (docker-java, .NET, Go) needs of the engine API, asked the way those clients ask: raw HTTP/1.1 on one kept-alive connection. - tests/testcontainers-api.py SOCKET [version|cmd|keepalive|follow|follow-created|timestamps ...] + tests/testcontainers-api.py SOCKET [version|cmd|keepalive|follow|follow-created|timestamps|exec ...] Prints one line per check, "ok " or "FAIL : why", and exits 1 if any failed. The containers it makes (tc-api-*) are left running. @@ -46,10 +46,10 @@ def take(self, n): out, self.buf = self.buf[:n], self.buf[n:] return out - def send(self, method, path, body=None, ctype="application/json"): + def send(self, method, path, body=None, ctype="application/json", extra=""): if isinstance(body, (dict, list)): body = json.dumps(body).encode() - head = f"{method} /v1.32{path} HTTP/1.1\r\nHost: localhost:2375\r\nAccept: application/json\r\n" + head = f"{method} /v1.32{path} HTTP/1.1\r\nHost: localhost:2375\r\nAccept: application/json\r\n{extra}" if body is not None: head += f"Content-Type: {ctype}\r\nContent-Length: {len(body)}\r\n" self.s.sendall(head.encode() + b"\r\n" + (body or b"")) @@ -212,9 +212,54 @@ def check_timestamps(): return "one RFC 3339 stamp" +def exec_start(c, cid, cmd, extra=""): + """Creates an exec and starts it on the same connection; returns the exec + id, the reply's status and headers, and everything it streamed to EOF.""" + status, _, reply = c.request("POST", f"/containers/{cid}/exec", {"AttachStdout": True, "AttachStderr": True, "Cmd": cmd}) + assert status == 201, f"exec create {status} {reply[:200]!r}" + eid = json.loads(reply)["Id"] + c.send("POST", f"/exec/{eid}/start", {"Detach": False, "Tty": False}, extra=extra) + status, headers = c.head() + out = c.buf + c.buf = b"" + try: + while True: + chunk = c.s.recv(65536) + if not chunk: + break + out += chunk + except socket.timeout: + raise AssertionError("the exec's stream never closed") + return eid, status, headers, out + + +def check_exec(): + # docker-java's execInContainer (MongoDBContainer's rs.initiate) sends no + # Upgrade header. Docker answers 200 with the stream as the body, closed + # when the exec ends; a 101 there made the client stop early and read an + # exec with no ExitCode. The docker CLI asks for the upgrade and gets 101. + c = Conn() + cid = fresh(c, "tc-api-exec", ["sleep", "600"]) + status, _, reply = c.request("POST", f"/containers/{cid}/start") + assert status == 204, f"start {status} {reply[:200]!r}" + eid, status, headers, out = exec_start(c, cid, ["sh", "-c", "echo out; exit 3"]) + assert status == 200, f"exec start without Upgrade answered {status}" + assert "upgrade" not in headers, f"Upgrade header in a 200: {headers}" + assert headers.get("content-type") == "application/vnd.docker.raw-stream", headers.get("content-type") + assert b"out\n" in out, f"stream {out!r}" + c2 = Conn() + _, _, reply = c2.request("GET", f"/exec/{eid}/json") + info = json.loads(reply) + assert info["Running"] is False and info["ExitCode"] == 3, f"after the stream closed: {info}" + _, status, headers, _ = exec_start(Conn(), cid, ["true"], extra="Connection: Upgrade\r\nUpgrade: tcp\r\n") + assert status == 101 and headers.get("upgrade") == "tcp", f"with Upgrade: tcp: {status} {headers}" + return "200 without Upgrade, ExitCode set at EOF; 101 with it" + + CHECKS = { "version": check_version, "cmd": check_cmd, "keepalive": check_keepalive, "follow": check_follow, "follow-created": check_follow_created, "timestamps": check_timestamps, + "exec": check_exec, } if __name__ == "__main__":