fix(cloudformation): wait for a stable ECS service (#4880)

AWS CloudFormation reports an AWS::ECS::Service CREATE_COMPLETE or
UPDATE_COMPLETE only once the service is stable: it polls
DescribeServices until the desired number of tasks of the current
task definition are running. Floci reported the resource complete as
soon as CreateService or UpdateService returned. The tasks were
started later by the ECS reconciler, every five seconds, and the
service's runningCount only caught up a tick after that, so a client
reading the service right after the stack operation saw runningCount
0.

EcsService.awaitServiceStable now waits, like
Ec2Service.awaitContainerLaunch, for runningCount to equal
desiredCount, for the current deployment to run that many tasks, and
for no task of an earlier deployment to be running. Instead of idling
until the next tick it submits up to three reconcile passes to the
single reconciler thread, so they never overlap a scheduled tick.
After five minutes it fails with "Service <arn> did not stabilize.".

The wait ends at once when the service's tasks cannot start. The
reconciler now keeps, in memory, why its last attempt to launch a
task for a service failed, for example an unknown subnet, and the
deployment it was launching for. Only a failure of the deployment
being awaited ends the wait, so a pass that began before an update
cannot fail the update. The record is cleared on a successful launch,
on DeleteService and on a state reset. A task that stops with
TaskFailedToStart during the wait ends it too. A task the reconciler
launches is stamped with the deployment read before its task
definition, not after the launch, so a launch that an update overtakes
counts as the earlier deployment's and is replaced.

The ECS provisioner calls the wait after an in-place update and after
a create. A created service stays marked as owned by the stack while
it waits, so the create rollback deletes it. A replacement created by
a stack update is deleted when it does not stabilize. A service
updated in place that does not stabilize is put back on the task
definition, desired count and network configuration it had, and the
provisioner waits for that to stabilize too, as CloudFormation's
rollback does. A restore that fails or does not stabilize is reported
as a rollback failure, so a rolled-back stack does not claim the prior
configuration is running when it is not.

The ECS service test in CloudFormationIntegrationTest used subnets
and a security group that do not exist, so its tasks could never
start. It now uses the default VPC's.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-09 10:05:39 -04:00
1 parent 3b36a4d0d8
commit 0ec64add2d
7 files changed
+1152 -11

No files matched your search

@@ -0,0 +1,208 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.hasSize;
/**
* CloudFormation reports an {@code AWS::ECS::Service} complete only once the service is stable,
* so a caller that reads the service right after the stack operation finishes sees its tasks
* running. Floci used to report the resource complete as soon as CreateService or UpdateService
* returned, and the tasks only started on a later reconciler tick.
*/
@QuarkusTest
class CloudFormationEcsServiceStableIntegrationTest {
private static final String ECS_TARGET = "AmazonEC2ContainerServiceV20141113.";
private static final String ECS_CT = "application/x-amz-json-1.1";
private static final String TEMPLATE = """
{
"Resources": {
"Cluster": {
"Type": "AWS::ECS::Cluster",
"Properties": { "ClusterName": "%1$s" }
},
"TaskDef": {
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"Family": "%1$s-td",
"ContainerDefinitions": [
{ "Name": "app", "Image": "%2$s", "Essential": true, "Memory": 128 }
]
}
},
"Service": {
"Type": "AWS::ECS::Service",
"Properties": {
"ServiceName": "%1$s-svc",
"Cluster": { "Ref": "Cluster" },
"TaskDefinition": { "Ref": "TaskDef" },
"DesiredCount": %3$d,
"LaunchType": "FARGATE"
}
}
}
}
""";
private static final String AWSVPC_TEMPLATE = """
{
"Resources": {
"Cluster": {
"Type": "AWS::ECS::Cluster",
"Properties": { "ClusterName": "%1$s" }
},
"TaskDef": {
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"Family": "%1$s-td",
"NetworkMode": "awsvpc",
"ContainerDefinitions": [
{ "Name": "app", "Image": "app:v1", "Essential": true, "Memory": 128 }
]
}
},
"Service": {
"Type": "AWS::ECS::Service",
"Properties": {
"ServiceName": "%1$s-svc",
"Cluster": { "Ref": "Cluster" },
"TaskDefinition": { "Ref": "TaskDef" },
"DesiredCount": %2$d,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
"AwsvpcConfiguration": { "Subnets": ["%3$s"] }
}
}
}
}
}
""";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void createStack_ecsService_runsItsDesiredTasksWhenTheStackCompletes() {
String name = "cfn-stable-" + Long.toString(System.nanoTime(), 36);
stackCall("CreateStack", name, TEMPLATE.formatted(name, "app:v1", 1));
assertThat(stackStatus(name), equalTo("CREATE_COMPLETE"));
Response service = describeService(name);
assertThat(service.path("services[0].desiredCount"), equalTo(1));
assertThat(service.path("services[0].runningCount"), equalTo(1));
assertThat(service.path("services[0].deployments"), hasSize(1));
assertThat(service.path("services[0].deployments[0].runningCount"), equalTo(1));
assertThat(service.path("services[0].deployments[0].rolloutState"), equalTo("COMPLETED"));
assertThat(runningTaskDefinitions(name), hasSize(1));
}
@Test
void updateStack_ecsService_runsOnlyTheNewRevisionWhenTheUpdateCompletes() {
String name = "cfn-stable-upd-" + Long.toString(System.nanoTime(), 36);
stackCall("CreateStack", name, TEMPLATE.formatted(name, "app:v1", 1));
stackCall("UpdateStack", name, TEMPLATE.formatted(name, "app:v2", 2));
assertThat(stackStatus(name), equalTo("UPDATE_COMPLETE"));
Response service = describeService(name);
assertThat(service.path("services[0].desiredCount"), equalTo(2));
assertThat(service.path("services[0].runningCount"), equalTo(2));
assertThat(service.path("services[0].taskDefinition"), endsWith(":task-definition/" + name + "-td:2"));
List<String> running = runningTaskDefinitions(name);
assertThat(running, hasSize(2));
assertThat("the previous revision's task has stopped", running,
everyItem(endsWith(":task-definition/" + name + "-td:2")));
}
@Test
void updateStack_ecsServiceThatCannotStabilize_putsThePriorConfigurationBackOnRollback() {
String name = "cfn-stable-rb-" + Long.toString(System.nanoTime(), 36);
String subnet = Ec2Service.defaultSubnetId("us-east-1", "a");
stackCall("CreateStack", name, AWSVPC_TEMPLATE.formatted(name, 1, subnet));
assertThat(stackStatus(name), equalTo("CREATE_COMPLETE"));
stackCall("UpdateStack", name, AWSVPC_TEMPLATE.formatted(name, 2, "subnet-does-not-exist"));
assertThat(stackStatus(name), equalTo("UPDATE_ROLLBACK_COMPLETE"));
Response service = describeService(name);
assertThat("the rollback must not leave the failed update live",
service.path("services[0].desiredCount"), equalTo(1));
assertThat(service.path("services[0].networkConfiguration.awsvpcConfiguration.subnets"),
equalTo(List.of(subnet)));
}
private static void stackCall(String action, String stackName, String template) {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", action)
.formParam("StackName", stackName)
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackId>"));
}
private static String stackStatus(String stackName) {
return given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath().getString("**.find { it.name() == 'StackStatus' }");
}
private static Response describeService(String name) {
return ecs("DescribeServices",
"{\"cluster\":\"" + name + "\",\"services\":[\"" + name + "-svc\"]}");
}
/** The task definition ARN of every RUNNING task of the stack's service. */
private static List<String> runningTaskDefinitions(String name) {
List<String> taskArns = ecs("ListTasks",
"{\"cluster\":\"" + name + "\",\"serviceName\":\"" + name + "-svc\"}").path("taskArns");
if (taskArns.isEmpty()) {
return List.of();
}
String quoted = String.join("\",\"", taskArns);
Response tasks = ecs("DescribeTasks", "{\"cluster\":\"" + name + "\",\"tasks\":[\"" + quoted + "\"]}");
List<String> statuses = tasks.path("tasks.lastStatus");
List<String> definitions = tasks.path("tasks.taskDefinitionArn");
assertThat(statuses, everyItem(equalTo("RUNNING")));
return definitions;
}
private static Response ecs(String action, String body) {
return given()
.header("X-Amz-Target", ECS_TARGET + action)
.contentType(ECS_CT)
.body(body)
.when()
.post("/")
.then()
.statusCode(200)
.extract().response();
}
}
@@ -7438,6 +7438,11 @@ class CloudFormationIntegrationTest {
private static final String ECS_TARGET_PREFIX = "AmazonEC2ContainerServiceV20141113.";
private static final String ECS_CONTENT_TYPE = "application/x-amz-json-1.1";
// The service's tasks must be able to start, since CloudFormation waits for them: an awsvpc
// task gets its ENI in a subnet and security group that exist, here the default VPC's.
private static final String ECS_SUBNET_A = Ec2Service.defaultSubnetId("us-east-1", "a");
private static final String ECS_SUBNET_B = Ec2Service.defaultSubnetId("us-east-1", "b");
private static final String ECS_SECURITY_GROUP = Ec2Service.defaultSecurityGroupId("us-east-1");
private static String outputValue(String describeXml, String key) {
return describeXml.split("<OutputKey>" + key + "</OutputKey>")[1]
@@ -7485,8 +7490,8 @@ class CloudFormationIntegrationTest {
"LaunchType": "FARGATE",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": ["subnet-aaa", "subnet-bbb"],
"SecurityGroups": ["sg-123"],
"Subnets": ["%1$s", "%2$s"],
"SecurityGroups": ["%3$s"],
"AssignPublicIp": "ENABLED"
}
}
@@ -7502,7 +7507,7 @@ class CloudFormationIntegrationTest {
"ServiceArn": { "Value": { "Fn::GetAtt": ["EcsService", "ServiceArn"] } }
}
}
""";
""".formatted(ECS_SUBNET_A, ECS_SUBNET_B, ECS_SECURITY_GROUP);
String stackName = "cfn-ecs-stack";
@@ -7572,8 +7577,8 @@ class CloudFormationIntegrationTest {
.body("services[0].serviceName", equalTo("cfn-ecs-service"))
.body("services[0].desiredCount", equalTo(2))
.body("services[0].launchType", equalTo("FARGATE"))
.body("services[0].networkConfiguration.awsvpcConfiguration.subnets", hasItem("subnet-aaa"))
.body("services[0].networkConfiguration.awsvpcConfiguration.securityGroups", hasItem("sg-123"))
.body("services[0].networkConfiguration.awsvpcConfiguration.subnets", hasItem(ECS_SUBNET_A))
.body("services[0].networkConfiguration.awsvpcConfiguration.securityGroups", hasItem(ECS_SECURITY_GROUP))
.body("services[0].networkConfiguration.awsvpcConfiguration.assignPublicIp", equalTo("ENABLED"));
}
@@ -323,6 +323,180 @@ class EcsCfnProvisionerTest {
assertFalse(provisioner.hasReplacementUpdate(r), "an in-place update owes no cleanup");
}
@Test
void aCreatedServiceIsAwaitedUntilStableBeforeTheResourceCompletes() {
EcsServiceModel created = service("front");
when(ecs.createService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
eq(REGION))).thenReturn(created);
StackResource r = resource("AWS::ECS::Service", "Service");
provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN), ctx());
InOrder order = inOrder(ecs);
order.verify(ecs).createService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(),
isNull(), eq(REGION));
order.verify(ecs).awaitServiceStable(created);
assertEquals(Set.of("Name", "ServiceArn"), r.getAttributes().keySet(),
"a stable service is no longer marked for the create rollback");
}
@Test
void anInPlaceUpdateIsAwaitedUntilStableBeforeTheResourceCompletes() {
EcsServiceModel updated = service("front");
when(ecs.updateService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(3), isNull(), eq(REGION)))
.thenReturn(updated);
StackResource r = resource("AWS::ECS::Service", "Service");
r.getAttributes().put("Name", "front");
provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN)
.put("DesiredCount", "3"), ctx(SERVICE_ARN));
InOrder order = inOrder(ecs);
order.verify(ecs).updateService("web", "front", TASK_DEF_ARN, 3, null, REGION);
order.verify(ecs).awaitServiceStable(updated);
}
@Test
void aCreatedServiceThatDoesNotStabilizeFailsAndStaysOwnedForTheCreateRollback() {
EcsServiceModel created = service("front");
when(ecs.createService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
eq(REGION))).thenReturn(created);
doThrow(new AwsException("NotStabilized", "Service " + SERVICE_ARN + " did not stabilize.", 500))
.when(ecs).awaitServiceStable(created);
StackResource r = resource("AWS::ECS::Service", "Service");
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN), ctx()));
assertEquals("Service " + SERVICE_ARN + " did not stabilize.", e.getMessage());
// The stack's create rollback deletes a CREATE_FAILED resource only when it is marked owned.
assertEquals(SERVICE_ARN, r.getPhysicalId());
assertEquals("true", r.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR));
verify(ecs, never()).deleteService(anyString(), anyString(), eq(true), anyString());
}
@Test
void aReplacementThatDoesNotStabilizeIsDeletedBeforeTheUpdateFails() {
EcsServiceModel replacement = service("front-v2");
when(ecs.createService(eq("web"), eq("front-v2"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
eq(REGION))).thenReturn(replacement);
doThrow(new AwsException("NotStabilized", "did not stabilize", 500))
.when(ecs).awaitServiceStable(replacement);
StackResource r = resource("AWS::ECS::Service", "Service");
r.getAttributes().put("Name", "front");
assertThrows(AwsException.class, () -> provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front-v2").put("TaskDefinition", TASK_DEF_ARN),
ctx(SERVICE_ARN)));
// The engine restores the prior resource on a failed update and never learns the new ARN,
// so the replacement is removed here; the prior service is left alone.
verify(ecs).deleteService("web", "front-v2", true, REGION);
verify(ecs, never()).deleteService("web", "front", true, REGION);
assertNull(r.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR));
assertNull(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));
}
@Test
void aReplacementThatCannotBeDeletedEitherReportsTheRollbackFailure() {
EcsServiceModel replacement = service("front-v2");
when(ecs.createService(eq("web"), eq("front-v2"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
eq(REGION))).thenReturn(replacement);
AwsException notStable = new AwsException("NotStabilized", "did not stabilize", 500);
doThrow(notStable).when(ecs).awaitServiceStable(replacement);
doThrow(new AwsException("ServerException", "busy", 500))
.when(ecs).deleteService("web", "front-v2", true, REGION);
StackResource r = resource("AWS::ECS::Service", "Service");
r.getAttributes().put("Name", "front");
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front-v2").put("TaskDefinition", TASK_DEF_ARN),
ctx(SERVICE_ARN)));
assertEquals(notStable, e, "the stabilization failure is what fails the update");
assertEquals("busy", e.getSuppressed()[0].getMessage());
assertTrue(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR)
.contains(replacement.getServiceArn()));
}
@Test
void anInPlaceUpdateWhoseRestoreFailsReportsTheRollbackFailure() {
EcsServiceModel prior = service("front");
prior.setTaskDefinition(TASK_DEF_ARN);
prior.setDesiredCount(1);
when(ecs.serviceByArn(SERVICE_ARN)).thenReturn(prior);
EcsServiceModel updated = service("front");
when(ecs.updateService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(3), isNull(), eq(REGION)))
.thenReturn(updated);
AwsException notStable = new AwsException("NotStabilized", "did not stabilize", 500);
doThrow(notStable).when(ecs).awaitServiceStable(updated);
when(ecs.updateService("web", "front", TASK_DEF_ARN, 1, null, REGION))
.thenThrow(new AwsException("ServerException", "busy", 500));
StackResource r = resource("AWS::ECS::Service", "Service");
r.getAttributes().put("Name", "front");
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN)
.put("DesiredCount", "3"), ctx(SERVICE_ARN)));
assertEquals(notStable, e, "the stabilization failure is what fails the update");
assertEquals("busy", e.getSuppressed()[0].getMessage());
verify(ecs).updateService("web", "front", TASK_DEF_ARN, 1, null, REGION);
assertTrue(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR).contains(SERVICE_ARN));
}
@Test
void anInPlaceUpdateThatDoesNotStabilizeAwaitsTheRestoredConfiguration() {
EcsServiceModel prior = service("front");
prior.setTaskDefinition(TASK_DEF_ARN);
prior.setDesiredCount(1);
when(ecs.serviceByArn(SERVICE_ARN)).thenReturn(prior);
EcsServiceModel updated = service("front");
when(ecs.updateService("web", "front", TASK_DEF_ARN, 3, null, REGION)).thenReturn(updated);
doThrow(new AwsException("NotStabilized", "did not stabilize", 500)).when(ecs).awaitServiceStable(updated);
EcsServiceModel restored = service("front");
when(ecs.updateService("web", "front", TASK_DEF_ARN, 1, null, REGION)).thenReturn(restored);
StackResource r = resource("AWS::ECS::Service", "Service");
r.getAttributes().put("Name", "front");
assertThrows(AwsException.class, () -> provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN)
.put("DesiredCount", "3"), ctx(SERVICE_ARN)));
InOrder order = inOrder(ecs);
order.verify(ecs).updateService("web", "front", TASK_DEF_ARN, 1, null, REGION);
order.verify(ecs).awaitServiceStable(restored);
assertNull(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));
}
@Test
void anInPlaceUpdateWhoseRestoreDoesNotStabilizeReportsTheRollbackFailure() {
EcsServiceModel prior = service("front");
prior.setTaskDefinition(TASK_DEF_ARN);
prior.setDesiredCount(1);
when(ecs.serviceByArn(SERVICE_ARN)).thenReturn(prior);
EcsServiceModel updated = service("front");
when(ecs.updateService("web", "front", TASK_DEF_ARN, 3, null, REGION)).thenReturn(updated);
AwsException notStable = new AwsException("NotStabilized", "did not stabilize", 500);
doThrow(notStable).when(ecs).awaitServiceStable(updated);
EcsServiceModel restored = service("front");
when(ecs.updateService("web", "front", TASK_DEF_ARN, 1, null, REGION)).thenReturn(restored);
doThrow(new AwsException("NotStabilized", "restore did not stabilize", 500))
.when(ecs).awaitServiceStable(restored);
StackResource r = resource("AWS::ECS::Service", "Service");
r.getAttributes().put("Name", "front");
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, mapper.createObjectNode()
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN)
.put("DesiredCount", "3"), ctx(SERVICE_ARN)));
assertEquals(notStable, e, "the stabilization failure is what fails the update");
assertEquals("restore did not stabilize", e.getSuppressed()[0].getMessage());
assertTrue(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR).contains(SERVICE_ARN));
}
@Test
void aTaskDefinitionUpdateDeregistersTheDisplacedRevisionAfterCommit() {
TaskDefinition next = new TaskDefinition();
@@ -0,0 +1,374 @@
package io.github.hectorvent.floci.services.ecs;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
import io.github.hectorvent.floci.services.ecs.container.EcsTaskHandle;
import io.github.hectorvent.floci.services.ecs.model.AwsVpcConfiguration;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.EcsServiceModel;
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
import io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration;
import io.github.hectorvent.floci.services.ecs.model.NetworkMode;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import io.github.hectorvent.floci.services.ecs.model.TaskStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* The wait CloudFormation runs before it reports an {@code AWS::ECS::Service} complete. These
* services are built without CDI, so no scheduled tick ever runs: every task the tests see was
* started by a reconcile pass the wait itself asked for.
*/
class EcsServiceAwaitStableTest {
private static final String REGION = "us-east-1";
private static final Duration GENEROUS = Duration.ofSeconds(30);
private final List<EcsService> created = new ArrayList<>();
@AfterEach
void stopReconcilers() {
created.forEach(EcsService::stopManagedContainers);
}
@Test
void awaitServiceStable_newService_returnsOnceDescribeServicesReportsTheDesiredTasks() {
EcsService service = mockModeService();
service.createCluster("stable-cluster", REGION);
registerTaskDef(service, "stable-fam", "app:1");
EcsServiceModel svc = service.createService("stable-cluster", "stable-svc", "stable-fam", 2,
LaunchType.FARGATE, List.of(), null, REGION);
service.awaitServiceStable(svc, GENEROUS);
EcsServiceModel described = describe(service, "stable-cluster", "stable-svc");
assertEquals(2, described.getRunningCount(), "DescribeServices reports the tasks the wait waited for");
assertEquals(2, service.deploymentsFor(described).getFirst().getRunningCount());
assertEquals(2, runningTasks(service).size());
}
@Test
void awaitServiceStable_desiredCountZero_returnsAtOnceWithoutStartingATask() {
EcsService service = mockModeService();
service.createCluster("zero-cluster", REGION);
registerTaskDef(service, "zero-fam", "app:1");
EcsServiceModel svc = service.createService("zero-cluster", "zero-svc", "zero-fam", 0,
LaunchType.FARGATE, List.of(), null, REGION);
// A zero timeout leaves no room for a reconcile pass: only a service that is already
// stable when the wait starts gets through.
service.awaitServiceStable(svc, Duration.ZERO);
assertTrue(runningTasks(service).isEmpty());
}
@Test
void awaitServiceStable_rollingUpdate_returnsOnlyOnceThePreviousDeploymentsTasksHaveStopped() {
EcsService service = mockModeService();
service.createCluster("roll-cluster", REGION);
registerTaskDef(service, "roll-fam", "app:1");
EcsServiceModel svc = service.createService("roll-cluster", "roll-svc", "roll-fam", 1,
LaunchType.FARGATE, List.of(), null, REGION);
service.awaitServiceStable(svc, GENEROUS);
TaskDefinition rev2 = registerTaskDef(service, "roll-fam", "app:2");
EcsServiceModel updated = service.updateService("roll-cluster", "roll-svc",
rev2.getTaskDefinitionArn(), 2, null, REGION);
service.awaitServiceStable(updated, GENEROUS);
List<EcsTask> running = runningTasks(service);
assertEquals(2, running.size());
assertThat(running.stream().map(EcsTask::getTaskDefinitionArn).toList(),
everyItem(equalTo(rev2.getTaskDefinitionArn())));
assertEquals(2, describe(service, "roll-cluster", "roll-svc").getRunningCount());
}
@Test
void awaitServiceStable_reconcilesOnTheReconcilerThreadNotTheCallers() {
List<String> launchThreads = Collections.synchronizedList(new ArrayList<>());
EcsService service = dockerModeService(dockerContainerManager(
task -> launchThreads.add(Thread.currentThread().getName())));
service.createCluster("thread-cluster", REGION);
registerTaskDef(service, "thread-fam", "app:1");
EcsServiceModel svc = service.createService("thread-cluster", "thread-svc", "thread-fam", 1,
LaunchType.FARGATE, List.of(), null, REGION);
service.awaitServiceStable(svc, GENEROUS);
assertEquals(List.of("ecs-reconciler"), launchThreads,
"the pass runs on the single reconciler thread, so it never overlaps a scheduled tick");
}
@Test
void awaitServiceStable_serviceThatNeverStabilizes_throwsNotStabilizedOnTimeout() {
EcsContainerManager containerManager = dockerContainerManager(task -> { });
// Every container has exited by the time it is inspected: each pass finds the task gone and
// starts another, so the service never settles and nothing fails to launch.
when(containerManager.getExitCodeIfStopped(anyString())).thenReturn(1);
EcsService service = dockerModeService(containerManager);
service.createCluster("stuck-cluster", REGION);
registerTaskDef(service, "stuck-fam", "app:1");
EcsServiceModel svc = service.createService("stuck-cluster", "stuck-svc", "stuck-fam", 1,
LaunchType.FARGATE, List.of(), null, REGION);
AwsException error = assertThrows(AwsException.class,
() -> service.awaitServiceStable(svc, Duration.ofMillis(300)));
assertEquals("NotStabilized", error.getErrorCode());
assertEquals("Service " + svc.getServiceArn() + " did not stabilize.", error.getMessage());
}
@Test
void awaitServiceStable_taskCannotBeLaunched_failsWithTheLaunchFailureLongBeforeTheTimeout() {
EcsContainerManager containerManager = mock(EcsContainerManager.class);
// What the container manager raises for an awsvpc task whose subnet EC2 does not know.
doAnswer(invocation -> {
EcsTask task = invocation.getArgument(0);
String subnet = task.getNetworkConfiguration().getAwsvpcConfiguration().getSubnets().getFirst();
throw new AwsException("InvalidParameterException", "The subnet ID '" + subnet + "' does not exist", 400);
}).when(containerManager).attachTaskNetwork(any(), any(), anyString());
EcsService service = mockModeService(containerManager);
service.createCluster("net-cluster", REGION);
ContainerDefinition container = new ContainerDefinition();
container.setName("app");
container.setImage("app:1");
service.registerTaskDefinition("net-fam", List.of(container), NetworkMode.awsvpc, null, null,
null, null, List.of(), REGION);
AwsVpcConfiguration awsvpc = new AwsVpcConfiguration();
awsvpc.setSubnets(List.of("subnet-does-not-exist"));
NetworkConfiguration network = new NetworkConfiguration();
network.setAwsvpcConfiguration(awsvpc);
EcsServiceModel svc = service.createService("net-cluster", "net-svc", "net-fam", 1,
LaunchType.FARGATE, List.of(), network, REGION);
long started = System.nanoTime();
AwsException error = assertThrows(AwsException.class,
() -> service.awaitServiceStable(svc, Duration.ofMinutes(1)));
Duration waited = Duration.ofNanos(System.nanoTime() - started);
assertEquals("NotStabilized", error.getErrorCode());
assertEquals("Service " + svc.getServiceArn() + " did not stabilize. Task launch failed: "
+ "The subnet ID 'subnet-does-not-exist' does not exist", error.getMessage());
assertTrue(waited.compareTo(Duration.ofSeconds(10)) < 0, "the wait took " + waited);
}
@Test
void awaitServiceStable_taskFailsToStart_failsWithTheStoppedReasonBeforeTheTimeout() {
EcsService service = dockerModeService(dockerContainerManager(task -> {
throw new IllegalStateException("CannotPullContainerError: pull access denied for no-such-image");
}));
service.createCluster("fail-cluster", REGION);
registerTaskDef(service, "fail-fam", "no-such-image");
EcsServiceModel svc = service.createService("fail-cluster", "fail-svc", "fail-fam", 1,
LaunchType.FARGATE, List.of(), null, REGION);
AwsException error = assertThrows(AwsException.class, () -> service.awaitServiceStable(svc, GENEROUS));
assertEquals("NotStabilized", error.getErrorCode());
assertThat(error.getMessage(), containsString("Service " + svc.getServiceArn() + " did not stabilize."));
assertThat(error.getMessage(), containsString("CannotPullContainerError: pull access denied for no-such-image"));
}
@Test
void awaitServiceStable_launchFailureOfThePreviousDeployment_doesNotEndTheWait() throws InterruptedException {
CountDownLatch staleLaunching = new CountDownLatch(1);
CountDownLatch releaseStale = new CountDownLatch(1);
EcsContainerManager containerManager = mock(EcsContainerManager.class);
EcsService service = mockModeService(containerManager);
Thread stalePass = new Thread(service::reconcile);
stalePass.setDaemon(true);
// The pass that began before the update is still launching for the previous deployment.
doAnswer(invocation -> {
if (Thread.currentThread() == stalePass) {
staleLaunching.countDown();
releaseStale.await();
throw new AwsException("InvalidParameterException", "stale launch failure", 400);
}
return null;
}).when(containerManager).attachTaskNetwork(any(), any(), anyString());
// Its launch fails only once the wait has cleared the service's launch failure and started
// the new deployment's task: when the wait's second pass stops the previous deployment's.
doAnswer(invocation -> {
releaseStale.countDown();
stalePass.join();
return null;
}).when(containerManager).releaseTaskNetwork(any(), anyString());
service.createCluster("race-cluster", REGION);
registerTaskDef(service, "race-fam", "app:1");
EcsServiceModel svc = service.createService("race-cluster", "race-svc", "race-fam", 1,
LaunchType.FARGATE, List.of(), null, REGION);
service.awaitServiceStable(svc, GENEROUS);
service.updateService("race-cluster", "race-svc", null, 2, null, REGION);
stalePass.start();
assertTrue(staleLaunching.await(10, TimeUnit.SECONDS));
TaskDefinition rev2 = registerTaskDef(service, "race-fam", "app:2");
EcsServiceModel updated = service.updateService("race-cluster", "race-svc",
rev2.getTaskDefinitionArn(), 1, null, REGION);
service.awaitServiceStable(updated, GENEROUS);
assertEquals(List.of(rev2.getTaskDefinitionArn()),
runningTasks(service).stream().map(EcsTask::getTaskDefinitionArn).toList());
}
@Test
void awaitServiceStable_launchOfThePreviousDeploymentOvertakenByAnUpdate_isReplaced()
throws InterruptedException {
CountDownLatch staleLaunching = new CountDownLatch(1);
CountDownLatch releaseStale = new CountDownLatch(1);
EcsContainerManager containerManager = mock(EcsContainerManager.class);
EcsService service = mockModeService(containerManager);
Thread stalePass = new Thread(service::reconcile);
stalePass.setDaemon(true);
// The pass launching the first revision's task is still attaching its network when the update lands.
doAnswer(invocation -> {
if (Thread.currentThread() == stalePass) {
staleLaunching.countDown();
releaseStale.await();
}
return null;
}).when(containerManager).attachTaskNetwork(any(), any(), anyString());
service.createCluster("overtaken-cluster", REGION);
TaskDefinition rev1 = registerTaskDef(service, "overtaken-fam", "app:1");
service.createService("overtaken-cluster", "overtaken-svc", rev1.getTaskDefinitionArn(), 1,
LaunchType.FARGATE, List.of(), null, REGION);
stalePass.start();
assertTrue(staleLaunching.await(10, TimeUnit.SECONDS));
TaskDefinition rev2 = registerTaskDef(service, "overtaken-fam", "app:2");
EcsServiceModel updated = service.updateService("overtaken-cluster", "overtaken-svc",
rev2.getTaskDefinitionArn(), 1, null, REGION);
releaseStale.countDown();
stalePass.join(TimeUnit.SECONDS.toMillis(10));
service.awaitServiceStable(updated, GENEROUS);
assertEquals(List.of(rev2.getTaskDefinitionArn()),
runningTasks(service).stream().map(EcsTask::getTaskDefinitionArn).toList());
}
@Test
void deleteService_forgetsTheServicesLaunchFailure() {
EcsContainerManager containerManager = mock(EcsContainerManager.class);
doThrow(new AwsException("InvalidParameterException", "no such subnet", 400))
.when(containerManager).attachTaskNetwork(any(), any(), anyString());
EcsService service = mockModeService(containerManager);
service.createCluster("gone-cluster", REGION);
registerTaskDef(service, "gone-fam", "app:1");
EcsServiceModel svc = service.createService("gone-cluster", "gone-svc", "gone-fam", 1,
LaunchType.FARGATE, List.of(), null, REGION);
service.reconcile();
assertTrue(service.hasLaunchFailure(svc.getServiceArn()));
service.deleteService("gone-cluster", "gone-svc", true, REGION);
assertFalse(service.hasLaunchFailure(svc.getServiceArn()));
}
private EcsService mockModeService() {
return mockModeService(mock(EcsContainerManager.class));
}
private EcsService mockModeService(EcsContainerManager containerManager) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().ecs().mock()).thenReturn(true);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
return track(new EcsService(new RegionResolver(REGION, "000000000000"), containerManager,
config, mock(EcsLoadBalancerRegistrar.class), new InMemoryStorageFactory(), null));
}
/** A container manager that runs {@code onStart} in {@code startTask}; its containers keep running. */
private static EcsContainerManager dockerContainerManager(Consumer<EcsTask> onStart) {
EcsContainerManager containerManager = mock(EcsContainerManager.class);
// Mockito answers 0 for an Integer, which reads as "every container exited".
when(containerManager.getExitCodeIfStopped(anyString())).thenReturn(null);
when(containerManager.startTask(any(), any(), any(), anyString())).thenAnswer(invocation -> {
EcsTask task = invocation.getArgument(0);
onStart.accept(task);
return new EcsTaskHandle(task.getTaskArn(), Map.of("app", "docker-" + task.getTaskArn().hashCode()),
Map.of());
});
return containerManager;
}
private EcsService dockerModeService(EcsContainerManager containerManager) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().ecs().mock()).thenReturn(false);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
return track(new EcsService(new RegionResolver(REGION, "000000000000"), containerManager,
config, mock(EcsLoadBalancerRegistrar.class), new InMemoryStorageFactory(), null));
}
private EcsService track(EcsService service) {
service.initializeStorage();
created.add(service);
return service;
}
private static TaskDefinition registerTaskDef(EcsService service, String family, String image) {
ContainerDefinition container = new ContainerDefinition();
container.setName("app");
container.setImage(image);
container.setEssential(true);
return service.registerTaskDefinition(family, List.of(container), null, null, null,
null, null, List.of(), REGION);
}
private static EcsServiceModel describe(EcsService service, String cluster, String name) {
return service.describeServices(cluster, List.of(name), REGION).getFirst();
}
private static List<EcsTask> runningTasks(EcsService service) {
return service.describeTasks(null, service.listTasks(null, null, null, null, REGION), REGION).stream()
.filter(t -> TaskStatus.RUNNING.name().equals(t.getLastStatus()))
.toList();
}
private static final class InMemoryStorageFactory extends StorageFactory {
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
private InMemoryStorageFactory() {
super(null, null);
}
@Override
@SuppressWarnings("unchecked")
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
TypeReference<Map<String, V>> typeReference) {
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
}
}
}