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