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