Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(cloudformation): default CreateChangeSet type to UPDATE (#5287)

Matheus Oliveiraf6e76bbe300d
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 3e40e4e0db..b862fa7a41 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
@@ -391,7 +391,7 @@ public class CloudFormationService implements ResourceProvider {
// was already released, losing an accepted change set or corrupting the map's links. Only
// persistStack() stays outside: it is storage I/O, and compute()'s contract is that the
// remapping function does short, non-blocking work.
- boolean isCreateType = changeSetType == null || "CREATE".equalsIgnoreCase(changeSetType);
+ boolean isCreateType = "CREATE".equalsIgnoreCase(changeSetType);
// An update names its stack by name or stack ID; the map is keyed by name. One that names no
// live stack is keyed as given, so it lands in the missing-stack branch below.
Stack live = isCreateType ? null : resolveStack(stackName, region, accountId);
@@ -409,7 +409,6 @@ public class CloudFormationService implements ResourceProvider {
// matching stack-level event (as AWS and LocalStack do) so DescribeStackEvents is
// non-empty straight after change-set creation — tooling such as the AWS SAM CLI
// reads StackEvents[0] there and otherwise fails with an IndexError.
- // (CreateChangeSet defaults a null type to CREATE.)
if (isCreateType) {
addEvent(target, target.getStackName(), target.getStackId(),
"AWS::CloudFormation::Stack", "REVIEW_IN_PROGRESS", "User Initiated");
@@ -445,7 +444,7 @@ public class CloudFormationService implements ResourceProvider {
cs.setChangeSetName(changeSetName);
cs.setStackName(canonicalStackName);
cs.setStackId(target.getStackId());
- cs.setChangeSetType(changeSetType != null ? changeSetType : "CREATE");
+ cs.setChangeSetType(changeSetType != null ? changeSetType : "UPDATE");
cs.setTemplateBody(resolvedTemplate);
cs.setParameters(parameters);
cs.setCapabilities(capabilities);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackIdUpdateIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackIdUpdateIntegrationTest.java
index f27628bc47..ffbb61fc04 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackIdUpdateIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationStackIdUpdateIntegrationTest.java
@@ -1,9 +1,11 @@
package io.github.hectorvent.floci.services.cloudformation;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import io.restassured.specification.RequestSpecification;
+import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -20,7 +22,8 @@ import static org.hamcrest.Matchers.not;
/**
* UpdateStack and CreateChangeSet take "the name or the unique stack ID" as StackName. An UPDATE
* change set given the stack ID has to land on the stack that ID names, not on a new key spelled
- * like the ARN (floci-io/floci#4842).
+ * like the ARN (floci-io/floci#4842). A CreateChangeSet without a ChangeSetType is an UPDATE, as it
+ * is on AWS.
*/
@QuarkusTest
class CloudFormationStackIdUpdateIntegrationTest {
@@ -44,6 +47,12 @@ class CloudFormationStackIdUpdateIntegrationTest {
}
""";
+ @Inject
+ CloudFormationService cfnService;
+
+ @Inject
+ EmulatorConfig config;
+
private final Set<String> stacksToDelete = new LinkedHashSet<>();
@BeforeAll
@@ -205,6 +214,63 @@ class CloudFormationStackIdUpdateIntegrationTest {
.body(not(containsString("update-by-stale-stack-id")));
}
+ @Test
+ void changeSetWithoutATypeIsAnUpdateOfTheExistingStack() {
+ String stackName = "stack-id-untyped-" + Long.toString(System.nanoTime(), 36);
+ createStack(stackName);
+
+ withParameters(stackName, "second")
+ .formParam("Action", "CreateChangeSet")
+ .formParam("StackName", stackName)
+ .formParam("ChangeSetName", "untyped")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeChangeSet")
+ .formParam("StackName", stackName)
+ .formParam("ChangeSetName", "untyped")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<Action>Modify</Action>"));
+ assertThat(cfnService.describeChangeSet(stackName, "untyped", config.defaultRegion()).getChangeSetType(),
+ equalTo("UPDATE"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "ExecuteChangeSet")
+ .formParam("StackName", stackName)
+ .formParam("ChangeSetName", "untyped")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ assertThat(CfnStackWaits.awaitTerminal(stackName).status(), equalTo("UPDATE_COMPLETE"));
+ assertThat(parameterValue(stackName), equalTo("second"));
+ }
+
+ @Test
+ void changeSetWithoutATypeOnAMissingStackIsAValidationError() {
+ String stackName = "stack-id-untyped-missing-" + Long.toString(System.nanoTime(), 36);
+
+ withParameters(stackName, "first")
+ .formParam("Action", "CreateChangeSet")
+ .formParam("StackName", stackName)
+ .formParam("ChangeSetName", "untyped")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("<Code>ValidationError</Code>"))
+ .body(containsString("Stack with id " + stackName + " does not exist"));
+ }
+
private String createStack(String stackName) {
stacksToDelete.add(stackName);
String stackId = withParameters(stackName, "first")