Search in sources :

Example 1 with ProcessInputStream

use of org.jenkinsci.utils.process.ProcessInputStream in project acceptance-test-harness by jenkinsci.

the class FallbackConfig method createContainerWebDriver.

private WebDriver createContainerWebDriver(TestCleaner cleaner, String image, MutableCapabilities capabilities) throws IOException {
    try {
        final int controlPort = IOUtil.randomTcpPort();
        final int vncPort = IOUtil.randomTcpPort(5900, 6000);
        final int displayNumber = vncPort - 5900;
        Path log = Files.createTempFile("ath-docker-browser", "log");
        LOGGER.info("Starting selenium container. Logs in " + log);
        Docker.cmd("pull", image).popen().verifyOrDieWith("Failed to pull image " + image);
        // While this only needs to expose two ports (controlPort, vncPort), it needs to be able to talk to Jenkins running
        // out of container so using host networking is the most straightforward way to go.
        String[] args = { "run", "-d", "--shm-size=2g", "--network=host", "-e", "SE_OPTS=-port " + controlPort, "-e", "DISPLAY=:" + displayNumber, image };
        ProcessInputStream popen = Docker.cmd(args).popen();
        popen.waitFor();
        String cid = popen.verifyOrDieWith("Failed to run selenium container").trim();
        new ProcessBuilder(Docker.cmd("logs", "-f", cid).toCommandArray()).redirectErrorStream(true).redirectOutput(log.toFile()).start();
        Closeable cleanContainer = new Closeable() {

            @Override
            public void close() {
                try {
                    Docker.cmd("kill", cid).popen().verifyOrDieWith("Failed to kill " + cid);
                    Docker.cmd("rm", cid).popen().verifyOrDieWith("Failed to rm " + cid);
                } catch (IOException | InterruptedException e) {
                    throw new Error("Failed removing container", e);
                }
            }

            @Override
            public String toString() {
                return "Kill and remove selenium container";
            }
        };
        // Give the container and selenium some time to spawn
        Thread.sleep(3000);
        try {
            RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL("http://127.0.0.1:" + controlPort + "/wd/hub"), capabilities);
            cleaner.addTask(cleanContainer);
            return remoteWebDriver;
        } catch (RuntimeException e) {
            cleanContainer.close();
            throw e;
        } catch (Throwable e) {
            cleanContainer.close();
            throw new Error(e);
        }
    } catch (InterruptedException e) {
        throw new Error(e);
    }
}
Also used : Path(java.nio.file.Path) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) Closeable(java.io.Closeable) IOException(java.io.IOException) URL(java.net.URL) ProcessInputStream(org.jenkinsci.utils.process.ProcessInputStream)

Aggregations

Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 ProcessInputStream (org.jenkinsci.utils.process.ProcessInputStream)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1