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 
  24 
  25 /*
  26  * Methods and definitions common to docker tests container in this directory
  27  */
  28 
  29 import java.nio.file.Files;
  30 import java.nio.file.Paths;
  31 import jdk.test.lib.containers.docker.DockerRunOptions;
  32 import jdk.test.lib.containers.docker.DockerTestUtils;
  33 import jdk.test.lib.Utils;
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 
  36 
  37 public class Common {
  38     public static final String imageNameAndTag = "jdk-internal:test";
  39 
  40     public static String imageName(String suffix) {
  41         return imageNameAndTag + "-" + suffix;
  42     }
  43 
  44 
  45     public static void prepareWhiteBox() throws Exception {
  46         Files.copy(Paths.get(ClassFileInstaller.getJarPath("whitebox.jar")),
  47                    Paths.get(Utils.TEST_CLASSES, "whitebox.jar"));
  48     }
  49 
  50 
  51     // create simple commonly used options
  52     public static DockerRunOptions newOpts(String imageNameAndTag) {
  53         return new DockerRunOptions(imageNameAndTag, "/jdk/bin/java", "-version")
  54             .addJavaOpts("-Xlog:os+container=trace");
  55     }
  56 
  57 
  58     // create commonly used options with class to be launched inside container
  59     public static DockerRunOptions newOpts(String imageNameAndTag, String testClass) {
  60         DockerRunOptions opts =
  61             new DockerRunOptions(imageNameAndTag, "/jdk/bin/java", testClass);
  62         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/");
  63         opts.addJavaOpts("-Xlog:os+container=trace", "-cp", "/test-classes/");
  64         return opts;
  65     }
  66 
  67 
  68     public static DockerRunOptions addWhiteBoxOpts(DockerRunOptions opts) {
  69         opts.addJavaOpts("-Xbootclasspath/a:/test-classes/whitebox.jar",
  70                          "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI");
  71         return opts;
  72     }
  73 
  74 
  75     // most common type of run and checks
  76     public static OutputAnalyzer run(DockerRunOptions opts) throws Exception {
  77         return DockerTestUtils.dockerRunJava(opts)
  78             .shouldHaveExitValue(0).shouldContain("Initializing Container Support");
  79     }
  80 
  81 
  82     // log beginning of a test case
  83     public static void logNewTestCase(String msg) {
  84         System.out.println("========== NEW TEST CASE:      " + msg);
  85     }
  86 
  87 }