--- /dev/null 2017-09-20 17:23:05.215683345 -0700 +++ new/test/hotspot/jtreg/runtime/containers/docker/DockerBasicTest.java 2017-09-21 17:43:17.337889718 -0700 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + */ + +/* + * @test + * @summary Basic (sanity) test for JDK-under-test inside a docker image. + * @requires (sun.arch.data.model != "32") & (os.family == "linux") + * @library /test/lib + * @modules java.base/jdk.internal.misc + * @modules java.management + * jdk.jartool/sun.tools.jar + * @run main DockerBasicTest + */ +import jdk.test.lib.containers.docker.DockerTestUtils; + + +public class DockerBasicTest { + public static void main(String[] args) throws Exception { + if (!DockerTestUtils.canTestDocker()) + return; + + String dockerImageName = "jdk10-internal:test"; + DockerTestUtils.buildJdkDockerImage(dockerImageName, + "Dockerfile-BasicTest", "jdk-docker"); + + testJavaVersion(dockerImageName); + } + + + private static void testJavaVersion(String imageName) throws Exception { + DockerTestUtils.execute("docker", "run", "--tty=true", imageName, + "/jdk/bin/java", "-version") + .shouldHaveExitValue(0) + .shouldContain("Java HotSpot"); + } +} --- /dev/null 2017-09-20 17:23:05.215683345 -0700 +++ new/test/hotspot/jtreg/runtime/containers/docker/Dockerfile-BasicTest 2017-09-21 17:43:17.665889727 -0700 @@ -0,0 +1,8 @@ +FROM oraclelinux:7.0 +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/lib/jdk/test/lib/containers/docker/DockerTestUtils.java 2017-09-21 17:43:17.969889734 -0700 @@ -0,0 +1,270 @@ +/* + * 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 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; + + // diagnostic flag - print output of child process to main test process + private static final boolean PRINT_CHILD_OUTPUT = true; + + /** + * 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 and usable. + // Runs 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. + private static boolean isDockerEngineAvailableCheck() + throws Exception { + + try { + execute(true, false, "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(PRINT_CHILD_OUTPUT, false, + "docker", "build", buildDir.toString(), "--no-cache", "--tag", imageName) + .shouldHaveExitValue(0).shouldContain("Successfully built"); + } + + + /** + * Execute a process with specified command and args, + * and append Java command line options specified for the test run + * at the end of command line. + * + * @param retainChildStdout set true to retain stdout of a child process + * @param appendTestJavaOpts set true to append java options specified + * for jtreg test run to the jdk-under-test command + * @param command the command and parameters to execute + * @return The output from the process + * @throws Exception + */ + public static OutputAnalyzer execute(boolean retainChildStdout, + boolean appendTestJavaOpts, String... command) + throws Exception { + + ArrayList cmd = new ArrayList<>(); + + Collections.addAll(cmd, command); + + if (appendTestJavaOpts) { + Collections.addAll(cmd, Utils.getTestJavaOpts()); + } + + + // Reporting + StringBuilder cmdLine = new StringBuilder(); + for (String s : cmd) + cmdLine.append(s).append(' '); + System.out.println("Command line: [" + cmdLine.toString() + "]"); + + return execute(new ProcessBuilder(cmd.toArray(new String[cmd.size()])), + retainChildStdout); + } + + + /** + * Convenicence method - by defaul retains stdout of child process + * and appends test Java opts. + * + * @param command the command and parameters to execute + * @return The output from the process + * @throws Exception + */ + public static OutputAnalyzer execute(String... command) + throws Exception { + return execute(true, true, command); + } + + + /** + * Execute a process specified by ProcessBuilder, record elapsed time + * and stderr in the main test log, and optionally retain child + * stdout in the main test log. + * + * @param pb process to 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(ProcessBuilder pb, boolean retainChildStdout) + throws Exception { + + 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) + 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; + } + } +}