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