ServerInstaller { public Server install(SonarDistribution distrib) { Packaging packaging = packagingResolver.resolve(distrib); File homeDir = unzip(packaging); if (!distrib.isKeepBundledPlugins()) { removeBundledPlugins(homeDir); } copyBundledPlugins(distrib.getBundledPluginLocations(), homeDir); copyExternalPlugins(packaging, distrib.getPluginLocations(), homeDir); copyJdbcDriver(homeDir); Properties properties = configureProperties(distrib); writePropertiesFile(properties, homeDir); String host = properties.getProperty(WEB_HOST_PROPERTY); String url = format("http: return new Server(locators, homeDir, packaging.getEdition(), packaging.getVersion(), HttpUrl.parse(url), Integer.parseInt(properties.getProperty(SEARCH_PORT_PROPERTY)), (String) properties.get(SONAR_CLUSTER_NODE_NAME)); } ServerInstaller(PackagingResolver packagingResolver, Configuration configuration, Locators locators, DatabaseClient databaseClient); Server install(SonarDistribution distrib);  }
@Test public void test_install() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); Server server = newInstaller().install(new SonarDistribution().setVersion(VERSION_4_5_6)); assertThat(server.version()).isEqualTo(Version.create(VERSION_4_5_6)); assertThat(server.getHome().getParentFile().getParentFile()).isEqualTo(workspaceDir); assertThat(server.getEdition()).isEqualTo(Edition.COMMUNITY); Properties props = openPropertiesFile(server); assertThat(props.getProperty("sonar.jdbc.url")).isEqualTo("jdbc:h2:mem"); }
@Test public void use_random_web_port_on_loopback_address_if_not_defined() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); Server server = newInstaller().install(new SonarDistribution().setVersion(VERSION_4_5_6)); URL serverUrl = new URL(server.getUrl()); assertThat(InetAddress.getByName(serverUrl.getHost()).isLoopbackAddress()).isTrue(); assertThat(serverUrl.getPort()).isGreaterThan(1023); assertThat(serverUrl.getPath()).isEqualTo(""); }
@Test public void show_0_0_0_0_as_localhost() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distribution = new SonarDistribution().setVersion(VERSION_4_5_6); distribution .setServerProperty("sonar.web.host", "0.0.0.0"); Server server = newInstaller().install(distribution); URL serverUrl = new URL(server.getUrl()); assertThat(InetAddress.getByName(serverUrl.getHost()).isLoopbackAddress()).isTrue(); }
@Test public void use_random_search_port_on_loopback_address_if_not_defined() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); Server server = newInstaller().install(new SonarDistribution().setVersion(VERSION_4_5_6)); assertThat(server.getSearchPort()).isGreaterThan(1023); }
@Test public void web_server_is_configured_through_sonar_properties() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distribution = new SonarDistribution().setVersion(VERSION_4_5_6); distribution .setServerProperty("sonar.web.port", "9999") .setServerProperty("sonar.web.context", "/foo") .setServerProperty("sonar.search.port", "6666") ; Server server = newInstaller().install(distribution); URL serverUrl = new URL(server.getUrl()); assertThat(serverUrl.getPort()).isEqualTo(9999); assertThat(serverUrl.getPath()).isEqualTo("/foo"); assertThat(server.getSearchPort()).isEqualTo(6666); }
@Test public void web_port_can_be_set_through_special_property_in_orchestrator_properties_file() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distribution = new SonarDistribution().setVersion(VERSION_4_5_6); Configuration.Builder configBuilder = Configuration.builder() .setProperty("orchestrator.container.port", "9999"); Server server = newInstaller(configBuilder).install(distribution); URL serverUrl = new URL(server.getUrl()); assertThat(InetAddress.getByName(serverUrl.getHost()).isLoopbackAddress()).isTrue(); assertThat(serverUrl.getPort()).isEqualTo(9999); assertThat(serverUrl.getPath()).isEqualTo(""); }
@Test public void special_orchestrator_property_for_web_port_is_not_used_if_port_defined_explicitly() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distribution = new SonarDistribution().setVersion(VERSION_4_5_6) .setServerProperty("sonar.web.port", "10000"); Configuration.Builder configBuilder = Configuration.builder() .setProperty("orchestrator.container.port", "9999"); Server server = newInstaller(configBuilder).install(distribution); URL serverUrl = new URL(server.getUrl()); assertThat(serverUrl.getPort()).isEqualTo(10000); }
@Test public void installation_directories_do_not_overlap() throws Exception { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); Server server1 = newInstaller().install(new SonarDistribution().setVersion(VERSION_4_5_6).setServerProperty("test.id", "1")); Server server2 = newInstaller().install(new SonarDistribution().setVersion(VERSION_4_5_6).setServerProperty("test.id", "2")); assertThat(server1.getHome()).exists().isDirectory(); assertThat(server2.getHome()).exists().isDirectory(); assertThat(server1.getHome()).isNotEqualTo(server2.getHome()); assertThat(openPropertiesFile(server1).getProperty("test.id")).isEqualTo("1"); assertThat(openPropertiesFile(server2).getProperty("test.id")).isEqualTo("2"); }
@Test public void copy_jdbc_driver_if_defined() { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); when(dbClient.getDriverFile()).thenReturn(FileUtils.toFile(getClass().getResource("ServerInstallerTest/fake-oracle-driver.jar"))); when(dbClient.getDialect()).thenReturn("oracle"); Server server = newInstaller().install(new SonarDistribution().setVersion(VERSION_4_5_6)); assertThat(new File(server.getHome(), "extensions/jdbc-driver/oracle/fake-oracle-driver.jar")).exists().isFile(); }
@Test public void throw_ISE_if_fail_to_copy_jdbc_driver() throws Exception { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to copy JDBC driver"); prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); File invalidDriver = temp.newFile(); invalidDriver.delete(); when(dbClient.getDriverFile()).thenReturn(invalidDriver); when(dbClient.getDialect()).thenReturn("oracle"); newInstaller().install(new SonarDistribution().setVersion(VERSION_4_5_6)); }
@Test public void copy_plugins() throws IOException { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distribution = new SonarDistribution().setVersion(VERSION_4_5_6); distribution.addPluginLocation(MavenLocation.of("fake", "sonar-foo-plugin", "1.0")); distribution.addPluginLocation(MavenLocation.of("fake", "sonar-bar-plugin", "1.0")); File jar1 = temp.newFile(); File jar2 = temp.newFile(); prepareCopyOfPlugin("sonar-foo-plugin", "1.0", jar1); prepareCopyOfPlugin("sonar-bar-plugin", "1.0", jar2); Server server = newInstaller().install(distribution); Collection<File> installedFiles = FileUtils.listFiles(new File(server.getHome(), "extensions/downloads"), null, false); assertThat(installedFiles).extracting(File::getName).containsExactlyInAnyOrder(jar1.getName(), jar2.getName()); }
@Test public void throw_ISE_if_fail_to_copy_plugins() throws Exception { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Can not find the plugin"); prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distrib = new SonarDistribution().setVersion(VERSION_4_5_6); File invalidPlugin = temp.newFile("plugin.jar"); invalidPlugin.delete(); distrib.addPluginLocation(FileLocation.of(invalidPlugin)); newInstaller().install(distrib); }
@Test public void remove_bundled_plugins_by_default() { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distrib = new SonarDistribution().setVersion(VERSION_4_5_6); Server server = newInstaller().install(distrib); assertThat(bundledPlugin(server)).doesNotExist(); }
@Test public void keep_bundled_plugins() { prepareResolutionOfPackaging(Edition.COMMUNITY, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distrib = new SonarDistribution().setVersion(VERSION_4_5_6).setKeepBundledPlugins(true); Server server = newInstaller().install(distrib); assertThat(bundledPlugin(server)).isFile().exists(); }
@Test public void do_not_override_license_plugin_to_commercial_editions_before_7_2_if_already_installed() throws Exception { File licenseJar = temp.newFile("sonar-license-plugin-3.2.jar"); prepareResolutionOfPackaging(Edition.ENTERPRISE, Version.create(VERSION_4_5_6), SQ_ZIP); SonarDistribution distribution = new SonarDistribution().setVersion(VERSION_4_5_6); distribution.addPluginLocation(MavenLocation.of("com.sonarsource.license", "sonar-license-plugin", "3.2")); prepareCopyOfPlugin("sonar-license-plugin", "3.2", licenseJar); Server server = newInstaller().install(distribution); File pluginsDir = new File(server.getHome(), "extensions/downloads/"); assertThat(new File(pluginsDir, licenseJar.getName())).exists().isFile(); }
@Test public void do_not_install_license_plugin_on_commercial_editions_after_7_2() { Version version = Version.create("7.2.0.10000"); prepareResolutionOfPackaging(Edition.ENTERPRISE, version, SQ_ZIP); SonarDistribution distribution = new SonarDistribution().setVersion(version.toString()); Server server = newInstaller().install(distribution); File downloadsDir = new File(server.getHome(), "extensions/downloads"); assertThat(downloadsDir.listFiles()).isEmpty(); }