--- old/test/hotspot/jtreg/TEST.ROOT 2017-09-28 15:04:41.724881017 -0700 +++ new/test/hotspot/jtreg/TEST.ROOT 2017-09-28 15:04:41.612881015 -0700 @@ -52,7 +52,8 @@ vm.rtm.cpu \ vm.rtm.os \ vm.aot \ - vm.cds + vm.cds \ + docker.support # Minimum jtreg version requiredVersion=4.2 b08 --- old/test/jtreg-ext/requires/VMProps.java 2017-09-28 15:04:41.996881024 -0700 +++ new/test/jtreg-ext/requires/VMProps.java 2017-09-28 15:04:41.884881021 -0700 @@ -73,6 +73,7 @@ map.put("vm.aot", vmAOT()); // vm.cds is true if the VM is compiled with cds support. map.put("vm.cds", vmCDS()); + map.put("docker.support", dockerSupport()); vmGC(map); // vm.gc.X = true/false VMProps.dump(map); @@ -293,6 +294,33 @@ } /** + * A simple check for docker support + * + * @return true if docker is supported in a given environment + */ + protected String dockerSupport() { + boolean isSupported; + try { + isSupported = checkDockerSupport(); + } catch (Exception e) { + isSupported = false; + System.err.println("dockerSupprt() threw exception: " + e); + } + + return (isSupported) ? "true" : "false"; + } + + private boolean checkDockerSupport() throws IOException, InterruptedException { + ProcessBuilder pb = new ProcessBuilder("docker", "ps"); + Process p = pb.start(); + p.waitFor(); + + return (p.exitValue() == 0); + } + + + + /** * Dumps the map to the file if the file name is given as the property. * This functionality could be helpful to know context in the real * execution. --- /dev/null 2017-09-20 17:23:05.215683345 -0700 +++ new/test/hotspot/jtreg/runtime/containers/docker/DockerBasicTest.java 2017-09-28 15:04:42.160881028 -0700 @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +/* + * @test + * @summary Basic (sanity) test for JDK-under-test inside a docker image. + * @requires (sun.arch.data.model != "32") & (os.family == "linux") + * @requires (docker.support == "true") + * @library /test/lib + * @modules java.base/jdk.internal.misc + * @modules java.management + * jdk.jartool/sun.tools.jar + * @build HelloDocker + * @run main DockerBasicTest + */ +import jdk.test.lib.containers.docker.DockerRunOptions; +import jdk.test.lib.containers.docker.DockerTestUtils; +import jdk.test.lib.Platform; + + +public class DockerBasicTest { + private static final String imageNameAndTag = "jdk10-internal:test"; + + public static void main(String[] args) throws Exception { + if (!DockerTestUtils.canTestDocker()) + return; + + DockerTestUtils.buildJdkDockerImage(imageNameAndTag, + "Dockerfile-BasicTest", "jdk-docker"); + + try { + testJavaVersion(); + testHelloDocker(); + } finally { + DockerTestUtils.removeDockerImage(imageNameAndTag); + } + } + + + private static void testJavaVersion() throws Exception { + DockerTestUtils.dockerRunJava( + new DockerRunOptions(imageNameAndTag, "/jdk/bin/java", "-version")) + .shouldHaveExitValue(0).shouldContain(Platform.vmName); + } + + + private static void testHelloDocker() throws Exception { + DockerRunOptions opts = + new DockerRunOptions(imageNameAndTag, "/jdk/bin/java", "HelloDocker") + .addJavaOpts("-cp", "/test-classes/") + .addDockerOpts("--volume", System.getProperty("test.classes") + ":/test-classes/"); + + DockerTestUtils.dockerRunJava(opts). + shouldHaveExitValue(0).shouldContain("Hello Docker"); + } + +} --- /dev/null 2017-09-20 17:23:05.215683345 -0700 +++ new/test/hotspot/jtreg/runtime/containers/docker/Dockerfile-BasicTest 2017-09-28 15:04:42.460881036 -0700 @@ -0,0 +1,8 @@ +FROM oraclelinux:7.2 +MAINTAINER mikhailo.seledtsov@oracle.com + +COPY /jdk /jdk + +ENV JAVA_HOME=/jdk + +CMD ["/bin/bash"] --- /dev/null 2017-09-20 17:23:05.215683345 -0700 +++ new/test/hotspot/jtreg/runtime/containers/docker/HelloDocker.java 2017-09-28 15:04:42.764881044 -0700 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +public class HelloDocker { + public static void main(String args[]) { + System.out.println("Hello Docker"); + } +} --- /dev/null 2017-09-20 17:23:05.215683345 -0700 +++ new/test/lib/jdk/test/lib/containers/docker/DockerRunOptions.java 2017-09-28 15:04:43.068881051 -0700 @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package jdk.test.lib.containers.docker; + +import java.util.ArrayList; +import java.util.Collections; + + +// This class represents options for running java inside docker containers +// in test environment. +public class DockerRunOptions { + public String imageNameAndTag; + public ArrayList dockerOpts = new ArrayList(); + public String command; // normally a full path to java + public ArrayList javaOpts = new ArrayList(); + public String classToRun; // class or "-version" + public ArrayList classParams = new ArrayList(); + + public boolean tty = true; + public boolean removeContainerAfterUse = true; + public boolean appendTestJavaOptions = true; + public boolean retainChildStdout = false; + + public DockerRunOptions(){} + + + /** + * Convenience constructor for most common use cases in testing. + * @param imageNameAndTag a string representing name and tag for the + * docker image to run, as "name:tag" + * @param javaCmd a java command to run (e.g. /jdk/bin/java) + * @param classToRun a class to run, or "-version" + * @param javaOpts java options to use + * + * @return Default docker run options + */ + public DockerRunOptions(String imageNameAndTag, String javaCmd, + String classToRun, String... javaOpts) { + DockerRunOptions opts = new DockerRunOptions(); + this.imageNameAndTag = imageNameAndTag; + this.command = javaCmd; + this.classToRun = classToRun; + this.addJavaOpts(javaOpts); + } + + public DockerRunOptions addDockerOpts(String... opts) { + Collections.addAll(dockerOpts, opts); + return this; + } + + public DockerRunOptions addJavaOpts(String... opts) { + Collections.addAll(javaOpts, opts); + return this; + } + +} --- /dev/null 2017-09-20 17:23:05.215683345 -0700 +++ new/test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java 2017-09-28 15:04:43.364881059 -0700 @@ -0,0 +1,281 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package jdk.test.lib.containers.docker; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.FileVisitResult; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import jdk.test.lib.Utils; +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; + + +public class DockerTestUtils { + private static final String FS = File.separator; + private static boolean isDockerEngineAvailable = false; + private static boolean wasDockerEngineChecked = false; + + // Diagnostics: set to true to enable more diagnostic info + private static final boolean DEBUG = false; + + /** + * Optimized check of whether the docker engine is available in a given + * environment. Checks only once, then remembers the result in singleton. + * + * @return true if docker engine is available + * @throws Exception + */ + public static boolean isDockerEngineAvailable() throws Exception { + if (wasDockerEngineChecked) + return isDockerEngineAvailable; + + isDockerEngineAvailable = isDockerEngineAvailableCheck(); + wasDockerEngineChecked = true; + return isDockerEngineAvailable; + } + + + /** + * Convenience method, will check if docker engine is available and usable; + * if not then the method will print the appropriate message and return false. + * + * @return Output from process. + * @throws Exception + */ + public static boolean canTestDocker() throws Exception { + if (isDockerEngineAvailable()) { + return true; + } else { + System.out.println("Docker engine is not available on this system"); + System.out.println("This test is SKIPPED"); + return false; + } + } + + + /** + * Simple check - is docker engine available, accessible and usable. + * Run basic docker command: 'docker ps' - list docker instances. + * If docker engine is available and accesible then true is returned + * and we can proceed with testing docker. + * + * @return true if docker engine is available and usable + * @throws Exception + */ + private static boolean isDockerEngineAvailableCheck() + throws Exception { + + try { + execute(true, "docker", "ps").shouldHaveExitValue(0) + .shouldContain("CONTAINER").shouldContain("IMAGE"); + } catch (Exception e) { + return false; + } + return true; + } + + + /** + * Build a docker image that contains JDK under test. + * The jdk will be placed under the "/jdk/" folder inside the docker file system. + * + * @param imageName name of the image to be created, including version tag + * @param dockerfile name of the dockerfile residing in the test source + * @param buildDirName name of the docker build/staging directory, which will + * be created in the jtreg's scratch folder + * @throws Exception + */ + public static void + buildJdkDockerImage(String imageName, String dockerfile, String buildDirName) + throws Exception { + + Path buildDir = Paths.get(".", buildDirName); + if (Files.exists(buildDir)) { + throw new RuntimeException("The docker build directory already exists: " + buildDir); + } + + Path jdkSrcDir = Paths.get(System.getProperty("test.jdk")); + Path jdkDstDir = buildDir.resolve("jdk"); + + Files.createDirectories(jdkDstDir); + + // Copy JDK-under-test tree to the docker build directory. + // This step is required for building a docker image. + Files.walkFileTree(jdkSrcDir, new CopyFileVisitor(jdkSrcDir, jdkDstDir)); + buildDockerImage(imageName, Paths.get(Utils.TEST_SRC, dockerfile), buildDir); + } + + + /** + * Build a docker image based on given docker file and docker build directory. + * + * @param imageName name of the image to be created, including version tag + * @param dockerfile path to the Dockerfile to be used for building the docker + * image. The specified dockerfile will be copied to the docker build + * directory as 'Dockerfile' + * @param buildDir build directory; it should already contain all the content + * needed to build the docker image. + * @throws Exception + */ + public static void + buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception { + + // Copy docker file to the build dir + Files.copy(dockerfile, buildDir.resolve("Dockerfile")); + + // Build the docker + execute(false, "docker", "build", buildDir.toString(), "--no-cache", + "--tag", imageName) + .shouldHaveExitValue(0).shouldContain("Successfully built"); + } + + + /** + * Run Java inside the docker image with specified parameters and options. + * + * @param DockerRunOptions optins for running docker + * @throws Exception + */ + public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception { + ArrayList cmd = new ArrayList<>(); + + cmd.add("docker"); + cmd.add("run"); + if (opts.tty) + cmd.add("--tty=true"); + if (opts.removeContainerAfterUse) + cmd.add("--rm"); + + cmd.addAll(opts.dockerOpts); + cmd.add(opts.imageNameAndTag); + cmd.add(opts.command); + + cmd.addAll(opts.javaOpts); + if (opts.appendTestJavaOptions) { + Collections.addAll(cmd, Utils.getTestJavaOpts()); + } + + cmd.add(opts.classToRun); + cmd.addAll(opts.classParams); + + return execute(opts.retainChildStdout, cmd); + } + + + /** + * Remove docker image + * + * @param DockerRunOptions optins for running docker + * @throws Exception + */ + public static OutputAnalyzer removeDockerImage(String imageNameAndTag) throws Exception { + return execute(false, "docker", "rmi", "--force", imageNameAndTag); + } + + + + /** + * Convenience method - express command as sequence of strings + * + * @param command to execute + * @return The output from the process + * @throws Exception + */ + public static OutputAnalyzer execute(boolean retainChildStdout, String... command) + throws Exception { + ArrayList cmd = new ArrayList(); + Collections.addAll(cmd, command); + return execute(retainChildStdout, cmd); + } + + + /** + * Execute a specified command in a process, report diagnostic info. + * + * @param pb process to be executed specified as ProcessBuilder + * @param retainChildStdout set to true to retain stdout of a child process + * in the main test log + * @return The output from the process + * @throws Exception + */ + public static OutputAnalyzer execute(boolean retainChildStdout, List command) + throws Exception { + + ProcessBuilder pb = new ProcessBuilder(command.toArray(new String[command.size()])); + if (DEBUG) + System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb)); + + long started = System.currentTimeMillis(); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + + System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]"); + System.out.println("[STDERR]\n" + output.getStderr()); + if (retainChildStdout || DEBUG) + System.out.println("[STDOUT]\n" + output.getStdout()); + + return output; + } + + + private static class CopyFileVisitor extends SimpleFileVisitor { + private final Path src; + private final Path dst; + + public CopyFileVisitor(Path src, Path dst) { + this.src = src; + this.dst = dst; + } + + + @Override + public FileVisitResult preVisitDirectory(Path file, + BasicFileAttributes attrs) throws IOException { + Path dstDir = dst.resolve(src.relativize(file)); + if (!dstDir.toFile().exists()) { + Files.createDirectories(dstDir); + } + return FileVisitResult.CONTINUE; + } + + + @Override + public FileVisitResult visitFile(Path file, + BasicFileAttributes attrs) throws IOException { + if (!file.toFile().isFile()) { + return FileVisitResult.CONTINUE; + } + Path dstFile = dst.resolve(src.relativize(file)); + Files.copy(file, dstFile, StandardCopyOption.COPY_ATTRIBUTES); + return FileVisitResult.CONTINUE; + } + } +}