Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(cloudformation): report CREATE events for a resource an update adds (#4932)

Mario Toffia8c61aa441d0d
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLogsMetricFilterTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLogsMetricFilterTest.java
index 9b35d742e2..9e0b3e9550 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLogsMetricFilterTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLogsMetricFilterTest.java
@@ -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);
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 97d699b186..36565a81ed 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
@@ -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())) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnUnchangedResourceUpdateIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnUnchangedResourceUpdateIntegrationTest.java
index 5c7ffd6ff7..d1aa9c0acf 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnUnchangedResourceUpdateIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnUnchangedResourceUpdateIntegrationTest.java
@@ -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);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogsMetricFilterIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogsMetricFilterIntegrationTest.java
index c9fe534209..819a1a6b73 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogsMetricFilterIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLogsMetricFilterIntegrationTest.java
@@ -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;
}
}