|
@@ -19,18 +19,6 @@
|
|
|
|
|
|
package org.elasticsearch.bootstrap;
|
|
|
|
|
|
-import org.elasticsearch.Build;
|
|
|
-import org.elasticsearch.Version;
|
|
|
-import org.elasticsearch.common.SuppressForbidden;
|
|
|
-import org.elasticsearch.common.cli.CliTool.ExitStatus;
|
|
|
-import org.elasticsearch.common.cli.CliToolTestCase;
|
|
|
-import org.elasticsearch.common.cli.MockTerminal;
|
|
|
-import org.elasticsearch.common.cli.UserError;
|
|
|
-import org.elasticsearch.common.collect.Tuple;
|
|
|
-import org.elasticsearch.monitor.jvm.JvmInfo;
|
|
|
-import org.junit.After;
|
|
|
-import org.junit.Before;
|
|
|
-
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
@@ -38,18 +26,27 @@ import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK;
|
|
|
-import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK_AND_EXIT;
|
|
|
-import static org.elasticsearch.common.cli.CliTool.ExitStatus.USAGE;
|
|
|
-import static org.hamcrest.Matchers.containsString;
|
|
|
-import static org.hamcrest.Matchers.hasItem;
|
|
|
+import joptsimple.OptionException;
|
|
|
+import org.elasticsearch.Build;
|
|
|
+import org.elasticsearch.Version;
|
|
|
+import org.elasticsearch.cli.Command;
|
|
|
+import org.elasticsearch.cli.CommandTestCase;
|
|
|
+import org.elasticsearch.cli.UserError;
|
|
|
+import org.elasticsearch.common.SuppressForbidden;
|
|
|
+import org.elasticsearch.monitor.jvm.JvmInfo;
|
|
|
+import org.junit.After;
|
|
|
+import org.junit.Before;
|
|
|
+
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
-import static org.hamcrest.Matchers.nullValue;
|
|
|
|
|
|
@SuppressForbidden(reason = "modifies system properties intentionally")
|
|
|
-public class BootstrapCliParserTests extends CliToolTestCase {
|
|
|
+public class BootstrapCliParserTests extends CommandTestCase {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Command newCommand() {
|
|
|
+ return new BootstrapCliParser();
|
|
|
+ }
|
|
|
|
|
|
- private MockTerminal terminal = new MockTerminal();
|
|
|
private List<String> propertiesToClear = new ArrayList<>();
|
|
|
private Map<Object, Object> properties;
|
|
|
|
|
@@ -67,195 +64,93 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|
|
assertEquals("properties leaked", properties, new HashMap<>(System.getProperties()));
|
|
|
}
|
|
|
|
|
|
- public void testThatVersionIsReturned() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- ExitStatus status = parser.execute(args("version"));
|
|
|
- assertStatus(status, OK_AND_EXIT);
|
|
|
-
|
|
|
- String output = terminal.getOutput();
|
|
|
- assertTrue(output, output.contains(Version.CURRENT.toString()));
|
|
|
- assertTrue(output, output.contains(Build.CURRENT.shortHash()));
|
|
|
- assertTrue(output, output.contains(Build.CURRENT.date()));
|
|
|
- assertTrue(output, output.contains(JvmInfo.jvmInfo().version()));
|
|
|
+ void assertShouldRun(boolean shouldRun) {
|
|
|
+ BootstrapCliParser parser = (BootstrapCliParser)command;
|
|
|
+ assertEquals(shouldRun, parser.shouldRun());
|
|
|
}
|
|
|
|
|
|
- public void testThatVersionIsReturnedAsStartParameter() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- ExitStatus status = parser.execute(args("start -V"));
|
|
|
- assertStatus(status, OK_AND_EXIT);
|
|
|
-
|
|
|
- String output = terminal.getOutput();
|
|
|
+ public void testVersion() throws Exception {
|
|
|
+ String output = execute("-V");
|
|
|
assertTrue(output, output.contains(Version.CURRENT.toString()));
|
|
|
assertTrue(output, output.contains(Build.CURRENT.shortHash()));
|
|
|
assertTrue(output, output.contains(Build.CURRENT.date()));
|
|
|
assertTrue(output, output.contains(JvmInfo.jvmInfo().version()));
|
|
|
+ assertShouldRun(false);
|
|
|
|
|
|
- terminal.resetOutput();
|
|
|
- parser = new BootstrapCLIParser(terminal);
|
|
|
- status = parser.execute(args("start --version"));
|
|
|
- assertStatus(status, OK_AND_EXIT);
|
|
|
-
|
|
|
- output = terminal.getOutput();
|
|
|
+ terminal.reset();
|
|
|
+ output = execute("--version");
|
|
|
assertTrue(output, output.contains(Version.CURRENT.toString()));
|
|
|
assertTrue(output, output.contains(Build.CURRENT.shortHash()));
|
|
|
assertTrue(output, output.contains(Build.CURRENT.date()));
|
|
|
assertTrue(output, output.contains(JvmInfo.jvmInfo().version()));
|
|
|
+ assertShouldRun(false);
|
|
|
}
|
|
|
|
|
|
- public void testThatPidFileCanBeConfigured() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
+ public void testPidfile() throws Exception {
|
|
|
registerProperties("es.pidfile");
|
|
|
|
|
|
- ExitStatus status = parser.execute(args("start --pidfile")); // missing pid file
|
|
|
- assertStatus(status, USAGE);
|
|
|
+ // missing argument
|
|
|
+ OptionException e = expectThrows(OptionException.class, () -> {
|
|
|
+ execute("-p");
|
|
|
+ });
|
|
|
+ assertEquals("Option p/pidfile requires an argument", e.getMessage());
|
|
|
+ assertShouldRun(false);
|
|
|
|
|
|
// good cases
|
|
|
- status = parser.execute(args("start --pidfile /tmp/pid"));
|
|
|
- assertStatus(status, OK);
|
|
|
+ terminal.reset();
|
|
|
+ execute("--pidfile", "/tmp/pid");
|
|
|
assertSystemProperty("es.pidfile", "/tmp/pid");
|
|
|
+ assertShouldRun(true);
|
|
|
|
|
|
System.clearProperty("es.pidfile");
|
|
|
- status = parser.execute(args("start -p /tmp/pid"));
|
|
|
- assertStatus(status, OK);
|
|
|
+ terminal.reset();
|
|
|
+ execute("-p", "/tmp/pid");
|
|
|
assertSystemProperty("es.pidfile", "/tmp/pid");
|
|
|
+ assertShouldRun(true);
|
|
|
}
|
|
|
|
|
|
- public void testThatParsingDaemonizeWorks() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
+ public void testNoDaemonize() throws Exception {
|
|
|
registerProperties("es.foreground");
|
|
|
|
|
|
- ExitStatus status = parser.execute(args("start -d"));
|
|
|
- assertStatus(status, OK);
|
|
|
- assertThat(System.getProperty("es.foreground"), is("false"));
|
|
|
+ execute();
|
|
|
+ assertSystemProperty("es.foreground", null);
|
|
|
+ assertShouldRun(true);
|
|
|
}
|
|
|
|
|
|
- public void testThatNotDaemonizingDoesNotConfigureProperties() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
+ public void testDaemonize() throws Exception {
|
|
|
registerProperties("es.foreground");
|
|
|
|
|
|
- ExitStatus status = parser.execute(args("start"));
|
|
|
- assertStatus(status, OK);
|
|
|
- assertThat(System.getProperty("es.foreground"), is(nullValue()));
|
|
|
+ execute("-d");
|
|
|
+ assertSystemProperty("es.foreground", "false");
|
|
|
+ assertShouldRun(true);
|
|
|
+
|
|
|
+ System.clearProperty("es.foreground");
|
|
|
+ execute("--daemonize");
|
|
|
+ assertSystemProperty("es.foreground", "false");
|
|
|
+ assertShouldRun(true);
|
|
|
}
|
|
|
|
|
|
- public void testThatJavaPropertyStyleArgumentsCanBeParsed() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
+ public void testConfig() throws Exception {
|
|
|
registerProperties("es.foo", "es.spam");
|
|
|
|
|
|
- ExitStatus status = parser.execute(args("start -Dfoo=bar -Dspam=eggs"));
|
|
|
- assertStatus(status, OK);
|
|
|
+ execute("-Dfoo=bar", "-Dspam=eggs");
|
|
|
assertSystemProperty("es.foo", "bar");
|
|
|
assertSystemProperty("es.spam", "eggs");
|
|
|
+ assertShouldRun(true);
|
|
|
}
|
|
|
|
|
|
- public void testThatJavaPropertyStyleArgumentsWithEsPrefixAreNotPrefixedTwice() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- registerProperties("es.spam", "es.pidfile");
|
|
|
-
|
|
|
- ExitStatus status = parser.execute(args("start -Des.pidfile=/path/to/foo/elasticsearch/distribution/zip/target/integ-tests/es.pid -Dspam=eggs"));
|
|
|
- assertStatus(status, OK);
|
|
|
- assertThat(System.getProperty("es.es.pidfile"), is(nullValue()));
|
|
|
- assertSystemProperty("es.pidfile", "/path/to/foo/elasticsearch/distribution/zip/target/integ-tests/es.pid");
|
|
|
- assertSystemProperty("es.spam", "eggs");
|
|
|
- }
|
|
|
-
|
|
|
- public void testThatUnknownLongOptionsCanBeParsed() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- registerProperties("es.network.host", "es.my.option");
|
|
|
-
|
|
|
- ExitStatus status = parser.execute(args("start --network.host 127.0.0.1 --my.option=true"));
|
|
|
- assertStatus(status, OK);
|
|
|
- assertSystemProperty("es.network.host", "127.0.0.1");
|
|
|
- assertSystemProperty("es.my.option", "true");
|
|
|
- }
|
|
|
-
|
|
|
- public void testThatUnknownLongOptionsNeedAValue() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- registerProperties("es.network.host");
|
|
|
-
|
|
|
- ExitStatus status = parser.execute(args("start --network.host"));
|
|
|
- assertStatus(status, USAGE);
|
|
|
- String output = terminal.getOutput();
|
|
|
- assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
|
|
-
|
|
|
- terminal.resetOutput();
|
|
|
- status = parser.execute(args("start --network.host --foo"));
|
|
|
- assertStatus(status, USAGE);
|
|
|
- output = terminal.getOutput();
|
|
|
- assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
|
|
- }
|
|
|
-
|
|
|
- public void testParsingErrors() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
-
|
|
|
- // unknown params
|
|
|
- ExitStatus status = parser.execute(args("version --unknown-param /tmp/pid"));
|
|
|
- assertStatus(status, USAGE);
|
|
|
- String output = terminal.getOutput();
|
|
|
- assertTrue(output, output.contains("Unrecognized option: --unknown-param"));
|
|
|
-
|
|
|
- // single dash in extra params
|
|
|
- terminal.resetOutput();
|
|
|
- parser = new BootstrapCLIParser(terminal);
|
|
|
- status = parser.execute(args("start -network.host 127.0.0.1"));
|
|
|
- assertStatus(status, USAGE);
|
|
|
- output = terminal.getOutput();
|
|
|
- assertTrue(output, output.contains("Parameter [-network.host]does not start with --"));
|
|
|
-
|
|
|
- // never ended parameter
|
|
|
- terminal = new MockTerminal();
|
|
|
- parser = new BootstrapCLIParser(terminal);
|
|
|
- status = parser.execute(args("start --network.host"));
|
|
|
- assertStatus(status, USAGE);
|
|
|
- output = terminal.getOutput();
|
|
|
- assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
|
|
-
|
|
|
- // free floating value
|
|
|
- terminal = new MockTerminal();
|
|
|
- parser = new BootstrapCLIParser(terminal);
|
|
|
- status = parser.execute(args("start 127.0.0.1"));
|
|
|
- assertStatus(status, USAGE);
|
|
|
- output = terminal.getOutput();
|
|
|
- assertTrue(output, output.contains("Parameter [127.0.0.1]does not start with --"));
|
|
|
- }
|
|
|
-
|
|
|
- public void testHelpWorks() throws Exception {
|
|
|
- List<Tuple<String, String>> tuples = new ArrayList<>();
|
|
|
- tuples.add(new Tuple<>("version --help", "elasticsearch-version.help"));
|
|
|
- tuples.add(new Tuple<>("version -h", "elasticsearch-version.help"));
|
|
|
- tuples.add(new Tuple<>("start --help", "elasticsearch-start.help"));
|
|
|
- tuples.add(new Tuple<>("start -h", "elasticsearch-start.help"));
|
|
|
- tuples.add(new Tuple<>("--help", "elasticsearch.help"));
|
|
|
- tuples.add(new Tuple<>("-h", "elasticsearch.help"));
|
|
|
-
|
|
|
- for (Tuple<String, String> tuple : tuples) {
|
|
|
- terminal.resetOutput();
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- ExitStatus status = parser.execute(args(tuple.v1()));
|
|
|
- assertStatus(status, OK_AND_EXIT);
|
|
|
- assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/bootstrap/" + tuple.v2());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void testThatSpacesInParametersAreSupported() throws Exception {
|
|
|
- // emulates: bin/elasticsearch --node.name "'my node with spaces'" --pidfile "'/tmp/my pid.pid'"
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- registerProperties("es.pidfile", "es.my.param");
|
|
|
-
|
|
|
- ExitStatus status = parser.execute("start", "--pidfile", "foo with space", "--my.param", "my awesome neighbour");
|
|
|
- assertStatus(status, OK);
|
|
|
- assertSystemProperty("es.pidfile", "foo with space");
|
|
|
- assertSystemProperty("es.my.param", "my awesome neighbour");
|
|
|
-
|
|
|
+ public void testConfigMalformed() throws Exception {
|
|
|
+ UserError e = expectThrows(UserError.class, () -> {
|
|
|
+ execute("-Dfoo");
|
|
|
+ });
|
|
|
+ assertTrue(e.getMessage(), e.getMessage().contains("Malformed elasticsearch setting"));
|
|
|
}
|
|
|
|
|
|
- public void testThatHelpfulErrorMessageIsGivenWhenParametersAreOutOfOrder() throws Exception {
|
|
|
- BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
|
|
- UserError e = expectThrows(UserError.class, () -> {
|
|
|
- parser.parse("start", new String[]{"--foo=bar", "-Dbaz=qux"});
|
|
|
+ public void testUnknownOption() throws Exception {
|
|
|
+ OptionException e = expectThrows(OptionException.class, () -> {
|
|
|
+ execute("--network.host");
|
|
|
});
|
|
|
- assertThat(e.getMessage(), containsString("must be before any parameters starting with --"));
|
|
|
- assertNull(System.getProperty("es.foo"));
|
|
|
+ assertTrue(e.getMessage(), e.getMessage().contains("network.host is not a recognized option"));
|
|
|
}
|
|
|
|
|
|
private void registerProperties(String ... systemProperties) {
|
|
@@ -266,8 +161,4 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|
|
String msg = String.format(Locale.ROOT, "Expected property %s to be %s, terminal output was %s", name, expectedValue, terminal.getOutput());
|
|
|
assertThat(msg, System.getProperty(name), is(expectedValue));
|
|
|
}
|
|
|
-
|
|
|
- private void assertStatus(ExitStatus status, ExitStatus expectedStatus) throws Exception {
|
|
|
- assertThat(String.format(Locale.ROOT, "Expected status to be [%s], but was [%s], terminal output was %s", expectedStatus, status, terminal.getOutput()), status, is(expectedStatus));
|
|
|
- }
|
|
|
}
|