mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
refactor(cloudformation): move AWS::ECS::Cluster, TaskDefinition and Service into EcsCfnProvisioner (#3114)
* refactor(cloudformation): move AWS::ECS::Cluster, TaskDefinition and Service into EcsCfnProvisioner Three more arms leave the legacy switch, together with the ECS-only parsers (container definitions, port mappings, environment, secrets, load balancers, network configuration) and the three idempotent delete helpers. The monolith no longer depends on EcsService at all; its constructor loses that parameter and CfnProvisionerFixture passes one argument less. Behaviour is unchanged except where the move fixes it: an unnamed cluster keeps the name its first execution generated (ctx.stablePhysicalName), where the switch generated a fresh name and created a second cluster on every UpdateStack. Create-vs-update for a service branches on ctx.isUpdate() and the stored Name attribute, since the physical id is the ARN; a renamed service is created as a replacement, with the prior one left behind as every replacement in the switch does today. A non-integer DesiredCount is a ValidationError instead of an uncaught NumberFormatException. Deletes go through CfnDeletes.safeDelete tolerating only the service's own not-found codes, so a ClusterContainsTasksException still fails the stack delete (#1634). Fn::GetAtt keys are unchanged (Cluster Arn, TaskDefinition TaskDefinitionArn, Service Name and ServiceArn), matching the registry schemas' readOnly properties, and CloudFormationIntegrationTest keeps asserting them end to end. EcsCfnProvisionerTest covers the three types against a mocked EcsService. The whole CloudFormation test package passes on this tree apart from the pre-existing ACM schema-gap row. * refactor(cloudformation): treat an ECS service's cluster as create-only and clean up replacements Review found that a service moved to another cluster while keeping its name took the in-place update path: updateService was called against the new cluster, which either fails with ServiceNotFoundException or mutates a same-named service the new cluster already has. Cluster is create-only on AWS::ECS::Service, so the update now compares the requested cluster (a name, an ARN's last segment, or the default) with the one embedded in the prior service ARN and creates a replacement when they differ. A replacement, here and for a renamed service or a task definition's new revision, now enters the replacement-cleanup lifecycle the engine already runs (UPDATE_COMPLETE_CLEANUP_IN_PROGRESS, DELETE_IN_PROGRESS on the prior id, three attempts, UpdateReplacePolicy Retain honoured) instead of leaving the displaced entity behind. ReplacementCleanup is the plain bookkeeping for provisioners without a rollback snapshot, beside the richer record PipesCfnProvisioner keeps; the provisioner delegates the four interface hooks to it and deletes through its own idempotent delete arm. Deregistering the displaced task-definition revision after commit is what CloudFormation does on AWS and stops revisions accumulating on every update. * refactor(cloudformation): roll an ECS replacement back when a later resource fails Review found that a replacement this provisioner produces (a service moved or renamed, a task definition's new revision) could not be rolled back: when a later resource failed the stack update, rollbackUpdate had no implementation, so the engine reported "Rollback is not implemented", left the replacement running and the stack in UPDATE_ROLLBACK_FAILED. ReplacementCleanup now keeps the attributes the resource carried before the provision alongside the prior physical id, and rollback() puts both back and deletes the replacement through the provisioner's own delete arm; the record is spent before the delete so a failing delete cannot put the restored prior on the next cleanup's list, as PipesCfnProvisioner reasons for its rename. The provisioner records once, in provision(), after the type switch, and delegates rollbackUpdate. In-place updates owe no record and keep the engine's existing handling. The committed-update cleanup in CloudFormationService now walks resources in reverse creation order, as every other teardown walk does: a displaced service has to go before the displaced cluster it runs in, and a displaced listener before the target group it forwards to, or that delete fails three times and leaves the entity behind. * refactor(cloudformation): order replacement cleanup by the template and keep an orphaned replacement owed Two gaps in the replacement lifecycle, both found in review. The committed cleanup walked the resource map reversed, but the map is insertion order and a resource a later update added sits after the resources that depend on it, so a cluster added after its service would still be deleted first. The cleanup now walks resourcesInCreationOrder reversed, the template's dependency order, exactly as DeleteStack does; a resource the template no longer names sorts last. And a rollback whose delete of the replacement failed forgot the replacement: the record was cleared before the delete, so nothing could remove that entity later even though UPDATE_ROLLBACK_FAILED is updatable here and both the next committed update and DeleteStack run the cleanup first. The record is now a list of every entity owed a delete: a replacement whose rollback delete fails is listed in the prior's place, never retained (a failed update created it), and the prior's own id and attributes are kept beside the list only while this update's replacement can still be rolled back. Every listed entity gets its attempt on every cleanup call, so one that keeps failing does not leave the others untried. * refactor(cloudformation): keep a displaced entity owed until its own attempts are used up Review found that once any listed entity reached three failed deletes, the engine's give-up cleared the whole record, forgetting entities that still had attempts left. clearUpdate now drops only the entries that used up their attempts (and the rollback fields), and the cleanup result reports the lowest attempt count among what remains, naming that entity, so the engine keeps retrying until every remaining entity has had its own three attempts.
This commit is contained in:
1 parent
fa265a4a33
commit
9df197f3af
8 files changed
+1305
-309
No files matched your search
+2
-1
@@ -36,6 +36,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2VpcEnd
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2VpcGatewayAttachmentCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.EcrCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.EcsCapacityCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.EcsCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.FirehoseCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.IamRoleCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisioner;
|
||||
@@ -235,6 +236,7 @@ final class CfnProvisionerFixture {
|
||||
}
|
||||
if (ecsService != null) {
|
||||
discovered.add(new EcsCapacityCfnProvisioner(ecsService));
|
||||
discovered.add(new EcsCfnProvisioner(ecsService));
|
||||
}
|
||||
if (apiGatewayService != null) {
|
||||
discovered.add(new ApiGatewayAccountCfnProvisioner(apiGatewayService));
|
||||
@@ -560,7 +562,6 @@ final class CfnProvisionerFixture {
|
||||
objectMapper,
|
||||
customResourceResponseStore,
|
||||
reachableEndpoint,
|
||||
ecsService,
|
||||
elbV2Service,
|
||||
stepFunctionsService,
|
||||
batchService,
|
||||
|
||||
+591
@@ -0,0 +1,591 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsService;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsCluster;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsServiceModel;
|
||||
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 org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
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.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The ECS core-type provisioner in isolation: one mocked service, no Quarkus boot. The
|
||||
* integration test in {@code CloudFormationIntegrationTest} covers the same three types end to
|
||||
* end, including the exact {@code Fn::GetAtt} keys.
|
||||
*/
|
||||
class EcsCfnProvisionerTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String CLUSTER_ARN = "arn:aws:ecs:us-east-1:000000000000:cluster/web";
|
||||
private static final String TASK_DEF_ARN = "arn:aws:ecs:us-east-1:000000000000:task-definition/web:3";
|
||||
private static final String SERVICE_ARN = "arn:aws:ecs:us-east-1:000000000000:service/web/front";
|
||||
|
||||
private final EcsService ecs = mock(EcsService.class);
|
||||
private final EcsCfnProvisioner provisioner = new EcsCfnProvisioner(ecs);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private ProvisionContext ctx() {
|
||||
return ctx(null);
|
||||
}
|
||||
|
||||
private ProvisionContext ctx(String priorPhysicalId) {
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
// resolveStringList delegates to the real engine method; for the literal arrays these
|
||||
// tests use it just walks the array and calls resolve(...) per element, stubbed above.
|
||||
when(engine.resolveStringList(any())).thenCallRealMethod();
|
||||
return new ProvisionContext(engine, REGION, "000000000000", "my-stack", priorPhysicalId);
|
||||
}
|
||||
|
||||
private StackResource resource(String type, String logicalId) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId(logicalId);
|
||||
r.setResourceType(type);
|
||||
r.setAttributes(new HashMap<>());
|
||||
return r;
|
||||
}
|
||||
|
||||
private EcsCluster cluster(String name) {
|
||||
EcsCluster c = new EcsCluster();
|
||||
c.setClusterName(name);
|
||||
c.setClusterArn("arn:aws:ecs:us-east-1:000000000000:cluster/" + name);
|
||||
return c;
|
||||
}
|
||||
|
||||
private EcsServiceModel service(String name) {
|
||||
EcsServiceModel s = new EcsServiceModel();
|
||||
s.setServiceName(name);
|
||||
s.setServiceArn("arn:aws:ecs:us-east-1:000000000000:service/web/" + name);
|
||||
return s;
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaresTheThreeCoreTypes() {
|
||||
assertEquals(Set.of("AWS::ECS::Cluster", "AWS::ECS::TaskDefinition", "AWS::ECS::Service"),
|
||||
provisioner.resourceTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterUsesTheDeclaredNameAndExposesArn() {
|
||||
when(ecs.createCluster("web", REGION)).thenReturn(cluster("web"));
|
||||
StackResource r = resource("AWS::ECS::Cluster", "Cluster");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode().put("ClusterName", "web"), ctx());
|
||||
|
||||
assertEquals("web", r.getPhysicalId());
|
||||
assertEquals(CLUSTER_ARN, r.getAttributes().get("Arn"));
|
||||
assertEquals(Set.of("Arn"), r.getAttributes().keySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnnamedClusterGeneratesANameOnCreateAndKeepsItOnUpdate() {
|
||||
when(ecs.createCluster(anyString(), eq(REGION))).thenAnswer(inv -> cluster(inv.getArgument(0)));
|
||||
StackResource created = resource("AWS::ECS::Cluster", "Cluster");
|
||||
provisioner.provision(created, mapper.createObjectNode(), ctx());
|
||||
String generated = created.getPhysicalId();
|
||||
assertTrue(generated.startsWith("my-stack-Cluster-"), generated);
|
||||
|
||||
StackResource updated = resource("AWS::ECS::Cluster", "Cluster");
|
||||
provisioner.provision(updated, mapper.createObjectNode(), ctx(generated));
|
||||
|
||||
// createCluster is idempotent, so the update re-issues it under the same name instead of
|
||||
// minting a second cluster.
|
||||
assertEquals(generated, updated.getPhysicalId());
|
||||
verify(ecs, times(2)).createCluster(generated, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskDefinitionRegistersTheParsedContainersAndExposesTheArn() {
|
||||
TaskDefinition td = new TaskDefinition();
|
||||
td.setTaskDefinitionArn(TASK_DEF_ARN);
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), eq(NetworkMode.awsvpc), eq("256"), eq("512"),
|
||||
eq("arn:aws:iam::000000000000:role/task"), eq("arn:aws:iam::000000000000:role/exec"),
|
||||
eq(List.of("FARGATE")), eq(REGION))).thenReturn(td);
|
||||
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Family", "web").put("NetworkMode", "awsvpc").put("Cpu", "256").put("Memory", "512")
|
||||
.put("TaskRoleArn", "arn:aws:iam::000000000000:role/task")
|
||||
.put("ExecutionRoleArn", "arn:aws:iam::000000000000:role/exec");
|
||||
props.putArray("RequiresCompatibilities").add("FARGATE");
|
||||
ObjectNode container = props.putArray("ContainerDefinitions").addObject()
|
||||
.put("Name", "app").put("Image", "nginx:1").put("Cpu", 128).put("Memory", 256);
|
||||
container.putArray("PortMappings").addObject().put("ContainerPort", 80).put("HostPort", 8080);
|
||||
container.putArray("Environment").addObject().put("Name", "MODE").put("Value", "prod");
|
||||
container.putArray("Secrets").addObject().put("Name", "DB").put("ValueFrom", "arn:aws:secretsmanager:x");
|
||||
container.putArray("Command").add("run").add("--fast");
|
||||
|
||||
StackResource r = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
assertEquals(TASK_DEF_ARN, r.getPhysicalId());
|
||||
assertEquals(TASK_DEF_ARN, r.getAttributes().get("TaskDefinitionArn"));
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<ContainerDefinition>> defs = ArgumentCaptor.forClass(List.class);
|
||||
verify(ecs).registerTaskDefinition(eq("web"), defs.capture(), eq(NetworkMode.awsvpc), eq("256"),
|
||||
eq("512"), anyString(), anyString(), anyList(), eq(REGION));
|
||||
ContainerDefinition def = defs.getValue().get(0);
|
||||
assertEquals("app", def.getName());
|
||||
assertEquals("nginx:1", def.getImage());
|
||||
assertEquals(128, def.getCpu());
|
||||
assertEquals(256, def.getMemory());
|
||||
assertEquals(80, def.getPortMappings().get(0).containerPort());
|
||||
assertEquals(8080, def.getPortMappings().get(0).hostPort());
|
||||
assertEquals("tcp", def.getPortMappings().get(0).protocol());
|
||||
assertEquals("prod", def.getEnvironment().get(0).value());
|
||||
assertEquals("arn:aws:secretsmanager:x", def.getSecrets().get(0).valueFrom());
|
||||
assertEquals(List.of("run", "--fast"), def.getCommand());
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskDefinitionWithoutFamilyGeneratesOneAndIgnoresAnUnknownNetworkMode() {
|
||||
TaskDefinition td = new TaskDefinition();
|
||||
td.setTaskDefinitionArn(TASK_DEF_ARN);
|
||||
when(ecs.registerTaskDefinition(anyString(), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
anyList(), eq(REGION))).thenReturn(td);
|
||||
|
||||
StackResource r = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
provisioner.provision(r, mapper.createObjectNode().put("NetworkMode", "not-a-mode"), ctx());
|
||||
|
||||
ArgumentCaptor<String> family = ArgumentCaptor.forClass(String.class);
|
||||
verify(ecs).registerTaskDefinition(family.capture(), anyList(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), anyList(), eq(REGION));
|
||||
assertTrue(family.getValue().startsWith("my-stack-TaskDef-"), family.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceIsCreatedWithTheParsedLoadBalancersAndNetwork() {
|
||||
when(ecs.createService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(2), eq(LaunchType.FARGATE),
|
||||
anyList(), any(), eq(REGION))).thenReturn(service("front"));
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN)
|
||||
.put("DesiredCount", "2").put("LaunchType", "FARGATE");
|
||||
props.putArray("LoadBalancers").addObject()
|
||||
.put("TargetGroupArn", "arn:aws:elasticloadbalancing:tg").put("ContainerName", "app").put("ContainerPort", 80);
|
||||
ObjectNode awsvpc = props.putObject("NetworkConfiguration").putObject("AwsvpcConfiguration");
|
||||
awsvpc.putArray("Subnets").add("subnet-1").add("subnet-2");
|
||||
awsvpc.putArray("SecurityGroups").add("sg-1");
|
||||
awsvpc.put("AssignPublicIp", "ENABLED");
|
||||
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
assertEquals(SERVICE_ARN, r.getPhysicalId());
|
||||
assertEquals("front", r.getAttributes().get("Name"));
|
||||
assertEquals(SERVICE_ARN, r.getAttributes().get("ServiceArn"));
|
||||
assertEquals(Set.of("Name", "ServiceArn"), r.getAttributes().keySet());
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<EcsLoadBalancer>> lbs = ArgumentCaptor.forClass(List.class);
|
||||
ArgumentCaptor<NetworkConfiguration> net = ArgumentCaptor.forClass(NetworkConfiguration.class);
|
||||
verify(ecs).createService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(2), eq(LaunchType.FARGATE),
|
||||
lbs.capture(), net.capture(), eq(REGION));
|
||||
assertEquals("app", lbs.getValue().get(0).getContainerName());
|
||||
assertEquals(80, lbs.getValue().get(0).getContainerPort());
|
||||
assertEquals(List.of("subnet-1", "subnet-2"), net.getValue().getAwsvpcConfiguration().getSubnets());
|
||||
assertEquals("ENABLED", net.getValue().getAwsvpcConfiguration().getAssignPublicIp());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnchangedServiceIsUpdatedInPlaceOnTheSecondPass() {
|
||||
when(ecs.updateService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(3), isNull(), eq(REGION)))
|
||||
.thenReturn(service("front"));
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
r.getAttributes().put("Name", "front");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Cluster", "web").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN)
|
||||
.put("DesiredCount", "3");
|
||||
|
||||
provisioner.provision(r, props, ctx(SERVICE_ARN));
|
||||
|
||||
assertEquals(SERVICE_ARN, r.getPhysicalId());
|
||||
verify(ecs).updateService("web", "front", TASK_DEF_ARN, 3, null, REGION);
|
||||
verify(ecs, never()).createService(anyString(), anyString(), anyString(), anyInt(), any(), anyList(),
|
||||
any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnnamedServiceKeepsTheNameItGotAtCreateTime() {
|
||||
when(ecs.updateService(eq("web"), eq("my-stack-Service-abc"), eq(TASK_DEF_ARN), eq(1), isNull(), eq(REGION)))
|
||||
.thenReturn(service("my-stack-Service-abc"));
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
r.getAttributes().put("Name", "my-stack-Service-abc");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Cluster", "web").put("TaskDefinition", TASK_DEF_ARN),
|
||||
ctx("arn:aws:ecs:us-east-1:000000000000:service/web/my-stack-Service-abc"));
|
||||
|
||||
verify(ecs).updateService("web", "my-stack-Service-abc", TASK_DEF_ARN, 1, null, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRenamedServiceIsCreatedAsAReplacement() {
|
||||
when(ecs.createService(eq("web"), eq("front-v2"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
|
||||
eq(REGION))).thenReturn(service("front-v2"));
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
r.getAttributes().put("Name", "front");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode()
|
||||
.put("Cluster", "web").put("ServiceName", "front-v2").put("TaskDefinition", TASK_DEF_ARN),
|
||||
ctx(SERVICE_ARN));
|
||||
|
||||
assertNotEquals(SERVICE_ARN, r.getPhysicalId());
|
||||
assertEquals("front-v2", r.getAttributes().get("Name"));
|
||||
verify(ecs, never()).updateService(anyString(), anyString(), anyString(), anyInt(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aServiceMovedToAnotherClusterIsCreatedAsAReplacementAndTheOldOneCleanedUpAfterCommit() {
|
||||
EcsServiceModel moved = service("front");
|
||||
moved.setServiceArn("arn:aws:ecs:us-east-1:000000000000:service/batch/front");
|
||||
when(ecs.createService(eq("batch"), eq("front"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
|
||||
eq(REGION))).thenReturn(moved);
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
r.getAttributes().put("Name", "front");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode()
|
||||
.put("Cluster", "batch").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN), ctx(SERVICE_ARN));
|
||||
|
||||
// Cluster is create-only: same name, other cluster is a replacement, never an in-place update
|
||||
// against a service the new cluster may not have (or may have under another owner).
|
||||
verify(ecs, never()).updateService(anyString(), anyString(), anyString(), anyInt(), any(), anyString());
|
||||
assertEquals(moved.getServiceArn(), r.getPhysicalId());
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals(SERVICE_ARN, provisioner.updateCleanupPhysicalId(r));
|
||||
|
||||
UpdateCleanupResult cleanup = provisioner.completeUpdate(r);
|
||||
|
||||
InOrder order = inOrder(ecs);
|
||||
order.verify(ecs).createService(eq("batch"), eq("front"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
|
||||
eq(REGION));
|
||||
order.verify(ecs).deleteService("web", "front", true, REGION);
|
||||
assertEquals(new UpdateCleanupResult(true, true, SERVICE_ARN, 0, null), cleanup);
|
||||
provisioner.clearUpdate(r);
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theClusterGivenAsAnArnStillCountsAsTheSameCluster() {
|
||||
when(ecs.updateService(eq(CLUSTER_ARN), eq("front"), eq(TASK_DEF_ARN), eq(1), isNull(), eq(REGION)))
|
||||
.thenReturn(service("front"));
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
r.getAttributes().put("Name", "front");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode()
|
||||
.put("Cluster", CLUSTER_ARN).put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN), ctx(SERVICE_ARN));
|
||||
|
||||
verify(ecs).updateService(CLUSTER_ARN, "front", TASK_DEF_ARN, 1, null, REGION);
|
||||
assertFalse(provisioner.hasReplacementUpdate(r), "an in-place update owes no cleanup");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTaskDefinitionUpdateDeregistersTheDisplacedRevisionAfterCommit() {
|
||||
TaskDefinition next = new TaskDefinition();
|
||||
next.setTaskDefinitionArn(TASK_DEF_ARN.replace(":3", ":4"));
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(next);
|
||||
StackResource r = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
|
||||
assertEquals(next.getTaskDefinitionArn(), r.getPhysicalId());
|
||||
assertEquals(TASK_DEF_ARN, provisioner.updateCleanupPhysicalId(r));
|
||||
assertEquals(new UpdateCleanupResult(true, true, TASK_DEF_ARN, 0, null), provisioner.completeUpdate(r));
|
||||
verify(ecs).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRetainedDisplacedEntityIsNotDeletedAndAFailingDeleteIsRetriedThreeTimes() {
|
||||
TaskDefinition next = new TaskDefinition();
|
||||
next.setTaskDefinitionArn(TASK_DEF_ARN.replace(":3", ":4"));
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(next);
|
||||
|
||||
StackResource retained = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
retained.setUpdateReplacePolicy("Retain");
|
||||
provisioner.provision(retained, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
assertNull(provisioner.updateCleanupPhysicalId(retained));
|
||||
assertEquals(new UpdateCleanupResult(true, true, TASK_DEF_ARN, 0, null), provisioner.completeUpdate(retained));
|
||||
verify(ecs, never()).deregisterTaskDefinition(anyString(), anyString());
|
||||
|
||||
StackResource failing = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
provisioner.provision(failing, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
doThrow(new AwsException("ServerException", "busy", 500)).when(ecs).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
UpdateCleanupResult first = provisioner.completeUpdate(failing);
|
||||
assertFalse(first.complete());
|
||||
assertEquals(1, first.attempts());
|
||||
provisioner.completeUpdate(failing);
|
||||
UpdateCleanupResult third = provisioner.completeUpdate(failing);
|
||||
assertEquals(3, third.attempts());
|
||||
assertEquals("busy", third.failureReason());
|
||||
UpdateCleanupResult givenUp = provisioner.completeUpdate(failing);
|
||||
assertEquals(3, givenUp.attempts(), "no fourth attempt");
|
||||
verify(ecs, org.mockito.Mockito.times(3)).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedStackUpdateRollsAReplacementBackToThePriorServiceAndDeletesTheReplacement() {
|
||||
EcsServiceModel moved = service("front");
|
||||
moved.setServiceArn("arn:aws:ecs:us-east-1:000000000000:service/batch/front");
|
||||
when(ecs.createService(eq("batch"), eq("front"), eq(TASK_DEF_ARN), eq(1), isNull(), anyList(), isNull(),
|
||||
eq(REGION))).thenReturn(moved);
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
r.getAttributes().put("Name", "front");
|
||||
r.getAttributes().put("ServiceArn", SERVICE_ARN);
|
||||
provisioner.provision(r, mapper.createObjectNode()
|
||||
.put("Cluster", "batch").put("ServiceName", "front").put("TaskDefinition", TASK_DEF_ARN), ctx(SERVICE_ARN));
|
||||
assertEquals(moved.getServiceArn(), r.getAttributes().get("ServiceArn"));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
// The resource names the prior service again, with the attributes it had, the replacement
|
||||
// is deleted in its cluster, the prior is never touched, and the cleanup record is spent.
|
||||
assertEquals(SERVICE_ARN, r.getPhysicalId());
|
||||
assertEquals(SERVICE_ARN, r.getAttributes().get("ServiceArn"));
|
||||
assertEquals("front", r.getAttributes().get("Name"));
|
||||
verify(ecs).deleteService("batch", "front", true, REGION);
|
||||
verify(ecs, never()).deleteService("web", "front", true, REGION);
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals(UpdateCleanupResult.notApplicable(), provisioner.completeUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnchangedClusterHasNothingToRollBack() {
|
||||
when(ecs.createCluster("web", REGION)).thenReturn(cluster("web"));
|
||||
StackResource r = resource("AWS::ECS::Cluster", "Cluster");
|
||||
provisioner.provision(r, mapper.createObjectNode().put("ClusterName", "web"), ctx("web"));
|
||||
|
||||
// The idempotent create changed nothing, so the rollback is complete with nothing to do.
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
assertEquals("web", r.getPhysicalId());
|
||||
verify(ecs, never()).deleteCluster(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInPlaceUpdateIsNotRolledBackHere() {
|
||||
when(ecs.updateService(eq("web"), eq("front"), eq(TASK_DEF_ARN), eq(3), isNull(), eq(REGION)))
|
||||
.thenReturn(service("front"));
|
||||
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));
|
||||
|
||||
assertFalse(provisioner.rollbackUpdate(r), "no replacement means nothing this helper can undo");
|
||||
assertEquals(SERVICE_ARN, r.getPhysicalId());
|
||||
verify(ecs, never()).deleteService(anyString(), anyString(), eq(true), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRollbackWhoseDeleteFailsStillPointsTheResourceAtThePriorAndPropagates() {
|
||||
TaskDefinition next = new TaskDefinition();
|
||||
next.setTaskDefinitionArn(TASK_DEF_ARN.replace(":3", ":4"));
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(next);
|
||||
doThrow(new AwsException("ServerException", "busy", 500)).when(ecs).deregisterTaskDefinition(next.getTaskDefinitionArn(), REGION);
|
||||
StackResource r = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
r.getAttributes().put("TaskDefinitionArn", TASK_DEF_ARN);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
assertEquals("busy", e.getMessage());
|
||||
assertEquals(TASK_DEF_ARN, r.getPhysicalId());
|
||||
assertEquals(TASK_DEF_ARN, r.getAttributes().get("TaskDefinitionArn"));
|
||||
|
||||
// The replacement the failed update created stays owed a delete; the restored prior does not.
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals(next.getTaskDefinitionArn(), provisioner.updateCleanupPhysicalId(r));
|
||||
org.mockito.Mockito.reset(ecs);
|
||||
assertEquals(new UpdateCleanupResult(true, true, next.getTaskDefinitionArn(), 0, null), provisioner.completeUpdate(r));
|
||||
verify(ecs).deregisterTaskDefinition(next.getTaskDefinitionArn(), REGION);
|
||||
verify(ecs, never()).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOrphanFromAFailedRollbackSurvivesAnInPlaceUpdateAndIsDeletedWithTheNextReplacement() {
|
||||
String orphan = TASK_DEF_ARN.replace(":3", ":4");
|
||||
TaskDefinition next = new TaskDefinition();
|
||||
next.setTaskDefinitionArn(orphan);
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(next);
|
||||
doThrow(new AwsException("ServerException", "busy", 500)).when(ecs).deregisterTaskDefinition(orphan, REGION);
|
||||
StackResource r = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
r.getAttributes().put("TaskDefinitionArn", TASK_DEF_ARN);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
assertEquals(TASK_DEF_ARN, r.getPhysicalId());
|
||||
|
||||
// A rollback with no replacement of its own deletes nothing and keeps the orphan owed.
|
||||
assertFalse(provisioner.rollbackUpdate(r));
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
|
||||
// The next update registers revision 5: revision 3 (the prior) joins the orphan on the list
|
||||
// and the committed cleanup deletes both, never the live revision.
|
||||
TaskDefinition fifth = new TaskDefinition();
|
||||
fifth.setTaskDefinitionArn(TASK_DEF_ARN.replace(":3", ":5"));
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(fifth);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
org.mockito.Mockito.reset(ecs);
|
||||
UpdateCleanupResult cleanup = provisioner.completeUpdate(r);
|
||||
assertTrue(cleanup.complete(), cleanup.toString());
|
||||
verify(ecs).deregisterTaskDefinition(orphan, REGION);
|
||||
verify(ecs).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
verify(ecs, never()).deregisterTaskDefinition(fifth.getTaskDefinitionArn(), REGION);
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givingUpOnOneEntityDoesNotForgetAnotherThatStillHasAttemptsLeft() {
|
||||
String orphan = TASK_DEF_ARN.replace(":3", ":4");
|
||||
TaskDefinition next = new TaskDefinition();
|
||||
next.setTaskDefinitionArn(orphan);
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(next);
|
||||
doThrow(new AwsException("ServerException", "busy", 500)).when(ecs).deregisterTaskDefinition(orphan, REGION);
|
||||
StackResource r = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
r.getAttributes().put("TaskDefinitionArn", TASK_DEF_ARN);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
// The orphan uses up its three attempts in a cleanup where it is the only entry.
|
||||
for (int i = 0; i < 3; i++) {
|
||||
provisioner.completeUpdate(r);
|
||||
}
|
||||
assertEquals(3, provisioner.completeUpdate(r).attempts());
|
||||
|
||||
// A later replacement adds revision 3 to the list; its delete also fails at first.
|
||||
TaskDefinition fifth = new TaskDefinition();
|
||||
fifth.setTaskDefinitionArn(TASK_DEF_ARN.replace(":3", ":5"));
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(fifth);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
doThrow(new AwsException("ServerException", "busy", 500)).when(ecs).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
|
||||
// The result reports the entry with attempts left, so the engine keeps retrying it rather
|
||||
// than giving up on the strength of the exhausted orphan.
|
||||
UpdateCleanupResult first = provisioner.completeUpdate(r);
|
||||
assertFalse(first.complete());
|
||||
assertEquals(1, first.attempts());
|
||||
assertEquals(TASK_DEF_ARN, first.previousPhysicalId());
|
||||
|
||||
// The engine gives up once the lowest count reaches three and clears; only the exhausted
|
||||
// entries go, and an entry that succeeded meanwhile is gone already.
|
||||
org.mockito.Mockito.doReturn(new TaskDefinition()).when(ecs).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
UpdateCleanupResult second = provisioner.completeUpdate(r);
|
||||
assertFalse(second.complete(), "the exhausted orphan is still listed");
|
||||
assertEquals(3, second.attempts());
|
||||
assertEquals(orphan, second.previousPhysicalId());
|
||||
provisioner.clearUpdate(r);
|
||||
assertFalse(provisioner.hasReplacementUpdate(r), "nothing with attempts left remains");
|
||||
// One delete during the failed rollback, then the three cleanup attempts, never a fifth.
|
||||
verify(ecs, org.mockito.Mockito.times(4)).deregisterTaskDefinition(orphan, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearingAfterAGiveUpKeepsAnEntryThatStillHasAttemptsLeft() {
|
||||
String orphan = TASK_DEF_ARN.replace(":3", ":4");
|
||||
TaskDefinition next = new TaskDefinition();
|
||||
next.setTaskDefinitionArn(orphan);
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(), anyList(),
|
||||
eq(REGION))).thenReturn(next);
|
||||
doThrow(new AwsException("ServerException", "busy", 500)).when(ecs).deregisterTaskDefinition(orphan, REGION);
|
||||
StackResource r = resource("AWS::ECS::TaskDefinition", "TaskDef");
|
||||
r.getAttributes().put("TaskDefinitionArn", TASK_DEF_ARN);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("Family", "web"), ctx(TASK_DEF_ARN));
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
provisioner.completeUpdate(r);
|
||||
|
||||
// Cleared with one attempt used: the orphan is not forgotten.
|
||||
provisioner.clearUpdate(r);
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals(orphan, provisioner.updateCleanupPhysicalId(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNonIntegerDesiredCountIsAValidationError() {
|
||||
StackResource r = resource("AWS::ECS::Service", "Service");
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r,
|
||||
mapper.createObjectNode().put("Cluster", "web").put("DesiredCount", "two"), ctx()));
|
||||
assertEquals("ValidationError", e.getErrorCode());
|
||||
assertNull(r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteToleratesOnlyTheServicesOwnNotFoundCodes() {
|
||||
doThrow(new AwsException("ClusterNotFoundException", "gone", 400)).when(ecs).deleteCluster("web", REGION);
|
||||
provisioner.delete("AWS::ECS::Cluster", "web", REGION);
|
||||
|
||||
doThrow(new AwsException("ClientException", "Unable to describe task definition", 400))
|
||||
.when(ecs).deregisterTaskDefinition(TASK_DEF_ARN, REGION);
|
||||
provisioner.delete("AWS::ECS::TaskDefinition", TASK_DEF_ARN, REGION);
|
||||
|
||||
doThrow(new AwsException("ServiceNotFoundException", "gone", 400))
|
||||
.when(ecs).deleteService("web", "front", true, REGION);
|
||||
provisioner.delete("AWS::ECS::Service", SERVICE_ARN, REGION);
|
||||
|
||||
doThrow(new AwsException("ClusterContainsTasksException", "busy", 400)).when(ecs).deleteCluster("busy", REGION);
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.delete("AWS::ECS::Cluster", "busy", REGION));
|
||||
assertEquals("ClusterContainsTasksException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteServiceParsesTheClusterOutOfTheArn() {
|
||||
provisioner.delete("AWS::ECS::Service", SERVICE_ARN, REGION);
|
||||
verify(ecs).deleteService("web", "front", true, REGION);
|
||||
|
||||
provisioner.delete("AWS::ECS::Service", "bare-name", REGION);
|
||||
verify(ecs).deleteService(null, "bare-name", true, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteWithoutAPhysicalIdIsANoOp() {
|
||||
provisioner.delete("AWS::ECS::Cluster", null, REGION);
|
||||
provisioner.delete("AWS::ECS::Service", "", REGION);
|
||||
verify(ecs, never()).deleteCluster(any(), any());
|
||||
verify(ecs, never()).deleteService(any(), any(), eq(true), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAResourceTypeItDoesNotOwn() {
|
||||
StackResource r = resource("AWS::ECS::CapacityProvider", "Cp");
|
||||
assertThrows(IllegalStateException.class, () -> provisioner.provision(r, mapper.createObjectNode(), ctx()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user