-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathEndToEndTests.swift
447 lines (375 loc) · 16 KB
/
EndToEndTests.swift
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import Logging
import SystemPackage
import XCTest
@testable import SwiftSDKGenerator
extension FileManager {
func withTemporaryDirectory<T>(logger: Logger, cleanup: Bool = true, body: (URL) async throws -> T) async throws -> T {
// Create a temporary directory using a UUID. Throws if the directory already exists.
// The docs suggest using FileManager.url(for: .itemReplacementDirectory, ...) to create a temporary directory,
// but on Linux the directory name contains spaces, which means we need to be careful to quote it everywhere:
//
// `(A Document Being Saved By \(name))`
//
// https://github.com/swiftlang/swift-corelibs-foundation/blob/21b3196b33a64d53a0989881fc9a486227b4a316/Sources/Foundation/FileManager.swift#L152
var logger = logger
let temporaryDirectory = self.temporaryDirectory.appendingPathComponent(UUID().uuidString)
logger[metadataKey: "temporaryDirectory"] = "\(temporaryDirectory.path)"
try createDirectory(at: temporaryDirectory, withIntermediateDirectories: false)
defer {
// Best effort cleanup.
do {
if cleanup {
try removeItem(at: temporaryDirectory)
logger.info("Removed temporary directory")
} else {
logger.info("Keeping temporary directory")
}
} catch {}
}
logger.info("Created temporary directory")
return try await body(temporaryDirectory)
}
}
// Building an SDK requires running the sdk-generator with `swift run swift-sdk-generator`.
// This takes a lock on `.build`, but if the tests are being run by `swift test` the outer Swift Package Manager
// instance will already hold this lock, causing the test to deadlock. We can work around this by giving
// the `swift run swift-sdk-generator` instance its own scratch directory.
func buildSDK(_ logger: Logger, scratchPath: String, withArguments runArguments: String) async throws -> String {
var logger = logger
logger[metadataKey: "runArguments"] = "\"\(runArguments)\""
logger[metadataKey: "scratchPath"] = "\(scratchPath)"
logger.info("Building SDK")
var packageDirectory = FilePath(#filePath)
packageDirectory.removeLastComponent()
packageDirectory.removeLastComponent()
let generatorOutput = try await Shell.readStdout(
"cd \(packageDirectory) && swift run --scratch-path \"\(scratchPath)\" swift-sdk-generator make-linux-sdk \(runArguments)"
)
logger.info("Finished building SDK")
let installCommand = try XCTUnwrap(generatorOutput.split(separator: "\n").first {
$0.contains("swift experimental-sdk install")
})
let bundleName = try XCTUnwrap(
FilePath(String(XCTUnwrap(installCommand.split(separator: " ").last))).components.last
).stem
logger[metadataKey: "bundleName"] = "\(bundleName)"
logger.info("Checking installed SDKs")
let installedSDKs = try await Shell.readStdout("swift experimental-sdk list").components(separatedBy: "\n")
// Make sure this bundle hasn't been installed already.
if installedSDKs.contains(bundleName) {
logger.info("Removing existing SDK")
try await Shell.run("swift experimental-sdk remove \(bundleName)")
}
logger.info("Installing new SDK")
let installOutput = try await Shell.readStdout(String(installCommand))
XCTAssertTrue(installOutput.contains("successfully installed"))
return bundleName
}
private let testcases = [
#"""
// Default program generated by swift package init
print("Hello, world!")
"""#,
#"""
// Check that libc_nonshared.a is linked properly
import Foundation
func fin() -> Void {
print("exiting")
}
atexit(fin)
"""#,
]
final class RepeatedBuildTests: XCTestCase {
private let logger = Logger(label: "swift-sdk-generator")
func testRepeatedSDKBuilds() async throws {
// if ProcessInfo.processInfo.environment.keys.contains("JENKINS_URL") {
// throw XCTSkip("EndToEnd tests cannot currently run in CI: https://github.com/swiftlang/swift-sdk-generator/issues/145")
// }
var logger = logger
logger[metadataKey: "testcase"] = "testRepeatedSDKBuilds"
// Test that an existing SDK can be rebuilt without cleaning up.
// Test with no arguments by default:
var possibleArguments = ["--no-host-toolchain"]
do {
try await Shell.run("podman ps")
possibleArguments.append("--with-docker --linux-distribution-name rhel --linux-distribution-version ubi9")
} catch {
self.logger.warning("Docker CLI does not seem to be working, skipping tests that involve Docker.")
}
for runArguments in possibleArguments {
if runArguments.contains("rhel") {
// Temporarily skip the RHEL-based SDK. XCTSkip() is not suitable as it would skipping the entire test case
logger.warning("RHEL-based SDKs currently do not work with Swift 6.0: https://github.com/swiftlang/swift-sdk-generator/issues/138")
continue
}
try await FileManager.default.withTemporaryDirectory(logger: logger) { tempDir in
let _ = try await buildSDK(logger, scratchPath: tempDir.path, withArguments: runArguments)
let _ = try await buildSDK(logger, scratchPath: tempDir.path, withArguments: runArguments)
}
}
}
}
// SDKConfiguration represents an SDK build configuration and can construct the corresponding SDK generator arguments
struct SDKConfiguration {
var swiftVersion: String
var linuxDistributionName: String
var architecture: String
var withDocker: Bool
var bundleName: String { "\(linuxDistributionName)_\(architecture)_\(swiftVersion)-RELEASE\(withDocker ? "_with-docker" : "")" }
func withDocker(_ enabled: Bool = true) -> SDKConfiguration {
var res = self
res.withDocker = enabled
return res
}
func withArchitecture(_ arch: String) -> SDKConfiguration {
var res = self
res.architecture = arch
return res
}
var hostArch: String? {
let triple = try? SwiftSDKGenerator.getCurrentTriple(isVerbose: false)
return triple?.arch?.rawValue
}
var sdkGeneratorArguments: String {
return [
"--sdk-name \(bundleName)",
"--no-host-toolchain",
withDocker ? "--with-docker" : nil,
"--swift-version \(swiftVersion)-RELEASE",
testLinuxSwiftSDKs ? "--host \(hostArch!)-unknown-linux-gnu" : nil,
"--target \(architecture)-unknown-linux-gnu",
"--linux-distribution-name \(linuxDistributionName)"
].compactMap{ $0 }.joined(separator: " ")
}
}
// Skip slow tests unless an environment variable is set
func skipSlow() throws {
try XCTSkipUnless(
ProcessInfo.processInfo.environment.keys.contains("SWIFT_SDK_GENERATOR_RUN_SLOW_TESTS"),
"Skipping slow test because SWIFT_SDK_GENERATOR_RUN_SLOW_TESTS is not set"
)
}
var testLinuxSwiftSDKs: Bool {
ProcessInfo.processInfo.environment.keys.contains("SWIFT_SDK_GENERATOR_TEST_LINUX_SWIFT_SDKS")
}
func buildTestcase(_ logger: Logger, testcase: String, bundleName: String, tempDir: URL) async throws {
let testPackageURL = tempDir.appendingPathComponent("swift-sdk-generator-test")
let testPackageDir = FilePath(testPackageURL.path)
try FileManager.default.createDirectory(atPath: testPackageDir.string, withIntermediateDirectories: true)
logger.info("Creating test project \(testPackageDir)")
try await Shell.run("swift package --package-path \(testPackageDir) init --type executable")
let main_swift = testPackageURL.appendingPathComponent("Sources/main.swift")
try testcase.write(to: main_swift, atomically: true, encoding: .utf8)
// This is a workaround for if Swift 6.0 is used as the host toolchain to run the generator.
// We manually set the swift-tools-version to 5.9 to support building our test cases.
logger.info("Updating minimum swift-tools-version in test project...")
let package_swift = testPackageURL.appendingPathComponent("Package.swift")
let text = try String(contentsOf: package_swift, encoding: .utf8)
var lines = text.components(separatedBy: .newlines)
if lines.count > 0 {
lines[0] = "// swift-tools-version: 5.9"
let result = lines.joined(separator: "\r\n")
try result.write(to: package_swift, atomically: true, encoding: .utf8)
}
var buildOutput = ""
// If we are testing Linux Swift SDKs, we will run the test cases on a matrix of Docker containers
// that contains each Swift-supported Linux distribution. This way we can validate that each
// distribution is capable of building using the Linux Swift SDK.
if testLinuxSwiftSDKs {
let swiftContainerVersions = ["focal", "jammy", "noble", "fedora39", "rhel-ubi9", "amazonlinux2", "bookworm"]
for containerVersion in swiftContainerVersions {
logger.info("Building test project in 6.0-\(containerVersion) container")
buildOutput = try await Shell.readStdout(
"""
podman run --rm -v \(testPackageDir):/src \
-v $HOME/.swiftpm/swift-sdks:/root/.swiftpm/swift-sdks \
--workdir /src swift:6.0-\(containerVersion) \
/bin/bash -c "swift build --scratch-path /root/.build --experimental-swift-sdk \(bundleName)"
"""
)
XCTAssertTrue(buildOutput.contains("Build complete!"))
logger.info("Test project built successfully")
logger.info("Building test project in 6.0-\(containerVersion) container with static-swift-stdlib")
buildOutput = try await Shell.readStdout(
"""
podman run --rm -v \(testPackageDir):/src \
-v $HOME/.swiftpm/swift-sdks:/root/.swiftpm/swift-sdks \
--workdir /src swift:6.0-\(containerVersion) \
/bin/bash -c "swift build --scratch-path /root/.build --experimental-swift-sdk \(bundleName) --static-swift-stdlib"
"""
)
XCTAssertTrue(buildOutput.contains("Build complete!"))
logger.info("Test project built successfully")
}
} else {
logger.info("Building test project")
buildOutput = try await Shell.readStdout(
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName)"
)
XCTAssertTrue(buildOutput.contains("Build complete!"))
logger.info("Test project built successfully")
try await Shell.run("rm -rf \(testPackageDir.appending(".build"))")
logger.info("Building test project with static-swift-stdlib")
buildOutput = try await Shell.readStdout(
"swift build --package-path \(testPackageDir) --experimental-swift-sdk \(bundleName) --static-swift-stdlib"
)
XCTAssertTrue(buildOutput.contains("Build complete!"))
logger.info("Test project built successfully")
}
}
func buildTestcases(config: SDKConfiguration) async throws {
var logger = Logger(label: "EndToEndTests")
logger[metadataKey: "testcase"] = "testPackageInitExecutable"
// if ProcessInfo.processInfo.environment.keys.contains("JENKINS_URL") {
// throw XCTSkip("EndToEnd tests cannot currently run in CI: https://github.com/swiftlang/swift-sdk-generator/issues/145")
// }
if config.withDocker {
do {
try await Shell.run("podman ps")
} catch {
throw XCTSkip("Container runtime is not available - skipping tests which require it")
}
}
let bundleName = try await FileManager.default.withTemporaryDirectory(logger: logger) { tempDir in
try await buildSDK(logger, scratchPath: tempDir.path, withArguments: config.sdkGeneratorArguments)
}
logger.info("Built SDK")
for testcase in testcases {
try await FileManager.default.withTemporaryDirectory(logger: logger) { tempDir in
try await buildTestcase(logger, testcase: testcase, bundleName: bundleName, tempDir: tempDir)
}
}
// Cleanup
logger.info("Removing SDK to cleanup...")
try await Shell.run("swift experimental-sdk remove \(bundleName)")
}
final class Swift59_UbuntuEndToEndTests: XCTestCase {
let config = SDKConfiguration(
swiftVersion: "5.9.2",
linuxDistributionName: "ubuntu",
architecture: "aarch64",
withDocker: false
)
func testAarch64Direct() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64"))
}
func testX86_64Direct() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64"))
}
func testAarch64FromContainer() async throws {
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
}
func testX86_64FromContainer() async throws {
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
}
}
final class Swift510_UbuntuEndToEndTests: XCTestCase {
let config = SDKConfiguration(
swiftVersion: "5.10.1",
linuxDistributionName: "ubuntu",
architecture: "aarch64",
withDocker: false
)
func testAarch64Direct() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64"))
}
func testX86_64Direct() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64"))
}
func testAarch64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
}
func testX86_64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
}
}
final class Swift60_UbuntuEndToEndTests: XCTestCase {
let config = SDKConfiguration(
swiftVersion: "6.0.3",
linuxDistributionName: "ubuntu",
architecture: "aarch64",
withDocker: false
)
func testAarch64Direct() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64"))
}
func testX86_64Direct() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64"))
}
func testAarch64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
}
func testX86_64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
}
}
final class Swift59_RHELEndToEndTests: XCTestCase {
let config = SDKConfiguration(
swiftVersion: "5.9.2",
linuxDistributionName: "rhel",
architecture: "aarch64",
withDocker: true // RHEL-based SDKs can only be built from containers
)
func testAarch64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
}
func testX86_64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
}
}
final class Swift510_RHELEndToEndTests: XCTestCase {
let config = SDKConfiguration(
swiftVersion: "5.10.1",
linuxDistributionName: "rhel",
architecture: "aarch64",
withDocker: true // RHEL-based SDKs can only be built from containers
)
func testAarch64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
}
func testX86_64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
}
}
final class Swift60_RHELEndToEndTests: XCTestCase {
let config = SDKConfiguration(
swiftVersion: "6.0.3",
linuxDistributionName: "rhel",
architecture: "aarch64",
withDocker: true // RHEL-based SDKs can only be built from containers
)
func testAarch64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("aarch64").withDocker())
}
func testX86_64FromContainer() async throws {
try skipSlow()
try await buildTestcases(config: config.withArchitecture("x86_64").withDocker())
}
}