mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 10:03:59 +00:00
#2488 - refactor extract logic into DockerHost helper, fix RedisSetup, pass docker host to ebean-test-docker
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>
|
||||
|
||||
@@ -29,7 +29,6 @@ class Config {
|
||||
private String databaseName;
|
||||
|
||||
private final Properties properties;
|
||||
|
||||
private int port;
|
||||
|
||||
private String url;
|
||||
@@ -37,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;
|
||||
@@ -237,21 +234,8 @@ class Config {
|
||||
}
|
||||
|
||||
String host() {
|
||||
String defaultHost = isInDocker() ? dockerHost() : "localhost";
|
||||
return getPlatformKey("host", defaultHost);
|
||||
}
|
||||
|
||||
static String dockerHost() {
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
boolean isInDocker() {
|
||||
return new File("/.dockerenv").exists();
|
||||
String explicitDockerHost = getKey("dockerHost", null);
|
||||
return getKey("host", dockerHost.dockerHost(explicitDockerHost));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -340,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");
|
||||
}
|
||||
@@ -387,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) {
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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