main.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import GRPC
  18. import NIO
  19. import NIOSSL
  20. import GRPCInteroperabilityTests
  21. import Commander
  22. enum InteroperabilityTestError: LocalizedError {
  23. case testNotFound(String)
  24. case testFailed(Error)
  25. var errorDescription: String? {
  26. switch self {
  27. case .testNotFound(let name):
  28. return "No test named '\(name)' was found"
  29. case .testFailed(let error):
  30. return "Test failed with error: \(error)"
  31. }
  32. }
  33. }
  34. /// Runs the test instance using the given connection.
  35. ///
  36. /// Success or failure is indicated by the lack or presence of thrown errors, respectively.
  37. ///
  38. /// - Parameters:
  39. /// - instance: `InteroperabilityTest` instance to run.
  40. /// - name: the name of the test, use for logging only.
  41. /// - connection: client connection to use for running the test.
  42. /// - Throws: `InteroperabilityTestError` if the test fails.
  43. func runTest(_ instance: InteroperabilityTest, name: String, connection: ClientConnection) throws {
  44. do {
  45. print("Running '\(name)' ... ", terminator: "")
  46. try instance.run(using: connection)
  47. print("PASSED")
  48. } catch {
  49. print("FAILED")
  50. throw InteroperabilityTestError.testFailed(error)
  51. }
  52. }
  53. /// Creates a new `InteroperabilityTest` instance with the given name, or throws an
  54. /// `InteroperabilityTestError` if no test matches the given name. Implemented test names can be
  55. /// found by running the `list_tests` target.
  56. func makeRunnableTest(name: String) throws -> InteroperabilityTest {
  57. guard let testCase = InteroperabilityTestCase(rawValue: name) else {
  58. throw InteroperabilityTestError.testNotFound(name)
  59. }
  60. return testCase.makeTest()
  61. }
  62. /// Runs the given block and exits with code 1 if the block throws an error.
  63. ///
  64. /// The "Commander" CLI elides thrown errors in favour of its own. This function is intended purely
  65. /// to work around this limitation by printing any errors before exiting.
  66. func exitOnThrow<T>(block: () throws -> T) -> T {
  67. do {
  68. return try block()
  69. } catch {
  70. print(error)
  71. exit(1)
  72. }
  73. }
  74. // MARK: - Command line options and "main".
  75. let serverHostOption = Option(
  76. "server_host",
  77. default: "localhost",
  78. description: "The server host to connect to.")
  79. let serverPortOption = Option(
  80. "server_port",
  81. default: 8080,
  82. description: "The server port to connect to.")
  83. let testCaseOption = Option(
  84. "test_case",
  85. default: InteroperabilityTestCase.emptyUnary.name,
  86. description: "The name of the test case to execute.")
  87. /// The spec requires a string (as opposed to having a flag) to indicate whether TLS is enabled or
  88. /// disabled.
  89. let useTLSOption = Option(
  90. "use_tls",
  91. default: "false",
  92. description: "Whether to use an encrypted or plaintext connection (true|false).") { value in
  93. let lowercased = value.lowercased()
  94. switch lowercased {
  95. case "true", "false":
  96. return lowercased
  97. default:
  98. throw ArgumentError.invalidType(value: value, type: "boolean", argument: "use_tls")
  99. }
  100. }
  101. let portOption = Option(
  102. "port",
  103. default: 8080,
  104. description: "The port to listen on.")
  105. let group = Group { group in
  106. group.command(
  107. "run_test",
  108. serverHostOption,
  109. serverPortOption,
  110. useTLSOption,
  111. testCaseOption,
  112. description: "Run a single test. See 'list_tests' for available test names."
  113. ) { host, port, useTLS, testCaseName in
  114. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  115. defer {
  116. try? eventLoopGroup.syncShutdownGracefully()
  117. }
  118. exitOnThrow {
  119. let instance = try makeRunnableTest(name: testCaseName)
  120. let connection = try makeInteroperabilityTestClientConnection(
  121. host: host,
  122. port: port,
  123. eventLoopGroup: eventLoopGroup,
  124. useTLS: useTLS == "true")
  125. try runTest(instance, name: testCaseName, connection: connection)
  126. }
  127. }
  128. group.command(
  129. "start_server",
  130. portOption,
  131. useTLSOption,
  132. description: "Starts the test server."
  133. ) { port, useTls in
  134. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  135. defer {
  136. try? eventLoopGroup.syncShutdownGracefully()
  137. }
  138. let server = exitOnThrow {
  139. return try makeInteroperabilityTestServer(
  140. host: "localhost",
  141. port: port,
  142. eventLoopGroup: eventLoopGroup,
  143. useTLS: useTls == "true")
  144. }
  145. server.map { $0.channel.localAddress?.port }.whenSuccess {
  146. print("Server started on port \($0!)")
  147. }
  148. // We never call close; run until we get killed.
  149. try server.flatMap { $0.onClose }.wait()
  150. }
  151. group.command(
  152. "list_tests",
  153. description: "List available test case names."
  154. ) {
  155. InteroperabilityTestCase.allCases.forEach {
  156. print($0.name)
  157. }
  158. }
  159. }
  160. group.run()