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

Adding support to spin up docker in a sibling container (from when Ka… #1603

Merged
merged 6 commits into from
May 22, 2021
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ protected DevToolsDriver(DriverOptions options, Command command, String webSocke
logger = options.driverLogger;
this.options = options;
this.command = command;

if (options.isRemoteHost()) {
String host = (String) options.options.get("host");
Integer port = (Integer) options.options.get("port");
webSocketUrl = webSocketUrl.replace("ws://localhost/", "ws://" + host + ":" + port + "/");
}

this.wait = new DevToolsWait(this, options);
int pos = webSocketUrl.lastIndexOf('/');
rootFrameId = webSocketUrl.substring(pos + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.intuit.karate.driver;

import com.intuit.karate.FileUtils;
import com.intuit.karate.KarateException;
import com.intuit.karate.Logger;
import com.intuit.karate.StringUtils;
import com.intuit.karate.core.ScenarioRuntime;
Expand All @@ -33,6 +34,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
Expand All @@ -45,7 +48,6 @@ public class DockerTarget implements Target {
private Function<Integer, String> command;
private final Map<String, Object> options;
private boolean pull = false;

private boolean karateChrome = false;

public DockerTarget(String dockerImage) {
Expand All @@ -72,10 +74,10 @@ public DockerTarget(Map<String, Object> options) {
}
if (imageId != null) {
if (imageId.contains("/chrome-headless")) {
command = p -> sb.toString() + " -p " + p + ":9222 " + imageId;
command = p -> sb.toString() + " -p 9222 " + imageId;
} else if (imageId.contains("/karate-chrome")) {
karateChrome = true;
command = p -> sb.toString() + " -p " + p + ":9222 " + imageId;
command = p -> sb.toString() + " -p 9222 " + imageId;
}
}
} else {
Expand All @@ -100,16 +102,26 @@ public Map<String, Object> start(ScenarioRuntime sr) {
sr.logger.debug("attempting to pull docker image: {}", imageId);
Command.execLine(null, "docker pull " + imageId);
}
int port = Command.getFreePort(0);
containerId = Command.execLine(null, command.apply(port));
Map<String, Object> map = new HashMap();
containerId = Command.execLine(null, command.apply(null));
int port = this.getContainerPort(containerId);
Map<String, Object> map = new HashMap();
if (options != null) {
map.putAll(options);
}

boolean remoteHost = options != null && options.get("remoteHost") != null && (Boolean) options.get("remoteHost");
boolean useDockerHost = options != null && options.get("useDockerHost") != null && (Boolean) options.get("useDockerHost");
String host = "127.0.0.1";
if (remoteHost) {
String containerName = Command.execLine(null, "docker inspect -f '{{.Name}}' " + containerId + " | cut -c 2-");
host = useDockerHost ? "host.docker.internal" : containerName;
}

map.put("start", false);
map.put("host", host);
map.put("port", port);
map.put("type", "chrome");
Command.waitForHttp("http://127.0.0.1:" + port + "/json");
Command.waitForHttp("http://" + host + ":" + port + "/json");
return map;
}

Expand Down Expand Up @@ -139,4 +151,21 @@ public Map<String, Object> stop(ScenarioRuntime sr) {
return Collections.singletonMap("video", copy.getPath());
}

private int getContainerPort(String containerId) {
String dockerPort = Command.execLine((File)null, "docker port " + containerId + " 9222/tcp");
Pattern portPattern = Pattern.compile("(\\d+?\\.){3}\\d:(\\d+)");
Matcher matcher = portPattern.matcher(dockerPort);
if (matcher.matches()) {
try {
return Integer.parseInt(matcher.group(2));
} catch (NumberFormatException e) {
throw new KarateException("Error fetching port from started docker container", e);
}
}
throw new KarateException("Error fetching port from started docker container");
}

public String getContainerId() {
return this.containerId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public String getPreSubmitHash() {
return preSubmitHash;
}

public boolean isRemoteHost() {
return options != null && options.get("remoteHost") != null && (Boolean) options.get("remoteHost");
ptrthomas marked this conversation as resolved.
Show resolved Hide resolved
}

public void setPreSubmitHash(String preSubmitHash) {
this.preSubmitHash = preSubmitHash;
}
Expand Down Expand Up @@ -217,6 +221,9 @@ private int resolvePort(int defaultPort) {

public Http getHttp() {
Http http = Http.to(getUrlBase());
if (this.isRemoteHost()) {
http.header("HOST", "localhost");
}
http.setAppender(driverLogger.getAppender());
if (httpConfig != null) {
http.configure(httpConfig);
Expand Down