feat(cloudformation): add optional-value and delete cleanup hooks (#5067)

This commit is contained in:
XuJian authored and GitHub committed 2026-10-09 15:04:03 -04:00
1 parent 233d05eab1
commit 07b529e150
6 files changed
+258 -14

No files matched your search

@@ -23,15 +23,19 @@ import java.time.Clock;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
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.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.RETURNS_DEFAULTS;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
@@ -139,11 +143,13 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {role, logGroup, alreadyDeleted, adopted, owned}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
assertEquals("DELETE_COMPLETE", stack.getStatus());
verify(provisioner).completeDeleteCleanup(role);
verify(provisioner, never()).completeUpdate(any());
verify(provisioner).delete(role, REGION);
verify(provisioner).delete(logGroup, REGION);
verify(provisioner).delete(owned, REGION);
@@ -165,7 +171,7 @@ class CloudFormationServiceRollbackTest {
StackResource bucket = resource("Bucket", "leak-probe-bucket", "AWS::S3::Bucket", "UPDATE_FAILED");
stack.getResources().put(bucket.getLogicalId(), bucket);
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
doThrow(new AwsException("BucketNotEmpty", "The bucket you tried to delete is not empty", 409))
.when(provisioner).delete(eq(bucket), eq(REGION));
@@ -224,7 +230,7 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {listener, firstQueue, secondQueue, targetGroup}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
@@ -261,7 +267,7 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {listener, firstQueue, secondQueue, targetGroup}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
@@ -295,7 +301,7 @@ class CloudFormationServiceRollbackTest {
for (StackResource resource : new StackResource[] {consumer, partner, firstQueue, secondQueue}) {
stack.getResources().put(resource.getLogicalId(), resource);
}
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
service.deleteStackResources(stack, REGION, ACCOUNT);
@@ -331,7 +337,7 @@ class CloudFormationServiceRollbackTest {
StackResource secondQueue = resource("SecondQueue", "second-url", "AWS::SQS::Queue", "CREATE_COMPLETE");
stack.getResources().put(firstQueue.getLogicalId(), firstQueue);
stack.getResources().put(secondQueue.getLogicalId(), secondQueue);
when(provisioner.completeUpdate(any())).thenReturn(UpdateCleanupResult.notApplicable());
when(provisioner.completeDeleteCleanup(any())).thenReturn(UpdateCleanupResult.notApplicable());
AtomicReference<Throwable> failure = new AtomicReference<>();
Thread deleter = new Thread(null, () -> {
@@ -352,6 +358,72 @@ class CloudFormationServiceRollbackTest {
deletes.verify(provisioner).delete(firstQueue, REGION);
}
@Test
void failedStackDeletePreservesRollbackMetadataUntilTheResourceDeleteSucceeds() {
Stack stack = new Stack();
stack.setStackName("delete-keeps-rollback-state");
stack.setStackId("stack-id");
stack.setRegion(REGION);
stack.setStatus("UPDATE_ROLLBACK_FAILED");
StackResource resource = resource("Stateful", "current-id", "AWS::Test::Stateful", "UPDATE_FAILED");
resource.getAttributes().put("rollbackSnapshot", "original-configuration");
resource.getAttributes().put("cleanupPending", "true");
stack.getResources().put(resource.getLogicalId(), resource);
CfnResourceDispatcher cleanupDispatcher = mock(CfnResourceDispatcher.class, invocation ->
switch (invocation.getMethod().getName()) {
case "updateCleanupPhysicalId" -> resource.getAttributes().containsKey("cleanupPending")
? "displaced-id" : null;
case "completeUpdate" -> {
resource.getAttributes().remove("rollbackSnapshot");
yield new UpdateCleanupResult(true, true, "displaced-id", 0, null);
}
case "completeDeleteCleanup" -> resource.getAttributes().containsKey("cleanupPending")
? new UpdateCleanupResult(true, true, "displaced-id", 0, null)
: UpdateCleanupResult.notApplicable();
case "clearUpdate" -> {
resource.getAttributes().remove("rollbackSnapshot");
resource.getAttributes().remove("cleanupPending");
yield null;
}
case "clearDeleteCleanup" -> {
resource.getAttributes().remove("cleanupPending");
yield null;
}
default -> RETURNS_DEFAULTS.answer(invocation);
});
AtomicInteger attempts = new AtomicInteger();
doAnswer(invocation -> {
if (attempts.incrementAndGet() == 1) {
throw new AwsException("ServiceUnavailable", "temporary current-resource deletion failure", 503);
}
assertEquals("original-configuration", resource.getAttributes().get("rollbackSnapshot"));
resource.getAttributes().remove("rollbackSnapshot");
return null;
}).when(cleanupDispatcher).delete(resource, REGION);
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.defaultAccountId()).thenReturn(ACCOUNT);
CloudFormationService deleteService = new CloudFormationService(cleanupDispatcher, mock(S3Service.class),
mock(SsmService.class), mock(CfnDynamicReferences.class), new ObjectMapper(), config,
mock(RegionResolver.class), Clock.systemUTC(), new InMemoryStorageFactory());
assertThrows(IllegalStateException.class, () -> deleteService.deleteStackResources(stack, REGION, ACCOUNT));
assertEquals("DELETE_FAILED", stack.getStatus());
assertEquals("DELETE_FAILED", resource.getStatus());
assertEquals("original-configuration", resource.getAttributes().get("rollbackSnapshot"));
assertNull(resource.getAttributes().get("cleanupPending"));
assertTrue(stack.getEvents().stream().anyMatch(event -> "Stateful".equals(event.getLogicalResourceId())
&& "displaced-id".equals(event.getPhysicalResourceId())
&& "DELETE_COMPLETE".equals(event.getResourceStatus())));
deleteService.deleteStackResources(stack, REGION, ACCOUNT);
assertEquals(2, attempts.get());
assertEquals("DELETE_COMPLETE", stack.getStatus());
assertEquals("DELETE_COMPLETE", resource.getStatus());
assertNull(resource.getAttributes().get("rollbackSnapshot"));
}
private static StackResource resource(String logicalId, String physicalId, String resourceType, String status) {
StackResource resource = new StackResource();
resource.setLogicalId(logicalId);
@@ -37,6 +37,81 @@ class CloudFormationTemplateEngineTest {
}
}
@Test
void selectedNoValueOmitsTheOptionalPropertyWhileKeepingLiteralEmptyText() {
JsonNode source = json("""
{"Scopes":{"Fn::If":["Include",[],{"Ref":"AWS::NoValue"}]},"Name":""}
""");
assertEquals(json("{\"Name\":\"\"}"),
engineWithCondition("Include", false).resolveNodeOmittingNoValue(source));
}
@Test
void selectedNoValueOmitsTheOptionalArrayEntryWhileKeepingLiteralEmptyText() {
JsonNode source = json("""
["first",{"Fn::If":["Include","read",{"Ref":"AWS::NoValue"}]},"","last"]
""");
assertEquals(json("[\"first\",\"\",\"last\"]"),
engineWithCondition("Include", false).resolveNodeOmittingNoValue(source));
}
@Test
void optionalNodeResolutionPreservesTheLegacyNoValueContract() {
JsonNode noValue = json("{\"Ref\":\"AWS::NoValue\"}");
assertEquals("", engine().resolveNode(noValue).asText());
assertTrue(engine().resolveNodeOmittingNoValue(noValue).isMissingNode());
assertEquals(json("\"\""), engine().resolveNodeOmittingNoValue(json("\"\"")));
assertEquals(json("\"AWS::NoValue\""), engine().resolveNodeOmittingNoValue(json("\"AWS::NoValue\"")));
}
@Test
void optionalNodeResolutionDoesNotRewriteOtherIntrinsicArguments() {
JsonNode joined = json("""
{"Fn::Join": ["-", ["read", {"Ref": "AWS::NoValue"}, "all"]]}
""");
assertEquals("read--all", engine().resolveNodeOmittingNoValue(joined).asText());
JsonNode malformedConditional = json("{\"Fn::If\":[\"IncludeScopes\"]}");
assertEquals(engine().resolveNode(malformedConditional),
engine().resolveNodeOmittingNoValue(malformedConditional));
assertEquals(json("{\"keep\":\"\"}"), engine().resolveNodeOmittingNoValue(
json("{\"keep\":{\"Ref\":\"AWS::NoValue\",\"extra\":true}}")));
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void optionalNodeResolutionOmitsSelectedNestedValuesWithoutMutatingInput(boolean include) {
CloudFormationTemplateEngine e = engineWithCondition("Include", include);
JsonNode source = json("""
{"nested":{"optional":{"Fn::If":["Include",{"list":[0,false,""]},{"Ref":"AWS::NoValue"}]},
"empty":"","literal":"AWS::NoValue","nil":null},
"items":["first",{"Fn::If":["Include",{"Ref":"AWS::Region"},{"Ref":"AWS::NoValue"}]},"last"]}
""");
JsonNode before = source.deepCopy();
JsonNode expected = json(include ? """
{"nested":{"optional":{"list":[0,false,""]},"empty":"","literal":"AWS::NoValue","nil":null},
"items":["first","us-east-1","last"]}
""" : """
{"nested":{"empty":"","literal":"AWS::NoValue","nil":null},"items":["first","last"]}
""");
assertEquals(expected, e.resolveNodeOmittingNoValue(source));
assertEquals(before, source);
}
@Test
void optionalNodeResolutionOnlyResolvesTheSelectedDynamicReferenceOnce() {
List<String> calls = new ArrayList<>();
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(), Map.of(),
Map.of("Include", true), Map.of(), mapper, name -> null, value -> {
calls.add(value);
return "{{resolve:ssm:/literal-result}}";
});
assertEquals("{{resolve:ssm:/literal-result}}", e.resolveNodeOmittingNoValue(json("""
{"Fn::If":["Include","{{resolve:ssm:/selected}}","{{resolve:ssm:/unselected}}"]}
""")).asText());
assertEquals(List.of("{{resolve:ssm:/selected}}"), calls);
}
@Test
void joinAcceptsSplitAsItsListOfValues() {
assertEquals("x|y|z", engine().resolve(json("""