mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eventbridge): start Step Functions targets (#4006)
Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
1 parent
275977cb75
commit
957542d172
4 files changed
+237
-2
No files matched your search
+49
@@ -22,6 +22,7 @@ import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -45,6 +46,7 @@ class EventBridgeInvokerTest {
|
||||
private FirehoseService firehoseService;
|
||||
private EventBridgeService eventBridgeService;
|
||||
private EcsService ecsService;
|
||||
private StepFunctionsService stepFunctionsService;
|
||||
private RegionResolver regionResolver;
|
||||
|
||||
@BeforeEach
|
||||
@@ -56,6 +58,7 @@ class EventBridgeInvokerTest {
|
||||
firehoseService = mock(FirehoseService.class);
|
||||
eventBridgeService = mock(EventBridgeService.class);
|
||||
ecsService = mock(EcsService.class);
|
||||
stepFunctionsService = mock(StepFunctionsService.class);
|
||||
regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
when(eventBridgeService.putEvents(anyList(), anyString(), any()))
|
||||
@@ -73,12 +76,58 @@ class EventBridgeInvokerTest {
|
||||
ecsService,
|
||||
new EcsJsonHandler(ecsService, new ObjectMapper(),
|
||||
new HostVolumePolicy(emulatorConfig)),
|
||||
stepFunctionsService,
|
||||
regionResolver,
|
||||
new ObjectMapper(),
|
||||
emulatorConfig
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_stateMachineTargetStartsExecutionWithDefaultEvent() {
|
||||
String arn = "arn:aws:states:eu-west-1:111122223333:stateMachine:orders";
|
||||
String event = "{\"detail\":{\"orderId\":\"o-42\"}}";
|
||||
Target target = new Target("id1", arn, null, null);
|
||||
|
||||
invoker.invokeTarget(target, event, "us-east-1");
|
||||
|
||||
verify(stepFunctionsService).startExecution(arn, null, event, "eu-west-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_stateMachineTargetUsesExplicitInput() {
|
||||
String arn = "arn:aws:states:us-east-1:000000000000:stateMachine:orders";
|
||||
Target target = new Target("id1", arn, "{\"source\":\"override\"}", null);
|
||||
|
||||
invoker.invokeTarget(target, "{\"ignored\":true}", "us-east-1");
|
||||
|
||||
verify(stepFunctionsService).startExecution(
|
||||
arn, null, "{\"source\":\"override\"}", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_qualifiedStateMachineTargetRemainsUnsupported() {
|
||||
String versionArn = "arn:aws:states:us-east-1:000000000000:stateMachine:orders:1";
|
||||
String aliasArn = "arn:aws:states:us-east-1:000000000000:stateMachine:orders:PROD";
|
||||
|
||||
invoker.invokeTarget(new Target("version", versionArn, "{}", null), "{}", "us-east-1");
|
||||
invoker.invokeTarget(new Target("alias", aliasArn, "{}", null), "{}", "us-east-1");
|
||||
|
||||
verifyNoInteractions(stepFunctionsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_stateMachineStartFailureDoesNotEscapeDelivery() {
|
||||
String arn = "arn:aws:states:us-east-1:000000000000:stateMachine:missing";
|
||||
when(stepFunctionsService.startExecution(arn, null, "{}", "us-east-1"))
|
||||
.thenThrow(new IllegalStateException("missing state machine"));
|
||||
|
||||
assertDoesNotThrow(() -> invoker.invokeTarget(
|
||||
new Target("id1", arn, "{}", null), "{\"ignored\":true}", "us-east-1"));
|
||||
|
||||
verify(stepFunctionsService).startExecution(arn, null, "{}", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_lambdaTargetPreservesArnAccount() {
|
||||
String arn = "arn:aws:lambda:ap-south-1:100000000012:function:cross-account-function";
|
||||
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.InputTransformer;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
@QuarkusTest
|
||||
class EventBridgeStepFunctionsIntegrationTest {
|
||||
|
||||
private static final String SOURCE_REGION = "us-east-1";
|
||||
private static final String TARGET_REGION = "eu-west-1";
|
||||
private static final String TARGET_ACCOUNT = "111122223333";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::111122223333:role/eventbridge-role";
|
||||
private static final String DEFINITION =
|
||||
"{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}";
|
||||
|
||||
@Inject
|
||||
EventBridgeService eventBridgeService;
|
||||
|
||||
@Inject
|
||||
StepFunctionsService stepFunctionsService;
|
||||
|
||||
@Inject
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
private final List<String> ruleNames = new ArrayList<>();
|
||||
private final List<String> stateMachineArns = new ArrayList<>();
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
for (String ruleName : ruleNames) {
|
||||
List<String> targetIds = eventBridgeService.listTargetsByRule(ruleName, null, SOURCE_REGION)
|
||||
.stream()
|
||||
.map(Target::getId)
|
||||
.toList();
|
||||
eventBridgeService.removeTargets(ruleName, null, targetIds, SOURCE_REGION);
|
||||
eventBridgeService.deleteRule(ruleName, null, SOURCE_REGION);
|
||||
}
|
||||
RequestScopes.runAs(TARGET_ACCOUNT, () -> {
|
||||
for (String stateMachineArn : stateMachineArns) {
|
||||
stepFunctionsService.deleteStateMachine(stateMachineArn);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchingRuleStartsStateMachinesWithEachInputMode() throws Exception {
|
||||
String defaultArn = createStateMachine("default");
|
||||
String explicitArn = createStateMachine("explicit");
|
||||
String pathArn = createStateMachine("path");
|
||||
String transformerArn = createStateMachine("transformer");
|
||||
String ruleName = createRule("input-modes", "{\"source\":[\"orders.created\"]}");
|
||||
|
||||
Target defaultTarget = new Target("default", defaultArn, null, null);
|
||||
Target explicitTarget = new Target("explicit", explicitArn, "{\"mode\":\"explicit\"}", null);
|
||||
Target pathTarget = new Target("path", pathArn, null, "$.detail");
|
||||
Target transformerTarget = new Target("transformer", transformerArn, null, null);
|
||||
transformerTarget.setInputTransformer(new InputTransformer(
|
||||
Map.of("orderId", "$.detail.orderId"), "{\"order\":\"<orderId>\"}"));
|
||||
assertEquals(0, eventBridgeService.putTargets(
|
||||
ruleName, null,
|
||||
List.of(defaultTarget, explicitTarget, pathTarget, transformerTarget),
|
||||
SOURCE_REGION));
|
||||
|
||||
EventBridgeService.PutEventsResult unmatched = eventBridgeService.putEvents(List.of(Map.of(
|
||||
"Source", "orders.ignored",
|
||||
"DetailType", "OrderCreated",
|
||||
"Detail", "{\"orderId\":\"o-42\"}")), SOURCE_REGION);
|
||||
assertEquals(0, unmatched.failedCount());
|
||||
assertEquals(0, executions(defaultArn).size());
|
||||
assertEquals(0, executions(explicitArn).size());
|
||||
assertEquals(0, executions(pathArn).size());
|
||||
assertEquals(0, executions(transformerArn).size());
|
||||
|
||||
EventBridgeService.PutEventsResult matched = eventBridgeService.putEvents(List.of(Map.of(
|
||||
"Source", "orders.created",
|
||||
"DetailType", "OrderCreated",
|
||||
"Detail", "{\"orderId\":\"o-42\",\"total\":19}")), SOURCE_REGION);
|
||||
assertEquals(0, matched.failedCount());
|
||||
assertNotNull(matched.entries().getFirst().get("EventId"));
|
||||
|
||||
Execution defaultExecution = onlyExecution(defaultArn);
|
||||
JsonNode defaultInput = objectMapper.readTree(defaultExecution.getInput());
|
||||
assertEquals("orders.created", defaultInput.path("source").asText());
|
||||
assertEquals("OrderCreated", defaultInput.path("detail-type").asText());
|
||||
assertEquals("o-42", defaultInput.path("detail").path("orderId").asText());
|
||||
assertEquals(SOURCE_REGION, defaultInput.path("region").asText());
|
||||
assertEquals("{\"mode\":\"explicit\"}", onlyExecution(explicitArn).getInput());
|
||||
assertEquals("{\"orderId\":\"o-42\",\"total\":19}", onlyExecution(pathArn).getInput());
|
||||
assertEquals("{\"order\":\"o-42\"}", onlyExecution(transformerArn).getInput());
|
||||
}
|
||||
|
||||
@Test
|
||||
void startFailureDoesNotRejectAcceptedEvent() {
|
||||
String ruleName = createRule("missing-target", "{\"source\":[\"orders.failed\"]}");
|
||||
Target missingTarget = new Target(
|
||||
"missing",
|
||||
"arn:aws:states:eu-west-1:111122223333:stateMachine:missing",
|
||||
"{}",
|
||||
null);
|
||||
assertEquals(0, eventBridgeService.putTargets(
|
||||
ruleName, null, List.of(missingTarget), SOURCE_REGION));
|
||||
|
||||
EventBridgeService.PutEventsResult result = eventBridgeService.putEvents(List.of(Map.of(
|
||||
"Source", "orders.failed",
|
||||
"DetailType", "OrderCreated",
|
||||
"Detail", "{}")), SOURCE_REGION);
|
||||
|
||||
assertEquals(0, result.failedCount());
|
||||
assertNotNull(result.entries().getFirst().get("EventId"));
|
||||
}
|
||||
|
||||
private String createStateMachine(String suffix) {
|
||||
String name = "eventbridge-" + suffix + "-" + UUID.randomUUID();
|
||||
StateMachine stateMachine = RequestScopes.callAs(TARGET_ACCOUNT,
|
||||
() -> stepFunctionsService.createStateMachine(
|
||||
name, DEFINITION, ROLE_ARN, "STANDARD", TARGET_REGION, Map.of()));
|
||||
stateMachineArns.add(stateMachine.getStateMachineArn());
|
||||
return stateMachine.getStateMachineArn();
|
||||
}
|
||||
|
||||
private String createRule(String suffix, String eventPattern) {
|
||||
String name = "eventbridge-sfn-" + suffix + "-" + UUID.randomUUID();
|
||||
eventBridgeService.putRule(
|
||||
name, null, eventPattern, null, RuleState.ENABLED, null, null, null, SOURCE_REGION);
|
||||
ruleNames.add(name);
|
||||
return name;
|
||||
}
|
||||
|
||||
private List<Execution> executions(String stateMachineArn) {
|
||||
return RequestScopes.callAs(
|
||||
TARGET_ACCOUNT, () -> stepFunctionsService.listExecutions(stateMachineArn));
|
||||
}
|
||||
|
||||
private Execution onlyExecution(String stateMachineArn) {
|
||||
List<Execution> executions = executions(stateMachineArn);
|
||||
assertEquals(1, executions.size());
|
||||
return executions.getFirst();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user