Skip to content
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a62bc2e
DRAFT: Mock out test interactions with swift.org and github.com
cmcgee1024 Jul 13, 2024
9028007
Add no-tty option to suppress the ioctl error in pipelines
cmcgee1024 Jul 13, 2024
b4a558e
Try gpg pinentry mode loopback
cmcgee1024 Jul 13, 2024
312e5b6
Try pinentry mode cancel
cmcgee1024 Jul 13, 2024
a9a0fe5
Try pinentry mode ask and force yes
cmcgee1024 Jul 13, 2024
fbb11f8
Check gnupg config
cmcgee1024 Jul 13, 2024
9a3726f
Touch gpg config file if it does not exist
cmcgee1024 Jul 13, 2024
cc5dbdf
Set loopback pinentry mode
cmcgee1024 Jul 13, 2024
d3932ba
Set batch mode options to disable passphrase on gpg keygen
cmcgee1024 Jul 13, 2024
531669b
Fix pinentry parameter for older gpg version
cmcgee1024 Jul 13, 2024
fb04cb8
Try removing pinentry parameter to adapt to amazon linux
cmcgee1024 Jul 13, 2024
e05cbf2
Fix typo on import statement
cmcgee1024 Jul 13, 2024
9845e97
Try to fix the lockup that occurs with amazon linux 2
cmcgee1024 Jul 13, 2024
13204eb
Re-add the yes option to gpg
cmcgee1024 Jul 13, 2024
89d2c9f
Re-add the no-protection batch option
cmcgee1024 Jul 13, 2024
07e3e90
Set GPG_TTY to the current tty
cmcgee1024 Jul 13, 2024
43f31de
Check entropy level before generating gpg key
cmcgee1024 Jul 13, 2024
5ad3682
Add a timeout for the gpg keygen
cmcgee1024 Jul 13, 2024
cec440f
Add a guard for generating gpg keys on Amazon Linux
cmcgee1024 Jul 13, 2024
1c8f84b
Try timeout kill option on amazon linux 2
cmcgee1024 Jul 13, 2024
ca8fd34
Output entropy information
cmcgee1024 Jul 13, 2024
4432c6a
When there is not enough entropy generate some more
cmcgee1024 Jul 13, 2024
6e52597
Add more sources of entropy
cmcgee1024 Jul 13, 2024
eb9c300
Use a test gpg key instead of generating one at test time
cmcgee1024 Jul 14, 2024
978e710
Fix soundness
cmcgee1024 Jul 14, 2024
aecbabb
Add end to end and integration tests
cmcgee1024 Jul 22, 2024
34f2bf2
Fix unnecessary data conversion for bytebuffer
cmcgee1024 Jul 22, 2024
34d9675
Fix compile warning/error
cmcgee1024 Jul 22, 2024
f4474c6
Use a test home directory on macOS where end-to-end is not possible i…
cmcgee1024 Jul 22, 2024
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
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import PackageDescription

let ghApiCacheResources = (1...27).map { Resource.embedInCode("gh-api-cache/swift-tags-page\($0).json") }

let package = Package(
name: "swiftly",
platforms: [
Expand Down Expand Up @@ -62,7 +64,11 @@ let package = Package(
),
.testTarget(
name: "SwiftlyTests",
dependencies: ["Swiftly"]
dependencies: ["Swiftly"],
resources: ghApiCacheResources + [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about embedInCode. Nice!

.embedInCode("gh-api-cache/swift-releases-page1.json"),
.embedInCode("mock-signing-key-private.pgp"),
]
),
]
)
30 changes: 18 additions & 12 deletions Sources/LinuxPlatform/Linux.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import SwiftlyCore

var swiftGPGKeysRefreshed = false

/// `Platform` implementation for Linux systems.
/// This implementation can be reused for any supported Linux platform.
/// TODO: replace dummy implementations
Expand Down Expand Up @@ -65,18 +67,22 @@ public struct Linux: Platform {
Self.skipVerificationMessage)
}

SwiftlyCore.print("Refreshing Swift PGP keys...")
do {
try self.runProgram(
"gpg",
"--quiet",
"--keyserver",
"hkp://keyserver.ubuntu.com",
"--refresh-keys",
"Swift"
)
} catch {
throw Error(message: "Failed to refresh PGP keys: \(error)")
// We only need to refresh the keys once per session, which will help with performance in tests
if !swiftGPGKeysRefreshed {
SwiftlyCore.print("Refreshing Swift PGP keys...")
do {
try self.runProgram(
"gpg",
"--quiet",
"--keyserver",
"hkp://keyserver.ubuntu.com",
"--refresh-keys",
"Swift"
)
} catch {
throw Error(message: "Failed to refresh PGP keys: \(error)")
}
swiftGPGKeysRefreshed = true
}
}

Expand Down
316 changes: 168 additions & 148 deletions Tests/SwiftlyTests/InstallTests.swift

Large diffs are not rendered by default.

188 changes: 179 additions & 9 deletions Tests/SwiftlyTests/SwiftlyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ class SwiftlyTests: XCTestCase {
}
}

