Skip to content

Commit

Permalink
Allow defining a fixed port for datasource DevServices
Browse files Browse the repository at this point in the history
Fixes quarkusio#16768

(cherry picked from commit ba938ce)
  • Loading branch information
gsmet committed May 11, 2021
1 parent acd4a0f commit f5736ea
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.io.Closeable;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

public interface DevServicesDatasourceProvider {

RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName,
Optional<String> imageName, Map<String, String> additionalProperties);
Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt port);

class RunningDevServicesDatasource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ private DevServicesDatasourceResultBuildItem.DbResult startDevDb(String dbName,
.startDatabase(ConfigProvider.getConfig().getOptionalValue(prefix + "username", String.class),
ConfigProvider.getConfig().getOptionalValue(prefix + "password", String.class),
Optional.ofNullable(dbName), dataSourceBuildTimeConfig.devservices.imageName,
dataSourceBuildTimeConfig.devservices.properties);
dataSourceBuildTimeConfig.devservices.properties,
dataSourceBuildTimeConfig.devservices.port);
closeableList.add(datasource.getCloseTask());

Map<String, String> devDebProperties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
Expand Down Expand Up @@ -32,4 +33,12 @@ public class DevServicesBuildTimeConfig {
*/
@ConfigItem
public Map<String, String> properties;

/**
* Optional fixed port the dev service will listen to.
* <p>
* If not defined, the port will be chosen randomly.
*/
@ConfigItem
public OptionalInt port;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import org.testcontainers.containers.Db2Container;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -25,13 +26,22 @@ DevServicesDatasourceProviderBuildItem setupDB2() {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.DB2, new DevServicesDatasourceProvider() {
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties) {
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt fixedExposedPort) {
Db2Container container = new Db2Container(
DockerImageName.parse(imageName.orElse("ibmcom/db2:" + TAG))
.asCompatibleSubstituteFor(DockerImageName.parse("ibmcom/db2")))
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
.asCompatibleSubstituteFor(DockerImageName.parse("ibmcom/db2"))) {
@Override
protected void configure() {
super.configure();
if (fixedExposedPort.isPresent()) {
addFixedExposedPort(fixedExposedPort.getAsInt(), DB2_PORT);
}
};
}
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
additionalProperties.forEach(container::withUrlParam);
container.start();
return new RunningDevServicesDatasource(container.getJdbcUrl(), container.getUsername(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import org.apache.derby.drda.NetworkServerControl;

Expand All @@ -23,9 +25,11 @@ DevServicesDatasourceProviderBuildItem setupDerby() {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.DERBY, new DevServicesDatasourceProvider() {
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties) {
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt fixedExposedPort) {
try {
NetworkServerControl server = new NetworkServerControl();
int port = fixedExposedPort.isPresent() ? fixedExposedPort.getAsInt() : 1527;
NetworkServerControl server = new NetworkServerControl(InetAddress.getByName("localhost"), port);
server.start(new PrintWriter(System.out));
for (int i = 1; i <= NUMBER_OF_PINGS; i++) {
try {
Expand All @@ -52,15 +56,16 @@ public RunningDevServicesDatasource startDatabase(Optional<String> username, Opt
additionalArgs.append(i.getValue());
}
return new RunningDevServicesDatasource(
"jdbc:derby://localhost:1527/memory:" + datasourceName.orElse("quarkus") + ";create=true"
"jdbc:derby://localhost:" + port + "/memory:" + datasourceName.orElse("quarkus") + ";create=true"
+ additionalArgs.toString(),
null,
null,
new Closeable() {
@Override
public void close() throws IOException {
try {
NetworkServerControl server = new NetworkServerControl();
NetworkServerControl server = new NetworkServerControl(
InetAddress.getByName("localhost"), port);
server.shutdown();
System.out.println("[INFO] Derby database was shut down");
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.sql.Statement;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import org.h2.tools.Server;

Expand All @@ -23,9 +24,11 @@ DevServicesDatasourceProviderBuildItem setupH2() {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.H2, new DevServicesDatasourceProvider() {
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties) {
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt port) {
try {
final Server tcpServer = Server.createTcpServer("-tcpPort", "0");
final Server tcpServer = Server.createTcpServer("-tcpPort",
port.isPresent() ? String.valueOf(port.getAsInt()) : "0");
tcpServer.start();

StringBuilder additionalArgs = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -22,13 +23,22 @@ DevServicesDatasourceProviderBuildItem setupMariaDB() {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.MARIADB, new DevServicesDatasourceProvider() {
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties) {
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt fixedExposedPort) {
MariaDBContainer container = new MariaDBContainer(
DockerImageName.parse(imageName.orElse(MariaDBContainer.IMAGE + ":" + TAG))
.asCompatibleSubstituteFor(DockerImageName.parse(MariaDBContainer.IMAGE)))
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
.asCompatibleSubstituteFor(DockerImageName.parse(MariaDBContainer.IMAGE))) {
@Override
protected void configure() {
super.configure();
if (fixedExposedPort.isPresent()) {
addFixedExposedPort(fixedExposedPort.getAsInt(), 3306);
}
};
}
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
additionalProperties.forEach(container::withUrlParam);
container.start();
return new RunningDevServicesDatasource(container.getJdbcUrl(), container.getUsername(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MSSQLServerContainer;
Expand All @@ -26,12 +27,21 @@ DevServicesDatasourceProviderBuildItem setupMSSQL() {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.MSSQL, new DevServicesDatasourceProvider() {
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties) {
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt fixedExposedPort) {
JdbcDatabaseContainer container = new MSSQLServerContainer(
DockerImageName
.parse(imageName.orElse(MSSQLServerContainer.IMAGE + ":" + TAG))
.asCompatibleSubstituteFor(MSSQLServerContainer.IMAGE))
.withPassword(password.orElse("Quarkuspassword1"));
.asCompatibleSubstituteFor(MSSQLServerContainer.IMAGE)) {
@Override
protected void configure() {
super.configure();
if (fixedExposedPort.isPresent()) {
addFixedExposedPort(fixedExposedPort.getAsInt(), MSSQLServerContainer.MS_SQL_SERVER_PORT);
}
};
}
.withPassword(password.orElse("Quarkuspassword1"));
additionalProperties.forEach(container::withUrlParam);
container.start();
return new RunningDevServicesDatasource(container.getJdbcUrl(), container.getUsername(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -22,13 +23,22 @@ DevServicesDatasourceProviderBuildItem setupMysql() {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.MYSQL, new DevServicesDatasourceProvider() {
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties) {
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt fixedExposedPort) {
MySQLContainer container = new MySQLContainer(
DockerImageName.parse(imageName.orElse(MySQLContainer.IMAGE + ":" + TAG))
.asCompatibleSubstituteFor(DockerImageName.parse(MySQLContainer.IMAGE)))
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
.asCompatibleSubstituteFor(DockerImageName.parse(MySQLContainer.IMAGE))) {
@Override
protected void configure() {
super.configure();
if (fixedExposedPort.isPresent()) {
addFixedExposedPort(fixedExposedPort.getAsInt(), MySQLContainer.MYSQL_PORT);
}
};
}
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
additionalProperties.forEach(container::withUrlParam);
container.start();
return new RunningDevServicesDatasource(container.getJdbcUrl(), container.getUsername(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;

import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -22,13 +23,22 @@ DevServicesDatasourceProviderBuildItem setupPostgres() {
return new DevServicesDatasourceProviderBuildItem(DatabaseKind.POSTGRESQL, new DevServicesDatasourceProvider() {
@Override
public RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties) {
Optional<String> datasourceName, Optional<String> imageName, Map<String, String> additionalProperties,
OptionalInt fixedExposedPort) {
PostgreSQLContainer container = new PostgreSQLContainer(
DockerImageName.parse(imageName.orElse(PostgreSQLContainer.IMAGE + ":" + TAG))
.asCompatibleSubstituteFor(DockerImageName.parse(PostgreSQLContainer.IMAGE)))
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
.asCompatibleSubstituteFor(DockerImageName.parse(PostgreSQLContainer.IMAGE))) {
@Override
protected void configure() {
super.configure();
if (fixedExposedPort.isPresent()) {
addFixedExposedPort(fixedExposedPort.getAsInt(), PostgreSQLContainer.POSTGRESQL_PORT);
}
};
}
.withPassword(password.orElse("quarkus"))
.withUsername(username.orElse("quarkus"))
.withDatabaseName(datasourceName.orElse("default"));
additionalProperties.forEach(container::withUrlParam);
container.start();
return new RunningDevServicesDatasource(container.getJdbcUrl(), container.getUsername(),
Expand Down

0 comments on commit f5736ea

Please sign in to comment.