InteroperabilityTestsExecutable.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. import ArgumentParser
  17. import GRPCCore
  18. import GRPCInteropTests
  19. import GRPCNIOTransportHTTP2
  20. @main
  21. struct InteroperabilityTestsExecutable: AsyncParsableCommand {
  22. static let configuration = CommandConfiguration(
  23. abstract: "gRPC Swift Interoperability Runner",
  24. subcommands: [StartServer.self, ListTests.self, RunTests.self]
  25. )
  26. struct StartServer: AsyncParsableCommand {
  27. static let configuration = CommandConfiguration(
  28. abstract: "Start the gRPC Swift interoperability test server."
  29. )
  30. @Option(help: "The port to listen on for new connections")
  31. var port: Int
  32. @Option(help: "The host to listen on for new connections")
  33. var host: String = "127.0.0.1"
  34. func run() async throws {
  35. let server = GRPCServer(
  36. transport: .http2NIOPosix(
  37. address: .ipv4(host: self.host, port: self.port),
  38. config: .defaults(transportSecurity: .plaintext) {
  39. $0.compression.enabledAlgorithms = .all
  40. }
  41. ),
  42. services: [TestService()]
  43. )
  44. try await withThrowingDiscardingTaskGroup { group in
  45. group.addTask {
  46. try await server.serve()
  47. }
  48. if let address = try await server.listeningAddress {
  49. print("listening address: \(address)")
  50. }
  51. }
  52. }
  53. }
  54. struct ListTests: ParsableCommand {
  55. static let configuration = CommandConfiguration(
  56. abstract: "List all interoperability test names."
  57. )
  58. func run() throws {
  59. for testCase in InteroperabilityTestCase.allCases {
  60. print(testCase.name)
  61. }
  62. }
  63. }
  64. struct RunTests: AsyncParsableCommand {
  65. static let configuration = CommandConfiguration(
  66. abstract: """
  67. Run gRPC interoperability tests using a gRPC Swift client.
  68. You can specify a test name as an argument to run a single test.
  69. If no test name is given, all interoperability tests will be run.
  70. """
  71. )
  72. @Option(help: "The host the server is running on")
  73. var host: String
  74. @Option(help: "The port to connect to")
  75. var port: Int
  76. @Argument(help: "The name of the tests to run. If none, all tests will be run.")
  77. var testNames: [String] = InteroperabilityTestCase.allCases.map { $0.name }
  78. func run() async throws {
  79. let client = try self.buildClient(host: self.host, port: self.port)
  80. try await withThrowingDiscardingTaskGroup { group in
  81. group.addTask {
  82. try await client.run()
  83. }
  84. for testName in testNames {
  85. guard let testCase = InteroperabilityTestCase(rawValue: testName) else {
  86. print(InteroperabilityTestError.testNotFound(name: testName))
  87. continue
  88. }
  89. await self.runTest(testCase, using: client)
  90. }
  91. client.beginGracefulShutdown()
  92. }
  93. }
  94. private func buildClient(host: String, port: Int) throws -> GRPCClient {
  95. let serviceConfig = ServiceConfig(loadBalancingConfig: [.roundRobin])
  96. return GRPCClient(
  97. transport: try .http2NIOPosix(
  98. target: .ipv4(host: host, port: port),
  99. config: .defaults(transportSecurity: .plaintext) {
  100. $0.compression.enabledAlgorithms = .all
  101. },
  102. serviceConfig: serviceConfig
  103. )
  104. )
  105. }
  106. private func runTest(
  107. _ testCase: InteroperabilityTestCase,
  108. using client: GRPCClient
  109. ) async {
  110. print("Running '\(testCase.name)' ... ", terminator: "")
  111. do {
  112. try await testCase.makeTest().run(client: client)
  113. print("PASSED")
  114. } catch {
  115. print("FAILED\n" + String(describing: InteroperabilityTestError.testFailed(cause: error)))
  116. }
  117. }
  118. }
  119. }
  120. enum InteroperabilityTestError: Error, CustomStringConvertible {
  121. case testNotFound(name: String)
  122. case testFailed(cause: any Error)
  123. var description: String {
  124. switch self {
  125. case .testNotFound(let name):
  126. return "Test \"\(name)\" not found."
  127. case .testFailed(let cause):
  128. return "Test failed with error: \(String(describing: cause))"
  129. }
  130. }
  131. }