mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): report CREATE events for a resource an update adds (#4932)
A logical id the stack did not hold before an UpdateStack is created, so AWS reports CREATE_IN_PROGRESS and CREATE_COMPLETE (or CREATE_FAILED) for it and DescribeStackResource shows CREATE_COMPLETE. Floci used the stack-wide update flag and reported UPDATE_* instead. The resource loop now decides per resource from whether the stack held it; stack-level statuses and the rollback of an added resource are unchanged.
This commit is contained in:
1 parent
e28c58cc8a
commit
8c61aa441d
4 files changed
+151
-4
No files matched your search
+1
-1
@@ -141,7 +141,7 @@ class CloudFormationLogsMetricFilterTest {
|
||||
assertThat(filters()).hasSize(1);
|
||||
List<StackEvent> events = newEvents();
|
||||
int changed = indexOf(events, "Filter", "UPDATE_COMPLETE", target);
|
||||
int failed = indexOf(events, "BadSecret", "UPDATE_FAILED", null);
|
||||
int failed = indexOf(events, "BadSecret", "CREATE_FAILED", null);
|
||||
assertThat(changed).as("filter update completed before dependent failure").isGreaterThanOrEqualTo(0);
|
||||
assertThat(failed).isGreaterThan(changed);
|
||||
assertSample("WARN", 2, null);
|
||||
|
||||
+5
-2
@@ -1526,7 +1526,10 @@ public class CloudFormationService implements ResourceProvider {
|
||||
stack.getResources().put(logicalId, resource);
|
||||
}
|
||||
|
||||
String inProgressStatus = isCreate
|
||||
// A logical id the stack did not hold is created, even on an update, as AWS
|
||||
// reports it; that includes a resource whose condition just turned true.
|
||||
boolean creating = isCreate || previousResource == null;
|
||||
String inProgressStatus = creating
|
||||
? "CREATE_IN_PROGRESS"
|
||||
: "UPDATE_IN_PROGRESS";
|
||||
addEvent(
|
||||
@@ -1550,7 +1553,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
resource.setUpdateReplacePolicy(
|
||||
resDef.path("UpdateReplacePolicy").asText(null));
|
||||
if (!isCreate) {
|
||||
if (!creating) {
|
||||
if ("CREATE_COMPLETE".equals(resource.getStatus())) {
|
||||
resource.setStatus("UPDATE_COMPLETE");
|
||||
} else if ("CREATE_FAILED".equals(resource.getStatus())) {
|
||||
|
||||
+144
@@ -4,6 +4,8 @@ import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,6 +30,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
*
|
||||
* <p>Regression cover for "CloudFormation update recreates unchanged named resources"
|
||||
* (floci-io/floci#2134).
|
||||
*
|
||||
* <p>The other side of the same loop is covered too: a resource the update adds is created, not
|
||||
* updated, so it reports CREATE_* events and status like AWS does.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CfnUnchangedResourceUpdateIntegrationTest {
|
||||
@@ -627,6 +632,145 @@ class CfnUnchangedResourceUpdateIntegrationTest {
|
||||
deleteStack(stackName);
|
||||
}
|
||||
|
||||
/**
|
||||
* A resource an update adds is created, not updated: AWS reports only CREATE_IN_PROGRESS and
|
||||
* CREATE_COMPLETE for it, and DescribeStackResource shows it CREATE_COMPLETE.
|
||||
*/
|
||||
@Test
|
||||
void anUpdateThatAddsAQueueReportsOnlyCreateEventsForIt() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "probe-added-queue-" + suffix;
|
||||
String added = """
|
||||
"Added": {
|
||||
"Type": "AWS::SQS::Queue",
|
||||
"Properties": {"QueueName": "probe-added-%s"}
|
||||
}""".formatted(suffix);
|
||||
|
||||
createStack(stackName, queueStackWith("probe-kept-" + suffix, null));
|
||||
updateStack(stackName, queueStackWith("probe-kept-" + suffix, added));
|
||||
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName, CFN_AUTH).status());
|
||||
|
||||
assertEquals(List.of("CREATE_IN_PROGRESS", "CREATE_COMPLETE"),
|
||||
resourceStatusesOldestFirst(stackName, "Added"));
|
||||
assertEquals("CREATE_COMPLETE", describedResourceStatus(stackName, "Added"));
|
||||
|
||||
deleteStack(stackName);
|
||||
}
|
||||
|
||||
/**
|
||||
* An added resource that fails reports CREATE_FAILED, as AWS does, and the update still rolls
|
||||
* back over the live stack. A nested stack with no TemplateURL is the resource that fails.
|
||||
*/
|
||||
@Test
|
||||
void anUpdateThatAddsAFailingResourceReportsCreateFailedAndRollsBack() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "probe-added-broken-" + suffix;
|
||||
String added = """
|
||||
"Broken": {"Type": "AWS::CloudFormation::Stack", "Properties": {}}""";
|
||||
|
||||
createStack(stackName, queueStackWith("probe-kept-" + suffix, null));
|
||||
updateStack(stackName, queueStackWith("probe-kept-" + suffix, added));
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName, CFN_AUTH);
|
||||
assertEquals("UPDATE_ROLLBACK_COMPLETE", state.status(), state.reason());
|
||||
|
||||
List<String> statuses = resourceStatusesOldestFirst(stackName, "Broken");
|
||||
assertEquals(List.of("CREATE_IN_PROGRESS", "CREATE_FAILED"), statuses.subList(0, 2), statuses.toString());
|
||||
assertTrue(statuses.stream().noneMatch(status -> status.startsWith("UPDATE_")), statuses.toString());
|
||||
|
||||
deleteStack(stackName);
|
||||
}
|
||||
|
||||
/** A nested stack an update adds is created too, so the parent reports CREATE_* for it. */
|
||||
@Test
|
||||
void anUpdateThatAddsANestedStackReportsCreateEventsForIt() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "probe-added-nested-" + suffix;
|
||||
String bucket = "probe-added-nested-" + suffix;
|
||||
String childTemplate = """
|
||||
{
|
||||
"Resources": {
|
||||
"ChildQueue": {
|
||||
"Type": "AWS::SQS::Queue",
|
||||
"Properties": {"QueueName": "probe-child-%s"}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(suffix);
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
given().contentType("application/json").body(childTemplate)
|
||||
.when().put("/" + bucket + "/child.json").then().statusCode(200);
|
||||
String added = """
|
||||
"Child": {
|
||||
"Type": "AWS::CloudFormation::Stack",
|
||||
"Properties": {"TemplateURL": "http://localhost/%s/child.json"}
|
||||
}""".formatted(bucket);
|
||||
|
||||
createStack(stackName, queueStackWith("probe-kept-" + suffix, null));
|
||||
updateStack(stackName, queueStackWith("probe-kept-" + suffix, added));
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName, CFN_AUTH);
|
||||
assertEquals("UPDATE_COMPLETE", state.status(), state.reason());
|
||||
|
||||
assertEquals(List.of("CREATE_IN_PROGRESS", "CREATE_COMPLETE"),
|
||||
resourceStatusesOldestFirst(stackName, "Child"));
|
||||
assertEquals("CREATE_COMPLETE", describedResourceStatus(stackName, "Child"));
|
||||
|
||||
deleteStack(stackName);
|
||||
}
|
||||
|
||||
/** A stack of one queue, plus the resource an update adds when {@code addedResourceJson} is set. */
|
||||
private static String queueStackWith(String queueName, String addedResourceJson) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Kept": {
|
||||
"Type": "AWS::SQS::Queue",
|
||||
"Properties": {"QueueName": "%s"}
|
||||
}%s
|
||||
}
|
||||
}
|
||||
""".formatted(queueName, addedResourceJson == null ? "" : ",\n" + addedResourceJson);
|
||||
}
|
||||
|
||||
private void createStack(String stackName, String templateBody) {
|
||||
given().contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", templateBody)
|
||||
.when().post("/").then().statusCode(200);
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName, CFN_AUTH).status());
|
||||
}
|
||||
|
||||
private void updateStack(String stackName, String templateBody) {
|
||||
given().contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", templateBody)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
/** DescribeStackEvents lists the newest event first; this returns one resource's statuses oldest first. */
|
||||
private List<String> resourceStatusesOldestFirst(String stackName, String logicalId) {
|
||||
String body = given()
|
||||
.contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents").formParam("StackName", stackName)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().body().asString();
|
||||
List<String> statuses = new ArrayList<>(XmlParser.extractGroups(body, "member").stream()
|
||||
.filter(member -> logicalId.equals(member.get("LogicalResourceId")))
|
||||
.map(member -> member.get("ResourceStatus"))
|
||||
.toList());
|
||||
Collections.reverse(statuses);
|
||||
return statuses;
|
||||
}
|
||||
|
||||
private String describedResourceStatus(String stackName, String logicalId) {
|
||||
String body = given()
|
||||
.contentType("application/x-www-form-urlencoded").header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackResource").formParam("StackName", stackName)
|
||||
.formParam("LogicalResourceId", logicalId)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().body().asString();
|
||||
return XmlParser.extractFirst(body, "ResourceStatus", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createChangeSet_updateRestApiAndUserPoolRename_reportsReplacementFalse() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
|
||||
+1
-1
@@ -606,7 +606,7 @@ class CloudFormationLogsMetricFilterIntegrationTest {
|
||||
&& "UPDATE_COMPLETE".equals(e.get("ResourceStatus")) && mutation == -1) {
|
||||
mutation = i;
|
||||
}
|
||||
if ("BadSecret".equals(e.get("LogicalResourceId")) && "UPDATE_FAILED".equals(e.get("ResourceStatus"))) {
|
||||
if ("BadSecret".equals(e.get("LogicalResourceId")) && "CREATE_FAILED".equals(e.get("ResourceStatus"))) {
|
||||
failure = i;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user