Files
container/Tests/CLITests/Subcommands/Run/TestCLIRunInitImage.swift
T
Noah Thornton e3c49803a0 Move to TOML configuration for defaults (#1425)
- Discussion topic #1336.
- This change migrates away from using `UserDefaults`,
  instead providing a TOML configuration mechanism for
  user configurable settings. All existing system property
  settings keys are supported in the new configuration
  file. However, users will have to migrate any settings
  they have configured in the `UserDefaults` into TOML
  for these settings to take effect.
- Breaking changes:
  * `container system property get` is removed in favor of
    users directly utilizing `container system property list --format toml | jq<>`.
  * `container system property set` is removed since the TOML
    configuration is effectively immutable during the lifetime of the
    `container` daemon. Uses can edit the TOML they have in their home
    directory, however no changes will take effect until the daemon is
    restarted via `container system stop && container system start`
* `container system property list --format table` is removed as
    generating tabular format is non-trivial and the new TOML format is
    intended to be human readable
2026-05-04 12:04:24 -07:00

115 lines
4.5 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the container project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import Foundation
import Testing
/// Tests for the `--init-image` flag which allows specifying a custom init filesystem
/// image for microvms. This enables customizing boot-time behavior before the OCI
/// container starts.
///
/// See: https://github.com/apple/container/discussions/838
///
/// Note: A full integration test that verifies custom init behavior would require
/// a pre-built test init image that writes a marker to /dev/kmsg. This can be added
/// once a test init image is published to the registry.
@Suite(.serialSuites)
class TestCLIRunInitImage: CLITest {
private func getTestName() -> String {
Test.current!.name.trimmingCharacters(in: ["(", ")"]).lowercased()
}
/// Test that specifying a non-existent init-image fails with an appropriate error.
@Test func testRunWithNonExistentInitImage() throws {
let name = getTestName()
let nonExistentImage = "nonexistent.invalid/init-image:does-not-exist"
#expect(throws: CLIError.self, "expected container run with non-existent init-image to fail") {
let (_, _, error, status) = try run(arguments: [
"run",
"--rm",
"--name", name,
"-d",
"--init-image", nonExistentImage,
alpine,
"sleep", "infinity",
])
defer { try? doRemove(name: name, force: true) }
if status != 0 {
throw CLIError.executionFailed("command failed: \(error)")
}
}
}
/// Test that the `--init-image` flag is recognized and documented in CLI help.
@Test func testInitImageFlagInHelp() throws {
let (_, output, _, status) = try run(arguments: ["run", "--help"])
#expect(status == 0, "expected help command to succeed")
#expect(
output.contains("--init-image"),
"expected help output to contain --init-image flag"
)
#expect(
output.contains("custom init image"),
"expected help output to describe the init-image flag"
)
}
/// Test that the `--init-image` flag works with `container create` command.
@Test func testCreateWithNonExistentInitImage() throws {
let name = getTestName()
let nonExistentImage = "nonexistent.invalid/init-image:does-not-exist"
#expect(throws: CLIError.self, "expected container create with non-existent init-image to fail") {
let (_, _, error, status) = try run(arguments: [
"create",
"--rm",
"--name", name,
"--init-image", nonExistentImage,
alpine,
"echo", "hello",
])
defer { try? doRemove(name: name, force: true) }
if status != 0 {
throw CLIError.executionFailed("command failed: \(error)")
}
}
}
/// Test that explicitly specifying the default init image works the same as
/// not specifying any init image.
@Test func testRunWithExplicitDefaultInitImage() throws {
let name = getTestName()
let config = try getSystemConfig()
let initImage = config.vminit.image
// Run container with explicit default init image
try doLongRun(name: name, args: ["--init-image", initImage])
defer {
try? doStop(name: name)
}
// Verify container is running and functional
try waitForContainerRunning(name)
let output = try doExec(name: name, cmd: ["echo", "hello"])
#expect(
output.trimmingCharacters(in: .whitespacesAndNewlines) == "hello",
"expected 'hello' output from exec, got '\(output)'"
)
}
}