mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): pass ClientContext to synchronous invocations (#5014)
Emit Lambda-Runtime-Client-Context from the decoded X-Amz-Client-Context header. * Fix #4968 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
ae0a591bb1
commit
b112c62949
9 files changed
+198
-12
No files matched your search
+19
@@ -177,6 +177,25 @@ class LambdaExecutorServiceTest {
|
||||
assertTrue(payload.contains("1 seconds"), "payload should contain timeout value");
|
||||
}
|
||||
|
||||
@Test
|
||||
void syncInvoke_clientContext_isPlacedOnTheEnqueuedInvocation() {
|
||||
RuntimeApiServer rtas = mock(RuntimeApiServer.class);
|
||||
ContainerHandle handle = new ContainerHandle("cid-ctx", "test-fn", rtas, ContainerState.WARM);
|
||||
when(warmPool.acquire(any())).thenReturn(handle);
|
||||
AtomicReference<String> seen = new AtomicReference<>();
|
||||
doAnswer(inv -> {
|
||||
PendingInvocation pi = inv.getArgument(0);
|
||||
seen.set(pi.getClientContext());
|
||||
pi.getResultFuture().complete(new InvokeResult(200, null, "{}".getBytes(), null, "req-ctx"));
|
||||
return pi.getResultFuture();
|
||||
}).when(rtas).enqueue(any(PendingInvocation.class));
|
||||
|
||||
String clientContext = "{\"custom\":{\"traceparent\":\"00-abc-def-01\"}}";
|
||||
executor.invoke(fn, "{}".getBytes(), InvocationType.RequestResponse, 0, null, clientContext);
|
||||
|
||||
assertEquals(clientContext, seen.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void successfulInvocation_releasesHandle_doesNotDestroy() {
|
||||
RuntimeApiServer rtas = mock(RuntimeApiServer.class);
|
||||
|
||||
@@ -135,6 +135,20 @@ class LambdaIntegrationTest {
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void syncInvoke_malformedClientContext_returns400() {
|
||||
given()
|
||||
.header("X-Amz-Client-Context", "not-base64-json!")
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post(BASE_PATH + "/functions/hello-world/invocations")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidRequestContentException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void createFunctionMissingRole_returns400() {
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class LambdaInvokeClientContextIntegrationTest {
|
||||
|
||||
private static final String CONTEXT_JSON = "{\"custom\":{\"traceparent\":\"00-abc-def-01\"}}";
|
||||
|
||||
@InjectMock
|
||||
LambdaService lambdaService;
|
||||
|
||||
|
||||
private String encodedContext() {
|
||||
String pretty = "{ \"custom\": { \"traceparent\": \"00-abc-def-01\" } }";
|
||||
return Base64.getEncoder().encodeToString(pretty.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void stubInvoke() {
|
||||
when(lambdaService.invoke(any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new InvokeResult(200, null, "{}".getBytes(), null, "req"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestResponse_validClientContext_isDecodedAndPassedToTheService() {
|
||||
stubInvoke();
|
||||
|
||||
given()
|
||||
.header("X-Amz-Client-Context", encodedContext())
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when().post("/2015-03-31/functions/ctx-fn/invocations")
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn"), any(), any(),
|
||||
eq(InvocationType.RequestResponse), eq(CONTEXT_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
void event_clientContext_isNotPassedToTheService() {
|
||||
stubInvoke();
|
||||
|
||||
given()
|
||||
.header("X-Amz-Invocation-Type", "Event")
|
||||
.header("X-Amz-Client-Context", encodedContext())
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when().post("/2015-03-31/functions/ctx-fn-event/invocations")
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn-event"), any(), any(),
|
||||
eq(InvocationType.Event), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestResponse_nonAsciiClientContext_isEscapedForTheHeader() {
|
||||
stubInvoke();
|
||||
String raw = "{\"custom\":{\"name\":\"café\"}}";
|
||||
|
||||
given()
|
||||
.header("X-Amz-Client-Context",
|
||||
Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8)))
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when().post("/2015-03-31/functions/ctx-fn-utf/invocations")
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn-utf"), any(), any(),
|
||||
eq(InvocationType.RequestResponse), eq("{\"custom\":{\"name\":\"caf\\u00E9\"}}"));
|
||||
}
|
||||
}
|
||||
+25
@@ -109,6 +109,31 @@ class RuntimeApiServerTest {
|
||||
assertTrue(response.body().contains("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void nextEndpoint_emitsClientContextHeaderOnlyWhenPresent() throws Exception {
|
||||
String clientContext = "{\"custom\":{\"traceparent\":\"00-abc-def-01\"}}";
|
||||
PendingInvocation withContext = new PendingInvocation(
|
||||
"req-ctx", "{}".getBytes(), System.currentTimeMillis() + 60_000,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
new CompletableFuture<>());
|
||||
withContext.setClientContext(clientContext);
|
||||
server.enqueue(withContext);
|
||||
|
||||
HttpRequest next = HttpRequest.newBuilder()
|
||||
.uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))
|
||||
.GET().build();
|
||||
HttpResponse<String> first = httpClient.send(next, HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(clientContext, first.headers().firstValue("Lambda-Runtime-Client-Context").orElse(null));
|
||||
|
||||
server.enqueue(new PendingInvocation(
|
||||
"req-noctx", "{}".getBytes(), System.currentTimeMillis() + 60_000,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
new CompletableFuture<>()));
|
||||
HttpResponse<String> second = httpClient.send(next, HttpResponse.BodyHandlers.ofString());
|
||||
assertTrue(second.headers().firstValue("Lambda-Runtime-Client-Context").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void nextEndpoint_startsDeadlineWhenInvocationIsDispatched() throws Exception {
|
||||
|
||||
Reference in new issue
Block a user