1 /*
   2  * Copyright (c) 2018, 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  * @test
  27  * @summary Test miscellanous functionality related to JVM running in docker container
  28  * @library /testlibrary /testlibrary/whitebox 
  29  * @build CheckContainerized sun.hotspot.WhiteBox PrintContainerInfo
  30  * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  31  * @run driver TestMisc
  32  */
  33 
  34 import com.oracle.java.testlibrary.Common;
  35 import com.oracle.java.testlibrary.DockerTestUtils;
  36 import com.oracle.java.testlibrary.DockerRunOptions;
  37 import com.oracle.java.testlibrary.OutputAnalyzer;
  38 import com.oracle.java.testlibrary.ProcessTools;
  39 
  40 
  41 public class TestMisc {
  42     private static final String imageName = Common.imageName("misc");
  43 
  44     public static void main(String[] args) throws Exception {
  45         if (!DockerTestUtils.canTestDocker()) {
  46             return;
  47         }
  48 
  49         Common.prepareWhiteBox();
  50         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  51 
  52         try {
  53             testMinusContainerSupport();
  54             testIsContainerized();
  55             testPrintContainerInfo();
  56         } finally {
  57             DockerTestUtils.removeDockerImage(imageName);
  58         }
  59     }
  60 
  61 
  62     private static void testMinusContainerSupport() throws Exception {
  63         Common.logNewTestCase("Test related flags: '-UseContainerSupport'");
  64         DockerRunOptions opts = new DockerRunOptions(imageName, "/jdk/bin/java", "-version");
  65         opts.addJavaOpts("-XX:+UnlockDiagnosticVMOptions", "-XX:-UseContainerSupport", "-XX:+PrintContainerInfo");
  66 
  67         Common.run(opts)
  68             .shouldContain("Container Support not enabled");
  69     }
  70 
  71 
  72     private static void testIsContainerized() throws Exception {
  73         Common.logNewTestCase("Test is_containerized() inside a docker container");
  74 
  75         DockerRunOptions opts = Common.newOpts(imageName, "CheckContainerized");
  76         Common.addWhiteBoxOpts(opts);
  77 
  78         Common.run(opts)
  79             .shouldContain(CheckContainerized.INSIDE_A_CONTAINER);
  80     }
  81 
  82 
  83     private static void testPrintContainerInfo() throws Exception {
  84         Common.logNewTestCase("Test print_container_info()");
  85 
  86         DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo");
  87         Common.addWhiteBoxOpts(opts);
  88 
  89         checkContainerInfo(Common.run(opts));
  90     }
  91 
  92 
  93     private static void checkContainerInfo(OutputAnalyzer out) throws Exception {
  94         String[] expectedToContain = new String[] {
  95             "cpuset.cpus",
  96             "cpuset.mems",
  97             "CPU Shares",
  98             "CPU Quota",
  99             "CPU Period",
 100             "OSContainer::active_processor_count",
 101             "Memory Limit",
 102             "Memory Soft Limit",
 103             "Memory Usage",
 104             "Maximum Memory Usage",
 105             "memory_max_usage_in_bytes"
 106         };
 107 
 108         for (String s : expectedToContain) {
 109             out.shouldContain(s);
 110         }
 111     }
 112 
 113 }