diff --git a/Package.swift b/Package.swift index e7b8b9a25..fe236f5f4 100644 --- a/Package.swift +++ b/Package.swift @@ -35,6 +35,7 @@ let package = Package( .library(name: "ContainerizationArchive", targets: ["ContainerizationArchive"]), .library(name: "VminitdCore", targets: ["VminitdCore", "Cgroup", "LCShim"]), .library(name: "CloudHypervisor", targets: ["CloudHypervisor"]), + .library(name: "ContainerizationTestSupport", targets: ["ContainerizationTestSupport"]), .executable(name: "cctl", targets: ["cctl"]), ], dependencies: [ @@ -275,6 +276,19 @@ let package = Package( "CShim", ] ), + .target( + name: "ContainerizationTestSupport", + dependencies: [ + .product(name: "Logging", package: "swift-log"), + .product(name: "NIOCore", package: "swift-nio"), + .product(name: "NIOPosix", package: "swift-nio"), + "ContainerizationError", + ] + ), + .testTarget( + name: "ContainerizationTestSupportTests", + dependencies: ["ContainerizationTestSupport"] + ), .target( name: "CShim" ), @@ -359,6 +373,7 @@ package.targets.append( .product(name: "NIOPosix", package: "swift-nio"), .product(name: "GRPCCore", package: "grpc-swift-2"), "Containerization", + "ContainerizationTestSupport", ], path: "Sources/Integration" ) diff --git a/Sources/Integration/NBDServer.swift b/Sources/ContainerizationTestSupport/NBDServer.swift similarity index 98% rename from Sources/Integration/NBDServer.swift rename to Sources/ContainerizationTestSupport/NBDServer.swift index 982c8752f..e4ed2a637 100644 --- a/Sources/Integration/NBDServer.swift +++ b/Sources/ContainerizationTestSupport/NBDServer.swift @@ -25,13 +25,13 @@ import NIOPosix /// /// Serves a file-backed block device using the NBD newstyle handshake protocol. /// Supports both TCP and Unix domain socket transports. -final class NBDServer: Sendable { +public final class NBDServer: Sendable { private let channel: Channel private let socketPath: String? private let group: EventLoopGroup - let url: String + public let url: String - init(filePath: String, socketPath: String, logger: Logger? = nil) throws { + public init(filePath: String, socketPath: String, logger: Logger? = nil) throws { self.socketPath = socketPath self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) @@ -43,7 +43,7 @@ final class NBDServer: Sendable { self.url = "nbd+unix:///?socket=\(socketPath)" } - init(filePath: String, port: Int, logger: Logger? = nil) throws { + public init(filePath: String, port: Int, logger: Logger? = nil) throws { self.socketPath = nil self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) @@ -57,7 +57,7 @@ final class NBDServer: Sendable { self.url = "nbd://127.0.0.1:\(boundPort)" } - func stop() { + public func stop() { try? channel.close().wait() try? group.syncShutdownGracefully() if let socketPath { diff --git a/Sources/Integration/PodVolumeTests.swift b/Sources/Integration/PodVolumeTests.swift index a28302b3c..0e861e31a 100644 --- a/Sources/Integration/PodVolumeTests.swift +++ b/Sources/Integration/PodVolumeTests.swift @@ -20,6 +20,7 @@ import ContainerizationEXT4 import ContainerizationError import ContainerizationOCI import ContainerizationOS +import ContainerizationTestSupport import Foundation import Logging import SystemPackage diff --git a/Tests/ContainerizationTestSupportTests/NBDServerTests.swift b/Tests/ContainerizationTestSupportTests/NBDServerTests.swift new file mode 100644 index 000000000..06ae86be5 --- /dev/null +++ b/Tests/ContainerizationTestSupportTests/NBDServerTests.swift @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// Copyright © 2026 Apple Inc. and the Containerization 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. +//===----------------------------------------------------------------------===// + +#if os(macOS) +import ContainerizationTestSupport +import Foundation +import Testing + +struct NBDServerTests { + private static let diskSize = 1024 * 1024 + + private func makeDisk() throws -> URL { + let url = FileManager.default.temporaryDirectory.appending(component: "nbd-\(UUID().uuidString).img") + try Data(count: Self.diskSize).write(to: url) + return url + } + + @Test + func unixSocketURL() throws { + let disk = try makeDisk() + defer { try? FileManager.default.removeItem(at: disk) } + let socketPath = "/tmp/nbd-\(UUID().uuidString.prefix(8)).sock" + + let server = try NBDServer(filePath: disk.path, socketPath: socketPath) + defer { server.stop() } + + #expect(server.url == "nbd+unix:///?socket=\(socketPath)") + #expect(FileManager.default.fileExists(atPath: socketPath)) + } + + @Test + func tcpURLUsesBoundPort() throws { + let disk = try makeDisk() + defer { try? FileManager.default.removeItem(at: disk) } + + let server = try NBDServer(filePath: disk.path, port: 0) + defer { server.stop() } + + let prefix = "nbd://127.0.0.1:" + #expect(server.url.hasPrefix(prefix)) + let port = Int(server.url.dropFirst(prefix.count)) ?? 0 + #expect(port > 0) + } +} +#endif