|
| 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 | +/// Tests for the `--init-image` flag which allows specifying a custom init filesystem |
| 21 | +/// image for microvms. This enables customizing boot-time behavior before the OCI |
| 22 | +/// container starts. |
| 23 | +/// |
| 24 | +/// See: https://github.com/apple/container/discussions/838 |
| 25 | +/// |
| 26 | +/// Note: A full integration test that verifies custom init behavior would require |
| 27 | +/// a pre-built test init image that writes a marker to /dev/kmsg. This can be added |
| 28 | +/// once a test init image is published to the registry. |
| 29 | +class TestCLIRunInitImage: CLITest { |
| 30 | + private func getTestName() -> String { |
| 31 | + Test.current!.name.trimmingCharacters(in: ["(", ")"]).lowercased() |
| 32 | + } |
| 33 | + |
| 34 | + /// Test that specifying a non-existent init-image fails with an appropriate error. |
| 35 | + @Test func testRunWithNonExistentInitImage() throws { |
| 36 | + let name = getTestName() |
| 37 | + let nonExistentImage = "nonexistent.invalid/init-image:does-not-exist" |
| 38 | + |
| 39 | + #expect(throws: CLIError.self, "expected container run with non-existent init-image to fail") { |
| 40 | + let (_, _, error, status) = try run(arguments: [ |
| 41 | + "run", |
| 42 | + "--rm", |
| 43 | + "--name", name, |
| 44 | + "-d", |
| 45 | + "--init-image", nonExistentImage, |
| 46 | + alpine, |
| 47 | + "sleep", "infinity", |
| 48 | + ]) |
| 49 | + defer { try? doRemove(name: name, force: true) } |
| 50 | + if status != 0 { |
| 51 | + throw CLIError.executionFailed("command failed: \(error)") |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Test that the `--init-image` flag is recognized and documented in CLI help. |
| 57 | + @Test func testInitImageFlagInHelp() throws { |
| 58 | + let (_, output, _, status) = try run(arguments: ["run", "--help"]) |
| 59 | + #expect(status == 0, "expected help command to succeed") |
| 60 | + #expect( |
| 61 | + output.contains("--init-image"), |
| 62 | + "expected help output to contain --init-image flag" |
| 63 | + ) |
| 64 | + #expect( |
| 65 | + output.contains("custom init image"), |
| 66 | + "expected help output to describe the init-image flag" |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + /// Test that the `--init-image` flag works with `container create` command. |
| 71 | + @Test func testCreateWithNonExistentInitImage() throws { |
| 72 | + let name = getTestName() |
| 73 | + let nonExistentImage = "nonexistent.invalid/init-image:does-not-exist" |
| 74 | + |
| 75 | + #expect(throws: CLIError.self, "expected container create with non-existent init-image to fail") { |
| 76 | + let (_, _, error, status) = try run(arguments: [ |
| 77 | + "create", |
| 78 | + "--rm", |
| 79 | + "--name", name, |
| 80 | + "--init-image", nonExistentImage, |
| 81 | + alpine, |
| 82 | + "echo", "hello", |
| 83 | + ]) |
| 84 | + defer { try? doRemove(name: name, force: true) } |
| 85 | + if status != 0 { |
| 86 | + throw CLIError.executionFailed("command failed: \(error)") |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// Test that explicitly specifying the default init image works the same as |
| 92 | + /// not specifying any init image. |
| 93 | + @Test func testRunWithExplicitDefaultInitImage() throws { |
| 94 | + let name = getTestName() |
| 95 | + |
| 96 | + // Get the default init image reference |
| 97 | + let (_, defaultInitImage, _, propStatus) = try run(arguments: [ |
| 98 | + "system", "property", "get", "image.init", |
| 99 | + ]) |
| 100 | + |
| 101 | + guard propStatus == 0 else { |
| 102 | + print("Skipping testRunWithExplicitDefaultInitImage: could not get default init image") |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + let initImage = defaultInitImage.trimmingCharacters(in: .whitespacesAndNewlines) |
| 107 | + |
| 108 | + // Run container with explicit default init image |
| 109 | + try doLongRun(name: name, args: ["--init-image", initImage]) |
| 110 | + defer { |
| 111 | + try? doStop(name: name) |
| 112 | + } |
| 113 | + |
| 114 | + // Verify container is running and functional |
| 115 | + try waitForContainerRunning(name) |
| 116 | + let output = try doExec(name: name, cmd: ["echo", "hello"]) |
| 117 | + #expect( |
| 118 | + output.trimmingCharacters(in: .whitespacesAndNewlines) == "hello", |
| 119 | + "expected 'hello' output from exec, got '\(output)'" |
| 120 | + ) |
| 121 | + } |
| 122 | +} |
0 commit comments