InteroperabilityTestsExecutable.swift 4.3 KB

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