Skip to content

Commit e49a563

Browse files
saehejkangjglogan
andauthored
[tests]: refactor run function for stdin/stdout support (#830)
When working on test cases for #827 and #734 the following issues occurred 1. There was no support to pass in `Data` for `stdin` (input pipe added + new argument for the data) 2. Pipes were getting blocked during the `try process.run()` and `process.waitUntilExit()` (order of operations were fixed) 3. We need the binary data for directly piping the `stdout` data to `stdin`, and converting the data to a `string`, returns `""` (added a new value to the tuple) I opened a new PR here for this as I did not want to bloat the other PRs with so many updates. At first I thought of creating a new function named `runStdinStdout`, but that seemed redundant as it pretty much was going to do the same thing as `run`. ## Testing - [x] Tested locally - [x] Added/updated tests - [ ] Added/updated docs --------- Co-authored-by: J Logan <[email protected]>
1 parent 9fbf84b commit e49a563

File tree

10 files changed

+78
-70
lines changed

10 files changed

+78
-70
lines changed

Tests/CLITests/Subcommands/Build/CLIBuildBase.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class TestCLIBuildBase: CLITest {
252252
}
253253

254254
func builderStart(cpus: Int64 = 2, memoryInGBs: Int64 = 2) throws {
255-
let (_, error, status) = try run(arguments: [
255+
let (_, _, error, status) = try run(arguments: [
256256
"builder",
257257
"start",
258258
"-c",
@@ -266,7 +266,7 @@ class TestCLIBuildBase: CLITest {
266266
}
267267

268268
func builderStop() throws {
269-
let (_, error, status) = try run(arguments: [
269+
let (_, _, error, status) = try run(arguments: [
270270
"builder",
271271
"stop",
272272
])
@@ -276,7 +276,7 @@ class TestCLIBuildBase: CLITest {
276276
}
277277

278278
func builderDelete(force: Bool = false) throws {
279-
let (_, error, status) = try run(
279+
let (_, _, error, status) = try run(
280280
arguments: [
281281
"builder",
282282
"delete",

Tests/CLITests/Subcommands/Containers/TestCLIExec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TestCLIExecCommand: CLITest {
6262
// Retry loop to check if the marker file was created by the detached process
6363
var markerFound = false
6464
for _ in 0..<3 {
65-
let (_, _, status) = try run(arguments: [
65+
let (_, _, _, status) = try run(arguments: [
6666
"exec",
6767
name,
6868
"test", "-f", "/tmp/detach_test_marker",

Tests/CLITests/Subcommands/Images/TestCLIImages.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TestCLIImagesCommand: CLITest {
3232
args.append("--all")
3333
}
3434

35-
let (_, error, status) = try run(arguments: args)
35+
let (_, _, error, status) = try run(arguments: args)
3636
if status != 0 {
3737
throw CLIError.executionFailed("command failed: \(error)")
3838
}
@@ -49,7 +49,7 @@ class TestCLIImagesCommand: CLITest {
4949
}
5050

5151
func doListImages() throws -> [Image] {
52-
let (output, error, status) = try run(arguments: [
52+
let (_, output, error, status) = try run(arguments: [
5353
"image",
5454
"list",
5555
"--format",
@@ -75,7 +75,7 @@ class TestCLIImagesCommand: CLITest {
7575
newName,
7676
]
7777

78-
let (_, error, status) = try run(arguments: tagArgs)
78+
let (_, _, error, status) = try run(arguments: tagArgs)
7979
if status != 0 {
8080
throw CLIError.executionFailed("command failed: \(error)")
8181
}
@@ -323,7 +323,7 @@ extension TestCLIImagesCommand {
323323
"--output",
324324
tempFile.path(),
325325
]
326-
let (_, error, status) = try run(arguments: saveArgs)
326+
let (_, _, error, status) = try run(arguments: saveArgs)
327327
if status != 0 {
328328
throw CLIError.executionFailed("command failed: \(error)")
329329
}
@@ -344,7 +344,7 @@ extension TestCLIImagesCommand {
344344
"-i",
345345
tempFile.path(),
346346
]
347-
let (_, loadErr, loadStatus) = try run(arguments: loadArgs)
347+
let (_, _, loadErr, loadStatus) = try run(arguments: loadArgs)
348348
if loadStatus != 0 {
349349
throw CLIError.executionFailed("command failed: \(loadErr)")
350350
}

Tests/CLITests/Subcommands/Plugins/TestCLIPluginErrors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct TestCLIPluginErrors {
2323
// without the APIServer started, so DefaultCommand will fail to create
2424
// a PluginLoader and emit the improved guidance.
2525
let cli = try CLITest()
26-
let (_, stderr, status) = try cli.run(arguments: ["nosuchplugin"]) // non-existent plugin name
26+
let (_, _, stderr, status) = try cli.run(arguments: ["nosuchplugin"]) // non-existent plugin name
2727

2828
#expect(status != 0)
2929
#expect(stderr.contains("container system start"))

Tests/CLITests/Subcommands/Run/TestCLIRunLifecycle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class TestCLIRunLifecycle: CLITest {
5555
}
5656
try self.waitForContainerRunning(name)
5757

58-
let (output, _, status) = try self.run(arguments: ["start", name])
58+
let (_, output, _, status) = try self.run(arguments: ["start", name])
5959
#expect(status == 0, "expected start to succeed on already running container")
6060
#expect(output.trimmingCharacters(in: .whitespacesAndNewlines) == name, "expected output to be container name")
6161

@@ -76,7 +76,7 @@ class TestCLIRunLifecycle: CLITest {
7676
}
7777
try self.waitForContainerRunning(name)
7878

79-
let (_, error, status) = try self.run(arguments: ["start", "-a", name])
79+
let (_, _, error, status) = try self.run(arguments: ["start", "-a", name])
8080
#expect(status != 0, "expected start with attach to fail on already running container")
8181
#expect(error.contains("attach is currently unsupported on already running containers"), "expected error message about attach not supported")
8282

Tests/CLITests/Subcommands/Run/TestCLIRunOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ class TestCLIRunCommand: CLITest {
577577
}
578578

579579
func getDefaultDomain() throws -> String? {
580-
let (output, err, status) = try run(arguments: ["system", "property", "get", "dns.domain"])
580+
let (_, output, err, status) = try run(arguments: ["system", "property", "get", "dns.domain"])
581581
try #require(status == 0, "default DNS domain retrieval returned status \(status): \(err)")
582582
let trimmedOutput = output.trimmingCharacters(in: .whitespacesAndNewlines)
583583
if trimmedOutput == "" {

Tests/CLITests/Subcommands/System/TestKernelSet.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TestCLIKernelSet: CLITest {
4141
"--recommended",
4242
"--force",
4343
]
44-
let (_, error, status) = try run(arguments: arguments)
44+
let (_, _, error, status) = try run(arguments: arguments)
4545
if status != 0 {
4646
throw CLIError.executionFailed("failed to reset kernel to recommended: \(error)")
4747
}
@@ -56,7 +56,7 @@ class TestCLIKernelSet: CLITest {
5656
]
5757
arguments.append(contentsOf: extraArgs)
5858

59-
let (_, error, status) = try run(arguments: arguments)
59+
let (_, _, error, status) = try run(arguments: arguments)
6060
if status != 0 {
6161
throw CLIError.executionFailed("failed to set kernel: \(error)")
6262
}

Tests/CLITests/Subcommands/Volumes/TestCLIAnonymousVolumes.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TestCLIAnonymousVolumes: CLITest {
2929

3030
private func cleanupAllTestResources() {
3131
// Clean up test containers (force remove)
32-
if let (output, _, status) = try? run(arguments: ["ls", "-a"]), status == 0 {
32+
if let (_, output, _, status) = try? run(arguments: ["ls", "-a"]), status == 0 {
3333
let containers = output.components(separatedBy: .newlines)
3434
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
3535
.filter { $0.lowercased().starts(with: "test") }
@@ -40,7 +40,7 @@ class TestCLIAnonymousVolumes: CLITest {
4040
}
4141

4242
// Clean up test volumes (both anonymous and named)
43-
if let (output, _, status) = try? run(arguments: ["volume", "list", "--quiet"]), status == 0 {
43+
if let (_, output, _, status) = try? run(arguments: ["volume", "list", "--quiet"]), status == 0 {
4444
let volumes = output.components(separatedBy: .newlines)
4545
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
4646
.filter { isValidUUID($0) || $0.lowercased().starts(with: "test") }
@@ -56,7 +56,7 @@ class TestCLIAnonymousVolumes: CLITest {
5656
}
5757

5858
func getAnonymousVolumeNames() throws -> [String] {
59-
let (output, error, status) = try run(arguments: ["volume", "list", "--quiet"])
59+
let (_, output, error, status) = try run(arguments: ["volume", "list", "--quiet"])
6060
guard status == 0 else {
6161
throw CLIError.executionFailed("volume list failed: \(error)")
6262
}
@@ -66,7 +66,7 @@ class TestCLIAnonymousVolumes: CLITest {
6666
}
6767

6868
func volumeExists(name: String) throws -> Bool {
69-
let (output, _, status) = try run(arguments: ["volume", "list", "--quiet"])
69+
let (_, output, _, status) = try run(arguments: ["volume", "list", "--quiet"])
7070
guard status == 0 else { return false }
7171
let volumes = output.components(separatedBy: .newlines)
7272
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
@@ -80,14 +80,14 @@ class TestCLIAnonymousVolumes: CLITest {
8080
}
8181

8282
func doVolumeCreate(name: String) throws {
83-
let (_, error, status) = try run(arguments: ["volume", "create", name])
83+
let (_, _, error, status) = try run(arguments: ["volume", "create", name])
8484
if status != 0 {
8585
throw CLIError.executionFailed("volume create failed: \(error)")
8686
}
8787
}
8888

8989
func doVolumeDeleteIfExists(name: String) {
90-
let (_, _, _) = (try? run(arguments: ["volume", "rm", name])) ?? ("", "", 1)
90+
let (_, _, _, _) = (try? run(arguments: ["volume", "rm", name])) ?? (nil, "", "", 1)
9191
}
9292

9393
func doRemoveIfExists(name: String, force: Bool = false) {
@@ -96,7 +96,7 @@ class TestCLIAnonymousVolumes: CLITest {
9696
args.append("--force")
9797
}
9898
args.append(name)
99-
let (_, _, _) = (try? run(arguments: args)) ?? ("", "", 1)
99+
let (_, _, _, _) = (try? run(arguments: args)) ?? (nil, "", "", 1)
100100
}
101101

102102
@Test func testAnonymousVolumeCreationAndPersistence() async throws {
@@ -115,7 +115,7 @@ class TestCLIAnonymousVolumes: CLITest {
115115
let beforeCount = try getAnonymousVolumeNames().count
116116

117117
// Run container with --rm and anonymous volume
118-
let (_, _, status) = try run(arguments: [
118+
let (_, _, _, status) = try run(arguments: [
119119
"run",
120120
"--rm",
121121
"--name",
@@ -133,7 +133,7 @@ class TestCLIAnonymousVolumes: CLITest {
133133
try await Task.sleep(for: .seconds(1))
134134

135135
// Verify container was removed
136-
let (lsOutput, _, _) = try run(arguments: ["ls", "-a"])
136+
let (_, lsOutput, _, _) = try run(arguments: ["ls", "-a"])
137137
let containers = lsOutput.components(separatedBy: .newlines)
138138
.filter { $0.contains(containerName) }
139139
#expect(containers.isEmpty, "container should be removed with --rm")
@@ -206,7 +206,7 @@ class TestCLIAnonymousVolumes: CLITest {
206206
let beforeCount = try getAnonymousVolumeNames().count
207207

208208
// Run with multiple anonymous volumes
209-
let (_, _, status) = try run(arguments: [
209+
let (_, _, _, status) = try run(arguments: [
210210
"run",
211211
"--rm",
212212
"--name",
@@ -243,7 +243,7 @@ class TestCLIAnonymousVolumes: CLITest {
243243
let beforeCount = try getAnonymousVolumeNames().count
244244

245245
// Use --mount syntax
246-
let (_, _, status) = try run(arguments: [
246+
let (_, _, _, status) = try run(arguments: [
247247
"run",
248248
"--rm",
249249
"--name",
@@ -314,7 +314,7 @@ class TestCLIAnonymousVolumes: CLITest {
314314
let volumeName = volumeNames[0]
315315

316316
// Inspect volume in JSON format
317-
let (output, error, status) = try run(arguments: ["volume", "list", "--format", "json"])
317+
let (_, output, error, status) = try run(arguments: ["volume", "list", "--format", "json"])
318318
#expect(status == 0, "volume list should succeed: \(error)")
319319

320320
// Parse JSON to verify metadata
@@ -351,7 +351,7 @@ class TestCLIAnonymousVolumes: CLITest {
351351
try waitForContainerRunning(containerName)
352352

353353
// List volumes
354-
let (output, error, status) = try run(arguments: ["volume", "list"])
354+
let (_, output, error, status) = try run(arguments: ["volume", "list"])
355355
#expect(status == 0, "volume list should succeed: \(error)")
356356

357357
// Verify TYPE column exists and shows both types
@@ -381,7 +381,7 @@ class TestCLIAnonymousVolumes: CLITest {
381381
let beforeAnonCount = try getAnonymousVolumeNames().count
382382

383383
// Run with both named and anonymous volumes, with --rm
384-
let (_, _, status) = try run(arguments: [
384+
let (_, _, _, status) = try run(arguments: [
385385
"run",
386386
"--rm",
387387
"--name",
@@ -427,7 +427,7 @@ class TestCLIAnonymousVolumes: CLITest {
427427
doRemoveIfExists(name: containerName, force: true)
428428

429429
// Manual deletion should succeed (volume is unmounted)
430-
let (_, error, status) = try run(arguments: ["volume", "rm", volumeID])
430+
let (_, _, error, status) = try run(arguments: ["volume", "rm", volumeID])
431431
#expect(status == 0, "manual deletion of unmounted anonymous volume should succeed: \(error)")
432432

433433
// Verify volume is gone
@@ -450,7 +450,7 @@ class TestCLIAnonymousVolumes: CLITest {
450450
let beforeCount = try getAnonymousVolumeNames().count
451451

452452
// Run in detached mode with --rm
453-
let (_, _, status) = try run(arguments: [
453+
let (_, _, _, status) = try run(arguments: [
454454
"run",
455455
"-d",
456456
"--rm",
@@ -467,7 +467,7 @@ class TestCLIAnonymousVolumes: CLITest {
467467
try await Task.sleep(for: .seconds(3))
468468

469469
// Container should be removed
470-
let (lsOutput, _, _) = try run(arguments: ["ls", "-a"])
470+
let (_, lsOutput, _, _) = try run(arguments: ["ls", "-a"])
471471
let containers = lsOutput.components(separatedBy: .newlines)
472472
.filter { $0.contains(containerName) }
473473
#expect(containers.isEmpty, "container should be auto-removed")

0 commit comments

Comments
 (0)