InteroperabilityTestsExecutable.swift 4.7 KB

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