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 
  29 /*
  30  * @test
  31  * @summary Test JDK Metrics class when running inside docker container
  32  * @requires docker.support
  33  * @library /test/lib
  34  * @modules java.base/jdk.internal.platform
  35  * @build MetricsMemoryTester
  36  * @run main/timeout=360 TestDockerMemoryMetrics
  37  */
  38 
  39 public class TestDockerMemoryMetrics {
  40     private static final String imageName = Common.imageName("metrics-memory");
  41 
  42     public static void main(String[] args) throws Exception {
  43         if (!DockerTestUtils.canTestDocker()) {
  44             return;
  45         }
  46 
  47         // These tests create a docker image and run this image with
  48         // varying docker memory options.  The arguments passed to the docker
  49         // container include the Java test class to be run along with the
  50         // resource to be examined and expected result.
  51 
  52         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  53         try {
  54             testMemoryLimit("200m");
  55             testMemoryLimit("1g");
  56 
  57             testMemoryAndSwapLimit("200m", "1g");
  58             testMemoryAndSwapLimit("100m", "200m");
  59 
  60             testKernelMemoryLimit("100m");
  61             testKernelMemoryLimit("1g");
  62 
  63             testOomKillFlag("100m", false);
  64             testOomKillFlag("100m", true);
  65 
  66             testMemoryFailCount("64m");
  67 
  68             testMemorySoftLimit("500m","200m");
  69 
  70         } finally {
  71             DockerTestUtils.removeDockerImage(imageName);
  72         }
  73     }
  74 
  75     private static void testMemoryLimit(String value) throws Exception {
  76         Common.logNewTestCase("testMemoryLimit, value = " + value);
  77         DockerRunOptions opts =
  78                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
  79         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  80                 .addDockerOpts("--memory=" + value)
  81                 .addJavaOpts("-cp", "/test-classes/")
  82                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
  83                 .addClassOptions("memory", value);
  84         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
  85     }
  86 
  87     private static void testMemoryFailCount(String value) throws Exception {
  88         Common.logNewTestCase("testMemoryFailCount" + value);
  89         DockerRunOptions opts =
  90                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
  91         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
  92                 .addDockerOpts("--memory=" + value)
  93                 .addJavaOpts("-Xmx" + value)
  94                 .addJavaOpts("-cp", "/test-classes/")
  95                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
  96                 .addClassOptions("failcount");
  97         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
  98     }
  99 
 100     private static void testMemoryAndSwapLimit(String memory, String memandswap) throws Exception {
 101         Common.logNewTestCase("testMemoryAndSwapLimit, memory = " + memory + ", memory and swap = " + memandswap);
 102         DockerRunOptions opts =
 103                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 104         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 105                 .addDockerOpts("--memory=" + memory)
 106                 .addDockerOpts("--memory-swap=" + memandswap)
 107                 .addJavaOpts("-cp", "/test-classes/")
 108                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 109                 .addClassOptions("memoryswap", memory, memandswap);
 110         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 111     }
 112 
 113     private static void testKernelMemoryLimit(String value) throws Exception {
 114         Common.logNewTestCase("testKernelMemoryLimit, value = " + value);
 115         DockerRunOptions opts =
 116                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 117         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 118                 .addDockerOpts("--kernel-memory=" + value)
 119                 .addJavaOpts("-cp", "/test-classes/")
 120                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 121                 .addClassOptions("kernelmem", value);
 122         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 123     }
 124 
 125     private static void testOomKillFlag(String value, boolean oomKillFlag) throws Exception {
 126         Common.logNewTestCase("testOomKillFlag, oomKillFlag = " + oomKillFlag);
 127         DockerRunOptions opts =
 128                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 129         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 130                 .addDockerOpts("--memory=" + value);
 131         if (!oomKillFlag) {
 132             opts.addDockerOpts("--oom-kill-disable");
 133         }
 134         opts.addJavaOpts("-cp", "/test-classes/")
 135                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 136                 .addClassOptions("memory", value, oomKillFlag + "");
 137         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 138     }
 139 
 140     private static void testMemorySoftLimit(String mem, String softLimit) throws Exception {
 141         Common.logNewTestCase("testMemorySoftLimit, memory = " + mem + ", soft limit = " + softLimit);
 142         DockerRunOptions opts =
 143                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
 144         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
 145                 .addDockerOpts("--memory=" + mem)
 146                 .addDockerOpts("--memory-reservation=" + softLimit);
 147         opts.addJavaOpts("-cp", "/test-classes/")
 148                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
 149                 .addClassOptions("softlimit", softLimit);
 150         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
 151     }
 152 }