mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(stepfunctions): honor ListExecutions status filters (#6406)
* fix(stepfunctions): honor ListExecutions status filters Filter by the requested current status, validating filter values and types before listing executions. Serialize the captured matching status, preserve unfiltered behavior, and cover the wire path through the normal SDK suite. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> * fix(stepfunctions): reject unsupported pending-redrive listings Reject PENDING_REDRIVE consistently without implying map-run listing support. Exercise execution filters through explicit stop completion instead of polling for asynchronous success. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> * fix(stepfunctions): use Smithy status filter validation Reuse the shared enum validation helper and preserve the AWS status ordering in invalid ListExecutions filter errors. Verify exact messages before service access and through boto3. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> --------- Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
32663a24ad
commit
40b6bfc204
4 files changed
+218
-1
No files matched your search
@@ -0,0 +1,70 @@
|
||||
"""Step Functions execution filters through boto3 and real workflow transitions."""
|
||||
import json
|
||||
|
||||
import boto3
|
||||
import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
|
||||
def test_execution_filters_follow_current_status(aws_config, client_config, iam_client, unique_name):
|
||||
client = boto3.client("stepfunctions", config=client_config, **aws_config)
|
||||
role_name = "sfn-filter-" + unique_name
|
||||
role = iam_client.create_role(
|
||||
RoleName=role_name,
|
||||
AssumeRolePolicyDocument=json.dumps({
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{
|
||||
"Effect": "Allow",
|
||||
"Principal": {"Service": "states.amazonaws.com"},
|
||||
"Action": "sts:AssumeRole",
|
||||
}],
|
||||
}),
|
||||
)["Role"]["Arn"]
|
||||
machine = running = None
|
||||
try:
|
||||
machine = client.create_state_machine(
|
||||
name="sfn-filter-" + unique_name,
|
||||
roleArn=role,
|
||||
definition=json.dumps({
|
||||
"StartAt": "Wait",
|
||||
"States": {
|
||||
"Wait": {"Type": "Wait", "Seconds": 300, "End": True},
|
||||
},
|
||||
}),
|
||||
)["stateMachineArn"]
|
||||
running = client.start_execution(stateMachineArn=machine)["executionArn"]
|
||||
|
||||
def listed(status=None):
|
||||
parameters = {"stateMachineArn": machine}
|
||||
if status is not None:
|
||||
parameters["statusFilter"] = status
|
||||
rows = client.list_executions(**parameters)["executions"]
|
||||
if status is not None:
|
||||
assert all(row["status"] == status for row in rows)
|
||||
return {row["executionArn"] for row in rows}
|
||||
|
||||
assert listed("RUNNING") == {running}
|
||||
assert listed("SUCCEEDED") == set()
|
||||
assert listed("FAILED") == set()
|
||||
assert listed() == {running}
|
||||
client.stop_execution(executionArn=running)
|
||||
assert listed("RUNNING") == set()
|
||||
assert listed("ABORTED") == {running}
|
||||
for invalid_status in ("", "succeeded", "UNKNOWN"):
|
||||
with pytest.raises(ClientError) as error:
|
||||
client.list_executions(stateMachineArn=machine, statusFilter=invalid_status)
|
||||
response = error.value.response
|
||||
assert response["ResponseMetadata"]["HTTPStatusCode"] == 400
|
||||
assert response["Error"]["Code"] == "ValidationException"
|
||||
assert response["Error"]["Message"] == (
|
||||
f"1 validation error detected: Value '{invalid_status}' at 'statusFilter' "
|
||||
"failed to satisfy constraint: Member must satisfy enum value set: "
|
||||
"[SUCCEEDED, TIMED_OUT, PENDING_REDRIVE, ABORTED, FAILED, RUNNING]"
|
||||
)
|
||||
finally:
|
||||
if running and client.describe_execution(executionArn=running)["status"] == "RUNNING":
|
||||
client.stop_execution(executionArn=running)
|
||||
if machine:
|
||||
client.delete_state_machine(stateMachineArn=machine)
|
||||
iam_client.delete_role(RoleName=role_name)
|
||||
client.close()
|
||||
@@ -46,6 +46,11 @@
|
||||
definition, role, logging, tracing, encryption, and tags without replacing the state machine;
|
||||
changes to `StateMachineName` or `StateMachineType` use replacement semantics.
|
||||
|
||||
`ListExecutions` honors `statusFilter` when listing a state machine's executions.
|
||||
Omitting the filter returns all statuses. Invalid filter values or types return
|
||||
`ValidationException`. Map Run execution listing and `PENDING_REDRIVE` filtering are not
|
||||
supported; `PENDING_REDRIVE` returns `ValidationException` even when a `mapRunArn` is supplied.
|
||||
|
||||
## Nested task credentials
|
||||
|
||||
Nested `states:startExecution` tasks, including `.sync` and `.sync:2`, and the SDK
|
||||
|
||||
+17
-1
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Activity;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.ActivityTask;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
@@ -432,10 +433,23 @@ public class StepFunctionsJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleListExecutions(JsonNode request) {
|
||||
String statusFilter = optionalText(request, "statusFilter");
|
||||
if (statusFilter != null && !EXECUTION_STATUSES.contains(statusFilter)) {
|
||||
throw SmithyValidationErrors.validationException(
|
||||
SmithyValidationErrors.notInEnum(statusFilter, "statusFilter", EXECUTION_STATUSES));
|
||||
}
|
||||
if ("PENDING_REDRIVE".equals(statusFilter)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"PENDING_REDRIVE execution listing is not supported.", 400);
|
||||
}
|
||||
List<Execution> list = service.listExecutions(request.path("stateMachineArn").asText());
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode array = response.putArray("executions");
|
||||
for (Execution e : list) {
|
||||
String status = e.getStatus();
|
||||
if (statusFilter != null && !statusFilter.equals(status)) {
|
||||
continue;
|
||||
}
|
||||
ObjectNode item = array.addObject();
|
||||
item.put("executionArn", e.getExecutionArn());
|
||||
item.put("stateMachineArn", e.getStateMachineArn());
|
||||
@@ -446,7 +460,7 @@ public class StepFunctionsJsonHandler {
|
||||
item.put("stateMachineAliasArn", e.getStateMachineAliasArn());
|
||||
}
|
||||
item.put("name", e.getName());
|
||||
item.put("status", e.getStatus());
|
||||
item.put("status", status);
|
||||
item.put("startDate", e.getStartDate());
|
||||
if (e.getStopDate() != null) item.put("stopDate", e.getStopDate());
|
||||
}
|
||||
@@ -497,6 +511,8 @@ public class StepFunctionsJsonHandler {
|
||||
|
||||
private static final Set<String> EXECUTION_DATA_FIELDS =
|
||||
Set.of("input", "inputDetails", "output", "outputDetails");
|
||||
private static final List<String> EXECUTION_STATUSES =
|
||||
List.of("SUCCEEDED", "TIMED_OUT", "PENDING_REDRIVE", "ABORTED", "FAILED", "RUNNING");
|
||||
|
||||
static String historyEventDetailsField(String type) {
|
||||
if (type.endsWith("StateEntered")) {
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class StepFunctionsListExecutionsFilterTest {
|
||||
|
||||
private static final String ARN = "arn:aws:states:us-east-2:000000000000:stateMachine:filter";
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private StepFunctionsService service;
|
||||
private StepFunctionsJsonHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = mock(StepFunctionsService.class);
|
||||
handler = new StepFunctionsJsonHandler(service, mapper);
|
||||
when(service.listExecutions(ARN)).thenReturn(
|
||||
List.of("RUNNING", "SUCCEEDED", "FAILED", "TIMED_OUT", "ABORTED").stream()
|
||||
.map(status -> {
|
||||
Execution execution = new Execution();
|
||||
execution.setExecutionArn(ARN + ":" + status);
|
||||
execution.setStateMachineArn(ARN);
|
||||
execution.setName(status);
|
||||
execution.setStatus(status);
|
||||
return execution;
|
||||
}).toList());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"RUNNING", "SUCCEEDED", "FAILED", "TIMED_OUT", "ABORTED"})
|
||||
void onlyMatchingCurrentStatusesAreReturned(String status) {
|
||||
JsonNode executions = list(request().put("statusFilter", status));
|
||||
assertEquals(1, executions.size());
|
||||
assertEquals(status, executions.get(0).path("status").asText());
|
||||
assertEquals(ARN + ":" + status, executions.get(0).path("executionArn").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedAndNullFiltersKeepAllStatuses() {
|
||||
assertEquals(5, list(request()).size());
|
||||
assertEquals(5, list(request().putNull("statusFilter")).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void absentMatchesReturnAnEmptyArray() {
|
||||
Execution succeeded = new Execution();
|
||||
succeeded.setStatus("SUCCEEDED");
|
||||
when(service.listExecutions(ARN)).thenReturn(List.of(succeeded));
|
||||
assertEquals(0, list(request().put("statusFilter", "RUNNING")).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emittedStatusIsTheSameSnapshotThatMatchedTheFilter() {
|
||||
Execution execution = mock(Execution.class);
|
||||
when(execution.getStatus()).thenReturn("RUNNING", "SUCCEEDED");
|
||||
when(service.listExecutions(ARN)).thenReturn(List.of(execution));
|
||||
JsonNode executions = list(request().put("statusFilter", "RUNNING"));
|
||||
assertEquals(1, executions.size());
|
||||
assertEquals("RUNNING", executions.get(0).path("status").asText());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "running", "UNKNOWN", "PENDING_REDRIVE"})
|
||||
void invalidStateMachineFiltersFailBeforeReadingExecutions(String status) {
|
||||
assertInvalid(request().put("statusFilter", status));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "succeeded", "UNKNOWN"})
|
||||
void invalidEnumValuesUseSmithyValidationWording(String status) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> list(request().put("statusFilter", status)));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals("1 validation error detected: Value '" + status
|
||||
+ "' at 'statusFilter' failed to satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[SUCCEEDED, TIMED_OUT, PENDING_REDRIVE, ABORTED, FAILED, RUNNING]", error.getMessage());
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pendingRedriveIsRejectedWithoutClaimingMapRunSupport() {
|
||||
assertInvalid(mapper.createObjectNode().put("statusFilter", "PENDING_REDRIVE"));
|
||||
assertInvalid(mapper.createObjectNode().put("statusFilter", "PENDING_REDRIVE")
|
||||
.put("mapRunArn", ARN + ":map-run"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonStringFiltersAreRejected() {
|
||||
assertInvalid(request().put("statusFilter", true));
|
||||
assertInvalid(request().put("statusFilter", 1));
|
||||
ObjectNode request = request();
|
||||
request.putArray("statusFilter").add("RUNNING");
|
||||
assertInvalid(request);
|
||||
}
|
||||
|
||||
private void assertInvalid(ObjectNode request) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> list(request));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
private ObjectNode request() {
|
||||
return mapper.createObjectNode().put("stateMachineArn", ARN);
|
||||
}
|
||||
|
||||
private JsonNode list(ObjectNode request) {
|
||||
return ((JsonNode) handler.handle("ListExecutions", request, "us-east-2").getEntity())
|
||||
.path("executions");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user