From 49e72335c1d98da8235766ba77cf257d8f5f318a Mon Sep 17 00:00:00 2001 From: Steve Marion Date: Wed, 20 Sep 2023 10:23:36 +0200 Subject: [PATCH] NO-JIRA make test independent of running container --- .../server/platform/ContainerSupportImpl.java | 17 +++- .../platform/ContainerSupportImplTest.java | 93 ++++++++++--------- 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java index e6d81ac1a9b..fe54544fd97 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java @@ -81,7 +81,7 @@ public class ContainerSupportImpl implements ContainerSupport { } private boolean isDocker() { - return executeCommand(MOUNT_GREP_COMMAND).contains("/docker") && paths2.exists("/.dockerenv"); + return getMountOverlays().contains("/docker") && paths2.exists("/.dockerenv"); } private boolean isPodman() { @@ -89,11 +89,11 @@ public class ContainerSupportImpl implements ContainerSupport { } private boolean isBuildah() { - return paths2.exists(CONTAINER_FILE_PATH) && executeCommand(CAT_COMMAND).contains("engine=\"buildah-"); + return paths2.exists(CONTAINER_FILE_PATH) && readContainerenvFile().contains("engine=\"buildah-"); } private boolean isContainerd() { - return executeCommand(MOUNT_GREP_COMMAND).contains("/containerd"); + return getMountOverlays().contains("/containerd"); } private boolean isGeneralContainer() { @@ -101,7 +101,16 @@ public class ContainerSupportImpl implements ContainerSupport { } @VisibleForTesting - String executeCommand(String[] command) { + String getMountOverlays() { + return executeCommand(MOUNT_GREP_COMMAND); + } + + @VisibleForTesting + String readContainerenvFile() { + return executeCommand(CAT_COMMAND); + } + + private static String executeCommand(String[] command) { try { Process process = new ProcessBuilder().command(command).start(); try (Scanner scanner = new Scanner(process.getInputStream(), UTF_8)) { diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java index 56b3406846b..246dfd634cc 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java @@ -19,9 +19,9 @@ */ package org.sonar.server.platform; -import java.util.Arrays; import java.util.Collection; -import org.junit.Assert; +import java.util.List; +import javax.annotation.Nullable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,78 +29,79 @@ import org.junit.runners.Parameterized; import org.sonar.api.utils.System2; import org.sonar.server.util.Paths2; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @RunWith(Parameterized.class) public class ContainerSupportImplTest { - private static final String CONTAINER_FILE_PATH = "/run/.containerenv"; - private static final String[] MOUNT_GREP_COMMAND = {"bash", "-c", "mount | grep 'overlay on /'"}; - private static final String[] CAT_COMMAND = {"bash", "-c", "cat /run/.containerenv"}; - private static final String DOCKER = "docker"; - private static final String PODMAN = "podman"; - private static final String BUILDAH = "buildah"; - private static final String CONTAINER_D = "containerd"; - private static final String GENERAL_CONTAINER = "general_container"; - private final Paths2 paths2 = mock(Paths2.class); private final System2 system2 = mock(System2.class); private ContainerSupportImpl underTest = new ContainerSupportImpl(paths2, system2); - private String containerContext; + private final ContainerEnvConfig containerContext; - public ContainerSupportImplTest(String containerContext) { + public ContainerSupportImplTest(ContainerEnvConfig containerContext) { this.containerContext = containerContext; } @Before public void setUp() { - if (containerContext == null) { - return; - } - - switch (containerContext) { - case DOCKER -> { - underTest = spy(underTest); - when(underTest.executeCommand(MOUNT_GREP_COMMAND)).thenReturn("/docker"); - when(paths2.exists("/.dockerenv")).thenReturn(true); - } - case PODMAN -> { - when(system2.envVariable("container")).thenReturn("podman"); - when(paths2.exists(CONTAINER_FILE_PATH)).thenReturn(true); - } - case BUILDAH -> { - underTest = spy(underTest); - when(paths2.exists(CONTAINER_FILE_PATH)).thenReturn(true); - when(underTest.executeCommand(CAT_COMMAND)).thenReturn("XXX engine=\"buildah- XXX"); - } - case CONTAINER_D -> { - underTest = spy(underTest); - when(underTest.executeCommand(MOUNT_GREP_COMMAND)).thenReturn("/containerd"); - } - case GENERAL_CONTAINER -> when(paths2.exists(CONTAINER_FILE_PATH)).thenReturn(true); - default -> { - } - } + underTest = spy(underTest); + when(underTest.readContainerenvFile()).thenReturn( + containerContext.hasBuildahContainerenv ? + "engine=\"buildah-" : ""); + when(paths2.exists("/run/.containerenv")).thenReturn(containerContext.hasContainerenvFile); + when(paths2.exists("/.dockerenv")).thenReturn(containerContext.hasDockerEnvFile); + when(system2.envVariable("container")).thenReturn( + containerContext.hasPodmanEnvVariable ? + "podman" : ""); + when(underTest.getMountOverlays()).thenReturn(containerContext.mountOverlays); underTest.populateCache(); } @Parameterized.Parameters - public static Collection data() { - return Arrays.asList(DOCKER, PODMAN, BUILDAH, CONTAINER_D, GENERAL_CONTAINER, null); + public static Collection data() { + return List.of(ContainerEnvConfig.values()); } @Test public void testGetContainerContext() { - Assert.assertEquals(containerContext, underTest.getContainerContext()); + assertThat(underTest.getContainerContext()) + .isEqualTo(containerContext.expectedContainerContext); } @Test public void testIsRunningInContainer() { - boolean expected = containerContext != null; - when(paths2.exists(CONTAINER_FILE_PATH)).thenReturn(expected); - Assert.assertEquals(expected, underTest.isRunningInContainer()); + assertThat(underTest.isRunningInContainer()) + .isEqualTo(containerContext.expectedContainerContext != null); + } + + public enum ContainerEnvConfig { + DOCKER("docker", false, true, false, false, "/docker"), + PODMAN("podman", true, false, true, false, ""), + BUILDAH("buildah", true, false, false, true, ""), + CONTAINER_D("containerd", false, false, false, false, "/containerd"), + GENERAL_CONTAINER("general_container", true, false, false, false, ""), + NONE(null, false, false, false, false, ""); + final String expectedContainerContext; + final boolean hasContainerenvFile; + final boolean hasDockerEnvFile; + final boolean hasPodmanEnvVariable; + final boolean hasBuildahContainerenv; + final String mountOverlays; + + + ContainerEnvConfig(@Nullable String expectedContainerContext, boolean hasContainerenvFile, boolean hasDockerEnvFile, boolean hasPodmanEnvVariable, + boolean hasBuildahContainerenv, String mountOverlays) { + this.expectedContainerContext = expectedContainerContext; + this.hasContainerenvFile = hasContainerenvFile; + this.hasDockerEnvFile = hasDockerEnvFile; + this.hasPodmanEnvVariable = hasPodmanEnvVariable; + this.hasBuildahContainerenv = hasBuildahContainerenv; + this.mountOverlays = mountOverlays; + } } } -- 2.39.5