Skip to content
Snippets Groups Projects
Commit 67439623 authored by Marius Dumitru Florea's avatar Marius Dumitru Florea
Browse files

[misc] Fix the realtime tests when running on CI (inside the xwiki/build...

[misc] Fix the realtime tests when running on CI (inside the xwiki/build Docker container with Jetty Standalone as servlet engine). In this case we shouldn't map the XWiki aliases to the Docker host but rather to the IP of the xwiki/build Docker container, but we need to determine it.

(cherry picked from commit 533156be)
parent fae627fd
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,11 @@
import static org.xwiki.test.docker.internal.junit5.DockerTestUtils.startContainer;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.BrowserWebDriverContainer;
......@@ -28,6 +33,7 @@
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.xwiki.test.docker.internal.junit5.AbstractContainerExecutor;
import org.xwiki.test.docker.internal.junit5.BrowserTestUtils;
import org.xwiki.test.docker.internal.junit5.DockerTestUtils;
import org.xwiki.test.docker.junit5.TestConfiguration;
import org.xwiki.test.docker.junit5.browser.Browser;
......@@ -100,9 +106,13 @@ public BrowserWebDriverContainer start() throws Exception
if (testConfiguration.getServletEngine().isOutsideDocker()) {
// The servlet engine is running on the host so we need to map the servlet engine aliases to the host in
// order for the browser to be able to access XWiki using the configured aliases.
testConfiguration.getServletEngineNetworkAliases().forEach(alias -> {
webDriverContainer.withExtraHost(alias, "host-gateway");
});
String host = DockerTestUtils.isInAContainer() ? getXWikiBuildContainerIPAddress() : "host-gateway";
if (this.testConfiguration.isVerbose()) {
LOGGER.info("Mapping servlet engine network aliases [{}] to [{}].",
testConfiguration.getServletEngineNetworkAliases(), host);
}
testConfiguration.getServletEngineNetworkAliases()
.forEach(alias -> webDriverContainer.withExtraHost(alias, host));
}
// In case some test-resources are provided, they need to be available from the browser
......@@ -125,6 +135,31 @@ public BrowserWebDriverContainer start() throws Exception
return webDriverContainer;
}
/**
* @return the IP address of the Docker container running the XWiki build
*/
private String getXWikiBuildContainerIPAddress()
{
// This gives the local address that would be used to connect to the specified remote host. There is no real
// connection established, hence the specified remote IP can be unreachable.
try (DatagramSocket socket = new DatagramSocket()) {
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
String ipAddress = socket.getLocalAddress().getHostAddress();
if (this.testConfiguration.isVerbose()) {
LOGGER.info("The IP address of the XWiki build container is [{}].", ipAddress);
}
return ipAddress;
} catch (IOException e) {
// We assume the build container runs directly on the Docker host and uses the default bridge network.
String defaultIPAddress = "172.17.0.2";
LOGGER.warn(
"Failed to determine the IP address of the XWiki build container. Root cause: [{}]. "
+ "Falling back to the default IP address [{}].",
ExceptionUtils.getRootCauseMessage(e), defaultIPAddress);
return defaultIPAddress;
}
}
/**
* @return the path where the test resources are stored by Maven after test compilation, on the host.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment