test/jdk/jdk/internal/platform/docker/TestDockerMemoryMetrics.java
changeset 50545 292a4a87c321
child 54217 98a01c1344aa
equal deleted inserted replaced
50544:5f20bf95c052 50545:292a4a87c321
       
     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 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("20m");
       
    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("-cp", "/test-classes/")
       
    94                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
       
    95                 .addClassOptions("failcount");
       
    96         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
       
    97     }
       
    98 
       
    99     private static void testMemoryAndSwapLimit(String memory, String memandswap) throws Exception {
       
   100         Common.logNewTestCase("testMemoryAndSwapLimit, memory = " + memory + ", memory and swap = " + memandswap);
       
   101         DockerRunOptions opts =
       
   102                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
       
   103         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
       
   104                 .addDockerOpts("--memory=" + memory)
       
   105                 .addDockerOpts("--memory-swap=" + memandswap)
       
   106                 .addJavaOpts("-cp", "/test-classes/")
       
   107                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
       
   108                 .addClassOptions("memoryswap", memory, memandswap);
       
   109         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
       
   110     }
       
   111 
       
   112     private static void testKernelMemoryLimit(String value) throws Exception {
       
   113         Common.logNewTestCase("testKernelMemoryLimit, value = " + value);
       
   114         DockerRunOptions opts =
       
   115                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
       
   116         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
       
   117                 .addDockerOpts("--kernel-memory=" + value)
       
   118                 .addJavaOpts("-cp", "/test-classes/")
       
   119                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
       
   120                 .addClassOptions("kernelmem", value);
       
   121         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
       
   122     }
       
   123 
       
   124     private static void testOomKillFlag(String value, boolean oomKillFlag) throws Exception {
       
   125         Common.logNewTestCase("testOomKillFlag, oomKillFlag = " + oomKillFlag);
       
   126         DockerRunOptions opts =
       
   127                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
       
   128         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
       
   129                 .addDockerOpts("--memory=" + value);
       
   130         if (!oomKillFlag) {
       
   131             opts.addDockerOpts("--oom-kill-disable");
       
   132         }
       
   133         opts.addJavaOpts("-cp", "/test-classes/")
       
   134                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
       
   135                 .addClassOptions("memory", value, oomKillFlag + "");
       
   136         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
       
   137     }
       
   138 
       
   139     private static void testMemorySoftLimit(String mem, String softLimit) throws Exception {
       
   140         Common.logNewTestCase("testMemorySoftLimit, memory = " + mem + ", soft limit = " + softLimit);
       
   141         DockerRunOptions opts =
       
   142                 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester");
       
   143         opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
       
   144                 .addDockerOpts("--memory=" + mem)
       
   145                 .addDockerOpts("--memory-reservation=" + softLimit);
       
   146         opts.addJavaOpts("-cp", "/test-classes/")
       
   147                 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED")
       
   148                 .addClassOptions("softlimit", softLimit);
       
   149         DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
       
   150     }
       
   151 }