fix(cloudformation): retain deleted stacks briefly for destroy polling (#1029)

This commit is contained in:
shrimptails-f authored and GitHub committed 2026-05-26 21:48:48 -05:00
1 parent 37f37d1292
commit 8be5d03bbc
6 files changed
+295 -6

No files matched your search

@@ -1,8 +1,11 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.testing.MutableClock;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode;
@@ -10,6 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Base64;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -33,11 +37,19 @@ class CloudFormationIntegrationTest {
private static final String COGNITO_CONTENT_TYPE = "application/x-amz-json-1.1";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Inject
MutableClock clock;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@BeforeEach
void resetClock() {
clock.reset();
}
private static byte[] buildHandlerZip() {
try {
var baos = new ByteArrayOutputStream();
@@ -999,6 +1011,170 @@ class CloudFormationIntegrationTest {
.body(containsString("<StackName>arn-events-stack</StackName>"));
}
@Test
void describeDeletedStackEvents_byArn_returnsDeleteCompleteEvents() throws Exception {
String template = """
{
"Resources": {
"MyBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "deleted-arn-events-test-bucket"
}
}
}
}
""";
String createResponse = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", "deleted-arn-events-stack")
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackId>"))
.extract().asString();
String stackArn = createResponse.substring(
createResponse.indexOf("<StackId>") + "<StackId>".length(),
createResponse.indexOf("</StackId>"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteStack")
.formParam("StackName", "deleted-arn-events-stack")
.when()
.post("/")
.then()
.statusCode(200);
String deletedEventsXml = null;
long deadline = System.currentTimeMillis() + 10_000;
while (System.currentTimeMillis() < deadline) {
deletedEventsXml = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStackEvents")
.formParam("StackName", stackArn)
.when()
.post("/")
.then()
.statusCode(200)
.extract().asString();
if (deletedEventsXml.contains("<ResourceStatus>DELETE_COMPLETE</ResourceStatus>")) {
break;
}
Thread.sleep(200);
}
assertThat(deletedEventsXml, containsString("<StackId>" + stackArn + "</StackId>"));
assertThat(deletedEventsXml, containsString("<ResourceStatus>DELETE_COMPLETE</ResourceStatus>"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackArn)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackStatus>DELETE_COMPLETE</StackStatus>"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", "deleted-arn-events-stack")
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("does not exist"));
}
@Test
void describeDeletedStack_byArn_expiresAfterRetentionWindow() throws Exception {
String template = """
{
"Resources": {
"MyBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "deleted-arn-expiry-test-bucket"
}
}
}
}
""";
String createResponse = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", "deleted-arn-expiry-stack")
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackId>"))
.extract().asString();
String stackArn = createResponse.substring(
createResponse.indexOf("<StackId>") + "<StackId>".length(),
createResponse.indexOf("</StackId>"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteStack")
.formParam("StackName", "deleted-arn-expiry-stack")
.when()
.post("/")
.then()
.statusCode(200);
long readyDeadline = System.currentTimeMillis() + 10_000;
while (System.currentTimeMillis() < readyDeadline) {
String deletedEventsXml = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStackEvents")
.formParam("StackName", stackArn)
.when()
.post("/")
.then()
.statusCode(200)
.extract().asString();
if (deletedEventsXml.contains("<ResourceStatus>DELETE_COMPLETE</ResourceStatus>")) {
break;
}
Thread.sleep(200);
}
clock.advance(Duration.ofSeconds(31));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStackEvents")
.formParam("StackName", stackArn)
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("does not exist"));
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackArn)
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("does not exist"));
}
@Test
void deleteChangeSet_nonExistentChangeSet_returnsError() {
String template = """
@@ -0,0 +1,46 @@
package io.github.hectorvent.floci.testing;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.concurrent.atomic.AtomicReference;
@Alternative
@Priority(1)
@ApplicationScoped
public class MutableClock extends Clock {
private static final Instant DEFAULT_INSTANT = Instant.parse("2026-01-01T00:00:00Z");
private final AtomicReference<Instant> current = new AtomicReference<>(DEFAULT_INSTANT);
private final ZoneId zone = ZoneOffset.UTC;
@Override
public ZoneId getZone() {
return zone;
}
@Override
public Clock withZone(ZoneId zone) {
return this;
}
@Override
public Instant instant() {
return current.getAndUpdate(now -> now.plusMillis(1));
}
public void reset() {
current.set(DEFAULT_INSTANT);
}
public void advance(Duration duration) {
current.updateAndGet(now -> now.plus(duration));
}
}