mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): persist stacks across restart (#1124)
Stacks lived only in memory, so after a Floci restart CloudFormation forgot every stack and cdk deploy/destroy could no longer find them, orphaning the underlying resources. Back the stacks and exports registries with StorageFactory (cloudformation-stacks.json / cloudformation-exports.json): the in-memory maps stay the live working copy with write-through at each lifecycle checkpoint and a load on startup. Everything is stored under one fixed account namespace (CloudFormation is account-blind), which stays consistent across request and background executor threads. Stack/StackResource/ChangeSet/StackEvent are now reflection- and unknown-field-tolerant for storage. Follows the configured global storage mode. Tests cover a full stack-graph round-trip across storage instances and an end-to-end persistent-mode write to disk. Refs #924
This commit is contained in:
1 parent
f450ede341
commit
73a4b69ccf
7 files changed
+307
-3
No files matched your search
+98
@@ -0,0 +1,98 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.Stack;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* End-to-end check that a created stack is written to persistent storage so it survives a restart
|
||||
* (criteria #10, #11). Runs with the {@code persistent} storage mode; verifies the on-disk file
|
||||
* directly with a fresh {@link PersistentStorage} (what the service would read on the next boot).
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(CloudFormationPersistenceIntegrationTest.PersistentStorageProfile.class)
|
||||
class CloudFormationPersistenceIntegrationTest {
|
||||
|
||||
static final String STORAGE_DIR = "target/cfn-persistence-it";
|
||||
private static final Path STACKS_FILE = Path.of(STORAGE_DIR, "cloudformation-stacks.json");
|
||||
|
||||
@BeforeAll
|
||||
static void setup() throws Exception {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
Files.deleteIfExists(STACKS_FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createdStackIsPersistedToDiskForRestart() {
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"PersistBucket": {
|
||||
"Type": "AWS::S3::Bucket",
|
||||
"Properties": { "BucketName": "cfn-persist-it-bucket" }
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", "persist-it-stack")
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", "persist-it-stack")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
|
||||
|
||||
// Read the on-disk file the way a fresh Floci process would on the next boot.
|
||||
var store = new PersistentStorage<String, Stack>(
|
||||
STACKS_FILE, new TypeReference<Map<String, Stack>>() {});
|
||||
store.load();
|
||||
|
||||
// Account-prefixed key: <defaultAccount>/<region>:<stackName>
|
||||
Optional<Stack> loaded = store.get("000000000000/us-east-1:persist-it-stack");
|
||||
assertTrue(loaded.isPresent(), "Stack must be written to persistent storage to survive a restart");
|
||||
assertEquals("CREATE_COMPLETE", loaded.get().getStatus());
|
||||
assertEquals("us-east-1", loaded.get().getRegion());
|
||||
assertTrue(loaded.get().getResources().containsKey("PersistBucket"),
|
||||
"Persisted stack must retain its resources");
|
||||
assertEquals("cfn-persist-it-bucket",
|
||||
loaded.get().getResources().get("PersistBucket").getPhysicalId());
|
||||
}
|
||||
|
||||
public static final class PersistentStorageProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of(
|
||||
"floci.storage.mode", "persistent",
|
||||
"floci.storage.persistent-path", STORAGE_DIR);
|
||||
}
|
||||
}
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.ChangeSet;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.Stack;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackEvent;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Verifies the CloudFormation stack model graph survives a restart by round-tripping it through a
|
||||
* fresh {@link PersistentStorage} instance (criteria #10, #11). Mirrors {@code RulePersistenceTest}.
|
||||
*/
|
||||
class CloudFormationStackPersistenceTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private static final String KEY = "000000000000/my-stack:us-east-1";
|
||||
|
||||
@Test
|
||||
void stackGraphPersistsAcrossInstances() {
|
||||
Path filePath = tempDir.resolve("cloudformation-stacks.json");
|
||||
|
||||
Stack stack = new Stack();
|
||||
stack.setStackId("arn:aws:cloudformation:us-east-1:000000000000:stack/my-stack/abc123");
|
||||
stack.setStackName("my-stack");
|
||||
stack.setRegion("us-east-1");
|
||||
stack.setStatus("CREATE_COMPLETE");
|
||||
stack.setTemplateBody("{\"Resources\":{}}");
|
||||
stack.getParameters().put("Env", "prod");
|
||||
stack.getOutputs().put("BucketArn", "arn:aws:s3:::my-bucket");
|
||||
stack.getExports().put("MyExport", "exported-value");
|
||||
stack.getTags().put("team", "platform");
|
||||
|
||||
StackResource resource = new StackResource();
|
||||
resource.setLogicalId("MyBucket");
|
||||
resource.setPhysicalId("my-bucket");
|
||||
resource.setResourceType("AWS::S3::Bucket");
|
||||
resource.setStatus("CREATE_COMPLETE");
|
||||
resource.getAttributes().put("Arn", "arn:aws:s3:::my-bucket");
|
||||
stack.getResources().put("MyBucket", resource);
|
||||
|
||||
StackEvent event = new StackEvent();
|
||||
event.setStackName("my-stack");
|
||||
event.setLogicalResourceId("MyBucket");
|
||||
event.setResourceStatus("CREATE_COMPLETE");
|
||||
stack.getEvents().add(event);
|
||||
|
||||
ChangeSet changeSet = new ChangeSet();
|
||||
changeSet.setChangeSetName("initial-create");
|
||||
changeSet.setStackName("my-stack");
|
||||
changeSet.setStatus("CREATE_COMPLETE");
|
||||
stack.getChangeSets().put("initial-create", changeSet);
|
||||
|
||||
var writer = new PersistentStorage<String, Stack>(
|
||||
filePath, new TypeReference<Map<String, Stack>>() {});
|
||||
writer.put(KEY, stack);
|
||||
|
||||
// Fresh instance + load() simulates a Floci restart reading the same file
|
||||
var reader = new PersistentStorage<String, Stack>(
|
||||
filePath, new TypeReference<Map<String, Stack>>() {});
|
||||
reader.load();
|
||||
|
||||
Optional<Stack> loadedOpt = reader.get(KEY);
|
||||
assertTrue(loadedOpt.isPresent(), "Stack should survive a fresh PersistentStorage + load()");
|
||||
Stack loaded = loadedOpt.get();
|
||||
|
||||
assertEquals("my-stack", loaded.getStackName());
|
||||
assertEquals("us-east-1", loaded.getRegion());
|
||||
assertEquals("CREATE_COMPLETE", loaded.getStatus());
|
||||
assertEquals("arn:aws:cloudformation:us-east-1:000000000000:stack/my-stack/abc123", loaded.getStackId());
|
||||
assertNotNull(loaded.getCreationTime(), "Instant fields must round-trip");
|
||||
assertEquals("prod", loaded.getParameters().get("Env"));
|
||||
assertEquals("arn:aws:s3:::my-bucket", loaded.getOutputs().get("BucketArn"));
|
||||
assertEquals("exported-value", loaded.getExports().get("MyExport"));
|
||||
assertEquals("platform", loaded.getTags().get("team"));
|
||||
|
||||
StackResource loadedResource = loaded.getResources().get("MyBucket");
|
||||
assertNotNull(loadedResource, "Nested stack resources must round-trip");
|
||||
assertEquals("my-bucket", loadedResource.getPhysicalId());
|
||||
assertEquals("AWS::S3::Bucket", loadedResource.getResourceType());
|
||||
assertEquals("CREATE_COMPLETE", loadedResource.getStatus());
|
||||
assertEquals("arn:aws:s3:::my-bucket", loadedResource.getAttributes().get("Arn"));
|
||||
|
||||
assertEquals(1, loaded.getEvents().size());
|
||||
assertEquals("CREATE_COMPLETE", loaded.getEvents().get(0).getResourceStatus());
|
||||
assertTrue(loaded.getChangeSets().containsKey("initial-create"));
|
||||
assertEquals("CREATE_COMPLETE", loaded.getChangeSets().get("initial-create").getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresUnknownPropertiesOnLoad() throws Exception {
|
||||
// A persisted file may contain fields a newer/older Floci version does not recognize.
|
||||
// Loading must not fail (forward/backward compatibility).
|
||||
Path filePath = tempDir.resolve("cloudformation-stacks.json");
|
||||
String legacyJson = """
|
||||
{
|
||||
"%s" : {
|
||||
"stackName" : "legacy-stack",
|
||||
"region" : "us-east-1",
|
||||
"status" : "CREATE_COMPLETE",
|
||||
"fieldFromFutureVersion" : "whatever"
|
||||
}
|
||||
}
|
||||
""".formatted(KEY);
|
||||
Files.writeString(filePath, legacyJson);
|
||||
|
||||
var store = new PersistentStorage<String, Stack>(
|
||||
filePath, new TypeReference<Map<String, Stack>>() {});
|
||||
store.load();
|
||||
|
||||
Optional<Stack> loaded = store.get(KEY);
|
||||
assertTrue(loaded.isPresent(), "Stacks with unknown extra fields should still load");
|
||||
assertEquals("legacy-stack", loaded.get().getStackName());
|
||||
assertEquals("CREATE_COMPLETE", loaded.get().getStatus());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user