mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(rds): restore a postgres snapshot on an image that creates a template database at startup (#7394)
* fix(rds): restore a postgres snapshot on an image that creates a template database at startup * test(rds): clean up the template restore test's instances and snapshot even when it fails * test(rds): build the template restore test's requests with RdsRequests
This commit is contained in:
1 parent
b9a22d55c8
commit
e02b91714d
3 files changed
+114
-6
No files matched your search
+5
-2
@@ -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
|
||||
|
||||
|
||||
+105
@@ -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<String, String> 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;
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -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_%'"),
|
||||
|
||||
Reference in new issue
Block a user