feat(batch): add UpdateComputeEnvironment and DeleteComputeEnvironment (#2857)

Mirrors the existing UpdateJobQueue/DeleteJobQueue pair. DeleteComputeEnvironment
requires DISABLED state and rejects deletion while still referenced by a job
queue's computeEnvironmentOrder, matching AWS; updating computeResources is a
partial merge (only the sent fields change), not a full replace. Both
UpdateComputeEnvironment and CreateComputeEnvironment now validate state is
ENABLED or DISABLED, so an invalid value can never be persisted and later
block deletion.

CloudFormation stack-delete wiring for AWS::Batch::ComputeEnvironment/
JobQueue/JobDefinition (currently a silent no-op on stack teardown) is left
for a follow-up that migrates all of Batch's CFN handling into a per-service
provisioner, rather than adding more cases to the legacy switch.

Closes #2293, closes #2803.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-01 18:06:57 -05:00
1 parent adc4b3627d
commit 3b7ba5c687
5 files changed
+292 -1

No files matched your search

@@ -498,6 +498,87 @@ class BatchIntegrationTest {
.statusCode(200);
}
@Test
void updateAndDeleteComputeEnvironmentSupportsTeardownFlow() {
String suffix = uniqueSuffix();
String ceName = "teardown-ce-" + suffix;
String ceArn = createComputeEnvironment(ceName);
String queueName = "teardown-ce-queue-" + suffix;
createQueue(queueName, ceArn);
// AWS Batch requires DISABLED before delete: rejected while ENABLED.
givenJson("{\"computeEnvironment\":\"%s\"}".formatted(ceName))
.when()
.post("/v1/deletecomputeenvironment")
.then()
.statusCode(400)
.body("__type", equalTo("ClientException"));
givenJson("""
{
"computeEnvironment": "%s",
"state": "DISABLED",
"serviceRole": "arn:aws:iam::000000000000:role/BatchServiceRole",
"computeResources": {"desiredvCpus": 0}
}
""".formatted(ceName))
.when()
.post("/v1/updatecomputeenvironment")
.then()
.statusCode(200)
.body("computeEnvironmentName", equalTo(ceName))
.body("computeEnvironmentArn", equalTo(ceArn));
givenJson("{\"computeEnvironments\":[\"%s\"]}".formatted(ceArn))
.when()
.post("/v1/describecomputeenvironments")
.then()
.statusCode(200)
.body("computeEnvironments", hasSize(1))
.body("computeEnvironments[0].state", equalTo("DISABLED"))
.body("computeEnvironments[0].serviceRole",
equalTo("arn:aws:iam::000000000000:role/BatchServiceRole"));
// DISABLED but still referenced by a job queue's computeEnvironmentOrder: still rejected.
givenJson("{\"computeEnvironment\":\"%s\"}".formatted(ceName))
.when()
.post("/v1/deletecomputeenvironment")
.then()
.statusCode(400)
.body("__type", equalTo("ClientException"));
givenJson("{\"jobQueue\":\"%s\",\"state\":\"DISABLED\"}".formatted(queueName))
.when()
.post("/v1/updatejobqueue")
.then()
.statusCode(200);
givenJson("{\"jobQueue\":\"%s\"}".formatted(queueName))
.when()
.post("/v1/deletejobqueue")
.then()
.statusCode(200);
givenJson("{\"computeEnvironment\":\"%s\"}".formatted(ceName))
.when()
.post("/v1/deletecomputeenvironment")
.then()
.statusCode(200)
.body("isEmpty()", equalTo(true));
givenJson("{\"computeEnvironments\":[\"%s\"]}".formatted(ceArn))
.when()
.post("/v1/describecomputeenvironments")
.then()
.statusCode(200)
.body("computeEnvironments", hasSize(0));
givenJson("{\"computeEnvironment\":\"%s\"}".formatted(ceName))
.when()
.post("/v1/deletecomputeenvironment")
.then()
.statusCode(200);
}
@Test
void describeUnknownJobsReturnsEmptyList() {
givenJson("{\"jobs\":[\"does-not-exist\"]}")
@@ -311,6 +311,143 @@ class BatchServiceTest {
assertEquals(0, repeated.size());
}
@Test
void updateComputeEnvironmentChangesStateServiceRoleAndComputeResources() throws Exception {
BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());
String computeArn = service.createComputeEnvironment(json("""
{
"computeEnvironmentName":"update-ce",
"type":"MANAGED",
"computeResources":{"type":"EC2","minvCpus":0,"maxvCpus":4,"instanceTypes":["optimal"]}
}
"""), REGION).path("computeEnvironmentArn").asText();
JsonNode updated = service.updateComputeEnvironment(json("""
{
"computeEnvironment":"update-ce",
"state":"DISABLED",
"serviceRole":"arn:aws:iam::000000000000:role/BatchServiceRole",
"computeResources":{"maxvCpus":8,"desiredvCpus":2}
}
"""));
assertEquals("update-ce", updated.path("computeEnvironmentName").asText());
assertEquals(computeArn, updated.path("computeEnvironmentArn").asText());
JsonNode detail = service.describeComputeEnvironments(json("""
{"computeEnvironments":["%s"]}
""".formatted(computeArn))).path("computeEnvironments").get(0);
assertEquals("DISABLED", detail.path("state").asText());
assertEquals("arn:aws:iam::000000000000:role/BatchServiceRole", detail.path("serviceRole").asText());
// Partial update: only the sent fields change, minvCpus/instanceTypes survive untouched.
assertEquals(0, detail.path("computeResources").path("minvCpus").asInt());
assertEquals(8, detail.path("computeResources").path("maxvCpus").asInt());
assertEquals(2, detail.path("computeResources").path("desiredvCpus").asInt());
assertEquals("optimal", detail.path("computeResources").path("instanceTypes").get(0).asText());
}
@Test
void updateComputeEnvironmentRejectsUnknownEnvironment() {
BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());
AwsException e = assertThrows(AwsException.class, () -> service.updateComputeEnvironment(json("""
{"computeEnvironment":"missing-ce","state":"DISABLED"}
""")));
assertEquals("ClientException", e.getErrorCode());
}
@Test
void updateComputeEnvironmentRejectsInvalidState() throws Exception {
BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());
service.createComputeEnvironment(json("""
{"computeEnvironmentName":"invalid-state-ce","type":"MANAGED"}
"""), REGION);
AwsException e = assertThrows(AwsException.class, () -> service.updateComputeEnvironment(json("""
{"computeEnvironment":"invalid-state-ce","state":"SUSPENDED"}
""")));
assertEquals("ClientException", e.getErrorCode());
// Rejected before being written: the environment is still ENABLED, not stuck holding
// an invalid value DeleteComputeEnvironment could never match against.
JsonNode detail = service.describeComputeEnvironments(json("""
{"computeEnvironments":["invalid-state-ce"]}
""")).path("computeEnvironments").get(0);
assertEquals("ENABLED", detail.path("state").asText());
}
@Test
void createComputeEnvironmentRejectsInvalidState() {
BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());
AwsException e = assertThrows(AwsException.class, () -> service.createComputeEnvironment(json("""
{"computeEnvironmentName":"bad-create-ce","type":"MANAGED","state":"SUSPENDED"}
"""), REGION));
assertEquals("ClientException", e.getErrorCode());
}
@Test
void deleteComputeEnvironmentRejectsEnabledEnvironment() throws Exception {
BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());
service.createComputeEnvironment(json("""
{"computeEnvironmentName":"delete-enabled-ce","type":"MANAGED"}
"""), REGION);
AwsException e = assertThrows(AwsException.class, () -> service.deleteComputeEnvironment(json("""
{"computeEnvironment":"delete-enabled-ce"}
""")));
assertEquals("ClientException", e.getErrorCode());
}
@Test
void deleteComputeEnvironmentRejectsEnvironmentStillAttachedToJobQueue() throws Exception {
BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());
String computeArn = service.createComputeEnvironment(json("""
{"computeEnvironmentName":"attached-ce","type":"MANAGED"}
"""), REGION).path("computeEnvironmentArn").asText();
service.createJobQueue(json("""
{
"jobQueueName":"attached-queue",
"priority":1,
"computeEnvironmentOrder":[{"order":1,"computeEnvironment":"%s"}]
}
""".formatted(computeArn)), REGION);
service.updateComputeEnvironment(json("""
{"computeEnvironment":"attached-ce","state":"DISABLED"}
"""));
AwsException e = assertThrows(AwsException.class, () -> service.deleteComputeEnvironment(json("""
{"computeEnvironment":"attached-ce"}
""")));
assertEquals("ClientException", e.getErrorCode());
}
@Test
void deleteComputeEnvironmentRemovesDisabledEnvironmentAndToleratesRepeatDeletes() throws Exception {
BatchService service = immediateService(new InMemoryStorage<String, BatchJob>());
String computeArn = service.createComputeEnvironment(json("""
{"computeEnvironmentName":"delete-ce","type":"MANAGED"}
"""), REGION).path("computeEnvironmentArn").asText();
service.updateComputeEnvironment(json("""
{"computeEnvironment":"delete-ce","state":"DISABLED"}
"""));
JsonNode deleted = service.deleteComputeEnvironment(json("""
{"computeEnvironment":"delete-ce"}
"""));
assertEquals(0, deleted.size());
JsonNode envs = service.describeComputeEnvironments(json("""
{"computeEnvironments":["%s"]}
""".formatted(computeArn))).path("computeEnvironments");
assertEquals(0, envs.size());
JsonNode repeated = service.deleteComputeEnvironment(json("""
{"computeEnvironment":"delete-ce"}
"""));
assertEquals(0, repeated.size());
}
private BatchService dockerService(BatchDockerRunner runner) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);