InteroperabilityTestsExecutable.swift 4.5 KB

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