Skip to content

Commit d2e4a23

Browse files
authored
Switch to generic test containers (#1489)
For Postgres, we are still using DB specific test containers. These containers require the corresponding JDBC driver. With this change, we get rid of the JDBC drivers transitive test dependency. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent a3a1739 commit d2e4a23

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

vertx-pg-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585

8686
<dependency>
8787
<groupId>org.testcontainers</groupId>
88-
<artifactId>postgresql</artifactId>
88+
<artifactId>testcontainers</artifactId>
8989
<version>${testcontainers.version}</version>
9090
<scope>test</scope>
9191
</dependency>

vertx-pg-client/src/test/java/io/vertx/pgclient/junit/ContainerPgRule.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
import io.vertx.sqlclient.PoolOptions;
2121
import org.junit.rules.ExternalResource;
2222
import org.testcontainers.containers.BindMode;
23+
import org.testcontainers.containers.GenericContainer;
2324
import org.testcontainers.containers.InternetProtocol;
24-
import org.testcontainers.containers.PostgreSQLContainer;
25+
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
2526

26-
import static org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT;
27+
import java.time.Duration;
28+
import java.time.temporal.ChronoUnit;
29+
30+
import static io.vertx.pgclient.PgConnectOptions.DEFAULT_PORT;
2731

2832
/**
2933
* Postgresql test database based on https://www.testcontainers.org
@@ -73,9 +77,14 @@ public ContainerPgRule user(String user) {
7377

7478
private void initServer(String version) throws Exception {
7579
server = new ServerContainer<>("postgres:" + version)
76-
.withDatabaseName("postgres")
77-
.withUsername(user)
78-
.withPassword("postgres")
80+
.withEnv("POSTGRES_DB", "postgres")
81+
.withEnv("POSTGRES_USER", user)
82+
.withEnv("POSTGRES_PASSWORD", "postgres")
83+
.waitingFor(new LogMessageWaitStrategy()
84+
.withRegEx(".*database system is ready to accept connections.*\\s")
85+
.withTimes(2)
86+
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS)))
87+
.withCommand("postgres", "-c", "fsync=off")
7988
.withClasspathResourceMapping("create-postgres.sql", "/docker-entrypoint-initdb.d/create-postgres.sql", BindMode.READ_ONLY);
8089
if (ssl) {
8190
server
@@ -89,9 +98,9 @@ private void initServer(String version) throws Exception {
8998
}
9099
}
91100
if (System.getProperties().containsKey("containerFixedPort")) {
92-
server.withFixedExposedPort(POSTGRESQL_PORT, POSTGRESQL_PORT);
101+
server.withFixedExposedPort(DEFAULT_PORT, DEFAULT_PORT);
93102
} else {
94-
server.withExposedPorts(POSTGRESQL_PORT);
103+
server.withExposedPorts(DEFAULT_PORT);
95104
}
96105
}
97106

@@ -108,7 +117,7 @@ public synchronized PgConnectOptions startServer(String databaseVersion) throws
108117
server.start();
109118

110119
return new PgConnectOptions()
111-
.setPort(server.getMappedPort(POSTGRESQL_PORT))
120+
.setPort(server.getMappedPort(DEFAULT_PORT))
112121
.setHost(server.getHost())
113122
.setDatabase("postgres")
114123
.setUser(user)
@@ -183,7 +192,7 @@ protected void after() {
183192
}
184193
}
185194

186-
private static class ServerContainer<SELF extends ServerContainer<SELF>> extends PostgreSQLContainer<SELF> {
195+
private static class ServerContainer<SELF extends ServerContainer<SELF>> extends GenericContainer<SELF> {
187196

188197
public ServerContainer(String dockerImageName) {
189198
super(dockerImageName);

vertx-sql-client-templates/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</dependency>
5353
<dependency>
5454
<groupId>org.testcontainers</groupId>
55-
<artifactId>postgresql</artifactId>
55+
<artifactId>testcontainers</artifactId>
5656
<version>${testcontainers.version}</version>
5757
<scope>test</scope>
5858
</dependency>

vertx-sql-client-templates/src/test/java/io/vertx/sqlclient/templates/PgTemplateTestBase.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,32 @@
1212
import org.junit.Before;
1313
import org.junit.BeforeClass;
1414
import org.junit.runner.RunWith;
15-
import org.testcontainers.containers.PostgreSQLContainer;
15+
import org.testcontainers.containers.GenericContainer;
16+
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
1617

18+
import java.time.Duration;
19+
import java.time.temporal.ChronoUnit;
1720
import java.util.function.Function;
1821

22+
import static io.vertx.pgclient.PgConnectOptions.DEFAULT_PORT;
23+
1924
@RunWith(VertxUnitRunner.class)
2025
public abstract class PgTemplateTestBase {
2126

22-
private static PostgreSQLContainer server;
27+
private static GenericContainer<?> server;
2328

2429
@BeforeClass
2530
public static void startDatabase() {
26-
server = new PostgreSQLContainer("postgres:" + "10.10")
27-
.withDatabaseName("postgres")
28-
.withUsername("postgres")
29-
.withPassword("postgres");
31+
server = new GenericContainer<>("postgres:" + "10.10")
32+
.withEnv("POSTGRES_DB", "postgres")
33+
.withEnv("POSTGRES_USER", "postgres")
34+
.withEnv("POSTGRES_PASSWORD", "postgres")
35+
.withExposedPorts(DEFAULT_PORT)
36+
.waitingFor(new LogMessageWaitStrategy()
37+
.withRegEx(".*database system is ready to accept connections.*\\s")
38+
.withTimes(2)
39+
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS)))
40+
.withCommand("postgres", "-c", "fsync=off");
3041
server.start();
3142
}
3243

@@ -40,7 +51,7 @@ public static void stopDatabase() {
4051
}
4152

4253
public static PgConnectOptions connectOptions() {
43-
Integer port = server.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT);
54+
Integer port = server.getMappedPort(DEFAULT_PORT);
4455
String ip = server.getHost();
4556
return new PgConnectOptions()
4657
.setPort(port)

0 commit comments

Comments
 (0)