mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(scheduler): start Step Functions targets (#3936)
Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
1 parent
3c251b2343
commit
bc018afad0
4 files changed
+298
-3
No files matched your search
+53
-1
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.services.scheduler.model.Target;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -50,6 +51,7 @@ class ScheduleInvokerTest {
|
||||
private SnsService snsService;
|
||||
private EventBridgeService eventBridgeService;
|
||||
private EcsService ecsService;
|
||||
private StepFunctionsService stepFunctionsService;
|
||||
private ScheduleInvoker invoker;
|
||||
|
||||
@BeforeEach
|
||||
@@ -59,10 +61,11 @@ class ScheduleInvokerTest {
|
||||
snsService = mock(SnsService.class);
|
||||
eventBridgeService = mock(EventBridgeService.class);
|
||||
ecsService = mock(EcsService.class);
|
||||
stepFunctionsService = mock(StepFunctionsService.class);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.baseUrl()).thenReturn("http://localhost:4566");
|
||||
invoker = new ScheduleInvoker(sqsService, lambdaService, snsService,
|
||||
eventBridgeService, ecsService, new ObjectMapper(), config);
|
||||
eventBridgeService, ecsService, stepFunctionsService, new ObjectMapper(), config);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -279,6 +282,55 @@ class ScheduleInvokerTest {
|
||||
eq("Scheduler"), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateMachineTargetStartsExecutionWithInputAndArnRegion() {
|
||||
String stateMachineArn = "arn:aws:states:eu-west-1:000000000000:stateMachine:scheduled-workflow";
|
||||
String input = "{\"order\":{\"id\":42}}";
|
||||
Target target = new Target();
|
||||
target.setArn(stateMachineArn);
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/scheduler-role");
|
||||
target.setInput(input);
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
|
||||
verify(stepFunctionsService).startExecution(stateMachineArn, null, input, "eu-west-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateMachineTargetWithoutInputUsesEmptyObject() {
|
||||
String stateMachineArn = "arn:aws:states:us-east-1:000000000000:stateMachine:scheduled-workflow";
|
||||
Target target = new Target();
|
||||
target.setArn(stateMachineArn);
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
|
||||
verify(stepFunctionsService).startExecution(stateMachineArn, null, "{}", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateMachineStartFailurePropagatesToSchedulerDeliveryHandling() {
|
||||
String stateMachineArn = "arn:aws:states:us-east-1:000000000000:stateMachine:missing";
|
||||
Target target = new Target();
|
||||
target.setArn(stateMachineArn);
|
||||
when(stepFunctionsService.startExecution(stateMachineArn, null, "{}", "us-east-1"))
|
||||
.thenThrow(new AwsException("StateMachineDoesNotExist", "State machine does not exist", 400));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> invoker.invoke(target, "us-east-1"));
|
||||
|
||||
assertEquals("StateMachineDoesNotExist", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void qualifiedStateMachineTargetRemainsUnsupported() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:states:us-east-1:000000000000:stateMachine:scheduled-workflow:PROD");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> invoker.invoke(target, "us-east-1"));
|
||||
verifyNoInteractions(stepFunctionsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void eventBusTargetUsesDeclaredDetailTypeAndSource() {
|
||||
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
package io.github.hectorvent.floci.services.scheduler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class SchedulerStepFunctionsIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/scheduler-role";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final DateTimeFormatter SCHEDULE_TIME =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC);
|
||||
|
||||
@Inject
|
||||
ScheduleInvoker scheduleInvoker;
|
||||
|
||||
@Inject
|
||||
SchedulerService schedulerService;
|
||||
|
||||
@Inject
|
||||
StepFunctionsService stepFunctionsService;
|
||||
|
||||
@Inject
|
||||
SqsService sqsService;
|
||||
|
||||
@Inject
|
||||
EmulatorConfig config;
|
||||
|
||||
private final List<String> scheduleNames = new ArrayList<>();
|
||||
private final List<String> stateMachineArns = new ArrayList<>();
|
||||
private final List<ScheduleDispatcher> testDispatchers = new ArrayList<>();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
for (String scheduleName : scheduleNames) {
|
||||
try {
|
||||
schedulerService.deleteSchedule(scheduleName, null, REGION);
|
||||
} catch (AwsException expected) {
|
||||
// A schedule with ActionAfterCompletion=DELETE may already be gone.
|
||||
}
|
||||
}
|
||||
for (String stateMachineArn : stateMachineArns) {
|
||||
stepFunctionsService.deleteStateMachine(stateMachineArn);
|
||||
}
|
||||
for (ScheduleDispatcher dispatcher : testDispatchers) {
|
||||
dispatcher.onStop(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void oneTimeScheduleStartsStateMachineWithExactInput() {
|
||||
String stateMachineArn = createPassStateMachine();
|
||||
String scheduleName = uniqueName("sfn-at");
|
||||
String input = "{\"order\":{\"id\":42},\"items\":[\"a\",\"b\"]}";
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", "ENABLED",
|
||||
stateMachineArn, input);
|
||||
|
||||
dispatcherFor(scheduleName).tick(fireAt);
|
||||
|
||||
List<Map<String, Object>> executions = listExecutions(stateMachineArn);
|
||||
assertEquals(1, executions.size());
|
||||
Response execution = waitForTerminalExecution((String) executions.get(0).get("executionArn"));
|
||||
assertEquals("SUCCEEDED", execution.jsonPath().getString("status"));
|
||||
assertEquals(input, execution.jsonPath().getString("input"));
|
||||
assertEquals(input, execution.jsonPath().getString("output"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void recurringScheduleStartsOneExecutionPerOccurrence() {
|
||||
String stateMachineArn = createPassStateMachine();
|
||||
String scheduleName = uniqueName("sfn-rate");
|
||||
createSchedule(scheduleName, "rate(1 minute)", "ENABLED", stateMachineArn, "{\"kind\":\"recurring\"}");
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
Instant firstFire = schedule.getCreationDate().plus(61, ChronoUnit.SECONDS);
|
||||
ScheduleDispatcher dispatcher = dispatcherFor(scheduleName);
|
||||
|
||||
dispatcher.tick(firstFire);
|
||||
dispatcher.tick(firstFire.plus(61, ChronoUnit.SECONDS));
|
||||
|
||||
assertEquals(2, listExecutions(stateMachineArn).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledScheduleDoesNotStartStateMachine() {
|
||||
String stateMachineArn = createPassStateMachine();
|
||||
String scheduleName = uniqueName("sfn-disabled");
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", "DISABLED",
|
||||
stateMachineArn, "{\"disabled\":true}");
|
||||
|
||||
dispatcherFor(scheduleName).tick(fireAt);
|
||||
|
||||
assertEquals(0, listExecutions(stateMachineArn).size());
|
||||
}
|
||||
|
||||
private String createPassStateMachine() {
|
||||
String name = uniqueName("scheduled-workflow");
|
||||
String definition = "{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}";
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("name", name);
|
||||
request.put("definition", definition);
|
||||
request.put("roleArn", ROLE_ARN);
|
||||
|
||||
Response response = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body(request.toString())
|
||||
.when()
|
||||
.post("/");
|
||||
response.then().statusCode(200);
|
||||
String stateMachineArn = response.jsonPath().getString("stateMachineArn");
|
||||
stateMachineArns.add(stateMachineArn);
|
||||
return stateMachineArn;
|
||||
}
|
||||
|
||||
private void createSchedule(String name, String expression, String state,
|
||||
String targetArn, String input) {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("ScheduleExpression", expression);
|
||||
request.putObject("FlexibleTimeWindow").put("Mode", "OFF");
|
||||
request.put("State", state);
|
||||
ObjectNode target = request.putObject("Target");
|
||||
target.put("Arn", targetArn);
|
||||
target.put("RoleArn", ROLE_ARN);
|
||||
target.put("Input", input);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(request.toString())
|
||||
.when()
|
||||
.post("/schedules/" + name)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
scheduleNames.add(name);
|
||||
}
|
||||
|
||||
private ScheduleDispatcher dispatcherFor(String scheduleName) {
|
||||
Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION);
|
||||
SchedulerService scopedSchedulerService = mock(SchedulerService.class);
|
||||
when(scopedSchedulerService.listAllSchedules()).thenReturn(List.of(schedule));
|
||||
ScheduleDispatcher dispatcher = new ScheduleDispatcher(
|
||||
scopedSchedulerService, scheduleInvoker, sqsService, config);
|
||||
testDispatchers.add(dispatcher);
|
||||
return dispatcher;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> listExecutions(String stateMachineArn) {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("stateMachineArn", stateMachineArn);
|
||||
Response response = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.ListExecutions")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body(request.toString())
|
||||
.when()
|
||||
.post("/");
|
||||
response.then().statusCode(200);
|
||||
return response.jsonPath().getList("executions");
|
||||
}
|
||||
|
||||
private Response waitForTerminalExecution(String executionArn) {
|
||||
await()
|
||||
.atMost(Duration.ofSeconds(5))
|
||||
.pollInterval(Duration.ofMillis(100))
|
||||
.until(() -> !"RUNNING".equals(describeExecution(executionArn).jsonPath().getString("status")));
|
||||
return describeExecution(executionArn);
|
||||
}
|
||||
|
||||
private Response describeExecution(String executionArn) {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("executionArn", executionArn);
|
||||
Response response = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body(request.toString())
|
||||
.when()
|
||||
.post("/");
|
||||
response.then().statusCode(200);
|
||||
return response;
|
||||
}
|
||||
|
||||
private static String uniqueName(String prefix) {
|
||||
return prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user