|
| 1 | +/* |
| 2 | + * Copyright 2024, gRPC Authors All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import ArgumentParser |
| 18 | +import GRPCCore |
| 19 | +import GRPCInteropTests |
| 20 | +import GRPCNIOTransportHTTP2 |
| 21 | + |
| 22 | +@main |
| 23 | +struct InteroperabilityTestsExecutable: AsyncParsableCommand { |
| 24 | + static let configuration = CommandConfiguration( |
| 25 | + abstract: "gRPC Swift Interoperability Runner", |
| 26 | + subcommands: [StartServer.self, ListTests.self, RunTests.self] |
| 27 | + ) |
| 28 | + |
| 29 | + struct StartServer: AsyncParsableCommand { |
| 30 | + static let configuration = CommandConfiguration( |
| 31 | + abstract: "Start the gRPC Swift interoperability test server." |
| 32 | + ) |
| 33 | + |
| 34 | + @Option(help: "The port to listen on for new connections") |
| 35 | + var port: Int |
| 36 | + |
| 37 | + @Option(help: "The host to listen on for new connections") |
| 38 | + var host: String = "127.0.0.1" |
| 39 | + |
| 40 | + func run() async throws { |
| 41 | + let server = GRPCServer( |
| 42 | + transport: .http2NIOPosix( |
| 43 | + address: .ipv4(host: self.host, port: self.port), |
| 44 | + config: .defaults(transportSecurity: .plaintext) { |
| 45 | + $0.compression.enabledAlgorithms = .all |
| 46 | + } |
| 47 | + ), |
| 48 | + services: [TestService()] |
| 49 | + ) |
| 50 | + |
| 51 | + try await withThrowingDiscardingTaskGroup { group in |
| 52 | + group.addTask { |
| 53 | + try await server.serve() |
| 54 | + } |
| 55 | + |
| 56 | + if let address = try await server.listeningAddress { |
| 57 | + print("listening address: \(address)") |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + struct ListTests: ParsableCommand { |
| 64 | + static let configuration = CommandConfiguration( |
| 65 | + abstract: "List all interoperability test names." |
| 66 | + ) |
| 67 | + |
| 68 | + func run() throws { |
| 69 | + for testCase in InteroperabilityTestCase.allCases { |
| 70 | + print(testCase.name) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + struct RunTests: AsyncParsableCommand { |
| 76 | + static let configuration = CommandConfiguration( |
| 77 | + abstract: """ |
| 78 | + Run gRPC interoperability tests using a gRPC Swift client. |
| 79 | + You can specify a test name as an argument to run a single test. |
| 80 | + If no test name is given, all interoperability tests will be run. |
| 81 | + """ |
| 82 | + ) |
| 83 | + |
| 84 | + @Option(help: "The host the server is running on") |
| 85 | + var host: String |
| 86 | + |
| 87 | + @Option(help: "The port to connect to") |
| 88 | + var port: Int |
| 89 | + |
| 90 | + @Argument(help: "The name of the tests to run. If none, all tests will be run.") |
| 91 | + var testNames: [String] = InteroperabilityTestCase.allCases.map { $0.name } |
| 92 | + |
| 93 | + func run() async throws { |
| 94 | + let client = try self.buildClient(host: self.host, port: self.port) |
| 95 | + |
| 96 | + try await withThrowingDiscardingTaskGroup { group in |
| 97 | + group.addTask { |
| 98 | + try await client.run() |
| 99 | + } |
| 100 | + |
| 101 | + for testName in testNames { |
| 102 | + guard let testCase = InteroperabilityTestCase(rawValue: testName) else { |
| 103 | + print(InteroperabilityTestError.testNotFound(name: testName)) |
| 104 | + continue |
| 105 | + } |
| 106 | + await self.runTest(testCase, using: client) |
| 107 | + } |
| 108 | + |
| 109 | + client.beginGracefulShutdown() |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private func buildClient(host: String, port: Int) throws -> GRPCClient { |
| 114 | + let serviceConfig = ServiceConfig(loadBalancingConfig: [.roundRobin]) |
| 115 | + return GRPCClient( |
| 116 | + transport: try .http2NIOPosix( |
| 117 | + target: .ipv4(host: host, port: port), |
| 118 | + config: .defaults(transportSecurity: .plaintext) { |
| 119 | + $0.compression.enabledAlgorithms = .all |
| 120 | + }, |
| 121 | + serviceConfig: serviceConfig |
| 122 | + ) |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + private func runTest( |
| 127 | + _ testCase: InteroperabilityTestCase, |
| 128 | + using client: GRPCClient |
| 129 | + ) async { |
| 130 | + print("Running '\(testCase.name)' ... ", terminator: "") |
| 131 | + do { |
| 132 | + try await testCase.makeTest().run(client: client) |
| 133 | + print("PASSED") |
| 134 | + } catch { |
| 135 | + print("FAILED\n" + String(describing: InteroperabilityTestError.testFailed(cause: error))) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +enum InteroperabilityTestError: Error, CustomStringConvertible { |
| 142 | + case testNotFound(name: String) |
| 143 | + case testFailed(cause: any Error) |
| 144 | + |
| 145 | + var description: String { |
| 146 | + switch self { |
| 147 | + case .testNotFound(let name): |
| 148 | + return "Test \"\(name)\" not found." |
| 149 | + case .testFailed(let cause): |
| 150 | + return "Test failed with error: \(String(describing: cause))" |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments