Skip to content

Commit 40de343

Browse files
authored
Fix relative path mount tests (#1168)
`make test` occasionally fails with: ``` ✘ Test testHostDNSReinitialize() recorded an issue at HostDNSResolverTest.swift:132:45: Expectation failed: (error → Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory") as? (ContainerizationError → NSError) ✘ Suite HostDNSResolverTest failed after 0.119 seconds with 1 issue. ```
1 parent aa17d46 commit 40de343

2 files changed

Lines changed: 14 additions & 31 deletions

File tree

Sources/Services/ContainerAPIService/Client/Parser.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,18 +316,18 @@ public struct Parser {
316316
return result
317317
}
318318

319-
public static func mounts(_ rawMounts: [String]) throws -> [VolumeOrFilesystem] {
319+
public static func mounts(_ rawMounts: [String], relativeTo basePath: URL? = nil) throws -> [VolumeOrFilesystem] {
320320
var mounts: [VolumeOrFilesystem] = []
321321
let rawMounts = rawMounts.dedupe()
322322
for mount in rawMounts {
323-
let m = try Parser.mount(mount)
323+
let m = try Parser.mount(mount, relativeTo: basePath)
324324
try validateMount(m)
325325
mounts.append(m)
326326
}
327327
return mounts
328328
}
329329

330-
public static func mount(_ mount: String) throws -> VolumeOrFilesystem {
330+
public static func mount(_ mount: String, relativeTo basePath: URL? = nil) throws -> VolumeOrFilesystem {
331331
let parts = mount.split(separator: ",")
332332
if parts.count == 0 {
333333
throw ContainerizationError(.invalidArgument, message: "invalid mount format: \(mount)")
@@ -407,7 +407,7 @@ public struct Parser {
407407
switch type {
408408
case "virtiofs", "bind":
409409
// For bind mounts, resolve both absolute and relative paths
410-
let url = URL(filePath: val)
410+
let url = basePath?.appending(path: val).standardizedFileURL ?? URL(filePath: val)
411411
let absolutePath = url.absoluteURL.path
412412

413413
var isDirectory: ObjCBool = false
@@ -458,17 +458,17 @@ public struct Parser {
458458
))
459459
}
460460

461-
public static func volumes(_ rawVolumes: [String]) throws -> [VolumeOrFilesystem] {
461+
public static func volumes(_ rawVolumes: [String], relativeTo basePath: URL? = nil) throws -> [VolumeOrFilesystem] {
462462
var mounts: [VolumeOrFilesystem] = []
463463
for volume in rawVolumes {
464-
let m = try Parser.volume(volume)
464+
let m = try Parser.volume(volume, relativeTo: basePath)
465465
try Parser.validateMount(m)
466466
mounts.append(m)
467467
}
468468
return mounts
469469
}
470470

471-
public static func volume(_ volume: String) throws -> VolumeOrFilesystem {
471+
public static func volume(_ volume: String, relativeTo basePath: URL? = nil) throws -> VolumeOrFilesystem {
472472
var vol = volume
473473
vol.trimLeft(char: ":")
474474

@@ -508,7 +508,7 @@ public struct Parser {
508508
options: options
509509
))
510510
}
511-
let url = URL(filePath: src)
511+
let url = basePath?.appending(path: src).standardizedFileURL ?? URL(filePath: src)
512512
let absolutePath = url.absoluteURL.path
513513

514514
var isDirectory: ObjCBool = false

Tests/ContainerAPIClientTests/ParserTest.swift

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -329,20 +329,6 @@ struct ParserTest {
329329

330330
@Test
331331
func testRelativePaths() throws {
332-
let originalDir = FileManager.default.currentDirectoryPath
333-
defer {
334-
FileManager.default.changeCurrentDirectoryPath(originalDir)
335-
}
336-
337-
func realpath(_ path: String) -> String {
338-
let resolved = UnsafeMutablePointer<CChar>.allocate(capacity: Int(PATH_MAX))
339-
defer { resolved.deallocate() }
340-
guard let result = Darwin.realpath(path, resolved) else {
341-
return path
342-
}
343-
return String(cString: result)
344-
}
345-
346332
// Test bind mount with relative path "."
347333
do {
348334
let tempDir = FileManager.default.temporaryDirectory.appendingPathComponent("test-bind-\(UUID().uuidString)")
@@ -351,12 +337,11 @@ struct ParserTest {
351337
try? FileManager.default.removeItem(at: tempDir)
352338
}
353339

354-
FileManager.default.changeCurrentDirectoryPath(tempDir.path)
355-
let result = try Parser.mount("type=bind,src=.,dst=/foo")
340+
let result = try Parser.mount("type=bind,src=.,dst=/foo", relativeTo: tempDir)
356341

357342
switch result {
358343
case .filesystem(let fs):
359-
#expect(fs.source == realpath(tempDir.path))
344+
#expect(fs.source == tempDir.standardizedFileURL.path)
360345
#expect(fs.destination == "/foo")
361346
#expect(!fs.isVolume)
362347
case .volume:
@@ -372,12 +357,11 @@ struct ParserTest {
372357
try? FileManager.default.removeItem(at: tempDir)
373358
}
374359

375-
FileManager.default.changeCurrentDirectoryPath(tempDir.path)
376-
let result = try Parser.volume("./:/foo")
360+
let result = try Parser.volume("./:/foo", relativeTo: tempDir)
377361

378362
switch result {
379363
case .filesystem(let fs):
380-
let expectedPath = realpath(tempDir.path)
364+
let expectedPath = tempDir.standardizedFileURL.path
381365
// Normalize trailing slashes for comparison
382366
#expect(fs.source.trimmingCharacters(in: CharacterSet(charactersIn: "/")) == expectedPath.trimmingCharacters(in: CharacterSet(charactersIn: "/")))
383367
#expect(fs.destination == "/foo")
@@ -395,12 +379,11 @@ struct ParserTest {
395379
try? FileManager.default.removeItem(at: tempDir)
396380
}
397381

398-
FileManager.default.changeCurrentDirectoryPath(tempDir.path)
399-
let result = try Parser.volume("./subdir:/foo")
382+
let result = try Parser.volume("./subdir:/foo", relativeTo: tempDir)
400383

401384
switch result {
402385
case .filesystem(let fs):
403-
let expectedPath = realpath(nestedDir.path)
386+
let expectedPath = nestedDir.standardizedFileURL.path
404387
// Normalize trailing slashes for comparison
405388
#expect(fs.source.trimmingCharacters(in: CharacterSet(charactersIn: "/")) == expectedPath.trimmingCharacters(in: CharacterSet(charactersIn: "/")))
406389
#expect(fs.destination == "/foo")

0 commit comments

Comments
 (0)