aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2017-09-08 16:40:41 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2017-09-08 16:40:41 +0200
commit942e46b9b3692a4c4147597c127189fa779dbfad (patch)
tree52839b3e85a2deb0c9ac66ae50b05df538243912 /server/sonar-process/src
parent931c34cc394b28e86e62f59b4017bb09133b6c65 (diff)
downloadsonarqube-942e46b9b3692a4c4147597c127189fa779dbfad.tar.gz
sonarqube-942e46b9b3692a4c4147597c127189fa779dbfad.zip
Add missing unit tests
Diffstat (limited to 'server/sonar-process/src')
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/es/EsFileSystem.java10
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java186
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/es/EsFileSystemTest.java28
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java4
4 files changed, 204 insertions, 24 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/es/EsFileSystem.java b/server/sonar-process/src/main/java/org/sonar/process/es/EsFileSystem.java
index d8fc7c099d0..033791c8049 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/es/EsFileSystem.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/es/EsFileSystem.java
@@ -44,7 +44,7 @@ public class EsFileSystem {
this.homeDirectory = new File(sqHomeDir, "elasticsearch");
this.dataDirectory = buildDataDir(props, sqHomeDir);
this.confDirectory = buildConfDir(props);
- this.logDirectory = buildLogPath(props, sqHomeDir);
+ this.logDirectory = buildLogPath(props);
}
private static File buildDataDir(Props props, File sqHomeDir) {
@@ -55,12 +55,8 @@ public class EsFileSystem {
return new File(sqHomeDir, "data/es");
}
- private static File buildLogPath(Props props, File sqHomeDir) {
- String logPath = props.value(ProcessProperties.PATH_LOGS);
- if (StringUtils.isNotEmpty(logPath)) {
- return new File(logPath);
- }
- return new File(sqHomeDir, "log");
+ private static File buildLogPath(Props props) {
+ return props.nonNullValueAsFile(ProcessProperties.PATH_LOGS);
}
private static File buildConfDir(Props props) {
diff --git a/server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java b/server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java
new file mode 100644
index 00000000000..f2bc87961d9
--- /dev/null
+++ b/server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java
@@ -0,0 +1,186 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.process.command;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.process.ProcessId;
+import org.sonar.process.ProcessProperties;
+import org.sonar.process.Props;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.entry;
+
+public class CommandFactoryImplTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private File homeDir;
+ private File tempDir;
+ private File logsDir;
+
+ @Before
+ public void setUp() throws Exception {
+ homeDir = temp.newFolder();
+ tempDir = temp.newFolder();
+ logsDir = temp.newFolder();
+ }
+
+ @Test
+ public void createEsCommand_throws_ISE_if_es_binary_is_not_found() throws Exception {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Cannot find elasticsearch binary");
+
+ newFactory(new Properties()).createEsCommand();
+ }
+
+ @Test
+ public void createEsCommand_returns_command_for_default_settings() throws Exception {
+ prepareEsFileSystem();
+
+ Properties props = new Properties();
+ props.setProperty("sonar.search.host", "localhost");
+
+ EsCommand esCommand = newFactory(props).createEsCommand();
+
+ assertThat(esCommand.getClusterName()).isEqualTo("sonarqube");
+ assertThat(esCommand.getHost()).isNotEmpty();
+ assertThat(esCommand.getPort()).isEqualTo(9001);
+ assertThat(esCommand.getEsJvmOptions().getAll())
+ // enforced values
+ .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8")
+ // default settings
+ .contains("-Xms512m", "-Xmx512m", "-XX:+HeapDumpOnOutOfMemoryError");
+ File esConfDir = new File(tempDir, "conf/es");
+ assertThat(esCommand.getEsOptions()).containsOnly("-Epath.conf=" + esConfDir.getAbsolutePath());
+ assertThat(esCommand.getEnvVariables())
+ .contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath()))
+ .containsKey("JAVA_HOME");
+ assertThat(esCommand.getEsYmlSettings()).isNotNull();
+
+ assertThat(esCommand.getLog4j2Properties())
+ .contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath()));
+ }
+
+ @Test
+ public void createEsCommand_returns_command_for_overridden_settings() throws Exception {
+ prepareEsFileSystem();
+
+ Properties props = new Properties();
+ props.setProperty("sonar.search.host", "localhost");
+ props.setProperty("sonar.cluster.name", "foo");
+ props.setProperty("sonar.search.port", "1234");
+ props.setProperty("sonar.search.javaOpts", "-Xms10G -Xmx10G");
+
+ EsCommand command = newFactory(props).createEsCommand();
+
+ assertThat(command.getClusterName()).isEqualTo("foo");
+ assertThat(command.getPort()).isEqualTo(1234);
+ assertThat(command.getEsJvmOptions().getAll())
+ // enforced values
+ .contains("-XX:+UseConcMarkSweepGC", "-server", "-Dfile.encoding=UTF-8")
+ // user settings
+ .contains("-Xms10G", "-Xmx10G")
+ // default values disabled
+ .doesNotContain("-XX:+HeapDumpOnOutOfMemoryError");
+ }
+
+ @Test
+ public void createWebCommand_returns_command_for_default_settings() throws Exception {
+ JavaCommand command = newFactory(new Properties()).createWebCommand(true);
+
+ assertThat(command.getClassName()).isEqualTo("org.sonar.server.app.WebServer");
+ assertThat(command.getWorkDir().getAbsolutePath()).isEqualTo(homeDir.getAbsolutePath());
+ assertThat(command.getClasspath())
+ .containsExactlyInAnyOrder("./lib/common/*", "./lib/server/*");
+ assertThat(command.getJvmOptions().getAll())
+ // enforced values
+ .contains("-Djava.awt.headless=true", "-Dfile.encoding=UTF-8")
+ // default settings
+ .contains("-Djava.io.tmpdir=" + tempDir.getAbsolutePath(), "-Dfile.encoding=UTF-8")
+ .contains("-Xmx512m", "-Xms128m", "-XX:+HeapDumpOnOutOfMemoryError");
+ assertThat(command.getProcessId()).isEqualTo(ProcessId.WEB_SERVER);
+ assertThat(command.getEnvVariables())
+ .containsKey("JAVA_HOME");
+ assertThat(command.getArguments())
+ // default settings
+ .contains(entry("sonar.web.javaOpts", "-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError"))
+ .contains(entry("sonar.cluster.enabled", "false"));
+ }
+
+ @Test
+ public void createWebCommand_configures_command_with_overridden_settings() throws Exception {
+ Properties props = new Properties();
+ props.setProperty("sonar.web.port", "1234");
+ props.setProperty("sonar.web.javaOpts", "-Xmx10G");
+ JavaCommand command = newFactory(props).createWebCommand(true);
+
+ assertThat(command.getJvmOptions().getAll())
+ // enforced values
+ .contains("-Djava.awt.headless=true", "-Dfile.encoding=UTF-8")
+ // default settings
+ .contains("-Djava.io.tmpdir=" + tempDir.getAbsolutePath(), "-Dfile.encoding=UTF-8")
+ // overridden values
+ .contains("-Xmx10G")
+ .doesNotContain("-Xms128m", "-XX:+HeapDumpOnOutOfMemoryError");
+ assertThat(command.getArguments())
+ // default settings
+ .contains(entry("sonar.web.javaOpts", "-Xmx10G"))
+ .contains(entry("sonar.cluster.enabled", "false"));
+ }
+
+ @Test
+ public void createWebCommand_adds_configured_jdbc_driver_to_classpath() throws Exception {
+ Properties props = new Properties();
+ File driverFile = temp.newFile();
+ props.setProperty("sonar.jdbc.driverPath", driverFile.getAbsolutePath());
+
+ JavaCommand command = newFactory(props).createWebCommand(true);
+
+ assertThat(command.getClasspath())
+ .containsExactlyInAnyOrder("./lib/common/*", "./lib/server/*", driverFile.getAbsolutePath());
+ }
+
+ private void prepareEsFileSystem() throws IOException {
+ FileUtils.touch(new File(homeDir, "elasticsearch/bin/elasticsearch"));
+ FileUtils.touch(new File(homeDir, "elasticsearch/bin/elasticsearch.bat"));
+ }
+
+ private CommandFactory newFactory(Properties userProps) throws IOException {
+ Properties p = new Properties();
+ p.setProperty("sonar.path.home", homeDir.getAbsolutePath());
+ p.setProperty("sonar.path.temp", tempDir.getAbsolutePath());
+ p.setProperty("sonar.path.logs", logsDir.getAbsolutePath());
+ p.putAll(userProps);
+
+ Props props = new Props(p);
+ ProcessProperties.completeDefaults(props);
+ return new CommandFactoryImpl(props, tempDir);
+ }
+}
diff --git a/server/sonar-process/src/test/java/org/sonar/process/es/EsFileSystemTest.java b/server/sonar-process/src/test/java/org/sonar/process/es/EsFileSystemTest.java
index c2bf6e704ae..d3d10693656 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/es/EsFileSystemTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/es/EsFileSystemTest.java
@@ -65,6 +65,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, sqHomeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
@@ -77,6 +78,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, sqHomeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
@@ -91,6 +93,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, sqHomeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, tempDir.getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.PATH_DATA, dataDir.getAbsolutePath());
@@ -100,31 +103,17 @@ public class EsFileSystemTest {
}
@Test
- public void getLogDirectory_is_log_subdirectory_of_sq_home_directory_by_default() throws IOException {
+ public void getLogDirectory_is_configured_with_non_nullable_PATH_LOG_variable() throws IOException {
File sqHomeDir = temp.newFolder();
- Props props = new Props(new Properties());
- props.set(ProcessProperties.PATH_HOME, sqHomeDir.getAbsolutePath());
- props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
-
- EsFileSystem underTest = new EsFileSystem(props);
-
- assertThat(underTest.getLogDirectory()).isEqualTo(new File(sqHomeDir, "log"));
- }
-
- @Test
- public void override_log_dir() throws Exception {
- File sqHomeDir = temp.newFolder();
- File tempDir = temp.newFolder();
File logDir = temp.newFolder();
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, sqHomeDir.getAbsolutePath());
- props.set(ProcessProperties.PATH_TEMP, tempDir.getAbsolutePath());
-
+ props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.PATH_LOGS, logDir.getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
- assertThat(underTest.getDataDirectory()).isEqualTo(new File(sqHomeDir, "data/es"));
+ assertThat(underTest.getLogDirectory()).isEqualTo(logDir);
}
@Test
@@ -133,6 +122,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, tempDir.getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
@@ -145,6 +135,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, sqHomeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
@@ -161,6 +152,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, tempDir.getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
@@ -173,6 +165,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, tempDir.getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
@@ -185,6 +178,7 @@ public class EsFileSystemTest {
Props props = new Props(new Properties());
props.set(ProcessProperties.PATH_HOME, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, tempDir.getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsFileSystem underTest = new EsFileSystem(props);
diff --git a/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java b/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java
index 80c5128c1aa..643e80c859f 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/es/EsSettingsTest.java
@@ -54,6 +54,7 @@ public class EsSettingsTest {
props.set(ProcessProperties.SEARCH_HOST, "127.0.0.1");
props.set(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.CLUSTER_NAME, "sonarqube");
EsSettings esSettings = new EsSettings(props, new EsFileSystem(props));
@@ -89,6 +90,7 @@ public class EsSettingsTest {
props.set(ProcessProperties.SEARCH_HOST, "127.0.0.1");
props.set(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
props.set(ProcessProperties.CLUSTER_NAME, "sonarqube-1");
props.set(ProcessProperties.CLUSTER_ENABLED, "true");
props.set(ProcessProperties.CLUSTER_NODE_NAME, "node-1");
@@ -110,6 +112,7 @@ public class EsSettingsTest {
props.set(ProcessProperties.SEARCH_HOST, "127.0.0.1");
props.set(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsSettings esSettings = new EsSettings(props, new EsFileSystem(props));
Map<String, String> generated = esSettings.build();
assertThat(generated.get("node.name")).startsWith("sonarqube-");
@@ -125,6 +128,7 @@ public class EsSettingsTest {
props.set(ProcessProperties.SEARCH_HOST, "127.0.0.1");
props.set(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath());
props.set(ProcessProperties.PATH_TEMP, temp.newFolder().getAbsolutePath());
+ props.set(ProcessProperties.PATH_LOGS, temp.newFolder().getAbsolutePath());
EsSettings esSettings = new EsSettings(props, new EsFileSystem(props));
Map<String, String> generated = esSettings.build();
assertThat(generated.get("node.name")).isEqualTo("sonarqube");