mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): honor alias async destination configuration (#4348)
* fix: route Lambda async destinations by invoked alias * test: verify qualified Lambda ARN executor calls * test: cover real alias destination routing
This commit is contained in:
1 parent
6fcfc0450a
commit
09276c75dc
8 files changed
+146
-42
No files matched your search
+45
-1
@@ -1,13 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import java.time.Instant;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.FunctionEventInvokeConfig;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
|
||||
import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
@@ -20,6 +23,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -339,6 +344,45 @@ class AsyncInvokeDestinationRouterTest {
|
||||
assertEquals("3", detail.path("responseContext").path("executedVersion").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void realAliasConfigDeliversToAliasBusAndFallsBackToVersionBus() {
|
||||
String versionBusArn = "arn:aws:events:us-east-1:000000000000:event-bus/version-bus";
|
||||
LambdaService realService = new LambdaService(
|
||||
new LambdaFunctionStore(new InMemoryStorage<>()),
|
||||
new WarmPool(),
|
||||
new CodeStore(Path.of("target/test-data/lambda-code")),
|
||||
new ZipExtractor(),
|
||||
new RegionResolver("us-east-1", "000000000000"));
|
||||
realService.createFunction("us-east-1", Map.of(
|
||||
"FunctionName", "bank-pawnshop",
|
||||
"PackageType", "Image",
|
||||
"Role", "arn:aws:iam::000000000000:role/test-role",
|
||||
"Code", Map.of("ImageUri", "public.ecr.aws/lambda/nodejs:20")));
|
||||
LambdaFunction version = realService.publishVersion("us-east-1", "bank-pawnshop", null);
|
||||
realService.putEventInvokeConfig("us-east-1", "bank-pawnshop", version.getVersion(), Map.of(
|
||||
"DestinationConfig", Map.of("OnSuccess", Map.of("Destination", versionBusArn))));
|
||||
realService.putEventInvokeConfig("us-east-1", "bank-pawnshop", "prod", Map.of(
|
||||
"DestinationConfig", Map.of("OnSuccess", Map.of("Destination", BUS_ARN))));
|
||||
AsyncInvokeDestinationRouter realRouter = new AsyncInvokeDestinationRouter(
|
||||
instanceOf(realService), instanceOf(eventBridgeService), instanceOf(sqsService),
|
||||
instanceOf(snsService), MAPPER, config);
|
||||
|
||||
realRouter.route(version, request(), success("{}"), 0, "prod");
|
||||
realRouter.route(version, request(), success("{}"), 0, "other");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<Map<String, Object>>> entries = ArgumentCaptor.forClass(List.class);
|
||||
verify(eventBridgeService, times(2)).putEvents(entries.capture(), eq("us-east-1"), eq(null));
|
||||
JsonNode aliasEntry = MAPPER.valueToTree(entries.getAllValues().get(0).get(0));
|
||||
JsonNode versionEntry = MAPPER.valueToTree(entries.getAllValues().get(1).get(0));
|
||||
assertEquals(BUS_ARN, aliasEntry.path("EventBusName").asText());
|
||||
assertEquals(versionBusArn, versionEntry.path("EventBusName").asText());
|
||||
assertEquals(version.getFunctionArn(), detailOf(aliasEntry)
|
||||
.path("requestContext").path("functionArn").asText());
|
||||
assertEquals(version.getVersion(), detailOf(aliasEntry)
|
||||
.path("responseContext").path("executedVersion").asText());
|
||||
}
|
||||
|
||||
private void configure(String onSuccess, String onFailure) {
|
||||
FunctionEventInvokeConfig config = new FunctionEventInvokeConfig();
|
||||
FunctionEventInvokeConfig.DestinationConfig destinations =
|
||||
|
||||
+9
-7
@@ -20,6 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.AdditionalMatchers.aryEq;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.ArgumentMatchers.nullable;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -48,7 +50,7 @@ class LambdaArnInvocationAccountTest {
|
||||
|
||||
LambdaExecutorService executor = mock(LambdaExecutorService.class);
|
||||
InvokeResult executorResult = new InvokeResult();
|
||||
when(executor.invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
|
||||
when(executor.invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull()))
|
||||
.thenReturn(executorResult);
|
||||
LambdaService service = new LambdaService(
|
||||
store,
|
||||
@@ -76,7 +78,7 @@ class LambdaArnInvocationAccountTest {
|
||||
InvokeResult result = service.invokeArn(functionArn, "{}".getBytes(), InvocationType.Event);
|
||||
|
||||
assertEquals("$LATEST", result.getExecutedVersion());
|
||||
verify(executor).invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0));
|
||||
verify(executor).invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,7 +113,7 @@ class LambdaArnInvocationAccountTest {
|
||||
"alias::" + region + "::" + functionName + "::live", alias);
|
||||
|
||||
LambdaExecutorService executor = mock(LambdaExecutorService.class);
|
||||
when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
|
||||
when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), nullable(String.class)))
|
||||
.thenAnswer(ignored -> new InvokeResult());
|
||||
LambdaService service = new LambdaService(
|
||||
functionStore,
|
||||
@@ -143,8 +145,8 @@ class LambdaArnInvocationAccountTest {
|
||||
|
||||
assertEquals("7", versionResult.getExecutedVersion());
|
||||
assertEquals("7", aliasResult.getExecutedVersion());
|
||||
verify(executor, org.mockito.Mockito.times(2))
|
||||
.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0));
|
||||
verify(executor).invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), eq("7"));
|
||||
verify(executor).invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), eq("live"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -178,9 +180,9 @@ class LambdaArnInvocationAccountTest {
|
||||
new AccountAwareStorageBackend<>(rawAliases, null, defaultAccount));
|
||||
|
||||
LambdaExecutorService executor = mock(LambdaExecutorService.class);
|
||||
when(executor.invoke(eq(latest), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
|
||||
when(executor.invoke(eq(latest), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull()))
|
||||
.thenAnswer(ignored -> new InvokeResult());
|
||||
when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
|
||||
when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), nullable(String.class)))
|
||||
.thenAnswer(ignored -> new InvokeResult());
|
||||
LambdaService service = service(functionStore, aliasStore, executor, region, defaultAccount);
|
||||
|
||||
|
||||
+22
@@ -145,4 +145,26 @@ class LambdaEventInvokeConfigTest {
|
||||
assertEquals("arn:aws:sqs:us-east-1:000000000000:dlq",
|
||||
cfg.getDestinationConfig().getOnFailure().getDestination());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokedAliasConfigTakesPrecedenceAndFallsBackToExecutedVersion() {
|
||||
String versionDestination = "arn:aws:sqs:us-east-1:000000000000:version-queue";
|
||||
String aliasDestination = "arn:aws:sqs:us-east-1:000000000000:alias-queue";
|
||||
service.putEventInvokeConfig(REGION, "test-fn", "1", Map.of(
|
||||
"DestinationConfig", Map.of("OnSuccess", Map.of("Destination", versionDestination))));
|
||||
service.putEventInvokeConfig(REGION, "test-fn", "prod", Map.of(
|
||||
"DestinationConfig", Map.of("OnSuccess", Map.of("Destination", aliasDestination))));
|
||||
|
||||
LambdaFunction executedVersion = new LambdaFunction();
|
||||
executedVersion.setFunctionArn("arn:aws:lambda:us-east-1:000000000000:function:test-fn:1");
|
||||
executedVersion.setAccountId("000000000000");
|
||||
executedVersion.setVersion("1");
|
||||
|
||||
assertEquals(aliasDestination, service.findEventInvokeConfig(executedVersion, "prod")
|
||||
.orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
|
||||
assertEquals(versionDestination, service.findEventInvokeConfig(executedVersion, "other")
|
||||
.orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
|
||||
assertEquals(versionDestination, service.findEventInvokeConfig(executedVersion)
|
||||
.orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
|
||||
}
|
||||
}
|
||||
+25
-2
@@ -27,6 +27,8 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -76,6 +78,27 @@ class LambdaExecutorServiceTest {
|
||||
verify(warmPool, timeout(5000)).acquire(fn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void asyncInvocationPassesInvokedAliasToDestinationRouter() {
|
||||
AsyncInvokeDestinationRouter router = mock(AsyncInvokeDestinationRouter.class);
|
||||
executor.shutdown();
|
||||
executor = new LambdaExecutorService(warmPool, new ObjectMapper(), concurrencyLimiter, router);
|
||||
RuntimeApiServer rtas = mock(RuntimeApiServer.class);
|
||||
ContainerHandle handle = new ContainerHandle("cid-alias", "test-fn", rtas, ContainerState.WARM);
|
||||
when(warmPool.acquire(fn)).thenReturn(handle);
|
||||
doAnswer(inv -> {
|
||||
PendingInvocation invocation = inv.getArgument(0);
|
||||
invocation.getResultFuture().complete(
|
||||
new InvokeResult(200, null, "{}".getBytes(), null, invocation.getRequestId()));
|
||||
return invocation.getResultFuture();
|
||||
}).when(rtas).enqueue(any(PendingInvocation.class));
|
||||
|
||||
byte[] payload = "{}".getBytes();
|
||||
executor.invoke(fn, payload, InvocationType.Event, 0, "prod");
|
||||
|
||||
verify(router, timeout(5000)).route(eq(fn), eq(payload), any(InvokeResult.class), eq(0), eq("prod"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void syncInvocationPastTheChainBound_isNotAffected() {
|
||||
// Only the asynchronous path can be re-entered by a destination chain, and refusing a
|
||||
@@ -234,14 +257,14 @@ class LambdaExecutorServiceTest {
|
||||
doAnswer(inv -> {
|
||||
routed.countDown();
|
||||
return null;
|
||||
}).when(router).route(any(), any(), any(), anyInt());
|
||||
}).when(router).route(any(), any(), any(), anyInt(), isNull());
|
||||
|
||||
byte[] payload = "{}".getBytes();
|
||||
InvokeResult result = routingExecutor.invoke(fn, payload, InvocationType.Event);
|
||||
|
||||
assertEquals(202, result.getStatusCode());
|
||||
assertTrue(routed.await(5, TimeUnit.SECONDS), "destination routing never ran");
|
||||
verify(router).route(fn, payload, expected, 0);
|
||||
verify(router).route(fn, payload, expected, 0, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user