|
| 1 | +package org.encalmo.lambda |
| 2 | + |
| 3 | +import com.sun.net.httpserver.{HttpContext, HttpExchange, HttpHandler, HttpServer} |
| 4 | +import munit.Assertions |
| 5 | +import upickle.default.* |
| 6 | + |
| 7 | +import java.net.InetSocketAddress |
| 8 | +import java.util.UUID |
| 9 | +import java.util.concurrent.LinkedBlockingQueue |
| 10 | +import scala.concurrent.ExecutionContext.Implicits.given |
| 11 | +import scala.concurrent.{Future, Promise} |
| 12 | +import scala.io.Source |
| 13 | +import scala.jdk.CollectionConverters.* |
| 14 | +import scala.reflect.ClassTag |
| 15 | +import scala.util.{Failure, Success, Try} |
| 16 | + |
| 17 | +case class LambdaError( |
| 18 | + success: Boolean, |
| 19 | + errorMessage: String, |
| 20 | + error: String |
| 21 | +) derives ReadWriter |
| 22 | + |
| 23 | +class LambdaServiceFixture extends munit.Fixture[LambdaService]("lambdaService") { |
| 24 | + |
| 25 | + private var server: HttpServer = null |
| 26 | + private var lambdaService: LambdaService = null |
| 27 | + |
| 28 | + override def apply(): LambdaService = |
| 29 | + if (lambdaService != null) |
| 30 | + then lambdaService |
| 31 | + else throw new Exception("Test http server not initialized!") |
| 32 | + |
| 33 | + final override def beforeAll(): Unit = { |
| 34 | + this.server = HttpServer.create(new InetSocketAddress(0), 0) |
| 35 | + val address = s"localhost:${server.getAddress().getPort()}" |
| 36 | + server.start() |
| 37 | + lambdaService = new LambdaService(server) |
| 38 | + println( |
| 39 | + s"Started test http server at port ${server.getAddress().getPort()}" |
| 40 | + ) |
| 41 | + System.setProperty("AWS_LAMBDA_RUNTIME_API", address) |
| 42 | + System.setProperty("AWS_LAMBDA_FUNCTION_NAME", "TestFunction") |
| 43 | + System.setProperty("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST") |
| 44 | + System.setProperty("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128") |
| 45 | + System.setProperty( |
| 46 | + "AWS_LAMBDA_LOG_GROUP_NAME", |
| 47 | + "/aws/lambda/TestFunction" |
| 48 | + ) |
| 49 | + System.setProperty( |
| 50 | + "AWS_LAMBDA_LOG_STREAM_NAME", |
| 51 | + "/aws/lambda/TestFunction/$LATEST" |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + final def close(): Unit = { |
| 56 | + server.stop(0) |
| 57 | + println( |
| 58 | + s"Shutdown test http server at port ${server.getAddress().getPort()}" |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +class LambdaService(server: HttpServer) extends Assertions { |
| 65 | + |
| 66 | + private val events = |
| 67 | + new LinkedBlockingQueue[(String, String)] |
| 68 | + |
| 69 | + server.createContext( |
| 70 | + "/2018-06-01/runtime/invocation/next", |
| 71 | + (exchange: HttpExchange) => { |
| 72 | + val (requestId, input) = events.take() |
| 73 | + |
| 74 | + val responseHeaders = exchange.getResponseHeaders() |
| 75 | + responseHeaders.put( |
| 76 | + "Lambda-Runtime-Aws-Request-Id", |
| 77 | + Seq(requestId).asJava |
| 78 | + ) |
| 79 | + responseHeaders.put( |
| 80 | + "Lambda-Runtime-Deadline-Ms", |
| 81 | + Seq("30000").asJava |
| 82 | + ) |
| 83 | + responseHeaders.put( |
| 84 | + "Lambda-Runtime-Invoked-Function-Arn", |
| 85 | + Seq( |
| 86 | + "arn:aws:lambda:us-east-1:00000000:function:TestFunction" |
| 87 | + ).asJava |
| 88 | + ) |
| 89 | + responseHeaders.put( |
| 90 | + "Lambda-Runtime-Trace-Id", |
| 91 | + Seq( |
| 92 | + "Root=1-5bef4de7-ad49b0e87f6ef6c87fc2e700;Parent=9a9197af755a6419;Sampled=1" |
| 93 | + ).asJava |
| 94 | + ) |
| 95 | + exchange.sendResponseHeaders(200, input.length()) |
| 96 | + val os = exchange.getResponseBody() |
| 97 | + os.write(input.getBytes()) |
| 98 | + os.close() |
| 99 | + } |
| 100 | + ) |
| 101 | + |
| 102 | + server.createContext( |
| 103 | + "/2018-06-01/runtime/init/error", |
| 104 | + (exchange: HttpExchange) => { |
| 105 | + println(s"[LambdaService] Initialization error: ${Source |
| 106 | + .fromInputStream(exchange.getRequestBody()) |
| 107 | + .mkString}") |
| 108 | + exchange.sendResponseHeaders(202, -1) |
| 109 | + } |
| 110 | + ) |
| 111 | + |
| 112 | + def mockAndAssertLambdaInvocation( |
| 113 | + input: String, |
| 114 | + expectedOutput: String |
| 115 | + ): Future[String] = { |
| 116 | + // println(s"[LambdaService] Expecting: $input => $expectedOutput") |
| 117 | + val requestId = UUID.randomUUID().toString() |
| 118 | + events.offer((requestId, input)) |
| 119 | + mockRuntimeInvocationResponse(requestId) |
| 120 | + .andThen { |
| 121 | + case Success(result) => |
| 122 | + assertEquals(result, expectedOutput) |
| 123 | + println( |
| 124 | + // s"[LambdaService] Success confirmed: $result == $expectedOutput" |
| 125 | + ) |
| 126 | + case Failure(exception) => |
| 127 | + fail( |
| 128 | + "[LambdaService] Invocation didn't return any result", |
| 129 | + exception |
| 130 | + ) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + def mockAndAssertLambdaInvocationError[I: ReadWriter]( |
| 135 | + input: I, |
| 136 | + expectedError: LambdaError |
| 137 | + ): Future[LambdaError] = { |
| 138 | + println(s"[LambdaService] Expecting failure: $input => $expectedError") |
| 139 | + val requestId = UUID.randomUUID().toString() |
| 140 | + val event = ujson.write(writeJs(input)) |
| 141 | + events.offer((requestId, event)) |
| 142 | + mockRuntimeInvocationError(requestId) |
| 143 | + .andThen { |
| 144 | + case Success(error) => |
| 145 | + assertEquals(error.errorMessage, expectedError.errorMessage) |
| 146 | + assertEquals(error.error, expectedError.error) |
| 147 | + println( |
| 148 | + s"[LambdaService] Error confirmed: ${error.errorMessage} == ${expectedError.errorMessage}" |
| 149 | + ) |
| 150 | + case Failure(exception) => |
| 151 | + fail( |
| 152 | + "[LambdaService] Invocation didn't return any error", |
| 153 | + exception |
| 154 | + ) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + def mockRuntimeInvocationResponse( |
| 159 | + requestId: String |
| 160 | + ): Future[String] = { |
| 161 | + var httpContext: HttpContext = null |
| 162 | + var errorHttpContext: HttpContext = null |
| 163 | + val promise = Promise[String] |
| 164 | + |
| 165 | + httpContext = server.createContext( |
| 166 | + s"/2018-06-01/runtime/invocation/$requestId/response", |
| 167 | + (exchange: HttpExchange) => { |
| 168 | + promise.complete(Try { |
| 169 | + val body = Source |
| 170 | + .fromInputStream(exchange.getRequestBody()) |
| 171 | + .mkString |
| 172 | + exchange |
| 173 | + .sendResponseHeaders(202, -1) |
| 174 | + server.removeContext(httpContext) |
| 175 | + server.removeContext(errorHttpContext) |
| 176 | + body |
| 177 | + }) |
| 178 | + } |
| 179 | + ) |
| 180 | + errorHttpContext = server.createContext( |
| 181 | + s"/2018-06-01/runtime/invocation/$requestId/error", |
| 182 | + (exchange: HttpExchange) => { |
| 183 | + val body = Source |
| 184 | + .fromInputStream(exchange.getRequestBody()) |
| 185 | + .mkString |
| 186 | + exchange.sendResponseHeaders(202, -1) |
| 187 | + server.removeContext(httpContext) |
| 188 | + server.removeContext(errorHttpContext) |
| 189 | + promise.complete( |
| 190 | + Failure( |
| 191 | + new Exception( |
| 192 | + s"Expected success but got an error:\n$body" |
| 193 | + ) |
| 194 | + ) |
| 195 | + ) |
| 196 | + } |
| 197 | + ) |
| 198 | + promise.future |
| 199 | + } |
| 200 | + |
| 201 | + def mockRuntimeInvocationError( |
| 202 | + requestId: String |
| 203 | + ): Future[LambdaError] = { |
| 204 | + var httpContext: HttpContext = null |
| 205 | + var errorHttpContext: HttpContext = null |
| 206 | + val promise = Promise[LambdaError] |
| 207 | + httpContext = server.createContext( |
| 208 | + s"/2018-06-01/runtime/invocation/$requestId/response", |
| 209 | + (exchange: HttpExchange) => { |
| 210 | + val body = Source |
| 211 | + .fromInputStream(exchange.getRequestBody()) |
| 212 | + .mkString |
| 213 | + exchange.sendResponseHeaders(202, -1) |
| 214 | + server.removeContext(httpContext) |
| 215 | + server.removeContext(errorHttpContext) |
| 216 | + promise.complete( |
| 217 | + Failure( |
| 218 | + new Exception( |
| 219 | + s"Expected an error but got successful result:\n$body" |
| 220 | + ) |
| 221 | + ) |
| 222 | + ) |
| 223 | + } |
| 224 | + ) |
| 225 | + |
| 226 | + errorHttpContext = server.createContext( |
| 227 | + s"/2018-06-01/runtime/invocation/$requestId/error", |
| 228 | + (exchange: HttpExchange) => { |
| 229 | + promise.complete(Try { |
| 230 | + val body = Source |
| 231 | + .fromInputStream(exchange.getRequestBody()) |
| 232 | + .mkString |
| 233 | + exchange.sendResponseHeaders(202, -1) |
| 234 | + server.removeContext(httpContext) |
| 235 | + server.removeContext(errorHttpContext) |
| 236 | + upickle.default.read[LambdaError](body) |
| 237 | + }) |
| 238 | + } |
| 239 | + ) |
| 240 | + promise.future |
| 241 | + } |
| 242 | +} |
0 commit comments