Skip to content

Commit 4367869

Browse files
author
duke
committed
Backport 9ca1004e76a614328cd2eb7546143839c4d2f810
1 parent d4adbca commit 4367869

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

test/hotspot/jtreg/containers/docker/TestMemoryWithSubgroups.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
import jdk.test.lib.Container;
2525
import jdk.test.lib.containers.docker.Common;
2626
import jdk.test.lib.containers.docker.DockerTestUtils;
27+
import jdk.test.lib.containers.docker.ContainerRuntimeVersionTestUtils;
2728
import jdk.test.lib.containers.docker.DockerRunOptions;
28-
import jdk.test.lib.process.OutputAnalyzer;
29-
import jdk.test.lib.process.ProcessTools;
3029
import jdk.internal.platform.Metrics;
3130

3231
import java.util.ArrayList;
@@ -44,7 +43,6 @@
4443
* @run main TestMemoryWithSubgroups
4544
*/
4645
public class TestMemoryWithSubgroups {
47-
4846
private static final String imageName = Common.imageName("subgroup");
4947

5048
static String getEngineInfo(String format) throws Exception {
@@ -70,6 +68,9 @@ public static void main(String[] args) throws Exception {
7068
System.out.println("Unable to run docker tests.");
7169
return;
7270
}
71+
72+
ContainerRuntimeVersionTestUtils.checkContainerVersionSupported();
73+
7374
if (isRootless()) {
7475
throw new SkippedException("Test skipped in rootless mode");
7576
}

test/jdk/jdk/internal/platform/docker/TestDockerMemoryMetricsSubgroup.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import jdk.test.lib.containers.docker.DockerfileConfig;
2828
import jdk.test.lib.containers.docker.DockerRunOptions;
2929
import jdk.test.lib.containers.docker.DockerTestUtils;
30+
import jdk.test.lib.containers.docker.ContainerRuntimeVersionTestUtils;
3031

3132
import java.util.ArrayList;
3233

@@ -59,6 +60,9 @@ public static void main(String[] args) throws Exception {
5960
System.out.println("Unable to run docker tests.");
6061
return;
6162
}
63+
64+
ContainerRuntimeVersionTestUtils.checkContainerVersionSupported();
65+
6266
if ("cgroupv1".equals(metrics.getProvider())) {
6367
testMemoryLimitSubgroupV1("200m", "400m", false);
6468
testMemoryLimitSubgroupV1("500m", "1G", false);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2025, 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+
/*
25+
* Methods and definitions related to container runtime version to test container in this directory
26+
*/
27+
28+
package jdk.test.lib.containers.docker;
29+
30+
import jdk.test.lib.Container;
31+
import jdk.test.lib.process.OutputAnalyzer;
32+
import jtreg.SkippedException;
33+
34+
public class ContainerRuntimeVersionTestUtils implements Comparable<ContainerRuntimeVersionTestUtils> {
35+
private final int major;
36+
private final int minor;
37+
private final int micro;
38+
private static final boolean IS_DOCKER = Container.ENGINE_COMMAND.contains("docker");
39+
private static final boolean IS_PODMAN = Container.ENGINE_COMMAND.contains("podman");
40+
public static final ContainerRuntimeVersionTestUtils DOCKER_MINIMAL_SUPPORTED_VERSION_CGROUPNS = new ContainerRuntimeVersionTestUtils(20, 10, 0);
41+
public static final ContainerRuntimeVersionTestUtils PODMAN_MINIMAL_SUPPORTED_VERSION_CGROUPNS = new ContainerRuntimeVersionTestUtils(1, 5, 0);
42+
43+
private ContainerRuntimeVersionTestUtils(int major, int minor, int micro) {
44+
this.major = major;
45+
this.minor = minor;
46+
this.micro = micro;
47+
}
48+
49+
public static void checkContainerVersionSupported() {
50+
if (IS_DOCKER && ContainerRuntimeVersionTestUtils.DOCKER_MINIMAL_SUPPORTED_VERSION_CGROUPNS.compareTo(ContainerRuntimeVersionTestUtils.getContainerRuntimeVersion()) > 0) {
51+
throw new SkippedException("Docker version too old for this test. Expected >= 20.10.0");
52+
}
53+
if (IS_PODMAN && ContainerRuntimeVersionTestUtils.PODMAN_MINIMAL_SUPPORTED_VERSION_CGROUPNS.compareTo(ContainerRuntimeVersionTestUtils.getContainerRuntimeVersion()) > 0) {
54+
throw new SkippedException("Podman version too old for this test. Expected >= 1.5.0");
55+
}
56+
}
57+
58+
@Override
59+
public int compareTo(ContainerRuntimeVersionTestUtils other) {
60+
if (this.major > other.major) {
61+
return 1;
62+
} else if (this.major < other.major) {
63+
return -1;
64+
} else if (this.minor > other.minor) {
65+
return 1;
66+
} else if (this.minor < other.minor) {
67+
return -1;
68+
} else if (this.micro > other.micro) {
69+
return 1;
70+
} else if (this.micro < other.micro) {
71+
return -1;
72+
} else {
73+
// equal majors, minors, micro
74+
return 0;
75+
}
76+
}
77+
78+
public static ContainerRuntimeVersionTestUtils fromVersionString(String version) {
79+
try {
80+
// Example 'docker version 20.10.0 or podman version 4.9.4-rhel'
81+
String versNums = version.split("\\s+", 3)[2];
82+
String[] numbers = versNums.split("-")[0].split("\\.", 3);
83+
return new ContainerRuntimeVersionTestUtils(Integer.parseInt(numbers[0]),
84+
Integer.parseInt(numbers[1]),
85+
Integer.parseInt(numbers[2]));
86+
} catch (Exception e) {
87+
throw new RuntimeException("Failed to parse container runtime version: " + version);
88+
}
89+
}
90+
91+
public static String getContainerRuntimeVersionStr() {
92+
try {
93+
ProcessBuilder pb = new ProcessBuilder(Container.ENGINE_COMMAND, "--version");
94+
OutputAnalyzer out = new OutputAnalyzer(pb.start())
95+
.shouldHaveExitValue(0);
96+
String result = out.asLines().get(0);
97+
System.out.println(Container.ENGINE_COMMAND + " --version returning: " + result);
98+
return result;
99+
} catch (Exception e) {
100+
throw new RuntimeException(Container.ENGINE_COMMAND + " --version command failed.");
101+
}
102+
}
103+
104+
public static ContainerRuntimeVersionTestUtils getContainerRuntimeVersion() {
105+
return ContainerRuntimeVersionTestUtils.fromVersionString(getContainerRuntimeVersionStr());
106+
}
107+
}

0 commit comments

Comments
 (0)