您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ServerPluginRepositoryTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.server.plugins;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.google.common.collect.ImmutableSet;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.Collections;
  26. import java.util.Map;
  27. import org.apache.commons.io.FileUtils;
  28. import org.junit.After;
  29. import org.junit.Before;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.ExpectedException;
  33. import org.junit.rules.TemporaryFolder;
  34. import org.mockito.Mockito;
  35. import org.sonar.api.SonarRuntime;
  36. import org.sonar.api.utils.MessageException;
  37. import org.sonar.api.utils.log.LogTester;
  38. import org.sonar.core.platform.PluginInfo;
  39. import org.sonar.core.platform.PluginLoader;
  40. import org.sonar.server.platform.ServerFileSystem;
  41. import org.sonar.updatecenter.common.Version;
  42. import static org.assertj.core.api.Assertions.assertThat;
  43. import static org.junit.Assert.fail;
  44. import static org.mockito.Mockito.mock;
  45. import static org.mockito.Mockito.when;
  46. public class ServerPluginRepositoryTest {
  47. @Rule
  48. public ExpectedException expectedException = ExpectedException.none();
  49. @Rule
  50. public TemporaryFolder temp = new TemporaryFolder();
  51. @Rule
  52. public LogTester logs = new LogTester();
  53. private SonarRuntime runtime = mock(SonarRuntime.class);
  54. private ServerFileSystem fs = mock(ServerFileSystem.class, Mockito.RETURNS_DEEP_STUBS);
  55. private PluginLoader pluginLoader = mock(PluginLoader.class);
  56. private ServerPluginRepository underTest = new ServerPluginRepository(runtime, fs, pluginLoader);
  57. @Before
  58. public void setUp() throws IOException {
  59. when(fs.getDeployedPluginsDir()).thenReturn(temp.newFolder());
  60. when(fs.getDownloadedPluginsDir()).thenReturn(temp.newFolder());
  61. when(fs.getHomeDir()).thenReturn(temp.newFolder());
  62. when(fs.getInstalledPluginsDir()).thenReturn(temp.newFolder());
  63. when(fs.getTempDir()).thenReturn(temp.newFolder());
  64. when(runtime.getApiVersion()).thenReturn(org.sonar.api.utils.Version.parse("5.2"));
  65. }
  66. @After
  67. public void tearDown() {
  68. underTest.stop();
  69. }
  70. @Test
  71. public void standard_startup_loads_installed_plugins() throws Exception {
  72. copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  73. underTest.start();
  74. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase");
  75. }
  76. @Test
  77. public void no_plugins_at_all_on_startup() {
  78. underTest.start();
  79. assertThat(underTest.getPluginInfos()).isEmpty();
  80. assertThat(underTest.getPluginInfosByKeys()).isEmpty();
  81. assertThat(underTest.hasPlugin("testbase")).isFalse();
  82. }
  83. @Test
  84. public void fail_if_multiple_jars_for_same_installed_plugin_on_startup() throws Exception {
  85. copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  86. copyTestPluginTo("test-base-plugin-v2", fs.getInstalledPluginsDir());
  87. try {
  88. underTest.start();
  89. fail();
  90. } catch (MessageException e) {
  91. assertThat(e)
  92. .hasMessageStartingWith("Found two versions of the plugin Base Plugin [testbase] in the directory extensions/plugins. Please remove one of ")
  93. // order is not guaranteed, so assertion is split
  94. .hasMessageContaining("test-base-plugin-0.1-SNAPSHOT.jar")
  95. .hasMessageContaining("test-base-plugin-0.2-SNAPSHOT.jar");
  96. }
  97. }
  98. @Test
  99. public void install_downloaded_plugins_on_startup() throws Exception {
  100. File downloadedJar = copyTestPluginTo("test-base-plugin", fs.getDownloadedPluginsDir());
  101. underTest.start();
  102. // plugin is moved to extensions/plugins then loaded
  103. assertThat(downloadedJar).doesNotExist();
  104. assertThat(new File(fs.getInstalledPluginsDir(), downloadedJar.getName())).isFile().exists();
  105. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase");
  106. }
  107. @Test
  108. public void downloaded_file_overrides_existing_installed_file_on_startup() throws Exception {
  109. File installedV1 = copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  110. File downloadedV2 = copyTestPluginTo("test-base-plugin-v2", fs.getDownloadedPluginsDir());
  111. underTest.start();
  112. // plugin is moved to extensions/plugins and replaces v1
  113. assertThat(downloadedV2).doesNotExist();
  114. assertThat(installedV1).doesNotExist();
  115. assertThat(new File(fs.getInstalledPluginsDir(), downloadedV2.getName())).exists();
  116. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase");
  117. assertThat(underTest.getPluginInfo("testbase").getVersion()).isEqualTo(Version.create("0.2-SNAPSHOT"));
  118. }
  119. @Test
  120. public void blacklisted_plugin_is_automatically_uninstalled_on_startup() throws Exception {
  121. underTest.setBlacklistedPluginKeys(ImmutableSet.of("testbase", "issuesreport"));
  122. File jar = copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  123. underTest.start();
  124. // plugin is not installed and file is deleted
  125. assertThat(underTest.getPluginInfos()).isEmpty();
  126. assertThat(jar).doesNotExist();
  127. }
  128. @Test
  129. public void test_plugin_requirements_at_startup() throws Exception {
  130. copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  131. copyTestPluginTo("test-require-plugin", fs.getInstalledPluginsDir());
  132. underTest.start();
  133. // both plugins are installed
  134. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase", "testrequire");
  135. }
  136. @Test
  137. public void plugin_is_ignored_if_required_plugin_is_missing_at_startup() throws Exception {
  138. copyTestPluginTo("test-require-plugin", fs.getInstalledPluginsDir());
  139. underTest.start();
  140. // plugin is not installed as test-base-plugin is missing
  141. assertThat(underTest.getPluginInfosByKeys()).isEmpty();
  142. }
  143. @Test
  144. public void plugin_is_ignored_if_required_plugin_is_too_old_at_startup() throws Exception {
  145. copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  146. copyTestPluginTo("test-requirenew-plugin", fs.getInstalledPluginsDir());
  147. underTest.start();
  148. // the plugin "requirenew" is not installed as it requires base 0.2+ to be installed.
  149. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase");
  150. }
  151. @Test
  152. public void fail_if_plugin_does_not_support_sq_version() throws Exception {
  153. when(runtime.getApiVersion()).thenReturn(org.sonar.api.utils.Version.parse("1.0"));
  154. copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  155. try {
  156. underTest.start();
  157. fail();
  158. } catch (MessageException e) {
  159. assertThat(e).hasMessage("Plugin Base Plugin [testbase] requires at least SonarQube 4.5.4");
  160. }
  161. }
  162. @Test
  163. public void uninstall() throws Exception {
  164. File installedJar = copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  165. File uninstallDir = temp.newFolder("uninstallDir");
  166. underTest.start();
  167. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase");
  168. underTest.uninstall("testbase", uninstallDir);
  169. assertThat(installedJar).doesNotExist();
  170. // still up. Will be dropped after next startup
  171. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase");
  172. assertThat(uninstallDir.list()).containsOnly(installedJar.getName());
  173. }
  174. @Test
  175. public void uninstall_dependents() throws Exception {
  176. File base = copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  177. File extension = copyTestPluginTo("test-require-plugin", fs.getInstalledPluginsDir());
  178. File uninstallDir = temp.newFolder("uninstallDir");
  179. underTest.start();
  180. assertThat(underTest.getPluginInfos()).hasSize(2);
  181. underTest.uninstall("testbase", uninstallDir);
  182. assertThat(base).doesNotExist();
  183. assertThat(extension).doesNotExist();
  184. assertThat(uninstallDir.list()).containsOnly(base.getName(), extension.getName());
  185. }
  186. @Test
  187. public void dont_uninstall_non_existing_dependents() throws IOException {
  188. File base = copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  189. File extension = copyTestPluginTo("test-require-plugin", fs.getInstalledPluginsDir());
  190. File uninstallDir = temp.newFolder("uninstallDir");
  191. underTest.start();
  192. assertThat(underTest.getPluginInfos()).hasSize(2);
  193. underTest.uninstall("testrequire", uninstallDir);
  194. assertThat(underTest.getPluginInfos()).hasSize(2);
  195. underTest.uninstall("testbase", uninstallDir);
  196. assertThat(base).doesNotExist();
  197. assertThat(extension).doesNotExist();
  198. assertThat(uninstallDir.list()).containsOnly(base.getName(), extension.getName());
  199. }
  200. @Test
  201. public void dont_uninstall_non_existing_files() throws IOException {
  202. File base = copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  203. File extension = copyTestPluginTo("test-require-plugin", fs.getInstalledPluginsDir());
  204. File uninstallDir = temp.newFolder("uninstallDir");
  205. underTest.start();
  206. assertThat(underTest.getPluginInfos()).hasSize(2);
  207. underTest.uninstall("testbase", uninstallDir);
  208. assertThat(underTest.getPluginInfos()).hasSize(2);
  209. underTest.uninstall("testbase", uninstallDir);
  210. assertThat(base).doesNotExist();
  211. assertThat(extension).doesNotExist();
  212. assertThat(uninstallDir.list()).containsOnly(base.getName(), extension.getName());
  213. }
  214. @Test
  215. public void install_plugin_and_its_extension_plugins_at_startup() throws Exception {
  216. copyTestPluginTo("test-base-plugin", fs.getInstalledPluginsDir());
  217. copyTestPluginTo("test-extend-plugin", fs.getInstalledPluginsDir());
  218. underTest.start();
  219. // both plugins are installed
  220. assertThat(underTest.getPluginInfosByKeys()).containsOnlyKeys("testbase", "testextend");
  221. }
  222. @Test
  223. public void extension_plugin_is_ignored_if_base_plugin_is_missing_at_startup() throws Exception {
  224. copyTestPluginTo("test-extend-plugin", fs.getInstalledPluginsDir());
  225. underTest.start();
  226. // plugin is not installed as its base plugin is not installed
  227. assertThat(underTest.getPluginInfos()).isEmpty();
  228. }
  229. @Test
  230. public void fail_to_get_missing_plugins() {
  231. underTest.start();
  232. try {
  233. underTest.getPluginInfo("unknown");
  234. fail();
  235. } catch (IllegalArgumentException e) {
  236. assertThat(e).hasMessage("Plugin [unknown] does not exist");
  237. }
  238. try {
  239. underTest.getPluginInstance("unknown");
  240. fail();
  241. } catch (IllegalArgumentException e) {
  242. assertThat(e).hasMessage("Plugin [unknown] does not exist");
  243. }
  244. }
  245. @Test
  246. public void plugin_is_incompatible_if_no_entry_point_class() {
  247. PluginInfo plugin = new PluginInfo("foo").setName("Foo");
  248. assertThat(ServerPluginRepository.isCompatible(plugin, runtime, Collections.emptyMap())).isFalse();
  249. assertThat(logs.logs()).contains("Plugin Foo [foo] is ignored because entry point class is not defined");
  250. }
  251. @Test
  252. public void fail_when_views_is_installed() throws Exception {
  253. copyTestPluginTo("fake-views-plugin", fs.getInstalledPluginsDir());
  254. expectedException.expect(MessageException.class);
  255. expectedException.expectMessage("Plugin 'views' is no longer compatible with this version of SonarQube");
  256. underTest.start();
  257. }
  258. @Test
  259. public void fail_when_sqale_plugin_is_installed() throws Exception {
  260. copyTestPluginTo("fake-sqale-plugin", fs.getInstalledPluginsDir());
  261. expectedException.expect(MessageException.class);
  262. expectedException.expectMessage("Plugin 'sqale' is no longer compatible with this version of SonarQube");
  263. underTest.start();
  264. }
  265. @Test
  266. public void fail_when_report_is_installed() throws Exception {
  267. copyTestPluginTo("fake-report-plugin", fs.getInstalledPluginsDir());
  268. expectedException.expect(MessageException.class);
  269. expectedException.expectMessage("Plugin 'report' is no longer compatible with this version of SonarQube");
  270. underTest.start();
  271. }
  272. /**
  273. * Some plugins can only extend the classloader of base plugin, without declaring new extensions.
  274. */
  275. @Test
  276. public void plugin_is_compatible_if_no_entry_point_class_but_extend_other_plugin() {
  277. PluginInfo basePlugin = new PluginInfo("base").setMainClass("org.bar.Bar");
  278. PluginInfo plugin = new PluginInfo("foo").setBasePlugin("base");
  279. Map<String, PluginInfo> plugins = ImmutableMap.of("base", basePlugin, "foo", plugin);
  280. assertThat(ServerPluginRepository.isCompatible(plugin, runtime, plugins)).isTrue();
  281. }
  282. @Test
  283. public void getPluginInstance_throws_ISE_if_repo_is_not_started() {
  284. expectedException.expect(IllegalStateException.class);
  285. expectedException.expectMessage("not started yet");
  286. underTest.getPluginInstance("foo");
  287. }
  288. @Test
  289. public void getPluginInfo_throws_ISE_if_repo_is_not_started() {
  290. expectedException.expect(IllegalStateException.class);
  291. expectedException.expectMessage("not started yet");
  292. underTest.getPluginInfo("foo");
  293. }
  294. @Test
  295. public void hasPlugin_throws_ISE_if_repo_is_not_started() {
  296. expectedException.expect(IllegalStateException.class);
  297. expectedException.expectMessage("not started yet");
  298. underTest.hasPlugin("foo");
  299. }
  300. @Test
  301. public void getPluginInfos_throws_ISE_if_repo_is_not_started() {
  302. expectedException.expect(IllegalStateException.class);
  303. expectedException.expectMessage("not started yet");
  304. underTest.getPluginInfos();
  305. }
  306. private File copyTestPluginTo(String testPluginName, File toDir) throws IOException {
  307. File jar = TestProjectUtils.jarOf(testPluginName);
  308. // file is copied because it's supposed to be moved by the test
  309. FileUtils.copyFileToDirectory(jar, toDir);
  310. return new File(toDir, jar.getName());
  311. }
  312. }