From a8f45c79f80a04fbd6c71581264b87459ead6363 Mon Sep 17 00:00:00 2001 From: Denis Zavedeev Date: Tue, 31 Aug 2021 22:55:32 +0300 Subject: [PATCH] Use `docker info` to distinguish cgroup version --- .../src/test/java/generic/CmdModifierTest.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/examples/junit4/generic/src/test/java/generic/CmdModifierTest.java b/docs/examples/junit4/generic/src/test/java/generic/CmdModifierTest.java index 97de801110a..887f81dc1c2 100644 --- a/docs/examples/junit4/generic/src/test/java/generic/CmdModifierTest.java +++ b/docs/examples/junit4/generic/src/test/java/generic/CmdModifierTest.java @@ -1,5 +1,6 @@ package generic; +import com.github.dockerjava.api.model.Info; import org.junit.Rule; import org.junit.Test; import org.testcontainers.containers.Container; @@ -7,6 +8,7 @@ import org.testcontainers.utility.DockerImageName; import java.io.IOException; +import java.util.Objects; import static org.junit.Assert.assertEquals; @@ -39,11 +41,17 @@ public void testHostnameModified() throws IOException, InterruptedException { @Test public void testMemoryLimitModified() throws IOException, InterruptedException { - final Container.ExecResult execResult = memoryLimitedRedis.execInContainer( - "cat", - "/sys/fs/cgroup/memory/memory.limit_in_bytes", - "/sys/fs/cgroup/memory.max" - ); + final Container.ExecResult execResult = memoryLimitedRedis.execInContainer("cat", getMemoryLimitFilePath()); assertEquals(String.valueOf(memoryInBytes), execResult.getStdout().trim()); } + + private String getMemoryLimitFilePath() { + Info info = memoryLimitedRedis.getDockerClient().infoCmd().exec(); + Object cgroupVersion = info.getRawValues().get("CgroupVersion"); + boolean cgroup2 = Objects.equals("2", cgroupVersion); + if (cgroup2) { + return "/sys/fs/cgroup/memory.max"; + } + return "/sys/fs/cgroup/memory/memory.limit_in_bytes"; + } }