mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(scheduler): deliver the default event to targets without Input (#4018)
* fix(scheduler): deliver the default event to targets without Input
Templated targets without Input received "{}" instead of the default
"Scheduled Event" notification that EventBridge Scheduler delivers, so
consumers that switch on detail-type or source never matched. Only SQS
targets had a dead-letter body in the shape of the target service's API;
every other target's body was the raw payload. CreateSchedule and
UpdateSchedule also accepted a non-JSON Input for the templated Lambda,
Step Functions, and EventBridge targets that the API reference requires
to be well-formed JSON.
Deliver the default notification (version, id, detail-type, source,
account, time, region, resources, and detail as the string "{}") to
every templated target whose Input is absent, with an id that is stable
per occurrence so retries and the dead-letter body carry the same event.
Materialize dead-letter bodies with the request fields of Lambda Invoke,
SNS Publish, Step Functions StartExecution, EventBridge PutEvents, and
ECS RunTask, next to the existing SQS SendMessage shape. Reject a
non-JSON Input for Lambda, Step Functions, and EventBridge targets with
a ValidationException, while SQS, SNS, and universal targets keep
accepting any text.
The invoker now takes the schedule and the occurrence time, since the
default event needs the schedule ARN, account, region, and time. RoleArn
is still not consulted at delivery: no Floci delivery path evaluates a
role's permissions, so that is left for a platform-wide follow-up.
Refs #3951
* fix(scheduler): reject blank and trailing-token Input for JSON targets
CreateSchedule and UpdateSchedule checked Input with readTree, which
accepts text after the JSON value ("{} garbage") and returns a missing
node for a blank value, so both were stored for Lambda, Step Functions,
and EventBridge targets. Parse with FAIL_ON_TRAILING_TOKENS and reject a
blank Input, which the API reference gives a minimum length of 1.
Import the scheduler model's AwsVpcConfiguration in ScheduleInvoker
instead of qualifying it inline. The ECS model's type of the same name
stays qualified, with a comment naming the clash.
This commit is contained in:
1 parent
bb67a443ce
commit
c0ed88d647
8 files changed
+590
-125
No files matched your search
+34
-34
@@ -75,8 +75,8 @@ class ScheduleDispatcherTest {
|
||||
s.getTarget().setDeadLetterConfig(new DeadLetterConfig(DLQ_TARGET_ARN));
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker)
|
||||
.invoke(eq(s.getTarget()), anyString());
|
||||
when(invoker.materializeRequest(eq(s.getTarget()), anyString())).thenReturn(TARGET_REQUEST);
|
||||
.invoke(eq(s), any());
|
||||
when(invoker.materializeRequest(eq(s), any())).thenReturn(TARGET_REQUEST);
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(s.getTarget(), "eu-central-1");
|
||||
verify(invoker, times(1)).invoke(eq(s), eq(Instant.parse("2026-04-21T09:17:54Z")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,7 +118,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:17:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -130,7 +130,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:20:00Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -163,7 +163,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -174,7 +174,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,7 +185,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -195,13 +195,13 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:04:00Z"));
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:06:00Z"));
|
||||
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:11:01Z"));
|
||||
verify(invoker, times(2)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -210,7 +210,7 @@ class ScheduleDispatcherTest {
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
|
||||
assertDoesNotThrow(() -> dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z")));
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -221,7 +221,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -232,7 +232,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 3);
|
||||
|
||||
verify(invoker, times(1)).invoke(s.getTarget(), "eu-central-1");
|
||||
verify(invoker, times(1)).invoke(eq(s), eq(Instant.parse("2026-04-21T09:17:54Z")));
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 3);
|
||||
|
||||
verify(invoker, never()).invoke(any(), anyString());
|
||||
verify(invoker, never()).invoke(any(), any());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -253,13 +253,13 @@ class ScheduleDispatcherTest {
|
||||
s.getTarget().setRetryPolicy(new RetryPolicy(3600, 1));
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker)
|
||||
.invoke(eq(s.getTarget()), anyString());
|
||||
.invoke(eq(s), any());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
|
||||
verify(invoker, times(2)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -268,15 +268,15 @@ class ScheduleDispatcherTest {
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:59Z"));
|
||||
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
verify(invoker, times(2)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:20:59Z"));
|
||||
verify(invoker, times(2)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
verify(invoker, times(3)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(3)).invoke(eq(s), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -286,7 +286,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
|
||||
verify(invoker, times(2)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -297,7 +297,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T11:18:00Z"));
|
||||
|
||||
verify(invoker, times(2)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(2)).invoke(eq(s), any());
|
||||
Map<String, MessageAttributeValue> attributes = deadLetters(1).get(0);
|
||||
assertEquals("MaximumRetryAttempts", attribute(attributes, "EXHAUSTED_RETRY_CONDITION"));
|
||||
assertEquals("1", attribute(attributes, "RETRY_ATTEMPTS"));
|
||||
@@ -311,7 +311,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
Map<String, MessageAttributeValue> attributes = deadLetters(1).get(0);
|
||||
assertEquals("MaximumEventAgeInSeconds", attribute(attributes, "EXHAUSTED_RETRY_CONDITION"));
|
||||
assertEquals("0", attribute(attributes, "RETRY_ATTEMPTS"));
|
||||
@@ -321,13 +321,13 @@ class ScheduleDispatcherTest {
|
||||
void sendsExhaustedOccurrenceToDeadLetterQueueWithSchedulerAttributes() {
|
||||
Schedule s = failingSchedule("dead-letter", "at(2026-04-21T09:17:54)", new RetryPolicy(3600, 2));
|
||||
doThrow(new AwsException("AWS.SimpleQueueService.NonExistentQueue",
|
||||
"The specified queue does not exist.", 400)).when(invoker).invoke(eq(s.getTarget()), anyString());
|
||||
"The specified queue does not exist.", 400)).when(invoker).invoke(eq(s), any());
|
||||
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:21:00Z"));
|
||||
|
||||
verify(invoker, times(3)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(3)).invoke(eq(s), any());
|
||||
Map<String, MessageAttributeValue> attributes = deadLetters(1).get(0);
|
||||
assertEquals(9, attributes.size());
|
||||
assertEquals("AWS.SimpleQueueService.NonExistentQueue", attribute(attributes, "ERROR_CODE"));
|
||||
@@ -339,7 +339,7 @@ class ScheduleDispatcherTest {
|
||||
assertEquals("2026-04-21T09:17:54Z", attribute(attributes, "SCHEDULED_TIME"));
|
||||
assertEquals(s.getArn(), attribute(attributes, "SCHEDULE_ARN"));
|
||||
assertEquals(SQS_TARGET_ARN, attribute(attributes, "TARGET_ARN"));
|
||||
verify(invoker, never()).invoke(argThat(target -> DLQ_TARGET_ARN.equals(target.getArn())), anyString());
|
||||
verify(invoker, never()).invoke(argThat(schedule -> DLQ_TARGET_ARN.equals(schedule.getTarget().getArn())), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -351,11 +351,11 @@ class ScheduleDispatcherTest {
|
||||
topic.getTarget().setRetryPolicy(new RetryPolicy(3600, 0));
|
||||
topic.getTarget().setDeadLetterConfig(new DeadLetterConfig("arn:aws:sns:eu-central-1:000000000000:dead-letter"));
|
||||
when(schedulerService.listAllSchedules()).thenReturn(List.of(fifo, topic));
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker).invoke(any(), anyString());
|
||||
doThrow(new RuntimeException("target unavailable")).when(invoker).invoke(any(), any());
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 2);
|
||||
|
||||
verify(invoker, times(2)).invoke(any(), anyString());
|
||||
verify(invoker, times(2)).invoke(any(), any());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -367,7 +367,7 @@ class ScheduleDispatcherTest {
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:10:00Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:12:00Z"));
|
||||
|
||||
verify(invoker, times(4)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(4)).invoke(eq(s), any());
|
||||
List<Map<String, MessageAttributeValue>> deadLetters = deadLetters(2);
|
||||
assertEquals("2026-04-21T09:05:00Z", attribute(deadLetters.get(0), "SCHEDULED_TIME"));
|
||||
assertEquals("2026-04-21T09:10:00Z", attribute(deadLetters.get(1), "SCHEDULED_TIME"));
|
||||
@@ -379,7 +379,7 @@ class ScheduleDispatcherTest {
|
||||
Schedule s = failingSchedule("at-delete", "at(2026-04-21T09:17:54)", new RetryPolicy(3600, 1));
|
||||
s.setActionAfterCompletion("DELETE");
|
||||
s.setAccountId("000000000000");
|
||||
when(invoker.invoke(eq(s.getTarget()), anyString()))
|
||||
when(invoker.invoke(eq(s), any()))
|
||||
.thenThrow(new RuntimeException("target unavailable"))
|
||||
.thenReturn(TARGET_REQUEST);
|
||||
|
||||
@@ -415,7 +415,7 @@ class ScheduleDispatcherTest {
|
||||
s.setState("ENABLED");
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:20Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -426,7 +426,7 @@ class ScheduleDispatcherTest {
|
||||
|
||||
tickEveryTenSeconds("2026-04-21T09:18:00Z", 3);
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
|
||||
@@ -439,7 +439,7 @@ class ScheduleDispatcherTest {
|
||||
s.setLastModificationDate(Instant.parse("2026-04-21T09:18:05Z"));
|
||||
dispatcher.tick(Instant.parse("2026-04-21T09:18:10Z"));
|
||||
|
||||
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
|
||||
verify(invoker, times(1)).invoke(eq(s), any());
|
||||
verifyNoInteractions(sqsService);
|
||||
}
|
||||
}
|
||||
+156
-24
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.scheduler.model.EventBridgeParameters
|
||||
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.NetworkConfiguration;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.SqsParameters;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Target;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
@@ -23,8 +24,10 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -45,6 +48,7 @@ import static org.mockito.Mockito.when;
|
||||
class ScheduleInvokerTest {
|
||||
|
||||
private static final String TOPIC_ARN = "arn:aws:sns:us-east-1:000000000000:repro-topic";
|
||||
private static final Instant SCHEDULED_AT = Instant.parse("2026-04-21T09:17:54Z");
|
||||
|
||||
private SqsService sqsService;
|
||||
private LambdaService lambdaService;
|
||||
@@ -68,6 +72,24 @@ class ScheduleInvokerTest {
|
||||
eventBridgeService, ecsService, stepFunctionsService, new ObjectMapper(), config);
|
||||
}
|
||||
|
||||
/** Delivers one occurrence of a schedule in {@code region} whose target is {@code target}. */
|
||||
private String invoke(Target target, String region) {
|
||||
return invoker.invoke(scheduleIn(region, target), SCHEDULED_AT);
|
||||
}
|
||||
|
||||
private String materializeRequest(Target target, String region) {
|
||||
return invoker.materializeRequest(scheduleIn(region, target), SCHEDULED_AT);
|
||||
}
|
||||
|
||||
private static Schedule scheduleIn(String region, Target target) {
|
||||
Schedule schedule = new Schedule();
|
||||
schedule.setName("test-schedule");
|
||||
schedule.setGroupName("default");
|
||||
schedule.setArn("arn:aws:scheduler:" + region + ":000000000000:schedule/default/test-schedule");
|
||||
schedule.setTarget(target);
|
||||
return schedule;
|
||||
}
|
||||
|
||||
@Test
|
||||
void universalSnsPublishForwardsMessageAttributes() {
|
||||
Target target = new Target();
|
||||
@@ -80,7 +102,7 @@ class ScheduleInvokerTest {
|
||||
+ "\"EventName\":{\"DataType\":\"String\",\"StringValue\":\"my-subject\"}"
|
||||
+ "}}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(snsService).publish(
|
||||
eq(TOPIC_ARN), isNull(), isNull(),
|
||||
@@ -93,6 +115,39 @@ class ScheduleInvokerTest {
|
||||
isNull(), isNull(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void templatedTargetWithoutInputDeliversTheDefaultScheduledEvent() throws Exception {
|
||||
// Repro from floci-io/floci#3951: a templated target created without Input must receive
|
||||
// Scheduler's default notification, not "{}".
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:sqs:eu-central-1:000000000000:no-input");
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/scheduler-role");
|
||||
|
||||
invoker.invoke(scheduleIn("eu-central-1", target), Instant.parse("2024-11-07T22:05:00Z"));
|
||||
|
||||
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService).sendMessage(eq("http://localhost:4566/000000000000/no-input"), body.capture(),
|
||||
eq(0), isNull(), isNull(), eq("eu-central-1"));
|
||||
assertDefaultScheduledEvent(body.getValue(), "eu-central-1", "2024-11-07T22:05:00Z");
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that {@code delivered} is the default notification Scheduler sends when a templated
|
||||
* target has no Input. The expected shape is the event captured from a real invocation in
|
||||
* aws/aws-lambda-dotnet#1864 (note {@code detail} is the string "{}"); only the id varies.
|
||||
*/
|
||||
private static void assertDefaultScheduledEvent(String delivered, String region, String time) throws Exception {
|
||||
String exampleId = "49672d39-8c3a-4cc6-8de5-c57b0acf718f";
|
||||
String id = new ObjectMapper().readTree(delivered).path("id").asText();
|
||||
assertEquals("{\"version\":\"0\",\"id\":\"" + exampleId + "\","
|
||||
+ "\"detail-type\":\"Scheduled Event\",\"source\":\"aws.scheduler\","
|
||||
+ "\"account\":\"000000000000\",\"time\":\"" + time + "\",\"region\":\"" + region + "\","
|
||||
+ "\"resources\":[\"arn:aws:scheduler:" + region + ":000000000000:schedule/default/test-schedule\"],"
|
||||
+ "\"detail\":\"{}\"}",
|
||||
id.isEmpty() ? delivered : delivered.replace(id, exampleId));
|
||||
assertEquals(id, UUID.fromString(id).toString(), "event id is a UUID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void materializesSqsRequestForDeadLetterBody() {
|
||||
Target target = new Target();
|
||||
@@ -100,7 +155,81 @@ class ScheduleInvokerTest {
|
||||
target.setInput("payload");
|
||||
|
||||
assertEquals("{\"MessageBody\":\"payload\",\"QueueUrl\":\"http://localhost:4566/000000000000/queue\"}",
|
||||
invoker.materializeRequest(target, "us-east-1"));
|
||||
materializeRequest(target, "us-east-1"));
|
||||
}
|
||||
|
||||
// The expected bodies below use the request field names of each target service's API
|
||||
// (Lambda Invoke, SNS Publish, Step Functions StartExecution, EventBridge PutEvents, ECS RunTask),
|
||||
// matching the SQS SendMessage example in the Scheduler dead-letter queue guide.
|
||||
|
||||
@Test
|
||||
void materializesLambdaInvokeRequestForDeadLetterBody() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:lambda:us-east-1:000000000000:function:my-func");
|
||||
target.setInput("{\"hello\":\"world\"}");
|
||||
|
||||
assertEquals("{\"FunctionName\":\"arn:aws:lambda:us-east-1:000000000000:function:my-func\","
|
||||
+ "\"InvocationType\":\"Event\",\"Payload\":\"{\\\"hello\\\":\\\"world\\\"}\"}",
|
||||
materializeRequest(target, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void materializesSnsPublishRequestForDeadLetterBody() {
|
||||
Target target = new Target();
|
||||
target.setArn(TOPIC_ARN);
|
||||
target.setInput("plain text");
|
||||
|
||||
assertEquals("{\"TopicArn\":\"" + TOPIC_ARN + "\",\"Message\":\"plain text\"}",
|
||||
materializeRequest(target, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void materializesStepFunctionsStartExecutionRequestForDeadLetterBody() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:states:eu-west-1:000000000000:stateMachine:scheduled-workflow");
|
||||
target.setInput("{\"order\":{\"id\":42}}");
|
||||
|
||||
assertEquals("{\"stateMachineArn\":\"arn:aws:states:eu-west-1:000000000000:stateMachine:scheduled-workflow\","
|
||||
+ "\"input\":\"{\\\"order\\\":{\\\"id\\\":42}}\"}",
|
||||
materializeRequest(target, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void materializesEventBridgePutEventsRequestForDeadLetterBody() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:events:us-east-1:000000000000:event-bus/my-bus");
|
||||
target.setInput("{\"hello\":\"world\"}");
|
||||
target.setEventBridgeParameters(new EventBridgeParameters("Order Placed", "my.app"));
|
||||
|
||||
assertEquals("{\"Entries\":[{\"EventBusName\":\"my-bus\",\"Source\":\"my.app\","
|
||||
+ "\"DetailType\":\"Order Placed\",\"Detail\":\"{\\\"hello\\\":\\\"world\\\"}\"}]}",
|
||||
materializeRequest(target, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void materializesEcsRunTaskRequestForDeadLetterBody() {
|
||||
Target target = new Target();
|
||||
target.setArn("arn:aws:ecs:us-east-1:000000000000:cluster/proof");
|
||||
EcsParameters ecsParameters = new EcsParameters();
|
||||
ecsParameters.setTaskDefinitionArn("arn:aws:ecs:us-east-1:000000000000:task-definition/proof:1");
|
||||
ecsParameters.setLaunchType("FARGATE");
|
||||
ecsParameters.setGroup("batch-group");
|
||||
ecsParameters.setTaskCount(2);
|
||||
AwsVpcConfiguration vpc = new AwsVpcConfiguration();
|
||||
vpc.setSubnets(List.of("subnet-a", "subnet-b"));
|
||||
vpc.setSecurityGroups(List.of("sg-a"));
|
||||
vpc.setAssignPublicIp("DISABLED");
|
||||
NetworkConfiguration network = new NetworkConfiguration();
|
||||
network.setAwsvpcConfiguration(vpc);
|
||||
ecsParameters.setNetworkConfiguration(network);
|
||||
target.setEcsParameters(ecsParameters);
|
||||
|
||||
assertEquals("{\"cluster\":\"arn:aws:ecs:us-east-1:000000000000:cluster/proof\","
|
||||
+ "\"taskDefinition\":\"arn:aws:ecs:us-east-1:000000000000:task-definition/proof:1\","
|
||||
+ "\"count\":2,\"launchType\":\"FARGATE\",\"group\":\"batch-group\","
|
||||
+ "\"networkConfiguration\":{\"awsvpcConfiguration\":{\"subnets\":[\"subnet-a\",\"subnet-b\"],"
|
||||
+ "\"securityGroups\":[\"sg-a\"],\"assignPublicIp\":\"DISABLED\"}}}",
|
||||
materializeRequest(target, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -115,7 +244,7 @@ class ScheduleInvokerTest {
|
||||
+ "\"BinAttr\":{\"DataType\":\"Binary\",\"BinaryValue\":\"aGVsbG8=\"}"
|
||||
+ "}}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(snsService).publish(
|
||||
eq(TOPIC_ARN), isNull(), isNull(),
|
||||
@@ -135,7 +264,7 @@ class ScheduleInvokerTest {
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"TopicArn\":\"" + TOPIC_ARN + "\",\"Message\":\"hello\"}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(snsService).publish(eq(TOPIC_ARN), isNull(), isNull(),
|
||||
eq("hello"), isNull(),
|
||||
@@ -150,7 +279,7 @@ class ScheduleInvokerTest {
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/x");
|
||||
target.setInput("{\"TopicArn\":\"" + TOPIC_ARN + "\",\"Message\":\"scheduled-universal-target\"}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
// The real TopicArn from Input must be used, NOT the universal-target ARN.
|
||||
verify(snsService).publish(eq(TOPIC_ARN), isNull(), isNull(),
|
||||
@@ -170,7 +299,7 @@ class ScheduleInvokerTest {
|
||||
+ "\"MessageBody\":\"hi\",\"MessageGroupId\":\"g1\","
|
||||
+ "\"MessageDeduplicationId\":\"dedup-1\"}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(sqsService).sendMessage(eq("http://localhost:4566/000000000000/q.fifo"),
|
||||
eq("hi"), eq(0), eq("g1"), eq("dedup-1"),
|
||||
@@ -193,7 +322,7 @@ class ScheduleInvokerTest {
|
||||
+ "\"BinaryAttr\":{\"DataType\":\"Binary.Custom\",\"BinaryValue\":\""
|
||||
+ binaryValueBase64 + "\"}}}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
ArgumentCaptor<Map<String, MessageAttributeValue>> attributesCaptor = ArgumentCaptor.captor();
|
||||
verify(sqsService).sendMessage(eq(queueUrl), eq("hi"), eq(0), eq("g1"), eq("dedup-1"),
|
||||
@@ -230,7 +359,7 @@ class ScheduleInvokerTest {
|
||||
+ "\"Valid\":{\"DataType\":\"String\",\"StringValue\":\"value\"}"
|
||||
+ "}}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
ArgumentCaptor<Map<String, MessageAttributeValue>> attributesCaptor = ArgumentCaptor.captor();
|
||||
verify(sqsService).sendMessage(eq(queueUrl), eq("hi"), eq(0), eq("g1"), eq("dedup-1"),
|
||||
@@ -248,7 +377,7 @@ class ScheduleInvokerTest {
|
||||
target.setInput("{\"hello\":\"world\"}");
|
||||
target.setSqsParameters(new SqsParameters("group-7"));
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(sqsService).sendMessage(anyString(), eq("{\"hello\":\"world\"}"), eq(0),
|
||||
eq("group-7"), isNull(), eq("us-east-1"));
|
||||
@@ -261,7 +390,7 @@ class ScheduleInvokerTest {
|
||||
target.setArn(arn);
|
||||
target.setInput("{\"detail\":{\"job\":\"workflow-recovery\"}}");
|
||||
|
||||
invoker.invoke(target, "ap-south-1");
|
||||
invoke(target, "ap-south-1");
|
||||
|
||||
verify(lambdaService).invokeArn(
|
||||
eq(arn),
|
||||
@@ -276,7 +405,7 @@ class ScheduleInvokerTest {
|
||||
target.setArn(TOPIC_ARN);
|
||||
target.setInput("{\"hello\":\"world\"}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(snsService).publish(eq(TOPIC_ARN), isNull(), eq("{\"hello\":\"world\"}"),
|
||||
eq("Scheduler"), eq("us-east-1"));
|
||||
@@ -291,20 +420,22 @@ class ScheduleInvokerTest {
|
||||
target.setRoleArn("arn:aws:iam::000000000000:role/scheduler-role");
|
||||
target.setInput(input);
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(stepFunctionsService).startExecution(stateMachineArn, null, input, "eu-west-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateMachineTargetWithoutInputUsesEmptyObject() {
|
||||
void stateMachineTargetWithoutInputStartsExecutionWithTheDefaultScheduledEvent() throws Exception {
|
||||
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");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(stepFunctionsService).startExecution(stateMachineArn, null, "{}", "us-east-1");
|
||||
ArgumentCaptor<String> input = ArgumentCaptor.forClass(String.class);
|
||||
verify(stepFunctionsService).startExecution(eq(stateMachineArn), isNull(), input.capture(), eq("us-east-1"));
|
||||
assertDefaultScheduledEvent(input.getValue(), "us-east-1", "2026-04-21T09:17:54Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -312,11 +443,12 @@ class ScheduleInvokerTest {
|
||||
String stateMachineArn = "arn:aws:states:us-east-1:000000000000:stateMachine:missing";
|
||||
Target target = new Target();
|
||||
target.setArn(stateMachineArn);
|
||||
target.setInput("{}");
|
||||
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"));
|
||||
() -> invoke(target, "us-east-1"));
|
||||
|
||||
assertEquals("StateMachineDoesNotExist", error.getErrorCode());
|
||||
}
|
||||
@@ -327,7 +459,7 @@ class ScheduleInvokerTest {
|
||||
target.setArn("arn:aws:states:us-east-1:000000000000:stateMachine:scheduled-workflow:PROD");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> invoker.invoke(target, "us-east-1"));
|
||||
() -> invoke(target, "us-east-1"));
|
||||
verifyNoInteractions(stepFunctionsService);
|
||||
}
|
||||
|
||||
@@ -339,7 +471,7 @@ class ScheduleInvokerTest {
|
||||
target.setInput("{\"hello\":\"world\"}");
|
||||
target.setEventBridgeParameters(new EventBridgeParameters("Order Placed", "my.app"));
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
ArgumentCaptor<List<Map<String, Object>>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(eventBridgeService).putEvents(captor.capture(), eq("us-east-1"));
|
||||
@@ -357,7 +489,7 @@ class ScheduleInvokerTest {
|
||||
target.setArn("arn:aws:events:us-east-1:000000000000:event-bus/my-bus");
|
||||
target.setInput("{\"hello\":\"world\"}");
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
ArgumentCaptor<List<Map<String, Object>>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(eventBridgeService).putEvents(captor.capture(), eq("us-east-1"));
|
||||
@@ -384,7 +516,7 @@ class ScheduleInvokerTest {
|
||||
ecsParameters.setNetworkConfiguration(network);
|
||||
target.setEcsParameters(ecsParameters);
|
||||
|
||||
invoker.invoke(target, "us-west-2");
|
||||
invoke(target, "us-west-2");
|
||||
|
||||
ArgumentCaptor<io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration> networkCaptor =
|
||||
ArgumentCaptor.forClass(io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration.class);
|
||||
@@ -414,7 +546,7 @@ class ScheduleInvokerTest {
|
||||
ecsParameters.setLaunchType("UNKNOWN");
|
||||
target.setEcsParameters(ecsParameters);
|
||||
|
||||
invoker.invoke(target, "us-east-1");
|
||||
invoke(target, "us-east-1");
|
||||
|
||||
verify(ecsService).runTask(
|
||||
eq("arn:aws:ecs:us-east-1:000000000000:cluster/proof"),
|
||||
@@ -434,7 +566,7 @@ class ScheduleInvokerTest {
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:dynamodb:putItem");
|
||||
target.setInput("{}");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> invoker.invoke(target, "us-east-1"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> invoke(target, "us-east-1"));
|
||||
|
||||
verifyNoInteractions(sqsService, lambdaService, snsService, eventBridgeService, ecsService);
|
||||
}
|
||||
@@ -445,7 +577,7 @@ class ScheduleInvokerTest {
|
||||
target.setArn("arn:aws:scheduler:::aws-sdk:sqs:sendMessage");
|
||||
target.setInput("{not-json");
|
||||
|
||||
assertThrows(AwsException.class, () -> invoker.invoke(target, "us-east-1"));
|
||||
assertThrows(AwsException.class, () -> invoke(target, "us-east-1"));
|
||||
|
||||
verifyNoInteractions(sqsService, lambdaService, snsService, eventBridgeService, ecsService);
|
||||
}
|
||||
@@ -456,7 +588,7 @@ class ScheduleInvokerTest {
|
||||
target.setArn("arn:aws:dynamodb:us-east-1:000000000000:table/orders");
|
||||
target.setInput("{}");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> invoker.invoke(target, "us-east-1"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> invoke(target, "us-east-1"));
|
||||
|
||||
verifyNoInteractions(sqsService, lambdaService, snsService, eventBridgeService, ecsService);
|
||||
}
|
||||
|
||||
+25
@@ -546,6 +546,31 @@ class SchedulerIntegrationTest {
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(43)
|
||||
void createScheduleWithNonJsonInputForLambdaTargetReturns400() {
|
||||
// Target.Input in the Scheduler API reference: templated Lambda, Step Functions and
|
||||
// EventBridge targets require well-formed JSON.
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"ScheduleExpression": "rate(1 hour)",
|
||||
"FlexibleTimeWindow": {"Mode": "OFF"},
|
||||
"Target": {
|
||||
"Arn": "arn:aws:lambda:us-east-1:000000000000:function:my-func",
|
||||
"RoleArn": "arn:aws:iam::000000000000:role/scheduler-role",
|
||||
"Input": "not json"
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/schedules/lambda-text-input")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void createScheduleWithEcsParametersRoundTrips() {
|
||||
|
||||
@@ -331,6 +331,104 @@ class SchedulerServiceTest {
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
|
||||
// Target.Input in the Scheduler API reference: templated Lambda, Step Functions and EventBridge
|
||||
// targets require well-formed JSON; other target types accept any text, and universal
|
||||
// (aws-sdk) target input is only checked when the schedule is invoked.
|
||||
|
||||
@Test
|
||||
void createScheduleRejectsNonJsonInputForLambdaTarget() {
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
service.createSchedule(
|
||||
newRequest("s", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target("arn:aws:lambda:us-east-1:000000000000:function:my-func",
|
||||
"arn:aws:iam::000000000000:role/my-role", "not json", null)),
|
||||
REGION));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createScheduleRejectsNonJsonInputForEventBridgeTarget() {
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
service.createSchedule(
|
||||
newRequest("s", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target("arn:aws:events:us-east-1:000000000000:event-bus/my-bus",
|
||||
"arn:aws:iam::000000000000:role/my-role", "{\"unterminated\":", null)),
|
||||
REGION));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateScheduleRejectsNonJsonInputForStepFunctionsTarget() {
|
||||
String stateMachineArn = "arn:aws:states:us-east-1:000000000000:stateMachine:my-workflow";
|
||||
service.createSchedule(
|
||||
newRequest("sfn-upd", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target(stateMachineArn, "arn:aws:iam::000000000000:role/my-role", "{}", null)),
|
||||
REGION);
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
service.updateSchedule(
|
||||
newRequest("sfn-upd", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target(stateMachineArn, "arn:aws:iam::000000000000:role/my-role", "not json", null)),
|
||||
REGION));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createScheduleRejectsInputWithTrailingTokensForLambdaTarget() {
|
||||
// Without FAIL_ON_TRAILING_TOKENS, "{} garbage" parses as the leading object and the
|
||||
// rest is silently dropped.
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
service.createSchedule(
|
||||
newRequest("s", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target("arn:aws:lambda:us-east-1:000000000000:function:my-func",
|
||||
"arn:aws:iam::000000000000:role/my-role", "{} garbage", null)),
|
||||
REGION));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createScheduleRejectsBlankInputForStepFunctionsTarget() {
|
||||
// Target.Input has a minimum length of 1, and readTree returns a missing node for a
|
||||
// blank value rather than failing.
|
||||
for (String blank : List.of("", " ")) {
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
service.createSchedule(
|
||||
newRequest("s", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target("arn:aws:states:us-east-1:000000000000:stateMachine:my-workflow",
|
||||
"arn:aws:iam::000000000000:role/my-role", blank, null)),
|
||||
REGION));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createScheduleAcceptsTextInputForSqsTarget() {
|
||||
Schedule s = service.createSchedule(
|
||||
newRequest("sqs-text", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target("arn:aws:sqs:us-east-1:000000000000:my-queue",
|
||||
"arn:aws:iam::000000000000:role/my-role", "plain text", null)),
|
||||
REGION);
|
||||
assertEquals("plain text", s.getTarget().getInput());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createScheduleAcceptsAnyInputForUniversalTarget() {
|
||||
Schedule s = service.createSchedule(
|
||||
newRequest("universal", null, "rate(1 hour)",
|
||||
new FlexibleTimeWindow("OFF", null),
|
||||
new Target("arn:aws:scheduler:::aws-sdk:lambda:invoke",
|
||||
"arn:aws:iam::000000000000:role/my-role", "not json", null)),
|
||||
REGION);
|
||||
assertEquals("not json", s.getTarget().getInput());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateScheduleMissingRequiredFieldsThrows() {
|
||||
service.createSchedule(
|
||||
|
||||
Reference in new issue
Block a user