More tests

This commit is contained in:
Ryan Ernst 2016-03-08 17:27:53 -08:00
parent fdce9d7c4d
commit 73ebe36ed0
4 changed files with 96 additions and 99 deletions

View File

@ -19,14 +19,22 @@
package org.elasticsearch.bootstrap;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import org.apache.lucene.util.Constants;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.StringHelper;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.PidFile;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.inject.CreationException;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.LogConfigurator;
@ -39,13 +47,6 @@ import org.elasticsearch.monitor.process.ProcessProbe;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.internal.InternalSettingsPreparer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
/**
@ -217,10 +218,17 @@ final class Bootstrap {
* This method is invoked by {@link Elasticsearch#main(String[])}
* to startup elasticsearch.
*/
static void init() throws Throwable {
static void init(String[] args) throws Throwable {
// Set the system property before anything has a chance to trigger its use
initLoggerPrefix();
BootstrapCliParser parser = new BootstrapCliParser();
int status = parser.main(args, Terminal.DEFAULT);
if (parser.shouldRun() == false || status != ExitCodes.OK) {
exit(status);
}
INSTANCE = new Bootstrap();
boolean foreground = !"false".equals(System.getProperty("es.foreground", System.getProperty("es-foreground")));

View File

@ -21,8 +21,6 @@ package org.elasticsearch.bootstrap;
import java.io.IOException;
import org.elasticsearch.cli.Terminal;
/**
* This class starts elasticsearch.
*/
@ -35,15 +33,8 @@ public final class Elasticsearch {
* Main entry point for starting elasticsearch
*/
public static void main(String[] args) throws Exception {
BootstrapCliParser parser = new BootstrapCliParser();
parser.main(args, Terminal.DEFAULT);
if (parser.shouldRun() == false) {
return;
}
try {
Bootstrap.init();
Bootstrap.init(args);
} catch (Throwable t) {
// format exceptions to the console in a special way
// to avoid 2MB stacktraces from guice, etc.

View File

@ -19,10 +19,87 @@
package org.elasticsearch.cli;
import joptsimple.OptionSet;
import org.junit.Before;
public class MultiCommandTests extends CommandTestCase {
static class DummyMultiCommand extends MultiCommand {
DummyMultiCommand() {
super("A dummy multi command");
}
}
static class DummySubCommand extends Command {
DummySubCommand() {
super("A dummy subcommand");
}
@Override
protected void execute(Terminal terminal, OptionSet options) throws Exception {
terminal.println("Arguments: " + options.nonOptionArguments().toString());
}
}
DummyMultiCommand multiCommand;
@Before
public void setupCommand() {
multiCommand = new DummyMultiCommand();
}
@Override
protected Command newCommand() {
return null;
return multiCommand;
}
public void testNoCommandsConfigured() throws Exception {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
execute();
});
assertEquals("No subcommands configured", e.getMessage());
}
public void testUnknownCommand() throws Exception {
multiCommand.subcommands.put("something", new DummySubCommand());
UserError e = expectThrows(UserError.class, () -> {
execute("somethingelse");
});
assertEquals(ExitCodes.USAGE, e.exitCode);
assertEquals("Unknown command [somethingelse]", e.getMessage());
}
public void testMissingCommand() throws Exception {
multiCommand.subcommands.put("command1", new DummySubCommand());
UserError e = expectThrows(UserError.class, () -> {
execute();
});
assertEquals(ExitCodes.USAGE, e.exitCode);
assertEquals("Missing command", e.getMessage());
}
public void testHelp() throws Exception {
multiCommand.subcommands.put("command1", new DummySubCommand());
multiCommand.subcommands.put("command2", new DummySubCommand());
execute("-h");
String output = terminal.getOutput();
assertTrue(output, output.contains("command1"));
assertTrue(output, output.contains("command2"));
}
public void testSubcommandHelp() throws Exception {
multiCommand.subcommands.put("command1", new DummySubCommand());
multiCommand.subcommands.put("command2", new DummySubCommand());
execute("command2", "-h");
String output = terminal.getOutput();
assertFalse(output, output.contains("command1"));
assertTrue(output, output.contains("A dummy subcommand"));
}
public void testSubcommandArguments() throws Exception {
multiCommand.subcommands.put("command1", new DummySubCommand());
execute("command1", "foo", "bar");
String output = terminal.getOutput();
assertFalse(output, output.contains("command1"));
assertTrue(output, output.contains("Arguments: [foo, bar]"));
}
}

View File

@ -1,79 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.plugins;
import java.io.IOException;
import java.nio.file.Path;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.common.cli.CliToolTestCase;
import org.elasticsearch.cli.MockTerminal;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.junit.Before;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
public class PluginCliTests extends CommandTestCase {
// the home dir for each test to use
Path homeDir;
// settings used to create an Environment for tools
Settings.Builder settingsBuilder;
@Before
public void setupHome() {
homeDir = createTempDir();
settingsBuilder = Settings.builder()
.put("path.home", homeDir);
}
@Override
protected Command newCommand() {
return new PluginCli(new Environment(settingsBuilder.build()));
}
public void testHelpWorks() throws Exception {
MockTerminal terminal = new MockTerminal();
/* nocommit
assertThat(new PluginCli(terminal).execute(args("--help")), is(OK_AND_EXIT));
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin.help");
terminal.resetOutput();
assertThat(new PluginCli(terminal).execute(args("install -h")), is(OK_AND_EXIT));
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-install.help");
for (String plugin : InstallPluginCommand.OFFICIAL_PLUGINS) {
assertThat(terminal.getOutput(), containsString(plugin));
}
terminal.resetOutput();
assertThat(new PluginCli(terminal).execute(args("remove --help")), is(OK_AND_EXIT));
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-remove.help");
terminal.resetOutput();
assertThat(new PluginCli(terminal).execute(args("list -h")), is(OK_AND_EXIT));
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-list.help");
*/
}
}