From 117324e300e142fa69699318b6d7b59b9f4760ee Mon Sep 17 00:00:00 2001 From: Danny Canter Date: Sun, 8 Jun 2025 02:07:51 -0700 Subject: [PATCH] Integration: Add a multi-exec output test (#34) Add a test similar to the concurrent exec test, except supply stdout. Signed-off-by: Danny Canter --- Sources/Integration/ProcessTests.swift | 65 ++++++++++++++++++++++++++ Sources/Integration/Suite.swift | 9 ++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Sources/Integration/ProcessTests.swift b/Sources/Integration/ProcessTests.swift index de7ca06d..1465cbb6 100644 --- a/Sources/Integration/ProcessTests.swift +++ b/Sources/Integration/ProcessTests.swift @@ -155,6 +155,71 @@ extension IntegrationSuite { } } + func testMultipleConcurrentProcessesOutput() async throws { + let id = "test-concurrent-processes-output" + + let bs = try await bootstrap() + let container = LinuxContainer( + id, + rootfs: bs.rootfs, + vmm: bs.vmm + ) + container.arguments = ["/bin/sleep", "1000"] + + do { + try await container.create() + try await container.start() + + let execConfig = ContainerizationOCI.Process( + args: ["/bin/echo", "hi"], + env: ["PATH=\(LinuxContainer.defaultPath)"] + ) + + try await withThrowingTaskGroup(of: Void.self) { group in + for i in 0...80 { + let idx = i + group.addTask { + let buffer = BufferWriter() + + var config = execConfig + config.args[1] = "hi\(idx)" + + let exec = try await container.exec( + "exec-\(idx)", + configuration: config, + stdout: buffer, + ) + try await exec.start() + + let status = try await exec.wait() + if status != 0 { + throw IntegrationError.assert(msg: "process status \(status) != 0") + } + + let output = String(data: buffer.data, encoding: .utf8) + guard output == "hi\(idx)\n" else { + throw IntegrationError.assert( + msg: "process should have returned on stdout 'hi\(idx)' != '\(output!))") + } + try await exec.delete() + } + } + + // wait for all the exec'd processes. + try await group.waitForAll() + print("all group processes exit") + + // kill the init process. + try await container.kill(SIGKILL) + let status = try await container.wait() + try await container.stop() + print("\(status)") + } + } catch { + throw error + } + } + func testProcessUser() async throws { let id = "test-process-user" diff --git a/Sources/Integration/Suite.swift b/Sources/Integration/Suite.swift index 416ac653..d7cfb07e 100644 --- a/Sources/Integration/Suite.swift +++ b/Sources/Integration/Suite.swift @@ -201,10 +201,11 @@ struct IntegrationSuite: AsyncParsableCommand { "process false": testProcessFalse, "process echo hi": testProcessEchoHi, "process user": testProcessUser, - "test multiple concurrent processes": testMultipleConcurrentProcesses, - "test container hostname": testHostname, - "test container mount": testMounts, - "test nested virt": testNestedVirtualizationEnabled, + "multiple concurrent processes": testMultipleConcurrentProcesses, + "multiple concurrent processes with output": testMultipleConcurrentProcessesOutput, + "container hostname": testHostname, + "container mount": testMounts, + "nested virt": testNestedVirtualizationEnabled, ] var passed = 0