1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.test.lib.containers.docker;
  24 
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.nio.file.Files;
  28 import java.nio.file.FileVisitResult;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.nio.file.SimpleFileVisitor;
  32 import java.nio.file.StandardCopyOption;
  33 import java.nio.file.attribute.BasicFileAttributes;
  34 import java.util.ArrayList;
  35 import java.util.Collections;
  36 import jdk.test.lib.Utils;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import jdk.test.lib.process.ProcessTools;
  39 
  40 
  41 public class DockerTestUtils {
  42     private static final String FS = File.separator;
  43     private static boolean isDockerEngineAvailable = false;
  44     private static boolean wasDockerEngineChecked = false;
  45 
  46     // diagnostic flag - print output of child process to main test process
  47     private static final boolean PRINT_CHILD_OUTPUT = true;
  48 
  49     /**
  50      * Optimized check of whether the docker engine is available in a given
  51      * environment. Checks only once, then remembers the result in singleton.
  52      *
  53      * @return true if docker engine is available
  54      * @throws Exception
  55      */
  56     public static boolean isDockerEngineAvailable() throws Exception {
  57         if (wasDockerEngineChecked)
  58             return isDockerEngineAvailable;
  59 
  60         isDockerEngineAvailable = isDockerEngineAvailableCheck();
  61         wasDockerEngineChecked = true;
  62         return isDockerEngineAvailable;
  63     }
  64 
  65 
  66     /**
  67      * Convenience method, will check if docker engine is available and usable;
  68      * if not then the method will print the appropriate message and return false.
  69      *
  70      * @return Output from process.
  71      * @throws Exception
  72      */
  73     public static boolean canTestDocker() throws Exception {
  74         if (isDockerEngineAvailable()) {
  75             return true;
  76         } else {
  77             System.out.println("Docker engine is not available on this system");
  78             System.out.println("This test is SKIPPED");
  79             return false;
  80         }
  81     }
  82 
  83 
  84     // Simple check - is docker engine available and usable.
  85     // Runs basic docker command: 'docker ps' - list docker instances.
  86     // If docker engine is available and accesible then true is returned
  87     // and we can proceed with testing docker.
  88     private static boolean isDockerEngineAvailableCheck()
  89         throws Exception {
  90 
  91         try {
  92             execute(true, false, "docker", "ps")
  93                 .shouldHaveExitValue(0)
  94                 .shouldContain("CONTAINER").shouldContain("IMAGE");
  95         } catch (Exception e) {
  96             return false;
  97         }
  98         return true;
  99     }
 100 
 101 
 102     /**
 103      * Build a docker image that contains JDK under test.
 104      * The jdk will be placed under the "/jdk/" folder inside the docker file system.
 105      *
 106      * @param imageName  name of the image to be created, including version tag
 107      * @param dockerfile  name of the dockerfile residing in the test source
 108      * @param buildDirName  name of the docker build/staging directory, which will
 109      *                      be created in the jtreg's scratch folder
 110      * @throws Exception
 111      */
 112     public static void
 113         buildJdkDockerImage(String imageName, String dockerfile, String buildDirName)
 114         throws Exception {
 115 
 116         Path buildDir = Paths.get(".", buildDirName);
 117         if (Files.exists(buildDir)) {
 118             throw new RuntimeException("The docker build directory already exists: " + buildDir);
 119         }
 120 
 121         Path jdkSrcDir = Paths.get(System.getProperty("test.jdk"));
 122         Path jdkDstDir = buildDir.resolve("jdk");
 123 
 124         Files.createDirectories(jdkDstDir);
 125 
 126         // Copy JDK-under-test tree to the docker build directory.
 127         // This step is required for building a docker image.
 128         Files.walkFileTree(jdkSrcDir,
 129                            new CopyFileVisitor(jdkSrcDir, jdkDstDir));
 130         buildDockerImage(imageName,
 131                          Paths.get(Utils.TEST_SRC, dockerfile), buildDir);
 132     }
 133 
 134 
 135     /**
 136      * Build a docker image based on given docker file and docker build directory.
 137      *
 138      * @param imageName  name of the image to be created, including version tag
 139      * @param dockerfile  path to the Dockerfile to be used for building the docker
 140      *        image. The specified dockerfile will be copied to the docker build
 141      *        directory as 'Dockerfile'
 142      * @param buildDir  build directory; it should already contain all the content
 143      *        needed to build the docker image.
 144      * @throws Exception
 145      */
 146     public static void
 147         buildDockerImage(String imageName, Path dockerfile, Path buildDir)
 148         throws Exception {
 149 
 150         // Copy docker file to the build dir
 151         Files.copy(dockerfile, buildDir.resolve("Dockerfile"));
 152 
 153         // Build the docker
 154         execute(PRINT_CHILD_OUTPUT, false,
 155                 "docker", "build", buildDir.toString(), "--no-cache", "--tag", imageName)
 156             .shouldHaveExitValue(0).shouldContain("Successfully built");
 157     }
 158 
 159 
 160     /**
 161      * Execute a process with specified command and args,
 162      * and append Java command line options specified for the test run
 163      * at the end of command line.
 164      *
 165      * @param retainChildStdout set true to retain stdout of a child process
 166      * @param appendTestJavaOpts set true to append java options specified
 167      *        for jtreg test run to the jdk-under-test command
 168      * @param command the command and parameters to execute
 169      * @return The output from the process
 170      * @throws Exception
 171      */
 172     public static OutputAnalyzer execute(boolean retainChildStdout,
 173                                          boolean appendTestJavaOpts, String... command)
 174         throws Exception {
 175 
 176         ArrayList<String> cmd = new ArrayList<>();
 177 
 178         Collections.addAll(cmd, command);
 179 
 180         if (appendTestJavaOpts) {
 181             Collections.addAll(cmd, Utils.getTestJavaOpts());
 182         }
 183 
 184 
 185         // Reporting
 186         StringBuilder cmdLine = new StringBuilder();
 187         for (String s : cmd)
 188             cmdLine.append(s).append(' ');
 189         System.out.println("Command line: [" + cmdLine.toString() + "]");
 190 
 191         return execute(new ProcessBuilder(cmd.toArray(new String[cmd.size()])),
 192                        retainChildStdout);
 193     }
 194 
 195 
 196     /**
 197      * Convenicence method - by defaul retains stdout of child process
 198      * and appends test Java opts.
 199      *
 200      * @param command the command and parameters to execute
 201      * @return The output from the process
 202      * @throws Exception
 203      */
 204     public static OutputAnalyzer execute(String... command)
 205         throws Exception {
 206         return execute(true, true, command);
 207     }
 208 
 209 
 210     /**
 211      * Execute a process specified by ProcessBuilder, record elapsed time
 212      * and stderr in the main test log, and optionally retain child
 213      * stdout in the main test log.
 214      *
 215      * @param pb process to executed specified as ProcessBuilder
 216      * @param retainChildStdout set to true to retain stdout of a child process
 217      *        in the main test log
 218      * @return The output from the process
 219      * @throws Exception
 220      */
 221     public static OutputAnalyzer execute(ProcessBuilder pb, boolean retainChildStdout)
 222         throws Exception {
 223 
 224         long started = System.currentTimeMillis();
 225         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 226 
 227         System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started)
 228                            + " ms]");
 229         System.out.println("[STDERR]\n" + output.getStderr());
 230 
 231         if (retainChildStdout)
 232             System.out.println("[STDOUT]\n" + output.getStdout());
 233 
 234         return output;
 235     }
 236 
 237 
 238     private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
 239         private final Path src;
 240         private final Path dst;
 241 
 242         public CopyFileVisitor(Path src, Path dst) {
 243             this.src = src;
 244             this.dst = dst;
 245         }
 246 
 247 
 248         @Override
 249         public FileVisitResult preVisitDirectory(Path file,
 250                 BasicFileAttributes attrs) throws IOException {
 251             Path dstDir = dst.resolve(src.relativize(file));
 252             if (!dstDir.toFile().exists()) {
 253                 Files.createDirectories(dstDir);
 254             }
 255             return FileVisitResult.CONTINUE;
 256         }
 257 
 258 
 259         @Override
 260         public FileVisitResult visitFile(Path file,
 261                 BasicFileAttributes attrs) throws IOException {
 262             if (!file.toFile().isFile()) {
 263                 return FileVisitResult.CONTINUE;
 264             }
 265             Path dstFile = dst.resolve(src.relativize(file));
 266             Files.copy(file, dstFile, StandardCopyOption.COPY_ATTRIBUTES);
 267             return FileVisitResult.CONTINUE;
 268         }
 269     }
 270 }