return loadSecretFileFromFile(path);
}
- Key loadSecretFileFromFile(@Nullable String path) throws IOException {
+ Key loadSecretFileFromFile(String path) throws IOException {
if (StringUtils.isBlank(path)) {
throw new IllegalStateException("Secret key not found. Please set the property " + ENCRYPTION_SECRET_KEY_PATH);
}
}
return pathToSecretKey;
}
-
- public void setPathToSecretKey(@Nullable String pathToSecretKey) {
- this.pathToSecretKey = pathToSecretKey;
- }
}
import java.io.File;
import java.io.FileReader;
-import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
public static Props loadPropsFromCommandLineArgs(String[] args) {
if (args.length != 1) {
- throw new IllegalStateException("Only a single command-line argument is accepted " +
+ throw new IllegalArgumentException("Only a single command-line argument is accepted " +
"(absolute path to configuration file)");
}
File propertyFile = new File(args[0]);
- if (!propertyFile.exists()) {
- throw new IllegalStateException("Property file '" + args[0] + "' does not exist! ");
- }
-
Properties properties = new Properties();
FileReader reader = null;
try {
reader = new FileReader(propertyFile);
properties.load(reader);
- } catch (IOException e) {
- throw new IllegalStateException("Could not read properties from file '" + args[0] + "'", e);
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not read properties from file: " + args[0], e);
} finally {
IOUtils.closeQuietly(reader);
FileUtils.deleteQuietly(propertyFile);
* Verify that temp directory is writable
*/
private void checkWritableTempDir() {
- String tempPath = System.getProperty("java.io.tmpdir");
+ checkWritableDir(System.getProperty("java.io.tmpdir"));
+ }
+
+ void checkWritableDir(String tempPath) {
try {
File tempFile = File.createTempFile("check", "tmp", new File(tempPath));
FileUtils.deleteQuietly(tempFile);
} catch (IOException e) {
- throw new MessageException(String.format(
- "Temp directory is not writable: %s. Reason: %s", tempPath, e.getMessage()));
+ throw new IllegalStateException(String.format("Temp directory is not writable: %s", tempPath), e);
}
}
private final static Logger LOGGER = LoggerFactory.getLogger(MonitoredProcess.class);
- public static final String DEBUG_AGENT = "-agentlib:jdwp";
private static final long AUTOKILL_TIMEOUT_MS = 30000L;
private static final long AUTOKILL_CHECK_DELAY_MS = 2000L;
public static final String NAME_PROPERTY = "pName";
private final boolean isMonitored;
protected MonitoredProcess(Props props) {
- this(props, !props.containsValue(DEBUG_AGENT));
+ this(props, !ProcessUtils.isJvmDebugEnabled());
}
protected MonitoredProcess(Props props, boolean monitor) {
throw new IllegalStateException("Already started");
}
LOGGER.debug("Process[{}] starting", name);
- scheduleAutokill(this.isMonitored);
+ scheduleAutokill(isMonitored);
try {
doStart();
} catch (Exception e) {
* If the process does not receive pings during the max allowed period, then
* process auto-kills
*/
- private void scheduleAutokill(final Boolean isMonitored) {
+ private void scheduleAutokill(final boolean isMonitored) {
final Runnable breakOnMissingPing = new Runnable() {
@Override
public void run() {
import javax.annotation.Nullable;
+import java.lang.management.ManagementFactory;
+
public class ProcessUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ProcessUtils.class);
IOUtils.closeQuietly(process.getErrorStream());
}
}
+
+ public static boolean isJvmDebugEnabled() {
+ return ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
+ }
}
*/
public class ProcessWrapper extends Thread implements Terminable {
- private final static Logger LOGGER = LoggerFactory.getLogger(ProcessWrapper.class);
+ private static final Logger LOGGER = LoggerFactory.getLogger(ProcessWrapper.class);
public static final long READY_TIMEOUT_MS = 300000L;
}
private String localAddress() {
- // TODO to be replaced by InetAddress.getLoopbackAddress() in Java 7
+ // to be replaced by InetAddress.getLoopbackAddress() in Java 7 ?
return "127.0.0.1";
}
}
public boolean waitForReady() throws InterruptedException {
- if (processMXBean == null) {
- return false;
- }
long now = 0;
long wait = 500L;
while (now < READY_TIMEOUT_MS) {
return properties.containsKey(key);
}
- public boolean containsValue(String value) {
- for (Object propertyValue : properties.values()) {
- if (propertyValue.toString().contains(value)) {
- return true;
- }
- }
- return false;
- }
-
@CheckForNull
public String of(String key) {
String value = properties.getProperty(key);
package org.sonar.process;
import com.google.common.collect.Maps;
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import java.io.File;
import java.util.Map;
import java.util.Properties;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
public class ConfigurationUtilsTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
@Test
public void shouldInterpolateVariables() {
Properties input = new Properties();
Properties output = ConfigurationUtils.interpolateVariables(input, variables);
- assertThat(output.size(), is(3));
- assertThat(output.getProperty("hello"), is("world"));
- assertThat(output.getProperty("url"), is("jdbc:h2:mem"));
- assertThat(output.getProperty("do_not_change"), is("${SONAR_JDBC_URL}"));
+ assertThat(output).hasSize(3);
+ assertThat(output.getProperty("hello")).isEqualTo("world");
+ assertThat(output.getProperty("url")).isEqualTo("jdbc:h2:mem");
+ assertThat(output.getProperty("do_not_change")).isEqualTo("${SONAR_JDBC_URL}");
// input is not changed
- assertThat(input.size(), is(3));
- assertThat(input.getProperty("hello"), is("world"));
- assertThat(input.getProperty("url"), is("${env:SONAR_JDBC_URL}"));
- assertThat(input.getProperty("do_not_change"), is("${SONAR_JDBC_URL}"));
+ assertThat(input).hasSize(3);
+ assertThat(input.getProperty("hello")).isEqualTo("world");
+ assertThat(input.getProperty("url")).isEqualTo("${env:SONAR_JDBC_URL}");
+ assertThat(input.getProperty("do_not_change")).isEqualTo("${SONAR_JDBC_URL}");
+ }
+
+ @Test
+ public void loadPropsFromCommandLineArgs_missing_argument() throws Exception {
+ try {
+ ConfigurationUtils.loadPropsFromCommandLineArgs(new String[0]);
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e.getMessage()).startsWith("Only a single command-line argument is accepted");
+ }
+ }
+
+ @Test
+ public void loadPropsFromCommandLineArgs_load_properties_from_file() throws Exception {
+ File propsFile = temp.newFile();
+ FileUtils.write(propsFile, "foo=bar");
+
+ Props result = ConfigurationUtils.loadPropsFromCommandLineArgs(new String[] {propsFile.getAbsolutePath()});
+ assertThat(result.of("foo")).isEqualTo("bar");
+ assertThat(result.rawProperties()).hasSize(1);
}
+ @Test
+ public void loadPropsFromCommandLineArgs_file_does_not_exist() throws Exception {
+ File propsFile = temp.newFile();
+ FileUtils.deleteQuietly(propsFile);
+
+ try {
+ ConfigurationUtils.loadPropsFromCommandLineArgs(new String[]{propsFile.getAbsolutePath()});
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Could not read properties from file: " + propsFile.getAbsolutePath());
+ }
+ }
}
package org.sonar.process;
import org.fest.assertions.Assertions;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
import static org.fest.assertions.Fail.fail;
public class MinimumViableSystemTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
/**
* Verifies that all checks can be verified without error.
* Test environment does not necessarily follows all checks.
mve.checkJavaOptions();
// do not fail
}
+
+ @Test
+ public void checkWritableTempDir() throws Exception {
+ File dir = temp.newFolder();
+ MinimumViableSystem mve = new MinimumViableSystem();
+
+ mve.checkWritableDir(dir.getAbsolutePath());
+
+ dir.delete();
+ try {
+ mve.checkWritableDir(dir.getAbsolutePath());
+ fail();
+ } catch (IllegalStateException e) {
+ Assertions.assertThat(e).hasMessage("Temp directory is not writable: " + dir.getAbsolutePath());
+ }
+ }
}
public void should_not_monitor_debug() throws Exception {
Properties properties = new Properties();
properties.setProperty(MonitoredProcess.NAME_PROPERTY, DummyProcess.NAME);
- properties.setProperty("sonar.search.javaOpts", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005");
- DummyProcess dummyProcess = new DummyProcess(new Props(properties));
+ DummyProcess dummyProcess = new DummyProcess(new Props(properties), false);
assertThat(dummyProcess.isMonitored()).isFalse();
}
assertThat(dummyProcess.isMonitored()).isTrue();
}
-
@Test(timeout = 3000L)
public void monitor_dies_when_no_pings() throws Exception {
Properties properties = new Properties();
public void process_does_not_die_when_debugged() throws Exception {
Properties properties = new Properties();
properties.setProperty(MonitoredProcess.NAME_PROPERTY, DummyProcess.NAME);
- properties.setProperty("sonar.search.javaOpts", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005");
- final DummyProcess dummyProcess = new DummyProcess(new Props(properties));
+ final DummyProcess dummyProcess = new DummyProcess(new Props(properties), false);
assertThat(dummyProcess.isMonitored()).isFalse();
dummyProcess.setTimeout(100L).setCheckDelay(100L);
assertProcessTerminated(dummyProcess);
}
-
private void assertJoinAndTerminate(DummyProcess dummyProcess, Thread process) throws InterruptedException {
process.join();
assertProcessTerminated(dummyProcess);
assertThat(dummyProcess.isTerminated()).isFalse();
}
-
private void assertProcessCreatedFile(DummyProcess dummyProcess) {
assertThat(dummyProcess.getCheckFile()).isNotNull();
assertThat(dummyProcess.getCheckFile().getName()).isEqualTo(DummyProcess.CHECKFILE_NAME);
}
-}
\ No newline at end of file
+}
import org.junit.Test;
+import static org.fest.assertions.Assertions.assertThat;
+
public class ProcessUtilsTest {
@Test
- public void check_process_alive() {
- ProcessBuilder processBuilder = new ProcessBuilder();
+ public void isJvmDebugEnabled() {
+ assertThat(ProcessUtils.isJvmDebugEnabled()).isFalse();
}
-}
\ No newline at end of file
+}
assertThat(props.booleanOf("foo", false)).isTrue();
assertThat(props.booleanOf("bar", true)).isFalse();
}
+
+ @Test
+ public void setDefault() throws Exception {
+ Properties p = new Properties();
+ p.setProperty("foo", "foo_value");
+ Props props = new Props(p);
+ props.setDefault("foo", "foo_def");
+ props.setDefault("bar", "bar_def");
+
+ assertThat(props.of("foo")).isEqualTo("foo_value");
+ assertThat(props.of("bar")).isEqualTo("bar_def");
+ assertThat(props.of("other")).isNull();
+ }
+
+ @Test
+ public void set() throws Exception {
+ Properties p = new Properties();
+ p.setProperty("foo", "old_foo");
+ Props props = new Props(p);
+ props.set("foo", "new_foo");
+ props.set("bar", "new_bar");
+
+ assertThat(props.of("foo")).isEqualTo("new_foo");
+ assertThat(props.of("bar")).isEqualTo("new_bar");
+ }
+
+ @Test
+ public void raw_properties() throws Exception {
+ Properties p = new Properties();
+ p.setProperty("encrypted_prop", "{aes}abcde");
+ p.setProperty("clear_prop", "foo");
+ Props props = new Props(p);
+
+ assertThat(props.rawProperties()).hasSize(2);
+ // do not decrypt
+ assertThat(props.rawProperties().get("encrypted_prop")).isEqualTo("{aes}abcde");
+ assertThat(props.rawProperties().get("clear_prop")).isEqualTo("foo");
+
+ }
}
try {
Logger logger = LoggerFactory.getLogger(getClass());
- if (props.containsValue(MonitoredProcess.DEBUG_AGENT)) {
- logger.info("**********************************************************");
- logger.info("* sonarQube is running in debug mode. No monitoring *");
- logger.info("**********************************************************");
- }
monitor.start();
File homeDir = props.fileOf("sonar.path.home");