Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/kiri/src/daemon/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,7 @@ fn attach(req: &Request, engine: &Arc<Engine>, 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)?;
}
Expand Down Expand Up @@ -2338,7 +2338,7 @@ fn exec_start(req: &Request, engine: &Arc<Engine>, 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)?;
}
Expand Down
37 changes: 37 additions & 0 deletions crates/kiri/src/daemon/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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}");
}
}
2 changes: 1 addition & 1 deletion tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 48 additions & 3 deletions tests/testcontainers-api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>" or "FAIL <name>: why", and exits 1 if
any failed. The containers it makes (tc-api-*) are left running.
Expand Down Expand Up @@ -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""))
Expand Down Expand Up @@ -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__":
Expand Down