forked from apple/container
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeConfigurationTests.swift
More file actions
163 lines (136 loc) · 6.09 KB
/
Copy pathRuntimeConfigurationTests.swift
File metadata and controls
163 lines (136 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//===----------------------------------------------------------------------===//
// 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 ContainerResource
import ContainerRuntimeClient
import ContainerRuntimeLinuxClient
import Containerization
import Foundation
import SystemPackage
import Testing
/// Unit tests for RuntimeConfiguration functionality.
///
/// These tests verify the runtime configuration serialization and deserialization,
/// ensuring that configuration can be properly written, read, and used to create bundles.
struct RuntimeConfigurationTests {
/// Test that reading non-existent runtime configuration file throws
/// appropriate error
@Test
func testReadNonExistentRuntimeConfiguration() throws {
let tempURL = FileManager.default.temporaryDirectory
let nonExistentPath = FilePath(tempURL.path(percentEncoded: false))
.appending("non-existent-\(UUID()).json")
#expect(throws: Error.self) {
_ = try RuntimeConfiguration.readRuntimeConfiguration(from: nonExistentPath)
}
}
/// Test that runtime configuration reads and writes as expected
@Test
func testRuntimeConfigurationReadWrite() throws {
let bundleURL = FileManager.default.temporaryDirectory
.appendingPathComponent("test-bundle-\(UUID())")
let bundlePath = FilePath(bundleURL.path(percentEncoded: false))
defer {
try? FileManager.default.removeItem(at: bundleURL)
}
let initFs = Filesystem.virtiofs(
source: "/path/to/initfs",
destination: "/",
options: ["ro"]
)
let kernel = Kernel(
path: URL(fileURLWithPath: "/path/to/kernel"),
platform: .linuxArm
)
let runtimeConfig = RuntimeConfiguration(
path: bundlePath,
initialFilesystem: initFs,
kernel: kernel,
containerConfiguration: nil,
containerRootFilesystem: nil,
options: nil
)
try runtimeConfig.writeRuntimeConfiguration()
let readRuntimeConfig = try RuntimeConfiguration.readRuntimeConfiguration(from: bundlePath)
#expect(
readRuntimeConfig.path == bundlePath,
"Path should match")
#expect(
readRuntimeConfig.kernel.path == kernel.path,
"Kernel path should match")
#expect(
readRuntimeConfig.initialFilesystem.source == initFs.source,
"Initial filesystem source should match")
#expect(
readRuntimeConfig.containerConfiguration == nil,
"Container configuration should be nil")
#expect(
readRuntimeConfig.containerRootFilesystem == nil,
"Root filesystem should be nil")
#expect(
readRuntimeConfig.options == nil,
"Options should be nil")
}
@Test
func testRuntimeConfigurationWithVariant() throws {
let bundleURL = FileManager.default.temporaryDirectory
.appendingPathComponent("test-bundle-\(UUID())")
let bundlePath = FilePath(bundleURL.path(percentEncoded: false))
defer {
try? FileManager.default.removeItem(at: bundleURL)
}
let initFs = Filesystem.virtiofs(
source: "/path/to/initfs",
destination: "/",
options: ["ro"]
)
let kernel = Kernel(
path: URL(fileURLWithPath: "/path/to/kernel"),
platform: .linuxArm
)
let linuxData = LinuxRuntimeData(variant: "test-variant")
let encodedData = try JSONEncoder().encode(linuxData)
let runtimeConfig = RuntimeConfiguration(
path: bundlePath,
initialFilesystem: initFs,
kernel: kernel,
runtimeData: encodedData
)
try runtimeConfig.writeRuntimeConfiguration()
let readRuntimeConfig = try RuntimeConfiguration.readRuntimeConfiguration(from: bundlePath)
#expect(readRuntimeConfig.runtimeData != nil, "runtimeData should be persisted")
let decodedData = try JSONDecoder().decode(LinuxRuntimeData.self, from: readRuntimeConfig.runtimeData!)
#expect(decodedData.variant == "test-variant", "Variant should round-trip through RuntimeConfiguration")
}
/// Verify that runtime-configuration.json files written before the
/// URL → FilePath migration (where `path` was a URL absoluteString
/// like "file:///foo/bar") still decode correctly. Otherwise an upgrade
/// would render existing containers unstartable.
@Test
func testRuntimeConfigurationDecodesLegacyURLPathFormat() throws {
let kernel = Kernel(path: URL(fileURLWithPath: "/path/to/kernel"), platform: .linuxArm)
let initFs = Filesystem.virtiofs(source: "/path/to/initfs", destination: "/", options: ["ro"])
let kernelJSON = try String(data: JSONEncoder().encode(kernel), encoding: .utf8) ?? ""
let initFsJSON = try String(data: JSONEncoder().encode(initFs), encoding: .utf8) ?? ""
let legacyJSON = """
{"path":"file:///tmp/legacy-bundle","initialFilesystem":\(initFsJSON),"kernel":\(kernelJSON)}
"""
let data = Data(legacyJSON.utf8)
let decoded = try JSONDecoder().decode(RuntimeConfiguration.self, from: data)
#expect(decoded.path == FilePath("/tmp/legacy-bundle"))
#expect(decoded.kernel.path == kernel.path)
#expect(decoded.initialFilesystem.source == initFs.source)
}
}