Skip to content

Commit a95eca6

Browse files
committed
Add TestCLIExport
1 parent 2f7f23a commit a95eca6

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

Sources/ContainerCommands/Container/ContainerExport.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ extension Application {
3333
@OptionGroup
3434
public var logOptions: Flags.Logging
3535

36-
@Option(name: .long, help: "container ID")
37-
var name: String
36+
@Option(name: .long, help: "image name")
37+
var image: String?
3838

39-
@Argument(help: "image name")
40-
var image: String
39+
@Argument(help: "container ID")
40+
var name: String
4141

4242
public func run() async throws {
4343
let client = ContainerClient()
@@ -48,6 +48,8 @@ extension Application {
4848
try? FileManager.default.removeItem(at: tempDir)
4949
}
5050

51+
let imageName = image ?? name
52+
5153
let archive = tempDir.appendingPathComponent("archive.tar")
5254
try await client.export(id: name, archive: archive)
5355

@@ -57,7 +59,7 @@ extension Application {
5759
"""
5860
try dockerfile.data(using: .utf8)!.write(to: tempDir.appendingPathComponent("Dockerfile"), options: .atomic)
5961

60-
let builder = try BuildCommand.parse(["-t", image, tempDir.absolutePath()])
62+
let builder = try BuildCommand.parse(["-t", imageName, tempDir.absolutePath()])
6163

6264
try await builder.run()
6365
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2026 Apple Inc. and the container project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import Foundation
18+
import Testing
19+
20+
class TestCLIExportCommand: CLITest {
21+
private func getTestName() -> String {
22+
Test.current!.name.trimmingCharacters(in: ["(", ")"]).lowercased()
23+
}
24+
25+
@Test func testExportCommand() throws {
26+
let name = getTestName()
27+
try doLongRun(name: name, autoRemove: false)
28+
defer {
29+
try? doRemove(name: name)
30+
}
31+
32+
let mustBeInImage = "must-be-in-image"
33+
_ = try doExec(name: name, cmd: ["sh", "-c", "echo \(mustBeInImage) > /foo"])
34+
35+
try doStop(name: name)
36+
try doExport(name: name, image: name)
37+
defer {
38+
try? doRemoveImages(images: [name])
39+
}
40+
41+
let exported = "\(name)-from-exported"
42+
try doLongRun(name: exported, image: name)
43+
defer {
44+
try? doStop(name: exported)
45+
}
46+
47+
let foo = try doExec(name: exported, cmd: ["cat", "/foo"])
48+
#expect(foo == mustBeInImage + "\n")
49+
}
50+
}

Tests/CLITests/Utilities/CLITest.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,16 @@ class CLITest {
583583
.filter { (key, val) in proxyVars.contains(key) }
584584
.flatMap { (key, val) in ["-e", "\(key)=\(val)"] }
585585
}
586+
587+
func doExport(name: String, image: String) throws {
588+
let (_, _, error, status) = try run(arguments: [
589+
"export",
590+
"--image",
591+
image,
592+
name,
593+
])
594+
if status != 0 {
595+
throw CLIError.executionFailed("command failed: \(error)")
596+
}
597+
}
586598
}

0 commit comments

Comments
 (0)