Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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"
),
Expand Down Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions Sources/Integration/PodVolumeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ContainerizationEXT4
import ContainerizationError
import ContainerizationOCI
import ContainerizationOS
import ContainerizationTestSupport
import Foundation
import Logging
import SystemPackage
Expand Down
58 changes: 58 additions & 0 deletions Tests/ContainerizationTestSupportTests/NBDServerTests.swift
Original file line number Diff line number Diff line change
@@ -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
Loading