import org.sonar.process.Props;
import org.sonar.process.System2;
-import static org.apache.commons.lang.SystemUtils.IS_OS_WINDOWS;
import static org.sonar.process.ProcessProperties.HTTPS_PROXY_HOST;
import static org.sonar.process.ProcessProperties.HTTPS_PROXY_PORT;
import static org.sonar.process.ProcessProperties.HTTP_PROXY_HOST;
private final Props props;
private final File tempDir;
+ private final System2 system2;
public CommandFactoryImpl(Props props, File tempDir, System2 system2) {
this.props = props;
this.tempDir = tempDir;
+ this.system2 = system2;
String javaToolOptions = system2.getenv(ENV_VAR_JAVA_TOOL_OPTIONS);
if (javaToolOptions != null && !javaToolOptions.trim().isEmpty()) {
LoggerFactory.getLogger(CommandFactoryImpl.class)
@Override
public AbstractCommand<?> createEsCommand() {
- if (IS_OS_WINDOWS) {
+ if (system2.isOsWindows()) {
return createEsCommandForWindows();
}
return createEsCommandForUnix();
}
private EsScriptCommand createEsCommandForUnix() {
- EsInstallation esInstallation = new EsInstallation(props);
- if (!esInstallation.getExecutable().exists()) {
- throw new IllegalStateException("Cannot find elasticsearch binary");
- }
- Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build();
-
- esInstallation
- .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory()))
- .setEsJvmOptions(new EsJvmOptions()
- .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS)
- .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
- .setEsYmlSettings(new EsYmlSettings(settingsMap))
- .setClusterName(settingsMap.get("cluster.name"))
- .setHost(settingsMap.get("network.host"))
- .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")));
-
+ EsInstallation esInstallation = createEsInstallation();
return new EsScriptCommand(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory())
.setEsInstallation(esInstallation)
.addOption("-Epath.conf=" + esInstallation.getConfDirectory().getAbsolutePath())
}
private JavaCommand createEsCommandForWindows() {
- EsInstallation esInstallation = new EsInstallation(props);
- if (!esInstallation.getExecutable().exists()) {
- throw new IllegalStateException("Cannot find elasticsearch binary");
- }
- Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build();
-
- esInstallation
- .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory()))
- .setEsJvmOptions(new EsJvmOptions()
- .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS)
- .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
- .setEsYmlSettings(new EsYmlSettings(settingsMap))
- .setClusterName(settingsMap.get("cluster.name"))
- .setHost(settingsMap.get("network.host"))
- .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")));
-
+ EsInstallation esInstallation = createEsInstallation();
return new JavaCommand<EsJvmOptions>(ProcessId.ELASTICSEARCH, esInstallation.getHomeDirectory())
.setEsInstallation(esInstallation)
.setReadsArgumentsFromFile(false)
.suppressEnvVariable(ENV_VAR_JAVA_TOOL_OPTIONS);
}
+ private EsInstallation createEsInstallation() {
+ EsInstallation esInstallation = new EsInstallation(props);
+ if (!esInstallation.getExecutable().exists()) {
+ throw new IllegalStateException("Cannot find elasticsearch binary");
+ }
+ Map<String, String> settingsMap = new EsSettings(props, esInstallation, System2.INSTANCE).build();
+
+ esInstallation
+ .setLog4j2Properties(new EsLogging().createProperties(props, esInstallation.getLogDirectory()))
+ .setEsJvmOptions(new EsJvmOptions()
+ .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_OPTS)
+ .addFromMandatoryProperty(props, ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS))
+ .setEsYmlSettings(new EsYmlSettings(settingsMap))
+ .setClusterName(settingsMap.get("cluster.name"))
+ .setHost(settingsMap.get("network.host"))
+ .setPort(Integer.valueOf(settingsMap.get("transport.tcp.port")));
+ return esInstallation;
+ }
+
@Override
public JavaCommand createWebCommand(boolean leader) {
File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME);
}
@Test
- public void createEsCommand_returns_command_for_default_settings() throws Exception {
+ public void createEsCommand_for_unix_returns_command_for_default_settings() throws Exception {
+ System2 system2 = Mockito.mock(System2.class);
+ when(system2.isOsWindows()).thenReturn(false);
prepareEsFileSystem();
Properties props = new Properties();
props.setProperty("sonar.search.host", "localhost");
- AbstractCommand esCommand = newFactory(props).createEsCommand();
+ AbstractCommand esCommand = newFactory(props, system2).createEsCommand();
EsInstallation esConfig = esCommand.getEsInstallation();
+ assertThat(esCommand).isInstanceOf(EsScriptCommand.class);
+ assertThat(esConfig.getClusterName()).isEqualTo("sonarqube");
+ assertThat(esConfig.getHost()).isNotEmpty();
+ assertThat(esConfig.getPort()).isEqualTo(9001);
+ assertThat(esConfig.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.getEnvVariables())
+ .contains(entry("ES_JVM_OPTIONS", new File(esConfDir, "jvm.options").getAbsolutePath()))
+ .containsKey("JAVA_HOME");
+ assertThat(esConfig.getEsYmlSettings()).isNotNull();
+
+ assertThat(esConfig.getLog4j2Properties())
+ .contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath()));
+
+ assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
+ }
+
+ @Test
+ public void createEsCommand_for_windows_returns_command_for_default_settings() throws Exception {
+ System2 system2 = Mockito.mock(System2.class);
+ when(system2.isOsWindows()).thenReturn(true);
+ prepareEsFileSystem();
+
+ Properties props = new Properties();
+ props.setProperty("sonar.search.host", "localhost");
+
+ AbstractCommand esCommand = newFactory(props, system2).createEsCommand();
+ EsInstallation esConfig = esCommand.getEsInstallation();
+
+ assertThat(esCommand).isInstanceOf(JavaCommand.class);
assertThat(esConfig.getClusterName()).isEqualTo("sonarqube");
assertThat(esConfig.getHost()).isNotEmpty();
assertThat(esConfig.getPort()).isEqualTo(9001);
assertThat(command.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
}
+ @Test
+ public void createCeCommand_returns_command_for_default_settings() throws Exception {
+ JavaCommand command = newFactory(new Properties()).createCeCommand();
+
+ assertThat(command.getClassName()).isEqualTo("org.sonar.ce.app.CeServer");
+ assertThat(command.getWorkDir().getAbsolutePath()).isEqualTo(homeDir.getAbsolutePath());
+ assertThat(command.getClasspath())
+ .containsExactlyInAnyOrder("./lib/common/*", "./lib/server/*", "./lib/ce/*");
+ 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.COMPUTE_ENGINE);
+ assertThat(command.getEnvVariables())
+ .isNotEmpty();
+ assertThat(command.getArguments())
+ // default settings
+ .contains(entry("sonar.web.javaOpts", "-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError"))
+ .contains(entry("sonar.cluster.enabled", "false"));
+
+ assertThat(command.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
+ }
+
@Test
public void createWebCommand_configures_command_with_overridden_settings() throws Exception {
Properties props = new Properties();
FileUtils.touch(new File(homeDir, "elasticsearch/bin/elasticsearch.bat"));
}
- private CommandFactory newFactory(Properties userProps) throws IOException {
+ private CommandFactoryImpl newFactory(Properties userProps) throws IOException {
+ return newFactory(userProps, System2.INSTANCE);
+ }
+
+ private CommandFactoryImpl newFactory(Properties userProps, System2 system2) throws IOException {
Properties p = new Properties();
p.setProperty("sonar.path.home", homeDir.getAbsolutePath());
p.setProperty("sonar.path.temp", tempDir.getAbsolutePath());
Props props = new Props(p);
ProcessProperties.completeDefaults(props);
- return new CommandFactoryImpl(props, tempDir, System2.INSTANCE);
+ return new CommandFactoryImpl(props, tempDir, system2);
}
private <T> void attachMemoryAppenderToLoggerOf(Class<T> loggerClass) {
package org.sonar.process;
import java.util.Map;
+import org.apache.commons.lang.SystemUtils;
/**
* An interface allowing to wrap around static call to {@link System} class.
public String getenv(String name) {
return System.getenv(name);
}
+
+ @Override
+ public boolean isOsWindows() {
+ return SystemUtils.IS_OS_WINDOWS;
+ }
};
/**
* Proxy to {@link System#getenv(String)}.
*/
String getenv(String name);
+
+ /**
+ * True if this is MS Windows.
+ */
+ boolean isOsWindows();
}