Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): report a failed resource in the stack's rollback reason the way AWS does (#5904)

Kenshin Okinakac68cc7694fcf
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
index 14b798317d..c1443ef223 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
@@ -2360,7 +2360,7 @@ public class CloudFormationService implements ResourceProvider {
Set<String> attemptedResourceIds) {
LOG.warnv("Stack {0} resource {1} failed: {2}", stack.getStackName(),
failedResource.getLogicalId(), failedResource.getStatusReason());
- rollbackFailedExecution(stack, region, isCreate, failedResource.getStatusReason(), previousState,
+ rollbackFailedExecution(stack, region, isCreate, rollbackReason(isCreate, failedResource), previousState,
attemptedResourceIds);
}
@@ -2403,6 +2403,57 @@ public class CloudFormationService implements ResourceProvider {
persistStack(stack);
}
+ /**
+ * The stack's reason for rolling back: a summary naming the failed resource, whose own message
+ * stays on its own event. A resource an update adds failed to create, and only a create
+ * rollback says the rollback was requested, the default OnFailure counting as the request.
+ */
+ private static String rollbackReason(boolean isCreate, StackResource failedResource) {
+ String operation = "UPDATE_FAILED".equals(failedResource.getStatus()) ? "update" : "create";
+ String summary = "The following resource(s) failed to " + operation + ": ["
+ + failedResource.getLogicalId() + "].";
+ return isCreate ? summary + " Rollback requested by user." : summary;
+ }
+
+ /**
+ * The stack's reason, or, while that is still the rollback summary, the failed resource's own
+ * message, which is what a stack set instance reports as the cause.
+ */
+ static String failureCause(Stack stack) {
+ String reason = stack.getStatusReason();
+ List<StackEvent> events = stack.eventsSnapshot();
+ int rollbackStart = events.size() - 1;
+ while (rollbackStart >= 0 && !isStackEvent(stack, events.get(rollbackStart), ROLLBACK_STARTS)) {
+ if (isStackEvent(stack, events.get(rollbackStart), OPERATION_STARTS)) {
+ return reason;
+ }
+ rollbackStart--;
+ }
+ if (rollbackStart < 0 || !Objects.equals(reason, events.get(rollbackStart).getResourceStatusReason())) {
+ return reason;
+ }
+ for (int i = rollbackStart - 1; i >= 0; i--) {
+ StackEvent event = events.get(i);
+ if (isOwnEvent(stack, event)) {
+ return reason;
+ }
+ if ("CREATE_FAILED".equals(event.getResourceStatus()) || "UPDATE_FAILED".equals(event.getResourceStatus())) {
+ return event.getResourceStatusReason();
+ }
+ }
+ return reason;
+ }
+
+ private static boolean isStackEvent(Stack stack, StackEvent event, Set<String> statuses) {
+ return isOwnEvent(stack, event) && statuses.contains(event.getResourceStatus());
+ }
+
+ /** The stack's own event: a resource may share the stack's name, but never its stack ID. */
+ private static boolean isOwnEvent(Stack stack, StackEvent event) {
+ return stack.getStackName().equals(event.getLogicalResourceId())
+ && Objects.equals(stack.getStackId(), event.getPhysicalResourceId());
+ }
+
private List<UpdateCleanupFailure> finishCommittedResourceCleanup(Stack stack, String region) {
return finishCommittedResourceCleanup(stack, region, false);
}
@@ -3612,6 +3663,10 @@ public class CloudFormationService implements ResourceProvider {
&& normalizedHost.endsWith("." + normalizedSuffix);
}
+ private static final Set<String> ROLLBACK_STARTS = Set.of("ROLLBACK_IN_PROGRESS", "UPDATE_ROLLBACK_IN_PROGRESS");
+
+ private static final Set<String> OPERATION_STARTS = Set.of("CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS");
+
/**
* Terminal statuses under which {@link #executeTemplate} actually finished creating or
* updating a stack's resources and computed its Outputs, including the two "committed but
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java
index f2456126a5..5599a0c68f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java
@@ -678,7 +678,7 @@ public class StackSetService {
} else if (!"CREATE_COMPLETE".equals(stackStatus) && !"UPDATE_COMPLETE".equals(stackStatus)) {
inst.setStatus("INOPERABLE");
inst.setDetailedStatus("FAILED");
- inst.setStatusReason(stacks.isEmpty() ? null : stacks.get(0).getStatusReason());
+ inst.setStatusReason(stacks.isEmpty() ? null : CloudFormationService.failureCause(stacks.get(0)));
} else {
inst.setDetailedStatus("SUCCEEDED");
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnStackWaits.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnStackWaits.java
index 96a2c0e15e..d3a7fafabb 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnStackWaits.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnStackWaits.java
@@ -1,9 +1,11 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.restassured.path.xml.XmlPath;
+import io.restassured.path.xml.element.Node;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
+import java.util.List;
import java.util.Set;
import static io.restassured.RestAssured.given;
@@ -30,6 +32,8 @@ final class CfnStackWaits {
"UPDATE_COMPLETE", "UPDATE_FAILED", "UPDATE_ROLLBACK_COMPLETE", "UPDATE_ROLLBACK_FAILED",
"DELETE_COMPLETE", "DELETE_FAILED");
+ private static final Set<String> OPERATION_STARTS = Set.of("CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS");
+
/**
* Waits until DescribeStacks reports a terminal status for a create or update and returns
* it, so the caller decides what counts as success. A DescribeStacks that is not 200 (the
@@ -112,6 +116,52 @@ final class CfnStackWaits {
return last;
}
+ /**
+ * The reason on the first resource event that failed a create or update in the stack's latest
+ * operation, the one its last stack-level CREATE_IN_PROGRESS or UPDATE_IN_PROGRESS began. The
+ * stack's own reason only names the failed resource, as on AWS, so the failure's message is read
+ * here. Fails when DescribeStackEvents is not 200 or no resource failed in that operation.
+ */
+ static String failedResourceReason(String stackName) {
+ return failedResourceReason(stackName, null);
+ }
+
+ static String failedResourceReason(String stackName, String authorization) {
+ RequestSpecification request = given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackEvents")
+ .formParam("StackName", stackName);
+ if (authorization != null) {
+ request.header("Authorization", authorization);
+ }
+ Response response = request.when().post("/");
+ if (response.statusCode() != 200) {
+ fail("DescribeStackEvents " + stackName + " answered " + response.statusCode() + ": " + response.asString());
+ }
+ List<Node> newestFirst = response.xmlPath().getList("**.findAll { it.name() == 'member' }", Node.class);
+ String reason = null;
+ for (Node event : newestFirst) {
+ String status = text(event, "ResourceStatus");
+ if (text(event, "StackName").equals(text(event, "LogicalResourceId"))
+ && text(event, "StackId").equals(text(event, "PhysicalResourceId"))) {
+ if (OPERATION_STARTS.contains(status)) {
+ break;
+ }
+ } else if ("CREATE_FAILED".equals(status) || "UPDATE_FAILED".equals(status)) {
+ reason = text(event, "ResourceStatusReason");
+ }
+ }
+ if (reason == null) {
+ fail("Stack " + stackName + " has no failed resource event in its latest operation: " + response.asString());
+ }
+ return reason;
+ }
+
+ private static String text(Node event, String name) {
+ Node child = event.getNode(name);
+ return child == null ? "" : child.value();
+ }
+
/**
* Waits until DescribeStacks answers 400, which is how a missing stack is reported. Fails on
* DELETE_FAILED or after ten seconds.
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAsgLaunchTemplateIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAsgLaunchTemplateIntegrationTest.java
index 9bc72658f5..6b1d893006 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAsgLaunchTemplateIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAsgLaunchTemplateIntegrationTest.java
@@ -327,7 +327,8 @@ class CloudFormationAsgLaunchTemplateIntegrationTest {
}
""".formatted(suffix, ltName));
- assertThat(describeStacks(stackName),
+ assertThat(describeStacks(stackName), containsString("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"));
+ assertThat(CfnStackWaits.failedResourceReason(stackName, CFN_AUTH),
containsString("Value of property OnDemandBaseCapacity must be an integer."));
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java
index b3ee768488..e0834d8805 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java
@@ -905,8 +905,8 @@ class CloudFormationDeletionPolicyIntegrationTest {
assertThat(events, containsString("<LogicalResourceId>ChildStack</LogicalResourceId>"));
assertThat(events, containsString("<ResourceStatus>CREATE_FAILED</ResourceStatus>"));
assertThat(events, containsString("<ResourceStatusReason>Nested stack " + stackName
- + "-ChildStack failed: The specified log group already exists: " + logGroupName
- + "</ResourceStatusReason>"));
+ + "-ChildStack failed: The following resource(s) failed to create: [MyLogGroup]."
+ + " Rollback requested by user.</ResourceStatusReason>"));
} finally {
deleteStack(stackName);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputEnforcementIntegrationTest.java
index 24a839648d..1731545489 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputEnforcementIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputEnforcementIntegrationTest.java
@@ -119,10 +119,12 @@ class CloudFormationGetStackOutputEnforcementIntegrationTest {
String producer = producer("vpc-" + base);
String roleArn = role(base, trustPolicy, DESCRIBE_STACKS);
- CfnStackWaits.StackState state = awaitConsumer(consume(base, producer, roleArn));
+ String consumer = consume(base, producer, roleArn);
+ CfnStackWaits.StackState state = awaitConsumer(consumer);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("User: arn:aws:iam::" + CONSUMER + ":root is not authorized to"
+ String failure = CfnStackWaits.failedResourceReason(consumer, auth(CONSUMER, REGION, "cloudformation"));
+ assertThat(failure, containsString("User: arn:aws:iam::" + CONSUMER + ":root is not authorized to"
+ " perform: sts:AssumeRole on resource: " + roleArn + " (Service: AWSSecurityTokenService; Status"
+ " Code: 403; Error Code: AccessDenied; Request ID: "));
}
@@ -131,13 +133,15 @@ class CloudFormationGetStackOutputEnforcementIntegrationTest {
String producer = producer("vpc-" + base);
String roleArn = role(base, allow("{\"AWS\":\"arn:aws:iam::" + CONSUMER + ":root\"}"), policy);
- CfnStackWaits.StackState state = awaitConsumer(consume(base, producer, roleArn));
+ String consumer = consume(base, producer, roleArn);
+ CfnStackWaits.StackState state = awaitConsumer(consumer);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("User: arn:aws:sts::" + OWNER + ":assumed-role/" + name(base)
+ String failure = CfnStackWaits.failedResourceReason(consumer, auth(CONSUMER, REGION, "cloudformation"));
+ assertThat(failure, containsString("User: arn:aws:sts::" + OWNER + ":assumed-role/" + name(base)
+ "/GetStackOutput is not authorized to perform: cloudformation:DescribeStacks on resource:"
+ " arn:aws:cloudformation:" + REGION + ":" + OWNER + ":stack/" + producer + "/"));
- assertThat(state.reason(), containsString(" " + reason + " (Service: AmazonCloudFormation; Status Code: 403;"
+ assertThat(failure, containsString(" " + reason + " (Service: AmazonCloudFormation; Status Code: 403;"
+ " Error Code: AccessDenied; Request ID: "));
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputIntegrationTest.java
index 82458df7f1..f115594566 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationGetStackOutputIntegrationTest.java
@@ -112,7 +112,7 @@ class CloudFormationGetStackOutputIntegrationTest {
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(consumer);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("TemplateError: Fn::GetStackOutput references output Nope from stack "
+ assertThat(CfnStackWaits.failedResourceReason(consumer), containsString("TemplateError: Fn::GetStackOutput references output Nope from stack "
+ producer + ", but this output was not found. The output may have been deleted."));
parameterValue(DEFAULT_ACCOUNT, US_EAST_1, parameter("missing-output")).statusCode(400);
}
@@ -126,9 +126,9 @@ class CloudFormationGetStackOutputIntegrationTest {
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(consumer);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("Stack with id " + missing + " does not exist (Service:"
+ assertThat(CfnStackWaits.failedResourceReason(consumer), containsString("Stack with id " + missing + " does not exist (Service:"
+ " AmazonCloudFormation; Status Code: 400; Error Code: ValidationError; Request ID: "));
- assertThat(state.reason(), endsWith("; Proxy: null)"));
+ assertThat(CfnStackWaits.failedResourceReason(consumer), endsWith("; Proxy: null)"));
parameterValue(DEFAULT_ACCOUNT, US_EAST_1, parameter("missing-stack")).statusCode(400);
}
@@ -173,11 +173,12 @@ class CloudFormationGetStackOutputIntegrationTest {
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(consumer).status());
parameterValue(DEFAULT_ACCOUNT, US_EAST_1, parameter("account")).body("Parameter.Value", equalTo("vpc-other-account"));
- assertThat(CfnStackWaits.awaitTerminal(noRole).reason(),
+ assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(noRole).status());
+ assertThat(CfnStackWaits.failedResourceReason(noRole),
containsString("Stack with id " + producer + " does not exist"));
CfnStackWaits.StackState missingRoleState = CfnStackWaits.awaitTerminal(missingRole);
assertEquals("ROLLBACK_COMPLETE", missingRoleState.status());
- assertThat(missingRoleState.reason(), containsString("is not authorized to perform: sts:AssumeRole on resource: "
+ assertThat(CfnStackWaits.failedResourceReason(missingRole), containsString("is not authorized to perform: sts:AssumeRole on resource: "
+ nobodyArn + " (Service: AWSSecurityTokenService; Status Code: 403;"
+ " Error Code: AccessDenied; Request ID: "));
}
@@ -243,7 +244,7 @@ class CloudFormationGetStackOutputIntegrationTest {
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(consumer);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("1 validation error detected: Value '" + reference + "'"
+ assertThat(CfnStackWaits.failedResourceReason(consumer), containsString("1 validation error detected: Value '" + reference + "'"
+ " at 'stackName' failed to satisfy constraint: Member must satisfy regular expression pattern:"
+ " [a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]* (Service: AmazonCloudFormation; Status Code: 400;"
+ " Error Code: ValidationError; Request ID: "));
@@ -268,7 +269,7 @@ class CloudFormationGetStackOutputIntegrationTest {
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(consumer);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("1 validation error detected: Value '" + reference
+ assertThat(CfnStackWaits.failedResourceReason(consumer), containsString("1 validation error detected: Value '" + reference
+ "' at 'stackName' failed to satisfy constraint"));
} finally {
ssmService.deleteParameter(producerName, US_EAST_1);
@@ -284,7 +285,7 @@ class CloudFormationGetStackOutputIntegrationTest {
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(consumer);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("The security token included in the request is invalid (Service:"
+ assertThat(CfnStackWaits.failedResourceReason(consumer), containsString("The security token included in the request is invalid (Service:"
+ " AmazonCloudFormation; Status Code: 403; Error Code: InvalidClientTokenId; Request ID: "));
}
@@ -450,7 +451,7 @@ class CloudFormationGetStackOutputIntegrationTest {
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(parent);
assertEquals("ROLLBACK_COMPLETE", state.status());
- assertThat(state.reason(), containsString("Template error: Cannot use Fn::GetStackOutput in Conditions."));
+ assertThat(CfnStackWaits.failedResourceReason(parent), containsString("Template error: Cannot use Fn::GetStackOutput in Conditions."));
} finally {
s3Service.deleteObject(bucket, "child.json");
s3Service.deleteBucket(bucket);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadIntegrationTest.java
index 907baec325..2ad4f2a995 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaHotReloadIntegrationTest.java
@@ -5,6 +5,7 @@ import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
@@ -61,8 +62,9 @@ class CloudFormationLambdaHotReloadIntegrationTest {
createStack(stackName, template(fnName, "relative/dist"));
describeStacks(stackName)
- .body(not(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>")))
- .body(containsString("Hot-reload S3Key must be an absolute path"));
+ .body(not(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>")));
+ assertThat(CfnStackWaits.failedResourceReason(stackName, CFN_AUTH),
+ containsString("Hot-reload S3Key must be an absolute path"));
given()
.when().get("/2015-03-31/functions/" + fnName)
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationParameterValuesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationParameterValuesIntegrationTest.java
index 28ac98a142..da1e41e5a8 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationParameterValuesIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationParameterValuesIntegrationTest.java
@@ -435,7 +435,8 @@ class CloudFormationParameterValuesIntegrationTest {
.then()
.statusCode(200);
CfnStackWaits.StackState state = CfnStackWaits.awaitStatus(stackName, "ROLLBACK_COMPLETE");
- assertEquals("Parameters: [Env] must have values", state.reason());
+ assertEquals("The following resource(s) failed to create: [Child]. Rollback requested by user.",
+ state.reason());
request("DescribeStackEvents", stackName)
.when()
.post("/")
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java
index a1abec149b..c842e953b5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java
@@ -180,7 +180,8 @@ class CloudFormationRoute53RecordSetIntegrationTest {
// AWS sends CREATE for a record the stack does not own yet, so the name being taken
// fails the create and the rollback leaves the other owner's record alone.
assertEquals("ROLLBACK_COMPLETE", state.status(), state.reason());
- assertTrue(state.reason().contains("already exists"), state.reason());
+ String failure = CfnStackWaits.failedResourceReason(stackName);
+ assertTrue(failure.contains("already exists"), failure);
String afterCreate = listResourceRecordSets(zoneId);
assertTrue(afterCreate.contains("192.0.2.10"), "API record overwritten: " + afterCreate);
assertFalse(afterCreate.contains("192.0.2.20"), "stack value written: " + afterCreate);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSchedulerScheduleIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSchedulerScheduleIntegrationTest.java
index 2bee5e6500..3dc1811b2d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSchedulerScheduleIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSchedulerScheduleIntegrationTest.java
@@ -862,7 +862,8 @@ class CloudFormationSchedulerScheduleIntegrationTest {
cloudFormation(stack, "CreateStack", root.toString(), Map.of("Name", name, "UseOptions", "false"));
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stack);
assertEquals("ROLLBACK_COMPLETE", state.status(), property);
- assertTrue(state.reason().toLowerCase(Locale.ROOT).contains(field.toLowerCase(Locale.ROOT)), state.reason());
+ String failure = CfnStackWaits.failedResourceReason(stack);
+ assertTrue(failure.toLowerCase(Locale.ROOT).contains(field.toLowerCase(Locale.ROOT)), failure);
getSchedule(name, "default").then().statusCode(404);
} finally {
deleteStack(stack);
@@ -884,7 +885,8 @@ class CloudFormationSchedulerScheduleIntegrationTest {
cloudFormation(stack, "CreateStack", root.toString(), Map.of("Name", name));
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stack);
assertEquals("ROLLBACK_COMPLETE", state.status(), property);
- assertTrue(state.reason().contains(property), state.reason());
+ String failure = CfnStackWaits.failedResourceReason(stack);
+ assertTrue(failure.contains(property), failure);
getSchedule(name, "default").then().statusCode(404);
} finally {
deleteStack(stack);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSecretTargetAttachmentIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSecretTargetAttachmentIntegrationTest.java
index 7a9ae7e8cf..e31d355b29 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSecretTargetAttachmentIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationSecretTargetAttachmentIntegrationTest.java
@@ -295,8 +295,9 @@ class CloudFormationSecretTargetAttachmentIntegrationTest {
String describeXml = describeStack(stackName);
assertTrue(describeXml.contains("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"),
"stack should be ROLLBACK_COMPLETE: " + describeXml);
- assertTrue(describeXml.contains("already attached"),
- "stack should report the duplicate attachment: " + describeXml);
+ String failure = CfnStackWaits.failedResourceReason(stackName, CFN_AUTH);
+ assertTrue(failure.contains("already attached"),
+ "the attachment should report the duplicate: " + failure);
JsonNode rolledBack = getSecretJson(secretName);
assertEquals("admin", rolledBack.path("username").asText());
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceRollbackTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceRollbackTest.java
index 9bf142ea20..5dcf83ee49 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceRollbackTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceRollbackTest.java
@@ -139,7 +139,8 @@ class CloudFormationServiceRollbackTest {
assertEquals(List.of("ROLLBACK_IN_PROGRESS", "ROLLBACK_COMPLETE"), stack.statuses());
assertEquals(List.of("ROLLBACK_IN_PROGRESS", "ROLLBACK_COMPLETE"), stackEventStatuses(stack));
stack.assertEachStatusFollowsItsEvent();
- assertEquals("simulated create failure", stack.writes().getFirst().reason());
+ assertEquals("The following resource(s) failed to create: [FailingResource]. Rollback requested by user.",
+ stack.writes().getFirst().reason());
}
/**
@@ -167,7 +168,100 @@ class CloudFormationServiceRollbackTest {
assertEquals(List.of("UPDATE_ROLLBACK_IN_PROGRESS", "UPDATE_ROLLBACK_COMPLETE"), stack.statuses());
assertEquals(List.of("UPDATE_ROLLBACK_IN_PROGRESS", "UPDATE_ROLLBACK_COMPLETE"), stackEventStatuses(stack));
stack.assertEachStatusFollowsItsEvent();
- assertEquals("already exists", stack.writes().getFirst().reason());
+ assertEquals("The following resource(s) failed to create: [Dup].", stack.writes().getFirst().reason());
+ }
+
+ /**
+ * The stack's rollback event names the resource that failed, as AWS does; the resource's own
+ * message stays on the resource's event. An update rollback carries no "Rollback requested by
+ * user." suffix.
+ */
+ @Test
+ void updateRollback_reasonSummarizesTheResourceThatFailedToUpdate() {
+ Stack stack = new Stack();
+ stack.setStackName("update-rollback-reason");
+ stack.setStackId("stack-id");
+ stack.setRegion(REGION);
+ stack.setStatus("UPDATE_IN_PROGRESS");
+ StackResource failed = resource("MyDB", "db-1", "AWS::RDS::DBInstance", "UPDATE_FAILED");
+ failed.setStatusReason("Size cannot be less than current size; requested: 5; current: 10");
+ stack.getResources().put(failed.getLogicalId(), failed);
+ StackUpdateSnapshot previousState = new StackUpdateSnapshot(
+ "previous-template", "previous-original", Map.of(), Map.of(), Map.of(), Map.of(), Map.of(),
+ Map.of());
+
+ service.rollbackFailedExecution(stack, REGION, false, failed, previousState,
+ new LinkedHashSet<>(List.of("MyDB")));
+
+ StackEvent rollbackEvent = stack.getEvents().stream()
+ .filter(event -> "UPDATE_ROLLBACK_IN_PROGRESS".equals(event.getResourceStatus()))
+ .findFirst()
+ .orElseThrow();
+ assertEquals("The following resource(s) failed to update: [MyDB].", rollbackEvent.getResourceStatusReason());
+ assertEquals("Size cannot be less than current size; requested: 5; current: 10", failed.getStatusReason());
+ }
+
+ /**
+ * The stack's own reason is the summary; the cause a stack set instance reports is the failed
+ * resource's message, from the operation that rolled back.
+ */
+ @Test
+ void failureCause_afterACreateRollback_isTheFailedResourcesMessage() {
+ Stack stack = new Stack();
+ stack.setStackName("failure-cause");
+ stack.setStackId("stack-id");
+ stack.setRegion(REGION);
+ stack.getEvents().add(stackEvent(stack, "CREATE_IN_PROGRESS"));
+ stack.getEvents().add(resourceEvent(stack, "FailingResource", "CREATE_FAILED", "simulated create failure"));
+ StackResource failed = resource("FailingResource", null, "AWS::Test::Failure", "CREATE_FAILED");
+ failed.setStatusReason("simulated create failure");
+ stack.getResources().put(failed.getLogicalId(), failed);
+
+ service.rollbackFailedExecution(stack, REGION, true, failed, null, Set.of());
+
+ assertEquals("ROLLBACK_COMPLETE", stack.getStatus());
+ assertEquals("simulated create failure", CloudFormationService.failureCause(stack));
+ }
+
+ /** A resource may share its stack's name; only the stack's ID tells the stack's own events apart. */
+ @Test
+ void failureCause_withAResourceNamedLikeItsStack_isThatResourcesMessage() {
+ Stack stack = new Stack();
+ stack.setStackName("Queue");
+ stack.setStackId("stack-id");
+ stack.setRegion(REGION);
+ stack.getEvents().add(stackEvent(stack, "CREATE_IN_PROGRESS"));
+ stack.getEvents().add(resourceEvent(stack, "Queue", "CREATE_FAILED", "simulated create failure"));
+ StackResource failed = resource("Queue", null, "AWS::SQS::Queue", "CREATE_FAILED");
+ failed.setStatusReason("simulated create failure");
+ stack.getResources().put(failed.getLogicalId(), failed);
+
+ service.rollbackFailedExecution(stack, REGION, true, failed, null, Set.of());
+
+ assertEquals("The following resource(s) failed to create: [Queue]. Rollback requested by user.",
+ stack.getStatusReason());
+ assertEquals("simulated create failure", CloudFormationService.failureCause(stack));
+ }
+
+ /** A reason that is not the rollback summary, such as a failed rollback's, is the cause as is. */
+ @Test
+ void failureCause_afterAFailedRollback_isTheStacksReason() {
+ Stack stack = new Stack();
+ stack.setStackName("failure-cause-rollback-failed");
+ stack.setStackId("stack-id");
+ stack.setRegion(REGION);
+ StackResource created = resource("Queue", "queue-url", "AWS::SQS::Queue", "CREATE_COMPLETE");
+ StackResource failed = resource("FailingResource", null, "AWS::Test::Failure", "CREATE_FAILED");
+ failed.setStatusReason("simulated create failure");
+ stack.getResources().put(created.getLogicalId(), created);
+ stack.getResources().put(failed.getLogicalId(), failed);
+ stack.getEvents().add(resourceEvent(stack, "FailingResource", "CREATE_FAILED", "simulated create failure"));
+ doThrow(new IllegalStateException("cannot delete")).when(provisioner).delete(eq(created), eq(REGION));
+
+ service.rollbackFailedExecution(stack, REGION, true, failed, null, Set.of());
+
+ assertEquals("ROLLBACK_FAILED", stack.getStatus());
+ assertEquals("The following resource(s) failed to roll back: [Queue].", CloudFormationService.failureCause(stack));
}
@Test
@@ -875,6 +969,22 @@ class CloudFormationServiceRollbackTest {
}
}
+ private static StackEvent stackEvent(Stack stack, String status) {
+ StackEvent event = resourceEvent(stack, stack.getStackName(), status, null);
+ event.setPhysicalResourceId(stack.getStackId());
+ event.setResourceType("AWS::CloudFormation::Stack");
+ return event;
+ }
+
+ private static StackEvent resourceEvent(Stack stack, String logicalId, String status, String reason) {
+ StackEvent event = new StackEvent();
+ event.setStackName(stack.getStackName());
+ event.setLogicalResourceId(logicalId);
+ event.setResourceStatus(status);
+ event.setResourceStatusReason(reason);
+ return event;
+ }
+
private static StackResource resource(String logicalId, String physicalId, String resourceType, String status) {
StackResource resource = new StackResource();
resource.setLogicalId(logicalId);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackSetsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackSetsIntegrationTest.java
index ac0fc7d24c..3ede15277c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackSetsIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackSetsIntegrationTest.java
@@ -675,7 +675,8 @@ class CloudFormationStackSetsIntegrationTest {
.header("Authorization", auth(ADMIN, "cloudformation"))
.when().post("/")
.then().statusCode(200)
- .body(containsString("<DetailedStatus>FAILED</DetailedStatus>"));
+ .body(containsString("<DetailedStatus>FAILED</DetailedStatus>"))
+ .body(containsString("<StatusReason>Missing TemplateURL</StatusReason>"));
// ...so the operation reports FAILED rather than a false SUCCEEDED.
given()
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationUnsupportedResourceTypeStrictIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationUnsupportedResourceTypeStrictIntegrationTest.java
index c931440c90..62ae936ff6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationUnsupportedResourceTypeStrictIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationUnsupportedResourceTypeStrictIntegrationTest.java
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
import static io.github.hectorvent.floci.services.cloudformation.CloudFormationLambdaMissingS3CodeIntegrationTest.CFN_AUTH;
import static io.github.hectorvent.floci.services.cloudformation.CloudFormationLambdaMissingS3CodeIntegrationTest.createStack;
import static io.restassured.RestAssured.given;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
/**
@@ -52,6 +53,8 @@ class CloudFormationUnsupportedResourceTypeStrictIntegrationTest {
.then()
.statusCode(200)
.body(containsString("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"))
- .body(containsString("Resource type AWS::Fake::Thing is not supported by Floci."));
+ .body(containsString("The following resource(s) failed to create: [MyThing]. Rollback requested by user."));
+ assertThat(CfnStackWaits.failedResourceReason(stackName, CFN_AUTH),
+ containsString("Resource type AWS::Fake::Thing is not supported by Floci."));
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java
index c55bcc4060..8f918a5685 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java
@@ -96,7 +96,8 @@ class CloudFormationVpcEndpointIntegrationTest {
if (type.isEmpty()) {
CfnStackWaits.StackState failed = CfnStackWaits.awaitTerminal(stack, CFN_AUTH);
assertEquals("ROLLBACK_COMPLETE", failed.status(), failed.reason());
- assertTrue(failed.reason().contains("IpAddressType"), failed.reason());
+ String failure = CfnStackWaits.failedResourceReason(stack, CFN_AUTH);
+ assertTrue(failure.contains("IpAddressType"), failure);
break;
}
assertEquals("CreateStack".equals(action) ? "CREATE_COMPLETE" : "UPDATE_COMPLETE",