1 /*
   2  * Copyright (c) 2018, 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 import jdk.test.lib.Utils;
  25 import jdk.test.lib.containers.docker.Common;
  26 import jdk.test.lib.containers.docker.DockerRunOptions;
  27 import jdk.test.lib.containers.docker.DockerTestUtils;
  28 import jdk.test.lib.process.OutputAnalyzer;
  29 
  30 /*
  31  * @test
  32  * @summary Test JDK Metrics class when running inside docker container
  33  * @requires docker.support
  34  * @library /test/lib
  35  * @modules java.base/jdk.internal.platform
  36  * @build MetricsMemoryTester
  37  * @run main/timeout=360 TestDockerMemoryMetrics
  38  */
  39 
  40 public class TestDockerMemoryMetrics {
  41     private static final String imageName = Common.imageName("metrics-memory");
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (!DockerTestUtils.canTestDocker()) {
  45             return;
  46         }
  47 
  48         // These tests create a docker image and run this image with
  49         // varying docker memory options.  The arguments passed to the docker
  50         // container include the Java test class to be run along with the
  51         // resource to be examined and expected result.
  52 
  53         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  54         try {
  55             testMemoryLimit("200m");
  56             testMemoryLimit("1g");
  57 
  58             testMemoryAndSwapLimit("200m", "1g");
  59             testMemoryAndSwapLimit("100m", "200m");
  60 
  61             testKernelMemoryLimit("100m");
  62             testKernelMemoryLimit("1g");
  63 
  64             testOomKillFlag("100m", false);
  65             testOomKillFlag("100m", true);
  66 
  67             testMemoryFailCount("64m");
  68 
  69             testMemorySoftLimit("500m","200m");
  70 
  71         } finally {
  72             if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {
  73                 DockerTestUtils.removeDockerImage(imageName);
  74             }
  75         }
  76     }
  77 
  78     private static void testMemoryLimit(String value) throws Exception {
  79         Common.logNewTestCase("testMemoryLimit, value = " + value);
  80         DockerRunOptions opts =
  81                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
  82         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  83                 .addDockerOpts("--memory=" + value)
  84                 .addJavaOpts("-cp", "/test-classes/")
  85                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
  86                 .addClassOptions("memory", value);
  87         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
  88     }
  89 
  90     private static void testMemoryFailCount(String value) throws Exception {
  91         Common.logNewTestCase("testMemoryFailCount" + value);
  92         DockerRunOptions opts =
  93                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
  94         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  95                 .addDockerOpts("--memory=" + value)
  96                 .addJavaOpts("-Xmx" + value)
  97                 .addJavaOpts("-cp", "/test-classes/")
  98                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
  99                 .addClassOptions("failcount");
 100         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 101     }
 102 
 103     private static void testMemoryAndSwapLimit(String memory, String memandswap) throws Exception {
 104         Common.logNewTestCase("testMemoryAndSwapLimit, memory = " + memory + ", memory and swap = " + memandswap);
 105         DockerRunOptions opts =
 106                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 107         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 108                 .addDockerOpts("--memory=" + memory)
 109                 .addDockerOpts("--memory-swap=" + memandswap)
 110                 .addJavaOpts("-cp", "/test-classes/")
 111                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 112                 .addClassOptions("memoryswap", memory, memandswap);
 113         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 114     }
 115 
 116     private static void testKernelMemoryLimit(String value) throws Exception {
 117         Common.logNewTestCase("testKernelMemoryLimit, value = " + value);
 118         DockerRunOptions opts =
 119                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 120         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 121                 .addDockerOpts("--kernel-memory=" + value)
 122                 .addJavaOpts("-cp", "/test-classes/")
 123                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 124                 .addClassOptions("kernelmem", value);
 125         OutputAnalyzer oa = DockerTestUtils.dockerRunJava(opts);
 126 
 127         // Some container runtimes (e.g. runc, docker 18.09)
 128         // have been built without kernel memory accounting. In
 129         // that case, the runtime issues a message on stderr saying
 130         // so. Skip the test in that case.
 131         if (oa.getStderr().contains("kernel memory accounting disabled")) {
 132             System.out.println("Kernel memory accounting disabled, " +
 133                                        "skipping the test case");
 134             return;
 135         }
 136         if (oa.getStderr().contains("cannot set kernel memory with cgroupv2")) {
 137             System.out.println("Kernel memory settings not possible with cgroupv2, " +
 138                                        "skipping the test case");
 139             return;
 140         }
 141 
 142         oa.shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 143     }
 144 
 145     private static void testOomKillFlag(String value, boolean oomKillFlag) throws Exception {
 146         Common.logNewTestCase("testOomKillFlag, oomKillFlag = " + oomKillFlag);
 147         DockerRunOptions opts =
 148                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 149         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 150                 .addDockerOpts("--memory=" + value);
 151         if (!oomKillFlag) {
 152             opts.addDockerOpts("--oom-kill-disable");
 153         }
 154         opts.addJavaOpts("-cp", "/test-classes/")
 155                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 156                 .addClassOptions("memory", value, oomKillFlag + "");
 157         OutputAnalyzer oa = DockerTestUtils.dockerRunJava(opts);
 158         if (oa.getStderr().contains("cannot disable OOM killer with cgroupv2")) {
 159             System.out.println("Disabling OOM killer not possible with cgroupv2, " +
 160                                        "skipping the test case");
 161             return;
 162         }
 163 
 164         oa.shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 165     }
 166 
 167     private static void testMemorySoftLimit(String mem, String softLimit) throws Exception {
 168         Common.logNewTestCase("testMemorySoftLimit, memory = " + mem + ", soft limit = " + softLimit);
 169         DockerRunOptions opts =
 170                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 171         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 172                 .addDockerOpts("--memory=" + mem)
 173                 .addDockerOpts("--memory-reservation=" + softLimit);
 174         opts.addJavaOpts("-cp", "/test-classes/")
 175                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 176                 .addClassOptions("softlimit", softLimit);
 177         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 178     }
 179 }