Skip to content

armgit / floci

Public Imported from GitHub
0

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

test(cloudformation): pin the nested-stack rollback that caused #3854 (#3919)

Matheus Oliveiraee4acfdf8189
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java
index 97527999b9..2dc4b71427 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java
@@ -14,7 +14,15 @@ public class Stack {
/** AWS account that owns this stack; absent only on legacy records. */
private String accountId;
private String region;
- private String status = "CREATE_IN_PROGRESS";
+ /**
+ * ExecuteChangeSet hands deployment to a background executor and returns without waiting for
+ * it, so a caller (DescribeStacks) observes this field from a different thread than the one
+ * that sets it. It is always the last field a deploy writes, after resources, exports and
+ * outputs; {@code volatile} gives that ordering a happens-before edge, so a reader that
+ * observes a terminal status is guaranteed to also see every write that preceded it, such as a
+ * nested stack's resolved {@code Outputs.*} attributes.
+ */
+ private volatile String status = "CREATE_IN_PROGRESS";
private String statusReason;
private Instant creationTime = Instant.now();
private Instant lastUpdatedTime;
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
index 7c197b74bd..c3a4615043 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
@@ -200,7 +200,7 @@ class CloudFormationIntegrationTest {
.then()
.statusCode(200)
.body(containsString("cf-test-queue"));
-
+
// 4. Describe Stacks
given()
.contentType("application/x-www-form-urlencoded")
@@ -6708,6 +6708,97 @@ class CloudFormationIntegrationTest {
.body(containsString("nested-stack-child-queue"));
}
+ /**
+ * Pins the actual root cause behind issue #3854 (a parent stack output resolving to the raw
+ * {@code LogicalId.Outputs.Key} literal instead of a nested stack's value), which turned out
+ * to have nothing to do with cross-thread visibility.
+ *
+ * <p>{@code executeNestedStack} copies {@code childStack.getOutputs()} into the parent
+ * resource's {@code Outputs.*} attributes right after the child's (synchronous, same-thread)
+ * {@code executeTemplate} call returns. When the child's own resource loop fails, {@code
+ * executeTemplate} never reaches its Outputs block at all: {@code rollbackFailedExecution}
+ * rewrites the child's status straight from {@code CREATE_FAILED} into {@code
+ * ROLLBACK_COMPLETE} before returning. {@code executeNestedStack} used to detect a failed
+ * child only by checking for the literal strings {@code CREATE_FAILED}/{@code UPDATE_FAILED},
+ * which a rolled-back create can never match, so the parent kept going and reported
+ * {@code CREATE_COMPLETE} with its {@code Fn::GetAtt} on the child's outputs left unresolved:
+ * exactly the symptom in #3854. That matching bug was already fixed by allow-listing the
+ * success statuses instead (commit 700d403, PR #3609) before #3854 was even filed; this test
+ * only adds the missing regression coverage tying it to this issue.
+ */
+ @Test
+ void createStack_failingNestedStackResource_rollsBackParentInsteadOfReportingUnresolvedOutput() {
+ String childTemplate = """
+ {
+ "Resources": {
+ "ConflictingSecret": {
+ "Type": "AWS::SecretsManager::Secret",
+ "Properties": {
+ "Name": "cfn-3854-nested-conflict-secret",
+ "SecretString": "explicit",
+ "GenerateSecretString": { "PasswordLength": 32 }
+ }
+ }
+ },
+ "Outputs": {
+ "SecretArn": { "Value": { "Ref": "ConflictingSecret" } }
+ }
+ }
+ """;
+
+ given().when().put("/issue-3854-templates").then();
+ given().contentType("application/json").body(childTemplate)
+ .when().put("/issue-3854-templates/failing-child.json")
+ .then().statusCode(200);
+
+ String parentTemplate = """
+ {
+ "Resources": {
+ "FailingNestedStack": {
+ "Type": "AWS::CloudFormation::Stack",
+ "Properties": {
+ "TemplateURL": "http://localhost/issue-3854-templates/failing-child.json"
+ }
+ }
+ },
+ "Outputs": {
+ "childSecretArn": {
+ "Value": { "Fn::GetAtt": ["FailingNestedStack", "Outputs.SecretArn"] }
+ }
+ }
+ }
+ """;
+
+ String stackName = "issue-3854-failing-nested-parent";
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", parentTemplate)
+ .when().post("/").then().statusCode(200);
+
+ String xml = null;
+ long deadline = System.currentTimeMillis() + 20_000;
+ while (System.currentTimeMillis() < deadline) {
+ xml = given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when().post("/")
+ .then().statusCode(200)
+ .extract().asString();
+ if (xml.contains("<StackStatus>ROLLBACK_COMPLETE</StackStatus>")
+ || xml.contains("<StackStatus>CREATE_COMPLETE</StackStatus>")) {
+ break;
+ }
+ }
+
+ assertThat(xml, containsString("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"));
+ assertThat(xml, not(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>")));
+ assertThat(xml, not(containsString("FailingNestedStack.Outputs")));
+ }
+
// ── Issue #1072: AWS::ApiGatewayV2::Api WEBSOCKET drops RouteSelectionExpression ───
@Test
@@ -7100,7 +7191,7 @@ class CloudFormationIntegrationTest {
.body("services[0].taskDefinition",
equalTo("arn:aws:ecs:us-east-1:000000000000:task-definition/cfn-ecs-update-taskdef:2"));
}
-
+
@Test
void deleteStack_ec2SecurityGroup_leavesNoOrphans() {
String stackName = "sg-delete-cleanup-stack";