]> source.dussan.org Git - sonarqube.git/commitdiff
NO-JIRA make test independent of running container
authorSteve Marion <steve.marion@sonarsource.com>
Wed, 20 Sep 2023 08:23:36 +0000 (10:23 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 20 Sep 2023 20:02:50 +0000 (20:02 +0000)
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/ContainerSupportImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ContainerSupportImplTest.java

index e6d81ac1a9bb0e9287d4369039d5b3874f9ee381..fe54544fd97a4572e2ee8df23af85a335203a424 100644 (file)
@@ -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)) {
index 56b3406846bde14996cad17c38cea59584c77023..246dfd634ccee31e4c63e0d486ceddc11c005811 100644 (file)
@@ -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;
+    }
   }
 
 }