use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.
the class OverrideNodeVersionCommandTests method testOverwritesIfTooNew.
public void testOverwritesIfTooNew() throws Exception {
final Version nodeVersion = NodeMetadataTests.tooNewVersion();
PersistedClusterStateService.overrideVersion(nodeVersion, nodePaths);
final MockTerminal mockTerminal = new MockTerminal();
mockTerminal.addTextInput(randomFrom("y", "Y"));
new OverrideNodeVersionCommand().processNodePaths(mockTerminal, nodePaths, 0, noOptions, environment);
assertThat(mockTerminal.getOutput(), allOf(containsString("data loss"), containsString("You should not use this tool"), containsString(Version.CURRENT.toString()), containsString(nodeVersion.toString()), containsString(OverrideNodeVersionCommand.SUCCESS_MESSAGE)));
expectThrows(IllegalStateException.class, () -> mockTerminal.readText(""));
final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(nodePaths);
assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT));
}
use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.
the class RemoveCorruptedShardDataCommandTests method testShardLock.
public void testShardLock() throws Exception {
indexDocs(indexShard, true);
final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
final MockTerminal t = new MockTerminal();
final OptionParser parser = command.getParser();
// Try running it before the shard is closed, it should flip out because it can't acquire the lock
try {
final OptionSet options = parser.parse("-d", indexPath.toString());
command.execute(t, options, environment);
fail("expected the command to fail not being able to acquire the lock");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("Failed to lock shard's directory"));
}
// close shard
closeShards(indexShard);
// Try running it before the shard is corrupted
try {
final OptionSet options = parser.parse("-d", indexPath.toString());
command.execute(t, options, environment);
fail("expected the command to fail not being able to find a corrupt file marker");
} catch (OpenSearchException e) {
assertThat(e.getMessage(), startsWith("Shard does not seem to be corrupted at"));
assertThat(t.getOutput(), containsString("Lucene index is clean at"));
}
}
use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.
the class RemoveCorruptedShardDataCommandTests method testCorruptedIndex.
public void testCorruptedIndex() throws Exception {
final int numDocs = indexDocs(indexShard, true);
// close shard
closeShards(indexShard);
final boolean corruptSegments = randomBoolean();
CorruptionUtils.corruptIndex(random(), indexPath, corruptSegments);
if (randomBoolean()) {
// test corrupted shard and add corruption marker
final IndexShard corruptedShard = reopenIndexShard(true);
allowShardFailures();
expectThrows(IndexShardRecoveryException.class, () -> newStartedShard(p -> corruptedShard, true));
closeShards(corruptedShard);
}
final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
final MockTerminal t = new MockTerminal();
final OptionParser parser = command.getParser();
// run command with dry-run
// mean dry run
t.addTextInput("n");
final OptionSet options = parser.parse("-d", indexPath.toString());
t.setVerbosity(Terminal.Verbosity.VERBOSE);
try {
command.execute(t, options, environment);
fail();
} catch (OpenSearchException e) {
if (corruptSegments) {
assertThat(e.getMessage(), either(is("Index is unrecoverable")).or(startsWith("unable to list commits")));
} else {
assertThat(e.getMessage(), containsString("aborted by user"));
}
} finally {
logger.info("--> output:\n{}", t.getOutput());
}
if (corruptSegments == false) {
// run command without dry-run
t.addTextInput("y");
command.execute(t, options, environment);
final String output = t.getOutput();
logger.info("--> output:\n{}", output);
// reopen shard
failOnShardFailures();
final IndexShard newShard = newStartedShard(p -> reopenIndexShard(false), true);
final Set<String> shardDocUIDs = getShardDocUIDs(newShard);
final Matcher matcher = NUM_CORRUPT_DOCS_PATTERN.matcher(output);
assertThat(matcher.find(), equalTo(true));
final int expectedNumDocs = numDocs - Integer.parseInt(matcher.group("docs"));
assertThat(shardDocUIDs.size(), equalTo(expectedNumDocs));
closeShards(newShard);
}
}
use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.
the class RemoveCorruptedShardDataCommandTests method testCorruptedTranslog.
public void testCorruptedTranslog() throws Exception {
final int numDocsToKeep = indexDocs(indexShard, false);
// close shard
closeShards(indexShard);
TestTranslog.corruptRandomTranslogFile(logger, random(), translogPath);
// test corrupted shard
final IndexShard corruptedShard = reopenIndexShard(true);
allowShardFailures();
// it has to fail on start up due to index.shard.check_on_startup = checksum
final Exception exception = expectThrows(Exception.class, () -> newStartedShard(p -> corruptedShard, true));
final Throwable cause = exception.getCause() instanceof EngineException ? exception.getCause().getCause() : exception.getCause();
assertThat(cause, instanceOf(TranslogCorruptedException.class));
// translog is corrupted already - do not check consistency
closeShard(corruptedShard, false);
final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
final MockTerminal t = new MockTerminal();
final OptionParser parser = command.getParser();
final OptionSet options = parser.parse("-d", translogPath.toString());
// run command with dry-run
// mean dry run
t.addTextInput("n");
t.setVerbosity(Terminal.Verbosity.VERBOSE);
try {
command.execute(t, options, environment);
fail();
} catch (OpenSearchException e) {
assertThat(e.getMessage(), containsString("aborted by user"));
assertThat(t.getOutput(), containsString("Continue and remove corrupted data from the shard ?"));
}
logger.info("--> output:\n{}", t.getOutput());
// run command without dry-run
t.reset();
t.addTextInput("y");
command.execute(t, options, environment);
final String output = t.getOutput();
logger.info("--> output:\n{}", output);
// reopen shard
failOnShardFailures();
final IndexShard newShard = newStartedShard(p -> reopenIndexShard(false), true);
final Set<String> shardDocUIDs = getShardDocUIDs(newShard);
assertThat(shardDocUIDs.size(), equalTo(numDocsToKeep));
closeShards(newShard);
}
use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.
the class RemoveCorruptedShardDataCommandTests method testTruncatesCleanTranslogIfRequested.
public void testTruncatesCleanTranslogIfRequested() throws Exception {
indexDocs(indexShard, true);
closeShards(indexShard);
final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
final MockTerminal t = new MockTerminal();
final OptionParser parser = command.getParser();
final OptionSet options = parser.parse("-d", translogPath.toString(), "--" + TRUNCATE_CLEAN_TRANSLOG_FLAG);
t.addTextInput("y");
t.setVerbosity(Terminal.Verbosity.VERBOSE);
command.execute(t, options, environment);
assertThat(t.getOutput(), containsString("Lucene index is clean"));
assertThat(t.getOutput(), containsString("Translog was not analysed and will be truncated"));
assertThat(t.getOutput(), containsString("Creating new empty translog"));
}
Aggregations