Skip to content

Commit

Permalink
Use org.testcontainers:testcontainers instead of per database special…
Browse files Browse the repository at this point in the history
…izations.

Motivation:

Test containers specializations for database do have a split package with the main test containers jars, preventing running tests in IDE. Since the benefit of these specialization is very thin, the tradeof is not worth and we should use basic testcontainers.

Changes:

Use org.testcontainers:testcontainers everywhere.
  • Loading branch information
vietj committed Sep 10, 2024
1 parent 96eb1c0 commit 42e4143
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion vertx-db2-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<!-- Testing purposes -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>db2</artifactId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import io.vertx.core.net.JksOptions;
import io.vertx.db2client.DB2ConnectOptions;
import org.junit.rules.ExternalResource;
import org.testcontainers.containers.Db2Container;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.InternetProtocol;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;

import java.nio.file.Files;
Expand All @@ -45,12 +46,25 @@ public class DB2Resource extends ExternalResource {
private boolean started = false;
private boolean isDb2OnZ = false;
private DB2ConnectOptions options;
private final Db2Container instance = new Db2Container("ibmcom/db2:11.5.0.0a")
.acceptLicense()
private final String database = "vertx";
private final String user = "vertx";
private final String password = "vertx";
private final ServerContainer instance = new ServerContainer("ibmcom/db2:11.5.0.0a") {
@Override
protected void configure() {
this.addEnv("LICENSE", "accept");
this.addEnv("DBNAME", database);
this.addEnv("DB2INSTANCE", user);
this.addEnv("DB2INST1_PASSWORD", password);
if (!this.getEnvMap().containsKey("AUTOCONFIG")) {
this.addEnv("AUTOCONFIG", "false");
}
if (!this.getEnvMap().containsKey("ARCHIVE_LOGS")) {
this.addEnv("ARCHIVE_LOGS", "false");
}
}
}
.withLogConsumer(out -> System.out.print("[DB2] " + out.getUtf8String()))
.withUsername("vertx")
.withPassword("vertx")
.withDatabaseName("vertx")
.withExposedPorts(50000, 50001)
.withFileSystemBind("src/test/resources/tls/server/", "/certs/")
.withFileSystemBind("src/test/resources/tls/db2_tls_setup.sh", "/var/custom/db2_tls_setup.sh")
Expand All @@ -69,9 +83,9 @@ protected void before() throws Throwable {
options = new DB2ConnectOptions()
.setHost(instance.getHost())
.setPort(instance.getMappedPort(50000))
.setDatabase(instance.getDatabaseName())
.setUser(instance.getUsername())
.setPassword(instance.getPassword());
.setDatabase(database)
.setUser(user)
.setPassword(password);
} else {
System.out.println("Using custom DB2 instance as requested via DB2_HOST=" + get("DB2_HOST"));
Objects.requireNonNull(get("DB2_PORT"), "Must set DB2_PORT to a non-null value if DB2_HOST is set");
Expand Down Expand Up @@ -143,4 +157,15 @@ private void runInitSql(Connection con) throws Exception {
}
}

private class ServerContainer extends GenericContainer<ServerContainer> {

public ServerContainer(String dockerImageName) {
super(dockerImageName);
}

public ServerContainer withFixedExposedPort(int hostPort, int containerPort) {
super.addFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP);
return self();
}
}
}
2 changes: 1 addition & 1 deletion vertx-pg-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import io.vertx.sqlclient.PoolOptions;
import org.junit.rules.ExternalResource;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.InternetProtocol;
import org.testcontainers.containers.PostgreSQLContainer;

import static org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT;

/**
* Postgresql test database based on https://www.testcontainers.org
Expand All @@ -34,11 +32,13 @@
*/
public class ContainerPgRule extends ExternalResource {

private static final int POSTGRESQL_PORT = 5432;

private static final String connectionUri = System.getProperty("connection.uri");
private static final String tlsConnectionUri = System.getProperty("tls.connection.uri");
private static final String tlsForceConnectionUri = System.getProperty("tls.force.connection.uri");

private ServerContainer<?> server;
private ServerContainer server;
private PgConnectOptions options;
private String databaseVersion;
private boolean ssl;
Expand Down Expand Up @@ -72,10 +72,7 @@ public ContainerPgRule user(String user) {
}

private void initServer(String version) throws Exception {
server = new ServerContainer<>("postgres:" + version)
.withDatabaseName("postgres")
.withUsername(user)
.withPassword("postgres")
server = new ServerContainer("postgres:" + version)
.withClasspathResourceMapping("create-postgres.sql", "/docker-entrypoint-initdb.d/create-postgres.sql", BindMode.READ_ONLY);
if (ssl) {
server
Expand Down Expand Up @@ -183,13 +180,20 @@ protected void after() {
}
}

private static class ServerContainer<SELF extends ServerContainer<SELF>> extends PostgreSQLContainer<SELF> {
private class ServerContainer extends GenericContainer<ServerContainer> {

public ServerContainer(String dockerImageName) {
super(dockerImageName);
}

public SELF withFixedExposedPort(int hostPort, int containerPort) {
@Override
protected void configure() {
this.addEnv("POSTGRES_DB", "postgres");
this.addEnv("POSTGRES_USER", user);
this.addEnv("POSTGRES_PASSWORD", "postgres");
}

public ServerContainer withFixedExposedPort(int hostPort, int containerPort) {
super.addFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP);
return self();
}
Expand Down

0 comments on commit 42e4143

Please sign in to comment.