LinuxContainer: Add ability to close stdin (#201)

This allows the user the ability to raise an EOF for the containers
stdin. Today there's no way to close stdin so something as simple as
"cat" and relying on EOF to move the process forward doesn't work
This commit is contained in:
Danny Canter
2025-07-08 18:38:04 -07:00
committed by GitHub
parent ad219fecf1
commit f254f48bfc
12 changed files with 366 additions and 0 deletions
+47
View File
@@ -74,6 +74,21 @@ extension IntegrationSuite {
}
}
final class StdinBuffer: ReaderStream {
let data: Data
init(data: Data) {
self.data = data
}
func stream() -> AsyncStream<Data> {
let (stream, cont) = AsyncStream<Data>.makeStream()
cont.yield(self.data)
cont.finish()
return stream
}
}
func testProcessEchoHi() async throws {
let id = "test-process-echo-hi"
let bs = try await bootstrap()
@@ -402,4 +417,36 @@ extension IntegrationSuite {
msg: "process should have returned on stdout '\(expected)' != '\(String(data: buffer.data, encoding: .utf8)!)'")
}
}
func testProcessStdin() async throws {
let id = "test-container-stdin"
let bs = try await bootstrap()
let container = LinuxContainer(
id,
rootfs: bs.rootfs,
vmm: bs.vmm
)
container.arguments = ["cat"]
container.stdin = StdinBuffer(data: "Hello from test".data(using: .utf8)!)
let buffer = BufferWriter()
container.stdout = buffer
try await container.create()
try await container.start()
let status = try await container.wait()
try await container.stop()
guard status == 0 else {
throw IntegrationError.assert(msg: "process status \(status) != 0")
}
let expected = "Hello from test"
guard String(data: buffer.data, encoding: .utf8) == "\(expected)" else {
throw IntegrationError.assert(
msg: "process should have returned on stdout '\(expected)' != '\(String(data: buffer.data, encoding: .utf8)!)'")
}
}
}
+1
View File
@@ -199,6 +199,7 @@ struct IntegrationSuite: AsyncParsableCommand {
"process false": testProcessFalse,
"process echo hi": testProcessEchoHi,
"process user": testProcessUser,
"process stdin": testProcessStdin,
"process home envvar": testProcessHomeEnvvar,
"process custom home envvar": testProcessCustomHomeEnvvar,
"process tty ensure TERM": testProcessTtyEnvvar,