Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use org.testcontainers:testcontainers instead of per database specializations #1465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<unix.socket.directory/>
<unix.socket.port/>
<jar.manifest>${project.basedir}/src/main/resources/META-INF/MANIFEST.MF</jar.manifest>
<testcontainers.version>1.17.6</testcontainers.version>
<testcontainers.version>1.20.1</testcontainers.version>
</properties>

<dependencyManagement>
Expand Down
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,12 @@
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 org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;

import static org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

/**
* Postgresql test database based on https://www.testcontainers.org
Expand All @@ -34,11 +36,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 +76,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,15 +184,29 @@ 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);
this.waitStrategy = (new LogMessageWaitStrategy()).withRegEx(".*database system is ready to accept connections.*\\s").withTimes(2).withStartupTimeout(Duration.of(60L, ChronoUnit.SECONDS));
this.setCommand("postgres", "-c", "fsync=off");
this.addExposedPort(POSTGRESQL_PORT);
}

@Override
protected void configure() {
this.addEnv("POSTGRES_DB", "postgres");
this.addEnv("POSTGRES_USER", user);
this.addEnv("POSTGRES_PASSWORD", "postgres");
}

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

protected void waitUntilContainerStarted() {
this.getWaitStrategy().waitUntilReady(this);
}
}
}
Loading