mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(redshift): run Data API statements from Step Functions and Scheduler (#5312)
* feat(redshift): run Data API statements from Step Functions and Scheduler Step Functions: add the arn:aws:states:::aws-sdk:redshiftdata:<action> Task integration. It reuses RedshiftDataJsonHandler, so every Data API action Floci implements works for provisioned clusters (ClusterIdentifier) and Serverless workgroups (WorkgroupName). Arguments and results keep the wire PascalCase names, CreatedAt and UpdatedAt are ISO-8601 at any depth, errors surface as RedshiftData.<Code>Exception, and the task fails when redshift or redshift-data is disabled. .sync resources are rejected, as AWS has no such variant. Scheduler: add the redshiftdata:executeStatement and redshiftdata:batchExecuteStatement universal targets. The call runs as the schedule's account and region, because the dispatcher thread carries no request account and cluster and statement storage is account-aware. A SQL error stays on the statement; a request the Data API rejects propagates so the existing retry and dead-letter handling applies. * fix(redshift-data): validate task input and isolate statement storage by account * fix(redshift): run scheduled Data API statements off the dispatcher thread Keep the request check on the dispatcher so a rejected request still retries, and run the SQL on its own thread under the schedule's account. Ask ServiceRegistry whether redshift-data is enabled instead of repeating the rule, keep the resolver and connection factory package-private, and cover the provisioned-cluster account in the resolver test. * fix(scheduler): await scheduled Redshift Data statement in docker test
This commit is contained in:
1 parent
07b529e150
commit
1a6f336e9f
13 files changed
+1291
-16
No files matched your search
+16
@@ -43,6 +43,7 @@ class RedshiftDataResourceResolverTest {
|
||||
private RedshiftDataResourceResolver resolver(RedshiftService redshift, SecretsManagerService secrets) {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn(ACCOUNT);
|
||||
when(regionResolver.getDefaultRegion()).thenReturn(REGION);
|
||||
return new RedshiftDataResourceResolver(redshift, secrets, mapper, broker, regionResolver, serverless);
|
||||
}
|
||||
|
||||
@@ -66,6 +67,21 @@ class RedshiftDataResourceResolverTest {
|
||||
assertEquals("Secret123", target.password());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aProvisionedClusterArnCarriesTheCallersAccount() {
|
||||
RedshiftService redshift = mock(RedshiftService.class);
|
||||
when(redshift.describeClusters("wh")).thenReturn(List.of(cluster()));
|
||||
ObjectNode req = mapper.createObjectNode();
|
||||
req.put("ClusterIdentifier", "wh");
|
||||
req.put("DbUser", "admin");
|
||||
req.put("Database", "dev");
|
||||
|
||||
RedshiftDataResourceResolver.DatabaseTarget target =
|
||||
resolver(redshift, mock(SecretsManagerService.class)).resolve(req, REGION);
|
||||
|
||||
assertEquals("arn:aws:redshift:us-east-1:" + ACCOUNT + ":cluster:wh", target.arn());
|
||||
}
|
||||
|
||||
private static RedshiftServerlessService.WorkgroupTarget workgroupTarget() {
|
||||
return new RedshiftServerlessService.WorkgroupTarget(
|
||||
"arn:aws:redshift-serverless:us-east-1:" + ACCOUNT + ":workgroup/wg-id", "wg-1",
|
||||
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package io.github.hectorvent.floci.services.redshiftdata;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.redshift.spectrum.SpectrumSession;
|
||||
import io.github.hectorvent.floci.services.scheduler.ScheduleInvoker;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Target;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The dispatcher fires a schedule on a thread with no request, so account-aware storage would
|
||||
* fall back to the default account unless the invoker runs the call as the schedule's account.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SchedulerRedshiftDataAccountIntegrationTest {
|
||||
|
||||
private static final String SCHEDULE_ACCOUNT = "111122223333";
|
||||
|
||||
@Inject
|
||||
ScheduleInvoker scheduleInvoker;
|
||||
|
||||
@Inject
|
||||
RedshiftDataService redshiftDataService;
|
||||
|
||||
@InjectMock
|
||||
RedshiftDataResourceResolver resolver;
|
||||
|
||||
@InjectMock
|
||||
RedshiftDataConnectionFactory connectionFactory;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws SQLException {
|
||||
redshiftDataService.clear();
|
||||
when(resolver.resolve(any(), any())).thenReturn(new RedshiftDataResourceResolver.DatabaseTarget(
|
||||
"arn:aws:redshift:us-east-1:" + SCHEDULE_ACCOUNT + ":cluster:analytics",
|
||||
"127.0.0.1", 5439, "dev", "admin", "x",
|
||||
new SpectrumSession(SCHEDULE_ACCOUNT, SCHEDULE_ACCOUNT + ":analytics", "dev", List.of(), false)));
|
||||
when(connectionFactory.open(any())).thenThrow(new SQLException("connection refused"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeStatementRunsAsTheSchedulesAccount() {
|
||||
scheduleInvoker.invoke(scheduleFor("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement",
|
||||
"{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}"),
|
||||
Instant.parse("2026-04-21T09:17:54Z"));
|
||||
|
||||
// Statement was stored under SCHEDULE_ACCOUNT
|
||||
JsonNode scheduledStatements = RequestScopes.callAs(SCHEDULE_ACCOUNT,
|
||||
() -> redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode()));
|
||||
assertEquals(1, scheduledStatements.path("Statements").size());
|
||||
String statementId = scheduledStatements.path("Statements").path(0).path("Id").asText();
|
||||
|
||||
JsonNode described = RequestScopes.callAs(SCHEDULE_ACCOUNT,
|
||||
() -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId)));
|
||||
assertEquals(statementId, described.path("Id").asText());
|
||||
awaitFailed(statementId);
|
||||
|
||||
// Statement is not visible under default account
|
||||
JsonNode defaultStatements = redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode());
|
||||
assertEquals(0, defaultStatements.path("Statements").size());
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class,
|
||||
() -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId)));
|
||||
assertEquals("ResourceNotFoundException", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchExecuteStatementRunsAsTheSchedulesAccount() {
|
||||
scheduleInvoker.invoke(scheduleFor("arn:aws:scheduler:::aws-sdk:redshiftdata:batchExecuteStatement",
|
||||
"{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sqls\":[\"select 1\"]}"),
|
||||
Instant.parse("2026-04-21T09:17:54Z"));
|
||||
|
||||
// Statement was stored under SCHEDULE_ACCOUNT
|
||||
JsonNode scheduledStatements = RequestScopes.callAs(SCHEDULE_ACCOUNT,
|
||||
() -> redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode()));
|
||||
assertEquals(1, scheduledStatements.path("Statements").size());
|
||||
String statementId = scheduledStatements.path("Statements").path(0).path("Id").asText();
|
||||
|
||||
JsonNode described = RequestScopes.callAs(SCHEDULE_ACCOUNT,
|
||||
() -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId)));
|
||||
assertEquals(statementId, described.path("Id").asText());
|
||||
awaitFailed(statementId);
|
||||
|
||||
// Statement is not visible under default account
|
||||
JsonNode defaultStatements = redshiftDataService.listStatements(JsonNodeFactory.instance.objectNode());
|
||||
assertEquals(0, defaultStatements.path("Statements").size());
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class,
|
||||
() -> redshiftDataService.describeStatement(JsonNodeFactory.instance.objectNode().put("Id", statementId)));
|
||||
assertEquals("ResourceNotFoundException", exception.getErrorCode());
|
||||
}
|
||||
|
||||
// The SQL runs off the invoking thread. Waiting for its outcome shows it ran as the schedule's account
|
||||
// too, and keeps a late write from leaking into the next test.
|
||||
private void awaitFailed(String statementId) {
|
||||
await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> assertEquals("FAILED",
|
||||
RequestScopes.callAs(SCHEDULE_ACCOUNT, () -> redshiftDataService.describeStatement(
|
||||
JsonNodeFactory.instance.objectNode().put("Id", statementId))).path("Status").asText()));
|
||||
}
|
||||
|
||||
private static Schedule scheduleFor(String targetArn, String input) {
|
||||
Target target = new Target();
|
||||
target.setArn(targetArn);
|
||||
target.setRoleArn("arn:aws:iam::" + SCHEDULE_ACCOUNT + ":role/scheduler-role");
|
||||
target.setInput(input);
|
||||
Schedule schedule = new Schedule();
|
||||
schedule.setName("rsdata-account");
|
||||
schedule.setGroupName("default");
|
||||
schedule.setAccountId(SCHEDULE_ACCOUNT);
|
||||
schedule.setArn("arn:aws:scheduler:us-east-1:" + SCHEDULE_ACCOUNT + ":schedule/default/rsdata-account");
|
||||
schedule.setTarget(target);
|
||||
return schedule;
|
||||
}
|
||||
}
|
||||
+152
-3
@@ -1,13 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.scheduler;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsService;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.AwsVpcConfiguration;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.EcsParameters;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.EventBridgeParameters;
|
||||
@@ -21,10 +24,13 @@ 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.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -41,6 +47,7 @@ import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -58,6 +65,8 @@ class ScheduleInvokerTest {
|
||||
private EventBridgeService eventBridgeService;
|
||||
private EcsService ecsService;
|
||||
private StepFunctionsService stepFunctionsService;
|
||||
private RedshiftDataService redshiftDataService;
|
||||
private final List<Runnable> submittedStatements = new ArrayList<>();
|
||||
private ScheduleInvoker invoker;
|
||||
|
||||
@BeforeEach
|
||||
@@ -68,10 +77,19 @@ class ScheduleInvokerTest {
|
||||
eventBridgeService = mock(EventBridgeService.class);
|
||||
ecsService = mock(EcsService.class);
|
||||
stepFunctionsService = mock(StepFunctionsService.class);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
redshiftDataService = mock(RedshiftDataService.class);
|
||||
invoker = invokerWithRedshiftEnabled(true, true);
|
||||
}
|
||||
|
||||
private ScheduleInvoker invokerWithRedshiftEnabled(boolean redshiftEnabled, boolean redshiftDataEnabled) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.baseUrl()).thenReturn("http://localhost:4566");
|
||||
invoker = new ScheduleInvoker(sqsService, lambdaService, snsService,
|
||||
eventBridgeService, ecsService, stepFunctionsService, new ObjectMapper(), config);
|
||||
ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
|
||||
when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(redshiftEnabled && redshiftDataEnabled);
|
||||
// Queue instead of run, so a test can see that the invoker returns before the SQL runs.
|
||||
return new ScheduleInvoker(sqsService, lambdaService, snsService,
|
||||
eventBridgeService, ecsService, stepFunctionsService, redshiftDataService,
|
||||
new ObjectMapper(), config, serviceRegistry, submittedStatements::add);
|
||||
}
|
||||
|
||||
/** Delivers one occurrence of a schedule in {@code region} whose target is {@code target}. */
|
||||
@@ -355,6 +373,137 @@ class ScheduleInvokerTest {
|
||||
assertNull(binaryAttribute.getStringValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataExecuteStatementForwardsClusterRequestAndRegion() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\","
|
||||
+ "\"DbUser\":\"admin\",\"Sql\":\"select 1\"}");
|
||||
|
||||
invoke(target, "eu-west-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).submitStatement(request.capture(), eq("eu-west-1"));
|
||||
assertEquals("select 1", request.getValue().path("Sql").asText());
|
||||
assertEquals("analytics", request.getValue().path("ClusterIdentifier").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataExecuteStatementForwardsWorkgroupName() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"WorkgroupName\":\"wg-1\",\"Database\":\"dev\",\"Sql\":\"select 1\"}");
|
||||
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).submitStatement(request.capture(), eq("us-east-1"));
|
||||
assertEquals("wg-1", request.getValue().path("WorkgroupName").asText());
|
||||
assertTrue(request.getValue().path("ClusterIdentifier").isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataBatchExecuteStatementForwardsSqls() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:batchExecuteStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\","
|
||||
+ "\"DbUser\":\"admin\",\"Sqls\":[\"select 1\",\"select 2\"]}");
|
||||
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).submitBatch(request.capture(), eq("us-east-1"));
|
||||
assertEquals(2, request.getValue().path("Sqls").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataRejectionPropagatesForRetryAndDeadLetterHandling() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"Sql\":\"select 1\"}");
|
||||
when(redshiftDataService.submitStatement(any(JsonNode.class), anyString()))
|
||||
.thenThrow(new AwsException("ValidationException", "no cluster", 400));
|
||||
|
||||
assertThrows(AwsException.class, () -> invoke(target, "us-east-1"));
|
||||
assertTrue(submittedStatements.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataRunsTheStatementOffTheCallingThread() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}");
|
||||
Runnable statement = mock(Runnable.class);
|
||||
when(redshiftDataService.submitStatement(any(JsonNode.class), anyString())).thenReturn(statement);
|
||||
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verifyNoInteractions(statement);
|
||||
assertEquals(1, submittedStatements.size());
|
||||
submittedStatements.get(0).run();
|
||||
verify(statement).run();
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataFailedStatementDoesNotEscapeTheExecutor() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Database\":\"dev\",\"Sql\":\"select 1\"}");
|
||||
when(redshiftDataService.submitStatement(any(JsonNode.class), anyString()))
|
||||
.thenReturn(() -> { throw new IllegalStateException("boom"); });
|
||||
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
submittedStatements.get(0).run();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false,true", "true,false"})
|
||||
void universalRedshiftDataFailsWithoutCallingTheServiceWhenItIsDisabled(
|
||||
boolean redshiftEnabled, boolean redshiftDataEnabled) {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"ClusterIdentifier\":\"analytics\",\"Sql\":\"select 1\"}");
|
||||
ScheduleInvoker disabledInvoker = invokerWithRedshiftEnabled(redshiftEnabled, redshiftDataEnabled);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> disabledInvoker.invoke(scheduleIn("us-east-1", target), SCHEDULED_AT));
|
||||
|
||||
assertEquals("ServiceNotAvailableException", failure.getErrorCode());
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataNonJsonInputFailsWithInvalidParameterValue() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("not json");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> invoke(target, "us-east-1"));
|
||||
|
||||
assertEquals("InvalidParameterValue", failure.getErrorCode());
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalRedshiftDataOtherActionsStayUnsupported() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:redshiftdata:describeStatement");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"Id\":\"stmt-1\"}");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> invoke(target, "us-east-1"));
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalSqsSendMessageSkipsAttributesWithoutDataType() {
|
||||
String queueUrl = "http://localhost:4566/000000000000/q.fifo";
|
||||
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
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.redshift.RedshiftService;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* Runs a one-time schedule whose universal target is the Redshift Data API against a real
|
||||
* PostgreSQL-backed cluster. Skipped when Docker is not available.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class SchedulerRedshiftDataDockerIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String CLUSTER_ID = "it-sched-rsdata";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/scheduler-role";
|
||||
private static final String TARGET_ARN = "arn:aws:scheduler:::aws-sdk:redshiftdata:executeStatement";
|
||||
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
|
||||
RedshiftService redshift;
|
||||
|
||||
@Inject
|
||||
ScheduleInvoker scheduleInvoker;
|
||||
|
||||
@Inject
|
||||
SchedulerService schedulerService;
|
||||
|
||||
@Inject
|
||||
SqsService sqsService;
|
||||
|
||||
@Inject
|
||||
EmulatorConfig config;
|
||||
|
||||
private final List<String> scheduleNames = new ArrayList<>();
|
||||
private final List<ScheduleDispatcher> testDispatchers = new ArrayList<>();
|
||||
private boolean clusterCreated;
|
||||
|
||||
@BeforeAll
|
||||
void createCluster() {
|
||||
Assumptions.assumeTrue(dockerAvailable(), "Docker is required for this test");
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
redshift.createCluster(CLUSTER_ID, "dc2.large", "admin", "Secret123");
|
||||
clusterCreated = true;
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
void deleteCluster() {
|
||||
if (clusterCreated) {
|
||||
redshift.deleteCluster(CLUSTER_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
for (String scheduleName : scheduleNames) {
|
||||
try {
|
||||
schedulerService.deleteSchedule(scheduleName, null, REGION);
|
||||
} catch (AwsException expected) {
|
||||
if (!"ResourceNotFoundException".equals(expected.getErrorCode())) {
|
||||
throw expected;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (ScheduleDispatcher dispatcher : testDispatchers) {
|
||||
dispatcher.onStop(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void oneTimeScheduleRunsSqlOnAProvisionedCluster() {
|
||||
String statementName = "sched-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String scheduleName = "rsdata-at-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String input = MAPPER.createObjectNode()
|
||||
.put("ClusterIdentifier", CLUSTER_ID)
|
||||
.put("Database", "dev")
|
||||
.put("DbUser", "admin")
|
||||
.put("StatementName", statementName)
|
||||
.put("Sql", "select 1 as one")
|
||||
.toString();
|
||||
Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS);
|
||||
createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", input);
|
||||
|
||||
dispatcherFor(scheduleName).tick(fireAt);
|
||||
|
||||
String statementId = RestAssuredJsonUtils.awsAction("RedshiftData", "ListStatements",
|
||||
"{\"StatementName\":\"" + statementName + "\"}")
|
||||
.then().statusCode(200).extract().path("Statements[0].Id");
|
||||
await().atMost(Duration.ofSeconds(10)).untilAsserted(() ->
|
||||
assertEquals("FINISHED", RestAssuredJsonUtils.awsAction("RedshiftData", "DescribeStatement",
|
||||
"{\"Id\":\"" + statementId + "\"}")
|
||||
.then().statusCode(200).extract().path("Status")));
|
||||
}
|
||||
|
||||
private void createSchedule(String name, String expression, String input) {
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("ScheduleExpression", expression);
|
||||
request.putObject("FlexibleTimeWindow").put("Mode", "OFF");
|
||||
request.put("State", "ENABLED");
|
||||
ObjectNode target = request.putObject("Target");
|
||||
target.put("Arn", TARGET_ARN);
|
||||
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 static boolean dockerAvailable() {
|
||||
try {
|
||||
Process p = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}")
|
||||
.redirectErrorStream(true).start();
|
||||
return p.waitFor() == 0;
|
||||
} catch (Exception ignored) {
|
||||
// No docker binary or daemon: the test is skipped through the assumption, not failed.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataJsonHandler;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AslExecutorRedshiftDataDisabledTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false,true", "true,false"})
|
||||
void disabledServiceFailsTaskWithoutInvokingHandlerOrService(boolean parentEnabled, boolean childEnabled) {
|
||||
runTask(parentEnabled, childEnabled, "", "FAILED",
|
||||
"RedshiftData.ServiceNotAvailableException", "Service redshift-data is not enabled.");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {".sync", ".sync:2"})
|
||||
void unsupportedSyncResourceFailsWithoutInvokingHandlerOrService(String suffix) {
|
||||
runTask(true, true, suffix, "FAILED", "States.TaskFailed",
|
||||
"Unsupported resource: arn:aws:states:::aws-sdk:redshiftdata:listStatements" + suffix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bothEnabledServiceInvokesOrdinaryTask() {
|
||||
runTask(true, true, "", "SUCCEEDED", null, null);
|
||||
}
|
||||
|
||||
private void runTask(boolean parentEnabled, boolean childEnabled, String suffix,
|
||||
String status, String error, String cause) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
|
||||
when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(parentEnabled && childEnabled);
|
||||
RedshiftDataService service = mock(RedshiftDataService.class);
|
||||
when(service.listStatements(any())).thenReturn(objectMapper.createObjectNode());
|
||||
RedshiftDataJsonHandler handler = spy(new RedshiftDataJsonHandler(service));
|
||||
AslExecutor executor = new AslExecutor(null, null, null, null, null, null, null,
|
||||
null, null, null, null, null, null, null, null, objectMapper,
|
||||
new JsonataEvaluator(objectMapper), null, config, null, null, handler, serviceRegistry);
|
||||
|
||||
StateMachine stateMachine = new StateMachine();
|
||||
stateMachine.setStateMachineArn("arn:aws:states:us-east-1:000000000000:stateMachine:disabled-data");
|
||||
stateMachine.setDefinition("""
|
||||
{"StartAt":"List","States":{"List":{"Type":"Task",
|
||||
"Resource":"arn:aws:states:::aws-sdk:redshiftdata:listStatements","End":true}}}
|
||||
""".replace("listStatements\"", "listStatements" + suffix + "\""));
|
||||
Execution execution = new Execution();
|
||||
execution.setExecutionArn("arn:aws:states:us-east-1:000000000000:execution:disabled-data:run");
|
||||
execution.setStateMachineArn(stateMachine.getStateMachineArn());
|
||||
execution.setInput("{}");
|
||||
try {
|
||||
executor.executeSync(stateMachine, execution, new ArrayList<>(), (updated, events) -> { });
|
||||
|
||||
assertEquals(status, execution.getStatus());
|
||||
assertEquals(error, execution.getError());
|
||||
assertEquals(cause, execution.getCause());
|
||||
if ("SUCCEEDED".equals(status)) {
|
||||
verify(service).listStatements(any());
|
||||
assertEquals("{}", execution.getOutput());
|
||||
} else {
|
||||
verifyNoInteractions(handler, service);
|
||||
}
|
||||
} finally {
|
||||
executor.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.redshift.RedshiftService;
|
||||
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.AfterAll;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
@QuarkusTest
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class StepFunctionsRedshiftDataDockerIntegrationTest {
|
||||
|
||||
private static final String CLUSTER_ID = "it-sfn-rsdata";
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role";
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
@Inject
|
||||
RedshiftService redshift;
|
||||
|
||||
private boolean clusterCreated;
|
||||
|
||||
@BeforeAll
|
||||
void createCluster() {
|
||||
Assumptions.assumeTrue(dockerAvailable(), "Docker is required for this test");
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
redshift.createCluster(CLUSTER_ID, "dc2.large", "admin", "Secret123");
|
||||
clusterCreated = true;
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
void deleteCluster() {
|
||||
if (clusterCreated) {
|
||||
redshift.deleteCluster(CLUSTER_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeThenDescribeStatementOnAProvisionedCluster() throws Exception {
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Run",
|
||||
"States": {
|
||||
"Run": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:redshiftdata:executeStatement",
|
||||
"Arguments": {"ClusterIdentifier": "CLUSTER", "Database": "dev",
|
||||
"DbUser": "admin", "Sql": "select 1 as one"},
|
||||
"Output": "{% $states.result %}",
|
||||
"Next": "Describe"
|
||||
},
|
||||
"Describe": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:redshiftdata:describeStatement",
|
||||
"Arguments": {"Id": "{% $states.input.Id %}"},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".replace("CLUSTER", CLUSTER_ID);
|
||||
|
||||
JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition));
|
||||
assertEquals("FINISHED", result.path("Status").asText());
|
||||
assertEquals(1, result.path("ResultRows").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedStatementIsNotATaskFailure() throws Exception {
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Run",
|
||||
"States": {
|
||||
"Run": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:redshiftdata:executeStatement",
|
||||
"Arguments": {"ClusterIdentifier": "CLUSTER", "Database": "dev",
|
||||
"DbUser": "admin", "Sql": "select * from no_such_table"},
|
||||
"Output": "{% $states.result %}",
|
||||
"Next": "Describe"
|
||||
},
|
||||
"Describe": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:redshiftdata:describeStatement",
|
||||
"Arguments": {"Id": "{% $states.input.Id %}"},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".replace("CLUSTER", CLUSTER_ID);
|
||||
|
||||
JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition));
|
||||
assertEquals("FAILED", result.path("Status").asText());
|
||||
}
|
||||
|
||||
private static boolean dockerAvailable() {
|
||||
try {
|
||||
Process process = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}")
|
||||
.redirectErrorStream(true).start();
|
||||
return process.waitFor() == 0;
|
||||
} catch (Exception ignored) {
|
||||
// An unavailable Docker daemon is an expected reason to skip this integration test.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static String succeedingOutput(String definition) {
|
||||
String stateMachineArn = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"name\":\"rsdata-docker-%s\",\"roleArn\":\"%s\",\"definition\":%s}"
|
||||
.formatted(System.nanoTime(), ROLE_ARN, quote(definition)))
|
||||
.when().post("/").then().statusCode(200).extract().path("stateMachineArn");
|
||||
String executionArn = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.StartExecution")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"stateMachineArn\":\"%s\",\"input\":\"{}\"}".formatted(stateMachineArn))
|
||||
.when().post("/").then().statusCode(200).extract().path("executionArn");
|
||||
for (int i = 0; i < 300; i++) {
|
||||
Response response = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"executionArn\":\"%s\"}".formatted(executionArn))
|
||||
.when().post("/");
|
||||
String status = response.jsonPath().getString("status");
|
||||
if (!"RUNNING".equals(status)) {
|
||||
assertEquals("SUCCEEDED", status, "cause: " + response.jsonPath().getString("cause"));
|
||||
return response.jsonPath().getString("output");
|
||||
}
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
fail("Interrupted while waiting for " + executionArn);
|
||||
}
|
||||
}
|
||||
fail("Execution did not complete: " + executionArn);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String quote(String raw) {
|
||||
return "\"" + raw.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\"";
|
||||
}
|
||||
}
|
||||
+332
@@ -0,0 +1,332 @@
|
||||
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.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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 org.mockito.ArgumentCaptor;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class StepFunctionsRedshiftDataIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String EXECUTE = "arn:aws:states:::aws-sdk:redshiftdata:executeStatement";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role";
|
||||
private static final String ISO_INSTANT = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z";
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
@InjectMock
|
||||
RedshiftDataService redshiftDataService;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void stubService() {
|
||||
when(redshiftDataService.executeStatement(any(JsonNode.class), anyString())).thenAnswer(invocation -> {
|
||||
JsonNode request = invocation.getArgument(0);
|
||||
if ("fail".equals(request.path("Sql").asText())) {
|
||||
throw new AwsException("ValidationException", "synthetic failure", 400);
|
||||
}
|
||||
ObjectNode response = OBJECT_MAPPER.createObjectNode();
|
||||
response.put("Id", "stmt-1");
|
||||
response.put("CreatedAt", 1_700_000_000.5);
|
||||
response.put("Database", request.path("Database").asText());
|
||||
response.put("HasResultSet", true);
|
||||
return response;
|
||||
});
|
||||
when(redshiftDataService.listStatements(any(JsonNode.class))).thenAnswer(invocation -> {
|
||||
ObjectNode response = OBJECT_MAPPER.createObjectNode();
|
||||
ObjectNode statement = response.putArray("Statements").addObject();
|
||||
statement.put("Id", "stmt-1");
|
||||
statement.put("CreatedAt", 1_700_000_000.5);
|
||||
statement.put("UpdatedAt", 1_700_000_001.0);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeStatementForwardsPascalCaseInputAndReturnsIsoCreatedAt() throws Exception {
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Run",
|
||||
"States": {
|
||||
"Run": {
|
||||
"Type": "Task",
|
||||
"Resource": "RESOURCE",
|
||||
"Arguments": {
|
||||
"ClusterIdentifier": "analytics",
|
||||
"Database": "dev",
|
||||
"DbUser": "admin",
|
||||
"Sql": "select 1"
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".replace("RESOURCE", EXECUTE);
|
||||
|
||||
JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition, "{}"));
|
||||
assertEquals("stmt-1", result.path("Id").asText());
|
||||
assertEquals("dev", result.path("Database").asText());
|
||||
assertTrue(result.path("HasResultSet").asBoolean());
|
||||
assertTrue(result.path("CreatedAt").asText().matches(ISO_INSTANT),
|
||||
"CreatedAt is not the SDK's ISO-8601 rendering: " + result.path("CreatedAt"));
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
ArgumentCaptor<String> region = ArgumentCaptor.forClass(String.class);
|
||||
verify(redshiftDataService).executeStatement(request.capture(), region.capture());
|
||||
assertEquals("select 1", request.getValue().path("Sql").asText());
|
||||
assertEquals("analytics", request.getValue().path("ClusterIdentifier").asText());
|
||||
assertEquals("us-east-1", region.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void workgroupNameReachesTheService() throws Exception {
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Run",
|
||||
"States": {
|
||||
"Run": {
|
||||
"Type": "Task",
|
||||
"Resource": "RESOURCE",
|
||||
"Arguments": {"WorkgroupName": "wg-1", "Database": "dev", "Sql": "select 1"},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".replace("RESOURCE", EXECUTE);
|
||||
|
||||
succeedingOutput(definition, "{}");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).executeStatement(request.capture(), anyString());
|
||||
assertEquals("wg-1", request.getValue().path("WorkgroupName").asText());
|
||||
assertTrue(request.getValue().path("ClusterIdentifier").isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listStatementsRendersNestedTimestampsAsIso() throws Exception {
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "List",
|
||||
"States": {
|
||||
"List": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:redshiftdata:listStatements",
|
||||
"Arguments": {},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
ObjectNode source = (ObjectNode) OBJECT_MAPPER.readTree("""
|
||||
{"Statements":[{"Id":"stmt-1","CreatedAt":1700000000.5,"UpdatedAt":1700000001,
|
||||
"SubStatements":[{"CreatedAt":1700000002.25,"ResultRows":7,
|
||||
"Metadata":{"Items":[{"UpdatedAt":1700000003.125,"Sql":"select 1",
|
||||
"Nested":[{"CreatedAt":"already textual","UpdatedAt":null,"Count":42}]}]}}]}],
|
||||
"NextToken":"retained"}
|
||||
""");
|
||||
JsonNode original = source.deepCopy();
|
||||
when(redshiftDataService.listStatements(any(JsonNode.class))).thenReturn(source);
|
||||
JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition, "{}"));
|
||||
JsonNode statement = result.path("Statements").path(0);
|
||||
assertEquals("2023-11-14T22:13:20.500Z", statement.path("CreatedAt").asText());
|
||||
assertEquals("2023-11-14T22:13:21.000Z", statement.path("UpdatedAt").asText());
|
||||
JsonNode subStatement = statement.path("SubStatements").path(0);
|
||||
assertEquals("2023-11-14T22:13:22.250Z", subStatement.path("CreatedAt").asText());
|
||||
assertEquals(7, subStatement.path("ResultRows").asInt());
|
||||
JsonNode item = subStatement.path("Metadata").path("Items").path(0);
|
||||
assertEquals("2023-11-14T22:13:23.125Z", item.path("UpdatedAt").asText());
|
||||
assertEquals("select 1", item.path("Sql").asText());
|
||||
assertEquals(original.path("Statements").path(0).path("SubStatements").path(0)
|
||||
.path("Metadata").path("Items").path(0).path("Nested"), item.path("Nested"));
|
||||
assertEquals("retained", result.path("NextToken").asText());
|
||||
assertEquals(original, source, "Rendering must not mutate the Data API response");
|
||||
assertTrue(source.path("Statements").path(0).path("CreatedAt").isNumber());
|
||||
assertTrue(source.path("Statements").path(0).path("SubStatements").path(0)
|
||||
.path("Metadata").path("Items").path(0).path("UpdatedAt").isNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceFailureCanBeCaughtByItsSdkExceptionName() throws Exception {
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Run",
|
||||
"States": {
|
||||
"Run": {
|
||||
"Type": "Task",
|
||||
"Resource": "RESOURCE",
|
||||
"Arguments": {"Sql": "fail"},
|
||||
"Catch": [{
|
||||
"ErrorEquals": ["RedshiftData.ValidationException"],
|
||||
"Next": "Recovered",
|
||||
"Output": {"Error": "{% $states.errorOutput.Error %}"}
|
||||
}],
|
||||
"End": true
|
||||
},
|
||||
"Recovered": {"Type": "Pass", "End": true}
|
||||
}
|
||||
}
|
||||
""".replace("RESOURCE", EXECUTE);
|
||||
|
||||
JsonNode result = OBJECT_MAPPER.readTree(succeedingOutput(definition, "{}"));
|
||||
assertEquals("RedshiftData.ValidationException", result.path("Error").asText());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {".sync", ".sync:2"})
|
||||
void unsupportedSyncResourceFailsBeforeExecutingSql(String suffix) {
|
||||
String resource = EXECUTE + suffix;
|
||||
String definition = """
|
||||
{"StartAt":"Run","States":{"Run":{"Type":"Task","Resource":"RESOURCE",
|
||||
"Parameters":{"Sql":"select 1"},"End":true}}}
|
||||
""".replace("RESOURCE", resource);
|
||||
Response describe = terminalExecution(definition, "{}");
|
||||
assertEquals("FAILED", describe.jsonPath().getString("status"));
|
||||
assertEquals("States.TaskFailed", describe.jsonPath().getString("error"));
|
||||
assertEquals("Unsupported resource: " + resource, describe.jsonPath().getString("cause"));
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deprecatedExecuteSqlFailsWithValidationException() {
|
||||
String definition = """
|
||||
{
|
||||
"StartAt": "Run",
|
||||
"States": {
|
||||
"Run": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::aws-sdk:redshiftdata:executeSql",
|
||||
"Parameters": {"Sql": "select 1"},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
Response describe = terminalExecution(definition, "{}");
|
||||
assertEquals("FAILED", describe.jsonPath().getString("status"));
|
||||
assertEquals("RedshiftData.ValidationException", describe.jsonPath().getString("error"));
|
||||
verify(redshiftDataService, never()).executeStatement(any(JsonNode.class), anyString());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"[1, 2]", "\"scalar\"", "123"})
|
||||
void nonObjectInputFailsWithValidationException(String input) {
|
||||
String definition = """
|
||||
{
|
||||
"StartAt": "Run",
|
||||
"States": {
|
||||
"Run": {
|
||||
"Type": "Task",
|
||||
"Resource": "RESOURCE",
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".replace("RESOURCE", EXECUTE);
|
||||
|
||||
Response describe = terminalExecution(definition, input);
|
||||
assertEquals("FAILED", describe.jsonPath().getString("status"));
|
||||
assertEquals("RedshiftData.ValidationException", describe.jsonPath().getString("error"));
|
||||
assertEquals("The task input must be a JSON object.", describe.jsonPath().getString("cause"));
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
private static String succeedingOutput(String definition, String input) {
|
||||
Response describe = terminalExecution(definition, input);
|
||||
assertEquals("SUCCEEDED", describe.jsonPath().getString("status"),
|
||||
"cause: " + describe.jsonPath().getString("cause"));
|
||||
return describe.jsonPath().getString("output");
|
||||
}
|
||||
|
||||
private static Response terminalExecution(String definition, String input) {
|
||||
String stateMachineArn = createStateMachine(definition);
|
||||
String executionArn = startExecution(stateMachineArn, input);
|
||||
for (int i = 0; i < 150; i++) {
|
||||
Response response = describeExecution(executionArn);
|
||||
if (!"RUNNING".equals(response.jsonPath().getString("status"))) {
|
||||
return response;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
fail("Interrupted while waiting for execution " + executionArn);
|
||||
}
|
||||
}
|
||||
fail("Execution did not complete within timeout: " + executionArn);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String createStateMachine(String definition) {
|
||||
Response response = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("""
|
||||
{"name": "redshiftdata-%s", "roleArn": "%s", "definition": %s}
|
||||
""".formatted(System.nanoTime(), ROLE_ARN, quote(definition)))
|
||||
.when().post("/");
|
||||
response.then().statusCode(200);
|
||||
return response.jsonPath().getString("stateMachineArn");
|
||||
}
|
||||
|
||||
private static String startExecution(String stateMachineArn, String input) {
|
||||
Response response = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.StartExecution")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("""
|
||||
{"stateMachineArn": "%s", "input": %s}
|
||||
""".formatted(stateMachineArn, quote(input)))
|
||||
.when().post("/");
|
||||
response.then().statusCode(200);
|
||||
return response.jsonPath().getString("executionArn");
|
||||
}
|
||||
|
||||
private static Response describeExecution(String executionArn) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"executionArn\":\"%s\"}".formatted(executionArn))
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static String quote(String raw) {
|
||||
return "\"" + raw
|
||||
.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
.replace("\t", "\\t")
|
||||
+ "\"";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user