mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
This commit is contained in:
1 parent
024162483c
commit
7331a6b1ce
8 files changed
+586
-20
No files matched your search
@@ -0,0 +1,80 @@
|
||||
package io.floci.aws.services.rds;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.services.eventbridge.EventBridgeService;
|
||||
import io.floci.aws.testutil.LogCapture;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
class RdsEventPublisherTest {
|
||||
|
||||
@Test
|
||||
void reentrantDeliveryPreservesSnapshotsAndDrainsWithoutRecursing() {
|
||||
EventBridgeService eventBridge = mock(EventBridgeService.class);
|
||||
RdsEventPublisher publisher = new RdsEventPublisher(eventBridge, new ObjectMapper());
|
||||
String arn = "arn:aws-cn:rds:cn-north-1:111122223333:db:one";
|
||||
List<String> details = new ArrayList<>();
|
||||
AtomicBoolean delivering = new AtomicBoolean();
|
||||
doAnswer(invocation -> {
|
||||
assertTrue(delivering.compareAndSet(false, true));
|
||||
details.add(invocation.getArgument(2));
|
||||
assertEquals(arn, invocation.getArgument(3));
|
||||
assertEquals("cn-north-1", invocation.getArgument(4));
|
||||
if (details.size() == 1) {
|
||||
publisher.queue(arn, "one", false, true);
|
||||
publisher.publishQueued();
|
||||
assertEquals(1, details.size());
|
||||
}
|
||||
delivering.set(false);
|
||||
return null;
|
||||
}).when(eventBridge).publishServiceEvent(anyString(), anyString(), anyString(), anyString(), anyString());
|
||||
publisher.queue(arn, "one", false, false);
|
||||
verifyNoInteractions(eventBridge);
|
||||
publisher.publishQueued();
|
||||
publisher.publishQueued();
|
||||
assertEquals(2, details.size());
|
||||
assertTrue(details.getFirst().contains("RDS-EVENT-0087"));
|
||||
assertTrue(details.getLast().contains("RDS-EVENT-0088"));
|
||||
assertFalse(delivering.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeliveryFailureDoesNotLoseLaterEventsOrEscapeToTheRdsCaller() {
|
||||
EventBridgeService eventBridge = mock(EventBridgeService.class);
|
||||
RdsEventPublisher publisher = new RdsEventPublisher(eventBridge, new ObjectMapper());
|
||||
List<String> details = new ArrayList<>();
|
||||
IllegalStateException failure = new IllegalStateException("target unavailable");
|
||||
doAnswer(invocation -> {
|
||||
details.add(invocation.getArgument(2));
|
||||
if (details.size() == 1) {
|
||||
throw failure;
|
||||
}
|
||||
return null;
|
||||
}).when(eventBridge).publishServiceEvent(anyString(), anyString(), anyString(), anyString(), anyString());
|
||||
String arn = "arn:aws:rds:eu-west-2:111122223333:cluster:one";
|
||||
publisher.queue(arn, "one", true, false);
|
||||
publisher.queue(arn, "one", true, true);
|
||||
List<LogRecord> records = LogCapture.capture(RdsEventPublisher.class, publisher::publishQueued);
|
||||
assertEquals(2, details.size());
|
||||
assertTrue(details.getFirst().contains("RDS-EVENT-0150"));
|
||||
assertTrue(details.getLast().contains("RDS-EVENT-0151"));
|
||||
assertEquals(1, records.size());
|
||||
assertEquals(Level.WARNING.intValue(), records.getFirst().getLevel().intValue());
|
||||
assertSame(failure, records.getFirst().getThrown());
|
||||
assertTrue(LogCapture.formatted(records.getFirst()).contains(arn));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package io.floci.aws.services.rds;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.EventRuleQueue;
|
||||
import io.floci.aws.testing.RdsMockProfile;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.rds.RdsClient;
|
||||
import software.amazon.awssdk.services.rds.model.RdsException;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** SDK control-plane calls through an EventBridge rule to SQS; only database containers are mocked. */
|
||||
@QuarkusTest
|
||||
@TestProfile(RdsMockProfile.class)
|
||||
class RdsLifecycleEventIntegrationTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void standaloneStopAndStartEmitNativeEventsAndRejectedCallsDoNot() throws Exception {
|
||||
String account = "111122223333";
|
||||
Region region = Region.US_WEST_2;
|
||||
String id = "rds-events-" + Long.toUnsignedString(System.nanoTime(), 36);
|
||||
try (RdsClient rds = AwsTestClients.create(RdsClient.builder(), account, region);
|
||||
EventRuleQueue queue = EventRuleQueue.route(account, region.id(), id, "{\"source\":[\"aws.rds\"]}")) {
|
||||
String arn = rds.createDBInstance(r -> r.dbInstanceIdentifier(id).engine("postgres")
|
||||
.masterUsername("admin").masterUserPassword("password123")
|
||||
.dbInstanceClass("db.t3.micro").allocatedStorage(5)).dbInstance().dbInstanceArn();
|
||||
try {
|
||||
assertThrows(RdsException.class, () -> rds.startDBInstance(r -> r.dbInstanceIdentifier(id)));
|
||||
assertTrue(queue.drain().isEmpty());
|
||||
assertEquals("stopping", rds.stopDBInstance(r -> r.dbInstanceIdentifier(id))
|
||||
.dbInstance().dbInstanceStatus());
|
||||
assertEquals("starting", rds.startDBInstance(r -> r.dbInstanceIdentifier(id))
|
||||
.dbInstance().dbInstanceStatus());
|
||||
List<JsonNode> events = queue.drain();
|
||||
assertEquals(2, events.size());
|
||||
assertEvent(events.get(0), account, region, arn, id, false, false);
|
||||
assertEvent(events.get(1), account, region, arn, id, false, true);
|
||||
assertTrue(queue.drain().isEmpty());
|
||||
} finally {
|
||||
rds.deleteDBInstance(r -> r.dbInstanceIdentifier(id).skipFinalSnapshot(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterTransitionsNotifyTheClusterAndItsMemberInTheirOwnScope() throws Exception {
|
||||
String account = "444455556666";
|
||||
Region region = Region.CN_NORTH_1;
|
||||
String cluster = "rds-events-" + Long.toUnsignedString(System.nanoTime(), 36);
|
||||
String member = cluster + "-member";
|
||||
String pattern = "{\"source\":[\"aws.rds\"]}";
|
||||
try (RdsClient rds = AwsTestClients.create(RdsClient.builder(), account, region);
|
||||
EventRuleQueue queue = EventRuleQueue.route(account, region.id(), cluster, pattern);
|
||||
EventRuleQueue foreignAccount = EventRuleQueue.route("777788889999", region.id(), cluster, pattern);
|
||||
EventRuleQueue foreignRegion = EventRuleQueue.route(account, Region.CN_NORTHWEST_1.id(), cluster, pattern)) {
|
||||
String clusterArn = rds.createDBCluster(r -> r.dbClusterIdentifier(cluster)
|
||||
.engine("aurora-postgresql").masterUsername("admin").masterUserPassword("password123"))
|
||||
.dbCluster().dbClusterArn();
|
||||
String memberArn = rds.createDBInstance(r -> r.dbInstanceIdentifier(member)
|
||||
.dbClusterIdentifier(cluster).engine("aurora-postgresql").dbInstanceClass("db.r6g.large"))
|
||||
.dbInstance().dbInstanceArn();
|
||||
try {
|
||||
rds.stopDBCluster(r -> r.dbClusterIdentifier(cluster));
|
||||
rds.startDBCluster(r -> r.dbClusterIdentifier(cluster));
|
||||
List<JsonNode> events = queue.drain();
|
||||
assertEquals(4, events.size());
|
||||
for (boolean started : List.of(false, true)) {
|
||||
String instanceEvent = started ? "RDS-EVENT-0088" : "RDS-EVENT-0087";
|
||||
String clusterEvent = started ? "RDS-EVENT-0151" : "RDS-EVENT-0150";
|
||||
assertEvent(find(events, instanceEvent), account, region, memberArn, member, false, started);
|
||||
assertEvent(find(events, clusterEvent), account, region, clusterArn, cluster, true, started);
|
||||
}
|
||||
assertTrue(foreignAccount.drain().isEmpty());
|
||||
assertTrue(foreignRegion.drain().isEmpty());
|
||||
} finally {
|
||||
rds.deleteDBInstance(r -> r.dbInstanceIdentifier(member).skipFinalSnapshot(true));
|
||||
rds.deleteDBCluster(r -> r.dbClusterIdentifier(cluster).skipFinalSnapshot(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsClusterSourceTypePatternReceivesClusterLifecycleEvents() throws Exception {
|
||||
String name = "rds-cluster-filter-" + Long.toUnsignedString(System.nanoTime(), 36);
|
||||
String pattern = """
|
||||
{"source":["aws.rds"],"detail-type":["RDS DB Cluster Event"],
|
||||
"detail":{"SourceType":["CLUSTER"]}}
|
||||
""";
|
||||
String account = "222233334444";
|
||||
Region region = Region.EU_WEST_2;
|
||||
try (RdsClient rds = AwsTestClients.create(RdsClient.builder(), account, region);
|
||||
EventRuleQueue queue = EventRuleQueue.route(account, region.id(), name, pattern)) {
|
||||
String arn = rds.createDBCluster(r -> r.dbClusterIdentifier(name)
|
||||
.engine("aurora-postgresql").masterUsername("admin").masterUserPassword("password123"))
|
||||
.dbCluster().dbClusterArn();
|
||||
try {
|
||||
rds.stopDBCluster(r -> r.dbClusterIdentifier(name));
|
||||
rds.startDBCluster(r -> r.dbClusterIdentifier(name));
|
||||
List<JsonNode> events = queue.drain();
|
||||
assertEquals(2, events.size());
|
||||
assertEvent(find(events, "RDS-EVENT-0150"), account, region, arn, name, true, false);
|
||||
assertEvent(find(events, "RDS-EVENT-0151"), account, region, arn, name, true, true);
|
||||
} finally {
|
||||
rds.deleteDBCluster(r -> r.dbClusterIdentifier(name).skipFinalSnapshot(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode find(List<JsonNode> events, String eventId) {
|
||||
return events.stream().filter(e -> eventId.equals(e.path("detail").path("EventID").asText()))
|
||||
.findFirst().orElseThrow();
|
||||
}
|
||||
|
||||
private void assertEvent(JsonNode event, String account, Region region, String arn, String id,
|
||||
boolean cluster, boolean started) {
|
||||
assertEquals("aws.rds", event.path("source").asText());
|
||||
assertEquals(cluster ? "RDS DB Cluster Event" : "RDS DB Instance Event",
|
||||
event.path("detail-type").asText());
|
||||
assertEquals(account, event.path("account").asText());
|
||||
assertEquals(region.id(), event.path("region").asText());
|
||||
assertEquals(mapper.valueToTree(List.of(arn)), event.path("resources"));
|
||||
JsonNode detail = event.path("detail");
|
||||
assertEquals(mapper.valueToTree(List.of("notification")), detail.path("EventCategories"));
|
||||
assertEquals(cluster ? "CLUSTER" : "DB_INSTANCE", detail.path("SourceType").asText());
|
||||
assertEquals(arn, detail.path("SourceArn").asText());
|
||||
assertEquals(id, detail.path("SourceIdentifier").asText());
|
||||
assertEquals("DB " + (cluster ? "cluster" : "instance") + (started ? " started." : " stopped."),
|
||||
detail.path("Message").asText());
|
||||
assertEquals(cluster ? (started ? "RDS-EVENT-0151" : "RDS-EVENT-0150")
|
||||
: (started ? "RDS-EVENT-0088" : "RDS-EVENT-0087"), detail.path("EventID").asText());
|
||||
assertFalse(Instant.parse(detail.path("Date").asText()).isAfter(Instant.now()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import io.floci.aws.services.ec2.model.SecurityGroup;
|
||||
import io.floci.aws.services.ec2.model.Subnet;
|
||||
import io.floci.aws.services.ec2.model.Vpc;
|
||||
import io.floci.aws.services.ec2.model.VpcIpv6CidrBlockAssociation;
|
||||
import io.floci.aws.services.eventbridge.EventBridgeService;
|
||||
import io.floci.aws.services.kms.KmsService;
|
||||
import io.floci.aws.services.kms.model.KmsKey;
|
||||
import io.floci.aws.services.rds.container.AutoPauseListener;
|
||||
@@ -164,6 +165,8 @@ class RdsServiceTest {
|
||||
"docdb8.0");
|
||||
|
||||
private RdsService rdsService;
|
||||
private EventBridgeService eventBridgeService;
|
||||
private RdsEventPublisher eventPublisher;
|
||||
private RdsContainerManager containerManager;
|
||||
private RdsProxyManager proxyManager;
|
||||
private Ec2Service ec2Service;
|
||||
@@ -176,6 +179,8 @@ class RdsServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
eventBridgeService = mock(EventBridgeService.class);
|
||||
eventPublisher = new RdsEventPublisher(eventBridgeService, new ObjectMapper());
|
||||
containerManager = mock(RdsContainerManager.class);
|
||||
proxyManager = mock(RdsProxyManager.class);
|
||||
ec2Service = mock(Ec2Service.class);
|
||||
@@ -6110,6 +6115,71 @@ class RdsServiceTest {
|
||||
assertNotEquals(a.getEndpoint().port(), b.getEndpoint().port());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void failedStopsDoNotPublishSuccessfulLifecycleEvents(boolean cluster) {
|
||||
String id = "failed-stop";
|
||||
createLifecycleResource(id, cluster);
|
||||
doThrow(new IllegalStateException("backend stop failed")).when(containerManager).stop(any());
|
||||
assertThrows(IllegalStateException.class, () -> stopLifecycleResource(id, cluster));
|
||||
verifyNoInteractions(eventBridgeService);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void failedStartsDoNotPublishSuccessfulLifecycleEvents(boolean cluster) {
|
||||
String id = "failed-start";
|
||||
createLifecycleResource(id, cluster);
|
||||
stopLifecycleResource(id, cluster);
|
||||
clearInvocations(eventBridgeService);
|
||||
when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenThrow(new IllegalStateException("backend start failed"));
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
if (cluster) {
|
||||
rdsService.startDbCluster(id, "us-east-1");
|
||||
} else {
|
||||
rdsService.startDbInstance(id);
|
||||
}
|
||||
});
|
||||
verifyNoInteractions(eventBridgeService);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void lifecycleDeliveryRunsAfterReleasingResourceAndServiceLocks(boolean cluster) {
|
||||
String id = "unlocked-delivery";
|
||||
createLifecycleResource(id, cluster);
|
||||
Object resource = cluster ? rdsService.getDbCluster(id, "us-east-1") : rdsService.getDbInstance(id);
|
||||
doAnswer(invocation -> {
|
||||
assertFalse(Thread.holdsLock(rdsService));
|
||||
assertFalse(Thread.holdsLock(resource));
|
||||
return null;
|
||||
}).when(eventBridgeService).publishServiceEvent(anyString(), anyString(), anyString(), anyString(), anyString());
|
||||
stopLifecycleResource(id, cluster);
|
||||
verify(eventBridgeService, times(cluster ? 2 : 1))
|
||||
.publishServiceEvent(eq("aws.rds"), anyString(), anyString(), anyString(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
private void createLifecycleResource(String id, boolean cluster) {
|
||||
if (cluster) {
|
||||
rdsService.createDbCluster(id, "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
rdsService.createDbInstance(id + "-member", "aurora-postgresql", "16.3",
|
||||
null, null, null, "db.r6g.large", 20, false, null, null, id);
|
||||
} else {
|
||||
rdsService.createDbInstance(id, "postgres", "16", "admin", "password", "dbname",
|
||||
"db.t3.micro", 20, false, null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void stopLifecycleResource(String id, boolean cluster) {
|
||||
if (cluster) {
|
||||
rdsService.stopDbCluster(id, "us-east-1");
|
||||
} else {
|
||||
rdsService.stopDbInstance(id, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopAndStartDbInstanceReleaseAndRestoreTheContainerOnTheSameVolume() {
|
||||
DbInstance created = rdsService.createDbInstance("standalone", "postgres", "16",
|
||||
@@ -11343,7 +11413,7 @@ class RdsServiceTest {
|
||||
return new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, subnetGroups,
|
||||
null, null, null, new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), null, kmsService);
|
||||
new InMemoryStorage<>(), null, kmsService, eventPublisher);
|
||||
}
|
||||
|
||||
private RdsService newService(RdsContainerManager containerManager,
|
||||
@@ -11356,7 +11426,7 @@ class RdsServiceTest {
|
||||
return new RdsService(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>(),
|
||||
secretsManager, null, null, new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), null, kmsService);
|
||||
new InMemoryStorage<>(), null, kmsService, eventPublisher);
|
||||
}
|
||||
|
||||
private RdsService proxyStoreService(
|
||||
|
||||
@@ -1,29 +1,38 @@
|
||||
package io.floci.aws.services.rds;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import com.github.dockerjava.core.command.ExecStartResultCallback;
|
||||
import io.floci.aws.services.rds.model.DbCluster;
|
||||
import io.floci.aws.services.rds.model.DbInstance;
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.EventRuleQueue;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.rds.RdsClient;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.restassured.http.ContentType.URLENC;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -49,6 +58,11 @@ class RdsStopStartIntegrationTest {
|
||||
@Inject
|
||||
RdsService rdsService;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void checkDocker() {
|
||||
try {
|
||||
@@ -65,6 +79,44 @@ class RdsStopStartIntegrationTest {
|
||||
.formParam("Version", "2014-10-31");
|
||||
}
|
||||
|
||||
@Test
|
||||
void realClusterRecoveryEmitsMemberEventsAndKeepsSqlData() throws Exception {
|
||||
String id = "native-events-" + Long.toUnsignedString(System.nanoTime(), 36);
|
||||
String member = id + "-member";
|
||||
String account = "000000000000";
|
||||
String memberArn = "arn:aws:rds:us-east-1:" + account + ":db:" + member;
|
||||
String pattern = "{\"source\":[\"aws.rds\"],\"resources\":[\"" + memberArn + "\"]}";
|
||||
try (RdsClient rds = AwsTestClients.create(RdsClient.builder(), account, Region.US_EAST_1);
|
||||
EventRuleQueue queue = EventRuleQueue.route(account, Region.US_EAST_1.id(), id, pattern)) {
|
||||
rds.createDBCluster(r -> r.dbClusterIdentifier(id).engine("aurora-postgresql").engineVersion("16.3")
|
||||
.masterUsername("admin").masterUserPassword("password123").databaseName("appdb"));
|
||||
rds.createDBInstance(r -> r.dbInstanceIdentifier(member).dbClusterIdentifier(id)
|
||||
.engine("aurora-postgresql").dbInstanceClass("db.r6g.large"));
|
||||
try {
|
||||
psql(rdsService.getDbCluster(id).getContainerId(),
|
||||
"CREATE TABLE marker(v text); INSERT INTO marker VALUES ('native-event-row');");
|
||||
rds.stopDBCluster(r -> r.dbClusterIdentifier(id));
|
||||
assertEquals("stopped", rds.describeDBClusters(r -> r.dbClusterIdentifier(id))
|
||||
.dbClusters().getFirst().status());
|
||||
rds.startDBCluster(r -> r.dbClusterIdentifier(id));
|
||||
assertTrue(psql(rdsService.getDbCluster(id).getContainerId(), "SELECT v FROM marker;")
|
||||
.contains("native-event-row"));
|
||||
List<JsonNode> messages = queue.drain();
|
||||
assertEquals(2, messages.size());
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
JsonNode event = messages.get(i);
|
||||
assertEquals("RDS DB Instance Event", event.path("detail-type").asText());
|
||||
assertEquals(memberArn, event.path("resources").get(0).asText());
|
||||
assertEquals(i == 0 ? "RDS-EVENT-0087" : "RDS-EVENT-0088",
|
||||
event.path("detail").path("EventID").asText());
|
||||
}
|
||||
} finally {
|
||||
rds.deleteDBInstance(r -> r.dbInstanceIdentifier(member).skipFinalSnapshot(true));
|
||||
rds.deleteDBCluster(r -> r.dbClusterIdentifier(id).skipFinalSnapshot(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppedInstanceKeepsItsDataAndEndpointForTheStart() throws Exception {
|
||||
String id = "stop-db-" + Long.toString(System.nanoTime(), 36);
|
||||
|
||||
@@ -24,11 +24,13 @@ public final class EventRuleQueue implements AutoCloseable {
|
||||
private static final String TARGET_ID = "1";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private final String account;
|
||||
private final String region;
|
||||
private final String name;
|
||||
private final String url;
|
||||
|
||||
private EventRuleQueue(String region, String name, String url) {
|
||||
private EventRuleQueue(String account, String region, String name, String url) {
|
||||
this.account = account;
|
||||
this.region = region;
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
@@ -36,18 +38,23 @@ public final class EventRuleQueue implements AutoCloseable {
|
||||
|
||||
/** A queue named {@code name}, and a rule of the same name sending it the events {@code pattern} matches. */
|
||||
public static EventRuleQueue route(String region, String name, String pattern) {
|
||||
String url = call(region, "sqs", "AmazonSQS.CreateQueue", SQS_CONTENT_TYPE,
|
||||
return route("AKID", region, name, pattern);
|
||||
}
|
||||
|
||||
/** The same route in the selected {@code account}, including reads and cleanup. */
|
||||
public static EventRuleQueue route(String account, String region, String name, String pattern) {
|
||||
String url = call(account, region, "sqs", "AmazonSQS.CreateQueue", SQS_CONTENT_TYPE,
|
||||
MAPPER.createObjectNode().put("QueueName", name)).jsonPath().getString("QueueUrl");
|
||||
ObjectNode attributes = MAPPER.createObjectNode().put("QueueUrl", url);
|
||||
attributes.putArray("AttributeNames").add("QueueArn");
|
||||
String arn = call(region, "sqs", "AmazonSQS.GetQueueAttributes", SQS_CONTENT_TYPE, attributes)
|
||||
String arn = call(account, region, "sqs", "AmazonSQS.GetQueueAttributes", SQS_CONTENT_TYPE, attributes)
|
||||
.jsonPath().getString("Attributes.QueueArn");
|
||||
call(region, "events", "AWSEvents.PutRule", EVENTS_CONTENT_TYPE,
|
||||
call(account, region, "events", "AWSEvents.PutRule", EVENTS_CONTENT_TYPE,
|
||||
MAPPER.createObjectNode().put("Name", name).put("EventPattern", pattern));
|
||||
ObjectNode targets = MAPPER.createObjectNode().put("Rule", name);
|
||||
targets.putArray("Targets").addObject().put("Id", TARGET_ID).put("Arn", arn);
|
||||
call(region, "events", "AWSEvents.PutTargets", EVENTS_CONTENT_TYPE, targets);
|
||||
return new EventRuleQueue(region, name, url);
|
||||
call(account, region, "events", "AWSEvents.PutTargets", EVENTS_CONTENT_TYPE, targets);
|
||||
return new EventRuleQueue(account, region, name, url);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,14 +67,14 @@ public final class EventRuleQueue implements AutoCloseable {
|
||||
ObjectNode receive = MAPPER.createObjectNode().put("QueueUrl", url).put("MaxNumberOfMessages", 10)
|
||||
.put("WaitTimeSeconds", 0);
|
||||
while (true) {
|
||||
Response response = call(region, "sqs", "AmazonSQS.ReceiveMessage", SQS_CONTENT_TYPE, receive);
|
||||
Response response = call(account, region, "sqs", "AmazonSQS.ReceiveMessage", SQS_CONTENT_TYPE, receive);
|
||||
JsonNode messages = MAPPER.readTree(response.asString()).path("Messages");
|
||||
if (messages.isEmpty()) {
|
||||
return events;
|
||||
}
|
||||
for (JsonNode message : messages) {
|
||||
events.add(MAPPER.readTree(message.path("Body").asText()));
|
||||
call(region, "sqs", "AmazonSQS.DeleteMessage", SQS_CONTENT_TYPE, MAPPER.createObjectNode()
|
||||
call(account, region, "sqs", "AmazonSQS.DeleteMessage", SQS_CONTENT_TYPE, MAPPER.createObjectNode()
|
||||
.put("QueueUrl", url).put("ReceiptHandle", message.path("ReceiptHandle").asText()));
|
||||
}
|
||||
}
|
||||
@@ -78,16 +85,17 @@ public final class EventRuleQueue implements AutoCloseable {
|
||||
public void close() {
|
||||
ObjectNode targets = MAPPER.createObjectNode().put("Rule", name);
|
||||
targets.putArray("Ids").add(TARGET_ID);
|
||||
call(region, "events", "AWSEvents.RemoveTargets", EVENTS_CONTENT_TYPE, targets);
|
||||
call(region, "events", "AWSEvents.DeleteRule", EVENTS_CONTENT_TYPE,
|
||||
call(account, region, "events", "AWSEvents.RemoveTargets", EVENTS_CONTENT_TYPE, targets);
|
||||
call(account, region, "events", "AWSEvents.DeleteRule", EVENTS_CONTENT_TYPE,
|
||||
MAPPER.createObjectNode().put("Name", name));
|
||||
call(region, "sqs", "AmazonSQS.DeleteQueue", SQS_CONTENT_TYPE,
|
||||
call(account, region, "sqs", "AmazonSQS.DeleteQueue", SQS_CONTENT_TYPE,
|
||||
MAPPER.createObjectNode().put("QueueUrl", url));
|
||||
}
|
||||
|
||||
private static Response call(String region, String service, String target, String contentType, JsonNode body) {
|
||||
private static Response call(String account, String region, String service, String target,
|
||||
String contentType, JsonNode body) {
|
||||
Response response = given()
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region + "/" + service
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account + "/20260215/" + region + "/" + service
|
||||
+ "/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.header("X-Amz-Target", target)
|
||||
.contentType(contentType)
|
||||
|
||||
Reference in new issue
Block a user