diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java index 3581c23f05..a67620e33b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java @@ -1293,11 +1293,14 @@ public class RdsContainerManager implements RdsBackendGate, RdsBackendEndpointRe set -e USER="$1" - # Terminate target connections (including pg_cron workers) when dropping databases + # Drop every database but postgres, template0 and template1, including template + # databases an image creates at startup (PostGIS's template_postgis), which the dump + # creates again. Terminate target connections (including pg_cron workers). psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -tAc \\ - "SELECT datname FROM pg_database WHERE datistemplate = false AND datname <> 'postgres'" \\ + "SELECT datname FROM pg_database WHERE datname NOT IN ('postgres', 'template0', 'template1')" \\ | while read -r db; do [ -z "$db" ] && continue + psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -c "ALTER DATABASE \\"$db\\" IS_TEMPLATE false" psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -c "DROP DATABASE IF EXISTS \\"$db\\" WITH (FORCE)" done diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsTemplateDatabaseRestoreIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsTemplateDatabaseRestoreIntegrationTest.java new file mode 100644 index 0000000000..3f18a93c1d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsTemplateDatabaseRestoreIntegrationTest.java @@ -0,0 +1,105 @@ +package io.github.hectorvent.floci.services.rds; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static io.github.hectorvent.floci.testing.RdsRequests.assumeDocker; +import static io.github.hectorvent.floci.testing.RdsRequests.deleteInstance; +import static io.github.hectorvent.floci.testing.RdsRequests.rds; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * A snapshot restores on an image that creates a template database at startup, such as PostGIS's + * {@code template_postgis}, which the snapshot's dump creates again. + */ +@QuarkusTest +@Tag("docker") +@TestProfile(RdsTemplateDatabaseRestoreIntegrationTest.PostgisImageProfile.class) +class RdsTemplateDatabaseRestoreIntegrationTest { + + public static class PostgisImageProfile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of("floci.services.rds.default-postgres-image", "postgis/postgis:17-3.5-alpine"); + } + } + + @BeforeEach + void checkDocker() throws InterruptedException { + assumeDocker(); + } + + @Test + void aSnapshotOfAnImageWithATemplateDatabaseRestores() throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String dbId = "postgis-src-" + suffix; + String snapshotId = "postgis-snap-" + suffix; + String restoreId = "postgis-restore-" + suffix; + + try { + rds("CreateDBInstance") + .formParam("DBInstanceIdentifier", dbId) + .formParam("Engine", "postgres") + .formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "secret123") + .formParam("AllocatedStorage", "5") + .formParam("DBInstanceClass", "db.t3.micro") + .when() + .post("/") + .then() + .statusCode(200); + assertEquals("available", waitForDb(dbId)); + + rds("CreateDBSnapshot") + .formParam("DBInstanceIdentifier", dbId) + .formParam("DBSnapshotIdentifier", snapshotId) + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString(snapshotId)); + + rds("RestoreDBInstanceFromDBSnapshot") + .formParam("DBInstanceIdentifier", restoreId) + .formParam("DBSnapshotIdentifier", snapshotId) + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString(restoreId)); + assertEquals("available", waitForDb(restoreId)); + } finally { + deleteInstance(restoreId); + deleteInstance(dbId); + rds("DeleteDBSnapshot") + .formParam("DBSnapshotIdentifier", snapshotId) + .when() + .post("/"); + } + } + + private String waitForDb(String dbId) throws Exception { + String status = null; + for (int i = 0; i < 120; i++) { + status = rds("DescribeDBInstances") + .formParam("DBInstanceIdentifier", dbId) + .when() + .post("/") + .then() + .extract().xmlPath().getString( + "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.DBInstanceStatus"); + if ("available".equals(status)) { + return status; + } + Thread.sleep(1000); + } + return status; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java index 97c09ee688..584ffbf88c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java @@ -1528,10 +1528,10 @@ class RdsContainerManagerTest { assertTrue(script.contains("WITH (FORCE)"), "Restore must terminate sessions such as pg_cron connected to the target database"); - assertTrue(script.contains("datistemplate = false"), - "Script must only drop non-template databases"); - assertTrue(script.contains("datname <> 'postgres'"), - "Script must preserve the postgres database"); + assertTrue(script.contains("datname NOT IN ('postgres', 'template0', 'template1')"), + "Script must preserve the postgres database and the system templates"); + assertTrue(script.contains("IS_TEMPLATE false"), + "Script must drop template databases an image created, which the dump creates again"); assertTrue(script.contains("rolname <> current_user"), "Script must not drop the current user"); assertTrue(script.contains("rolname NOT LIKE 'pg_%'"),