feat(redshiftserverless): add snapshot and restore operations (#5184)

* feat(redshiftserverless): add snapshot and restore operations

Adds CreateSnapshot, GetSnapshot (by name or ARN), ListSnapshots (filterable
by namespace), DeleteSnapshot and RestoreFromSnapshot on top of the existing
workgroup support. A snapshot is AVAILABLE as soon as it is created, so a
snapshot created in a standby region stands in for a cross-region copy.
RestoreFromSnapshot restores into an existing namespace and workgroup and
never creates either.

* docs(redshiftserverless): document snapshot and restore actions

* docs(redshiftserverless): update service matrix operation count

* fix(redshiftserverless): validate snapshot restore and ARN lookup

Reject a RestoreFromSnapshot workgroup that belongs to another namespace,
resolve snapshotArn only for this account and Region, and register the
snapshot model for reflection so persisted snapshots survive a native restart.

* fix(redshiftserverless): harden snapshot restore and add lifecycle tests

Validate namespaceName and snapshot names, accept snapshotArn on
RestoreFromSnapshot, read the snapshot once during restore, and cover the
handler with an integration lifecycle test and a Java SDK compatibility test.

* feat(redshiftserverless): dump and restore snapshot data and model retention, tags and filters

CreateSnapshot dumps a running workgroup with pg_dump and RestoreFromSnapshot
empties the public schema and replays the dump, reusing the provisioned
Redshift container manager. Snapshots now report adminUsername, kmsKeyId and
retention, accept tags (taggable by ARN), ListSnapshots filters by
namespaceArn, ownerAccount and a time window, and RestoreFromSnapshot rejects
snapshotName and snapshotArn together.

* fix(redshiftserverless): make snapshot restore atomic and strict

Dump the current contents before a restore and put them back when the replay
fails, drop every user schema rather than only public, run psql with
ON_ERROR_STOP so a failing statement fails the restore, and fail a restore
whose recorded dump file is missing.

* fix(redshiftserverless): keep the catalog bootstrap schema out of snapshot restore

The reset dropped floci_internal and with it the pg_catalog svv_external_*
views, so the strict replay failed. Leave that schema out of the reset and
out of the dump.

* fix(redshiftserverless): drop large objects when resetting before a restore

* refactor(redshift): share container lookup and defaults in the container manager

* docs(redshiftserverless): document the bootstrap schema left out of snapshots

* style(redshiftserverless): order imports in the runtime test

---------

Co-authored-by: yarammanasa2001-max <271440416+yarammanasa2001-max@users.noreply.github.com>
This commit is contained in:
yarammanasa2001-maxandyarammanasa2001-max authored and GitHub committed 2026-10-07 17:08:39 -04:00
1 parent 43c616d757
commit 9934b39a42
12 files changed
+1076 -30

No files matched your search

@@ -8,15 +8,20 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.file.Files;
import java.nio.file.Path;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
@QuarkusTest
@@ -187,6 +192,83 @@ class RedshiftServerlessIntegrationTest {
.body("__type", equalTo("ResourceNotFoundException"));
}
@Test
void snapshotLifecycleIsVisibleThroughSeparateReads() {
doAnswer(invocation -> {
Files.writeString(invocation.getArgument(5, Path.class), "-- dump");
return null;
}).when(runtime).takeSnapshot(any(), any(), any(), any(), any(), any());
call("CreateNamespace", "{\"namespaceName\":\"snap-life-ns\",\"adminUsername\":\"admin\"}").statusCode(200);
call("CreateWorkgroup", "{\"workgroupName\":\"snap-life-wg\",\"namespaceName\":\"snap-life-ns\"}")
.statusCode(200);
String snapshotArn = call("CreateSnapshot",
"{\"snapshotName\":\"snap-life\",\"namespaceName\":\"snap-life-ns\",\"retentionPeriod\":7,"
+ "\"tags\":[{\"key\":\"env\",\"value\":\"dev\"}]}")
.statusCode(200)
.body("snapshot.snapshotName", equalTo("snap-life"))
.body("snapshot.snapshotRetentionPeriod", equalTo(7))
.body("snapshot.snapshotRemainingDays", equalTo(7))
.body("snapshot.adminUsername", equalTo("admin"))
.body("snapshot.kmsKeyId", equalTo("AWS_OWNED_KMS_KEY"))
.body("snapshot.namespaceName", equalTo("snap-life-ns"))
.body("snapshot.status", equalTo("AVAILABLE"))
.body("snapshot.snapshotCreateTime", matchesPattern("\\d{4}-\\d{2}-\\d{2}T.*Z"))
.extract().path("snapshot.snapshotArn");
call("CreateSnapshot", "{\"snapshotName\":\"snap-life\",\"namespaceName\":\"snap-life-ns\"}")
.statusCode(409)
.body("__type", equalTo("ConflictException"));
call("GetSnapshot", "{\"snapshotName\":\"snap-life\"}")
.statusCode(200)
.body("snapshot.snapshotArn", equalTo(snapshotArn));
call("GetSnapshot", "{\"snapshotArn\":\"" + snapshotArn + "\"}")
.statusCode(200)
.body("snapshot.snapshotName", equalTo("snap-life"));
call("GetSnapshot", "{\"snapshotArn\":\"arn:aws:redshift-serverless:us-east-1:999999999999:snapshot/snap-life\"}")
.statusCode(400)
.body("__type", equalTo("ValidationException"));
call("ListSnapshots", "{\"namespaceName\":\"snap-life-ns\",\"maxResults\":1}")
.statusCode(200)
.body("snapshots.snapshotName", hasItem("snap-life"));
call("ListTagsForResource", "{\"resourceArn\":\"" + snapshotArn + "\"}")
.statusCode(200)
.body("tags.key", hasItem("env"));
call("ListSnapshots", "{\"ownerAccount\":\"999999999999\"}")
.statusCode(200)
.body("snapshots", hasSize(0));
call("ListSnapshots", "{\"startTime\":4102444800}")
.statusCode(200)
.body("snapshots", hasSize(0));
call("RestoreFromSnapshot", "{\"namespaceName\":\"snap-life-ns\",\"workgroupName\":\"snap-life-wg\","
+ "\"snapshotName\":\"snap-life\",\"snapshotArn\":\"" + snapshotArn + "\"}")
.statusCode(400)
.body("__type", equalTo("ValidationException"));
call("ListSnapshots", "{\"namespaceName\":\"no-such-ns\"}")
.statusCode(200)
.body("snapshots", hasSize(0));
call("RestoreFromSnapshot", "{\"namespaceName\":\"snap-life-ns\",\"workgroupName\":\"snap-life-wg\","
+ "\"snapshotArn\":\"" + snapshotArn + "\"}")
.statusCode(200)
.body("snapshotName", equalTo("snap-life"))
.body("namespace.namespaceName", equalTo("snap-life-ns"))
.body("namespace.status", equalTo("AVAILABLE"))
.body("ownerAccount", matchesPattern("\\d{12}"));
call("RestoreFromSnapshot", "{\"workgroupName\":\"snap-life-wg\",\"snapshotName\":\"snap-life\"}")
.statusCode(400)
.body("__type", equalTo("ValidationException"));
call("DeleteSnapshot", "{\"snapshotName\":\"snap-life\"}")
.statusCode(200)
.body("snapshot.snapshotName", equalTo("snap-life"));
call("GetSnapshot", "{\"snapshotName\":\"snap-life\"}")
.statusCode(404)
.body("__type", equalTo("ResourceNotFoundException"));
}
@Test
void workgroupLifecycleIsVisibleThroughSeparateReads() {
call("CreateNamespace", "{\"namespaceName\":\"wg-life-ns\",\"adminUsername\":\"admin\"}").statusCode(200);
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -74,8 +75,10 @@ class RedshiftServerlessJsonHandlerTest {
Instant.ofEpochSecond(1_800_000_000L), List.of()));
iamDbUserResolver = mock(RedshiftIamDbUserResolver.class);
when(iamDbUserResolver.resolveDbUser("AUTH")).thenReturn("IAM:alice");
EmulatorConfig config = mock(EmulatorConfig.class, org.mockito.Mockito.RETURNS_DEEP_STUBS);
when(config.storage().persistentPath()).thenReturn(System.getProperty("java.io.tmpdir"));
handler = new RedshiftServerlessJsonHandler(
new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime), mapper,
new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime, config), mapper,
iamDbUserResolver);
}
@@ -287,7 +290,7 @@ class RedshiftServerlessJsonHandlerTest {
@Test
void anUnknownActionIsReportedAsUnknownOperation() {
Response response = handler.handle("CreateSnapshot", mapper.createObjectNode(), REGION);
Response response = handler.handle("CreateScheduledAction", mapper.createObjectNode(), REGION);
assertEquals(400, response.getStatus());
assertEquals("UnknownOperationException", errorType(response));
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
import io.github.hectorvent.floci.services.redshift.RedshiftCredentialBroker;
import io.github.hectorvent.floci.services.redshift.TempCredential;
@@ -11,6 +12,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
@@ -25,6 +27,7 @@ import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -70,6 +73,41 @@ class RedshiftServerlessRuntimeTest {
verify(containers, never()).adoptOrStart(any(), any(), any(), any(), any());
}
@Test
void takeSnapshotDumpsTheWorkgroupBackend() {
Path dump = Path.of("/tmp/dump.sql");
runtime.takeSnapshot(ACCOUNT, REGION, "my-wg", "root", "analytics", dump);
verify(containers).takeSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", dump, true);
}
@Test
void restoreSnapshotEmptiesTheSchemasBeforeReplayingTheDumpAndStopsOnError() {
Path dump = Path.of("/tmp/dump.sql");
runtime.restoreSnapshot(ACCOUNT, REGION, "my-wg", "root", "analytics", dump);
InOrder order = inOrder(containers);
order.verify(containers).takeSnapshot(eq(ACCOUNT), eq(BACKEND_ID), eq("root"), eq("analytics"), any(Path.class), eq(true));
order.verify(containers).resetUserSchemas(ACCOUNT, BACKEND_ID, "root", "analytics");
order.verify(containers).restoreSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", dump, true);
}
@Test
void aFailedReplayPutsTheEarlierContentsBack() {
Path dump = Path.of("/tmp/dump.sql");
doThrow(new AwsException("InternalFailure", "bad dump", 500)).when(containers)
.restoreSnapshot(ACCOUNT, BACKEND_ID, "root", "analytics", dump, true);
assertThrows(AwsException.class,
() -> runtime.restoreSnapshot(ACCOUNT, REGION, "my-wg", "root", "analytics", dump));
verify(containers, times(2)).resetUserSchemas(ACCOUNT, BACKEND_ID, "root", "analytics");
verify(containers, times(2)).restoreSnapshot(eq(ACCOUNT), eq(BACKEND_ID), eq("root"), eq("analytics"),
any(Path.class), eq(true));
}
@Test
void adoptReattachesToASurvivingContainer() {
when(containers.adoptOrStart(ACCOUNT, BACKEND_ID, "root", "Secret123", "analytics")).thenReturn(handle());
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -11,11 +12,15 @@ import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
import io.github.hectorvent.floci.services.redshiftserverless.model.PricePerformanceTarget;
import io.github.hectorvent.floci.services.redshiftserverless.model.RedshiftServerlessSnapshot;
import io.github.hectorvent.floci.services.redshiftserverless.model.Workgroup;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.List;
import java.util.Map;
@@ -24,6 +29,7 @@ import java.util.UUID;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -34,6 +40,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -52,6 +59,9 @@ class RedshiftServerlessServiceTest {
private RedshiftServerlessRuntime runtime;
private int nextPort;
@TempDir
Path dumpDir;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
@@ -67,7 +77,11 @@ class RedshiftServerlessServiceTest {
when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-workgroups.json"),
any(TypeReference.class))).thenReturn((AccountAwareStorageBackend) workgroupStore);
when(storageFactory.create(eq("redshiftserverless"), eq("redshiftserverless-snapshots.json"),
any(TypeReference.class))).thenReturn(AccountAwareStorageBackend.inMemory(ACCOUNT_ID));
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
when(regionResolver.buildArn(eq("redshift-serverless"), any(String.class), any(String.class)))
.thenAnswer(invocation -> "arn:aws:redshift-serverless:"
+ invocation.getArgument(1, String.class) + ":" + ACCOUNT_ID + ":"
@@ -80,7 +94,9 @@ class RedshiftServerlessServiceTest {
when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
.thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
service = new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime);
EmulatorConfig config = mock(EmulatorConfig.class, org.mockito.Mockito.RETURNS_DEEP_STUBS);
when(config.storage().persistentPath()).thenReturn(dumpDir.toString());
service = new RedshiftServerlessService(storageFactory, regionResolver, endpoints, runtime, config);
}
@Test
@@ -791,6 +807,218 @@ class RedshiftServerlessServiceTest {
verify(runtime).stop(ACCOUNT_ID, REGION, "clr-wg");
}
@Test
void listSnapshotsFiltersByNamespace() {
create("snap-a");
create("snap-b");
RedshiftServerlessSnapshot snapshot = service.createSnapshot("snap-one", "snap-a", null, null, REGION);
service.createSnapshot("snap-two", "snap-b", null, null, REGION);
assertEquals("AVAILABLE", snapshot.getStatus());
assertEquals(ACCOUNT_ID, snapshot.getOwnerAccount());
assertEquals(List.of("snap-one"), service.listSnapshots("snap-a", null, null, null, null, REGION, null, null).items().stream()
.map(RedshiftServerlessSnapshot::getSnapshotName).toList());
assertEquals(2, service.listSnapshots(null, null, null, null, null, REGION, null, null).items().size());
AwsException conflict = assertThrows(AwsException.class,
() -> service.createSnapshot("snap-one", "snap-a", null, null, REGION));
assertEquals("ConflictException", conflict.getErrorCode());
}
@Test
void restoreRequiresAnExistingNamespaceWorkgroupAndSnapshot() {
create("restore-ns");
service.createSnapshot("restore-snap", "restore-ns", null, null, REGION);
AwsException noWorkgroup = assertThrows(AwsException.class,
() -> service.restoreFromSnapshot("restore-ns", "restore-wg", "restore-snap", null, REGION));
assertEquals("ResourceNotFoundException", noWorkgroup.getErrorCode());
createWorkgroup("restore-wg", "restore-ns");
RedshiftServerlessService.RestoreResult restored =
service.restoreFromSnapshot("restore-ns", "restore-wg", "restore-snap", null, REGION);
assertEquals("AVAILABLE", restored.namespace().getStatus());
assertEquals("restore-snap", restored.snapshot().getSnapshotName());
AwsException noSnapshot = assertThrows(AwsException.class,
() -> service.restoreFromSnapshot("restore-ns", "restore-wg", "missing-snap", null, REGION));
assertEquals("ResourceNotFoundException", noSnapshot.getErrorCode());
}
@Test
void restoreRejectsAWorkgroupOfAnotherNamespace() {
create("rest-a");
create("rest-b");
service.createSnapshot("rest-snap", "rest-a", null, null, REGION);
createWorkgroup("rest-b-wg", "rest-b");
AwsException mismatch = assertThrows(AwsException.class,
() -> service.restoreFromSnapshot("rest-a", "rest-b-wg", "rest-snap", null, REGION));
assertEquals("ValidationException", mismatch.getErrorCode());
}
@Test
void snapshotNameFromArnRejectsAnArnOutsideThisAccountAndRegion() {
create("arn-ns");
RedshiftServerlessSnapshot snapshot = service.createSnapshot("arn-snap", "arn-ns", null, null, REGION);
assertEquals("arn-snap", service.snapshotNameFromArn(snapshot.getSnapshotArn(), REGION));
for (String foreign : List.of(
"arn:aws:redshift-serverless:eu-west-1:" + ACCOUNT_ID + ":snapshot/arn-snap",
"arn:aws:redshift-serverless:us-east-1:999999999999:snapshot/arn-snap",
"arn:aws:redshift-serverless:us-east-1:" + ACCOUNT_ID + ":workgroup/arn-snap")) {
AwsException rejected = assertThrows(AwsException.class,
() -> service.snapshotNameFromArn(foreign, REGION));
assertEquals("ValidationException", rejected.getErrorCode());
}
}
@Test
void restoreWithoutANamespaceNameIsAValidationError() {
create("nullns-ns");
service.createSnapshot("nullns-snap", "nullns-ns", null, null, REGION);
createWorkgroup("nullns-wg", "nullns-ns");
AwsException missing = assertThrows(AwsException.class,
() -> service.restoreFromSnapshot(null, "nullns-wg", "nullns-snap", null, REGION));
assertEquals("ValidationException", missing.getErrorCode());
}
@Test
void snapshotNamesAreValidated() {
create("names-ns");
for (String bad : new String[] {null, "", "ab", "has/slash", "has::colons", "UPPER"}) {
AwsException rejected = assertThrows(AwsException.class,
() -> service.createSnapshot(bad, "names-ns", null, null, REGION));
assertEquals("ValidationException", rejected.getErrorCode());
}
AwsException missing = assertThrows(AwsException.class, () -> service.getSnapshot(null, REGION));
assertEquals("ValidationException", missing.getErrorCode());
assertThrows(AwsException.class, () -> service.deleteSnapshot(null, REGION));
}
@Test
void createSnapshotDumpsALiveWorkgroupAndRestoreReplaysIt() throws Exception {
create("dump-ns");
createWorkgroup("dump-wg", "dump-ns");
doAnswer(invocation -> {
Files.writeString(invocation.getArgument(5, Path.class), "-- dump");
return null;
}).when(runtime).takeSnapshot(any(), any(), any(), any(), any(), any());
RedshiftServerlessSnapshot snapshot = service.createSnapshot("dump-snap", "dump-ns", null, null, REGION);
assertTrue(Files.exists(Path.of(snapshot.getSqlDump())));
service.restoreFromSnapshot("dump-ns", "dump-wg", "dump-snap", null, REGION);
verify(runtime).restoreSnapshot(eq(ACCOUNT_ID), eq(REGION), eq("dump-wg"), any(), eq("dev"),
eq(Path.of(snapshot.getSqlDump())));
service.deleteSnapshot("dump-snap", REGION);
assertFalse(Files.exists(Path.of(snapshot.getSqlDump())));
}
@Test
void restoreFailsWhenTheDumpFileIsGone() throws Exception {
create("gone-ns");
createWorkgroup("gone-wg", "gone-ns");
doAnswer(invocation -> {
Files.writeString(invocation.getArgument(5, Path.class), "-- dump");
return null;
}).when(runtime).takeSnapshot(any(), any(), any(), any(), any(), any());
RedshiftServerlessSnapshot snapshot = service.createSnapshot("gone-snap", "gone-ns", null, null, REGION);
Files.delete(Path.of(snapshot.getSqlDump()));
AwsException missing = assertThrows(AwsException.class,
() -> service.restoreFromSnapshot("gone-ns", "gone-wg", "gone-snap", null, REGION));
assertEquals("InternalServerException", missing.getErrorCode());
verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any());
}
@Test
void createSnapshotWithoutALiveWorkgroupIsMetadataOnly() {
create("meta-ns");
createWorkgroup("meta-wg", "meta-ns");
Workgroup stopped = new Workgroup(service.getWorkgroup("meta-wg", REGION));
stopped.setRuntimeHost(null);
stopped.setRuntimePort(0);
workgroupStore.put(REGION + "::meta-wg", stopped);
RedshiftServerlessSnapshot snapshot = service.createSnapshot("meta-snap", "meta-ns", null, null, REGION);
service.restoreFromSnapshot("meta-ns", "meta-wg", "meta-snap", null, REGION);
assertNull(snapshot.getSqlDump());
verify(runtime, never()).takeSnapshot(any(), any(), any(), any(), any(), any());
verify(runtime, never()).restoreSnapshot(any(), any(), any(), any(), any(), any());
}
@Test
void snapshotCarriesRetentionNamespaceDetailsAndTags() {
create("meta-fields-ns");
RedshiftServerlessSnapshot snapshot = service.createSnapshot("fields-snap", "meta-fields-ns", 30,
Map.of("env", "dev"), REGION);
assertEquals(30, snapshot.getRetentionPeriod());
assertEquals("admin", snapshot.getAdminUsername());
assertEquals("AWS_OWNED_KMS_KEY", snapshot.getKmsKeyId());
assertEquals(Map.of("env", "dev"), service.listTagsForResource(snapshot.getSnapshotArn(), REGION));
service.tagResource(snapshot.getSnapshotArn(), Map.of("team", "data"), REGION);
service.untagResource(snapshot.getSnapshotArn(), List.of("env"), REGION);
assertEquals(Map.of("team", "data"), service.listTagsForResource(snapshot.getSnapshotArn(), REGION));
assertNull(service.createSnapshot("indef-snap", "meta-fields-ns", -1, null, REGION).getRetentionPeriod());
for (int bad : new int[] {0, 3654, -2}) {
AwsException rejected = assertThrows(AwsException.class,
() -> service.createSnapshot("bad-ret-snap", "meta-fields-ns", bad, null, REGION));
assertEquals("ValidationException", rejected.getErrorCode());
}
}
@Test
void listSnapshotsAppliesEveryFilter() throws Exception {
create("filt-a");
create("filt-b");
RedshiftServerlessSnapshot first = service.createSnapshot("filt-one", "filt-a", null, null, REGION);
service.createSnapshot("filt-two", "filt-b", null, null, REGION);
Instant created = first.getSnapshotCreateTime();
assertEquals(1, service.listSnapshots(null, first.getNamespaceArn(), null, null, null, REGION, null, null)
.items().size());
assertEquals(2, service.listSnapshots(null, null, ACCOUNT_ID, null, null, REGION, null, null).items().size());
assertEquals(0, service.listSnapshots(null, null, "999999999999", null, null, REGION, null, null)
.items().size());
assertEquals(0, service.listSnapshots(null, null, null, created.plusSeconds(3600), null, REGION, null, null)
.items().size());
assertEquals(0, service.listSnapshots(null, null, null, null, created.minusSeconds(3600), REGION, null, null)
.items().size());
assertEquals(2, service.listSnapshots(null, null, null, created.minusSeconds(3600),
created.plusSeconds(3600), REGION, null, null).items().size());
}
@Test
void restoreRejectsASnapshotOwnedByAnotherAccount() {
create("owner-ns");
service.createSnapshot("owner-snap", "owner-ns", null, null, REGION);
createWorkgroup("owner-wg", "owner-ns");
AwsException missing = assertThrows(AwsException.class,
() -> service.restoreFromSnapshot("owner-ns", "owner-wg", "owner-snap", "999999999999", REGION));
assertEquals("ResourceNotFoundException", missing.getErrorCode());
service.restoreFromSnapshot("owner-ns", "owner-wg", "owner-snap", ACCOUNT_ID, REGION);
}
@Test
void deleteSnapshotRemovesIt() {
create("snapdel-ns");
service.createSnapshot("snapdel", "snapdel-ns", null, null, REGION);
service.deleteSnapshot("snapdel", REGION);
AwsException missing = assertThrows(AwsException.class, () -> service.getSnapshot("snapdel", REGION));
assertEquals("ResourceNotFoundException", missing.getErrorCode());
}
private Namespace create(String namespaceName) {
return service.createNamespace(namespaceName, "admin", null, null, null, null, null, Map.of(), REGION);
}