Skip to content

Commit 0f4c8cf

Browse files
authored
Make unit tests runnable on Linux (#686)
Closes #606 This lets the unit tests be runnable on linux. The change: - Adds a linux-test makefile target so we can run the unit tests locally - Fixes up some test code to work on Linux (mostly ifdefs) - Runs the unit tests in CI now
1 parent 8908ffc commit 0f4c8cf

7 files changed

Lines changed: 32 additions & 13 deletions

File tree

.github/workflows/linux-build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ jobs:
5555

5656
- name: Build vminitd (musl)
5757
run: make -C vminitd
58+
59+
- name: Run unit tests
60+
run: swift test --disable-automatic-resolution -Xswiftc -warnings-as-errors

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ ifeq ($(LIBC),all)
8989
else
9090
$(call linux_run,make containerization && make -C vminitd LIBC=$(LIBC))
9191
endif
92+
93+
.PHONY: linux-test
94+
linux-test:
95+
$(call linux_run,swift test $(SWIFT_CONFIGURATION))
9296
endif
9397

9498
.PHONY: all

Tests/ContainerizationOCITests/RegistryClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ struct OCIClientTests: ~Copyable {
199199
buffer.withUnsafeReadableBytes { pointer in
200200
let unsafeBufferPointer = pointer.bindMemory(to: UInt8.self)
201201
if let addr = unsafeBufferPointer.baseAddress {
202-
outputStream!.write(addr, maxLength: buffer.readableBytes)
202+
_ = outputStream!.write(addr, maxLength: buffer.readableBytes)
203203
}
204204
}
205205
}

Tests/ContainerizationOSTests/FileDescriptor+SecurePathTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ struct FileDescriptorPathSecureTests {
132132
let attrs = try FileManager.default.attributesOfItem(atPath: dirPath.string)
133133
let posixPerms = attrs[.posixPermissions] as? NSNumber
134134
// Mask to permission bits only (not file type bits)
135-
let permMask: UInt16 = 0o777
136-
let actualPerms = (posixPerms?.uint16Value ?? 0) & permMask
135+
let permMask: CModeT = 0o777
136+
let actualPerms = CModeT(posixPerms?.uint16Value ?? 0) & permMask
137137
let expectedPerms = permissions.rawValue & permMask
138138
#expect(
139139
actualPerms == expectedPerms,
@@ -396,7 +396,7 @@ struct FileDescriptorPathSecureTests {
396396

397397
// Create a regular file
398398
let filePath = tempPath.appending("testfile.txt")
399-
FileManager.default.createFile(atPath: filePath.string, contents: Data("test".utf8))
399+
_ = FileManager.default.createFile(atPath: filePath.string, contents: Data("test".utf8))
400400

401401
// Verify file exists
402402
#expect(FileManager.default.fileExists(atPath: filePath.string))
@@ -452,9 +452,9 @@ struct FileDescriptorPathSecureTests {
452452
let deepdirPath = subdirPath.appending("deepdir")
453453

454454
try FileManager.default.createDirectory(atPath: deepdirPath.string, withIntermediateDirectories: true)
455-
FileManager.default.createFile(atPath: nestedPath.appending("file1.txt").string, contents: Data("1".utf8))
456-
FileManager.default.createFile(atPath: subdirPath.appending("file2.txt").string, contents: Data("2".utf8))
457-
FileManager.default.createFile(atPath: deepdirPath.appending("file3.txt").string, contents: Data("3".utf8))
455+
_ = FileManager.default.createFile(atPath: nestedPath.appending("file1.txt").string, contents: Data("1".utf8))
456+
_ = FileManager.default.createFile(atPath: subdirPath.appending("file2.txt").string, contents: Data("2".utf8))
457+
_ = FileManager.default.createFile(atPath: deepdirPath.appending("file3.txt").string, contents: Data("3".utf8))
458458

459459
// Verify structure exists
460460
#expect(FileManager.default.fileExists(atPath: nestedPath.string))
@@ -491,7 +491,7 @@ struct FileDescriptorPathSecureTests {
491491
// Create target file and symlink
492492
let targetPath = tempPath.appending("target.txt")
493493
let linkPath = tempPath.appending("link")
494-
FileManager.default.createFile(atPath: targetPath.string, contents: Data("target".utf8))
494+
_ = FileManager.default.createFile(atPath: targetPath.string, contents: Data("target".utf8))
495495
try FileManager.default.createSymbolicLink(atPath: linkPath.string, withDestinationPath: "target.txt")
496496

497497
// Verify both exist
@@ -523,7 +523,7 @@ struct FileDescriptorPathSecureTests {
523523
let subdirPath = mixedPath.appending("subdir")
524524

525525
try FileManager.default.createDirectory(atPath: subdirPath.string, withIntermediateDirectories: true)
526-
FileManager.default.createFile(atPath: mixedPath.appending("file.txt").string, contents: Data("test".utf8))
526+
_ = FileManager.default.createFile(atPath: mixedPath.appending("file.txt").string, contents: Data("test".utf8))
527527
try FileManager.default.createSymbolicLink(
528528
atPath: mixedPath.appending("link").string,
529529
withDestinationPath: "file.txt"
@@ -643,7 +643,7 @@ struct FileDescriptorPathSecureTests {
643643
attributes: permissions.map { [.posixPermissions: $0.rawValue] }
644644
)
645645
}
646-
FileManager.default.createFile(
646+
_ = FileManager.default.createFile(
647647
atPath: fullPath.string,
648648
contents: Data("test".utf8)
649649
)

Tests/ContainerizationOSTests/KeychainQueryTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
//===----------------------------------------------------------------------===//
1616

17+
#if os(macOS)
18+
1719
import Foundation
1820
import Testing
1921

@@ -77,3 +79,5 @@ struct KeychainQueryTests {
7779
ProcessInfo.processInfo.environment["CI"] != nil
7880
}
7981
}
82+
83+
#endif

Tests/ContainerizationOSTests/SocketTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class SocketTests {
4747
var cmsgBuf = [UInt8](repeating: 0, count: Int(CZ_CMSG_SPACE(Int(MemoryLayout<Int32>.size))))
4848

4949
msg.msg_control = withUnsafeMutablePointer(to: &cmsgBuf[0]) { UnsafeMutableRawPointer($0) }
50-
msg.msg_controllen = socklen_t(cmsgBuf.count)
50+
msg.msg_controllen = .init(cmsgBuf.count)
5151

5252
// Set up control message
5353
let cmsgPtr = withUnsafeMutablePointer(to: &msg) { CZ_CMSG_FIRSTHDR($0) }
@@ -56,8 +56,8 @@ final class SocketTests {
5656
}
5757

5858
cmsg.pointee.cmsg_level = SOL_SOCKET
59-
cmsg.pointee.cmsg_type = SCM_RIGHTS
60-
cmsg.pointee.cmsg_len = socklen_t(CZ_CMSG_LEN(Int(MemoryLayout<Int32>.size)))
59+
cmsg.pointee.cmsg_type = .init(SCM_RIGHTS)
60+
cmsg.pointee.cmsg_len = .init(CZ_CMSG_LEN(Int(MemoryLayout<Int32>.size)))
6161

6262
guard let dataPtr = CZ_CMSG_DATA(cmsg) else {
6363
throw SocketError.invalidFileDescriptor
@@ -78,7 +78,11 @@ final class SocketTests {
7878
func testSCMRightsFileDescriptorPassing() throws {
7979
// Create a socketpair for testing
8080
var fds: [Int32] = [0, 0]
81+
#if os(macOS)
8182
let result = socketpair(AF_UNIX, SOCK_STREAM, 0, &fds)
83+
#else
84+
let result = socketpair(AF_UNIX, Int32(SOCK_STREAM.rawValue), 0, &fds)
85+
#endif
8286
try #require(result == 0, "socketpair should succeed")
8387

8488
defer {

Tests/ContainerizationTests/ContainerManagerTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
//===----------------------------------------------------------------------===//
1616

17+
#if os(macOS)
18+
1719
import Containerization
1820
import ContainerizationArchive
1921
import ContainerizationError
@@ -151,3 +153,5 @@ struct ContainerManagerTests {
151153
#expect(closureWasCalled, "configuration closure must be invoked to validate interfaces")
152154
}
153155
}
156+
157+
#endif

0 commit comments

Comments
 (0)