mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): replace a log group through the engine and delete the old one only after the update commits (#8463)
* fix(cloudformation): replace a log group through the engine and delete the old one only after the update commits * refactor: use the derived record form * test(cloudformation): pin a renamed log group surviving a rolled-back update * test(cloudformation): bring the log group unit tests to engine-decided replacement * fix: drop the retired name-mode attribute on update
This commit is contained in:
1 parent
c1a8a3b3ac
commit
d7c42e2065
4 files changed
+146
-129
No files matched your search
-20
@@ -12,8 +12,6 @@ import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
@@ -49,24 +47,6 @@ class CloudFormationLogGroupProvisionerTest {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyExplicitNameWithNoRecordedMode_replacesWhenNameIsRemoved() {
|
||||
// Simulates a log group created before FlociLogGroupNameMode was tracked: no attributes at
|
||||
// all, and a physical id that does not match generatePhysicalName's shape, so it must have
|
||||
// been an explicit LogGroupName the caller chose.
|
||||
String legacyExplicitName = "my-hand-chosen-log-group";
|
||||
when(logsService.logGroupExists(legacyExplicitName, REGION)).thenReturn(true);
|
||||
|
||||
StackResource result = provision("{}", legacyExplicitName, Map.of());
|
||||
|
||||
assertEquals("CREATE_COMPLETE", result.getStatus());
|
||||
assertNotEquals(legacyExplicitName, result.getPhysicalId());
|
||||
assertTrue(result.getPhysicalId().startsWith(STACK_NAME + "-LogGroup-"),
|
||||
"expected a freshly generated name, got: " + result.getPhysicalId());
|
||||
verify(logsService).createLogGroup(eq(result.getPhysicalId()), isNull(), anyMap(), eq(REGION));
|
||||
verify(logsService).deleteLogGroup(legacyExplicitName, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyGeneratedNameWithNoRecordedMode_reconcilesInPlace() {
|
||||
// A physical id matching the exact <stackName>-<logicalId>-<12 hex chars> shape must have
|
||||
|
||||
+67
@@ -368,6 +368,73 @@ class CloudFormationLogsIntegrationTest {
|
||||
.body(containsString(oldName));
|
||||
}
|
||||
|
||||
@Test
|
||||
void renamedLogGroupSurvivesARolledBackUpdateAndTheReplacementIsRemoved() {
|
||||
// LogGroupName is "Update requires: Replacement": AWS creates the new group first and removes
|
||||
// the old one only in the cleanup phase after the whole update succeeds. When a later
|
||||
// resource fails, the rollback keeps the old group and deletes the new one.
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-logs-rename-rollback-stack-" + suffix;
|
||||
String oldName = "/cfn/test/rb-old-" + suffix;
|
||||
String newName = "/cfn/test/rb-new-" + suffix;
|
||||
String failingTemplate = """
|
||||
{"Resources":{
|
||||
"LogGroup":{"Type":"AWS::Logs::LogGroup","Properties":{"LogGroupName":"%s"}},
|
||||
"ZFail":{"Type":"AWS::CloudFormation::Stack","DependsOn":"LogGroup","Properties":{}}
|
||||
}}
|
||||
""".formatted(newName);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", logGroupTemplate(oldName, 7))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE", CFN_AUTH);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "UpdateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", failingTemplate)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
CfnStackWaits.awaitStatus(stackName, "UPDATE_ROLLBACK_COMPLETE", CFN_AUTH);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackResources")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<PhysicalResourceId>" + oldName + "</PhysicalResourceId>"))
|
||||
.body(not(containsString(newName)));
|
||||
|
||||
given()
|
||||
.config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig()
|
||||
.encodeContentTypeAs("application/x-amz-json-1.1", ContentType.TEXT)))
|
||||
.header("Authorization", LOGS_AUTH)
|
||||
.header("X-Amz-Target", "Logs_20140328.DescribeLogGroups")
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.body("{\"logGroupNamePrefix\":\"/cfn/test/rb-\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("\"logGroupName\":\"" + oldName + "\""))
|
||||
.body(not(containsString(newName)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateStackRemovingExplicitName_generatesFreshNameInsteadOfKeepingOld() {
|
||||
// Going from an explicit LogGroupName to none is a naming-mode change, which is itself a
|
||||
|
||||
+28
-27
@@ -66,13 +66,19 @@ class LogsCfnProvisionerTest {
|
||||
|
||||
/** Provisions as a create (no prior physical id) or an update (prior id and attributes). */
|
||||
private StackResource provision(String json, String priorPhysicalId, Map<String, String> attrs) {
|
||||
return provision(json, priorPhysicalId, attrs, null);
|
||||
}
|
||||
|
||||
private StackResource provision(String json, String priorPhysicalId, Map<String, String> attrs,
|
||||
String priorJson) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId("LogGroup");
|
||||
r.setResourceType("AWS::Logs::LogGroup");
|
||||
r.setPhysicalId(priorPhysicalId);
|
||||
r.setAttributes(new HashMap<>(attrs));
|
||||
provisioner.provision(r, props(json),
|
||||
new ProvisionContext(engine, REGION, ACCOUNT_ID, STACK, priorPhysicalId));
|
||||
new ProvisionContext(engine, REGION, ACCOUNT_ID, STACK, priorPhysicalId,
|
||||
event -> { }, priorJson == null ? null : props(priorJson)));
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -86,15 +92,13 @@ class LogsCfnProvisionerTest {
|
||||
assertEquals("/aws/app", r.getPhysicalId(), "Ref is the log group name");
|
||||
assertEquals("arn:aws:logs:us-east-1:000000000000:log-group:/aws/app:*",
|
||||
r.getAttributes().get("Arn"));
|
||||
assertEquals("explicit", r.getAttributes().get("FlociLogGroupNameMode"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnnamedGroupGetsAGeneratedNameRecordedAsGenerated() {
|
||||
void anUnnamedGroupGetsAGeneratedName() {
|
||||
StackResource r = provision("{}", null, Map.of());
|
||||
|
||||
assertTrue(r.getPhysicalId().startsWith("my-stack-LogGroup-"), r.getPhysicalId());
|
||||
assertEquals("generated", r.getAttributes().get("FlociLogGroupNameMode"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,55 +151,52 @@ class LogsCfnProvisionerTest {
|
||||
verify(logs, never()).putRetentionPolicy(anyString(), anyInt(), anyString());
|
||||
}
|
||||
|
||||
/** The old group is not deleted inline: the engine deletes it on commit, via the recorded cleanup. */
|
||||
@Test
|
||||
void arenameCreatesTheNewGroupBeforeDeletingTheOld() {
|
||||
void aRenameCreatesTheNewGroupAndLeavesTheOldForTheEngineToDelete() {
|
||||
when(logs.logGroupExists("old", REGION)).thenReturn(true);
|
||||
|
||||
StackResource r = provision("""
|
||||
{"LogGroupName": "new"}
|
||||
""", "old", Map.of("FlociLogGroupNameMode", "explicit"));
|
||||
""", "old", Map.of(), """
|
||||
{"LogGroupName": "old"}
|
||||
""");
|
||||
|
||||
assertEquals("new", r.getPhysicalId());
|
||||
verify(logs).createLogGroup(eq("new"), isNull(), anyMap(), eq(REGION));
|
||||
verify(logs).deleteLogGroup("old", REGION);
|
||||
verify(logs, never()).deleteLogGroup("old", REGION);
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals("old", provisioner.updateCleanupPhysicalId(r));
|
||||
}
|
||||
|
||||
/**
|
||||
* If creating the replacement fails, the original must survive and the resource must say so, or
|
||||
* rollback would try to restore a group that was never deleted.
|
||||
*/
|
||||
/** If creating the replacement fails, the original is untouched and the failure propagates. */
|
||||
@Test
|
||||
void aFailedRenameLeavesTheOriginalAndFlagsTheRollback() {
|
||||
void aFailedRenameLeavesTheOriginalAndThrows() {
|
||||
when(logs.logGroupExists("old", REGION)).thenReturn(true);
|
||||
doThrow(new RuntimeException("name taken"))
|
||||
.when(logs).createLogGroup(eq("new"), any(), anyMap(), anyString());
|
||||
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId("LogGroup");
|
||||
r.setResourceType("AWS::Logs::LogGroup");
|
||||
r.setPhysicalId("old");
|
||||
r.setAttributes(new HashMap<>(Map.of("FlociLogGroupNameMode", "explicit")));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> provisioner.provision(r, props("""
|
||||
assertThrows(RuntimeException.class, () -> provision("""
|
||||
{"LogGroupName": "new"}
|
||||
"""), new ProvisionContext(engine, REGION, ACCOUNT_ID, STACK, "old")));
|
||||
""", "old", Map.of(), """
|
||||
{"LogGroupName": "old"}
|
||||
"""));
|
||||
|
||||
verify(logs, never()).deleteLogGroup("old", REGION);
|
||||
assertEquals("true", r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dropping an explicit LogGroupName is a replacement on real AWS, so it must not keep
|
||||
* reconciling under the old explicit name.
|
||||
*/
|
||||
/** Dropping an explicit LogGroupName is a replacement on real AWS. */
|
||||
@Test
|
||||
void removingAnExplicitNameReplacesTheGroup() {
|
||||
when(logs.logGroupExists("chosen", REGION)).thenReturn(true);
|
||||
|
||||
StackResource r = provision("{}", "chosen", Map.of("FlociLogGroupNameMode", "explicit"));
|
||||
StackResource r = provision("{}", "chosen", Map.of(), """
|
||||
{"LogGroupName": "chosen"}
|
||||
""");
|
||||
|
||||
assertTrue(r.getPhysicalId().startsWith("my-stack-LogGroup-"), r.getPhysicalId());
|
||||
verify(logs).deleteLogGroup("chosen", REGION);
|
||||
verify(logs, never()).deleteLogGroup("chosen", REGION);
|
||||
assertEquals("chosen", provisioner.updateCleanupPhysicalId(r));
|
||||
}
|
||||
|
||||
/** A generated name is kept across updates so a no-op update does not churn the group. */
|
||||
|
||||
Reference in new issue
Block a user