You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScannerPluginInstallerTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.bootstrap;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.io.StringReader;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Optional;
  29. import java.util.jar.Attributes;
  30. import java.util.jar.JarOutputStream;
  31. import java.util.jar.Manifest;
  32. import org.apache.commons.io.FileUtils;
  33. import org.junit.Rule;
  34. import org.junit.Test;
  35. import org.junit.rules.TemporaryFolder;
  36. import org.sonar.scanner.WsTestUtil;
  37. import org.sonar.scanner.http.DefaultScannerWsClient;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  40. import static org.mockito.ArgumentMatchers.argThat;
  41. import static org.mockito.Mockito.doReturn;
  42. import static org.mockito.Mockito.mock;
  43. public class ScannerPluginInstallerTest {
  44. @Rule
  45. public TemporaryFolder temp = new TemporaryFolder();
  46. private PluginFiles pluginFiles = mock(PluginFiles.class);
  47. private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
  48. private ScannerPluginInstaller underTest = new ScannerPluginInstaller(pluginFiles, wsClient);
  49. @Test
  50. public void download_installed_plugins() throws IOException {
  51. WsTestUtil.mockReader(wsClient, "api/plugins/installed",
  52. new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json")));
  53. enqueueDownload("scmgit", "abc");
  54. enqueueDownload("java", "def");
  55. Map<String, ScannerPlugin> result = underTest.installRequiredPlugins();
  56. assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit");
  57. ScannerPlugin gitPlugin = result.get("scmgit");
  58. assertThat(gitPlugin.getKey()).isEqualTo("scmgit");
  59. assertThat(gitPlugin.getInfo().getNonNullJarFile()).exists().isFile();
  60. assertThat(gitPlugin.getUpdatedAt()).isEqualTo(100L);
  61. Map<String, ScannerPlugin> result2 = underTest.installPluginsForLanguages(new HashSet<>(List.of("java")));
  62. assertThat(result2.keySet()).containsExactlyInAnyOrder("java");
  63. ScannerPlugin javaPlugin = result2.get("java");
  64. assertThat(javaPlugin.getKey()).isEqualTo("java");
  65. assertThat(javaPlugin.getInfo().getNonNullJarFile()).exists().isFile();
  66. assertThat(javaPlugin.getUpdatedAt()).isEqualTo(200L);
  67. }
  68. @Test
  69. public void download_all_plugins() throws IOException {
  70. WsTestUtil.mockReader(wsClient, "api/plugins/installed",
  71. new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json")));
  72. enqueueDownload("scmgit", "abc");
  73. enqueueDownload("java", "def");
  74. Map<String, ScannerPlugin> result = underTest.installAllPlugins();
  75. assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit", "java");
  76. ScannerPlugin gitPlugin = result.get("scmgit");
  77. assertThat(gitPlugin.getKey()).isEqualTo("scmgit");
  78. assertThat(gitPlugin.getInfo().getNonNullJarFile()).exists().isFile();
  79. assertThat(gitPlugin.getUpdatedAt()).isEqualTo(100L);
  80. ScannerPlugin javaPlugin = result.get("java");
  81. assertThat(javaPlugin.getKey()).isEqualTo("java");
  82. assertThat(javaPlugin.getInfo().getNonNullJarFile()).exists().isFile();
  83. assertThat(javaPlugin.getUpdatedAt()).isEqualTo(200L);
  84. }
  85. @Test
  86. public void fail_if_json_of_installed_plugins_is_not_valid() {
  87. WsTestUtil.mockReader(wsClient, "api/plugins/installed", new StringReader("not json"));
  88. assertThatThrownBy(() -> underTest.installRequiredPlugins())
  89. .isInstanceOf(IllegalStateException.class)
  90. .hasMessage("Fail to parse response of api/plugins/installed");
  91. }
  92. @Test
  93. public void reload_list_if_plugin_uninstalled_during_blue_green_switch() throws IOException {
  94. WsTestUtil.mockReader(wsClient, "api/plugins/installed",
  95. new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/blue-installed.json")),
  96. new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/green-installed.json")));
  97. enqueueNotFoundDownload("scmgit", "abc");
  98. enqueueDownload("java", "def");
  99. enqueueDownload("cobol", "ghi");
  100. Map<String, ScannerPlugin> result = underTest.installRequiredPlugins();
  101. assertThat(result.keySet()).containsExactlyInAnyOrder("java", "cobol");
  102. }
  103. @Test
  104. public void fail_if_plugin_not_found_two_times() throws IOException {
  105. WsTestUtil.mockReader(wsClient, "api/plugins/installed",
  106. new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/blue-installed.json")),
  107. new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/green-installed.json")));
  108. enqueueDownload("scmgit", "abc");
  109. enqueueDownload("cobol", "ghi");
  110. enqueueNotFoundDownload("java", "def");
  111. assertThatThrownBy(() -> underTest.installRequiredPlugins())
  112. .isInstanceOf(IllegalStateException.class)
  113. .hasMessage("Fail to download plugin [java]. Not found.");
  114. }
  115. @Test
  116. public void installLocals_always_returns_empty() {
  117. // this method is used only by medium tests
  118. assertThat(underTest.installLocals()).isEmpty();
  119. }
  120. private void enqueueDownload(String pluginKey, String pluginHash) throws IOException {
  121. File jar = temp.newFile();
  122. Manifest manifest = new Manifest();
  123. manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
  124. manifest.getMainAttributes().putValue("Plugin-Key", pluginKey);
  125. try (JarOutputStream output = new JarOutputStream(FileUtils.openOutputStream(jar), manifest)) {
  126. }
  127. doReturn(Optional.of(jar)).when(pluginFiles).get(argThat(p -> pluginKey.equals(p.key) && pluginHash.equals(p.hash)));
  128. }
  129. private void enqueueNotFoundDownload(String pluginKey, String pluginHash) {
  130. doReturn(Optional.empty()).when(pluginFiles).get(argThat(p -> pluginKey.equals(p.key) && pluginHash.equals(p.hash)));
  131. }
  132. }