override class func tearDown() {
#if os(Linux)
let deleteTestGPGKeys = Process()
deleteTestGPGKeys.executableURL = URL(fileURLWithPath: "/usr/bin/env")
deleteTestGPGKeys.arguments = [
"bash",
"-c",
"""
gpg --batch --yes --delete-secret-keys --fingerprint "A2A645E5249D25845C43954E7D210032D2F670B7" >/dev/null 2>&1
gpg --batch --yes --delete-keys --fingerprint "A2A645E5249D25845C43954E7D210032D2F670B7" >/dev/null 2>&1
""",
]
try? deleteTestGPGKeys.run()
deleteTestGPGKeys.waitUntilExit()
#endif
}

// Below are some constants that can be used to write test cases.
static let oldStable = ToolchainVersion(major: 5, minor: 6, patch: 0)
static let oldStableNewPatch = ToolchainVersion(major: 5, minor: 6, patch: 3)
Expand Down Expand Up @@ -451,30 +468,139 @@ private struct MockHTTPRequestExecutor: HTTPRequestExecutor {

/// An `HTTPRequestExecutor` which will return a mocked response to any toolchain download requests.
/// All other requests are performed using an actual HTTP client.
public struct MockToolchainDownloader: HTTPRequestExecutor {
public class MockToolchainDownloader: HTTPRequestExecutor {
private static let releaseURLRegex: Regex<(Substring, Substring, Substring, Substring?)> =
try! Regex("swift-(\\d+)\\.(\\d+)(?:\\.(\\d+))?-RELEASE")
private static let snapshotURLRegex: Regex<Substring> =
try! Regex("swift(?:-[0-9]+\\.[0-9]+)?-DEVELOPMENT-SNAPSHOT-[0-9]{4}-[0-9]{2}-[0-9]{2}")

private let executables: [String]
#if os(Linux)
private var signatures: [String: URL]
#endif
public let httpRequestExecutor: HTTPRequestExecutor

public init(executables: [String]? = nil, prevExecutor: HTTPRequestExecutor) {
self.executables = executables ?? ["swift"]
self.httpRequestExecutor = prevExecutor
#if os(Linux)
self.signatures = [:]
#endif
}

public func execute(_ request: HTTPClientRequest, timeout: TimeAmount) async throws -> HTTPClientResponse {
public func execute(_ request: HTTPClientRequest, timeout _: TimeAmount) async throws -> HTTPClientResponse {
guard let url = URL(string: request.url) else {
throw SwiftlyTestError(message: "invalid request URL: \(request.url)")
}

if url.host == "download.swift.org" {
return try self.makeToolchainDownloadResponse(from: url)
} else if url.host == "api.github.com" {
if url.path == "/repos/apple/swift/releases" {
return try self.makeGitHubReleasesAPIResponse(from: url)
} else if url.path == "/repos/apple/swift/tags" {
return try self.makeGitHubTagsAPIResponse(from: url)
} else {
throw SwiftlyTestError(message: "unxpected github API request URL: \(request.url)")
}
} else {
return try await self.httpRequestExecutor.execute(request, timeout: timeout)
throw SwiftlyTestError(message: "unmocked URL: \(request.url)")
}
}

private func makeGitHubReleasesAPIResponse(from url: URL) throws -> HTTPClientResponse {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
throw SwiftlyTestError(message: "unexpected github url: \(url)")
}

guard let queryItems = components.queryItems else {
return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(PackageResources.swift_releases_page1_json))))
}

guard let page = queryItems.first(where: { $0.name == "page" }) else {
return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(PackageResources.swift_releases_page1_json))))
}

if page.value != "1" {
return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(Array("[]".utf8)))))
}

return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(PackageResources.swift_releases_page1_json))))
}

private func makeGitHubTagsAPIResponse(from url: URL) throws -> HTTPClientResponse {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
throw SwiftlyTestError(message: "unexpected github url: \(url)")
}

guard let queryItems = components.queryItems else {
return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(PackageResources.swift_tags_page1_json))))
}

guard let page = queryItems.first(where: { $0.name == "page" }) else {
return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(PackageResources.swift_tags_page1_json))))
}

let payload = switch page.value {
case "1":
PackageResources.swift_tags_page1_json
case "2":
PackageResources.swift_tags_page2_json
case "3":
PackageResources.swift_tags_page3_json
case "4":
PackageResources.swift_tags_page4_json
case "5":
PackageResources.swift_tags_page5_json
case "6":
PackageResources.swift_tags_page6_json
case "7":
PackageResources.swift_tags_page7_json
case "8":
PackageResources.swift_tags_page8_json
case "9":
PackageResources.swift_tags_page9_json
case "10":
PackageResources.swift_tags_page10_json
case "11":
PackageResources.swift_tags_page11_json
case "12":
PackageResources.swift_tags_page12_json
case "13":
PackageResources.swift_tags_page13_json
case "14":
PackageResources.swift_tags_page14_json
case "15":
PackageResources.swift_tags_page15_json
case "16":
PackageResources.swift_tags_page16_json
case "17":
PackageResources.swift_tags_page17_json
case "18":
PackageResources.swift_tags_page18_json
case "19":
PackageResources.swift_tags_page19_json
case "20":
PackageResources.swift_tags_page20_json
case "21":
PackageResources.swift_tags_page21_json
case "22":
PackageResources.swift_tags_page22_json
case "23":
PackageResources.swift_tags_page23_json
case "24":
PackageResources.swift_tags_page24_json
case "25":
PackageResources.swift_tags_page25_json
case "26":
PackageResources.swift_tags_page26_json
case "27":
PackageResources.swift_tags_page27_json
default:
Array("[]".utf8)
}

