mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge pull request #2488 from ebean-orm/feature/2487
#2487 - ebean-test support Docker In Docker - docker container's hostname rather than hardcoded "localhost"
This commit is contained in:
+1
-1
@@ -56,7 +56,7 @@
|
||||
<dependency>
|
||||
<groupId>io.ebean</groupId>
|
||||
<artifactId>ebean-test-docker</artifactId>
|
||||
<version>4.3</version>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@@ -11,7 +11,7 @@ class ClickHouseSetup implements PlatformSetup {
|
||||
config.setDefaultPort(8123);
|
||||
config.setUsername("default");
|
||||
config.setPassword("");
|
||||
config.setUrl("jdbc:clickhouse://localhost:${port}/${databaseName}");
|
||||
config.setUrl("jdbc:clickhouse://${host}:${port}/${databaseName}");
|
||||
config.setDriver("ru.yandex.clickhouse.ClickHouseDriver");
|
||||
config.datasourceDefaults();
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class CockroachSetup implements PlatformSetup {
|
||||
config.setDefaultPort(26257);
|
||||
config.setUsername("root");
|
||||
config.setPassword("");
|
||||
config.setUrl("jdbc:postgresql://localhost:${port}/${databaseName}?sslmode=disable");
|
||||
config.setUrl("jdbc:postgresql://${host}:${port}/${databaseName}?sslmode=disable");
|
||||
config.setDriver("org.postgresql.Driver");
|
||||
config.datasourceDefaults();
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ import io.ebean.config.DatabaseConfig;
|
||||
import io.ebean.datasource.DataSourceConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
@@ -26,11 +26,9 @@ class Config {
|
||||
private final String db;
|
||||
private final String platform;
|
||||
private String dockerPlatform;
|
||||
|
||||
private String databaseName;
|
||||
|
||||
private final Properties properties;
|
||||
|
||||
private int port;
|
||||
|
||||
private String url;
|
||||
@@ -38,12 +36,10 @@ class Config {
|
||||
private String schema;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
private final DatabaseConfig config;
|
||||
|
||||
private boolean containerDropCreate;
|
||||
|
||||
private final Properties dockerProperties = new Properties();
|
||||
private final DockerHost dockerHost = new DockerHost();
|
||||
|
||||
Config(String db, String platform, String databaseName, DatabaseConfig config) {
|
||||
this.db = db;
|
||||
@@ -231,11 +227,17 @@ class Config {
|
||||
|
||||
void setUrl(String urlPattern) {
|
||||
String val = getPlatformKey("url", urlPattern);
|
||||
val = val.replace("${host}", host());
|
||||
val = val.replace("${port}", String.valueOf(port));
|
||||
val = val.replace("${databaseName}", databaseName);
|
||||
this.url = val;
|
||||
}
|
||||
|
||||
String host() {
|
||||
String explicitDockerHost = getKey("dockerHost", null);
|
||||
return getKey("host", dockerHost.dockerHost(explicitDockerHost));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to the connection URL.
|
||||
*/
|
||||
@@ -322,7 +324,6 @@ class Config {
|
||||
void setDockerVersion(String version) {
|
||||
String val = getPlatformKey("version", version);
|
||||
dockerProperties.setProperty(dockerKey("version"), val);
|
||||
|
||||
if (containerDropCreate) {
|
||||
dockerProperties.setProperty(dockerKey("startMode"), "dropCreate");
|
||||
}
|
||||
@@ -369,7 +370,10 @@ class Config {
|
||||
}
|
||||
|
||||
private void initDockerProperties() {
|
||||
|
||||
if (dockerHost.runningInDocker()) {
|
||||
// tell ebean-docker-test we are not using localhost (for jdbc DB setup commands)
|
||||
dockerProperties.setProperty(dockerKey("host"), dockerHost.dockerHost());
|
||||
}
|
||||
dockerProperties.setProperty(dockerKey("port"), String.valueOf(port));
|
||||
dockerProperties.setProperty(dockerKey("dbName"), databaseName);
|
||||
if (schema != null) {
|
||||
|
||||
@@ -11,7 +11,7 @@ class Db2Setup implements PlatformSetup {
|
||||
config.setDefaultPort(50000);
|
||||
config.setUsernameDefault();
|
||||
config.setPasswordDefault();
|
||||
config.setUrl("jdbc:db2://localhost:${port}/${databaseName}");
|
||||
config.setUrl("jdbc:db2://${host}:${port}/${databaseName}");
|
||||
config.setDriver("com.ibm.db2.jcc.DB2Driver");
|
||||
config.datasourceDefaults();
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package io.ebean.test.config.platform;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Helper to detect if running inside docker and determine host name for that case.
|
||||
*/
|
||||
class DockerHost {
|
||||
|
||||
private final boolean runningInDocker;
|
||||
private String dockerHost;
|
||||
|
||||
DockerHost() {
|
||||
runningInDocker = initInDocker();
|
||||
}
|
||||
|
||||
boolean runningInDocker() {
|
||||
return runningInDocker;
|
||||
}
|
||||
|
||||
String dockerHost() {
|
||||
return dockerHost;
|
||||
}
|
||||
|
||||
String dockerHost(String explicitHost) {
|
||||
if (!runningInDocker) {
|
||||
return "localhost";
|
||||
}
|
||||
dockerHost = explicitHost != null ? explicitHost : defaultDockerHost();
|
||||
return dockerHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if running inside a docker container (we are using docker in docker).
|
||||
*/
|
||||
boolean initInDocker() {
|
||||
return new File("/.dockerenv").exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the default host name to use when running in docker.
|
||||
* <p>
|
||||
* Can instead be explicitly specified via <code>ebean.test.dockerHost</code>.
|
||||
*/
|
||||
String defaultDockerHost() {
|
||||
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
|
||||
if (os.contains("mac") || os.contains("darwin") || os.contains("win")) {
|
||||
return "host.docker.internal";
|
||||
} else {
|
||||
return "172.17.0.1";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class HanaSetup implements PlatformSetup {
|
||||
config.setUsername("SYSTEM");
|
||||
config.setPassword("HXEHana1");
|
||||
config.setDatabaseName("HXE");
|
||||
config.setUrl("jdbc:sap://localhost:${port}/?databaseName=${databaseName}");
|
||||
config.setUrl("jdbc:sap://${host}:${port}/?databaseName=${databaseName}");
|
||||
String schema = config.getSchema();
|
||||
if (schema != null && !schema.equals(config.getUsername())) {
|
||||
config.urlAppend("¤tSchema=" + schema);
|
||||
|
||||
@@ -13,7 +13,7 @@ class MariaDBSetup implements PlatformSetup {
|
||||
config.setDefaultPort(defaultPort);
|
||||
config.setUsernameDefault();
|
||||
config.setPasswordDefault();
|
||||
config.setUrl("jdbc:mariadb://localhost:${port}/${databaseName}?useLegacyDatetimeCode=false");
|
||||
config.setUrl("jdbc:mariadb://${host}:${port}/${databaseName}?useLegacyDatetimeCode=false");
|
||||
config.datasourceDefaults();
|
||||
|
||||
return dockerProperties(config);
|
||||
|
||||
@@ -13,7 +13,7 @@ class MySqlSetup implements PlatformSetup {
|
||||
config.setDefaultPort(defaultPort);
|
||||
config.setUsernameDefault();
|
||||
config.setPasswordDefault();
|
||||
config.setUrl("jdbc:mysql://localhost:${port}/${databaseName}");
|
||||
config.setUrl("jdbc:mysql://${host}:${port}/${databaseName}");
|
||||
config.setDriver(defaultDriver());
|
||||
config.datasourceDefaults();
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class PostgisSetup implements PlatformSetup {
|
||||
config.setUsernameDefault();
|
||||
config.setPasswordDefault();
|
||||
config.setDriver("org.postgis.DriverWrapperLW");
|
||||
config.setUrl("jdbc:postgresql_lwgis://localhost:${port}/${databaseName}");
|
||||
config.setUrl("jdbc:postgresql_lwgis://${host}:${port}/${databaseName}");
|
||||
|
||||
String schema = config.getSchema();
|
||||
if (schema != null && !schema.equals(config.getUsername())) {
|
||||
|
||||
@@ -13,7 +13,7 @@ class PostgresSetup implements PlatformSetup {
|
||||
config.setDefaultPort(defaultPort);
|
||||
config.setUsernameDefault();
|
||||
config.setPasswordDefault();
|
||||
config.setUrl("jdbc:postgresql://localhost:${port}/${databaseName}");
|
||||
config.setUrl("jdbc:postgresql://${host}:${port}/${databaseName}");
|
||||
|
||||
String schema = config.getSchema();
|
||||
if (schema != null && !schema.equals(config.getUsername())) {
|
||||
@@ -44,7 +44,7 @@ class PostgresSetup implements PlatformSetup {
|
||||
config.setDefaultPort(defaultPort);
|
||||
config.setExtraUsernameDefault();
|
||||
config.setExtraDbPasswordDefault();
|
||||
config.setUrl("jdbc:postgresql://localhost:${port}/${databaseName}");
|
||||
config.setUrl("jdbc:postgresql://${host}:${port}/${databaseName}");
|
||||
config.setDriver("org.postgresql.Driver");
|
||||
config.extraDatasourceDefaults();
|
||||
}
|
||||
|
||||
@@ -8,10 +8,14 @@ import java.util.Properties;
|
||||
class RedisSetup {
|
||||
|
||||
static void run(Properties properties) {
|
||||
|
||||
String version = properties.getProperty("ebean.test.redis");
|
||||
version = properties.getProperty("ebean.test.redis.version", version);
|
||||
if (version != null) {
|
||||
DockerHost dockerHost = new DockerHost();
|
||||
if (dockerHost.runningInDocker()) {
|
||||
String host = dockerHost.dockerHost(properties.getProperty("ebean.test.dockerHost"));
|
||||
properties.setProperty("redis.host", host);
|
||||
}
|
||||
RedisConfig redisConfig = new RedisConfig(version, properties);
|
||||
RedisContainer container = new RedisContainer(redisConfig);
|
||||
container.start();
|
||||
|
||||
@@ -13,7 +13,7 @@ class SqlServerSetup implements PlatformSetup {
|
||||
config.setDefaultPort(1433);
|
||||
config.setUsernameDefault();
|
||||
config.setPassword("SqlS3rv#r");
|
||||
config.setUrl("jdbc:sqlserver://localhost:${port};databaseName=${databaseName};sendTimeAsDateTime=false");
|
||||
config.setUrl("jdbc:sqlserver://${host}:${port};databaseName=${databaseName};sendTimeAsDateTime=false");
|
||||
config.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
|
||||
config.datasourceDefaults();
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package io.ebean.test.config.platform;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DockerHostTest {
|
||||
|
||||
@Test
|
||||
void runningInDocker_when_false_alwaysUseLocalhost() {
|
||||
DockerHost dockerHost = new DockerHost();
|
||||
assertFalse(dockerHost.runningInDocker());
|
||||
assertEquals("localhost", dockerHost.dockerHost("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runningInDocker_when_true_useExplicit() {
|
||||
TDDockerHost dockerHost = new TDDockerHost();
|
||||
assertTrue(dockerHost.runningInDocker());
|
||||
|
||||
assertEquals("my-host", dockerHost.dockerHost("my-host"));
|
||||
}
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
void runningInDocker_when_trueAndLinux_useDefault() {
|
||||
TDDockerHost dockerHost = new TDDockerHost();
|
||||
assertTrue(dockerHost.runningInDocker());
|
||||
|
||||
assertEquals("172.17.0.1", dockerHost.dockerHost(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runningInDocker_when_windowsDefault() {
|
||||
TDDockerHost dockerHost = new TDDockerHost();
|
||||
assertTrue(dockerHost.runningInDocker());
|
||||
|
||||
String origName = System.getProperty("os.name");
|
||||
System.setProperty("os.name", "win");
|
||||
try {
|
||||
assertEquals("host.docker.internal",dockerHost.defaultDockerHost());
|
||||
assertEquals("host.docker.internal", dockerHost.dockerHost(null));
|
||||
} finally {
|
||||
System.setProperty("os.name", origName);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void runningInDocker_when_macDefault() {
|
||||
TDDockerHost dockerHost = new TDDockerHost();
|
||||
assertTrue(dockerHost.runningInDocker());
|
||||
|
||||
String origName = System.getProperty("os.name");
|
||||
System.setProperty("os.name", "mac");
|
||||
try {
|
||||
assertEquals("host.docker.internal",dockerHost.defaultDockerHost());
|
||||
assertEquals("host.docker.internal", dockerHost.dockerHost(null));
|
||||
} finally {
|
||||
System.setProperty("os.name", origName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void runningInDocker_when_linuxDefault() {
|
||||
TDDockerHost dockerHost = new TDDockerHost();
|
||||
assertTrue(dockerHost.runningInDocker());
|
||||
|
||||
String origName = System.getProperty("os.name");
|
||||
System.setProperty("os.name", "linux");
|
||||
try {
|
||||
assertEquals("172.17.0.1",dockerHost.defaultDockerHost());
|
||||
assertEquals("172.17.0.1", dockerHost.dockerHost(null));
|
||||
} finally {
|
||||
System.setProperty("os.name", origName);
|
||||
}
|
||||
}
|
||||
|
||||
static class TDDockerHost extends DockerHost {
|
||||
|
||||
@Override
|
||||
boolean initInDocker() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user