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 DockerTestUtils.removeDockerImage(imageName); 73 } 74 } 75 76 private static void testMemoryLimit(String value) throws Exception { 77 Common.logNewTestCase("testMemoryLimit, value = " + value); 78 DockerRunOptions opts = 79 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 80 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 81 .addDockerOpts("--memory=" + value) 82 .addJavaOpts("-cp", "/test-classes/") 83 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 84 .addClassOptions("memory", value); 85 DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 86 } 87 88 private static void testMemoryFailCount(String value) throws Exception { 89 Common.logNewTestCase("testMemoryFailCount" + value); 90 DockerRunOptions opts = 91 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 92 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 93 .addDockerOpts("--memory=" + value) 94 .addJavaOpts("-Xmx" + value) 95 .addJavaOpts("-cp", "/test-classes/") 96 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 97 .addClassOptions("failcount"); 98 DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 99 } 100 101 private static void testMemoryAndSwapLimit(String memory, String memandswap) throws Exception { 102 Common.logNewTestCase("testMemoryAndSwapLimit, memory = " + memory + ", memory and swap = " + memandswap); 103 DockerRunOptions opts = 104 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 105 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 106 .addDockerOpts("--memory=" + memory) 107 .addDockerOpts("--memory-swap=" + memandswap) 108 .addJavaOpts("-cp", "/test-classes/") 109 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 110 .addClassOptions("memoryswap", memory, memandswap); 111 DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 112 } 113 114 private static void testKernelMemoryLimit(String value) throws Exception { 115 Common.logNewTestCase("testKernelMemoryLimit, value = " + value); 116 DockerRunOptions opts = 117 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 118 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 119 .addDockerOpts("--kernel-memory=" + value) 120 .addJavaOpts("-cp", "/test-classes/") 121 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 122 .addClassOptions("kernelmem", value); 123 OutputAnalyzer oa = DockerTestUtils.dockerRunJava(opts); 124 125 // Some container runtimes (e.g. runc, docker 18.09) 126 // have been built without kernel memory accounting. In 127 // that case, the runtime issues a message on stderr saying 128 // so. Skip the test in that case. 129 if (oa.getStderr().contains("kernel memory accounting disabled")) { 130 System.out.println("Kernel memory accounting disabled, " + 131 "skipping the test case"); 132 return; 133 } 134 135 oa.shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 136 } 137 138 private static void testOomKillFlag(String value, boolean oomKillFlag) throws Exception { 139 Common.logNewTestCase("testOomKillFlag, oomKillFlag = " + oomKillFlag); 140 DockerRunOptions opts = 141 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 142 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 143 .addDockerOpts("--memory=" + value); 144 if (!oomKillFlag) { 145 opts.addDockerOpts("--oom-kill-disable"); 146 } 147 opts.addJavaOpts("-cp", "/test-classes/") 148 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 149 .addClassOptions("memory", value, oomKillFlag + ""); 150 DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 151 } 152 153 private static void testMemorySoftLimit(String mem, String softLimit) throws Exception { 154 Common.logNewTestCase("testMemorySoftLimit, memory = " + mem + ", soft limit = " + softLimit); 155 DockerRunOptions opts = 156 new DockerRunOptions(imageName, "/jdk/bin/java", "MetricsMemoryTester"); 157 opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/") 158 .addDockerOpts("--memory=" + mem) 159 .addDockerOpts("--memory-reservation=" + softLimit); 160 opts.addJavaOpts("-cp", "/test-classes/") 161 .addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED") 162 .addClassOptions("softlimit", softLimit); 163 DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!"); 164 } 165 }