aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Marion <steve.marion@sonarsource.com>2023-09-20 10:23:36 +0200
committersonartech <sonartech@sonarsource.com>2023-09-20 20:02:50 +0000
commit1237a2eb3ab3a94d78cbbb223126278b7bee1fc1 (patch)
treeddf322833caf54457a225e809fcbc9416ba7f2b6
parente96a172a7e74905d3d75c9e6e52fb5fd32b377aa (diff)
downloadsonarqube-1237a2eb3ab3a94d78cbbb223126278b7bee1fc1.tar.gz
sonarqube-1237a2eb3ab3a94d78cbbb223126278b7bee1fc1.zip
NO-JIRA make test independent of running container
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java17
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java93
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<String> data() {
- return Arrays.asList(DOCKER, PODMAN, BUILDAH, CONTAINER_D, GENERAL_CONTAINER, null);
+ public static Collection<ContainerEnvConfig> 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;
+ }
}
}