fix(ecs): report service deployments and MISSING failures on DescribeServices (#2176)

* fix(ecs): report service deployments and MISSING failures on DescribeServices

DescribeServices omitted services[].deployments entirely and always returned an
empty failures list. Both are read by AWS's own ServicesStable waiter, which
accepts on

  length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`

and fails fast on failures[].reason == "MISSING" (botocore
ecs/2014-11-13/waiters-2.json). Because that file is codegen'd into every SDK,
`aws ecs wait services-stable` and every SDK waiter polled for the full
40 x 15s before giving up, and Terraform's aws_ecs_service never converged
(terraform-provider-aws internal/service/ecs/service.go:2206 applies the same
len(Deployments) == 1 test). Evaluating the acceptor against the old payload
raises JMESPathTypeError, so it could not even be evaluated.

An ACTIVE service now reports exactly one PRIMARY deployment derived from its
current state, with rolloutState COMPLETED once runningCount reaches
desiredCount. The deployment id is derived from the service ARN and task
definition rather than generated per request, so it is stable across calls and
restarts and rolls over when the task definition changes. An unresolvable
service reference is returned as a MISSING failure carrying the ARN it would
have had, instead of being dropped.

Known deviations are documented in docs/services/ecs.md: a single deployment
with no draining ACTIVE alongside it, no deploymentController gating, and
pendingCount always 0.

Closes #2174

* fix(ecs): stamp a deployment with its own start time, not the service's

A task-definition change mints a new deployment id, but both deployment
timestamps were still taken from the service's creation time, so the payload
contradicted itself: a new deployment claiming to have been created when the
service was. botocore documents Deployment.createdAt as "the time when the
service deployment was created", and moto stamps it at deployment construction
(moto/ecs/models.py:540) rather than reusing the service's.

Track when the current deployment began on the service model, set at creation
and on every task-definition change, and report that. Services persisted before
this field existed fall back to the service creation time.

Raised in review on #2176.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-08-08 21:37:59 -05:00
1 parent aed50cc2b2
commit 8b8f837728
8 files changed
+552 -17

No files matched your search

@@ -1,9 +1,12 @@
package com.floci.test;
import org.junit.jupiter.api.*;
import software.amazon.awssdk.core.waiters.WaiterOverrideConfiguration;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.services.ecs.EcsClient;
import software.amazon.awssdk.services.ecs.model.*;
import java.time.Duration;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
@@ -773,6 +776,25 @@ class EcsTests {
assertThat(services).hasSize(1);
assertThat(services.get(0).serviceName()).isEqualTo(serviceName);
assertThat(services.get(0).desiredCount()).isEqualTo(1);
// An ACTIVE service must report exactly one PRIMARY deployment. AWS's ServicesStable
// waiter accepts on length(deployments) == 1, so an empty list silently breaks every
// SDK waiter and Terraform's aws_ecs_service (floci-io/floci#2174).
List<Deployment> deployments = services.get(0).deployments();
assertThat(deployments).hasSize(1);
assertThat(deployments.get(0).status()).isEqualTo("PRIMARY");
assertThat(deployments.get(0).id()).startsWith("ecs-svc/");
assertThat(deployments.get(0).taskDefinition()).isEqualTo(services.get(0).taskDefinition());
assertThat(deployments.get(0).desiredCount()).isEqualTo(1);
assertThat(deployments.get(0).rolloutState())
.isIn(DeploymentRolloutState.COMPLETED, DeploymentRolloutState.IN_PROGRESS);
// Deployments are synthesized per request, so the id must not drift between calls.
List<Service> again = ecs.describeServices(DescribeServicesRequest.builder()
.cluster(clusterName)
.services(serviceName)
.build()).services();
assertThat(again.get(0).deployments().get(0).id()).isEqualTo(deployments.get(0).id());
}
@Test
@@ -807,6 +829,46 @@ class EcsTests {
@Test
@Order(45)
@DisplayName("ServicesStable waiter - reaches stable")
void servicesStableWaiter() {
// The strongest available check on services[].deployments: this drives AWS's own
// waiter acceptor,
// length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`
// rather than our reading of it. With deployments absent the expression never matches.
// waitTimeout is capped so a regression fails in 30s rather than the default 40 x 15s.
WaiterResponse<DescribeServicesResponse> response = ecs.waiter().waitUntilServicesStable(
DescribeServicesRequest.builder()
.cluster(clusterName)
.services(serviceName)
.build(),
WaiterOverrideConfiguration.builder()
.waitTimeout(Duration.ofSeconds(30))
.build());
assertThat(response.matched().response()).isPresent();
assertThat(response.matched().response().get().services().get(0).deployments()).hasSize(1);
}
@Test
@Order(46)
@DisplayName("DescribeServices - unknown service returns a MISSING failure")
void describeServicesReportsMissingService() {
// ECS reports an unresolvable service as a failure rather than erroring, and the
// ServicesStable waiter fails fast on failures[].reason == MISSING. Dropping it
// silently would leave that waiter polling for its full timeout.
DescribeServicesResponse response = ecs.describeServices(DescribeServicesRequest.builder()
.cluster(clusterName)
.services("no-such-service-" + suffix)
.build());
assertThat(response.services()).isEmpty();
assertThat(response.failures()).hasSize(1);
assertThat(response.failures().get(0).reason()).isEqualTo("MISSING");
assertThat(response.failures().get(0).arn()).endsWith("no-such-service-" + suffix);
}
@Test
@Order(47)
@DisplayName("ListServiceDeployments - list deployments")
void listServiceDeployments() {
List<ServiceDeploymentBrief> briefs = ecs.listServiceDeployments(
@@ -830,7 +892,7 @@ class EcsTests {
}
@Test
@Order(46)
@Order(48)
@DisplayName("UpdateService - update desiredCount to 0")
void updateServiceDesiredCount() {
Service updated = ecs.updateService(UpdateServiceRequest.builder()
@@ -843,7 +905,7 @@ class EcsTests {
}
@Test
@Order(47)
@Order(49)
@DisplayName("UpdateService - update taskDefinition")
void updateServiceTaskDefinition() {
Service updated = ecs.updateService(UpdateServiceRequest.builder()
@@ -856,7 +918,7 @@ class EcsTests {
}
@Test
@Order(48)
@Order(50)
@DisplayName("CreateTaskSet - create task set")
void createTaskSet() {
software.amazon.awssdk.services.ecs.model.TaskSet ts =
@@ -920,7 +982,7 @@ class EcsTests {
}
@Test
@Order(49)
@Order(51)
@DisplayName("DeleteService - delete service")
void deleteService() {
Service deleted = ecs.deleteService(DeleteServiceRequest.builder()
@@ -932,7 +994,7 @@ class EcsTests {
}
@Test
@Order(50)
@Order(52)
@DisplayName("ListServices - service no longer in list")
void listServicesAfterDelete() {
List<String> serviceArns = ecs.listServices(ListServicesRequest.builder()
@@ -943,7 +1005,7 @@ class EcsTests {
}
@Test
@Order(51)
@Order(53)
@DisplayName("DeregisterTaskDefinition - deregister revision 1")
void deregisterTaskDefinition() {
TaskDefinition deregistered = ecs.deregisterTaskDefinition(
@@ -955,7 +1017,7 @@ class EcsTests {
}
@Test
@Order(52)
@Order(54)
@DisplayName("ListTaskDefinitions - filter by ACTIVE")
void listTaskDefinitionsActive() {
List<String> activeArns = ecs.listTaskDefinitions(ListTaskDefinitionsRequest.builder()
@@ -968,7 +1030,7 @@ class EcsTests {
}
@Test
@Order(53)
@Order(55)
@DisplayName("DeleteTaskDefinitions - delete INACTIVE")
void deleteTaskDefinitions() {
List<TaskDefinition> deletedDefs = ecs.deleteTaskDefinitions(
@@ -981,7 +1043,7 @@ class EcsTests {
}
@Test
@Order(54)
@Order(56)
@DisplayName("DeleteCluster - fails with running tasks")
void deleteClusterFailsWithTasks() {
ecs.runTask(RunTaskRequest.builder()
@@ -996,7 +1058,7 @@ class EcsTests {
}
@Test
@Order(55)
@Order(57)
@DisplayName("DeleteCluster - delete after stopping tasks")
void deleteCluster() {
List<String> running = ecs.listTasks(ListTasksRequest.builder()
@@ -1019,7 +1081,7 @@ class EcsTests {
}
@Test
@Order(56)
@Order(58)
@DisplayName("ListClusters - cluster no longer in list")
void listClustersAfterDelete() {
List<String> arns = ecs.listClusters(ListClustersRequest.builder().build()).clusterArns();
@@ -1027,7 +1089,7 @@ class EcsTests {
}
@Test
@Order(57)
@Order(59)
@DisplayName("RegisterTaskDefinition - container secrets round trip")
void registerTaskDefinitionWithSecrets() {
String secretFamily = family + "-secrets";
+37 -1
View File
@@ -49,10 +49,46 @@ ECS emulates clusters, task definitions, tasks, and services. In the default con
| `CreateService` | Create a long-running service |
| `UpdateService` | Update desired count, task definition, or deployment config |
| `DeleteService` | Delete a service (supports `force`) |
| `DescribeServices` | Describe one or more services |
| `DescribeServices` | Describe one or more services (includes `deployments`, see below) |
| `ListServices` | List service ARNs in a cluster |
| `ListServicesByNamespace` | List services filtered by Cloud Map namespace |
#### Service deployments
An `ACTIVE` service reports exactly one `PRIMARY` entry in `services[].deployments`,
synthesized from the service's current state rather than tracked as a rollout. This is
what AWS's `ServicesStable` waiter accepts on, so `aws ecs wait services-stable`, the
SDK waiters, and Terraform's `aws_ecs_service` all converge normally. A deleted
(`INACTIVE`) service reports an empty list.
`rolloutState` is `COMPLETED` once `runningCount` reaches `desiredCount`, and
`IN_PROGRESS` before that. The deployment `id` is derived from the service ARN and its
task definition, so it is stable across calls and across restarts, and rolls over when
the task definition changes. `createdAt` tracks the deployment rather than the service:
it is the service's creation time until a task-definition change starts a new
deployment, and moves with it thereafter.
Known differences from AWS:
- There is never a second `ACTIVE` deployment draining alongside the `PRIMARY` one;
Floci swaps the task definition in place.
- `deployments` is reported for every service. AWS omits it for services that use the
`CODE_DEPLOY` or `EXTERNAL` deployment controller, but Floci does not yet record
`deploymentController`, and `ECS` is the AWS default.
- `pendingCount` is always `0`, matching the top-level service field.
- `forceNewDeployment` does not mint a new deployment `id`.
- `updatedAt` equals `createdAt`. AWS advances it as a rollout progresses; Floci has no
intermediate rollout state to report.
#### Unknown services
A service reference that does not resolve is returned in `failures` with
`reason: MISSING` and the ARN the service would have had, rather than being dropped from
the response. `DescribeServices` therefore returns partial results instead of erroring, as
AWS does, and `aws ecs wait services-stable` on a nonexistent service fails immediately
instead of polling for its full timeout. A reference supplied as an ARN is echoed back
unchanged.
### Task Sets
| Operation | Description |
@@ -8,6 +8,8 @@ import io.github.hectorvent.floci.services.ecs.model.CapacityProvider;
import io.github.hectorvent.floci.services.ecs.model.ClusterSetting;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.ContainerInstance;
import io.github.hectorvent.floci.services.ecs.model.Deployment;
import io.github.hectorvent.floci.services.ecs.model.Failure;
import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;
import io.github.hectorvent.floci.services.ecs.model.EcsCluster;
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
@@ -497,13 +499,16 @@ public class EcsJsonHandler {
String cluster = req.has("cluster") ? req.path("cluster").asText() : null;
List<String> serviceIds = jsonArrayToList(req.path("services"));
List<EcsServiceModel> found = service.describeServices(cluster, serviceIds, region);
EcsService.DescribeServicesResult found =
service.describeServicesDetailed(cluster, serviceIds, region);
ObjectNode resp = objectMapper.createObjectNode();
ArrayNode arr = objectMapper.createArrayNode();
found.forEach(s -> arr.add(serviceNode(s)));
found.services().forEach(s -> arr.add(serviceNode(s)));
resp.set("services", arr);
resp.set("failures", objectMapper.createArrayNode());
ArrayNode failures = objectMapper.createArrayNode();
found.failures().forEach(f -> failures.add(failureNode(f)));
resp.set("failures", failures);
return Response.ok(resp).build();
}
@@ -1149,6 +1154,34 @@ public class EcsJsonHandler {
networkConfig.set("awsvpcConfiguration", awsvpcNode);
n.set("networkConfiguration", networkConfig);
}
ArrayNode deployments = objectMapper.createArrayNode();
service.deploymentsFor(s).forEach(d -> deployments.add(deploymentNode(d)));
n.set("deployments", deployments);
return n;
}
private ObjectNode failureNode(Failure f) {
ObjectNode n = objectMapper.createObjectNode();
n.put("arn", f.arn());
n.put("reason", f.reason());
if (f.detail() != null) { n.put("detail", f.detail()); }
return n;
}
private ObjectNode deploymentNode(Deployment d) {
ObjectNode n = objectMapper.createObjectNode();
n.put("id", d.getId());
n.put("status", d.getStatus());
n.put("taskDefinition", d.getTaskDefinition());
n.put("desiredCount", d.getDesiredCount());
n.put("pendingCount", d.getPendingCount());
n.put("runningCount", d.getRunningCount());
n.put("failedTasks", d.getFailedTasks());
n.put("rolloutState", d.getRolloutState());
n.put("rolloutStateReason", d.getRolloutStateReason());
if (d.getLaunchType() != null) { n.put("launchType", d.getLaunchType().name()); }
if (d.getCreatedAt() != null) { n.put("createdAt", d.getCreatedAt().toEpochMilli() / 1000.0); }
if (d.getUpdatedAt() != null) { n.put("updatedAt", d.getUpdatedAt().toEpochMilli() / 1000.0); }
return n;
}
@@ -14,6 +14,8 @@ import io.github.hectorvent.floci.services.ecs.model.CapacityProvider;
import io.github.hectorvent.floci.services.ecs.model.ClusterSetting;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.ContainerInstance;
import io.github.hectorvent.floci.services.ecs.model.Deployment;
import io.github.hectorvent.floci.services.ecs.model.Failure;
import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;
import io.github.hectorvent.floci.services.ecs.model.EcsCluster;
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
@@ -705,6 +707,7 @@ public class EcsService implements ContainerTeardown {
svc.setNetworkConfiguration(networkConfiguration);
svc.setStatus("ACTIVE");
svc.setCreatedAt(Instant.now());
svc.setLastDeploymentAt(svc.getCreatedAt());
if (tags != null && !tags.isEmpty()) {
svc.setTags(new LinkedHashMap<>(tags));
}
@@ -745,6 +748,7 @@ public class EcsService implements ContainerTeardown {
if (taskDefinition != null) {
resolveTaskDefinitionOrThrow(taskDefinition, region);
svc.setTaskDefinition(taskDefinition);
svc.setLastDeploymentAt(Instant.now());
recordServiceDeployment(svc, taskDefinition, region);
}
services.put(key, svc);
@@ -791,16 +795,43 @@ public class EcsService implements ContainerTeardown {
return svc;
}
/** The services that resolved, plus a {@code MISSING} failure for each reference that did not. */
public record DescribeServicesResult(List<EcsServiceModel> services, List<Failure> failures) {}
public List<EcsServiceModel> describeServices(String clusterRef, List<String> serviceIds, String region) {
return describeServicesDetailed(clusterRef, serviceIds, region).services();
}
/**
* Resolves each reference, reporting the ones that do not exist as {@code MISSING} failures
* rather than dropping them. ECS answers a describe for an unknown service with a failure
* entry, not an error, and its {@code ServicesStable} waiter fails fast on
* {@code failures[].reason == "MISSING"} — so silently returning an empty list leaves the
* waiter polling for its full timeout instead of reporting the service does not exist.
*/
public DescribeServicesResult describeServicesDetailed(String clusterRef, List<String> serviceIds,
String region) {
EcsCluster cluster = resolveClusterOrDefault(clusterRef, region);
List<EcsServiceModel> result = new ArrayList<>();
List<Failure> failures = new ArrayList<>();
for (String id : serviceIds) {
EcsServiceModel svc = resolveService(cluster.getClusterName(), id, region);
if (svc != null) {
result.add(svc);
} else {
failures.add(Failure.missing(missingServiceArn(cluster, id, region)));
}
}
return result;
return new DescribeServicesResult(result, failures);
}
/** Echoes an ARN the caller supplied, otherwise builds the ARN the service would have had. */
private String missingServiceArn(EcsCluster cluster, String serviceRef, String region) {
if (serviceRef != null && serviceRef.startsWith("arn:")) {
return serviceRef;
}
return regionResolver.buildArn("ecs", region,
"service/" + cluster.getClusterName() + "/" + serviceRef);
}
public List<String> listServices(String clusterRef, String region) {
@@ -1257,6 +1288,64 @@ public class EcsService implements ContainerTeardown {
.toList();
}
/**
* The deployments of a service, derived from its current state rather than tracked as a
* rollout. An ACTIVE service always has exactly one PRIMARY deployment: AWS's own
* {@code ServicesStable} waiter accepts on
* {@code length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`},
* so reporting none leaves every SDK waiter polling until it times out.
*
* <p>A task-definition change is applied in place rather than draining a previous
* deployment, so there is never a second ACTIVE entry beside the PRIMARY one. An INACTIVE
* service has no deployments.
*/
public List<Deployment> deploymentsFor(EcsServiceModel svc) {
if (svc == null || !"ACTIVE".equals(svc.getStatus())) {
return List.of();
}
String deploymentId = deploymentId(svc);
boolean converged = svc.getRunningCount() >= svc.getDesiredCount();
Deployment d = new Deployment();
d.setId(deploymentId);
d.setStatus("PRIMARY");
d.setTaskDefinition(svc.getTaskDefinition());
d.setDesiredCount(svc.getDesiredCount());
d.setPendingCount(svc.getPendingCount());
d.setRunningCount(svc.getRunningCount());
d.setFailedTasks(0);
d.setRolloutState(converged ? "COMPLETED" : "IN_PROGRESS");
d.setRolloutStateReason("ECS deployment " + deploymentId
+ (converged ? " completed." : " in progress."));
d.setLaunchType(svc.getLaunchType());
// The deployment's own start time, not the service's: a task-definition change mints a
// new deployment id, so reporting service creation here would contradict it. Older
// persisted services predate the field and fall back to the service creation time.
Instant startedAt = svc.getLastDeploymentAt() != null
? svc.getLastDeploymentAt()
: svc.getCreatedAt();
d.setCreatedAt(startedAt);
d.setUpdatedAt(startedAt);
return List.of(d);
}
/**
* A stable {@code ecs-svc/<id>} identifier derived from the service ARN and its task
* definition. Deployments are derived per request, so a random id would differ on every
* {@code DescribeServices} call and read as perpetual drift in clients that persist it.
* Deriving it from persisted state keeps it identical across calls and across restarts,
* while still rolling over when the task definition changes.
*/
private String deploymentId(EcsServiceModel svc) {
String seed = svc.getServiceArn() + ":" + svc.getTaskDefinition();
long hash = 0xcbf29ce484222325L;
for (int i = 0; i < seed.length(); i++) {
hash = (hash ^ seed.charAt(i)) * 0x100000001b3L;
}
return "ecs-svc/" + Long.toUnsignedString(hash);
}
private void recordServiceDeployment(EcsServiceModel svc, String taskDefinition, String region) {
String deploymentId = UUID.randomUUID().toString().replace("-", "");
String deploymentArn = regionResolver.buildArn("ecs", region,
@@ -0,0 +1,53 @@
package io.github.hectorvent.floci.services.ecs.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
/**
* A deployment of an ECS service, as reported in {@code DescribeServices}'
* {@code services[].deployments}. Distinct from {@link ServiceDeployment}, which is the
* separate {@code DescribeServiceDeployments} API shape and carries neither counts nor a
* rollout state.
*/
@RegisterForReflection
public class Deployment {
private String id;
private String status;
private String taskDefinition;
private int desiredCount;
private int pendingCount;
private int runningCount;
private int failedTasks;
private String rolloutState;
private String rolloutStateReason;
private LaunchType launchType;
private Instant createdAt;
private Instant updatedAt;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getTaskDefinition() { return taskDefinition; }
public void setTaskDefinition(String taskDefinition) { this.taskDefinition = taskDefinition; }
public int getDesiredCount() { return desiredCount; }
public void setDesiredCount(int desiredCount) { this.desiredCount = desiredCount; }
public int getPendingCount() { return pendingCount; }
public void setPendingCount(int pendingCount) { this.pendingCount = pendingCount; }
public int getRunningCount() { return runningCount; }
public void setRunningCount(int runningCount) { this.runningCount = runningCount; }
public int getFailedTasks() { return failedTasks; }
public void setFailedTasks(int failedTasks) { this.failedTasks = failedTasks; }
public String getRolloutState() { return rolloutState; }
public void setRolloutState(String rolloutState) { this.rolloutState = rolloutState; }
public String getRolloutStateReason() { return rolloutStateReason; }
public void setRolloutStateReason(String rolloutStateReason) { this.rolloutStateReason = rolloutStateReason; }
public LaunchType getLaunchType() { return launchType; }
public void setLaunchType(LaunchType launchType) { this.launchType = launchType; }
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
public Instant getUpdatedAt() { return updatedAt; }
public void setUpdatedAt(Instant updatedAt) { this.updatedAt = updatedAt; }
}
@@ -21,6 +21,8 @@ public class EcsServiceModel {
private int pendingCount;
private String status;
private Instant createdAt;
/** When the current deployment began: service creation, or the last task-definition change. */
private Instant lastDeploymentAt;
private String namespace;
private String deploymentController;
private Map<String, String> tags = new HashMap<>();
@@ -56,6 +58,8 @@ public class EcsServiceModel {
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
public Instant getLastDeploymentAt() { return lastDeploymentAt; }
public void setLastDeploymentAt(Instant lastDeploymentAt) { this.lastDeploymentAt = lastDeploymentAt; }
public String getNamespace() { return namespace; }
public void setNamespace(String namespace) { this.namespace = namespace; }
@@ -0,0 +1,16 @@
package io.github.hectorvent.floci.services.ecs.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
/**
* A per-resource failure returned alongside the results of an ECS {@code Describe*} call.
* ECS reports a resource it could not find here rather than throwing, so callers get partial
* results; {@code reason} is {@code MISSING} for a resource that does not exist.
*/
@RegisterForReflection
public record Failure(String arn, String reason, String detail) {
public static Failure missing(String arn) {
return new Failure(arn, "MISSING", null);
}
}
@@ -0,0 +1,242 @@
package io.github.hectorvent.floci.services.ecs;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.config.JsonPathConfig;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Integration tests for the two {@code DescribeServices} response fields that AWS's
* {@code ServicesStable} waiter depends on (floci-io/floci#2174).
*
* <p>The waiter accepts on
* {@code length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`}
* and fails fast on {@code failures[].reason == "MISSING"} (botocore
* {@code ecs/2014-11-13/waiters-2.json}). So an ACTIVE service must report exactly one
* deployment, and an unknown service must come back as a failure rather than being dropped;
* getting either wrong leaves {@code aws ecs wait services-stable} and every SDK waiter
* polling until they time out.
*/
@QuarkusTest
class EcsDescribeServicesIntegrationTest {
private static final String TARGET = "AmazonEC2ContainerServiceV20141113.";
private static final String CT = "application/x-amz-json-1.1";
@BeforeAll
static void configure() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
private static Response call(String action, String body) {
return given().contentType(CT).header("X-Amz-Target", TARGET + action)
.body(body)
.when().post("/")
.then().statusCode(200)
.extract().response();
}
private static void seedClusterAndTaskDef(String cluster, String family) {
call("CreateCluster", "{\"clusterName\":\"" + cluster + "\"}");
registerTaskDef(family);
}
private static void registerTaskDef(String family) {
call("RegisterTaskDefinition", "{\"family\":\"" + family + "\","
+ "\"containerDefinitions\":[{\"name\":\"web\",\"image\":\"nginx\",\"memory\":128}]}");
}
/**
* Epoch seconds carry 10 significant digits, more than a float holds. RestAssured's default
* number type is FLOAT_AND_DOUBLE, which silently rounds two timestamps a second apart to the
* same value, so read them as BigDecimal.
*/
private static double epochSeconds(Response response, String path) {
return ((java.math.BigDecimal) response
.jsonPath(new JsonPathConfig(JsonPathConfig.NumberReturnType.BIG_DECIMAL))
.get(path)).doubleValue();
}
private static Response describe(String cluster, String service) {
return call("DescribeServices",
"{\"cluster\":\"" + cluster + "\",\"services\":[\"" + service + "\"]}");
}
@Test
void describeServicesReportsExactlyOnePrimaryDeployment() {
seedClusterAndTaskDef("dep-cluster-1", "dep-td-1");
call("CreateService", "{\"cluster\":\"dep-cluster-1\",\"serviceName\":\"dep-svc-1\","
+ "\"taskDefinition\":\"dep-td-1\",\"desiredCount\":0}");
Response r = describe("dep-cluster-1", "dep-svc-1");
String taskDefinition = r.path("services[0].taskDefinition");
int runningCount = r.path("services[0].runningCount");
int pendingCount = r.path("services[0].pendingCount");
r.then()
.body("services", hasSize(1))
.body("services[0].deployments", hasSize(1))
.body("services[0].deployments[0].status", equalTo("PRIMARY"))
.body("services[0].deployments[0].id", startsWith("ecs-svc/"))
.body("services[0].deployments[0].taskDefinition", equalTo(taskDefinition))
.body("services[0].deployments[0].desiredCount", equalTo(0))
.body("services[0].deployments[0].runningCount", equalTo(runningCount))
.body("services[0].deployments[0].pendingCount", equalTo(pendingCount))
.body("services[0].deployments[0].failedTasks", equalTo(0))
.body("services[0].deployments[0].rolloutStateReason",
startsWith("ECS deployment ecs-svc/"));
}
@Test
void deploymentIdIsStableAcrossCalls() {
// Deployments are synthesized per request. A random id would change on every call and
// surface as perpetual drift in any client that persists it, so this is the regression
// guard for deriving the id from the service ARN and task definition instead.
seedClusterAndTaskDef("dep-cluster-2", "dep-td-2");
call("CreateService", "{\"cluster\":\"dep-cluster-2\",\"serviceName\":\"dep-svc-2\","
+ "\"taskDefinition\":\"dep-td-2\",\"desiredCount\":0}");
String first = describe("dep-cluster-2", "dep-svc-2").path("services[0].deployments[0].id");
String second = describe("dep-cluster-2", "dep-svc-2").path("services[0].deployments[0].id");
assertEquals(first, second, "deployment id must not change between DescribeServices calls");
}
@Test
void convergedServiceReportsCompletedRollout() {
// desiredCount 0 is trivially converged (running 0 >= desired 0), so this asserts the
// COMPLETED branch without depending on Docker-backed tasks actually starting.
seedClusterAndTaskDef("dep-cluster-3", "dep-td-3");
call("CreateService", "{\"cluster\":\"dep-cluster-3\",\"serviceName\":\"dep-svc-3\","
+ "\"taskDefinition\":\"dep-td-3\",\"desiredCount\":0}");
describe("dep-cluster-3", "dep-svc-3").then()
.body("services[0].deployments[0].rolloutState", equalTo("COMPLETED"))
.body("services[0].deployments[0].rolloutStateReason", endsWith("completed."));
}
@Test
void deploymentIdRollsOverWhenTaskDefinitionChanges() {
seedClusterAndTaskDef("dep-cluster-5", "dep-td-5a");
call("CreateService", "{\"cluster\":\"dep-cluster-5\",\"serviceName\":\"dep-svc-5\","
+ "\"taskDefinition\":\"dep-td-5a\",\"desiredCount\":0}");
String before = describe("dep-cluster-5", "dep-svc-5").path("services[0].deployments[0].id");
registerTaskDef("dep-td-5b");
call("UpdateService", "{\"cluster\":\"dep-cluster-5\",\"service\":\"dep-svc-5\","
+ "\"taskDefinition\":\"dep-td-5b\"}");
String after = describe("dep-cluster-5", "dep-svc-5").path("services[0].deployments[0].id");
assertNotEquals(before, after,
"a new task definition is a new deployment and must get a new id");
}
@Test
void newDeploymentCarriesItsOwnStartTimeNotTheServiceCreationTime() throws Exception {
// A task-definition change mints a new deployment id, so the timestamps have to move with
// it. Reporting the service's creation time would contradict the new id and misrepresent
// when the deployment actually started.
seedClusterAndTaskDef("dep-cluster-11", "dep-td-11a");
call("CreateService", "{\"cluster\":\"dep-cluster-11\",\"serviceName\":\"dep-svc-11\","
+ "\"taskDefinition\":\"dep-td-11a\",\"desiredCount\":0}");
Response before = describe("dep-cluster-11", "dep-svc-11");
double createdAtBefore = epochSeconds(before, "services[0].deployments[0].createdAt");
String idBefore = before.path("services[0].deployments[0].id");
Thread.sleep(1100);
registerTaskDef("dep-td-11b");
call("UpdateService", "{\"cluster\":\"dep-cluster-11\",\"service\":\"dep-svc-11\","
+ "\"taskDefinition\":\"dep-td-11b\"}");
Response after = describe("dep-cluster-11", "dep-svc-11");
double createdAtAfter = epochSeconds(after, "services[0].deployments[0].createdAt");
double serviceCreatedAt = epochSeconds(after, "services[0].createdAt");
assertNotEquals(idBefore, after.path("services[0].deployments[0].id"));
assertTrue(createdAtAfter > createdAtBefore,
"a new deployment must carry a later createdAt than the one it replaced");
assertTrue(createdAtAfter > serviceCreatedAt,
"the deployment started after the service was created, so its createdAt must be later");
}
@Test
void createAndUpdateServiceEchoTheSameDeploymentShape() {
seedClusterAndTaskDef("dep-cluster-6", "dep-td-6");
call("CreateService", "{\"cluster\":\"dep-cluster-6\",\"serviceName\":\"dep-svc-6\","
+ "\"taskDefinition\":\"dep-td-6\",\"desiredCount\":0}").then()
.body("service.deployments", hasSize(1))
.body("service.deployments[0].status", equalTo("PRIMARY"));
call("UpdateService", "{\"cluster\":\"dep-cluster-6\",\"service\":\"dep-svc-6\","
+ "\"desiredCount\":0}").then()
.body("service.deployments", hasSize(1))
.body("service.deployments[0].status", equalTo("PRIMARY"));
}
@Test
void unknownServiceIsReportedAsAMissingFailureRatherThanDroppedSilently() {
// ECS answers a describe for an unknown service with a failure entry, not an error, and
// the ServicesStable waiter fails fast on failures[].reason == "MISSING". Returning an
// empty services list with no failure leaves that waiter polling for its full timeout.
seedClusterAndTaskDef("dep-cluster-8", "dep-td-8");
describe("dep-cluster-8", "no-such-service").then()
.body("services", hasSize(0))
.body("failures", hasSize(1))
.body("failures[0].reason", equalTo("MISSING"))
.body("failures[0].arn", endsWith(":service/dep-cluster-8/no-such-service"));
}
@Test
void describeMixesFoundServicesWithMissingOnes() {
seedClusterAndTaskDef("dep-cluster-9", "dep-td-9");
call("CreateService", "{\"cluster\":\"dep-cluster-9\",\"serviceName\":\"dep-svc-9\","
+ "\"taskDefinition\":\"dep-td-9\",\"desiredCount\":0}");
call("DescribeServices", "{\"cluster\":\"dep-cluster-9\","
+ "\"services\":[\"dep-svc-9\",\"ghost-service\"]}").then()
.body("services", hasSize(1))
.body("services[0].serviceName", equalTo("dep-svc-9"))
.body("failures", hasSize(1))
.body("failures[0].reason", equalTo("MISSING"))
.body("failures[0].arn", endsWith(":service/dep-cluster-9/ghost-service"));
}
@Test
void aServiceArnThatDoesNotExistIsEchoedBackInTheFailure() {
seedClusterAndTaskDef("dep-cluster-10", "dep-td-10");
String arn = "arn:aws:ecs:us-east-1:000000000000:service/dep-cluster-10/absent";
call("DescribeServices", "{\"cluster\":\"dep-cluster-10\",\"services\":[\"" + arn + "\"]}").then()
.body("failures", hasSize(1))
.body("failures[0].arn", equalTo(arn))
.body("failures[0].reason", equalTo("MISSING"));
}
@Test
void deletedServiceReportsAnEmptyDeploymentListRatherThanOmittingIt() {
// AWS's ServicesStable waiter fails an INACTIVE service on status, so an empty list is
// correct here, but the key must still be present, matching how moto renders it.
seedClusterAndTaskDef("dep-cluster-7", "dep-td-7");
call("CreateService", "{\"cluster\":\"dep-cluster-7\",\"serviceName\":\"dep-svc-7\","
+ "\"taskDefinition\":\"dep-td-7\",\"desiredCount\":0}");
call("DeleteService", "{\"cluster\":\"dep-cluster-7\",\"service\":\"dep-svc-7\"}");
describe("dep-cluster-7", "dep-svc-7").then()
.body("services[0].status", equalTo("INACTIVE"))
.body("services[0].deployments", hasSize(0));
}
}