aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap
diff options
context:
space:
mode:
authorMatteo Mara <matteo.mara@sonarsource.com>2023-12-06 16:58:03 +0100
committersonartech <sonartech@sonarsource.com>2024-01-04 20:02:48 +0000
commit7013e543f07fca1831fd1efee29997981b6ec19b (patch)
tree658cc82afdefacdfa1e1454cc78fd9c3dbf5d652 /sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap
parent8fd8c030e7dda6eb03c83eb4e59474f5e2d4e401 (diff)
downloadsonarqube-7013e543f07fca1831fd1efee29997981b6ec19b.tar.gz
sonarqube-7013e543f07fca1831fd1efee29997981b6ec19b.zip
SONAR-21195 Enhance scanner engine to download only required plugins
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java37
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java63
2 files changed, 82 insertions, 18 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java
index cf73820f7ce..0d27bcb284b 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java
@@ -23,6 +23,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
+import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.jar.Attributes;
@@ -51,11 +53,36 @@ public class ScannerPluginInstallerTest {
@Test
public void download_installed_plugins() throws IOException {
- WsTestUtil.mockReader(wsClient, "api/plugins/installed", new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json")));
+ WsTestUtil.mockReader(wsClient, "api/plugins/installed",
+ new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json")));
+ enqueueDownload("scmgit", "abc");
+ enqueueDownload("java", "def");
+
+ Map<String, ScannerPlugin> result = underTest.installRequiredPlugins();
+
+ assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit");
+ ScannerPlugin gitPlugin = result.get("scmgit");
+ assertThat(gitPlugin.getKey()).isEqualTo("scmgit");
+ assertThat(gitPlugin.getInfo().getNonNullJarFile()).exists().isFile();
+ assertThat(gitPlugin.getUpdatedAt()).isEqualTo(100L);
+
+ Map<String, ScannerPlugin> result2 = underTest.installPluginsForLanguages(new HashSet<>(List.of("java")));
+
+ assertThat(result2.keySet()).containsExactlyInAnyOrder("java");
+ ScannerPlugin javaPlugin = result2.get("java");
+ assertThat(javaPlugin.getKey()).isEqualTo("java");
+ assertThat(javaPlugin.getInfo().getNonNullJarFile()).exists().isFile();
+ assertThat(javaPlugin.getUpdatedAt()).isEqualTo(200L);
+ }
+
+ @Test
+ public void download_all_plugins() throws IOException {
+ WsTestUtil.mockReader(wsClient, "api/plugins/installed",
+ new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json")));
enqueueDownload("scmgit", "abc");
enqueueDownload("java", "def");
- Map<String, ScannerPlugin> result = underTest.installRemotes();
+ Map<String, ScannerPlugin> result = underTest.installAllPlugins();
assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit", "java");
ScannerPlugin gitPlugin = result.get("scmgit");
@@ -73,7 +100,7 @@ public class ScannerPluginInstallerTest {
public void fail_if_json_of_installed_plugins_is_not_valid() {
WsTestUtil.mockReader(wsClient, "api/plugins/installed", new StringReader("not json"));
- assertThatThrownBy(() -> underTest.installRemotes())
+ assertThatThrownBy(() -> underTest.installRequiredPlugins())
.isInstanceOf(IllegalStateException.class)
.hasMessage("Fail to parse response of api/plugins/installed");
}
@@ -87,7 +114,7 @@ public class ScannerPluginInstallerTest {
enqueueDownload("java", "def");
enqueueDownload("cobol", "ghi");
- Map<String, ScannerPlugin> result = underTest.installRemotes();
+ Map<String, ScannerPlugin> result = underTest.installRequiredPlugins();
assertThat(result.keySet()).containsExactlyInAnyOrder("java", "cobol");
}
@@ -101,7 +128,7 @@ public class ScannerPluginInstallerTest {
enqueueDownload("cobol", "ghi");
enqueueNotFoundDownload("java", "def");
- assertThatThrownBy(() -> underTest.installRemotes())
+ assertThatThrownBy(() -> underTest.installRequiredPlugins())
.isInstanceOf(IllegalStateException.class)
.hasMessage("Fail to download plugin [java]. Not found.");
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java
index 0b7a65e7344..2fa2343f2e1 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java
@@ -21,9 +21,16 @@ package org.sonar.scanner.bootstrap;
import com.google.common.collect.ImmutableMap;
import java.io.File;
+import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
import org.junit.Test;
import org.sonar.api.Plugin;
+import org.sonar.api.config.Configuration;
import org.sonar.core.platform.ExplodedPlugin;
import org.sonar.core.platform.PluginClassLoader;
import org.sonar.core.platform.PluginInfo;
@@ -32,9 +39,12 @@ import org.sonar.core.plugin.PluginType;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -43,31 +53,58 @@ public class ScannerPluginRepositoryTest {
PluginInstaller installer = mock(PluginInstaller.class);
PluginClassLoader loader = mock(PluginClassLoader.class);
PluginJarExploder exploder = new FakePluginJarExploder();
- ScannerPluginRepository underTest = new ScannerPluginRepository(installer, exploder, loader);
+ Configuration properties = mock(Configuration.class);
+ ScannerPluginRepository underTest = new ScannerPluginRepository(installer, exploder, loader, properties);
@Test
public void install_and_load_plugins() {
- PluginInfo info = new PluginInfo("java");
- ImmutableMap<String, ScannerPlugin> plugins = ImmutableMap.of("java", new ScannerPlugin("java", 1L, PluginType.EXTERNAL, info));
- Plugin instance = mock(Plugin.class);
- when(loader.load(anyMap())).thenReturn(ImmutableMap.of("java", instance));
- when(installer.installRemotes()).thenReturn(plugins);
+ PluginInfo squidInfo = new PluginInfo("squid");
+ PluginInfo javaInfo = new PluginInfo("java");
+ Map<String, ScannerPlugin> globalPlugins = Map.of("squid", new ScannerPlugin("squid", 1L, PluginType.BUNDLED, squidInfo));
+ Map<String, ScannerPlugin> languagePlugins = Map.of("java", new ScannerPlugin("java", 1L, PluginType.EXTERNAL, javaInfo));
+ Plugin squidInstance = mock(Plugin.class);
+ Plugin javaInstance = mock(Plugin.class);
+ when(loader.load(anyMap()))
+ .thenReturn(ImmutableMap.of("squid", squidInstance))
+ .thenReturn(ImmutableMap.of("java", javaInstance));
+
+ when(installer.installRequiredPlugins()).thenReturn(globalPlugins);
+ when(installer.installPluginsForLanguages(anySet())).thenReturn(languagePlugins);
underTest.start();
- assertThat(underTest.getPluginInfos()).containsOnly(info);
- assertThat(underTest.getPluginsByKey()).isEqualTo(plugins);
- assertThat(underTest.getPluginInfo("java")).isSameAs(info);
- assertThat(underTest.getPluginInstance("java")).isSameAs(instance);
- assertThat(underTest.getPluginInstances()).containsOnly(instance);
- assertThat(underTest.getBundledPluginsInfos()).isEmpty();
- assertThat(underTest.getExternalPluginsInfos()).isEqualTo(underTest.getPluginInfos());
+ assertThat(underTest.getPluginInfos()).containsOnly(squidInfo);
+ assertThat(underTest.getPluginsByKey()).isEqualTo(globalPlugins);
+ assertThat(underTest.getPluginInfo("squid")).isSameAs(squidInfo);
+ assertThat(underTest.getPluginInstance("squid")).isSameAs(squidInstance);
+
+ Collection<PluginInfo> result = underTest.installPluginsForLanguages(new HashSet<>(List.of("java")));
+
+ assertThat(result).containsOnly(javaInfo);
+ assertThat(underTest.getPluginInfos()).containsExactlyInAnyOrder(squidInfo, javaInfo);
+ assertThat(underTest.getExternalPluginsInfos()).containsExactlyInAnyOrder(javaInfo);
+ assertThat(underTest.getPluginsByKey().values()).containsExactlyInAnyOrder(globalPlugins.get("squid"), languagePlugins.get("java"));
underTest.stop();
verify(loader).unload(anyCollection());
}
@Test
+ public void should_install_all_plugins_when_loadall_flag_is_set() {
+ when(properties.getBoolean("sonar.plugins.loadAll")).thenReturn(Optional.of(true));
+
+ underTest.start();
+
+ verify(installer).installAllPlugins();
+ verify(installer, never()).installRequiredPlugins();
+
+ Collection<PluginInfo> result = underTest.installPluginsForLanguages(Set.of("java"));
+
+ assertThat(result).isEmpty();
+ verify(installer, never()).installPluginsForLanguages(any());
+ }
+
+ @Test
public void fail_if_requesting_missing_plugin() {
underTest.start();