Light, fluffy, and always free - The AWS Local Emulator alternative
feat(batch): tag actions and one-lock teardown, wired into the CloudFormation provisioner (#4268)
Hector Venturabfedf6c679ca
diff --git a/docs/services/batch.md b/docs/services/batch.mdindex 1c98a02a60..47b5104946 100644--- a/docs/services/batch.md+++ b/docs/services/batch.md@@ -25,6 +25,9 @@ Floci Batch implements the AWS Batch control plane for local integration tests.| `TerminateJob` | `POST /v1/terminatejob` | Terminate a nonterminal job and stop its running container or nodes || `DescribeJobs` | `POST /v1/describejobs` | Describe jobs by job ID || `ListJobs` | `POST /v1/listjobs` | List jobs by queue, status, AWS `filters`, and pagination |+| `ListTagsForResource` | `GET /v1/tags/{resourceArn}` | List the tags of a compute environment, job queue, job definition, or job |+| `TagResource` | `POST /v1/tags/{resourceArn}` | Add or overwrite tags on a resource; tags the request omits are kept |+| `UntagResource` | `DELETE /v1/tags/{resourceArn}?tagKeys=...` | Remove the named tag keys from a resource |## Runner Modesdiff --git a/src/main/java/io/github/hectorvent/floci/services/batch/BatchService.java b/src/main/java/io/github/hectorvent/floci/services/batch/BatchService.javaindex aae27817b7..e5e6176b22 100644--- a/src/main/java/io/github/hectorvent/floci/services/batch/BatchService.java+++ b/src/main/java/io/github/hectorvent/floci/services/batch/BatchService.java@@ -46,6 +46,7 @@ import java.util.Objects;import java.util.Optional;import java.util.Set;import java.util.UUID;+import java.util.function.Consumer;import java.util.regex.Pattern;@ApplicationScoped@@ -305,6 +306,146 @@ public class BatchService {return objectMapper.createObjectNode();}+ /**+ * A stack delete needs look-up, disable and delete as one step. The three public calls each+ * take the lock on their own, so between them a resource removed out of band would fail the+ * disable instead of counting as already gone, and a queue attached after the disable would+ * be seen only by the delete. Held once here, the sequence sees one state throughout; the+ * refusals the delete raises (still attached to a queue) still propagate.+ *+ * @return whether the environment existed; a repeated stack delete gets {@code false}+ */+ public synchronized boolean teardownComputeEnvironment(String ref) {+ Optional<BatchComputeEnvironment> env = resolveComputeEnvironmentOptional(ref);+ if (env.isEmpty()) {+ return false;+ }+ String arn = env.get().getComputeEnvironmentArn();+ ObjectNode disable = objectMapper.createObjectNode();+ disable.put("computeEnvironment", arn);+ disable.put("state", "DISABLED");+ updateComputeEnvironment(disable);+ ObjectNode delete = objectMapper.createObjectNode();+ delete.put("computeEnvironment", arn);+ deleteComputeEnvironment(delete);+ return true;+ }++ /** The job queue twin of {@link #teardownComputeEnvironment}. */+ public synchronized boolean teardownJobQueue(String ref) {+ Optional<BatchJobQueue> queue = resolveJobQueueOptional(ref);+ if (queue.isEmpty()) {+ return false;+ }+ String arn = queue.get().getJobQueueArn();+ ObjectNode disable = objectMapper.createObjectNode();+ disable.put("jobQueue", arn);+ disable.put("state", "DISABLED");+ updateJobQueue(disable);+ ObjectNode delete = objectMapper.createObjectNode();+ delete.put("jobQueue", arn);+ deleteJobQueue(delete);+ return true;+ }++ /**+ * Look-up and deregister under one hold: deregister throws on a missing definition, so the+ * look-up is what keeps a repeated stack delete idempotent, and it has to see the same state.+ * A revision already INACTIVE counts as gone.+ */+ public synchronized boolean teardownJobDefinition(String ref) {+ Optional<BatchJobDefinition> def = resolveJobDefinitionOptional(ref, false);+ if (def.isEmpty()) {+ return false;+ }+ ObjectNode deregister = objectMapper.createObjectNode();+ deregister.put("jobDefinition", def.get().getJobDefinitionArn());+ deregisterJobDefinition(deregister);+ return true;+ }++ // ── tags ─────────────────────────────────────────────────────────────────++ public synchronized Map<String, String> listTagsForResource(String resourceArn) {+ return new LinkedHashMap<>(taggable(resourceArn).tags());+ }++ /**+ * TagResource merges: tags already on the resource that the request omits are kept, as on+ * AWS. The request map obeys the same rules as create-time tags (reserved {@code aws:} keys,+ * key and value lengths, at most 50 entries), and so does the merged result: a request that+ * fits on its own can still push a resource past 50, and nothing is stored when it does.+ */+ public synchronized void tagResource(String resourceArn, Map<String, String> tags) {+ if (tags == null || tags.isEmpty()) {+ throw client("tags must contain between 1 and 50 entries");+ }+ validateTags(tags);+ Taggable target = taggable(resourceArn);+ Map<String, String> merged = new LinkedHashMap<>(target.tags());+ merged.putAll(tags);+ validateTags(merged);+ target.store().accept(merged);+ }++ public synchronized void untagResource(String resourceArn, List<String> tagKeys) {+ if (tagKeys == null || tagKeys.isEmpty() || tagKeys.size() > MAX_TAGS) {+ throw client("tagKeys must contain between 1 and 50 entries");+ }+ Taggable target = taggable(resourceArn);+ Map<String, String> remaining = new LinkedHashMap<>(target.tags());+ tagKeys.forEach(remaining::remove);+ target.store().accept(remaining);+ }++ /** A resource's current tags and how to write them back, so the three tag actions share one look-up. */+ private record Taggable(Map<String, String> tags, Consumer<Map<String, String>> store) {+ }++ private Taggable taggable(String resourceArn) {+ String resource;+ try {+ resource = AwsArnUtils.parse(resourceArn).resource();+ } catch (IllegalArgumentException e) {+ throw client("Invalid resource ARN: " + resourceArn);+ }+ if (resource.startsWith("compute-environment/")) {+ BatchComputeEnvironment env = resolveComputeEnvironment(resourceArn);+ return new Taggable(tagsOf(env.getTags()), tags -> {+ env.setTags(tags);+ computeEnvironmentStore.put(env.getComputeEnvironmentArn(), env);+ });+ }+ if (resource.startsWith("job-queue/")) {+ BatchJobQueue queue = resolveJobQueueOptional(resourceArn)+ .orElseThrow(() -> client("Job queue not found: " + resourceArn));+ return new Taggable(tagsOf(queue.getTags()), tags -> {+ queue.setTags(tags);+ jobQueueStore.put(queue.getJobQueueArn(), queue);+ });+ }+ if (resource.startsWith("job-definition/")) {+ BatchJobDefinition def = resolveJobDefinition(resourceArn, true);+ return new Taggable(tagsOf(def.getTags()), tags -> {+ def.setTags(tags);+ putJobDefinition(def);+ });+ }+ if (resource.startsWith("job/")) {+ String jobId = resource.substring("job/".length());+ BatchJob job = jobStore.get(jobId).orElseThrow(() -> client("Job not found: " + resourceArn));+ return new Taggable(tagsOf(job.getTags()), tags -> {+ job.setTags(tags);+ jobStore.put(job.getJobId(), job);+ });+ }+ throw client("Resource not found: " + resourceArn);+ }++ private static Map<String, String> tagsOf(Map<String, String> tags) {+ return tags == null ? Map.of() : tags;+ }+public ObjectNode describeJobQueues(JsonNode request) {List<String> refs = stringList(request.path("jobQueues"));if (refs.size() > DESCRIBE_JOBS_LIMIT) {diff --git a/src/main/java/io/github/hectorvent/floci/services/batch/BatchTagHandler.java b/src/main/java/io/github/hectorvent/floci/services/batch/BatchTagHandler.javanew file mode 100644index 0000000000..ba1720de6e--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/batch/BatchTagHandler.java@@ -0,0 +1,62 @@+package io.github.hectorvent.floci.services.batch;++import io.github.hectorvent.floci.core.common.TagHandler;+import io.github.hectorvent.floci.core.common.V1Tags;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.util.List;+import java.util.Map;++/**+ * Batch's tag endpoints, which AWS puts on {@code /v1/tags/{resourceArn}}: served by the shared+ * {@code V1TagsController}, which resolves the owning service from the ARN, the same way MSK and+ * AppSync are. A separate bean for the reason {@code MskTagHandler} gives: a class-level+ * {@link V1Tags} on the service would strip its {@code @Default} qualifier.+ *+ * <p>Batch's wire shape matches the {@link TagHandler} defaults on every axis but one: a lowercase+ * {@code tags} map, POST for TagResource, {@code tagKeys} for UntagResource, and a 200 on+ * TagResource and UntagResource where MSK answers 204: the Batch model declares no+ * {@code responseCode} on either, which in the rest-json protocol is the default 200.+ */+@ApplicationScoped+@V1Tags+public class BatchTagHandler implements TagHandler {++ private final BatchService batchService;++ @Inject+ public BatchTagHandler(BatchService batchService) {+ this.batchService = batchService;+ }++ @Override+ public String serviceKey() {+ return "batch";+ }++ @Override+ public int tagResourceSuccessStatus() {+ return 200;+ }++ @Override+ public int untagResourceSuccessStatus() {+ return 200;+ }++ @Override+ public Map<String, String> listTags(String region, String arn) {+ return batchService.listTagsForResource(arn);+ }++ @Override+ public void tagResource(String region, String arn, Map<String, String> tags) {+ batchService.tagResource(arn, tags);+ }++ @Override+ public void untagResource(String region, String arn, List<String> tagKeys) {+ batchService.untagResource(arn, tagKeys);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisioner.javaindex 236d882cc5..dfdc213ac0 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisioner.java@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.cloudformation.model.StackResource;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;+import java.util.List;import java.util.Map;import java.util.Set;import java.util.function.Function;@@ -163,7 +164,7 @@ public class BatchCfnProvisioner implements CfnResourceProvisioner {// deregistering the revision this update registered and naming the prior one again.String registered = resource.getPhysicalId();if (registered != null && !registered.equals(priorId)) {- deregisterJobDefinition(registered);+ batchService.teardownJobDefinition(registered);}resource.setPhysicalId(priorId);resource.getAttributes().put("Arn", priorId);@@ -235,6 +236,10 @@ public class BatchCfnProvisioner implements CfnResourceProvisioner {* step from throwing on a resource someone removed out of band. {@code EcsCfnProvisioner} does* tolerate a blanket {@code ClientException} for its task-definition arm, so the divergence* here is deliberate rather than an oversight.+ *+ * <p>The look-up, the disable and the delete run under one hold of the service lock+ * ({@code BatchService.teardown*}), so a resource removed out of band between them counts as+ * gone rather than failing the disable, and a queue attached in between is still refused.*/@Overridepublic void delete(String resourceType, String physicalId, String region) {@@ -242,62 +247,15 @@ public class BatchCfnProvisioner implements CfnResourceProvisioner {return;}switch (resourceType) {- case "AWS::Batch::ComputeEnvironment" -> deleteComputeEnvironment(physicalId);- case "AWS::Batch::JobQueue" -> deleteJobQueue(physicalId);- case "AWS::Batch::JobDefinition" -> deregisterJobDefinition(physicalId);+ case "AWS::Batch::ComputeEnvironment" -> batchService.teardownComputeEnvironment(physicalId);+ case "AWS::Batch::JobQueue" -> batchService.teardownJobQueue(physicalId);+ case "AWS::Batch::JobDefinition" -> batchService.teardownJobDefinition(physicalId);default -> {// no other type reaches this provisioner}}}- private void deleteComputeEnvironment(String physicalId) {- if (!exists("computeEnvironments", physicalId,- req -> batchService.describeComputeEnvironments(req))) {- return;- }- ObjectNode disable = JsonNodeFactory.instance.objectNode();- disable.put("computeEnvironment", physicalId);- disable.put("state", "DISABLED");- batchService.updateComputeEnvironment(disable);-- ObjectNode req = JsonNodeFactory.instance.objectNode();- req.put("computeEnvironment", physicalId);- batchService.deleteComputeEnvironment(req);- }-- private void deleteJobQueue(String physicalId) {- if (!exists("jobQueues", physicalId, req -> batchService.describeJobQueues(req))) {- return;- }- ObjectNode disable = JsonNodeFactory.instance.objectNode();- disable.put("jobQueue", physicalId);- disable.put("state", "DISABLED");- batchService.updateJobQueue(disable);-- ObjectNode req = JsonNodeFactory.instance.objectNode();- req.put("jobQueue", physicalId);- batchService.deleteJobQueue(req);- }-- private void deregisterJobDefinition(String physicalId) {- // Unlike the other two, deregister throws when the definition is gone, so the existence- // check is what makes a repeated stack delete idempotent.- if (!exists("jobDefinitions", physicalId, req -> batchService.describeJobDefinitions(req))) {- return;- }- ObjectNode req = JsonNodeFactory.instance.objectNode();- req.put("jobDefinition", physicalId);- batchService.deregisterJobDefinition(req);- }-- private boolean exists(String requestKey, String physicalId,- Function<ObjectNode, ObjectNode> describe) {- ObjectNode req = JsonNodeFactory.instance.objectNode();- req.putArray(requestKey).add(physicalId);- return !describe.apply(req).path(requestKey).isEmpty();- }-private void provisionComputeEnvironment(StackResource r, JsonNode props, ProvisionContext ctx,Map<String, String> attributesBefore) {String name = stableName(r, ctx, props, "ComputeEnvironmentName");@@ -359,6 +317,11 @@ public class BatchCfnProvisioner implements CfnResourceProvisioner {update.set("computeEnvironmentOrder", computeEnvironmentOrder(props, ctx));batchService.updateJobQueue(update);arn = ctx.priorPhysicalId();+ // Tags are tag-updatable on a job queue (unlike a compute environment, where they are+ // createOnly), so a declared set is driven to the template's through the tag actions.+ if (props != null && props.has("Tags")) {+ reconcileTags(arn, ctx.resolveTags(props, "Tags"));+ }} else {ObjectNode req = JsonNodeFactory.instance.objectNode();req.put("jobQueueName", name);@@ -583,6 +546,21 @@ public class BatchCfnProvisioner implements CfnResourceProvisioner {return retry;}+ /** Drives a resource's tags to the template's: a dropped key is untagged, an unchanged set is left alone. */+ private void reconcileTags(String arn, Map<String, String> desired) {+ Map<String, String> current = batchService.listTagsForResource(arn);+ if (desired.equals(current)) {+ return;+ }+ List<String> stale = ProvisionContext.staleTagKeys(current, desired);+ if (!stale.isEmpty()) {+ batchService.untagResource(arn, stale);+ }+ if (!desired.isEmpty()) {+ batchService.tagResource(arn, desired);+ }+ }+private void putTags(ObjectNode req, JsonNode props, ProvisionContext ctx) {Map<String, String> tags = ctx.resolveTags(props, "Tags");if (!tags.isEmpty()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.javaindex e9047eb40e..411ec92e7a 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java@@ -59,11 +59,12 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio}/**- * Resolves a CloudFormation {@code [{Key, Value}]} tag list to a map, preserving template- * order. The whole node is resolved first so an {@code Fn::If} wrapping the list works, not- * just intrinsics inside each entry. Entries whose key resolves blank are skipped and a- * missing value becomes {@code ""}; an absent or non-array property yields an empty map, never- * null.+ * Resolves a CloudFormation tag property to a map, preserving template order. Both registry+ * shapes are read: the {@code [{Key, Value}]} list most types declare, and the+ * {@code {key: value}} object types such as {@code AWS::Batch::*} declare. The whole node is+ * resolved first so an {@code Fn::If} wrapping it works, not just intrinsics inside each entry.+ * Entries whose key resolves blank are skipped and a missing value becomes {@code ""}; an+ * absent property, or one of any other shape, yields an empty map, never null.** <p>Deliberately does not validate. Callers needing AWS's tag rules (the 50-tag cap, the* reserved {@code aws:} prefix) keep their own validating parse, and callers using null to mean@@ -75,7 +76,17 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regioreturn tags;}JsonNode resolved = engine.resolveNode(props.get(name));- if (resolved == null || !resolved.isArray()) {+ if (resolved == null) {+ return tags;+ }+ if (resolved.isObject()) {+ resolved.fields().forEachRemaining(entry -> {+ String value = engine.resolve(entry.getValue());+ tags.put(entry.getKey(), value == null ? "" : value);+ });+ return tags;+ }+ if (!resolved.isArray()) {return tags;}for (JsonNode tag : resolved) {diff --git a/src/test/java/io/github/hectorvent/floci/services/batch/BatchIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/batch/BatchIntegrationTest.javaindex fc4d33fd24..65389a0b43 100644--- a/src/test/java/io/github/hectorvent/floci/services/batch/BatchIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/batch/BatchIntegrationTest.java@@ -755,6 +755,58 @@ class BatchIntegrationTest {.body("jobs", hasSize(0));}+ @Test+ void tagRoutesTagListAndUntagAResource() {+ String suffix = uniqueSuffix();+ String computeArn = createComputeEnvironment("batch-tags-ce-" + suffix);++ givenJson("{\"tags\":{\"team\":\"a\",\"tier\":\"gold\"}}")+ .when()+ .post("/v1/tags/" + computeArn)+ .then()+ .statusCode(200);+ givenJson("{\"tags\":{\"env\":\"blue\"}}")+ .when()+ .post("/v1/tags/" + computeArn)+ .then()+ .statusCode(200);++ given()+ .header("Authorization", AUTH)+ .when()+ .get("/v1/tags/" + computeArn)+ .then()+ .statusCode(200)+ .body("tags.team", equalTo("a"))+ .body("tags.tier", equalTo("gold"))+ .body("tags.env", equalTo("blue"));++ given()+ .header("Authorization", AUTH)+ .queryParam("tagKeys", "tier", "env")+ .when()+ .delete("/v1/tags/" + computeArn)+ .then()+ .statusCode(200);++ given()+ .header("Authorization", AUTH)+ .when()+ .get("/v1/tags/" + computeArn)+ .then()+ .statusCode(200)+ .body("tags.team", equalTo("a"))+ .body("tags.size()", equalTo(1));++ given()+ .header("Authorization", AUTH)+ .when()+ .get("/v1/tags/arn:aws:batch:us-east-1:000000000000:job-queue/never-" + suffix)+ .then()+ .statusCode(400)+ .header("X-Amzn-Errortype", "ClientException");+ }+private static io.restassured.specification.RequestSpecification givenJson(String body) {return given().header("Authorization", AUTH)diff --git a/src/test/java/io/github/hectorvent/floci/services/batch/BatchServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/batch/BatchServiceTest.javaindex 5767245170..150ed44302 100644--- a/src/test/java/io/github/hectorvent/floci/services/batch/BatchServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/batch/BatchServiceTest.java@@ -21,14 +21,17 @@ import java.util.ArrayList;import java.util.Collections;import java.util.LinkedHashMap;import java.util.List;+import java.util.Map;import java.util.Optional;import java.util.Set;import java.util.UUID;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;import java.util.function.Predicate;+import java.util.stream.Collectors;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNotNull;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -1058,6 +1061,145 @@ class BatchServiceTest {""".formatted(suffix, computeArn)), REGION).path("jobQueueArn").asText();}+ // ── teardown (one hold of the lock: look-up, disable, delete) ────────────++ @Test+ void teardownDisablesAndDeletesAnEnabledComputeEnvironmentAndIsIdempotent() throws Exception {+ BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());+ String arn = service.createComputeEnvironment(json("""+ {"computeEnvironmentName":"td-ce","type":"MANAGED"}+ """), REGION).path("computeEnvironmentArn").asText();+ assertEquals("ENABLED", service.describeComputeEnvironments(json(+ "{\"computeEnvironments\":[\"td-ce\"]}")).path("computeEnvironments").get(0).path("state").asText());++ assertTrue(service.teardownComputeEnvironment(arn), "an ENABLED environment is disabled then deleted");+ assertTrue(service.describeComputeEnvironments(json("{\"computeEnvironments\":[\"td-ce\"]}"))+ .path("computeEnvironments").isEmpty());+ assertFalse(service.teardownComputeEnvironment(arn), "a repeat counts the environment as gone");+ }++ @Test+ void teardownRefusesAComputeEnvironmentStillAttachedToAQueueUntilTheQueueIsGone() throws Exception {+ BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());+ String computeArn = service.createComputeEnvironment(json("""+ {"computeEnvironmentName":"td-attached-ce","type":"MANAGED"}+ """), REGION).path("computeEnvironmentArn").asText();+ String queueArn = service.createJobQueue(json("""+ {"jobQueueName":"td-queue","priority":1,+ "computeEnvironmentOrder":[{"order":1,"computeEnvironment":"%s"}]}+ """.formatted(computeArn)), REGION).path("jobQueueArn").asText();++ AwsException refused = assertThrows(AwsException.class,+ () -> service.teardownComputeEnvironment(computeArn));+ assertTrue(refused.getMessage().contains("still associated with a job queue"), refused.getMessage());+ // The refused teardown left the environment behind, disabled, exactly as AWS would.+ assertEquals("DISABLED", service.describeComputeEnvironments(json(+ "{\"computeEnvironments\":[\"td-attached-ce\"]}")).path("computeEnvironments").get(0).path("state").asText());++ assertTrue(service.teardownJobQueue(queueArn), "an ENABLED queue is disabled then deleted");+ assertFalse(service.teardownJobQueue(queueArn));+ assertTrue(service.teardownComputeEnvironment(computeArn));+ }++ @Test+ void teardownDeregistersAnActiveJobDefinitionOnce() throws Exception {+ BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());+ String arn = service.registerJobDefinition(json("""+ {"jobDefinitionName":"td-def","type":"container",+ "containerProperties":{"image":"public.ecr.aws/example/job:latest"}}+ """), REGION).path("jobDefinitionArn").asText();++ assertTrue(service.teardownJobDefinition(arn));+ assertEquals("INACTIVE", service.describeJobDefinitions(json("{\"jobDefinitions\":[\"" + arn + "\"]}"))+ .path("jobDefinitions").get(0).path("status").asText());+ assertFalse(service.teardownJobDefinition(arn), "an INACTIVE revision counts as gone");+ assertFalse(service.teardownJobDefinition("arn:aws:batch:us-east-1:000000000000:job-definition/never:1"));+ }++ // ── tags ─────────────────────────────────────────────────────────────────++ /** TagResource obeys the create-time tag rules, on the request and on the merged result. */+ @Test+ void tagResourceEnforcesTheCreateTimeTagRulesOnTheRequestAndTheMergedResult() throws Exception {+ BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());+ String queueArn = createTaggedQueue(service, "rules-queue", Map.of("team", "a"));++ assertEquals("ClientException", assertThrows(AwsException.class,+ () -> service.tagResource(queueArn, Map.of("aws:cloudformation:stack-name", "x"))).getErrorCode());+ assertThrows(AwsException.class, () -> service.tagResource(queueArn, Map.of("k".repeat(129), "v")));+ assertThrows(AwsException.class, () -> service.tagResource(queueArn, Map.of("k", "v".repeat(257))));+ assertThrows(AwsException.class, () -> service.tagResource(queueArn, tagsNumbered(0, 51)));++ service.tagResource(queueArn, tagsNumbered(0, 39));+ assertEquals(40, service.listTagsForResource(queueArn).size());+ assertThrows(AwsException.class, () -> service.tagResource(queueArn, tagsNumbered(39, 59)),+ "a request that fits on its own must not push the resource past 50 tags");+ assertEquals(40, service.listTagsForResource(queueArn).size(), "a rejected request stores nothing");+ service.tagResource(queueArn, tagsNumbered(39, 49));+ assertEquals(50, service.listTagsForResource(queueArn).size());++ assertThrows(AwsException.class, () -> service.untagResource(queueArn,+ tagsNumbered(0, 51).keySet().stream().toList()));+ service.untagResource(queueArn, tagsNumbered(0, 49).keySet().stream().toList());+ assertEquals(Map.of("team", "a"), service.listTagsForResource(queueArn));+ }++ private static Map<String, String> tagsNumbered(int from, int toExclusive) {+ Map<String, String> tags = new LinkedHashMap<>();+ for (int i = from; i < toExclusive; i++) {+ tags.put("k" + i, "v");+ }+ return tags;+ }++ private String createTaggedQueue(BatchService service, String name, Map<String, String> tags) throws Exception {+ String computeArn = service.createComputeEnvironment(json("""+ {"computeEnvironmentName":"%s-ce","type":"MANAGED"}+ """.formatted(name)), REGION).path("computeEnvironmentArn").asText();+ return service.createJobQueue(json("""+ {"jobQueueName":"%s","priority":1,"tags":%s,+ "computeEnvironmentOrder":[{"order":1,"computeEnvironment":"%s"}]}+ """.formatted(name, new ObjectMapper().writeValueAsString(tags), computeArn)), REGION)+ .path("jobQueueArn").asText();+ }++ @Test+ void tagResourceMergesAndUntagResourceRemovesOnEveryTaggableType() throws Exception {+ BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());+ String computeArn = service.createComputeEnvironment(json("""+ {"computeEnvironmentName":"tag-ce","type":"MANAGED","tags":{"team":"a"}}+ """), REGION).path("computeEnvironmentArn").asText();+ String queueArn = service.createJobQueue(json("""+ {"jobQueueName":"tag-queue","priority":1,+ "computeEnvironmentOrder":[{"order":1,"computeEnvironment":"%s"}]}+ """.formatted(computeArn)), REGION).path("jobQueueArn").asText();+ String definitionArn = service.registerJobDefinition(json("""+ {"jobDefinitionName":"tag-def","type":"container",+ "containerProperties":{"image":"public.ecr.aws/example/job:latest"}}+ """), REGION).path("jobDefinitionArn").asText();++ for (String arn : List.of(computeArn, queueArn, definitionArn)) {+ service.tagResource(arn, Map.of("tier", "gold"));+ service.tagResource(arn, Map.of("env", "blue"));+ Map<String, String> tags = service.listTagsForResource(arn);+ assertEquals("gold", tags.get("tier"), arn);+ assertEquals("blue", tags.get("env"), "an earlier tag survives a later TagResource: " + arn);+ service.untagResource(arn, List.of("tier", "never-set"));+ assertEquals(Map.of("env", "blue", "team", "a").entrySet().stream()+ .filter(e -> arn.equals(computeArn) || !"team".equals(e.getKey()))+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),+ service.listTagsForResource(arn), arn);+ }+ // The create-time tags of the compute environment were kept through the merges.+ assertEquals("a", service.listTagsForResource(computeArn).get("team"));++ AwsException unknown = assertThrows(AwsException.class, () -> service.listTagsForResource(+ "arn:aws:batch:us-east-1:000000000000:job-queue/never"));+ assertEquals("ClientException", unknown.getErrorCode());+ assertThrows(AwsException.class, () -> service.tagResource(queueArn, Map.of()));+ assertThrows(AwsException.class, () -> service.untagResource(queueArn, List.of()));+ }+private BatchService dockerService(BatchDockerRunner runner) {return service("docker", new InMemoryStorage<String, BatchJob>(), runner);}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationBatchIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationBatchIntegrationTest.javaindex 9a02d006e7..fd9d2c30d7 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationBatchIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationBatchIntegrationTest.java@@ -218,6 +218,7 @@ class CloudFormationBatchIntegrationTest {"Properties": {"JobQueueName": "%s","Priority": %d,+ "Tags": {"env": "%s"},"ComputeEnvironmentOrder": [{"Order": 1,"ComputeEnvironment": {"Ref": "Compute"}@@ -236,7 +237,7 @@ class CloudFormationBatchIntegrationTest {.contentType("application/x-www-form-urlencoded").formParam("Action", "CreateStack").formParam("StackName", stackName)- .formParam("TemplateBody", template.formatted(computeName, 4, queueName, 1))+ .formParam("TemplateBody", template.formatted(computeName, 4, queueName, 1, "green")).when().post("/").then()@@ -248,7 +249,7 @@ class CloudFormationBatchIntegrationTest {.contentType("application/x-www-form-urlencoded").formParam("Action", "UpdateStack").formParam("StackName", stackName)- .formParam("TemplateBody", template.formatted(computeName, 8, queueName, 5))+ .formParam("TemplateBody", template.formatted(computeName, 8, queueName, 5, "blue")).when().post("/").then()@@ -272,7 +273,12 @@ class CloudFormationBatchIntegrationTest {.then().statusCode(200).body("jobQueues", hasSize(1))- .body("jobQueues[0].priority", equalTo(5));+ .body("jobQueues[0].priority", equalTo(5))+ // Tags are tag-updatable on a queue: the kept queue carries the template's new value.+ .body("jobQueues[0].tags.env", equalTo("blue"))+ // Both entities are still ENABLED (AWS's default), which is what the delete below+ // has to disable first: a delete that only works on DISABLED ones proves nothing.+ .body("jobQueues[0].state", equalTo("ENABLED"));given().contentType("application/x-www-form-urlencoded")diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisionerTest.javaindex c33627c31a..ab6705cd40 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisionerTest.java@@ -12,6 +12,8 @@ import org.mockito.ArgumentCaptor;import org.mockito.InOrder;import java.util.HashMap;+import java.util.Map;+import java.util.List;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;@@ -168,6 +170,39 @@ class BatchCfnProvisionerTest {assertEquals(CE_ARN, r.getPhysicalId(), "an in-place update keeps the physical id");}+ @Test+ void updatingAJobQueueReconcilesItsDeclaredTags() {+ // JobQueue tags are tag-updatable, so a kept queue is driven to the template's set: the+ // dropped key is untagged, the rest tagged, through the same tag actions the API serves.+ when(batch.listTagsForResource(JQ_ARN)).thenReturn(Map.of("team", "a", "stale", "x"));+ StackResource r = resource("AWS::Batch::JobQueue", "Queue");+ r.getAttributes().put("JobQueueName", "queue");+ ObjectNode props = mapper.createObjectNode();+ props.put("JobQueueName", "queue");+ props.put("Priority", "9");+ props.putObject("Tags").put("team", "a").put("tier", "gold");++ provisioner.provision(r, props, updateCtx(JQ_ARN));++ verify(batch).untagResource(JQ_ARN, List.of("stale"));+ verify(batch).tagResource(JQ_ARN, Map.of("team", "a", "tier", "gold"));+ }++ @Test+ void updatingAJobQueueWithoutDeclaredTagsLeavesItsTagsAlone() {+ StackResource r = resource("AWS::Batch::JobQueue", "Queue");+ r.getAttributes().put("JobQueueName", "queue");+ ObjectNode props = mapper.createObjectNode();+ props.put("JobQueueName", "queue");+ props.put("Priority", "9");++ provisioner.provision(r, props, updateCtx(JQ_ARN));++ verify(batch, never()).listTagsForResource(anyString());+ verify(batch, never()).untagResource(anyString(), any());+ verify(batch, never()).tagResource(anyString(), any());+ }+@Testvoid updatingAJobQueueUpdatesInPlace() {StackResource r = resource("AWS::Batch::JobQueue", "Queue");@@ -308,58 +343,18 @@ class BatchCfnProvisionerTest {// ── delete ───────────────────────────────────────────────────────────────@Test- void deletingAJobQueueDisablesItFirst() {- // DeleteJobQueue refuses an ENABLED queue, as on AWS, so the disable has to come first- // and in this order.- when(batch.describeJobQueues(any())).thenReturn(describeWith("jobQueues", JQ_ARN));-- provisioner.delete("AWS::Batch::JobQueue", JQ_ARN, "us-east-1");-- InOrder order = inOrder(batch);- ArgumentCaptor<JsonNode> disable = ArgumentCaptor.forClass(JsonNode.class);- order.verify(batch).updateJobQueue(disable.capture());- order.verify(batch).deleteJobQueue(any());- assertEquals("DISABLED", disable.getValue().path("state").asText());- assertEquals(JQ_ARN, disable.getValue().path("jobQueue").asText());- }-- @Test- void deletingAComputeEnvironmentDisablesItFirst() {- when(batch.describeComputeEnvironments(any()))- .thenReturn(describeWith("computeEnvironments", CE_ARN));-- provisioner.delete("AWS::Batch::ComputeEnvironment", CE_ARN, "us-east-1");-- InOrder order = inOrder(batch);- ArgumentCaptor<JsonNode> disable = ArgumentCaptor.forClass(JsonNode.class);- order.verify(batch).updateComputeEnvironment(disable.capture());- order.verify(batch).deleteComputeEnvironment(any());- assertEquals("DISABLED", disable.getValue().path("state").asText());- }-- @Test- void deletingAJobDefinitionDeregistersIt() {- when(batch.describeJobDefinitions(any())).thenReturn(describeWith("jobDefinitions", JD_ARN));-- provisioner.delete("AWS::Batch::JobDefinition", JD_ARN, "us-east-1");-- ArgumentCaptor<JsonNode> req = ArgumentCaptor.forClass(JsonNode.class);- verify(batch).deregisterJobDefinition(req.capture());- assertEquals(JD_ARN, req.getValue().path("jobDefinition").asText());- }-- @Test- void deletingAnEntityThatIsAlreadyGoneDoesNothing() {- // A repeated stack delete must be idempotent. It matters most for the job definition:- // deregister throws when the definition is missing, so the existence check is the guard.- when(batch.describeComputeEnvironments(any())).thenReturn(describeEmpty("computeEnvironments"));- when(batch.describeJobQueues(any())).thenReturn(describeEmpty("jobQueues"));- when(batch.describeJobDefinitions(any())).thenReturn(describeEmpty("jobDefinitions"));-+ void deletingTearsEachTypeDownThroughTheServiceInOneStep() {+ // The disable-before-delete order and the already-gone guard live in BatchService's+ // teardown methods, under one hold of its lock (BatchServiceTest pins them); the+ // provisioner must not re-implement them as separately locked calls.provisioner.delete("AWS::Batch::ComputeEnvironment", CE_ARN, "us-east-1");provisioner.delete("AWS::Batch::JobQueue", JQ_ARN, "us-east-1");provisioner.delete("AWS::Batch::JobDefinition", JD_ARN, "us-east-1");+ verify(batch).teardownComputeEnvironment(CE_ARN);+ verify(batch).teardownJobQueue(JQ_ARN);+ verify(batch).teardownJobDefinition(JD_ARN);+ verify(batch, never()).describeComputeEnvironments(any());verify(batch, never()).updateComputeEnvironment(any());verify(batch, never()).deleteComputeEnvironment(any());verify(batch, never()).updateJobQueue(any());@@ -371,9 +366,7 @@ class BatchCfnProvisionerTest {void aRefusedDeletePropagatesInsteadOfBeingSwallowed() {// The failure that must not be tolerated: a compute environment still attached to a queue.// Swallowing it would report a green stack delete over a resource that is still there.- when(batch.describeComputeEnvironments(any()))- .thenReturn(describeWith("computeEnvironments", CE_ARN));- when(batch.deleteComputeEnvironment(any())).thenThrow(new AwsException("ClientException",+ when(batch.teardownComputeEnvironment(CE_ARN)).thenThrow(new AwsException("ClientException","Cannot delete compute environment still associated with a job queue: envy", 400));AwsException thrown = assertThrows(AwsException.class,@@ -386,8 +379,7 @@ class BatchCfnProvisionerTest {provisioner.delete("AWS::Batch::JobQueue", null, "us-east-1");provisioner.delete("AWS::Batch::JobQueue", "", "us-east-1");- verify(batch, never()).describeJobQueues(any());- verify(batch, never()).deleteJobQueue(any());+ verify(batch, never()).teardownJobQueue(any());}// ── replacement cleanup ──────────────────────────────────────────────────@@ -441,9 +433,7 @@ class BatchCfnProvisionerTest {assertTrue(result.applicable());assertTrue(result.complete(), "the displaced environment was deleted");- ArgumentCaptor<JsonNode> deleted = ArgumentCaptor.forClass(JsonNode.class);- verify(batch).deleteComputeEnvironment(deleted.capture());- assertEquals(CE_ARN, deleted.getValue().path("computeEnvironment").asText());+ verify(batch).teardownComputeEnvironment(CE_ARN);}@Test@@ -599,10 +589,8 @@ class BatchCfnProvisionerTest {assertTrue(provisioner.rollbackUpdate(r));- ArgumentCaptor<JsonNode> deregistered = ArgumentCaptor.forClass(JsonNode.class);- verify(batch).deregisterJobDefinition(deregistered.capture());- assertEquals(JD_ARN + "-r2", deregistered.getValue().path("jobDefinition").asText(),- "the revision the failed update registered, not the one it started from");+ verify(batch).teardownJobDefinition(JD_ARN + "-r2");+ verify(batch, never()).teardownJobDefinition(JD_ARN);assertEquals(JD_ARN, r.getPhysicalId(), "the resource names the prior revision again");}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.javaindex 7217aca43c..79fcd341f1 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java@@ -128,6 +128,21 @@ class ProvisionContextTest {assertEquals("", tags.get("novalue"), "a missing Value becomes empty, not null");}+ @Test+ void resolveTagsReadsTheMapShapeSomeTypesDeclare() {+ // AWS::Batch::* declare Tags as {key: value} rather than [{Key, Value}]; the map form+ // resolves each value and keeps template order too.+ Map<String, String> tags = context(null).resolveTags(+ props("""+ {"Tags": {"zeta": "z", "alpha": "a", "empty": ""}}+ """),+ "Tags");++ assertEquals(List.of("zeta", "alpha", "empty"), List.copyOf(tags.keySet()));+ assertEquals("z", tags.get("zeta"));+ assertEquals("", tags.get("empty"));+ }+@Testvoid resolveTagsSkipsBlankKeysAndNeverReturnsNull() {Map<String, String> blankKeys = context(null).resolveTags(