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