feat(rds): implement CreateDBSnapshot and RestoreDBInstanceFromDBSnap… (#1894)

* feat(rds): implement CreateDBSnapshot and RestoreDBInstanceFromDBSnapshot for postgres (#1817)

* feat(rds): implement CreateDBSnapshot and RestoreDBInstanceFromDBSnapshot for postgres (#1817)

* feat(rds): generated docs

* fix(rds): preserve master password across DbSnapshot restore

* fix(rds): preserve thread interrupt flag in restorePostgresSnapshot

* fix(rds): handle duplicate RDS snapshots and mock mode operations

* fix(rds): separate stdout and stderr in container exec to prevent snapshot corruption

* fix(rds): add missing stderr argument to ContainerExecResult on timeout

* fix(rds): cleanup RDS instance on snapshot restore failure

* fix(rds): return structured AWS error when RDS snapshot restore fails

* fix(rds): address PR #1894 snapshot and restore review comments
- Wire handleDescribeDbSnapshots to use the new snapshots storage backend
- Change engine validation error from UnsupportedOperation to InvalidDBInstanceState
- Change restore failure error from InvalidDBSnapshotStateFault to InvalidDBSnapshotState
- Add corresponding unit and query handler tests

* fix(rds): address PR #1894 review feedback and restore process issues

- Wire DescribeDBSnapshots to the snapshots storage backend with filtering support
- Align CreateDBSnapshot engine error code to InvalidDBInstanceState
- Align restore snapshot failure error code to InvalidDBSnapshotState
- Carry over dbName from DbSnapshot to the restored DBInstance
- Connect to template1 during psql restore to prevent active postgres connection drop failures
- Remove unused AwsNamespaces import in RdsService
- Add corresponding unit and query handler tests

* fix(rds): address PR #1894 review feedback and snapshot error handling

* fix(rds): add --if-exists to pg_dumpall snapshot command

- Appends the --if-exists flag to pg_dumpall commands during database snapshot creation. This ensures DROP DATABASE statements are emitted conditionally, preventing restore failures on fresh container instances where user databases do not yet exist.

* fix(rds): added real docker test

* docs(rds): add descriptions for snapshot actions
Added concise, handwritten descriptions for the `CreateDBSnapshot` and
`RestoreDBInstanceFromDBSnapshot` actions in the RDS documentation
action table to comply with the contributing guidelines.

* feat(rds): improve RDS snapshot restore reliability and API coverage

- Update `postgresRestoreScript` to explicitly filter out the master user `CREATE ROLE` statement, preventing restore failures when `ON_ERROR_STOP=1` is enabled.
- Add strict validation for `masterUsername` in `CreateDBInstance` to adhere to AWS requirements (only alphanumeric chars starting with a letter, max 16 chars).
- Ensure `dbInstanceClass` is accurately preserved in the `DbSnapshot` model so it can be restored to the same hardware class automatically.
- Fix missing Windows absolute path detection in `ContainerStorageHelper`, resolving test-environment `NoSuchElement` errors on Windows.
- Make `RdsAwsIntegrationTest` gracefully skip on agents without a Docker daemon, matching test suite expectations.
- Update `rds.md` to include concise handwritten descriptions for all remaining placeholder actions.
- Improve exception logging inside container execution wrappers.
This commit is contained in:
Prakhar Kapoor authored and GitHub committed 2026-09-07 18:27:54 -04:00
1 parent 1de6b80035
commit 2869502e7c
11 files changed
+1088 -26

No files matched your search

+8 -6
View File
@@ -32,16 +32,18 @@ RDS Data API (`rds-data`) is documented separately because it uses REST JSON rou
| `DeleteDBParameterGroup` | Delete a parameter group |
| `ModifyDBParameterGroup` | Update parameter group settings |
| `DescribeDBParameters` | List parameters in a group |
| `CreateDBClusterParameterGroup` | - |
| `DescribeDBClusterParameterGroups` | - |
| `DeleteDBClusterParameterGroup` | - |
| `ModifyDBClusterParameterGroup` | - |
| `DescribeDBClusterParameters` | - |
| `CreateDBClusterParameterGroup` | Create an Aurora-compatible cluster parameter group |
| `DescribeDBClusterParameterGroups` | List cluster parameter groups |
| `DeleteDBClusterParameterGroup` | Delete a cluster parameter group |
| `ModifyDBClusterParameterGroup` | Update cluster parameter group settings |
| `DescribeDBClusterParameters` | List parameters in a cluster group |
| `CreateOptionGroup` | Create an option group |
| `DescribeOptionGroups` | List option groups, including the implicit `default:` groups |
| `ModifyOptionGroup` | Add, update, or remove options in an option group |
| `DeleteOptionGroup` | Delete an option group |
| `DescribeDBSnapshots` | Return an empty snapshot list (snapshots are not modeled) |
| `CreateDBSnapshot` | Create a snapshot of a DB instance |
| `RestoreDBInstanceFromDBSnapshot` | Create a new DB instance from a snapshot |
| `DescribeDBSnapshots` | List DB instance snapshots |
| `DescribeDBProxies` | List DB proxies |
| `CreateDBProxy` | Create a DB proxy |
| `ModifyDBProxy` | Update mutable DB proxy authentication, logging, timeout, TLS, role, and security-group settings |
@@ -168,7 +168,11 @@ public final class ContainerStorageHelper {
* they are treated as named-volume mode.
*/
public static boolean isNamedVolumeMode(EmulatorConfig config) {
return !config.storage().hostPersistentPath().startsWith("/");
String path = config.storage().hostPersistentPath();
if (path == null || path.isBlank()) {
return true;
}
return !java.nio.file.Paths.get(path).isAbsolute() && !path.startsWith("/");
}
/**
@@ -92,6 +92,8 @@ public class RdsQueryHandler {
case "DescribeOptionGroups" -> handleDescribeOptionGroups(params, region);
case "ModifyOptionGroup" -> handleModifyOptionGroup(params, region);
case "DeleteOptionGroup" -> handleDeleteOptionGroup(params, region);
case "CreateDBSnapshot" -> handleCreateDbSnapshot(params);
case "RestoreDBInstanceFromDBSnapshot" -> handleRestoreDbInstanceFromDbSnapshot(params);
case "DescribeDBSnapshots" -> handleDescribeDbSnapshots(params);
case "DescribeDBProxies" -> handleDescribeDbProxies(params, region);
case "CreateDBProxy" -> handleCreateDbProxy(params, region);
@@ -937,12 +939,71 @@ public class RdsQueryHandler {
// ── Snapshots & Proxies (not modeled — empty lists) ───────────────────────
private Response handleCreateDbSnapshot(MultivaluedMap<String, String> params) {
String snapshotId = params.getFirst("DBSnapshotIdentifier");
String instanceId = params.getFirst("DBInstanceIdentifier");
if (snapshotId == null || snapshotId.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue", "DBSnapshotIdentifier is required.", AwsNamespaces.RDS, 400);
}
if (instanceId == null || instanceId.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue", "DBInstanceIdentifier is required.", AwsNamespaces.RDS, 400);
}
try {
io.github.hectorvent.floci.services.rds.model.DbSnapshot snapshot = service.createDbSnapshot(snapshotId, instanceId);
String result = dbSnapshotXml(snapshot);
return Response.ok(AwsQueryResponse.envelope("CreateDBSnapshot", AwsNamespaces.RDS, result)).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleRestoreDbInstanceFromDbSnapshot(MultivaluedMap<String, String> params) {
String instanceId = params.getFirst("DBInstanceIdentifier");
String snapshotId = params.getFirst("DBSnapshotIdentifier");
if (instanceId == null || instanceId.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue", "DBInstanceIdentifier is required.", AwsNamespaces.RDS, 400);
}
if (snapshotId == null || snapshotId.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue", "DBSnapshotIdentifier is required.", AwsNamespaces.RDS, 400);
}
String dbInstanceClass = params.getFirst("DBInstanceClass");
String availabilityZone = params.getFirst("AvailabilityZone");
String multiAzStr = params.getFirst("MultiAZ");
boolean multiAz = multiAzStr != null && Boolean.parseBoolean(multiAzStr);
String dbSubnetGroupName = params.getFirst("DBSubnetGroupName");
java.util.List<String> vpcSecurityGroupIds = new java.util.ArrayList<>();
for (int i = 1; ; i++) {
String sg = params.getFirst("VpcSecurityGroupIds.VpcSecurityGroupId." + i);
if (sg == null) break;
vpcSecurityGroupIds.add(sg);
}
java.util.Map<String, String> tags = parseTags(params);
try {
DbInstance instance = service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags);
String result = dbInstanceXml(instance);
return Response.ok(AwsQueryResponse.envelope("RestoreDBInstanceFromDBSnapshot", AwsNamespaces.RDS, result)).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleDescribeDbSnapshots(MultivaluedMap<String, String> params) {
// DB snapshots are not modeled; return the RDS Query API's wire-accurate empty
// result (empty <DBSnapshots> wrapper, no <Marker>) so SDK clients complete the
// read instead of failing with UnsupportedOperation.
String result = new XmlBuilder().start("DBSnapshots").end("DBSnapshots").build();
return Response.ok(AwsQueryResponse.envelope("DescribeDBSnapshots", AwsNamespaces.RDS, result)).build();
String snapshotId = params.getFirst("DBSnapshotIdentifier");
String instanceId = params.getFirst("DBInstanceIdentifier");
try {
Collection<io.github.hectorvent.floci.services.rds.model.DbSnapshot> result = service.describeDbSnapshots(snapshotId, instanceId);
XmlBuilder xml = new XmlBuilder().start("DBSnapshots");
for (io.github.hectorvent.floci.services.rds.model.DbSnapshot s : result) {
xml.raw(dbSnapshotXml(s));
}
xml.end("DBSnapshots");
return Response.ok(AwsQueryResponse.envelope("DescribeDBSnapshots", AwsNamespaces.RDS, xml.build())).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus());
}
}
private Response handleDescribeDbProxies(MultivaluedMap<String, String> params, String region) {
@@ -1282,6 +1343,26 @@ public class RdsQueryHandler {
return new XmlBuilder().start("DBInstance").raw(dbInstanceInnerXml(i)).end("DBInstance").build();
}
private String dbSnapshotXml(io.github.hectorvent.floci.services.rds.model.DbSnapshot s) {
String engineStr = s.getEngine() != null ? s.getEngine().name().toLowerCase() : "";
XmlBuilder xml = new XmlBuilder().start("DBSnapshot")
.elem("DBSnapshotIdentifier", s.getDbSnapshotIdentifier())
.elem("DBInstanceIdentifier", s.getDbInstanceIdentifier())
.elem("SnapshotCreateTime", s.getSnapshotCreateTime() != null ? s.getSnapshotCreateTime().toString() : "")
.elem("Engine", engineStr)
.elem("EngineVersion", s.getEngineVersion())
.elem("AllocatedStorage", s.getAllocatedStorage())
.elem("Status", s.getStatus())
.elem("MasterUsername", s.getMasterUsername());
if (s.getAvailabilityZone() != null) xml.elem("AvailabilityZone", s.getAvailabilityZone());
if (s.getVpcId() != null) xml.elem("VpcId", s.getVpcId());
xml.elem("InstanceCreateTime", s.getInstanceCreateTime() != null ? s.getInstanceCreateTime().toString() : "")
.elem("Port", s.getPort())
.elem("IAMDatabaseAuthenticationEnabled", s.isIamDatabaseAuthenticationEnabled());
if (s.getDbiResourceId() != null) xml.elem("DbiResourceId", s.getDbiResourceId());
return xml.end("DBSnapshot").build();
}
private String dbInstanceInnerXml(DbInstance i) {
DbEndpoint ep = i.getEndpoint();
String engineStr = instanceEngine(i);
@@ -35,6 +35,7 @@ import io.github.hectorvent.floci.services.rds.model.DbProxy;
import io.github.hectorvent.floci.services.rds.model.DbProxyAuth;
import io.github.hectorvent.floci.services.rds.model.DbProxyTarget;
import io.github.hectorvent.floci.services.rds.model.DbProxyTargetGroup;
import io.github.hectorvent.floci.services.rds.model.DbSnapshot;
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
import io.github.hectorvent.floci.services.rds.model.OptionGroup;
import io.github.hectorvent.floci.services.rds.model.OptionGroupOption;
@@ -137,6 +138,8 @@ public class RdsService implements Resettable, ResourceProvider {
private final StorageBackend<String, DbSubnetGroup> subnetGroups;
private final StorageBackend<String, DbProxy> proxies;
private final StorageBackend<String, DbProxyTargetGroup> proxyTargetGroups;
private final StorageBackend<String, DbSnapshot> snapshots;
private final StorageBackend<String, String> snapshotData;
private final RdsContainerManager containerManager;
private final RdsProxyManager proxyManager;
// CreateDBCluster/CreateDBInstance register the new resource only after the
@@ -214,6 +217,10 @@ public class RdsService implements Resettable, ResourceProvider {
new TypeReference<Map<String, DbProxy>>() {});
this.proxyTargetGroups = storageFactory.create("rds", "rds-proxy-target-groups.json",
new TypeReference<Map<String, DbProxyTargetGroup>>() {});
this.snapshots = storageFactory.create("rds", "rds-snapshots.json",
new TypeReference<Map<String, DbSnapshot>>() {});
this.snapshotData = storageFactory.create("rds", "rds-snapshot-data.json",
new TypeReference<Map<String, String>>() {});
}
RdsService(RdsContainerManager containerManager,
@@ -345,6 +352,8 @@ public class RdsService implements Resettable, ResourceProvider {
this.proxies = proxies;
this.proxyTargetGroups = proxyTargetGroups;
this.taggingService = taggingService;
this.snapshots = new io.github.hectorvent.floci.core.storage.InMemoryStorage<>();
this.snapshotData = new io.github.hectorvent.floci.core.storage.InMemoryStorage<>();
}
public void restorePersistedRuntime() {
@@ -572,6 +581,9 @@ public class RdsService implements Resettable, ResourceProvider {
int proxyPort = allocateProxyPort();
if (masterUsername == null || masterUsername.isBlank()) {
masterUsername = "root";
} else if (masterUsername.length() > 16 || !masterUsername.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) {
throw new AwsException("InvalidParameterValue",
"MasterUsername must begin with a letter and contain only alphanumeric characters or underscores.", 400);
}
if (manageMasterUserPassword && (masterPassword == null || masterPassword.isBlank())) {
masterPassword = generatedMasterPassword();
@@ -699,6 +711,96 @@ public class RdsService implements Resettable, ResourceProvider {
return instance;
}
public DbSnapshot createDbSnapshot(String snapshotId, String instanceId) {
if (snapshots.get(snapshotId).isPresent()) {
throw new AwsException("DBSnapshotAlreadyExists", "DBSnapshot " + snapshotId + " already exists.", 400);
}
DbInstance instance = getDbInstance(instanceId);
if (instance.getEngine() != DatabaseEngine.POSTGRES) {
throw new AwsException("InvalidDBInstanceState", "Operation CreateDBSnapshot is not supported for engine " + instance.getEngine() + ".", 400);
}
DbSnapshot snapshot = new DbSnapshot(snapshotId, instanceId, Instant.now(), instance.getEngine(),
instance.getEngineVersion(), instance.getAllocatedStorage(), "available",
instance.getMasterUsername(), instance.getMasterPassword(), instance.getAvailabilityZone(), instance.getVpcId(),
instance.getCreatedAt(), instance.getEndpoint() != null ? instance.getEndpoint().port() : instance.getProxyPort(),
instance.isIamDatabaseAuthenticationEnabled(), instance.getDbiResourceId(), instance.getDbInstanceClass());
snapshot.setDbName(instance.getDbName());
String sqlDump = "";
if (!config.services().rds().mock()) {
try {
sqlDump = containerManager.createPostgresSnapshot(instance.getContainerId(), instance.getMasterUsername());
} catch (Exception e) {
throw new AwsException("InvalidDBInstanceState", "Failed to create snapshot: " + e.getMessage(), 400);
}
}
snapshotData.put(snapshotId, sqlDump);
snapshots.put(snapshotId, snapshot);
return snapshot;
}
public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass, String availabilityZone, boolean multiAz, String dbSubnetGroupName, java.util.List<String> vpcSecurityGroupIds, java.util.Map<String, String> tags) {
DbSnapshot snapshot = snapshots.get(snapshotId)
.orElseThrow(() -> new AwsException("DBSnapshotNotFound", "DBSnapshot " + snapshotId + " not found.", 404));
String sqlDump = snapshotData.get(snapshotId)
.orElseThrow(() -> new AwsException("DBSnapshotNotFound", "DBSnapshot data for " + snapshotId + " not found.", 404));
String targetClass = (dbInstanceClass != null && !dbInstanceClass.isBlank()) ? dbInstanceClass : snapshot.getDbInstanceClass();
if (targetClass == null || targetClass.isBlank()) {
targetClass = "db.t3.micro";
}
// Use the parameters from the snapshot
DbInstance instance = createDbInstance(instanceId, snapshot.getEngine().name().toLowerCase(), snapshot.getEngineVersion(),
snapshot.getMasterUsername(), snapshot.getMasterPassword(),
snapshot.getDbName(), targetClass, snapshot.getAllocatedStorage(), snapshot.isIamDatabaseAuthenticationEnabled(),
null, dbSubnetGroupName, null, availabilityZone, multiAz, false, null, tags, vpcSecurityGroupIds);
if (!config.services().rds().mock()) {
try {
containerManager.restorePostgresSnapshot(instance.getContainerId(), instance.getMasterUsername(), sqlDump);
} catch (Exception e) {
try {
deleteDbInstance(instanceId);
} catch (Exception cleanupError) {
e.addSuppressed(cleanupError);
}
AwsException awsEx = new AwsException("InvalidDBSnapshotState", "Failed to restore snapshot: " + e.getMessage(), 400);
awsEx.initCause(e);
throw awsEx;
}
}
return instance;
}
public Collection<DbSnapshot> describeDbSnapshots(String snapshotId, String instanceId) {
if (snapshotId != null && !snapshotId.isBlank()) {
DbSnapshot snapshot = snapshots.get(snapshotId)
.orElseThrow(() -> new AwsException("DBSnapshotNotFound", "DBSnapshot " + snapshotId + " not found.", 404));
if (instanceId != null && !instanceId.isBlank()) {
if (instanceId.equals(snapshot.getDbInstanceIdentifier())) {
return List.of(snapshot);
} else {
return List.of();
}
}
return List.of(snapshot);
}
List<DbSnapshot> allSnapshots = snapshots.scan(k -> true);
if (instanceId != null && !instanceId.isBlank()) {
return allSnapshots.stream()
.filter(s -> instanceId.equals(s.getDbInstanceIdentifier()))
.toList();
}
return allSnapshots;
}
public Map<String, String> listTagsForResource(String resourceName) {
return listTagsForResource(resourceName, resourceRegionOrDefault(resourceName));
}
@@ -592,7 +592,7 @@ public class RdsContainerManager {
for (int attempt = 1; attempt <= 60; attempt++) {
try {
ContainerExecResult result = execInContainer(containerId, cmd, 5);
lastOutput = result.output();
lastOutput = result.output() + (result.stderr().isEmpty() ? "" : "\n" + result.stderr());
if (result.exitCode() == 0) {
LOG.infov("Initialized {0} in RDS container {1}", description, containerName);
return;
@@ -610,6 +610,120 @@ public class RdsContainerManager {
throw new IllegalStateException("Timed out initializing " + description + " in " + containerName + ": " + lastOutput);
}
public String createPostgresSnapshot(String containerId, String masterUsername) {
String effectiveUser = (masterUsername != null && !masterUsername.isBlank()) ? masterUsername : "postgres";
String[] cmd = {
"pg_dumpall",
"-U", effectiveUser
};
try {
ContainerExecResult result = execInContainer(containerId, cmd, 120);
if (result.exitCode() != 0) {
throw new RuntimeException("pg_dumpall failed with exit code " + result.exitCode() + ": " + result.stderr());
}
return result.output();
} catch (Exception e) {
throw new RuntimeException("Failed to create postgres snapshot", e);
}
}
public void restorePostgresSnapshot(String containerId, String masterUsername, String sqlDump) {
String effectiveUser = (masterUsername != null && !masterUsername.isBlank()) ? masterUsername : "postgres";
try {
String restoreScript = postgresRestoreScript();
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
try (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream tar =
new org.apache.commons.compress.archivers.tar.TarArchiveOutputStream(bos)) {
tar.setLongFileMode(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.LONGFILE_GNU);
addTarEntry(tar, "dump.sql", sqlDump);
addTarEntry(tar, "restore.sh", restoreScript);
}
try (java.io.InputStream tarStream = new java.io.ByteArrayInputStream(bos.toByteArray())) {
lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId)
.withRemotePath("/tmp")
.withTarInputStream(tarStream)
.exec();
}
String[] cmd = { "sh", "/tmp/restore.sh", effectiveUser };
ContainerExecResult result = null;
for (int i = 0; i < 60; i++) {
result = execInContainer(containerId, cmd, 120);
if (result.exitCode() == 0) {
break;
}
if (result.exitCode() != 2) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted while restoring postgres snapshot", ie);
}
}
if (result == null || result.exitCode() != 0) {
String errMsg = result != null ? (result.stderr().isEmpty() ? result.output() : result.stderr()) : "";
throw new RuntimeException("psql restore failed with exit code "
+ (result != null ? result.exitCode() : -1) + ": " + errMsg);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Failed to restore postgres snapshot", e);
}
}
static String postgresRestoreScript() {
return """
#!/bin/sh
set -e
USER="$1"
# Drop all non-template, non-postgres databases
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -tAc \\
"SELECT datname FROM pg_database WHERE datistemplate = false AND datname <> 'postgres'" \\
| while read -r db; do
[ -z "$db" ] && continue
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -c "DROP DATABASE IF EXISTS \\"$db\\""
done
# Exclude the master user CREATE ROLE statement to avoid conflicts during restore
sed -e "s/^CREATE ROLE \\\"\\?$USER\\\"\\?.*;/-- &/" /tmp/dump.sql > /tmp/dump_filtered.sql
mv /tmp/dump_filtered.sql /tmp/dump.sql
# Drop all non-current-user, non-system roles
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -tAc \\
"SELECT rolname FROM pg_roles WHERE rolname <> current_user AND rolname NOT LIKE 'pg_%'" \\
| while read -r role; do
[ -z "$role" ] && continue
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -c "DROP ROLE IF EXISTS \\"$role\\""
done
# Replay the dump connected to postgres
psql -v ON_ERROR_STOP=1 -U "$USER" -d postgres -f /tmp/dump.sql
""";
}
private static void addTarEntry(
org.apache.commons.compress.archivers.tar.TarArchiveOutputStream tar,
String name, String content) throws java.io.IOException {
byte[] bytes = content.getBytes(java.nio.charset.StandardCharsets.UTF_8);
org.apache.commons.compress.archivers.tar.TarArchiveEntry entry =
new org.apache.commons.compress.archivers.tar.TarArchiveEntry(name);
entry.setSize(bytes.length);
entry.setMode(0755);
tar.putArchiveEntry(entry);
tar.write(bytes);
tar.closeArchiveEntry();
}
private ContainerExecResult execInContainer(String containerId, String[] cmd, int timeoutSeconds) throws Exception {
String execId = lifecycleManager.getDockerClient().execCreateCmd(containerId)
.withCmd(cmd)
@@ -620,13 +734,19 @@ public class RdsContainerManager {
CountDownLatch latch = new CountDownLatch(1);
ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
Closeable callback = lifecycleManager.getDockerClient().execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {
@Override
public void onNext(Frame frame) {
if (frame.getPayload() != null) {
try {
output.write(frame.getPayload());
} catch (IOException ignored) {
if (frame.getStreamType() == com.github.dockerjava.api.model.StreamType.STDOUT) {
output.write(frame.getPayload());
} else if (frame.getStreamType() == com.github.dockerjava.api.model.StreamType.STDERR) {
stderr.write(frame.getPayload());
}
} catch (IOException e) {
LOG.warnv(e, "Failed to read output stream for container exec {0}", execId);
}
}
}
@@ -645,18 +765,19 @@ public class RdsContainerManager {
try {
boolean completed = latch.await(timeoutSeconds, TimeUnit.SECONDS);
if (!completed) {
return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s");
return new ContainerExecResult(-1, "Timed out after " + timeoutSeconds + "s", "");
}
Long exitCode = lifecycleManager.getDockerClient().inspectExecCmd(execId).exec().getExitCodeLong();
return new ContainerExecResult(
exitCode != null ? exitCode : -1,
output.toString(StandardCharsets.UTF_8));
output.toString(StandardCharsets.UTF_8),
stderr.toString(StandardCharsets.UTF_8));
} finally {
callback.close();
}
}
record ContainerExecResult(long exitCode, String output) {}
record ContainerExecResult(long exitCode, String output, String stderr) {}
public void removeVolume(String instanceId, String volumeId) {
removeVolume(instanceId, instanceId,
@@ -0,0 +1,108 @@
package io.github.hectorvent.floci.services.rds.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
@RegisterForReflection
public class DbSnapshot {
private String dbSnapshotIdentifier;
private String dbInstanceIdentifier;
private Instant snapshotCreateTime;
private DatabaseEngine engine;
private String engineVersion;
private int allocatedStorage;
private String status;
private String masterUsername;
private String masterPassword;
private String availabilityZone;
private String vpcId;
private Instant instanceCreateTime;
private int port;
private boolean iamDatabaseAuthenticationEnabled;
private String dbiResourceId;
private String dbName;
private String dbInstanceClass;
private Map<String, String> tags = new LinkedHashMap<>();
public DbSnapshot() {}
public DbSnapshot(String dbSnapshotIdentifier, String dbInstanceIdentifier, Instant snapshotCreateTime,
DatabaseEngine engine, String engineVersion, int allocatedStorage, String status,
String masterUsername, String masterPassword, String availabilityZone, String vpcId, Instant instanceCreateTime,
int port, boolean iamDatabaseAuthenticationEnabled, String dbiResourceId, String dbInstanceClass) {
this.dbSnapshotIdentifier = dbSnapshotIdentifier;
this.dbInstanceIdentifier = dbInstanceIdentifier;
this.snapshotCreateTime = snapshotCreateTime;
this.engine = engine;
this.engineVersion = engineVersion;
this.allocatedStorage = allocatedStorage;
this.status = status;
this.masterUsername = masterUsername;
this.masterPassword = masterPassword;
this.availabilityZone = availabilityZone;
this.vpcId = vpcId;
this.instanceCreateTime = instanceCreateTime;
this.port = port;
this.iamDatabaseAuthenticationEnabled = iamDatabaseAuthenticationEnabled;
this.dbiResourceId = dbiResourceId;
this.dbInstanceClass = dbInstanceClass;
}
public String getDbSnapshotIdentifier() { return dbSnapshotIdentifier; }
public void setDbSnapshotIdentifier(String dbSnapshotIdentifier) { this.dbSnapshotIdentifier = dbSnapshotIdentifier; }
public String getDbInstanceIdentifier() { return dbInstanceIdentifier; }
public void setDbInstanceIdentifier(String dbInstanceIdentifier) { this.dbInstanceIdentifier = dbInstanceIdentifier; }
public Instant getSnapshotCreateTime() { return snapshotCreateTime; }
public void setSnapshotCreateTime(Instant snapshotCreateTime) { this.snapshotCreateTime = snapshotCreateTime; }
public DatabaseEngine getEngine() { return engine; }
public void setEngine(DatabaseEngine engine) { this.engine = engine; }
public String getEngineVersion() { return engineVersion; }
public void setEngineVersion(String engineVersion) { this.engineVersion = engineVersion; }
public int getAllocatedStorage() { return allocatedStorage; }
public void setAllocatedStorage(int allocatedStorage) { this.allocatedStorage = allocatedStorage; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getMasterUsername() { return masterUsername; }
public void setMasterUsername(String masterUsername) { this.masterUsername = masterUsername; }
public String getMasterPassword() { return masterPassword; }
public void setMasterPassword(String masterPassword) { this.masterPassword = masterPassword; }
public String getAvailabilityZone() { return availabilityZone; }
public void setAvailabilityZone(String availabilityZone) { this.availabilityZone = availabilityZone; }
public String getVpcId() { return vpcId; }
public void setVpcId(String vpcId) { this.vpcId = vpcId; }
public Instant getInstanceCreateTime() { return instanceCreateTime; }
public void setInstanceCreateTime(Instant instanceCreateTime) { this.instanceCreateTime = instanceCreateTime; }
public int getPort() { return port; }
public void setPort(int port) { this.port = port; }
public boolean isIamDatabaseAuthenticationEnabled() { return iamDatabaseAuthenticationEnabled; }
public void setIamDatabaseAuthenticationEnabled(boolean iamDatabaseAuthenticationEnabled) { this.iamDatabaseAuthenticationEnabled = iamDatabaseAuthenticationEnabled; }
public String getDbiResourceId() { return dbiResourceId; }
public void setDbiResourceId(String dbiResourceId) { this.dbiResourceId = dbiResourceId; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
public String getDbName() { return dbName; }
public void setDbName(String dbName) { this.dbName = dbName; }
public String getDbInstanceClass() { return dbInstanceClass; }
public void setDbInstanceClass(String dbInstanceClass) { this.dbInstanceClass = dbInstanceClass; }
}
@@ -0,0 +1,114 @@
package io.github.hectorvent.floci.services.rds;
import com.github.dockerjava.api.DockerClient;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@QuarkusTest
@Tag("docker")
class RdsAwsIntegrationTest {
private static final String RDS_AUTH = "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/rds/aws4_request";
@Inject
DockerClient dockerClient;
@BeforeEach
void checkDocker() {
try {
dockerClient.pingCmd().exec();
} catch (Exception e) {
assumeTrue(false, "Docker is not available: " + e.getMessage());
}
}
@Test
void createDescribeRestoreRoundTrip() throws Exception {
String suffix = Long.toString(System.nanoTime(), 36);
String dbId = "test-db-" + suffix;
String snapshotId = "test-snap-" + suffix;
String restoreId = "test-restore-" + suffix;
// 1. Create DB instance
given()
.header("Authorization", RDS_AUTH)
.formParam("Action", "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)
.body(containsString(dbId));
// 2. Wait for it to be available
waitForDb(dbId);
// 3. Create snapshot
given()
.header("Authorization", RDS_AUTH)
.formParam("Action", "CreateDBSnapshot")
.formParam("DBInstanceIdentifier", dbId)
.formParam("DBSnapshotIdentifier", snapshotId)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString(snapshotId));
// 4. Restore DB instance
given()
.header("Authorization", RDS_AUTH)
.formParam("Action", "RestoreDBInstanceFromDBSnapshot")
.formParam("DBInstanceIdentifier", restoreId)
.formParam("DBSnapshotIdentifier", snapshotId)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString(restoreId));
// 5. Wait for it to be available
waitForDb(restoreId);
// 6. Describe restored DB instance to ensure it exists
given()
.header("Authorization", RDS_AUTH)
.formParam("Action", "DescribeDBInstances")
.formParam("DBInstanceIdentifier", restoreId)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString(restoreId));
}
private void waitForDb(String dbId) throws Exception {
for (int i = 0; i < 60; i++) {
String status = given()
.header("Authorization", RDS_AUTH)
.formParam("Action", "DescribeDBInstances")
.formParam("DBInstanceIdentifier", dbId)
.when()
.post("/")
.then()
.extract().xmlPath().getString("DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.DBInstanceStatus");
if ("available".equals(status) || status == null) {
return; // mock mode might return available immediately or not have status depending on delay config
}
Thread.sleep(1000);
}
throw new RuntimeException("DB instance " + dbId + " did not become available.");
}
}
@@ -1005,6 +1005,7 @@ class RdsQueryHandlerTest {
@Test
void describeDbSnapshots_returnsEmptyListWith200() {
when(service.describeDbSnapshots(null, null)).thenReturn(List.of());
Response response = handler.handle("DescribeDBSnapshots", params());
String body = (String) response.getEntity();
@@ -1014,6 +1015,26 @@ class RdsQueryHandlerTest {
assertFalse(body.contains("<Marker>"));
}
@Test
void describeDbSnapshots_returnsSnapshotListWith200() {
io.github.hectorvent.floci.services.rds.model.DbSnapshot snapshot = new io.github.hectorvent.floci.services.rds.model.DbSnapshot();
snapshot.setDbSnapshotIdentifier("mysnap");
snapshot.setDbInstanceIdentifier("mydb");
snapshot.setEngine(io.github.hectorvent.floci.services.rds.model.DatabaseEngine.POSTGRES);
when(service.describeDbSnapshots("mysnap", "mydb")).thenReturn(List.of(snapshot));
MultivaluedMap<String, String> p = params();
p.add("DBSnapshotIdentifier", "mysnap");
p.add("DBInstanceIdentifier", "mydb");
Response response = handler.handle("DescribeDBSnapshots", p);
assertEquals(200, response.getStatus());
String body = (String) response.getEntity();
assertTrue(body.contains("<DescribeDBSnapshotsResult>"));
assertTrue(body.contains("<DBSnapshotIdentifier>mysnap</DBSnapshotIdentifier>"));
assertTrue(body.contains("<DBInstanceIdentifier>mydb</DBInstanceIdentifier>"));
}
@Test
void describeDbProxies_returnsEmptyListWith200() {
Response response = handler.handle("DescribeDBProxies", params());
@@ -1658,18 +1679,49 @@ class RdsQueryHandlerTest {
}
@Test
void describeDbSubnetGroups_missingNameReturnsNotFoundFault() {
when(service.listDbSubnetGroups("does-not-exist", null))
.thenThrow(new AwsException("DBSubnetGroupNotFoundFault",
"DB subnet group does-not-exist not found.", 404));
void createDbSnapshot_success() {
io.github.hectorvent.floci.services.rds.model.DbSnapshot snapshot = new io.github.hectorvent.floci.services.rds.model.DbSnapshot();
snapshot.setDbSnapshotIdentifier("mysnap");
snapshot.setDbInstanceIdentifier("mydb");
snapshot.setEngine(io.github.hectorvent.floci.services.rds.model.DatabaseEngine.POSTGRES);
when(service.createDbSnapshot("mysnap", "mydb")).thenReturn(snapshot);
MultivaluedMap<String, String> p = params();
p.add("DBSubnetGroupName", "does-not-exist");
p.add("DBSnapshotIdentifier", "mysnap");
p.add("DBInstanceIdentifier", "mydb");
Response response = handler.handle("CreateDBSnapshot", p);
Response response = handler.handle("DescribeDBSubnetGroups", p);
assertEquals(200, response.getStatus());
String body = (String) response.getEntity();
assertTrue(body.contains("<CreateDBSnapshotResult>"));
assertTrue(body.contains("<DBSnapshotIdentifier>mysnap</DBSnapshotIdentifier>"));
assertTrue(body.contains("<DBInstanceIdentifier>mydb</DBInstanceIdentifier>"));
assertTrue(body.contains("<Engine>postgres</Engine>"));
}
assertEquals(404, response.getStatus());
assertTrue(((String) response.getEntity()).contains("DBSubnetGroupNotFoundFault"));
@Test
void restoreDbInstanceFromDbSnapshot_success() {
DbInstance instance = makeInstance("mydb");
when(service.restoreDbInstanceFromDbSnapshot(eq("mydb"), eq("mysnap"), eq("db.t3.large"), eq("us-east-1a"), eq(true), eq("my-subnets"), eq(List.of("sg-123")), eq(Map.of("Env", "Prod"))))
.thenReturn(instance);
MultivaluedMap<String, String> p = params();
p.add("DBInstanceIdentifier", "mydb");
p.add("DBSnapshotIdentifier", "mysnap");
p.add("DBInstanceClass", "db.t3.large");
p.add("AvailabilityZone", "us-east-1a");
p.add("MultiAZ", "true");
p.add("DBSubnetGroupName", "my-subnets");
p.add("VpcSecurityGroupIds.VpcSecurityGroupId.1", "sg-123");
p.add("Tags.Tag.1.Key", "Env");
p.add("Tags.Tag.1.Value", "Prod");
Response response = handler.handle("RestoreDBInstanceFromDBSnapshot", p);
assertEquals(200, response.getStatus());
String body = (String) response.getEntity();
assertTrue(body.contains("<RestoreDBInstanceFromDBSnapshotResult>"));
assertTrue(body.contains("<DBInstanceIdentifier>mydb</DBInstanceIdentifier>"));
}
// ─────────────── NotFound faults for missing identifiers (AWS parity) ───────────────
@@ -2608,6 +2608,155 @@ class RdsServiceTest {
eq("admin"), eq("secret"), eq("app"), any());
}
@Test
void createDbInstanceAcceptsMasterUsernameWithUnderscores() {
DbInstance instance = rdsService.createDbInstance("mydb", "postgres", "13",
"my_master_user", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
assertEquals("my_master_user", instance.getMasterUsername());
}
@Test
void createDbInstanceRejectsInvalidMasterUsername() {
AwsException ex1 = assertThrows(AwsException.class, () ->
rdsService.createDbInstance("mydb1", "postgres", "13",
"_admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false));
assertEquals("InvalidParameterValue", ex1.getErrorCode());
assertEquals("MasterUsername must begin with a letter and contain only alphanumeric characters or underscores.", ex1.getMessage());
AwsException ex2 = assertThrows(AwsException.class, () ->
rdsService.createDbInstance("mydb2", "postgres", "13",
"1admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false));
assertEquals("InvalidParameterValue", ex2.getErrorCode());
AwsException ex3 = assertThrows(AwsException.class, () ->
rdsService.createDbInstance("mydb3", "postgres", "13",
"admin-user", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false));
assertEquals("InvalidParameterValue", ex3.getErrorCode());
AwsException ex4 = assertThrows(AwsException.class, () ->
rdsService.createDbInstance("mydb4", "postgres", "13",
"a1234567890123456", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false));
assertEquals("InvalidParameterValue", ex4.getErrorCode());
}
@Test
void createDbSnapshotThrowsForNonPostgres() {
rdsService.createDbInstance("mydb", "mysql", "8.0",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
AwsException exception = assertThrows(AwsException.class, () ->
rdsService.createDbSnapshot("mysnap", "mydb"));
assertEquals("InvalidDBInstanceState", exception.getErrorCode());
assertTrue(exception.getMessage().contains("CreateDBSnapshot is not supported for engine MYSQL"));
}
@Test
void createDbSnapshotSucceedsForPostgres() {
rdsService.createDbInstance("mydb", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
io.github.hectorvent.floci.services.rds.model.DbSnapshot snapshot = rdsService.createDbSnapshot("mysnap", "mydb");
assertEquals("mysnap", snapshot.getDbSnapshotIdentifier());
assertEquals("mydb", snapshot.getDbInstanceIdentifier());
assertEquals(DatabaseEngine.POSTGRES, snapshot.getEngine());
assertEquals("available", snapshot.getStatus());
verify(containerManager).createPostgresSnapshot(any(), eq("admin"));
}
@Test
void createDbSnapshotThrowsOnDumpFailure() {
rdsService.createDbInstance("mydb", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenThrow(new RuntimeException("dump failed"));
AwsException exception = assertThrows(AwsException.class, () ->
rdsService.createDbSnapshot("mysnap", "mydb"));
assertEquals("InvalidDBInstanceState", exception.getErrorCode());
assertTrue(exception.getMessage().contains("Failed to create snapshot: dump failed"));
}
@Test
void restoreDbInstanceFromDbSnapshotRestoresData() {
rdsService.createDbInstance("mydb", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
rdsService.createDbSnapshot("mysnap", "mydb");
DbInstance restored = rdsService.restoreDbInstanceFromDbSnapshot("restored-db", "mysnap", "db.t3.large", "us-east-1a", false, null, null, Map.of());
assertEquals("restored-db", restored.getDbInstanceIdentifier());
assertEquals("db.t3.large", restored.getDbInstanceClass());
assertEquals(DatabaseEngine.POSTGRES, restored.getEngine());
assertEquals("admin", restored.getMasterUsername());
assertEquals("us-east-1a", restored.getAvailabilityZone());
assertEquals("dbname", restored.getDbName());
verify(containerManager).restorePostgresSnapshot(any(), eq("admin"), eq("MOCK_DUMP_DATA"));
}
@Test
void restoreDbInstanceFromDbSnapshotThrowsOnRestoreFailure() {
rdsService.createDbInstance("mydb", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
rdsService.createDbSnapshot("mysnap", "mydb");
org.mockito.Mockito.doThrow(new RuntimeException("failed restore"))
.when(containerManager).restorePostgresSnapshot(any(), eq("admin"), eq("MOCK_DUMP_DATA"));
AwsException exception = assertThrows(AwsException.class, () ->
rdsService.restoreDbInstanceFromDbSnapshot("restored-db", "mysnap", "db.t3.large", "us-east-1a", false, null, null, Map.of()));
assertEquals("InvalidDBSnapshotState", exception.getErrorCode());
assertTrue(exception.getMessage().contains("Failed to restore snapshot: failed restore"));
}
@Test
void describeDbSnapshotsFiltersCorrectly() {
rdsService.createDbInstance("mydb1", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
rdsService.createDbInstance("mydb2", "postgres", "13",
"admin", "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
rdsService.createDbSnapshot("snap1", "mydb1");
rdsService.createDbSnapshot("snap2", "mydb2");
// 1. Describe all
Collection<io.github.hectorvent.floci.services.rds.model.DbSnapshot> all = rdsService.describeDbSnapshots(null, null);
assertEquals(2, all.size());
// 2. Describe by snapshot ID
Collection<io.github.hectorvent.floci.services.rds.model.DbSnapshot> snap1Result = rdsService.describeDbSnapshots("snap1", null);
assertEquals(1, snap1Result.size());
assertEquals("snap1", snap1Result.iterator().next().getDbSnapshotIdentifier());
// 3. Describe by invalid snapshot ID throws
assertThrows(AwsException.class, () -> rdsService.describeDbSnapshots("invalid-snap", null));
// 4. Describe by DB instance ID
Collection<io.github.hectorvent.floci.services.rds.model.DbSnapshot> inst1Result = rdsService.describeDbSnapshots(null, "mydb1");
assertEquals(1, inst1Result.size());
assertEquals("snap1", inst1Result.iterator().next().getDbSnapshotIdentifier());
}
@Test
void failedInstanceRestoreClearsRuntimeStateAndReleasesItsEndpointPort() {
InMemoryStorage<String, DbInstance> instances = new InMemoryStorage<>();
@@ -0,0 +1,298 @@
package io.github.hectorvent.floci.services.rds.container;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Round-trip test for PostgreSQL snapshot dump and restore using real Docker containers.
* Verifies that a dump taken from one container can be restored into a fresh container
* with the same master user, which previously failed due to "current user cannot be dropped".
*/
@Tag("docker")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RdsContainerManagerPostgresTest {
private static final String IMAGE = "postgres:16-alpine";
private static final String MASTER_USER = "admin";
private static final String MASTER_PASSWORD = "secret";
private static final String DB_NAME = "appdb";
private static final int PG_PORT = 5432;
private DockerClient dockerClient;
private String sourceContainerId;
private String targetContainerId;
@BeforeAll
void setUp() {
try {
DefaultDockerClientConfig clientConfig = DefaultDockerClientConfig
.createDefaultConfigBuilder()
.build();
ApacheDockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
.dockerHost(clientConfig.getDockerHost())
.maxConnections(10)
.connectionTimeout(Duration.ofSeconds(10))
.responseTimeout(Duration.ofMinutes(5))
.build();
dockerClient = DockerClientImpl.getInstance(clientConfig, httpClient);
dockerClient.pingCmd().exec();
} catch (Exception e) {
assumeTrue(false, "Docker is not available: " + e.getMessage());
}
try {
dockerClient.pullImageCmd(IMAGE).start().awaitCompletion(120, TimeUnit.SECONDS);
} catch (Exception e) {
assumeTrue(false, "Could not pull " + IMAGE + ": " + e.getMessage());
}
}
@AfterAll
void tearDown() {
removeContainer(sourceContainerId);
removeContainer(targetContainerId);
if (dockerClient != null) {
try {
dockerClient.close();
} catch (IOException ignored) {
// best effort
}
}
}
@Test
void snapshotRoundTripPreservesDataWithNonDefaultUser() throws Exception {
// 1. Start source container with non-default master user
sourceContainerId = startPostgresContainer("floci-test-pg-source");
waitForPostgresReady(sourceContainerId, MASTER_USER, DB_NAME, 60);
// 2. Create a probe table with data in the application database
execPsql(sourceContainerId, MASTER_USER, DB_NAME,
"CREATE TABLE probe (id serial PRIMARY KEY, value text);"
+ "INSERT INTO probe (value) VALUES ('round-trip-test');");
// 3. Dump using the production code path
RdsContainerManager manager = createManager();
String dump = manager.createPostgresSnapshot(sourceContainerId, MASTER_USER);
assertTrue(dump.contains("CREATE ROLE " + MASTER_USER),
"Dump should contain CREATE ROLE for the master user");
assertTrue(dump.contains(DB_NAME),
"Dump should reference the application database");
// 4. Start a fresh target container with the same master user
targetContainerId = startPostgresContainer("floci-test-pg-target");
waitForPostgresReady(targetContainerId, MASTER_USER, DB_NAME, 60);
// 5. Restore using the production code path
manager.restorePostgresSnapshot(targetContainerId, MASTER_USER, dump);
// 6. Verify the probe row exists in the target
String result = queryPsql(targetContainerId, MASTER_USER, DB_NAME,
"SELECT value FROM probe WHERE id = 1");
assertEquals("round-trip-test", result.trim(),
"Restored database should contain the probe row");
}
private String startPostgresContainer(String name) {
removeContainerByName(name);
ExposedPort pgPort = ExposedPort.tcp(PG_PORT);
CreateContainerResponse container = dockerClient.createContainerCmd(IMAGE)
.withName(name)
.withExposedPorts(pgPort)
.withHostConfig(HostConfig.newHostConfig()
.withPortBindings(new Ports(pgPort, Ports.Binding.empty())))
.withEnv(
"POSTGRES_USER=" + MASTER_USER,
"POSTGRES_PASSWORD=" + MASTER_PASSWORD,
"POSTGRES_DB=" + DB_NAME,
"POSTGRES_HOST_AUTH_METHOD=md5"
)
.exec();
dockerClient.startContainerCmd(container.getId()).exec();
return container.getId();
}
private void waitForPostgresReady(String containerId, String user, String dbName, int maxSeconds) {
String[] cmd = {"psql", "-U", user, "-d", dbName, "-c", "SELECT 1"};
for (int i = 0; i < maxSeconds; i++) {
try {
ExecResult result = exec(containerId, cmd, 5);
if (result.exitCode() == 0) {
return;
}
} catch (Exception ignored) {
// not ready yet
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted waiting for PostgreSQL", e);
}
}
throw new RuntimeException("PostgreSQL did not become ready within " + maxSeconds + "s");
}
private void execPsql(String containerId, String user, String db, String sql) throws Exception {
String[] cmd = {"psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db, "-c", sql};
ExecResult result = exec(containerId, cmd, 30);
if (result.exitCode() != 0) {
throw new RuntimeException("psql exec failed (exit " + result.exitCode() + "): "
+ result.stderr() + " | " + result.stdout());
}
}
private String queryPsql(String containerId, String user, String db, String sql) throws Exception {
String[] cmd = {"psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db, "-tAc", sql};
ExecResult result = exec(containerId, cmd, 30);
if (result.exitCode() != 0) {
throw new RuntimeException("psql query failed (exit " + result.exitCode() + "): "
+ result.stderr() + " | " + result.stdout());
}
return result.stdout();
}
private ExecResult exec(String containerId, String[] cmd, int timeoutSeconds) throws Exception {
String execId = dockerClient.execCreateCmd(containerId)
.withCmd(cmd)
.withAttachStdout(true)
.withAttachStderr(true)
.exec()
.getId();
CountDownLatch latch = new CountDownLatch(1);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
Closeable callback = dockerClient.execStartCmd(execId)
.exec(new ResultCallback.Adapter<Frame>() {
@Override
public void onNext(Frame frame) {
if (frame.getPayload() != null) {
try {
if (frame.getStreamType() == StreamType.STDOUT) {
stdout.write(frame.getPayload());
} else if (frame.getStreamType() == StreamType.STDERR) {
stderr.write(frame.getPayload());
}
} catch (IOException ignored) {
// best effort
}
}
}
@Override
public void onComplete() {
latch.countDown();
}
@Override
public void onError(Throwable t) {
latch.countDown();
}
});
try {
boolean completed = latch.await(timeoutSeconds, TimeUnit.SECONDS);
if (!completed) {
return new ExecResult(-1, stdout.toString(StandardCharsets.UTF_8),
"Timed out after " + timeoutSeconds + "s");
}
Long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();
return new ExecResult(
exitCode != null ? exitCode : -1,
stdout.toString(StandardCharsets.UTF_8),
stderr.toString(StandardCharsets.UTF_8));
} finally {
callback.close();
}
}
private RdsContainerManager createManager() {
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
return new RdsContainerManager(
mock(ContainerBuilder.class),
lifecycleManager,
mock(ContainerLogStreamer.class),
mock(ContainerDetector.class),
mock(EmulatorConfig.class),
new RegionResolver("us-east-1", "000000000000"),
mock(ServiceConfigAccess.class));
}
private void removeContainer(String containerId) {
if (containerId == null || dockerClient == null) {
return;
}
try {
dockerClient.stopContainerCmd(containerId).withTimeout(5).exec();
} catch (DockerException ignored) {
// already stopped or gone
}
try {
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
} catch (DockerException ignored) {
// already removed
}
}
private void removeContainerByName(String name) {
try {
List<com.github.dockerjava.api.model.Container> containers = dockerClient
.listContainersCmd()
.withShowAll(true)
.withNameFilter(List.of(name))
.exec();
for (com.github.dockerjava.api.model.Container c : containers) {
removeContainer(c.getId());
}
} catch (DockerException ignored) {
// best effort
}
}
record ExecResult(long exitCode, String stdout, String stderr) {}
}
@@ -1072,6 +1072,36 @@ class RdsContainerManagerTest {
org.mockito.ArgumentMatchers.isNull());
}
@Test
void restoreScriptConnectsToPostgresDatabase() {
String script = RdsContainerManager.postgresRestoreScript();
assertTrue(script.contains("-d postgres"), "Script must connect to postgres, not template1");
assertTrue(script.contains("-f /tmp/dump.sql"), "Script must replay the dump file");
}
@Test
void restoreScriptDropsNonSystemDatabasesAndRoles() {
String script = RdsContainerManager.postgresRestoreScript();
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("rolname <> current_user"),
"Script must not drop the current user");
assertTrue(script.contains("rolname NOT LIKE 'pg_%'"),
"Script must not drop system roles");
}
@Test
void restoreScriptInterpolatesUser() {
String script = RdsContainerManager.postgresRestoreScript();
assertTrue(script.contains("USER=\"$1\""),
"Script must assign the first argument to the USER variable");
}
private static EmulatorConfig config(Path hostRoot) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
@@ -1088,6 +1118,7 @@ class RdsContainerManagerTest {
when(docker.logMaxFile()).thenReturn("3");
when(config.storage()).thenReturn(storage);
when(storage.hostPersistentPath()).thenReturn(hostRoot.toString());
when(storage.mode()).thenReturn("persistent");
return config;
}