mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(rds): stop, start and reboot DB instances and clusters (#4071)
* feat(rds): stop, start and reboot DB instances and clusters Add StopDBInstance, StartDBInstance, StopDBCluster, StartDBCluster and RebootDBCluster. A stop reports "stopping", takes the optional snapshot, removes the container and proxy and settles the record to "stopped" while its identifier, endpoint, groups and Docker volume stay; a start reports "starting" and brings the container back on the kept volume and port. Cluster stop, start and reboot carry the member instances along. StopDBInstance refuses cluster members, read replicas and their sources, and anything not available; ModifyDBInstance refuses a stopped instance; a stopped resource stays stopped across an emulator restart. Closes #4043 * feat(rds): refuse starting a stopped cluster's member alone, serialize cluster lifecycle calls StartDBInstance now refuses a cluster member as StopDBInstance does, and both answer InvalidDBClusterStateFault, the fault the model lists for a member on either call; a member started alone would have got a standalone container on its own volume while the cluster stayed stopped. stopDbCluster, startDbCluster and rebootDbCluster are synchronized like the instance calls, so two concurrent starts cannot both see STOPPED. The five handler arms use the shared required-parameter helper and the central AwsException handling. * test(rds): import DbInstanceStatus instead of qualifying it inline AGENTS.md asks for imports over inline fully-qualified names; the file already imports DbInstanceStatus.
This commit is contained in:
1 parent
c083aad12b
commit
1542c641a6
8 files changed
+910
-1
No files matched your search
@@ -1913,6 +1913,44 @@ class RdsQueryHandlerTest {
|
||||
verify(service).modifyDbSnapshot("mysnap", "14", "new-options", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopStartAndRebootActions_dispatchAndRenderTransitionalStatuses() {
|
||||
DbInstance stopping = makeInstance("standalone");
|
||||
stopping.setStatus(DbInstanceStatus.STOPPING);
|
||||
when(service.stopDbInstance(eq("standalone"), eq("before-stop"), isNull())).thenReturn(stopping);
|
||||
MultivaluedMap<String, String> stop = params();
|
||||
stop.add("DBInstanceIdentifier", "standalone");
|
||||
stop.add("DBSnapshotIdentifier", "before-stop");
|
||||
String stopBody = (String) handler.handle("StopDBInstance", stop).getEntity();
|
||||
assertTrue(stopBody.contains("<StopDBInstanceResult>"));
|
||||
assertTrue(stopBody.contains("<DBInstanceStatus>stopping</DBInstanceStatus>"));
|
||||
|
||||
DbInstance starting = makeInstance("standalone");
|
||||
starting.setStatus(DbInstanceStatus.STARTING);
|
||||
when(service.startDbInstance(eq("standalone"), isNull())).thenReturn(starting);
|
||||
MultivaluedMap<String, String> start = params();
|
||||
start.add("DBInstanceIdentifier", "standalone");
|
||||
String startBody = (String) handler.handle("StartDBInstance", start).getEntity();
|
||||
assertTrue(startBody.contains("<StartDBInstanceResult>"));
|
||||
assertTrue(startBody.contains("<DBInstanceStatus>starting</DBInstanceStatus>"));
|
||||
|
||||
DbCluster cluster = new DbCluster();
|
||||
cluster.setDbClusterIdentifier("aurora");
|
||||
cluster.setStatus(DbInstanceStatus.STOPPED);
|
||||
when(service.stopDbCluster(eq("aurora"), isNull())).thenReturn(cluster);
|
||||
MultivaluedMap<String, String> stopCluster = params();
|
||||
stopCluster.add("DBClusterIdentifier", "aurora");
|
||||
String clusterBody = (String) handler.handle("StopDBCluster", stopCluster).getEntity();
|
||||
assertTrue(clusterBody.contains("<StopDBClusterResult>"));
|
||||
assertTrue(clusterBody.contains("<Status>stopped</Status>"));
|
||||
|
||||
assertEquals(400, handler.handle("StopDBInstance", params()).getStatus());
|
||||
assertEquals(400, handler.handle("StartDBInstance", params()).getStatus());
|
||||
assertEquals(400, handler.handle("StartDBCluster", params()).getStatus());
|
||||
assertEquals(400, handler.handle("RebootDBCluster", params()).getStatus());
|
||||
verify(service, never()).startDbCluster(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeDbSnapshotAttributes_returnsRestoreAttribute() {
|
||||
io.github.hectorvent.floci.services.rds.model.DbSnapshot snapshot = new io.github.hectorvent.floci.services.rds.model.DbSnapshot();
|
||||
|
||||
@@ -2029,6 +2029,142 @@ class RdsServiceTest {
|
||||
assertNotEquals(a.getEndpoint().port(), b.getEndpoint().port());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopAndStartDbInstanceReleaseAndRestoreTheContainerOnTheSameVolume() {
|
||||
DbInstance created = rdsService.createDbInstance("standalone", "postgres", "16",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
String volume = created.getDockerVolumeName();
|
||||
int port = created.getEndpoint().port();
|
||||
|
||||
DbInstance stopping = rdsService.stopDbInstance("standalone", null);
|
||||
assertEquals(DbInstanceStatus.STOPPING, stopping.getStatus());
|
||||
DbInstance stopped = rdsService.getDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.STOPPED, stopped.getStatus());
|
||||
assertNull(stopped.getContainerId());
|
||||
assertEquals(volume, stopped.getDockerVolumeName());
|
||||
assertEquals(port, stopped.getEndpoint().port());
|
||||
verify(containerManager).stop(any());
|
||||
verify(proxyManager).stopProxy(any());
|
||||
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbInstance("standalone", null)).getErrorCode());
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.modifyDbInstance("standalone", "new-password", null, null, null, null, null)).getErrorCode());
|
||||
|
||||
DbInstance starting = rdsService.startDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.STARTING, starting.getStatus());
|
||||
DbInstance started = rdsService.getDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, started.getStatus());
|
||||
assertEquals("cont-id", started.getContainerId());
|
||||
assertEquals(port, started.getEndpoint().port());
|
||||
verify(containerManager, times(2)).tryStart(any(), any(), any(), eq(volume), any(), any(), any(), any(), any());
|
||||
verify(proxyManager, times(2)).startProxy(any(), any(), anyBoolean(), eq(port), any(), anyInt(),
|
||||
any(), any(), any(), any(), any());
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.startDbInstance("standalone")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopDbInstanceTakesTheRequestedSnapshotFirst() {
|
||||
rdsService.createDbInstance("standalone", "postgres", "16",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("DUMP");
|
||||
|
||||
rdsService.stopDbInstance("standalone", "before-stop");
|
||||
|
||||
assertEquals("available", rdsService.describeDbSnapshots("before-stop", null).iterator().next().getStatus());
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("standalone").getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopDbInstanceRefusesClusterMembersAndReplicas() {
|
||||
rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
rdsService.createDbInstance("aurora-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r6g.large",
|
||||
20, false, null, null, "aurora-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
AwsException member = assertThrows(AwsException.class, () -> rdsService.stopDbInstance("aurora-member", null));
|
||||
assertEquals("InvalidDBClusterStateFault", member.getErrorCode());
|
||||
assertTrue(member.getMessage().contains("StopDBCluster"));
|
||||
// A stopped cluster's member cannot be started on its own either.
|
||||
rdsService.stopDbCluster("aurora-cluster", "us-east-1");
|
||||
AwsException startMember = assertThrows(AwsException.class, () -> rdsService.startDbInstance("aurora-member"));
|
||||
assertEquals("InvalidDBClusterStateFault", startMember.getErrorCode());
|
||||
assertTrue(startMember.getMessage().contains("StartDBCluster"));
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
rdsService.startDbCluster("aurora-cluster", "us-east-1");
|
||||
|
||||
when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenAnswer(invocation -> new RdsContainerHandle(
|
||||
"cont-" + invocation.getArgument(1), invocation.getArgument(1), "localhost", 5432));
|
||||
createPostgresSource("primary");
|
||||
when(containerManager.createPostgresSnapshot("cont-primary", "admin")).thenReturn("DUMP");
|
||||
rdsService.createDbInstanceReadReplica(replicaRequest("primary-replica", "primary"), null);
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbInstance("primary", null)).getErrorCode());
|
||||
assertEquals("InvalidDBInstanceState", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbInstance("primary-replica", null)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopStartAndRebootDbClusterCarryTheMembersAlong() {
|
||||
DbCluster created = rdsService.createDbCluster("aurora-cluster", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", false, null);
|
||||
rdsService.createDbInstance("aurora-member", "aurora-postgresql", "16.3",
|
||||
"admin", "password", "dbname", "db.r6g.large",
|
||||
20, false, null, null, "aurora-cluster", null, false, false, null,
|
||||
Map.of(), List.of(), null, null, true, DbInstanceSettings.defaults());
|
||||
int port = created.getEndpoint().port();
|
||||
|
||||
DbCluster stopping = rdsService.stopDbCluster("aurora-cluster", "us-east-1");
|
||||
assertEquals(DbInstanceStatus.STOPPING, stopping.getStatus());
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbCluster("aurora-cluster").getStatus());
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
assertNull(rdsService.getDbCluster("aurora-cluster").getContainerId());
|
||||
assertEquals("InvalidDBClusterStateFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbCluster("aurora-cluster", "us-east-1")).getErrorCode());
|
||||
assertEquals("InvalidDBClusterStateFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.rebootDbCluster("aurora-cluster", "us-east-1")).getErrorCode());
|
||||
|
||||
DbCluster starting = rdsService.startDbCluster("aurora-cluster", "us-east-1");
|
||||
assertEquals(DbInstanceStatus.STARTING, starting.getStatus());
|
||||
DbCluster started = rdsService.getDbCluster("aurora-cluster");
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, started.getStatus());
|
||||
assertEquals(port, started.getEndpoint().port());
|
||||
assertEquals("cont-id", started.getContainerId());
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
assertEquals("InvalidDBClusterStateFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.startDbCluster("aurora-cluster", "us-east-1")).getErrorCode());
|
||||
|
||||
DbCluster rebooting = rdsService.rebootDbCluster("aurora-cluster", "us-east-1");
|
||||
assertEquals(DbInstanceStatus.REBOOTING, rebooting.getStatus());
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbCluster("aurora-cluster").getStatus());
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbInstance("aurora-member").getStatus());
|
||||
// create, start and reboot each brought the cluster container up once.
|
||||
verify(containerManager, times(3)).tryStart(eq(created.getDbClusterArn()), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
assertEquals("DBClusterNotFoundFault", assertThrows(AwsException.class,
|
||||
() -> rdsService.stopDbCluster("absent", "us-east-1")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockModeStopAndStartTouchNoContainerOrProxy() {
|
||||
when(config.services().rds().mock()).thenReturn(true);
|
||||
rdsService.createDbInstance("standalone", "postgres", "16",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
|
||||
rdsService.stopDbInstance("standalone", null);
|
||||
assertEquals(DbInstanceStatus.STOPPED, rdsService.getDbInstance("standalone").getStatus());
|
||||
rdsService.startDbInstance("standalone");
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, rdsService.getDbInstance("standalone").getStatus());
|
||||
verify(containerManager, never()).stop(any());
|
||||
verify(containerManager, never()).tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
verify(proxyManager, never()).stopProxy(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockModeRebootSkipsContainerAndProxy() {
|
||||
when(config.services().rds().mock()).thenReturn(true);
|
||||
|
||||
+261
@@ -0,0 +1,261 @@
|
||||
package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
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.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstance;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
* Stop and start against real Postgres containers: a stopped instance has no container and
|
||||
* its endpoint refuses connections, a started one has the same endpoint and the row written
|
||||
* before the stop, and a cluster carries its member along. Statuses follow the user guide:
|
||||
* "stopping" in the stop response, "stopped" afterwards, "starting" then "available".
|
||||
*/
|
||||
@QuarkusTest
|
||||
@Tag("docker")
|
||||
class RdsStopStartIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260921/us-east-1/rds/aws4_request, "
|
||||
+ "SignedHeaders=content-type;host, Signature=test";
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
RdsService rdsService;
|
||||
|
||||
@BeforeEach
|
||||
void checkDocker() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
} catch (Exception e) {
|
||||
assumeTrue(false, "Docker is not available: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static RequestSpecification query(String action) {
|
||||
return given().header("Authorization", AUTH)
|
||||
.contentType(URLENC)
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2014-10-31");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppedInstanceKeepsItsDataAndEndpointForTheStart() throws Exception {
|
||||
String id = "stop-db-" + Long.toString(System.nanoTime(), 36);
|
||||
query("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.formParam("Engine", "postgres")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("DBName", "appdb")
|
||||
.formParam("DBInstanceClass", "db.t3.micro")
|
||||
.formParam("AllocatedStorage", "5")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForInstance(id, "available");
|
||||
DbInstance running = rdsService.getDbInstance(id);
|
||||
String containerId = running.getContainerId();
|
||||
int port = running.getEndpoint().port();
|
||||
psql(containerId, "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('before-stop');");
|
||||
assertTrue(portOpen(port), "the endpoint must accept connections while available");
|
||||
|
||||
query("StopDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StopDBInstanceResult>"))
|
||||
.body(containsString("<DBInstanceStatus>stopping</DBInstanceStatus>"));
|
||||
waitForInstance(id, "stopped");
|
||||
DbInstance stopped = rdsService.getDbInstance(id);
|
||||
assertNull(stopped.getContainerId(), "a stopped instance has no container");
|
||||
assertFalse(containerExists(containerId), "the container is gone while stopped");
|
||||
assertFalse(portOpen(port), "the endpoint refuses connections while stopped");
|
||||
|
||||
query("ModifyDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.formParam("MasterUserPassword", "changed")
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidDBInstanceState</Code>"));
|
||||
query("StopDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body(containsString("<Code>InvalidDBInstanceState</Code>"));
|
||||
|
||||
query("StartDBInstance")
|
||||
.formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StartDBInstanceResult>"))
|
||||
.body(containsString("<DBInstanceStatus>starting</DBInstanceStatus>"));
|
||||
waitForInstance(id, "available");
|
||||
DbInstance started = rdsService.getDbInstance(id);
|
||||
assertTrue(started.getEndpoint().port() == port, "the endpoint keeps its port");
|
||||
assertTrue(portOpen(port), "the endpoint accepts connections again");
|
||||
assertTrue(psql(started.getContainerId(), "SELECT v FROM marker;").contains("before-stop"),
|
||||
"the row written before the stop must survive on the volume");
|
||||
|
||||
query("DeleteDBInstance").formParam("DBInstanceIdentifier", id).formParam("SkipFinalSnapshot", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppedClusterCarriesItsMemberAndRebootKeepsTheData() throws Exception {
|
||||
String cluster = "stop-cluster-" + Long.toString(System.nanoTime(), 36);
|
||||
String member = cluster + "-1";
|
||||
query("CreateDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("EngineVersion", "16.3")
|
||||
.formParam("MasterUsername", "admin")
|
||||
.formParam("MasterUserPassword", "password123")
|
||||
.formParam("DatabaseName", "appdb")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForCluster(cluster, "available");
|
||||
query("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", member)
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.formParam("Engine", "aurora-postgresql")
|
||||
.formParam("DBInstanceClass", "db.r6g.large")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForInstance(member, "available");
|
||||
DbCluster running = rdsService.getDbCluster(cluster);
|
||||
psql(running.getContainerId(), "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('cluster-row');");
|
||||
|
||||
query("StopDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StopDBClusterResult>"))
|
||||
.body(containsString("<Status>stopping</Status>"));
|
||||
waitForCluster(cluster, "stopped");
|
||||
waitForInstance(member, "stopped");
|
||||
assertNull(rdsService.getDbCluster(cluster).getContainerId());
|
||||
|
||||
query("StartDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<Status>starting</Status>"));
|
||||
waitForCluster(cluster, "available");
|
||||
waitForInstance(member, "available");
|
||||
assertTrue(psql(rdsService.getDbCluster(cluster).getContainerId(), "SELECT v FROM marker;").contains("cluster-row"));
|
||||
|
||||
query("RebootDBCluster")
|
||||
.formParam("DBClusterIdentifier", cluster)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<RebootDBClusterResult>"))
|
||||
.body(containsString("<Status>rebooting</Status>"));
|
||||
waitForCluster(cluster, "available");
|
||||
assertTrue(psql(rdsService.getDbCluster(cluster).getContainerId(), "SELECT v FROM marker;").contains("cluster-row"));
|
||||
|
||||
query("DeleteDBInstance").formParam("DBInstanceIdentifier", member).formParam("SkipFinalSnapshot", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
query("DeleteDBCluster").formParam("DBClusterIdentifier", cluster).formParam("SkipFinalSnapshot", "true")
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static boolean portOpen(int port) {
|
||||
try (Socket socket = new Socket("localhost", port)) {
|
||||
return socket.isConnected();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean containerExists(String containerId) {
|
||||
try {
|
||||
dockerClient.inspectContainerCmd(containerId).exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void waitForInstance(String id, String expected) throws Exception {
|
||||
for (int i = 0; i < 60; i++) {
|
||||
String status = query("DescribeDBInstances").formParam("DBInstanceIdentifier", id)
|
||||
.when().post("/").then().extract().xmlPath().getString(
|
||||
"DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.DBInstanceStatus");
|
||||
if (expected.equals(status)) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
throw new RuntimeException("DB instance " + id + " did not reach " + expected + ".");
|
||||
}
|
||||
|
||||
private void waitForCluster(String id, String expected) throws Exception {
|
||||
for (int i = 0; i < 60; i++) {
|
||||
String status = query("DescribeDBClusters").formParam("DBClusterIdentifier", id)
|
||||
.when().post("/").then().extract().xmlPath().getString(
|
||||
"DescribeDBClustersResponse.DescribeDBClustersResult.DBClusters.DBCluster.Status");
|
||||
if (expected.equals(status)) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
throw new RuntimeException("DB cluster " + id + " did not reach " + expected + ".");
|
||||
}
|
||||
|
||||
private String psql(String containerId, String sql) throws Exception {
|
||||
String[] command = {"psql", "-U", "admin", "-d", "appdb", "-tAc", sql};
|
||||
String execId = dockerClient.execCreateCmd(containerId)
|
||||
.withCmd(command).withAttachStdout(true).withAttachStderr(true).exec().getId();
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errors = new ByteArrayOutputStream();
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
Closeable callback = dockerClient.execStartCmd(execId).exec(new ExecStartResultCallback() {
|
||||
@Override
|
||||
public void onNext(Frame frame) {
|
||||
try {
|
||||
if (frame.getStreamType() == StreamType.STDOUT) {
|
||||
output.write(frame.getPayload());
|
||||
} else if (frame.getStreamType() == StreamType.STDERR) {
|
||||
errors.write(frame.getPayload());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to capture psql output", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
assertTrue(latch.await(30, TimeUnit.SECONDS), "Timed out running psql");
|
||||
long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();
|
||||
assertTrue(exitCode == 0, "psql failed with exit code " + exitCode + ": " + errors.toString(StandardCharsets.UTF_8));
|
||||
return output.toString(StandardCharsets.UTF_8);
|
||||
} finally {
|
||||
callback.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user