< prev index next >

test/lib/jdk/test/lib/containers/docker/DockerTestUtils.java

Print this page
@  rev 55754 : 8228434: jdk/net/Sockets/Test.java fails after JDK-8227642
|  Summary: Move container constant to separate test lib class
~  Reviewed-by: duke
o  rev 55753 : 8228434: jdk/net/Sockets/Test.java fails after JDK-8227642
|  Reviewed-by: duke
~


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.containers.docker;
  25 
  26 import java.io.File;
  27 import java.io.FileWriter;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.FileVisitResult;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.nio.file.SimpleFileVisitor;
  34 import java.nio.file.StandardCopyOption;
  35 import java.nio.file.attribute.BasicFileAttributes;
  36 import java.util.Arrays;
  37 import java.util.ArrayList;
  38 import java.util.Collections;
  39 import java.util.List;
  40 import jdk.test.lib.Platform;
  41 import jdk.test.lib.Utils;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.process.ProcessTools;
  44 import jtreg.SkippedException;
  45 
  46 
  47 public class DockerTestUtils {
  48     private static final String FS = File.separator;
  49     private static boolean isDockerEngineAvailable = false;
  50     private static boolean wasDockerEngineChecked = false;
  51 
  52     // Specifies how many lines to copy from child STDOUT to main test output.
  53     // Having too many lines in the main test output will result
  54     // in JT harness trimming the output, and can lead to loss of useful
  55     // diagnostic information.
  56     private static final int MAX_LINES_TO_COPY_FOR_CHILD_STDOUT = 100;
  57 
  58     // Set this property to true to retain image after test. By default
  59     // images are removed after test execution completes.
  60     // Retaining the image can be useful for diagnostics and image inspection.


  95     public static boolean canTestDocker() throws Exception {
  96         if (isDockerEngineAvailable()) {
  97             return true;
  98         } else {
  99             throw new SkippedException("Docker engine is not available on this system");
 100         }
 101     }
 102 
 103 
 104     /**
 105      * Simple check - is docker engine available, accessible and usable.
 106      * Run basic docker command: 'docker ps' - list docker instances.
 107      * If docker engine is available and accesible then true is returned
 108      * and we can proceed with testing docker.
 109      *
 110      * @return true if docker engine is available and usable
 111      * @throws Exception
 112      */
 113     private static boolean isDockerEngineAvailableCheck() throws Exception {
 114         try {
 115             execute(Platform.DOCKER_COMMAND, "ps")
 116                 .shouldHaveExitValue(0)
 117                 .shouldContain("CONTAINER")
 118                 .shouldContain("IMAGE");
 119         } catch (Exception e) {
 120             return false;
 121         }
 122         return true;
 123     }
 124 
 125 
 126     /**
 127      * Build a docker image that contains JDK under test.
 128      * The jdk will be placed under the "/jdk/" folder inside the docker file system.
 129      *
 130      * @param imageName     name of the image to be created, including version tag
 131      * @param dockerfile    name of the dockerfile residing in the test source;
 132      *                      we check for a platform specific dockerfile as well
 133      *                      and use this one in case it exists
 134      * @param buildDirName  name of the docker build/staging directory, which will
 135      *                      be created in the jtreg's scratch folder


 158 
 159     /**
 160      * Build a docker image based on given docker file and docker build directory.
 161      *
 162      * @param imageName  name of the image to be created, including version tag
 163      * @param dockerfile  path to the Dockerfile to be used for building the docker
 164      *        image. The specified dockerfile will be copied to the docker build
 165      *        directory as 'Dockerfile'
 166      * @param buildDir  build directory; it should already contain all the content
 167      *        needed to build the docker image.
 168      * @throws Exception
 169      */
 170     public static void
 171         buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
 172 
 173         generateDockerFile(buildDir.resolve("Dockerfile"),
 174                            DockerfileConfig.getBaseImageName(),
 175                            DockerfileConfig.getBaseImageVersion());
 176         try {
 177             // Build the docker
 178             execute(Platform.DOCKER_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
 179                 .shouldHaveExitValue(0);
 180         } catch (Exception e) {
 181             // If docker image building fails there is a good chance it happens due to environment and/or
 182             // configuration other than product failure. Throw jtreg skipped exception in such case
 183             // instead of failing the test.
 184             throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
 185         }
 186     }
 187 
 188 
 189     /**
 190      * Build the docker command to run java inside a container
 191      *
 192      * @param DockerRunOptions optins for running docker
 193      *
 194      * @return command
 195      * @throws Exception
 196      */
 197     public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
 198         List<String> cmd = new ArrayList<>();
 199 
 200         cmd.add(Platform.DOCKER_COMMAND);
 201         cmd.add("run");
 202         if (opts.tty)
 203             cmd.add("--tty=true");
 204         if (opts.removeContainerAfterUse)
 205             cmd.add("--rm");
 206 
 207         cmd.addAll(opts.dockerOpts);
 208         cmd.add(opts.imageNameAndTag);
 209         cmd.add(opts.command);
 210 
 211         cmd.addAll(opts.javaOpts);
 212         if (opts.appendTestJavaOptions) {
 213             Collections.addAll(cmd, Utils.getTestJavaOpts());
 214         }
 215 
 216         cmd.add(opts.classToRun);
 217         cmd.addAll(opts.classParams);
 218 
 219         return cmd;
 220     }


 222     /**
 223      * Run Java inside the docker image with specified parameters and options.
 224      *
 225      * @param DockerRunOptions optins for running docker
 226      *
 227      * @return output of the run command
 228      * @throws Exception
 229      */
 230     public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
 231         return execute(buildJavaCommand(opts));
 232     }
 233 
 234 
 235      /**
 236      * Remove docker image
 237      *
 238      * @param DockerRunOptions optins for running docker
 239      * @throws Exception
 240      */
 241     public static void removeDockerImage(String imageNameAndTag) throws Exception {
 242             execute(Platform.DOCKER_COMMAND, "rmi", "--force", imageNameAndTag);
 243     }
 244 
 245 
 246 
 247     /**
 248      * Convenience method - express command as sequence of strings
 249      *
 250      * @param command to execute
 251      * @return The output from the process
 252      * @throws Exception
 253      */
 254     public static OutputAnalyzer execute(List<String> command) throws Exception {
 255         return execute(command.toArray(new String[command.size()]));
 256     }
 257 
 258 
 259     /**
 260      * Execute a specified command in a process, report diagnostic info.
 261      *
 262      * @param command to be executed




  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.containers.docker;
  25 
  26 import java.io.File;
  27 import java.io.FileWriter;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.FileVisitResult;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.nio.file.SimpleFileVisitor;
  34 import java.nio.file.StandardCopyOption;
  35 import java.nio.file.attribute.BasicFileAttributes;
  36 import java.util.Arrays;
  37 import java.util.ArrayList;
  38 import java.util.Collections;
  39 import java.util.List;
  40 import jdk.test.lib.Container;
  41 import jdk.test.lib.Utils;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.process.ProcessTools;
  44 import jtreg.SkippedException;
  45 
  46 
  47 public class DockerTestUtils {
  48     private static final String FS = File.separator;
  49     private static boolean isDockerEngineAvailable = false;
  50     private static boolean wasDockerEngineChecked = false;
  51 
  52     // Specifies how many lines to copy from child STDOUT to main test output.
  53     // Having too many lines in the main test output will result
  54     // in JT harness trimming the output, and can lead to loss of useful
  55     // diagnostic information.
  56     private static final int MAX_LINES_TO_COPY_FOR_CHILD_STDOUT = 100;
  57 
  58     // Set this property to true to retain image after test. By default
  59     // images are removed after test execution completes.
  60     // Retaining the image can be useful for diagnostics and image inspection.


  95     public static boolean canTestDocker() throws Exception {
  96         if (isDockerEngineAvailable()) {
  97             return true;
  98         } else {
  99             throw new SkippedException("Docker engine is not available on this system");
 100         }
 101     }
 102 
 103 
 104     /**
 105      * Simple check - is docker engine available, accessible and usable.
 106      * Run basic docker command: 'docker ps' - list docker instances.
 107      * If docker engine is available and accesible then true is returned
 108      * and we can proceed with testing docker.
 109      *
 110      * @return true if docker engine is available and usable
 111      * @throws Exception
 112      */
 113     private static boolean isDockerEngineAvailableCheck() throws Exception {
 114         try {
 115             execute(Container.ENGINE_COMMAND, "ps")
 116                 .shouldHaveExitValue(0)
 117                 .shouldContain("CONTAINER")
 118                 .shouldContain("IMAGE");
 119         } catch (Exception e) {
 120             return false;
 121         }
 122         return true;
 123     }
 124 
 125 
 126     /**
 127      * Build a docker image that contains JDK under test.
 128      * The jdk will be placed under the "/jdk/" folder inside the docker file system.
 129      *
 130      * @param imageName     name of the image to be created, including version tag
 131      * @param dockerfile    name of the dockerfile residing in the test source;
 132      *                      we check for a platform specific dockerfile as well
 133      *                      and use this one in case it exists
 134      * @param buildDirName  name of the docker build/staging directory, which will
 135      *                      be created in the jtreg's scratch folder


 158 
 159     /**
 160      * Build a docker image based on given docker file and docker build directory.
 161      *
 162      * @param imageName  name of the image to be created, including version tag
 163      * @param dockerfile  path to the Dockerfile to be used for building the docker
 164      *        image. The specified dockerfile will be copied to the docker build
 165      *        directory as 'Dockerfile'
 166      * @param buildDir  build directory; it should already contain all the content
 167      *        needed to build the docker image.
 168      * @throws Exception
 169      */
 170     public static void
 171         buildDockerImage(String imageName, Path dockerfile, Path buildDir) throws Exception {
 172 
 173         generateDockerFile(buildDir.resolve("Dockerfile"),
 174                            DockerfileConfig.getBaseImageName(),
 175                            DockerfileConfig.getBaseImageVersion());
 176         try {
 177             // Build the docker
 178             execute(Container.ENGINE_COMMAND, "build", "--no-cache", "--tag", imageName, buildDir.toString())
 179                 .shouldHaveExitValue(0);
 180         } catch (Exception e) {
 181             // If docker image building fails there is a good chance it happens due to environment and/or
 182             // configuration other than product failure. Throw jtreg skipped exception in such case
 183             // instead of failing the test.
 184             throw new SkippedException("Building docker image failed. Details: \n" + e.getMessage());
 185         }
 186     }
 187 
 188 
 189     /**
 190      * Build the docker command to run java inside a container
 191      *
 192      * @param DockerRunOptions optins for running docker
 193      *
 194      * @return command
 195      * @throws Exception
 196      */
 197     public static List<String> buildJavaCommand(DockerRunOptions opts) throws Exception {
 198         List<String> cmd = new ArrayList<>();
 199 
 200         cmd.add(Container.ENGINE_COMMAND);
 201         cmd.add("run");
 202         if (opts.tty)
 203             cmd.add("--tty=true");
 204         if (opts.removeContainerAfterUse)
 205             cmd.add("--rm");
 206 
 207         cmd.addAll(opts.dockerOpts);
 208         cmd.add(opts.imageNameAndTag);
 209         cmd.add(opts.command);
 210 
 211         cmd.addAll(opts.javaOpts);
 212         if (opts.appendTestJavaOptions) {
 213             Collections.addAll(cmd, Utils.getTestJavaOpts());
 214         }
 215 
 216         cmd.add(opts.classToRun);
 217         cmd.addAll(opts.classParams);
 218 
 219         return cmd;
 220     }


 222     /**
 223      * Run Java inside the docker image with specified parameters and options.
 224      *
 225      * @param DockerRunOptions optins for running docker
 226      *
 227      * @return output of the run command
 228      * @throws Exception
 229      */
 230     public static OutputAnalyzer dockerRunJava(DockerRunOptions opts) throws Exception {
 231         return execute(buildJavaCommand(opts));
 232     }
 233 
 234 
 235      /**
 236      * Remove docker image
 237      *
 238      * @param DockerRunOptions optins for running docker
 239      * @throws Exception
 240      */
 241     public static void removeDockerImage(String imageNameAndTag) throws Exception {
 242             execute(Container.ENGINE_COMMAND, "rmi", "--force", imageNameAndTag);
 243     }
 244 
 245 
 246 
 247     /**
 248      * Convenience method - express command as sequence of strings
 249      *
 250      * @param command to execute
 251      * @return The output from the process
 252      * @throws Exception
 253      */
 254     public static OutputAnalyzer execute(List<String> command) throws Exception {
 255         return execute(command.toArray(new String[command.size()]));
 256     }
 257 
 258 
 259     /**
 260      * Execute a specified command in a process, report diagnostic info.
 261      *
 262      * @param command to be executed


< prev index next >