return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(payload))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to convert to Data to then convert to ByteBuffer

Suggested change
return HTTPClientResponse(body: .bytes(ByteBuffer(data: Data(payload))))
return HTTPClientResponse(body: .bytes(ByteBuffer(bytes: payload)))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've fixed this in the latest commit.

}

private func makeToolchainDownloadResponse(from url: URL) throws -> HTTPClientResponse {
Expand All @@ -497,12 +623,22 @@ public struct MockToolchainDownloader: HTTPRequestExecutor {
throw SwiftlyTestError(message: "invalid toolchain download URL: \(url.path)")
}

let mockedToolchain = try self.makeMockedToolchain(toolchain: toolchain)
let mockedToolchain = try self.makeMockedToolchain(toolchain: toolchain, name: url.lastPathComponent)
return HTTPClientResponse(body: .bytes(ByteBuffer(data: mockedToolchain)))
}

#if os(Linux)
func makeMockedToolchain(toolchain: ToolchainVersion) throws -> Data {
func makeMockedToolchain(toolchain: ToolchainVersion, name: String) throws -> Data {
// Check our cache if this is a signature request
if name.hasSuffix(".sig") {
// Signatures will either be in the cache or they don't exist
guard let signature = self.signatures[toolchain.name] else {
throw SwiftlyTestError(message: "signature wasn't found in the cache")
}

return try Data(contentsOf: signature)
}

let tmp = FileManager.default.temporaryDirectory.appendingPathComponent("swiftly-\(UUID())")
let toolchainDir = tmp.appendingPathComponent("toolchain", isDirectory: true)
let toolchainBinDir = toolchainDir
Expand All @@ -513,9 +649,6 @@ public struct MockToolchainDownloader: HTTPRequestExecutor {
at: toolchainBinDir,
withIntermediateDirectories: true
)
defer {
try? FileManager.default.removeItem(at: tmp)
}

for executable in self.executables {
let executablePath = toolchainBinDir.appendingPathComponent(executable)
Expand All @@ -542,12 +675,49 @@ public struct MockToolchainDownloader: HTTPRequestExecutor {
try task.run()
task.waitUntilExit()

// Extra step involves generating a gpg signature and putting that in a cache for a later request. We will
// use a local key for this to avoid running into entropy problems in CI.
let gpgKeyFile = FileManager.default.temporaryDirectory.appendingPathComponent("swiftly-\(UUID())")
try Data(PackageResources.mock_signing_key_private_pgp).write(to: gpgKeyFile)
let importKey = Process()
importKey.executableURL = URL(fileURLWithPath: "/usr/bin/env")
importKey.arguments = ["bash", "-c", """
mkdir -p $HOME/.gnupg
touch $HOME/.gnupg/gpg.conf
gpg --batch --import \(gpgKeyFile.path) >/dev/null 2>&1 || echo -n
"""]
try importKey.run()
importKey.waitUntilExit()
if importKey.terminationStatus != 0 {
throw SwiftlyTestError(message: "unable to import test gpg signing key")
}

let detachSign = Process()
detachSign.executableURL = URL(fileURLWithPath: "/usr/bin/env")
detachSign.arguments = ["bash", "-c", """
export GPG_TTY=$(tty)
gpg --version | grep '2.0.' > /dev/null
if [ "$?" == "0" ]; then
gpg --default-key "A2A645E5249D25845C43954E7D210032D2F670B7" --detach-sign "\(archive.path)"
else
gpg --pinentry-mode loopback --default-key "A2A645E5249D25845C43954E7D210032D2F670B7" --detach-sign "\(archive.path)"
fi
"""]
try detachSign.run()
detachSign.waitUntilExit()

if detachSign.terminationStatus != 0 {
throw SwiftlyTestError(message: "unable to sign archive using the test user's gpg key")
}

self.signatures[toolchain.name] = archive.appendingPathExtension("sig")

return try Data(contentsOf: archive)
}

#elseif os(macOS)

func makeMockedToolchain(toolchain: ToolchainVersion) throws -> Data {
func makeMockedToolchain(toolchain: ToolchainVersion, name _: String) throws -> Data {
let tmp = FileManager.default.temporaryDirectory.appendingPathComponent("swiftly-\(UUID())")
let toolchainDir = tmp.appendingPathComponent("toolchain", isDirectory: true)
let toolchainBinDir = toolchainDir.appendingPathComponent("usr/bin", isDirectory: true)
Expand Down
Loading