Merge branch 'master' into cli-parsing
This commit is contained in:
commit
7a49cd1287
|
@ -24,7 +24,6 @@ import java.io.Console;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.elasticsearch.common.SuppressForbidden;
|
||||
|
@ -54,6 +53,13 @@ public abstract class Terminal {
|
|||
/** The current verbosity for the terminal, defaulting to {@link Verbosity#NORMAL}. */
|
||||
private Verbosity verbosity = Verbosity.NORMAL;
|
||||
|
||||
/** The newline used when calling println. */
|
||||
private final String lineSeparator;
|
||||
|
||||
protected Terminal(String lineSeparator) {
|
||||
this.lineSeparator = lineSeparator;
|
||||
}
|
||||
|
||||
/** Sets the verbosity of the terminal. */
|
||||
public void setVerbosity(Verbosity verbosity) {
|
||||
this.verbosity = verbosity;
|
||||
|
@ -68,9 +74,6 @@ public abstract class Terminal {
|
|||
/** Returns a Writer which can be used to write to the terminal directly. */
|
||||
public abstract PrintWriter getWriter();
|
||||
|
||||
/** Print a message directly to the terminal. */
|
||||
protected abstract void doPrint(String msg);
|
||||
|
||||
/** Prints a line to the terminal at {@link Verbosity#NORMAL} verbosity level. */
|
||||
public final void println(String msg) {
|
||||
println(Verbosity.NORMAL, msg);
|
||||
|
@ -79,7 +82,8 @@ public abstract class Terminal {
|
|||
/** Prints a line to the terminal at {@code verbosity} level. */
|
||||
public final void println(Verbosity verbosity, String msg) {
|
||||
if (this.verbosity.ordinal() >= verbosity.ordinal()) {
|
||||
doPrint(msg + System.lineSeparator());
|
||||
getWriter().print(msg + lineSeparator);
|
||||
getWriter().flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,6 +91,10 @@ public abstract class Terminal {
|
|||
|
||||
private static final Console console = System.console();
|
||||
|
||||
ConsoleTerminal() {
|
||||
super(System.lineSeparator());
|
||||
}
|
||||
|
||||
static boolean isSupported() {
|
||||
return console != null;
|
||||
}
|
||||
|
@ -96,12 +104,6 @@ public abstract class Terminal {
|
|||
return console.writer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doPrint(String msg) {
|
||||
console.printf("%s", msg);
|
||||
console.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readText(String prompt) {
|
||||
return console.readLine("%s", prompt);
|
||||
|
@ -115,13 +117,15 @@ public abstract class Terminal {
|
|||
|
||||
private static class SystemTerminal extends Terminal {
|
||||
|
||||
private static final PrintWriter writer = new PrintWriter(System.out);
|
||||
private static final PrintWriter writer = newWriter();
|
||||
|
||||
@Override
|
||||
@SuppressForbidden(reason = "System#out")
|
||||
public void doPrint(String msg) {
|
||||
System.out.print(msg);
|
||||
System.out.flush();
|
||||
SystemTerminal() {
|
||||
super(System.lineSeparator());
|
||||
}
|
||||
|
||||
@SuppressForbidden(reason = "Writer for System.out")
|
||||
private static PrintWriter newWriter() {
|
||||
return new PrintWriter(System.out);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -131,7 +135,7 @@ public abstract class Terminal {
|
|||
|
||||
@Override
|
||||
public String readText(String text) {
|
||||
doPrint(text);
|
||||
getWriter().print(text);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
|
||||
try {
|
||||
return reader.readLine();
|
||||
|
|
|
@ -39,6 +39,7 @@ import org.elasticsearch.common.inject.Inject;
|
|||
import org.elasticsearch.common.settings.Setting;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
|
||||
import org.elasticsearch.discovery.Discovery;
|
||||
import org.elasticsearch.env.NodeEnvironment;
|
||||
import org.elasticsearch.rest.RestStatus;
|
||||
|
@ -206,7 +207,20 @@ public class GatewayService extends AbstractLifecycleComponent<GatewayService> i
|
|||
}
|
||||
} else {
|
||||
if (recovered.compareAndSet(false, true)) {
|
||||
threadPool.generic().execute(() -> gateway.performStateRecovery(recoveryListener));
|
||||
threadPool.generic().execute(new AbstractRunnable() {
|
||||
@Override
|
||||
public void onFailure(Throwable t) {
|
||||
logger.warn("Recovery failed", t);
|
||||
// we reset `recovered` in the listener don't reset it here otherwise there might be a race
|
||||
// that resets it to false while a new recover is already running?
|
||||
recoveryListener.onFailure("state recovery failed: " + t.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRun() throws Exception {
|
||||
gateway.performStateRecovery(recoveryListener);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -132,10 +132,11 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc
|
|||
public static class TypeParser implements Mapper.TypeParser {
|
||||
@Override
|
||||
public Mapper.Builder parse(String fieldName, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
|
||||
if (parserContext.indexVersionCreated().onOrAfter(Version.V_5_0_0)) {
|
||||
// TODO: temporarily disabled to give Kibana time to upgrade to text/keyword mappings
|
||||
/*if (parserContext.indexVersionCreated().onOrAfter(Version.V_5_0_0)) {
|
||||
throw new IllegalArgumentException("The [string] type is removed in 5.0. You should now use either a [text] "
|
||||
+ "or [keyword] field instead for field [" + fieldName + "]");
|
||||
}
|
||||
}*/
|
||||
StringFieldMapper.Builder builder = new StringFieldMapper.Builder(fieldName);
|
||||
// hack for the fact that string can't just accept true/false for
|
||||
// the index property and still accepts no/not_analyzed/analyzed
|
||||
|
@ -240,10 +241,11 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc
|
|||
int positionIncrementGap, int ignoreAbove,
|
||||
Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
|
||||
super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo);
|
||||
if (Version.indexCreated(indexSettings).onOrAfter(Version.V_5_0_0)) {
|
||||
// TODO: temporarily disabled to give Kibana time to upgrade to text/keyword mappings
|
||||
/*if (Version.indexCreated(indexSettings).onOrAfter(Version.V_5_0_0)) {
|
||||
throw new IllegalArgumentException("The [string] type is removed in 5.0. You should now use either a [text] "
|
||||
+ "or [keyword] field instead for field [" + fieldType.name() + "]");
|
||||
}
|
||||
}*/
|
||||
if (fieldType.tokenized() && fieldType.indexOptions() != NONE && fieldType().hasDocValues()) {
|
||||
throw new MapperParsingException("Field [" + fieldType.name() + "] cannot be analyzed and have doc values");
|
||||
}
|
||||
|
|
|
@ -317,18 +317,15 @@ public class Node implements Closeable {
|
|||
discovery.start();
|
||||
transportService.acceptIncomingRequests();
|
||||
discovery.startInitialJoin();
|
||||
|
||||
// tribe nodes don't have a master so we shouldn't register an observer
|
||||
if (DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.get(settings).millis() > 0) {
|
||||
final ThreadPool thread = injector.getInstance(ThreadPool.class);
|
||||
ClusterStateObserver observer = new ClusterStateObserver(clusterService, null, logger, thread.getThreadContext());
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
if (observer.observedState().nodes().masterNodeId() == null) {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
observer.waitForNextChange(new ClusterStateObserver.Listener() {
|
||||
@Override
|
||||
public void onNewClusterState(ClusterState state) {
|
||||
latch.countDown();
|
||||
}
|
||||
public void onNewClusterState(ClusterState state) { latch.countDown(); }
|
||||
|
||||
@Override
|
||||
public void onClusterServiceClose() {
|
||||
|
@ -337,18 +334,19 @@ public class Node implements Closeable {
|
|||
|
||||
@Override
|
||||
public void onTimeout(TimeValue timeout) {
|
||||
assert false;
|
||||
}
|
||||
// use null timeout as we use timeout on the latchwait
|
||||
}, MasterNodeChangePredicate.INSTANCE, null);
|
||||
logger.warn("timed out while waiting for initial discovery state - timeout: {}",
|
||||
DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.get(settings));
|
||||
latch.countDown();
|
||||
}
|
||||
}, MasterNodeChangePredicate.INSTANCE, DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.get(settings));
|
||||
|
||||
try {
|
||||
latch.await(DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.get(settings).millis(), TimeUnit.MILLISECONDS);
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
throw new ElasticsearchTimeoutException("Interrupted while waiting for initial discovery state");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.getAsBoolean("http.enabled", true)) {
|
||||
injector.getInstance(HttpServer.class).start();
|
||||
|
|
|
@ -1,58 +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.cluster.allocation;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterModule;
|
||||
import org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator;
|
||||
import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
||||
import static org.elasticsearch.test.ESIntegTestCase.Scope;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
|
||||
@ClusterScope(scope= Scope.TEST, numDataNodes =0)
|
||||
public class ShardsAllocatorModuleIT extends ESIntegTestCase {
|
||||
|
||||
public void testLoadDefaultShardsAllocator() throws IOException {
|
||||
assertAllocatorInstance(Settings.Builder.EMPTY_SETTINGS, BalancedShardsAllocator.class);
|
||||
}
|
||||
|
||||
public void testLoadByShortKeyShardsAllocator() throws IOException {
|
||||
Settings build = settingsBuilder().put(ClusterModule.SHARDS_ALLOCATOR_TYPE_SETTING.getKey(), "even_shard") // legacy just to make sure we don't barf
|
||||
.build();
|
||||
assertAllocatorInstance(build, BalancedShardsAllocator.class);
|
||||
build = settingsBuilder().put(ClusterModule.SHARDS_ALLOCATOR_TYPE_SETTING.getKey(), ClusterModule.BALANCED_ALLOCATOR).build();
|
||||
assertAllocatorInstance(build, BalancedShardsAllocator.class);
|
||||
}
|
||||
|
||||
private void assertAllocatorInstance(Settings settings, Class<? extends ShardsAllocator> clazz) throws IOException {
|
||||
while (cluster().size() != 0) {
|
||||
internalCluster().stopRandomDataNode();
|
||||
}
|
||||
internalCluster().startNode(settings);
|
||||
ShardsAllocator instance = internalCluster().getInstance(ShardsAllocator.class);
|
||||
assertThat(instance, instanceOf(clazz));
|
||||
}
|
||||
}
|
|
@ -19,41 +19,41 @@
|
|||
|
||||
package org.elasticsearch.common.cli;
|
||||
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
public class TerminalTests extends CliToolTestCase {
|
||||
public void testVerbosity() throws Exception {
|
||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal(Terminal.Verbosity.SILENT);
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
terminal.setVerbosity(Terminal.Verbosity.SILENT);
|
||||
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
||||
assertNotPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
||||
assertNotPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
||||
|
||||
terminal = new CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
||||
terminal = new MockTerminal();
|
||||
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
||||
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
||||
assertNotPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
||||
|
||||
terminal = new CaptureOutputTerminal(Terminal.Verbosity.VERBOSE);
|
||||
terminal = new MockTerminal();
|
||||
terminal.setVerbosity(Terminal.Verbosity.VERBOSE);
|
||||
assertPrinted(terminal, Terminal.Verbosity.SILENT, "text");
|
||||
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "text");
|
||||
assertPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
|
||||
}
|
||||
|
||||
public void testEscaping() throws Exception {
|
||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
assertPrinted(terminal, Terminal.Verbosity.NORMAL, "This message contains percent like %20n");
|
||||
}
|
||||
|
||||
private void assertPrinted(CaptureOutputTerminal logTerminal, Terminal.Verbosity verbosity, String text) {
|
||||
private void assertPrinted(MockTerminal logTerminal, Terminal.Verbosity verbosity, String text) throws Exception {
|
||||
logTerminal.println(verbosity, text);
|
||||
assertEquals(1, logTerminal.getTerminalOutput().size());
|
||||
assertTrue(logTerminal.getTerminalOutput().get(0).contains(text));
|
||||
logTerminal.terminalOutput.clear();
|
||||
String output = logTerminal.getOutput();
|
||||
assertTrue(output, output.contains(text));
|
||||
logTerminal.resetOutput();
|
||||
}
|
||||
|
||||
private void assertNotPrinted(CaptureOutputTerminal logTerminal, Terminal.Verbosity verbosity, String text) {
|
||||
private void assertNotPrinted(MockTerminal logTerminal, Terminal.Verbosity verbosity, String text) throws Exception {
|
||||
logTerminal.println(verbosity, text);
|
||||
assertThat(logTerminal.getTerminalOutput(), hasSize(0));
|
||||
String output = logTerminal.getOutput();
|
||||
assertTrue(output, output.isEmpty());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,21 +19,21 @@
|
|||
|
||||
package org.elasticsearch.common.logging;
|
||||
|
||||
import org.apache.log4j.Appender;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.node.internal.InternalSettingsPreparer;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.log4j.Appender;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.node.internal.InternalSettingsPreparer;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.junit.Before;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
@ -162,7 +162,7 @@ public class LoggingConfigurationTests extends ESTestCase {
|
|||
.put("appender.console.type", "console")
|
||||
.put("appender.console.layout.type", "consolePattern")
|
||||
.put("appender.console.layout.conversionPattern", "[%d{ISO8601}][%-5p][%-25c] %m%n")
|
||||
.build(), new CliToolTestCase.MockTerminal());
|
||||
.build(), new MockTerminal());
|
||||
LogConfigurator.configure(environment.settings(), true);
|
||||
// args should overwrite whatever is in the config
|
||||
ESLogger esLogger = ESLoggerFactory.getLogger("test_resolve_order");
|
||||
|
@ -187,7 +187,7 @@ public class LoggingConfigurationTests extends ESTestCase {
|
|||
Settings.builder()
|
||||
.put(Environment.PATH_CONF_SETTING.getKey(), tmpDir.toAbsolutePath())
|
||||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
|
||||
.build(), new CliToolTestCase.MockTerminal());
|
||||
.build(), new MockTerminal());
|
||||
LogConfigurator.configure(environment.settings(), false);
|
||||
ESLogger esLogger = ESLoggerFactory.getLogger("test_config_not_read");
|
||||
|
||||
|
|
|
@ -19,9 +19,13 @@
|
|||
|
||||
package org.elasticsearch.node.internal;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.env.Environment;
|
||||
|
@ -29,17 +33,9 @@ import org.elasticsearch.test.ESTestCase;
|
|||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class InternalSettingsPreparerTests extends ESTestCase {
|
||||
|
||||
|
@ -81,17 +77,9 @@ public class InternalSettingsPreparerTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testReplacePromptPlaceholders() {
|
||||
final Terminal terminal = new CliToolTestCase.MockTerminal() {
|
||||
@Override
|
||||
public char[] readSecret(String message) {
|
||||
return "replaced".toCharArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readText(String message) {
|
||||
return "text";
|
||||
}
|
||||
};
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
terminal.addTextInput("text");
|
||||
terminal.addSecretInput("replaced");
|
||||
|
||||
Settings.Builder builder = settingsBuilder()
|
||||
.put(baseEnvSettings)
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.elasticsearch.plugins;
|
||||
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
|
||||
import static org.elasticsearch.common.cli.CliTool.ExitStatus.OK_AND_EXIT;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
@ -28,23 +29,23 @@ import static org.hamcrest.Matchers.is;
|
|||
|
||||
public class PluginCliTests extends CliToolTestCase {
|
||||
public void testHelpWorks() throws Exception {
|
||||
/*
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal();
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
/* nocommit
|
||||
assertThat(new PluginCli(terminal).execute(args("--help")), is(OK_AND_EXIT));
|
||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin.help");
|
||||
|
||||
terminal.getTerminalOutput().clear();
|
||||
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.getTerminalOutput(), hasItem(containsString(plugin)));
|
||||
assertThat(terminal.getOutput(), containsString(plugin));
|
||||
}
|
||||
|
||||
terminal.getTerminalOutput().clear();
|
||||
terminal.resetOutput();
|
||||
assertThat(new PluginCli(terminal).execute(args("remove --help")), is(OK_AND_EXIT));
|
||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-remove.help");
|
||||
|
||||
terminal.getTerminalOutput().clear();
|
||||
terminal.resetOutput();
|
||||
assertThat(new PluginCli(terminal).execute(args("list -h")), is(OK_AND_EXIT));
|
||||
assertTerminalOutputContainsHelpFile(terminal, "/org/elasticsearch/plugins/plugin-list.help");
|
||||
*/
|
||||
|
|
|
@ -25,9 +25,9 @@ import org.elasticsearch.common.SuppressForbidden;
|
|||
import org.elasticsearch.common.cli.CliTool.ExitStatus;
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.cli.UserError;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.common.collect.Tuple;
|
||||
import org.elasticsearch.monitor.jvm.JvmInfo;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
|
@ -42,14 +42,13 @@ 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 static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@SuppressForbidden(reason = "modifies system properties intentionally")
|
||||
public class BootstrapCliParserTests extends CliToolTestCase {
|
||||
|
||||
private CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
||||
private MockTerminal terminal = new MockTerminal();
|
||||
private List<String> propertiesToClear = new ArrayList<>();
|
||||
private Map<Object, Object> properties;
|
||||
|
||||
|
@ -72,10 +71,11 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|||
ExitStatus status = parser.execute(args("version"));
|
||||
assertStatus(status, OK_AND_EXIT);
|
||||
|
||||
assertThatTerminalOutput(containsString(Version.CURRENT.toString()));
|
||||
assertThatTerminalOutput(containsString(Build.CURRENT.shortHash()));
|
||||
assertThatTerminalOutput(containsString(Build.CURRENT.date()));
|
||||
assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version()));
|
||||
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()));
|
||||
}
|
||||
|
||||
public void testThatVersionIsReturnedAsStartParameter() throws Exception {
|
||||
|
@ -83,20 +83,22 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|||
ExitStatus status = parser.execute(args("start -V"));
|
||||
assertStatus(status, OK_AND_EXIT);
|
||||
|
||||
assertThatTerminalOutput(containsString(Version.CURRENT.toString()));
|
||||
assertThatTerminalOutput(containsString(Build.CURRENT.shortHash()));
|
||||
assertThatTerminalOutput(containsString(Build.CURRENT.date()));
|
||||
assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version()));
|
||||
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()));
|
||||
|
||||
CaptureOutputTerminal terminal = new CaptureOutputTerminal();
|
||||
terminal.resetOutput();
|
||||
parser = new BootstrapCLIParser(terminal);
|
||||
status = parser.execute(args("start --version"));
|
||||
assertStatus(status, OK_AND_EXIT);
|
||||
|
||||
assertThatTerminalOutput(containsString(Version.CURRENT.toString()));
|
||||
assertThatTerminalOutput(containsString(Build.CURRENT.shortHash()));
|
||||
assertThatTerminalOutput(containsString(Build.CURRENT.date()));
|
||||
assertThatTerminalOutput(containsString(JvmInfo.jvmInfo().version()));
|
||||
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()));
|
||||
}
|
||||
|
||||
public void testThatPidFileCanBeConfigured() throws Exception {
|
||||
|
@ -172,11 +174,14 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|||
|
||||
ExitStatus status = parser.execute(args("start --network.host"));
|
||||
assertStatus(status, USAGE);
|
||||
assertThatTerminalOutput(containsString("Parameter [network.host] needs value"));
|
||||
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);
|
||||
assertThatTerminalOutput(containsString("Parameter [network.host] needs value"));
|
||||
output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
||||
}
|
||||
|
||||
public void testParsingErrors() throws Exception {
|
||||
|
@ -185,28 +190,32 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|||
// unknown params
|
||||
ExitStatus status = parser.execute(args("version --unknown-param /tmp/pid"));
|
||||
assertStatus(status, USAGE);
|
||||
assertThatTerminalOutput(containsString("Unrecognized option: --unknown-param"));
|
||||
String output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("Unrecognized option: --unknown-param"));
|
||||
|
||||
// single dash in extra params
|
||||
terminal = new CaptureOutputTerminal();
|
||||
terminal.resetOutput();
|
||||
parser = new BootstrapCLIParser(terminal);
|
||||
status = parser.execute(args("start -network.host 127.0.0.1"));
|
||||
assertStatus(status, USAGE);
|
||||
assertThatTerminalOutput(containsString("Parameter [-network.host]does not start with --"));
|
||||
output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("Parameter [-network.host]does not start with --"));
|
||||
|
||||
// never ended parameter
|
||||
terminal = new CaptureOutputTerminal();
|
||||
terminal = new MockTerminal();
|
||||
parser = new BootstrapCLIParser(terminal);
|
||||
status = parser.execute(args("start --network.host"));
|
||||
assertStatus(status, USAGE);
|
||||
assertThatTerminalOutput(containsString("Parameter [network.host] needs value"));
|
||||
output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("Parameter [network.host] needs value"));
|
||||
|
||||
// free floating value
|
||||
terminal = new CaptureOutputTerminal();
|
||||
terminal = new MockTerminal();
|
||||
parser = new BootstrapCLIParser(terminal);
|
||||
status = parser.execute(args("start 127.0.0.1"));
|
||||
assertStatus(status, USAGE);
|
||||
assertThatTerminalOutput(containsString("Parameter [127.0.0.1]does not start with --"));
|
||||
output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("Parameter [127.0.0.1]does not start with --"));
|
||||
}
|
||||
|
||||
public void testHelpWorks() throws Exception {
|
||||
|
@ -219,7 +228,7 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|||
tuples.add(new Tuple<>("-h", "elasticsearch.help"));
|
||||
|
||||
for (Tuple<String, String> tuple : tuples) {
|
||||
terminal = new CaptureOutputTerminal();
|
||||
terminal.resetOutput();
|
||||
BootstrapCLIParser parser = new BootstrapCLIParser(terminal);
|
||||
ExitStatus status = parser.execute(args(tuple.v1()));
|
||||
assertStatus(status, OK_AND_EXIT);
|
||||
|
@ -252,16 +261,12 @@ public class BootstrapCliParserTests extends CliToolTestCase {
|
|||
propertiesToClear.addAll(Arrays.asList(systemProperties));
|
||||
}
|
||||
|
||||
private void assertSystemProperty(String name, String expectedValue) {
|
||||
String msg = String.format(Locale.ROOT, "Expected property %s to be %s, terminal output was %s", name, expectedValue, terminal.getTerminalOutput());
|
||||
private void assertSystemProperty(String name, String expectedValue) throws Exception {
|
||||
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) {
|
||||
assertThat(String.format(Locale.ROOT, "Expected status to be [%s], but was [%s], terminal output was %s", expectedStatus, status, terminal.getTerminalOutput()), status, is(expectedStatus));
|
||||
}
|
||||
|
||||
private void assertThatTerminalOutput(Matcher<String> matcher) {
|
||||
assertThat(terminal.getTerminalOutput(), hasItem(matcher));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ import org.elasticsearch.Version;
|
|||
import org.elasticsearch.cli.ExitCodes;
|
||||
import org.elasticsearch.common.cli.CliTool;
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.cli.UserError;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
|
@ -118,8 +119,8 @@ public class InstallPluginCommandTests extends ESTestCase {
|
|||
return writeZip(structure, "elasticsearch");
|
||||
}
|
||||
|
||||
static CliToolTestCase.CaptureOutputTerminal installPlugin(String pluginUrl, Environment env) throws Exception {
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
||||
static MockTerminal installPlugin(String pluginUrl, Environment env) throws Exception {
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
new InstallPluginCommand(env).execute(terminal, pluginUrl, true);
|
||||
return terminal;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.apache.lucene.util.LuceneTestCase;
|
|||
import org.elasticsearch.cli.ExitCodes;
|
||||
import org.elasticsearch.common.cli.CliTool;
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
|
@ -46,8 +47,8 @@ public class ListPluginsCommandTests extends ESTestCase {
|
|||
return new Environment(settings);
|
||||
}
|
||||
|
||||
static CliToolTestCase.CaptureOutputTerminal listPlugins(Environment env) throws Exception {
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
|
||||
static MockTerminal listPlugins(Environment env) throws Exception {
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
String[] args = {};
|
||||
int status = new ListPluginsCommand(env).main(args, terminal);
|
||||
assertEquals(ExitCodes.OK, status);
|
||||
|
@ -64,29 +65,24 @@ public class ListPluginsCommandTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testNoPlugins() throws Exception {
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = listPlugins(createEnv());
|
||||
List<String> lines = terminal.getTerminalOutput();
|
||||
assertEquals(0, lines.size());
|
||||
MockTerminal terminal = listPlugins(createEnv());
|
||||
assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty());
|
||||
}
|
||||
|
||||
public void testOnePlugin() throws Exception {
|
||||
Environment env = createEnv();
|
||||
Files.createDirectory(env.pluginsFile().resolve("fake"));
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = listPlugins(env);
|
||||
List<String> lines = terminal.getTerminalOutput();
|
||||
assertEquals(1, lines.size());
|
||||
assertTrue(lines.get(0).contains("fake"));
|
||||
MockTerminal terminal = listPlugins(env);
|
||||
assertTrue(terminal.getOutput(), terminal.getOutput().contains("fake"));
|
||||
}
|
||||
|
||||
public void testTwoPlugins() throws Exception {
|
||||
Environment env = createEnv();
|
||||
Files.createDirectory(env.pluginsFile().resolve("fake1"));
|
||||
Files.createDirectory(env.pluginsFile().resolve("fake2"));
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = listPlugins(env);
|
||||
List<String> lines = terminal.getTerminalOutput();
|
||||
assertEquals(2, lines.size());
|
||||
Collections.sort(lines);
|
||||
assertTrue(lines.get(0).contains("fake1"));
|
||||
assertTrue(lines.get(1).contains("fake2"));
|
||||
MockTerminal terminal = listPlugins(env);
|
||||
String output = terminal.getOutput();
|
||||
assertTrue(output, output.contains("fake1"));
|
||||
assertTrue(output, output.contains("fake2"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.nio.file.Path;
|
|||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.elasticsearch.cli.UserError;
|
||||
import org.elasticsearch.common.cli.CliToolTestCase;
|
||||
import org.elasticsearch.common.cli.MockTerminal;
|
||||
import org.elasticsearch.common.cli.Terminal;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
|
@ -47,8 +48,8 @@ public class RemovePluginCommandTests extends ESTestCase {
|
|||
return new Environment(settings);
|
||||
}
|
||||
|
||||
static CliToolTestCase.CaptureOutputTerminal removePlugin(String name, Environment env) throws Exception {
|
||||
CliToolTestCase.CaptureOutputTerminal terminal = new CliToolTestCase.CaptureOutputTerminal(Terminal.Verbosity.VERBOSE);
|
||||
static MockTerminal removePlugin(String name, Environment env) throws Exception {
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
new RemovePluginCommand(env).execute(terminal, name);
|
||||
return terminal;
|
||||
}
|
||||
|
|
|
@ -20,9 +20,6 @@
|
|||
package org.elasticsearch.common.cli;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.SuppressForbidden;
|
||||
|
@ -31,10 +28,6 @@ import org.elasticsearch.test.StreamsUtils;
|
|||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
public abstract class CliToolTestCase extends ESTestCase {
|
||||
|
||||
@Before
|
||||
|
@ -56,68 +49,10 @@ public abstract class CliToolTestCase extends ESTestCase {
|
|||
return command.split("\\s+");
|
||||
}
|
||||
|
||||
/**
|
||||
* A terminal implementation that discards everything
|
||||
*/
|
||||
public static class MockTerminal extends Terminal {
|
||||
|
||||
@Override
|
||||
protected void doPrint(String msg) {}
|
||||
|
||||
@Override
|
||||
public String readText(String prompt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char[] readSecret(String prompt) {
|
||||
return new char[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintWriter getWriter() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A terminal implementation that captures everything written to it
|
||||
*/
|
||||
public static class CaptureOutputTerminal extends MockTerminal {
|
||||
|
||||
List<String> terminalOutput = new ArrayList<>();
|
||||
|
||||
public CaptureOutputTerminal() {
|
||||
this(Verbosity.NORMAL);
|
||||
}
|
||||
|
||||
public CaptureOutputTerminal(Verbosity verbosity) {
|
||||
setVerbosity(verbosity);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPrint(String msg) {
|
||||
terminalOutput.add(msg);
|
||||
}
|
||||
|
||||
public List<String> getTerminalOutput() {
|
||||
return terminalOutput;
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertTerminalOutputContainsHelpFile(CliToolTestCase.CaptureOutputTerminal terminal, String classPath) throws IOException {
|
||||
List<String> nonEmptyLines = new ArrayList<>();
|
||||
for (String line : terminal.getTerminalOutput()) {
|
||||
String originalPrintedLine = line.replaceAll(System.lineSeparator(), "");
|
||||
if (Strings.isNullOrEmpty(originalPrintedLine)) {
|
||||
nonEmptyLines.add(originalPrintedLine);
|
||||
}
|
||||
}
|
||||
assertThat(nonEmptyLines, hasSize(greaterThan(0)));
|
||||
|
||||
public static void assertTerminalOutputContainsHelpFile(MockTerminal terminal, String classPath) throws IOException {
|
||||
String output = terminal.getOutput();
|
||||
assertFalse(output, output.isEmpty());
|
||||
String expectedDocs = StreamsUtils.copyToStringFromClasspath(classPath);
|
||||
for (String nonEmptyLine : nonEmptyLines) {
|
||||
assertThat(expectedDocs, containsString(nonEmptyLine.replaceAll(System.lineSeparator(), "")));
|
||||
}
|
||||
assertTrue(output, output.contains(expectedDocs));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* 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.common.cli;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
/**
|
||||
* A terminal for tests which captures all output, and
|
||||
* can be plugged with fake input.
|
||||
*/
|
||||
public class MockTerminal extends Terminal {
|
||||
|
||||
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
private final PrintWriter writer = new PrintWriter(new OutputStreamWriter(buffer, StandardCharsets.UTF_8));
|
||||
private final Deque<String> textInput = new ArrayDeque<>();
|
||||
private final Deque<String> secretInput = new ArrayDeque<>();
|
||||
|
||||
public MockTerminal() {
|
||||
super("\n"); // always *nix newlines for tests
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readText(String prompt) {
|
||||
if (textInput.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return textInput.removeFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public char[] readSecret(String prompt) {
|
||||
if (secretInput.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return secretInput.removeFirst().toCharArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
/** Adds an an input that will be return from {@link #readText(String)}. Values are read in FIFO order. */
|
||||
public void addTextInput(String input) {
|
||||
textInput.addLast(input);
|
||||
}
|
||||
|
||||
/** Adds an an input that will be return from {@link #readText(String)}. Values are read in FIFO order. */
|
||||
public void addSecretInput(String input) {
|
||||
secretInput.addLast(input);
|
||||
}
|
||||
|
||||
/** Returns all output written to this terminal. */
|
||||
public String getOutput() throws UnsupportedEncodingException {
|
||||
return buffer.toString("UTF-8");
|
||||
}
|
||||
|
||||
/** Wipes the output. */
|
||||
public void resetOutput() {
|
||||
buffer.reset();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue