mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(rds): add the snapshot export task API (#5787)
* feat(rds): add StartExportTask, DescribeExportTasks and CancelExportTask with the export task lifecycle * docs(rds): document the snapshot export task API * fix(rds): route the export task actions to RDS when a request has no signing scope * fix(rds): refuse an export to a bucket outside the snapshot's region * fix(rds): answer export task calls with a copy of the task taken under the lock * fix(rds): refuse an unknown DescribeExportTasks filter name before matching any task * fix(rds): fail an export to a bucket in another region as a task, as RDS does, rather than refuse it * docs(rds): say that a bucket in another region fails an export in mock mode too
This commit is contained in:
1 parent
dfeb05a400
commit
772ec8d0c9
15 files changed
+1724
-5
No files matched your search
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.docdb.DocDbQueryHandler;
|
||||
import io.github.hectorvent.floci.services.neptune.NeptuneQueryHandler;
|
||||
import io.github.hectorvent.floci.services.rds.export.RdsExportTaskService;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbClusterParameterGroup;
|
||||
@@ -20,6 +21,7 @@ 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.EventSubscription;
|
||||
import io.github.hectorvent.floci.services.rds.model.ExportTask;
|
||||
import io.github.hectorvent.floci.services.rds.model.OptionGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.OptionGroupOption;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
@@ -33,6 +35,7 @@ import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -46,6 +49,7 @@ import static org.mockito.Mockito.*;
|
||||
class RdsQueryHandlerTest {
|
||||
|
||||
private RdsService service;
|
||||
private RdsExportTaskService exportTaskService;
|
||||
private DocDbQueryHandler docDbHandler;
|
||||
private NeptuneQueryHandler neptuneHandler;
|
||||
private RdsQueryHandler handler;
|
||||
@@ -61,7 +65,8 @@ class RdsQueryHandlerTest {
|
||||
when(config.defaultAvailabilityZone()).thenReturn("us-east-1a");
|
||||
docDbHandler = mock(DocDbQueryHandler.class);
|
||||
neptuneHandler = mock(NeptuneQueryHandler.class);
|
||||
handler = new RdsQueryHandler(service, config, docDbHandler, neptuneHandler);
|
||||
exportTaskService = mock(RdsExportTaskService.class);
|
||||
handler = new RdsQueryHandler(service, exportTaskService, config, docDbHandler, neptuneHandler);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@@ -3035,6 +3040,107 @@ class RdsQueryHandlerTest {
|
||||
return group;
|
||||
}
|
||||
|
||||
@Test
|
||||
void startExportTask_passesTheRequestAndAnswersWithTheTask() {
|
||||
ExportTask task = exportTask("appdb-export", ExportTask.STATUS_STARTING);
|
||||
when(exportTaskService.startExportTask(eq("appdb-export"), eq(task.getSourceArn()), eq("exports"),
|
||||
eq("items/rds"), eq("arn:aws:iam::000000000000:role/export"), eq("alias/export"),
|
||||
eq(List.of("appdb.public.items", "appdb.audit")), eq("us-east-1"))).thenReturn(task);
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("ExportTaskIdentifier", "appdb-export");
|
||||
params.add("SourceArn", task.getSourceArn());
|
||||
params.add("S3BucketName", "exports");
|
||||
params.add("S3Prefix", "items/rds");
|
||||
params.add("IamRoleArn", "arn:aws:iam::000000000000:role/export");
|
||||
params.add("KmsKeyId", "alias/export");
|
||||
params.add("ExportOnly.member.2", "appdb.audit");
|
||||
params.add("ExportOnly.member.1", "appdb.public.items");
|
||||
|
||||
String body = (String) handler.handle("StartExportTask", params, "us-east-1").getEntity();
|
||||
|
||||
assertTrue(body.contains("<StartExportTaskResult><ExportTaskIdentifier>appdb-export</ExportTaskIdentifier>"), body);
|
||||
assertTrue(body.contains("<ExportOnly><member>appdb.public.items</member></ExportOnly>"), body);
|
||||
assertTrue(body.contains("<SourceType>SNAPSHOT</SourceType>"), body);
|
||||
assertTrue(body.contains("<SnapshotTime>2026-10-13T08:00:00Z</SnapshotTime>"), body);
|
||||
assertTrue(body.contains("<Status>STARTING</Status>"), body);
|
||||
assertTrue(body.contains("<PercentProgress>0</PercentProgress>"), body);
|
||||
assertTrue(body.contains("<TotalExtractedDataInGB>0</TotalExtractedDataInGB>"), body);
|
||||
assertFalse(body.contains("<TaskEndTime>"), body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void startExportTask_requiresTheRoleAndTheKey() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("ExportTaskIdentifier", "appdb-export");
|
||||
params.add("SourceArn", "arn:aws:rds:us-east-1:000000000000:snapshot:snap");
|
||||
params.add("S3BucketName", "exports");
|
||||
|
||||
String body = (String) handler.handle("StartExportTask", params, "us-east-1").getEntity();
|
||||
|
||||
assertTrue(body.contains("IamRoleArn"), body);
|
||||
verifyNoInteractions(exportTaskService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeExportTasks_listsEachTaskAndPassesFilters() {
|
||||
when(exportTaskService.describeExportTasks(eq(null), eq(null), eq("SNAPSHOT"), any(), eq("us-east-1")))
|
||||
.thenReturn(List.of(exportTask("one", ExportTask.STATUS_COMPLETE),
|
||||
exportTask("two", ExportTask.STATUS_IN_PROGRESS)));
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("SourceType", "SNAPSHOT");
|
||||
params.add("Filters.Filter.1.Name", "status");
|
||||
params.add("Filters.Filter.1.Values.Value.1", "complete");
|
||||
|
||||
String body = (String) handler.handle("DescribeExportTasks", params, "us-east-1").getEntity();
|
||||
|
||||
assertTrue(body.contains("<ExportTasks><ExportTask><ExportTaskIdentifier>one</ExportTaskIdentifier>"), body);
|
||||
assertTrue(body.contains("<ExportTask><ExportTaskIdentifier>two</ExportTaskIdentifier>"), body);
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, List<String>>> filters = ArgumentCaptor.forClass(Map.class);
|
||||
verify(exportTaskService).describeExportTasks(eq(null), eq(null), eq("SNAPSHOT"), filters.capture(),
|
||||
eq("us-east-1"));
|
||||
assertEquals(Map.of("status", List.of("complete")), filters.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelExportTask_answersCanceling() {
|
||||
when(exportTaskService.cancelExportTask("appdb-export", "us-east-1"))
|
||||
.thenReturn(exportTask("appdb-export", ExportTask.STATUS_CANCELING));
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("ExportTaskIdentifier", "appdb-export");
|
||||
|
||||
String body = (String) handler.handle("CancelExportTask", params, "us-east-1").getEntity();
|
||||
|
||||
assertTrue(body.contains("<CancelExportTaskResult>"), body);
|
||||
assertTrue(body.contains("<Status>CANCELING</Status>"), body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void exportTaskErrorsKeepTheirRdsCode() {
|
||||
when(exportTaskService.cancelExportTask("missing", "us-east-1")).thenThrow(new AwsException(
|
||||
"ExportTaskNotFound", "The export task with the ID missing doesn't exist.", 404));
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("ExportTaskIdentifier", "missing");
|
||||
|
||||
Response response = handler.handle("CancelExportTask", params, "us-east-1");
|
||||
|
||||
assertEquals(404, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("<Code>ExportTaskNotFound</Code>"));
|
||||
}
|
||||
|
||||
private static ExportTask exportTask(String id, String status) {
|
||||
ExportTask task = new ExportTask();
|
||||
task.setExportTaskIdentifier(id);
|
||||
task.setSourceArn("arn:aws:rds:us-east-1:000000000000:snapshot:appdb-snap");
|
||||
task.setSourceType(ExportTask.SOURCE_TYPE_SNAPSHOT);
|
||||
task.setExportOnly(List.of("appdb.public.items"));
|
||||
task.setSnapshotTime(Instant.parse("2026-10-13T08:00:00Z"));
|
||||
task.setTaskStartTime(Instant.parse("2026-10-13T09:00:00Z"));
|
||||
task.setS3Bucket("exports");
|
||||
task.setStatus(status);
|
||||
return task;
|
||||
}
|
||||
|
||||
private static MultivaluedMap<String, String> params() {
|
||||
return new MultivaluedHashMap<>();
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import io.github.hectorvent.floci.services.rds.container.AutoPauseListener;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendGate;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerHandle;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
|
||||
import io.github.hectorvent.floci.services.rds.export.SnapshotExportSource;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbClusterEndpoint;
|
||||
@@ -65,6 +66,7 @@ import java.net.ServerSocket;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -5340,6 +5342,75 @@ class RdsServiceTest {
|
||||
rdsService.listTagsForResource(snapshot.getDbSnapshotArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void snapshotExportSourceCarriesTheSnapshotsDump() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", "password", "dbname", "db.t3.micro", 20, false, null, null, null, null, false);
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
|
||||
DbSnapshot snapshot = rdsService.createDbSnapshot("mysnap", "mydb");
|
||||
|
||||
SnapshotExportSource source = rdsService.snapshotExportSource(snapshot.getDbSnapshotArn(), "us-east-1");
|
||||
|
||||
assertEquals("MOCK_DUMP_DATA", source.sqlDump());
|
||||
assertEquals(snapshot.getDbSnapshotArn(), source.sourceArn());
|
||||
assertEquals("admin", source.masterUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void snapshotExportSourceRefusesAnArnFromAnotherPartition() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", "password", "dbname", "db.t3.micro", 20, false, null, null, null, null, false);
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
|
||||
DbSnapshot snapshot = rdsService.createDbSnapshot("mysnap", "mydb");
|
||||
String otherPartition = snapshot.getDbSnapshotArn().replaceFirst("^arn:aws:", "arn:aws-cn:");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> rdsService.snapshotExportSource(otherPartition, "us-east-1"));
|
||||
|
||||
assertEquals("DBSnapshotNotFound", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void snapshotExportSourceRefusesASnapshotWhoseDumpIsMissing() {
|
||||
Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
StorageFactory factory = mock(StorageFactory.class);
|
||||
when(factory.create(eq("rds"), any(), any())).thenAnswer(invocation -> stores.computeIfAbsent(
|
||||
invocation.getArgument(1), name -> AccountAwareStorageBackend.inMemory("123456789012")));
|
||||
RdsService service = new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
factory, null, null, null, null, kmsService, null);
|
||||
service.createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", "password", "dbname", "db.t3.micro", 20, false, null, null, null, null, false);
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("MOCK_DUMP_DATA");
|
||||
DbSnapshot snapshot = service.createDbSnapshot("mysnap", "mydb");
|
||||
stores.get("rds-snapshot-data.json").clear();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.snapshotExportSource(snapshot.getDbSnapshotArn(), "us-east-1"));
|
||||
|
||||
assertEquals("DBSnapshotNotFound", error.getErrorCode());
|
||||
assertEquals(404, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void snapshotExportSourceRefusesAClusterSnapshotWhoseDumpIsMissing() {
|
||||
Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
StorageFactory factory = mock(StorageFactory.class);
|
||||
when(factory.create(eq("rds"), any(), any())).thenAnswer(invocation -> stores.computeIfAbsent(
|
||||
invocation.getArgument(1), name -> AccountAwareStorageBackend.inMemory("123456789012")));
|
||||
RdsService service = new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
factory, null, null, null, null, kmsService, null);
|
||||
service.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("CLUSTER_DUMP");
|
||||
DbClusterSnapshot snapshot = service.createDbClusterSnapshot("csnap", "aurora-cluster", Map.of());
|
||||
stores.get("rds-snapshot-data.json").clear();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.snapshotExportSource(snapshot.getDbClusterSnapshotArn(), "us-east-1"));
|
||||
|
||||
assertEquals("DBClusterSnapshotNotFoundFault", error.getErrorCode());
|
||||
}
|
||||
|
||||
private DbClusterSnapshot clusterSnapshotOfNewCluster(String snapshotId) {
|
||||
rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.github.hectorvent.floci.services.rds.export;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** Which tables an export's {@code ExportOnly} selects, and the entries it refuses. */
|
||||
class ExportPlanTest {
|
||||
|
||||
@Test
|
||||
void emptyExportOnlySelectsEverything() {
|
||||
ExportOnlySelection selection = ExportOnlySelection.of(List.of());
|
||||
|
||||
assertTrue(selection.includesDatabase("appdb"));
|
||||
assertTrue(selection.includes("appdb", "public", "items"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exportOnlySelectsByDatabaseSchemaOrTable() {
|
||||
ExportOnlySelection selection = ExportOnlySelection.of(
|
||||
List.of("whole", "appdb.audit", "appdb.public.items"));
|
||||
|
||||
assertTrue(selection.includes("whole", "any", "table"));
|
||||
assertTrue(selection.includes("appdb", "audit", "events"));
|
||||
assertTrue(selection.includes("appdb", "public", "items"));
|
||||
assertFalse(selection.includes("appdb", "public", "orders"));
|
||||
assertFalse(selection.includesDatabase("other"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"appdb.public.items.extra", "app-db", "appdb..items", "appdb.public items"})
|
||||
void exportOnlyRefusesNamesAPartialExportCannotTake(String entry) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> ExportOnlySelection.of(List.of(entry)));
|
||||
|
||||
assertThat(error.getErrorCode(), equalTo("InvalidExportOnly"));
|
||||
}
|
||||
}
|
||||
+522
@@ -0,0 +1,522 @@
|
||||
package io.github.hectorvent.floci.services.rds.export;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.rds.RdsService;
|
||||
import io.github.hectorvent.floci.services.rds.model.ExportTask;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The export task lifecycle, with the export itself replaced: the statuses a task passes
|
||||
* through, cancellation, the request checks and the recovery of tasks a restart interrupted.
|
||||
*/
|
||||
class RdsExportTaskServiceTest {
|
||||
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String SNAPSHOT_ARN = "arn:aws:rds:us-east-1:000000000000:snapshot:appdb-snap";
|
||||
private static final Instant SNAPSHOT_TIME = Instant.parse("2026-10-13T08:00:00Z");
|
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-10-13T09:00:00Z"), ZoneOffset.UTC);
|
||||
|
||||
private RdsService rdsService;
|
||||
private S3Service s3Service;
|
||||
private RegionResolver regionResolver;
|
||||
private final List<Runnable> workers = new ArrayList<>();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rdsService = mock(RdsService.class);
|
||||
s3Service = mock(S3Service.class);
|
||||
regionResolver = new RegionResolver(REGION, ACCOUNT);
|
||||
when(rdsService.snapshotExportSource(SNAPSHOT_ARN, REGION)).thenReturn(new SnapshotExportSource(
|
||||
SNAPSHOT_ARN, "postgres:16-alpine", "master", "secret", SNAPSHOT_TIME, "-- dump"));
|
||||
when(s3Service.bucketExists("exports")).thenReturn(true);
|
||||
}
|
||||
|
||||
private RdsExportTaskService service(SnapshotExporter exporter) {
|
||||
return new RdsExportTaskService(new InMemoryStorage<>(), rdsService, s3Service, exporter,
|
||||
regionResolver, false, CLOCK, workers::add);
|
||||
}
|
||||
|
||||
private static SnapshotExporter.Outcome outcome(long bytes, List<String> warnings) {
|
||||
return new SnapshotExporter.Outcome(bytes, warnings, CLOCK.instant());
|
||||
}
|
||||
|
||||
private ExportTask start(RdsExportTaskService service, String id, List<String> exportOnly) {
|
||||
return service.startExportTask(id, SNAPSHOT_ARN, "exports", "prefix", "arn:aws:iam::000000000000:role/export",
|
||||
"alias/export", exportOnly, REGION);
|
||||
}
|
||||
|
||||
private ExportTask describe(RdsExportTaskService service, String id) {
|
||||
return service.describeExportTasks(id, null, null, Map.of(), REGION).getFirst();
|
||||
}
|
||||
|
||||
private void runWorkers() {
|
||||
List<Runnable> pending = List.copyOf(workers);
|
||||
workers.clear();
|
||||
pending.forEach(Runnable::run);
|
||||
}
|
||||
|
||||
@Test
|
||||
void startExportTask_isStartingWithTheSnapshotAndRequestRecorded() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
|
||||
ExportTask task = start(service, "appdb-export", List.of("appdb.public.items"));
|
||||
|
||||
assertThat(task.getStatus(), equalTo("STARTING"));
|
||||
assertThat(task.getSourceArn(), equalTo(SNAPSHOT_ARN));
|
||||
assertThat(task.getSourceType(), equalTo("SNAPSHOT"));
|
||||
assertThat(task.getSnapshotTime(), equalTo(SNAPSHOT_TIME));
|
||||
assertThat(task.getTaskStartTime(), equalTo(CLOCK.instant()));
|
||||
assertThat(task.getExportOnly(), contains("appdb.public.items"));
|
||||
assertThat(task.getIamRoleArn(), equalTo("arn:aws:iam::000000000000:role/export"));
|
||||
assertThat(task.getKmsKeyId(), equalTo("alias/export"));
|
||||
assertThat(task.getPercentProgress(), equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTaskMovesThroughInProgressToComplete() {
|
||||
List<String> seen = new ArrayList<>();
|
||||
RdsExportTaskService[] holder = new RdsExportTaskService[1];
|
||||
holder[0] = service((task, source, progress) -> {
|
||||
progress.extracting(50);
|
||||
seen.add(describe(holder[0], task.getExportTaskIdentifier()).getStatus()
|
||||
+ "@" + describe(holder[0], task.getExportTaskIdentifier()).getPercentProgress());
|
||||
return outcome(2_147_483_648L, List.of());
|
||||
});
|
||||
start(holder[0], "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
ExportTask done = describe(holder[0], "appdb-export");
|
||||
assertThat(seen, contains("IN_PROGRESS@50"));
|
||||
assertThat(done.getStatus(), equalTo("COMPLETE"));
|
||||
assertThat(done.getPercentProgress(), equalTo(100));
|
||||
assertThat(done.getTotalExtractedDataInGb(), equalTo(2));
|
||||
assertThat(done.getTaskEndTime(), equalTo(CLOCK.instant()));
|
||||
assertThat(done.getFailureCause(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedExportReportsItsCause() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> {
|
||||
throw new IllegalStateException("psql failed with exit code 1: relation does not exist");
|
||||
});
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
ExportTask failed = describe(service, "appdb-export");
|
||||
assertThat(failed.getStatus(), equalTo("FAILED"));
|
||||
assertThat(failed.getFailureCause(), equalTo("psql failed with exit code 1: relation does not exist"));
|
||||
assertThat(failed.getTaskEndTime(), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void outsideMockModeATaskFailsUntilSnapshotDataCanBeWritten() {
|
||||
RdsExportTaskService service = service(new UnsupportedSnapshotExporter());
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
ExportTask failed = describe(service, "appdb-export");
|
||||
assertThat(failed.getStatus(), equalTo("FAILED"));
|
||||
assertThat(failed.getFailureCause(), equalTo(UnsupportedSnapshotExporter.FAILURE_CAUSE));
|
||||
assertThat(failed.getTaskEndTime(), equalTo(CLOCK.instant()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailureWithoutAMessageStillReportsACause() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> {
|
||||
throw new NullPointerException();
|
||||
});
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
assertThat(describe(service, "appdb-export").getFailureCause(), equalTo("java.lang.NullPointerException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void skippedTablesBecomeTheWarningMessage() {
|
||||
RdsExportTaskService service = service((task, source, progress) ->
|
||||
outcome(10, List.of("The table appdb.public.odd was skipped.")));
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
assertThat(describe(service, "appdb-export").getWarningMessage(),
|
||||
equalTo("The table appdb.public.odd was skipped."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelingStopsTheExportAndEndsCanceled() {
|
||||
List<Boolean> canceledSeen = new ArrayList<>();
|
||||
RdsExportTaskService[] holder = new RdsExportTaskService[1];
|
||||
holder[0] = service((task, source, progress) -> {
|
||||
progress.extracting(10);
|
||||
holder[0].cancelExportTask(task.getExportTaskIdentifier(), REGION);
|
||||
assertThat(describe(holder[0], task.getExportTaskIdentifier()).getStatus(), equalTo("CANCELING"));
|
||||
progress.extracting(60);
|
||||
canceledSeen.add(progress.canceled());
|
||||
return outcome(0, List.of());
|
||||
});
|
||||
start(holder[0], "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
ExportTask canceled = describe(holder[0], "appdb-export");
|
||||
assertThat(canceledSeen, contains(true));
|
||||
assertThat(canceled.getStatus(), equalTo("CANCELED"));
|
||||
assertThat(canceled.getPercentProgress(), equalTo(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFinishedTaskCannotBeCanceled() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
start(service, "appdb-export", List.of());
|
||||
runWorkers();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.cancelExportTask("appdb-export", REGION));
|
||||
|
||||
assertThat(error.getErrorCode(), equalTo("InvalidExportTaskStateFault"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIdentifierCanOnlyBeUsedOnce() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> start(service, "appdb-export", List.of()));
|
||||
|
||||
assertThat(error.getErrorCode(), equalTo("ExportTaskAlreadyExists"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMissingBucketIsRefusedBeforeAnythingRuns() {
|
||||
when(s3Service.bucketExists("exports")).thenReturn(false);
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> start(service, "appdb-export", List.of()));
|
||||
|
||||
assertThat(error.getErrorCode(), equalTo("InvalidS3BucketFault"));
|
||||
assertThat(workers, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBucketInAnotherRegionFailsTheTaskWithoutExporting() {
|
||||
when(s3Service.getBucketRegion("exports")).thenReturn("eu-west-1");
|
||||
SnapshotExporter exporter = mock(SnapshotExporter.class);
|
||||
RdsExportTaskService service = service(exporter);
|
||||
|
||||
assertThat(start(service, "appdb-export", List.of()).getStatus(), equalTo("STARTING"));
|
||||
runWorkers();
|
||||
|
||||
ExportTask failed = describe(service, "appdb-export");
|
||||
assertThat(failed.getStatus(), equalTo("FAILED"));
|
||||
assertThat(failed.getFailureCause(), equalTo("The S3 bucket exports isn't located in the current AWS Region."
|
||||
+ " Review your S3 bucket name and retry the export."));
|
||||
verify(exporter, never()).export(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBucketWithoutALocationConstraintIsInTheDefaultRegion() {
|
||||
when(s3Service.getBucketRegion("exports")).thenReturn(null);
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
|
||||
assertThat(start(service, "appdb-export", List.of()).getStatus(), equalTo("STARTING"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void identifiersFollowTheRdsRules() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
|
||||
for (String invalid : List.of("1export", "export--task", "export-", "export_task", "e".repeat(61))) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> start(service, invalid, List.of()));
|
||||
assertThat(invalid, error.getErrorCode(), equalTo("InvalidParameterValue"));
|
||||
}
|
||||
verify(rdsService, never()).snapshotExportSource(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownTaskIsNotFound() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
|
||||
AwsException describe = assertThrows(AwsException.class,
|
||||
() -> service.describeExportTasks("missing", null, null, Map.of(), REGION));
|
||||
AwsException cancel = assertThrows(AwsException.class, () -> service.cancelExportTask("missing", REGION));
|
||||
|
||||
assertThat(describe.getErrorCode(), equalTo("ExportTaskNotFound"));
|
||||
assertThat(describe.getHttpStatus(), equalTo(404));
|
||||
assertThat(cancel.getErrorCode(), equalTo("ExportTaskNotFound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeFiltersByStatusBucketAndSource() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
start(service, "first", List.of());
|
||||
runWorkers();
|
||||
start(service, "second", List.of());
|
||||
|
||||
assertThat(service.describeExportTasks(null, null, null, Map.of("status", List.of("complete")), REGION)
|
||||
.stream().map(ExportTask::getExportTaskIdentifier).toList(), contains("first"));
|
||||
assertThat(service.describeExportTasks(null, null, null, Map.of("status", List.of("starting")), REGION)
|
||||
.stream().map(ExportTask::getExportTaskIdentifier).toList(), contains("second"));
|
||||
assertThat(service.describeExportTasks(null, SNAPSHOT_ARN, "SNAPSHOT",
|
||||
Map.of("s3-bucket", List.of("exports")), REGION), hasSize(2));
|
||||
assertThat(service.describeExportTasks(null, null, "CLUSTER", Map.of(), REGION), empty());
|
||||
AwsException unknown = assertThrows(AwsException.class, () ->
|
||||
service.describeExportTasks(null, null, null, Map.of("engine", List.of("postgres")), REGION));
|
||||
assertThat(unknown.getErrorCode(), equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownFilterNameIsRefusedEvenWithNoTasks() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
|
||||
AwsException unknown = assertThrows(AwsException.class, () ->
|
||||
service.describeExportTasks(null, null, null, Map.of("staus", List.of("COMPLETE")), REGION));
|
||||
|
||||
assertThat(unknown.getErrorCode(), equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockModeCompletesWithoutExporting() {
|
||||
SnapshotExporter exporter = mock(SnapshotExporter.class);
|
||||
RdsExportTaskService service = new RdsExportTaskService(new InMemoryStorage<>(), rdsService, s3Service,
|
||||
exporter, regionResolver, true, CLOCK, workers::add);
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
assertThat(describe(service, "appdb-export").getStatus(), equalTo("COMPLETE"));
|
||||
verify(exporter, never()).export(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theResponsesKeepTheStatusTheirCallSetWhenTheWorkerFinishesFirst() {
|
||||
RdsExportTaskService service = new RdsExportTaskService(new InMemoryStorage<>(), rdsService, s3Service,
|
||||
(task, source, progress) -> outcome(0, List.of()), regionResolver, true, CLOCK, Runnable::run);
|
||||
|
||||
ExportTask started = start(service, "appdb-export", List.of());
|
||||
ExportTask described = describe(service, "appdb-export");
|
||||
|
||||
assertThat(started.getStatus(), equalTo("STARTING"));
|
||||
assertThat(described.getStatus(), equalTo("COMPLETE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDescribedTaskDoesNotChangeWhenItsWorkerDoes() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
start(service, "appdb-export", List.of());
|
||||
ExportTask described = describe(service, "appdb-export");
|
||||
|
||||
runWorkers();
|
||||
|
||||
assertThat(described.getStatus(), equalTo("STARTING"));
|
||||
assertThat(describe(service, "appdb-export").getStatus(), equalTo("COMPLETE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCancelResponseIsCancelingEvenOnceTheTaskIsCanceled() {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
ExportTask canceling = service.cancelExportTask("appdb-export", REGION);
|
||||
runWorkers();
|
||||
|
||||
assertThat(canceling.getStatus(), equalTo("CANCELING"));
|
||||
assertThat(describe(service, "appdb-export").getStatus(), equalTo("CANCELED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTaskARestartInterruptedIsFailed() {
|
||||
AccountAwareStorageBackend<ExportTask> persisted =
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, ACCOUNT);
|
||||
ExportTask running = new ExportTask();
|
||||
running.setExportTaskIdentifier("appdb-export");
|
||||
running.setStatus(ExportTask.STATUS_IN_PROGRESS);
|
||||
running.setTaskStartTime(SNAPSHOT_TIME);
|
||||
persisted.putForAccount(ACCOUNT, "export-task::us-east-1::appdb-export", running);
|
||||
|
||||
SnapshotExporter exporter = mock(SnapshotExporter.class);
|
||||
|
||||
RdsExportTaskService service = new RdsExportTaskService(persisted, rdsService, s3Service, exporter,
|
||||
regionResolver, false, CLOCK, workers::add);
|
||||
|
||||
ExportTask recovered = describe(service, "appdb-export");
|
||||
assertThat(recovered.getStatus(), equalTo("FAILED"));
|
||||
assertThat(recovered.getFailureCause(), equalTo(RdsExportTaskService.RESTART_FAILURE_CAUSE));
|
||||
verify(exporter).discard(argThat(task -> "appdb-export".equals(task.getExportTaskIdentifier())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTaskBeingCanceledWhenARestartInterruptedItIsCanceled() {
|
||||
AccountAwareStorageBackend<ExportTask> persisted =
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, ACCOUNT);
|
||||
ExportTask canceling = new ExportTask();
|
||||
canceling.setExportTaskIdentifier("appdb-export");
|
||||
canceling.setStatus(ExportTask.STATUS_CANCELING);
|
||||
canceling.setTaskStartTime(SNAPSHOT_TIME);
|
||||
persisted.putForAccount(ACCOUNT, "export-task::us-east-1::appdb-export", canceling);
|
||||
SnapshotExporter exporter = mock(SnapshotExporter.class);
|
||||
|
||||
RdsExportTaskService service = new RdsExportTaskService(persisted, rdsService, s3Service, exporter,
|
||||
regionResolver, false, CLOCK, workers::add);
|
||||
|
||||
ExportTask recovered = describe(service, "appdb-export");
|
||||
assertThat(recovered.getStatus(), equalTo("CANCELED"));
|
||||
assertThat(recovered.getFailureCause(), nullValue());
|
||||
verify(exporter).discard(argThat(task -> "appdb-export".equals(task.getExportTaskIdentifier())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTaskCanceledBeforeItsWorkerStartsNeverExports() {
|
||||
SnapshotExporter exporter = mock(SnapshotExporter.class);
|
||||
RdsExportTaskService service = service(exporter);
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
String answered = service.cancelExportTask("appdb-export", REGION).getStatus();
|
||||
runWorkers();
|
||||
|
||||
assertThat(answered, equalTo("CANCELING"));
|
||||
assertThat(describe(service, "appdb-export").getStatus(), equalTo("CANCELED"));
|
||||
verify(exporter, never()).export(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theSameIdentifierInTwoRegionsGetsItsOwnRuntime() {
|
||||
String otherArn = "arn:aws:rds:eu-west-1:000000000000:snapshot:appdb-snap";
|
||||
when(rdsService.snapshotExportSource(otherArn, "eu-west-1")).thenReturn(new SnapshotExportSource(
|
||||
otherArn, "postgres:16-alpine", "master", "secret", SNAPSHOT_TIME, "-- dump"));
|
||||
when(s3Service.bucketExists("exports-west")).thenReturn(true);
|
||||
when(s3Service.getBucketRegion("exports-west")).thenReturn("eu-west-1");
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
|
||||
ExportTask east = start(service, "appdb-export", List.of());
|
||||
ExportTask west = service.startExportTask("appdb-export", otherArn, "exports-west", null,
|
||||
"arn:aws:iam::000000000000:role/export", "alias/export", List.of(), "eu-west-1");
|
||||
|
||||
assertThat(east.getRuntimeArn(), equalTo("arn:aws:rds:us-east-1:000000000000:export-task:appdb-export"));
|
||||
assertThat(west.getRuntimeArn(), equalTo("arn:aws:rds:eu-west-1:000000000000:export-task:appdb-export"));
|
||||
assertThat(east.getRuntimeVolumeId(), not(equalTo(west.getRuntimeVolumeId())));
|
||||
assertThat(service.describeExportTasks(null, null, null, Map.of(), "eu-west-1"), hasSize(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStartDuringAResetWaitsForItToFinish() throws Exception {
|
||||
RdsExportTaskService service = service((task, source, progress) -> outcome(0, List.of()));
|
||||
service.beforeReset();
|
||||
List<ExportTask> admitted = new CopyOnWriteArrayList<>();
|
||||
Thread caller = Thread.ofVirtual().start(() -> admitted.add(start(service, "appdb-export", List.of())));
|
||||
|
||||
caller.join(300);
|
||||
assertThat("the start waits while the reset runs", admitted, empty());
|
||||
service.clear();
|
||||
service.afterReset();
|
||||
caller.join(5_000);
|
||||
|
||||
assertThat(admitted, hasSize(1));
|
||||
assertThat(describe(service, "appdb-export").getStatus(), equalTo("STARTING"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWorkerQueuedBeforeAResetDoesNotExport() {
|
||||
SnapshotExporter exporter = mock(SnapshotExporter.class);
|
||||
RdsExportTaskService service = service(exporter);
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
service.beforeReset();
|
||||
service.afterReset();
|
||||
runWorkers();
|
||||
|
||||
verify(exporter, never()).export(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWorkerThatOutlivesAResetLeavesTheNewTaskWithItsIdentifierAlone() {
|
||||
RdsExportTaskService[] holder = new RdsExportTaskService[1];
|
||||
holder[0] = service((task, source, progress) -> {
|
||||
holder[0].beforeReset();
|
||||
holder[0].clear();
|
||||
holder[0].afterReset();
|
||||
start(holder[0], task.getExportTaskIdentifier(), List.of());
|
||||
progress.extracting(50);
|
||||
throw new IllegalStateException("the restore container was removed by the reset");
|
||||
});
|
||||
start(holder[0], "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
ExportTask restarted = describe(holder[0], "appdb-export");
|
||||
assertThat(restarted.getStatus(), equalTo("STARTING"));
|
||||
assertThat(restarted.getPercentProgress(), equalTo(0));
|
||||
assertThat(restarted.getFailureCause(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theTaskEndsAtTheTimeItsExportRecorded() {
|
||||
Instant finished = Instant.parse("2026-10-13T09:05:00Z");
|
||||
RdsExportTaskService service = service((task, source, progress) ->
|
||||
new SnapshotExporter.Outcome(0, List.of(), finished));
|
||||
start(service, "appdb-export", List.of());
|
||||
|
||||
runWorkers();
|
||||
|
||||
assertThat(describe(service, "appdb-export").getTaskEndTime(), equalTo(finished));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailingCleanupAtStartupDoesNotStopRecovery() {
|
||||
AccountAwareStorageBackend<ExportTask> persisted =
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, ACCOUNT);
|
||||
for (String id : List.of("first", "second")) {
|
||||
ExportTask running = new ExportTask();
|
||||
running.setExportTaskIdentifier(id);
|
||||
running.setStatus(ExportTask.STATUS_STARTING);
|
||||
running.setTaskStartTime(SNAPSHOT_TIME);
|
||||
persisted.putForAccount(ACCOUNT, "export-task::us-east-1::" + id, running);
|
||||
}
|
||||
SnapshotExporter exporter = mock(SnapshotExporter.class);
|
||||
doThrow(new IllegalStateException("Docker is not reachable")).when(exporter)
|
||||
.discard(argThat(task -> "first".equals(task.getExportTaskIdentifier())));
|
||||
|
||||
RdsExportTaskService service = new RdsExportTaskService(persisted, rdsService, s3Service, exporter,
|
||||
regionResolver, false, CLOCK, workers::add);
|
||||
|
||||
assertThat(describe(service, "first").getStatus(), equalTo("FAILED"));
|
||||
assertThat(describe(service, "second").getStatus(), equalTo("FAILED"));
|
||||
verify(exporter).discard(argThat(task -> "second".equals(task.getExportTaskIdentifier())));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user