From 5a8694e97a0b5416d29a864aef22dfef52b0265d Mon Sep 17 00:00:00 2001 From: Mario Toffia Date: Thu, 8 Oct 2026 13:41:20 +0200 Subject: [PATCH] fix(ecs): accept a service ARN in ListTasks (#4859) ListTasks looked the serviceName filter up by the string it was given, so only a bare service name matched. A service ARN, which is what CDK passes, found no service, and the call returned an empty taskArns list even while the service had running tasks. The filter now goes through extractServiceName, the helper UpdateService and DeleteService already use, which keeps the last path segment. A name, a full service// ARN, the older service/ ARN and / all resolve to the service in the request's cluster. AWS accepts each of these forms. An unknown service still lists nothing, which is unchanged. --- .../floci/services/ecs/EcsService.java | 5 +-- .../EcsFargateEdgeCaseIntegrationTest.java | 31 +++++++++++++++++++ .../ecs/EcsServiceTaskOwnershipTest.java | 23 ++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java index 0d35331313..b33f1bfbae 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java @@ -2186,9 +2186,10 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab : (serviceName != null ? clusters.get(clusterKey(region, DEFAULT_CLUSTER)) : null); String clusterArn = clusterRef != null ? cluster.getClusterArn() : null; // A serviceName filter selects the service's own tasks, so it resolves through the - // reconciler-stamped ownership rather than the caller-supplied group. + // reconciler-stamped ownership rather than the caller-supplied group. It may be the + // service's name or its ARN; as on AWS, only the last path segment names the service. EcsServiceModel svc = serviceName != null && cluster != null - ? services.get(serviceKey(region, cluster.getClusterName(), serviceName)) + ? services.get(serviceKey(region, cluster.getClusterName(), extractServiceName(serviceName))) : null; String family = request.getFamily(); LaunchType launchType = request.getLaunchType(); diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsFargateEdgeCaseIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsFargateEdgeCaseIntegrationTest.java index 6b593296a0..e176d8674d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsFargateEdgeCaseIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsFargateEdgeCaseIntegrationTest.java @@ -6,10 +6,12 @@ import io.restassured.response.Response; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.time.Duration; import java.util.List; import java.util.Map; import static io.restassured.RestAssured.given; +import static org.awaitility.Awaitility.await; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItems; @@ -524,6 +526,35 @@ class EcsFargateEdgeCaseIntegrationTest { "the last page carries no token"); } + @Test + void listTasksTakesTheServiceNameOrItsArn() { + String family = seed("edge-list-service"); + String serviceArn = call("CreateService", "{\"cluster\":\"" + CLUSTER + + "\",\"serviceName\":\"edge-list-svc\",\"taskDefinition\":\"" + family + + "\",\"desiredCount\":1,\"launchType\":\"FARGATE\"," + NETWORK + "}", 200) + .jsonPath().getString("service.serviceArn"); + String idleArn = call("CreateService", "{\"cluster\":\"" + CLUSTER + + "\",\"serviceName\":\"edge-list-idle-svc\",\"taskDefinition\":\"" + family + + "\",\"desiredCount\":0,\"launchType\":\"FARGATE\"," + NETWORK + "}", 200) + .jsonPath().getString("service.serviceArn"); + + // The reconciler launches the service's task on its own schedule. + await().atMost(Duration.ofSeconds(30)).pollInterval(Duration.ofMillis(200)) + .until(() -> listTaskArns("edge-list-svc").size() == 1); + List byName = listTaskArns("edge-list-svc"); + + assertEquals(byName, listTaskArns(serviceArn), "the service ARN must select the same task"); + assertEquals(byName, listTaskArns(serviceArn.replace("service/" + CLUSTER + "/", "service/")), + "the old service/ ARN format must select it too"); + assertEquals(List.of(), listTaskArns(idleArn), + "another service's ARN must not select this service's task"); + } + + private static List listTaskArns(String serviceName) { + return call("ListTasks", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + serviceName + + "\"}", 200).jsonPath().getList("taskArns"); + } + @Test void onlyTheDocumentedUpdatesRollTheServiceDeployment() { String family = seed("edge-update-rolls"); diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServiceTaskOwnershipTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServiceTaskOwnershipTest.java index 32a12e20c9..9c3014c41d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServiceTaskOwnershipTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServiceTaskOwnershipTest.java @@ -149,6 +149,29 @@ class EcsServiceTaskOwnershipTest { "a task that merely names the service as its group is not service-owned"); } + @Test + void listTasksByServiceArnListsTheSameTasksAsByServiceName() { + EcsService service = newMockModeService(); + service.createCluster("arn-cluster", REGION); + registerTaskDef(service); + String serviceArn = service.createService("arn-cluster", "svc", "own-fam", 1, LaunchType.FARGATE, + List.of(), null, REGION).getServiceArn(); + String idleArn = service.createService("arn-cluster", "idle", "own-fam", 0, LaunchType.FARGATE, + List.of(), null, REGION).getServiceArn(); + service.reconcileServices(); + + List byName = service.listTasks("arn-cluster", null, null, "svc", REGION); + String shortArn = serviceArn.replace("service/arn-cluster/", "service/"); + + assertEquals(1, byName.size(), "the service runs its one task"); + assertEquals(byName, service.listTasks("arn-cluster", null, null, serviceArn, REGION), + "the service ARN selects the same tasks as the service name"); + assertEquals(byName, service.listTasks("arn-cluster", null, null, shortArn, REGION), + "the old service/ ARN format selects them too"); + assertEquals(List.of(), service.listTasks("arn-cluster", null, null, idleArn, REGION), + "another service's ARN does not select this service's tasks"); + } + @Test void aSameNamedClusterInAnotherRegionIsNotSweptIntoReconciliation() { EcsService service = newMockModeService();