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.

ServerPluginJarsInstallerTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.io.Resources;
  22. import org.apache.commons.io.FileUtils;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.junit.rules.TemporaryFolder;
  28. import org.sonar.api.platform.PluginMetadata;
  29. import org.sonar.api.platform.Server;
  30. import org.sonar.api.platform.ServerUpgradeStatus;
  31. import org.sonar.api.utils.MessageException;
  32. import org.sonar.core.plugins.DefaultPluginMetadata;
  33. import org.sonar.server.platform.DefaultServerFileSystem;
  34. import java.io.File;
  35. import java.util.Collection;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.junit.Assert.fail;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.when;
  40. public class ServerPluginJarsInstallerTest {
  41. @Rule
  42. public ExpectedException exception = ExpectedException.none();
  43. @Rule
  44. public TemporaryFolder temp = new TemporaryFolder();
  45. DefaultServerFileSystem fileSystem;
  46. File homeDir, pluginsDir, downloadsDir, bundledDir, trashDir, coreDir;
  47. ServerPluginJarInstaller jarInstaller;
  48. ServerPluginJarsInstaller jarsInstaller;
  49. Server server = mock(Server.class);
  50. ServerUpgradeStatus upgradeStatus = mock(ServerUpgradeStatus.class);
  51. @Before
  52. public void before() throws Exception {
  53. when(server.getVersion()).thenReturn("3.1");
  54. when(server.getDeployDir()).thenReturn(temp.newFolder("deploy"));
  55. homeDir = temp.newFolder("home");
  56. pluginsDir = new File(homeDir, "extensions/plugins");
  57. FileUtils.forceMkdir(pluginsDir);
  58. downloadsDir = new File(homeDir, "extensions/downloads");
  59. trashDir = new File(homeDir, "extensions/trash");
  60. bundledDir = new File(homeDir, "lib/bundled-plugins");
  61. coreDir = new File(homeDir, "lib/core-plugins");
  62. FileUtils.forceMkdir(bundledDir);
  63. fileSystem = new DefaultServerFileSystem(homeDir, temp.newFolder(), server);
  64. jarInstaller = new ServerPluginJarInstaller();
  65. jarsInstaller = new ServerPluginJarsInstaller(server, upgradeStatus, fileSystem, jarInstaller);
  66. }
  67. private File jar(String name) throws Exception {
  68. return new File(Resources.getResource(getClass(), "ServerPluginJarsInstallerTest/" + name).toURI());
  69. }
  70. @Test
  71. public void copy_bundled_plugin_on_fresh_install() throws Exception {
  72. when(upgradeStatus.isFreshInstall()).thenReturn(true);
  73. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), bundledDir);
  74. jarsInstaller.install();
  75. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).hasSize(1);
  76. assertThat(new File(pluginsDir, "foo-plugin-1.0.jar")).exists().isFile();
  77. PluginMetadata plugin = jarsInstaller.getMetadata("foo");
  78. assertThat(plugin.getName()).isEqualTo("Foo");
  79. assertThat(plugin.getDeployedFiles()).hasSize(1);
  80. assertThat(plugin.isCore()).isFalse();
  81. assertThat(plugin.isUseChildFirstClassLoader()).isFalse();
  82. }
  83. @Test
  84. public void do_not_copy_bundled_plugin_on_non_fresh_install() throws Exception {
  85. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  86. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), bundledDir);
  87. jarsInstaller.install();
  88. assertThat(FileUtils.listFiles(pluginsDir, new String[]{"jar"}, false)).isEmpty();
  89. }
  90. @Test
  91. public void do_not_copy_bundled_plugin_if_already_installed() throws Exception {
  92. // fresh install but plugins are already packaged in extensions/plugins
  93. when(upgradeStatus.isFreshInstall()).thenReturn(true);
  94. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), bundledDir);
  95. FileUtils.copyFileToDirectory(jar("foo-plugin-2.0.jar"), pluginsDir);
  96. FileUtils.copyFileToDirectory(jar("bar-plugin-1.0.jar"), pluginsDir);
  97. jarsInstaller.install();
  98. // do not copy foo 1.0
  99. assertThat(FileUtils.listFiles(pluginsDir, new String[]{"jar"}, false)).hasSize(2);
  100. assertThat(new File(pluginsDir, "foo-plugin-2.0.jar")).exists().isFile();
  101. assertThat(new File(pluginsDir, "bar-plugin-1.0.jar")).exists().isFile();
  102. PluginMetadata plugin = jarsInstaller.getMetadata("foo");
  103. assertThat(plugin.getVersion()).isEqualTo("2.0");
  104. }
  105. @Test
  106. public void deploy_installed_plugin() throws Exception {
  107. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  108. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), pluginsDir);
  109. jarsInstaller.install();
  110. // check that the plugin is registered
  111. assertThat(jarsInstaller.getMetadata()).hasSize(1);
  112. PluginMetadata plugin = jarsInstaller.getMetadata("foo");
  113. assertThat(plugin.getName()).isEqualTo("Foo");
  114. assertThat(plugin.getDeployedFiles()).hasSize(1);
  115. assertThat(plugin.isCore()).isFalse();
  116. assertThat(plugin.isUseChildFirstClassLoader()).isFalse();
  117. // check that the file is still present in extensions/plugins
  118. assertThat(FileUtils.listFiles(pluginsDir, new String[]{"jar"}, false)).hasSize(1);
  119. assertThat(new File(pluginsDir, "foo-plugin-1.0.jar")).exists().isFile();
  120. }
  121. @Test
  122. public void ignore_non_plugin_jars() throws Exception {
  123. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  124. FileUtils.copyFileToDirectory(jar("not-a-plugin.jar"), pluginsDir);
  125. jarsInstaller.install();
  126. // nothing to install but keep the file
  127. assertThat(jarsInstaller.getMetadata()).isEmpty();
  128. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).hasSize(1);
  129. assertThat(new File(pluginsDir, "not-a-plugin.jar")).exists().isFile();
  130. }
  131. @Test
  132. public void fail_if_plugin_requires_greater_SQ_version() throws Exception {
  133. exception.expect(MessageException.class);
  134. exception.expectMessage("Plugin switchoffviolations needs a more recent version of SonarQube than 2.0. At least 2.5 is expected");
  135. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  136. when(server.getVersion()).thenReturn("2.0");
  137. FileUtils.copyFileToDirectory(jar("require-sq-2.5.jar"), pluginsDir);
  138. jarsInstaller.install();
  139. }
  140. @Test
  141. public void move_downloaded_plugins() throws Exception {
  142. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), downloadsDir);
  143. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  144. jarsInstaller.install();
  145. assertThat(FileUtils.listFiles(pluginsDir, new String[]{"jar"}, false)).hasSize(1);
  146. assertThat(FileUtils.listFiles(downloadsDir, new String[] {"jar"}, false)).isEmpty();
  147. assertThat(new File(pluginsDir, "foo-plugin-1.0.jar")).exists().isFile();
  148. }
  149. @Test
  150. public void downloaded_plugins_overrides_existing_plugin() throws Exception {
  151. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), pluginsDir);
  152. FileUtils.copyFileToDirectory(jar("foo-plugin-2.0.jar"), downloadsDir);
  153. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  154. jarsInstaller.install();
  155. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).hasSize(1);
  156. assertThat(FileUtils.listFiles(downloadsDir, new String[] {"jar"}, false)).isEmpty();
  157. assertThat(new File(pluginsDir, "foo-plugin-2.0.jar")).exists().isFile();
  158. }
  159. @Test
  160. public void downloaded_plugins_overrides_existing_plugin_even_if_same_filename() throws Exception {
  161. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), pluginsDir, true);
  162. // foo-plugin-1.0.jar in extensions/downloads is in fact version 2.0. It's used to verify
  163. // that it has correctly overridden extensions/plugins/foo-plugin-1.0.jar
  164. FileUtils.copyFile(jar("foo-plugin-2.0.jar"), new File(downloadsDir, "foo-plugin-1.0.jar"));
  165. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  166. jarsInstaller.install();
  167. PluginMetadata plugin = jarsInstaller.getMetadata("foo");
  168. assertThat(plugin).isNotNull();
  169. assertThat(plugin.getVersion()).isEqualTo("2.0");
  170. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).hasSize(1);
  171. assertThat(FileUtils.listFiles(downloadsDir, new String[] {"jar"}, false)).isEmpty();
  172. File installed = new File(pluginsDir, "foo-plugin-1.0.jar");
  173. assertThat(installed).exists().isFile();
  174. }
  175. @Test
  176. public void delete_trash() throws Exception {
  177. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), trashDir);
  178. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  179. jarsInstaller.install();
  180. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).isEmpty();
  181. assertThat(trashDir).doesNotExist();
  182. }
  183. @Test
  184. public void fail_if_two_installed_plugins_with_same_key() throws Exception {
  185. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  186. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), pluginsDir);
  187. FileUtils.copyFileToDirectory(jar("foo-plugin-2.0.jar"), pluginsDir);
  188. try {
  189. jarsInstaller.install();
  190. fail();
  191. } catch (MessageException e) {
  192. assertThat(e.getMessage())
  193. .contains("Found two files for the same plugin 'foo'")
  194. .contains("foo-plugin-1.0.jar")
  195. .contains("foo-plugin-2.0.jar");
  196. }
  197. }
  198. @Test
  199. public void uninstall_plugin() throws Exception {
  200. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  201. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), pluginsDir);
  202. jarsInstaller.install();
  203. jarsInstaller.uninstall("foo");
  204. assertThat(FileUtils.listFiles(pluginsDir, new String[]{"jar"}, false)).isEmpty();
  205. assertThat(FileUtils.listFiles(trashDir, new String[] {"jar"}, false)).hasSize(1);
  206. assertThat(jarsInstaller.getUninstalledPluginFilenames()).containsOnly("foo-plugin-1.0.jar");
  207. }
  208. @Test
  209. public void pending_removals_reads_metadata() throws Exception {
  210. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  211. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), pluginsDir);
  212. jarsInstaller.install();
  213. jarsInstaller.uninstall("foo");
  214. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).isEmpty();
  215. assertThat(FileUtils.listFiles(trashDir, new String[] {"jar"}, false)).hasSize(1);
  216. Collection<DefaultPluginMetadata> removals = jarsInstaller.getUninstalledPlugins();
  217. assertThat(removals).hasSize(1);
  218. PluginMetadata metadata = removals.iterator().next();
  219. assertThat(metadata.getKey()).isEqualTo("foo");
  220. assertThat(metadata.getName()).isEqualTo("Foo");
  221. assertThat(metadata.getVersion()).isEqualTo("1.0");
  222. assertThat(metadata.getOrganization()).isEqualTo("SonarSource");
  223. assertThat(metadata.getOrganizationUrl()).isEqualTo("http://www.sonarsource.org");
  224. assertThat(metadata.getLicense()).isEqualTo("LGPL 3");
  225. assertThat(metadata.getMainClass()).isEqualTo("foo.Main");
  226. }
  227. @Test
  228. public void cancel_uninstallation() throws Exception {
  229. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  230. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), pluginsDir);
  231. jarsInstaller.install();
  232. jarsInstaller.uninstall("foo");
  233. jarsInstaller.cancelUninstalls();
  234. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).hasSize(1);
  235. assertThat(FileUtils.listFiles(trashDir, new String[] {"jar"}, false)).hasSize(0);
  236. assertThat(jarsInstaller.getUninstalledPluginFilenames()).isEmpty();
  237. }
  238. @Test
  239. public void deploy_core_plugins() throws Exception {
  240. when(upgradeStatus.isFreshInstall()).thenReturn(false);
  241. FileUtils.copyFileToDirectory(jar("foo-plugin-1.0.jar"), coreDir);
  242. jarsInstaller.install();
  243. // do not deploy in extensions/plugins
  244. assertThat(FileUtils.listFiles(pluginsDir, new String[] {"jar"}, false)).hasSize(0);
  245. // do not remove from lib/core-plugins
  246. assertThat(FileUtils.listFiles(coreDir, new String[] {"jar"}, false)).hasSize(1);
  247. PluginMetadata plugin = jarsInstaller.getMetadata("foo");
  248. assertThat(plugin).isNotNull();
  249. assertThat(plugin.isCore()).isTrue();
  250. }
  251. }