Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(sfn): no execution data available via SDK (#1506)

Juri Duvale5a646d40cc6
diff --git a/pom.xml b/pom.xml
index e58119b5db..b2d55e7ca7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -246,6 +246,11 @@
<version>5.28.5</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java
index db5a51bacd..7a5b7fdf0c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java
@@ -212,7 +212,10 @@ public class StepFunctionsJsonHandler {
}
private Response handleGetExecutionHistory(JsonNode request) {
- List<HistoryEvent> events = service.getExecutionHistory(request.path("executionArn").asText());
+ var arn = request.path("executionArn").asText();
+ var includeExecutionData = request.path("includeExecutionData").asBoolean(true);
+
+ List<HistoryEvent> events = service.getExecutionHistory(arn);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode array = response.putArray("events");
for (HistoryEvent e : events) {
@@ -221,13 +224,23 @@ public class StepFunctionsJsonHandler {
item.put("timestamp", e.getTimestamp());
item.put("type", e.getType());
if (e.getPreviousEventId() != null) item.put("previousEventId", e.getPreviousEventId());
- if (e.getDetails() != null) {
- item.set(e.getType() + "EventDetails", objectMapper.valueToTree(e.getDetails()));
+ if (includeExecutionData && e.getDetails() != null) {
+ item.set(historyEventDetailsField(e.getType()), objectMapper.valueToTree(e.getDetails()));
}
}
return Response.ok(response).build();
}
+ static String historyEventDetailsField(String type) {
+ if (type.endsWith("StateEntered")) {
+ return "stateEnteredEventDetails";
+ }
+ if (type.endsWith("StateExited")) {
+ return "stateExitedEventDetails";
+ }
+ return Character.toLowerCase(type.charAt(0)) + type.substring(1) + "EventDetails";
+ }
+
private Response handleSendTaskSuccess(JsonNode request) {
service.sendTaskSuccess(request.path("taskToken").asText(), request.path("output").asText());
return Response.ok(objectMapper.createObjectNode()).build();
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsExecutionHistoryIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsExecutionHistoryIntegrationTest.java
new file mode 100644
index 0000000000..b8a4e95e07
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsExecutionHistoryIntegrationTest.java
@@ -0,0 +1,192 @@
+package io.github.hectorvent.floci.services.stepfunctions;
+
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.fail;
+
+@QuarkusTest
+class StepFunctionsExecutionHistoryIntegrationTest {
+
+ private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";
+ private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role";
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @Test
+ void getExecutionHistory_populatesExecutionSucceededEventDetails() throws Exception {
+ String definition = """
+ {
+ "Comment": "A Hello World example that demonstrates various state types in the Amazon States Language, and showcases data flow and transformations using variables and JSONata expressions. This example consists solely of flow control states, so no additional resources are needed to run it.",
+ "QueryLanguage": "JSONata",
+ "StartAt": "Set Variables and State Output",
+ "States": {
+ "Set Variables and State Output": {
+ "Type": "Pass",
+ "Comment": "A Pass state passes its input to its output, without performing work. They can also generate static JSON output, or transform JSON input using JSONata expressions, and pass the transformed data to the next state. Pass states are useful when constructing and debugging state machines.",
+ "End": true,
+ "Output": {
+ "ExecutionWaitTimeInSeconds": 3
+ },
+ "Assign": {
+ "CheckpointCount": 0,
+ "ExecutionWaitTimeInSeconds": 3
+ }
+ }
+ }
+ }
+ """;
+
+ String stateMachineArn = createStateMachine("execution-history-test", definition);
+ String executionArn = startExecution(stateMachineArn);
+ waitForExecution(executionArn);
+
+ given()
+ .header("X-Amz-Target", "AWSStepFunctions.GetExecutionHistory")
+ .contentType(SFN_CONTENT_TYPE)
+ .body(String.format("""
+ {
+ "executionArn": "%s",
+ "includeExecutionData": true
+ }
+ """, executionArn))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("events.find { it.type == 'PassStateEntered' }.type", equalTo("PassStateEntered"))
+ .body("events.find { it.type == 'PassStateEntered' }.stateEnteredEventDetails", notNullValue())
+ .body("events.find { it.type == 'PassStateEntered' }.stateEnteredEventDetails.name",
+ equalTo("Set Variables and State Output"))
+ .body("events.find { it.type == 'PassStateEntered' }.passStateEnteredEventDetails", nullValue())
+ .body("events.find { it.type == 'PassStateExited' }.type", equalTo("PassStateExited"))
+ .body("events.find { it.type == 'PassStateExited' }.stateExitedEventDetails", notNullValue())
+ .body("events.find { it.type == 'PassStateExited' }.stateExitedEventDetails.name",
+ equalTo("Set Variables and State Output"))
+ .body("events.find { it.type == 'PassStateExited' }.passStateExitedEventDetails", nullValue())
+ .body("events.find { it.type == 'ExecutionSucceeded' }.type", equalTo("ExecutionSucceeded"))
+ .body("events.find { it.type == 'ExecutionSucceeded' }.executionSucceededEventDetails", notNullValue())
+ .body("events.find { it.type == 'ExecutionSucceeded' }.executionSucceededEventDetails.output",
+ equalTo("{\"ExecutionWaitTimeInSeconds\":3}"));
+ }
+
+ @Test
+ void getExecutionHistory_omitsExecutionSucceededEventDetailsWhenIncludeExecutionDataIsFalse() throws Exception {
+ String definition = """
+ {
+ "StartAt": "Set Output",
+ "States": {
+ "Set Output": {
+ "Type": "Pass",
+ "End": true,
+ "Result": {
+ "message": "hidden"
+ }
+ }
+ }
+ }
+ """;
+
+ String stateMachineArn = createStateMachine("execution-history-no-data-test", definition);
+ String executionArn = startExecution(stateMachineArn);
+ waitForExecution(executionArn);
+
+ given()
+ .header("X-Amz-Target", "AWSStepFunctions.GetExecutionHistory")
+ .contentType(SFN_CONTENT_TYPE)
+ .body(String.format("""
+ {
+ "executionArn": "%s",
+ "includeExecutionData": false
+ }
+ """, executionArn))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("events.find { it.type == 'PassStateEntered' }.stateEnteredEventDetails", nullValue())
+ .body("events.find { it.type == 'PassStateExited' }.stateExitedEventDetails", nullValue())
+ .body("events.find { it.type == 'ExecutionSucceeded' }.type", equalTo("ExecutionSucceeded"))
+ .body("events.find { it.type == 'ExecutionSucceeded' }.executionSucceededEventDetails", nullValue());
+ }
+
+ private String createStateMachine(String name, String definition) {
+ Response response = given()
+ .header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
+ .contentType(SFN_CONTENT_TYPE)
+ .body(String.format("""
+ {
+ "name": "%s",
+ "definition": %s,
+ "roleArn": "%s"
+ }
+ """, name, quote(definition), ROLE_ARN))
+ .when()
+ .post("/");
+ response.then().statusCode(200);
+ return response.jsonPath().getString("stateMachineArn");
+ }
+
+ private String startExecution(String stateMachineArn) {
+ Response response = given()
+ .header("X-Amz-Target", "AWSStepFunctions.StartExecution")
+ .contentType(SFN_CONTENT_TYPE)
+ .body(String.format("""
+ {
+ "stateMachineArn": "%s"
+ }
+ """, stateMachineArn))
+ .when()
+ .post("/");
+ response.then().statusCode(200);
+ return response.jsonPath().getString("executionArn");
+ }
+
+ private void waitForExecution(String executionArn) {
+ await()
+ .atMost(Duration.ofSeconds(5))
+ .pollInterval(Duration.ofMillis(100))
+ .untilAsserted(() -> {
+ Response response = given()
+ .header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
+ .contentType(SFN_CONTENT_TYPE)
+ .body(String.format("""
+ { "executionArn": "%s" }
+ """, executionArn))
+ .when()
+ .post("/");
+ response.then().statusCode(200);
+
+ String status = response.jsonPath().getString("status");
+ if ("FAILED".equals(status) || "ABORTED".equals(status)) {
+ fail("Execution " + status + ": " + response.body().asString());
+ }
+ if (!"SUCCEEDED".equals(status)) {
+ fail("Execution status was " + status);
+ }
+ });
+ }
+
+ private static String quote(String raw) {
+ return "\"" + raw
+ .replace("\\", "\\\\")
+ .replace("\"", "\\\"")
+ .replace("\n", "\\n")
+ .replace("\r", "\\r")
+ .replace("\t", "\\t")
+ + "\"";
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsHistoryEventDetailsFieldTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsHistoryEventDetailsFieldTest.java
new file mode 100644
index 0000000000..9153bca24f
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsHistoryEventDetailsFieldTest.java
@@ -0,0 +1,67 @@
+package io.github.hectorvent.floci.services.stepfunctions;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class StepFunctionsHistoryEventDetailsFieldTest {
+
+ @ParameterizedTest
+ @CsvSource({
+ "ActivityFailed, activityFailedEventDetails",
+ "ActivityScheduleFailed, activityScheduleFailedEventDetails",
+ "ActivityScheduled, activityScheduledEventDetails",
+ "ActivityStarted, activityStartedEventDetails",
+ "ActivitySucceeded, activitySucceededEventDetails",
+ "ActivityTimedOut, activityTimedOutEventDetails",
+ "TaskFailed, taskFailedEventDetails",
+ "TaskScheduled, taskScheduledEventDetails",
+ "TaskStartFailed, taskStartFailedEventDetails",
+ "TaskStarted, taskStartedEventDetails",
+ "TaskSubmitFailed, taskSubmitFailedEventDetails",
+ "TaskSubmitted, taskSubmittedEventDetails",
+ "TaskSucceeded, taskSucceededEventDetails",
+ "TaskTimedOut, taskTimedOutEventDetails",
+ "ExecutionFailed, executionFailedEventDetails",
+ "ExecutionStarted, executionStartedEventDetails",
+ "ExecutionSucceeded, executionSucceededEventDetails",
+ "ExecutionAborted, executionAbortedEventDetails",
+ "ExecutionTimedOut, executionTimedOutEventDetails",
+ "ExecutionRedriven, executionRedrivenEventDetails",
+ "MapStateStarted, mapStateStartedEventDetails",
+ "MapIterationStarted, mapIterationStartedEventDetails",
+ "MapIterationSucceeded, mapIterationSucceededEventDetails",
+ "MapIterationFailed, mapIterationFailedEventDetails",
+ "MapIterationAborted, mapIterationAbortedEventDetails",
+ "LambdaFunctionFailed, lambdaFunctionFailedEventDetails",
+ "LambdaFunctionScheduleFailed, lambdaFunctionScheduleFailedEventDetails",
+ "LambdaFunctionScheduled, lambdaFunctionScheduledEventDetails",
+ "LambdaFunctionStartFailed, lambdaFunctionStartFailedEventDetails",
+ "LambdaFunctionSucceeded, lambdaFunctionSucceededEventDetails",
+ "LambdaFunctionTimedOut, lambdaFunctionTimedOutEventDetails",
+ "MapRunStarted, mapRunStartedEventDetails",
+ "MapRunFailed, mapRunFailedEventDetails",
+ "MapRunRedriven, mapRunRedrivenEventDetails",
+ "EvaluationFailed, evaluationFailedEventDetails",
+ "PassStateEntered, stateEnteredEventDetails",
+ "TaskStateEntered, stateEnteredEventDetails",
+ "ChoiceStateEntered, stateEnteredEventDetails",
+ "WaitStateEntered, stateEnteredEventDetails",
+ "SucceedStateEntered, stateEnteredEventDetails",
+ "FailStateEntered, stateEnteredEventDetails",
+ "ParallelStateEntered, stateEnteredEventDetails",
+ "MapStateEntered, stateEnteredEventDetails",
+ "PassStateExited, stateExitedEventDetails",
+ "TaskStateExited, stateExitedEventDetails",
+ "ChoiceStateExited, stateExitedEventDetails",
+ "WaitStateExited, stateExitedEventDetails",
+ "SucceedStateExited, stateExitedEventDetails",
+ "FailStateExited, stateExitedEventDetails",
+ "ParallelStateExited, stateExitedEventDetails",
+ "MapStateExited, stateExitedEventDetails"
+ })
+ void historyEventDetailsField_matchesAwsSdkFieldName(String type, String fieldName) {
+ assertEquals(fieldName, StepFunctionsJsonHandler.historyEventDetailsField(type));
+ }
+}