8181592: [TESTBUG] Docker test utils and docker jdk basic test
authormseledtsov
Tue, 10 Oct 2017 19:18:36 -0700
changeset 47615 6210a27c3704
parent 47614 0ecfd6c951a6
child 47616 4f26db3c02af
8181592: [TESTBUG] Docker test utils and docker jdk basic test Summary: Implemented docker test utilities and basic test Reviewed-by: iignatyev, lmesnik, gtriantafill
test/hotspot/jtreg/TEST.ROOT
test/hotspot/jtreg/runtime/containers/docker/DockerBasicTest.java
test/hotspot/jtreg/runtime/containers/docker/Dockerfile-BasicTest
test/hotspot/jtreg/runtime/containers/docker/HelloDocker.java
test/jtreg-ext/requires/VMProps.java
test/lib/jdk/test/lib/containers/docker/DockerRunOptions.java
test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java
--- a/test/hotspot/jtreg/TEST.ROOT	Tue Oct 10 14:38:56 2017 -0700
+++ b/test/hotspot/jtreg/TEST.ROOT	Tue Oct 10 19:18:36 2017 -0700
@@ -53,7 +53,8 @@
     vm.rtm.os \
     vm.aot \
     vm.cds \
-    vm.graal.enabled
+    vm.graal.enabled \
+    docker.support
 
 # Minimum jtreg version
 requiredVersion=4.2 b08
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/containers/docker/DockerBasicTest.java	Tue Oct 10 19:18:36 2017 -0700
@@ -0,0 +1,83 @@
+/*
+ * 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 (docker.support)
+ * @library /test/lib
+ * @modules java.base/jdk.internal.misc
+ *          java.management
+ *          jdk.jartool/sun.tools.jar
+ * @build HelloDocker
+ * @run driver DockerBasicTest
+ */
+import jdk.test.lib.containers.docker.DockerRunOptions;
+import jdk.test.lib.containers.docker.DockerTestUtils;
+import jdk.test.lib.Platform;
+import jdk.test.lib.Utils;
+
+
+public class DockerBasicTest {
+    private static final String imageNameAndTag = "jdk10-internal:test";
+    // Diganostics: set to false to examine image after the test
+    private static final boolean removeImageAfterTest = true;
+
+    public static void main(String[] args) throws Exception {
+        if (!DockerTestUtils.canTestDocker()) {
+            return;
+        }
+
+        DockerTestUtils.buildJdkDockerImage(imageNameAndTag, "Dockerfile-BasicTest", "jdk-docker");
+
+        try {
+            testJavaVersion();
+            testHelloDocker();
+        } finally {
+            if (removeImageAfterTest)
+                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", Utils.TEST_CLASSES + ":/test-classes/");
+
+        DockerTestUtils.dockerRunJava(opts)
+            .shouldHaveExitValue(0)
+            .shouldContain("Hello Docker");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/containers/docker/Dockerfile-BasicTest	Tue Oct 10 19:18:36 2017 -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	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/containers/docker/HelloDocker.java	Tue Oct 10 19:18:36 2017 -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");
+    }
+}
--- a/test/jtreg-ext/requires/VMProps.java	Tue Oct 10 14:38:56 2017 -0700
+++ b/test/jtreg-ext/requires/VMProps.java	Tue Oct 10 19:18:36 2017 -0700
@@ -32,6 +32,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -75,6 +76,7 @@
         map.put("vm.cds", vmCDS());
         // vm.graal.enabled is true if Graal is used as JIT
         map.put("vm.graal.enabled", isGraalEnabled());
+        map.put("docker.support", dockerSupport());
         vmGC(map); // vm.gc.X = true/false
 
         VMProps.dump(map);
@@ -329,6 +331,38 @@
         return "true";
     }
 
+
+   /**
+     * A simple check for docker support
+     *
+     * @return true if docker is supported in a given environment
+     */
+    protected String dockerSupport() {
+        // currently docker testing is only supported for Linux-x64
+        if (! ( Platform.isLinux() && Platform.isX64() ) )
+            return "false";
+
+        boolean isSupported;
+        try {
+            isSupported = checkDockerSupport();
+        } catch (Exception e) {
+            isSupported = false;
+            System.err.println("dockerSupport() 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(10, TimeUnit.SECONDS);
+
+        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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/lib/jdk/test/lib/containers/docker/DockerRunOptions.java	Tue Oct 10 19:18:36 2017 -0700
@@ -0,0 +1,72 @@
+/*
+ * 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<String> dockerOpts = new ArrayList<String>();
+    public String command;    // normally a full path to java
+    public ArrayList<String> javaOpts = new ArrayList<String>();
+    public String classToRun;  // class or "-version"
+    public ArrayList<String> classParams = new ArrayList<String>();
+
+    public boolean tty = true;
+    public boolean removeContainerAfterUse = true;
+    public boolean appendTestJavaOptions = true;
+    public boolean retainChildStdout = false;
+
+    /**
+     * 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) {
+        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	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java	Tue Oct 10 19:18:36 2017 -0700
@@ -0,0 +1,276 @@
+/*
+ * 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 a 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;
+     * will print the appropriate message when not available.
+     *
+     * @return true if docker engine is available
+     * @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("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(Utils.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("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
+     *
+     * @return output of the run command
+     * @throws Exception
+     */
+    public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
+        ArrayList<String> 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(cmd);
+    }
+
+
+     /**
+     * Remove docker image
+     *
+     * @param DockerRunOptions optins for running docker
+     * @return output of the command
+     * @throws Exception
+     */
+    public static OutputAnalyzer removeDockerImage(String imageNameAndTag) throws Exception {
+        return execute("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(List<String> command) throws Exception {
+        return execute(command.toArray(new String[command.size()]));
+    }
+
+
+    /**
+     * Execute a specified command in a process, report diagnostic info.
+     *
+     * @param command to be executed
+     * @return The output from the process
+     * @throws Exception
+     */
+    public static OutputAnalyzer execute(String... command) throws Exception {
+
+        ProcessBuilder pb = new ProcessBuilder(command);
+        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());
+        System.out.println("[STDOUT]\n" + output.getStdout());
+
+        return output;
+    }
+
+
+    private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
+        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;
+        }
+    }
+}