Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): return {"status":"OK"} ack body from runtime invocation endpoints (#1418)
Stuart Langdace94fa804d
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.javaindex 6a1115c7b4..9e5d2095ea 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java@@ -36,6 +36,11 @@ public class RuntimeApiServer {private static final byte[] CONTAINER_STOPPED_PAYLOAD ="{\"errorMessage\":\"Container stopped\",\"errorType\":\"ContainerStopped\"}".getBytes();+ // Acknowledgement body for the /response, /error and /init/error endpoints. Some+ // runtime clients (e.g. .NET's Amazon.Lambda.RuntimeSupport) deserialize it and+ // fail on an empty body.+ private static final String STATUS_OK_BODY = "{\"status\":\"OK\"}";+private final Vertx vertx;private final int port;@@ -110,7 +115,7 @@ public class RuntimeApiServer {InvokeResult result = new InvokeResult(200, null, payload, null, requestId);invocation.getResultFuture().complete(result);}- ctx.response().setStatusCode(202).end();+ sendStatusOk(ctx);});// POST /runtime/invocation/{requestId}/error — failure@@ -124,13 +129,13 @@ public class RuntimeApiServer {InvokeResult result = new InvokeResult(200, functionError, payload, null, requestId);invocation.getResultFuture().complete(result);}- ctx.response().setStatusCode(202).end();+ sendStatusOk(ctx);});// POST /runtime/init/error — runtime initialization failurerouter.post(INIT_ERROR_PATH).handler(ctx -> {LOG.warnv("Lambda runtime reported init error on port {0}", String.valueOf(port));- ctx.response().setStatusCode(202).end();+ sendStatusOk(ctx);});long deadline = System.currentTimeMillis() + 5000;@@ -237,6 +242,13 @@ public class RuntimeApiServer {return invocation.getResultFuture();}+ private void sendStatusOk(RoutingContext ctx) {+ ctx.response()+ .setStatusCode(202)+ .putHeader("Content-Type", "application/json")+ .end(STATUS_OK_BODY);+ }+private void sendInvocation(RoutingContext ctx, PendingInvocation invocation) {inFlight.put(invocation.getRequestId(), invocation);diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.javaindex ec916f724d..fed89321ac 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.lambda.runtime;import io.github.hectorvent.floci.services.lambda.model.InvokeResult;import io.github.hectorvent.floci.services.lambda.model.PendingInvocation;import io.vertx.core.Vertx;+import io.vertx.core.json.JsonObject;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;@@ -138,6 +139,74 @@ class RuntimeApiServerTest {assertEquals("req-parked", response.headers().firstValue("Lambda-Runtime-Aws-Request-Id").orElse(""));}+ /**+ * The /error endpoint must return HTTP 202 with a {@code {"status":"OK"}} body, not+ * an empty body. The AWS .NET runtime client (Amazon.Lambda.RuntimeSupport)+ * deserializes the acknowledgement and crashes the runtime process with "Could not+ * deserialize the response body" when it is empty.+ */+ @Test+ @Timeout(15)+ void errorEndpoint_returns202WithStatusOkBody() throws Exception {+ PendingInvocation invocation = new PendingInvocation(+ "req-error", "{}".getBytes(), System.currentTimeMillis() + 60_000,+ "arn:aws:lambda:us-east-1:000000000000:function:test",+ new CompletableFuture<>());+ server.enqueue(invocation);++ // Deliver the invocation to a /next poller so it moves to inFlight.+ httpClient.send(HttpRequest.newBuilder()+ .uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))+ .GET().build(),+ HttpResponse.BodyHandlers.ofString());++ HttpResponse<String> response = httpClient.send(HttpRequest.newBuilder()+ .uri(URI.create("http://localhost:" + port+ + "/2018-06-01/runtime/invocation/req-error/error"))+ .header("Lambda-Runtime-Function-Error-Type", "Function.Handled")+ .POST(HttpRequest.BodyPublishers.ofString(+ "{\"errorMessage\":\"intentional failure\"}"))+ .build(),+ HttpResponse.BodyHandlers.ofString());++ assertEquals(202, response.statusCode());+ assertEquals("application/json",+ response.headers().firstValue("Content-Type").orElse(""));+ assertEquals("OK", new JsonObject(response.body()).getString("status"),+ "/error must return a JSON ack body so the .NET runtime client can deserialize it");+ }++ /**+ * The /response acknowledgement carries the same {@code {"status":"OK"}} body as+ * /error so runtime clients that deserialize it succeed.+ */+ @Test+ @Timeout(15)+ void responseEndpoint_returns202WithStatusOkBody() throws Exception {+ PendingInvocation invocation = new PendingInvocation(+ "req-response", "{}".getBytes(), System.currentTimeMillis() + 60_000,+ "arn:aws:lambda:us-east-1:000000000000:function:test",+ new CompletableFuture<>());+ server.enqueue(invocation);++ httpClient.send(HttpRequest.newBuilder()+ .uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))+ .GET().build(),+ HttpResponse.BodyHandlers.ofString());++ HttpResponse<String> response = httpClient.send(HttpRequest.newBuilder()+ .uri(URI.create("http://localhost:" + port+ + "/2018-06-01/runtime/invocation/req-response/response"))+ .POST(HttpRequest.BodyPublishers.ofString("\"result\""))+ .build(),+ HttpResponse.BodyHandlers.ofString());++ assertEquals(202, response.statusCode());+ assertEquals("application/json",+ response.headers().firstValue("Content-Type").orElse(""));+ assertEquals("OK", new JsonObject(response.body()).getString("status"));+ }+@Test@Timeout(15)void stopCompletesInFlightWithContainerStopped() throws Exception {