fix(autoscaling): fail SSM commands for stale ASG instances (#1600)

* fix(autoscaling): fail SSM commands for stale ASG instances

* fix(ssm): clear stale instance message indexes
This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-07-01 22:28:04 -05:00
1 parent fef03c6b0f
commit a895802b8b
4 files changed
+173 -1

No files matched your search

@@ -12,11 +12,13 @@ import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;
import io.github.hectorvent.floci.services.elbv2.model.TargetHealth;
import io.github.hectorvent.floci.services.ssm.SsmCommandService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.eq;
@@ -293,6 +295,36 @@ class AutoScalingReconcilerTest {
eq("Successful"));
}
@Test
void reconcileFailsActiveSsmInvocationsBeforePruningStaleInstance() {
AutoScalingService asgService = mock(AutoScalingService.class);
Ec2Service ec2Service = mock(Ec2Service.class);
ElbV2Service elbV2Service = mock(ElbV2Service.class);
SsmCommandService ssmCommandService = mock(SsmCommandService.class);
AutoScalingReconciler reconciler = new AutoScalingReconciler(
asgService, ec2Service, elbV2Service, ssmCommandService);
AutoScalingGroup asg = new AutoScalingGroup();
asg.setRegion("us-east-1");
asg.setAutoScalingGroupName("app-asg");
asg.setDesiredCapacity(0);
asg.getInstances().add(instance("i-dead", "Pending"));
Instance ec2Instance = new Instance();
ec2Instance.setInstanceId("i-dead");
ec2Instance.setState(InstanceState.terminated());
Reservation reservation = new Reservation();
reservation.setInstances(List.of(ec2Instance));
when(ec2Service.describeInstances("us-east-1", List.of("i-dead"), null))
.thenReturn(List.of(reservation));
when(ssmCommandService.failActiveInvocationsForInstances("us-east-1", Set.of("i-dead"), "Undeliverable"))
.thenReturn(1);
reconciler.reconcile(asg);
assertEquals(0, asg.getInstances().size());
verify(ssmCommandService).failActiveInvocationsForInstances("us-east-1", Set.of("i-dead"), "Undeliverable");
}
@Test
void reconcilePromotesPendingAsgInstanceWhenEc2InstanceIsRunning() {
AutoScalingService asgService = mock(AutoScalingService.class);
@@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -98,6 +99,71 @@ class SsmCommandServiceDirectExecutionTest {
assertEquals(1, service.getMessages("i-agent", "request-id", 30).size());
}
@Test
void unavailableInstanceFailsQueuedCommandInvocation() throws Exception {
SsmDirectCommandExecutor executor = mock(SsmDirectCommandExecutor.class);
when(executor.supports(eq("i-stale"), eq("AWS-RunShellScript"))).thenReturn(false);
SsmCommandService service = new SsmCommandService(
new InMemoryStorageFactory(),
objectMapper,
regionResolver,
executor);
Command command = service.sendCommand(objectMapper.readTree("""
{
"InstanceIds": ["i-stale"],
"DocumentName": "AWS-RunShellScript",
"Parameters": {
"commands": ["echo hello"]
},
"TimeoutSeconds": 60
}
"""), "us-west-2");
assertEquals(1, service.failActiveInvocationsForInstance("us-west-2", "i-stale", "Undeliverable"));
CommandInvocation invocation = service.getCommandInvocation(command.getCommandId(), "i-stale", "us-west-2");
assertEquals("Failed", invocation.getStatus());
assertEquals("Undeliverable", invocation.getStatusDetails());
assertEquals(-1, invocation.getResponseCode());
assertEquals("Failed", service.listCommands(command.getCommandId(), null, "us-west-2").getFirst().getStatus());
assertTrue(service.getMessages("i-stale", "request-id", 30).isEmpty());
}
@Test
void unavailableInstancesFailQueuedCommandInvocationsInBulk() throws Exception {
SsmDirectCommandExecutor executor = mock(SsmDirectCommandExecutor.class);
when(executor.supports(any(), eq("AWS-RunShellScript"))).thenReturn(false);
SsmCommandService service = new SsmCommandService(
new InMemoryStorageFactory(),
objectMapper,
regionResolver,
executor);
Command command = service.sendCommand(objectMapper.readTree("""
{
"InstanceIds": ["i-stale-a", "i-stale-b", "i-active"],
"DocumentName": "AWS-RunShellScript",
"Parameters": {
"commands": ["echo hello"]
},
"TimeoutSeconds": 60
}
"""), "us-west-2");
assertEquals(2, service.failActiveInvocationsForInstances(
"us-west-2", Set.of("i-stale-a", "i-stale-b"), "Undeliverable"));
assertEquals("Failed", service.getCommandInvocation(command.getCommandId(), "i-stale-a", "us-west-2").getStatus());
assertEquals("Failed", service.getCommandInvocation(command.getCommandId(), "i-stale-b", "us-west-2").getStatus());
assertEquals("Pending", service.getCommandInvocation(command.getCommandId(), "i-active", "us-west-2").getStatus());
assertTrue(service.getMessages("i-stale-a", "request-id", 30).isEmpty());
assertTrue(service.getMessages("i-stale-b", "request-id", 30).isEmpty());
assertEquals(1, service.getMessages("i-active", "request-id", 30).size());
}
@Test
void directTimeoutMarksCommandTimedOut() throws Exception {
SsmDirectCommandExecutor executor = mock(SsmDirectCommandExecutor.class);