use of joptsimple.OptionSet in project whirr by apache.
the class ClusterSpecCommandTest method testOverrides.
@Test
public void testOverrides() throws Exception {
ClusterSpecCommand clusterSpecCommand = new ClusterSpecCommand("name", "description", new ServiceFactory()) {
@Override
public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
return 0;
}
};
OptionSet optionSet = clusterSpecCommand.parser.parse("--service-name", "overridden-test-service", "--config", "whirr-override-test.properties");
ClusterSpec clusterSpec = clusterSpecCommand.getClusterSpec(optionSet);
assertThat(clusterSpec.getServiceName(), is("overridden-test-service"));
assertThat(clusterSpec.getClusterName(), is("test-cluster"));
}
use of joptsimple.OptionSet in project whirr by apache.
the class DestroyClusterCommand method run.
@Override
public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
OptionSet optionSet = parser.parse(args.toArray(new String[0]));
if (!optionSet.nonOptionArguments().isEmpty()) {
printUsage(parser, err);
return -1;
}
try {
ClusterSpec clusterSpec = getClusterSpec(optionSet);
Service service = factory.create(clusterSpec.getServiceName());
service.destroyCluster(clusterSpec);
return 0;
} catch (IllegalArgumentException e) {
err.println(e.getMessage());
printUsage(parser, err);
return -1;
}
}
use of joptsimple.OptionSet in project elasticsearch by elastic.
the class EvilCommandTests method testCommandShutdownHook.
public void testCommandShutdownHook() throws Exception {
final AtomicBoolean closed = new AtomicBoolean();
final boolean shouldThrow = randomBoolean();
final Command command = new Command("test-command-shutdown-hook") {
@Override
protected void execute(Terminal terminal, OptionSet options) throws Exception {
}
@Override
public void close() throws IOException {
closed.set(true);
if (shouldThrow) {
throw new IOException("fail");
}
}
};
final MockTerminal terminal = new MockTerminal();
command.main(new String[0], terminal);
assertNotNull(command.shutdownHookThread.get());
// successful removal here asserts that the runtime hook was installed in Command#main
assertTrue(Runtime.getRuntime().removeShutdownHook(command.shutdownHookThread.get()));
command.shutdownHookThread.get().run();
command.shutdownHookThread.get().join();
assertTrue(closed.get());
final String output = terminal.getOutput();
if (shouldThrow) {
// ensure that we dump the exception
assertThat(output, containsString("java.io.IOException: fail"));
// ensure that we dump the stack trace too
assertThat(output, containsString("\tat org.elasticsearch.cli.EvilCommandTests$1.close"));
} else {
assertThat(output, isEmptyString());
}
}
use of joptsimple.OptionSet in project elasticsearch by elastic.
the class TruncateTranslogIT method testCorruptTranslogTruncation.
public void testCorruptTranslogTruncation() throws Exception {
internalCluster().startNodes(1, Settings.EMPTY);
assertAcked(prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put("index.refresh_interval", "-1").put(MockEngineSupport.DISABLE_FLUSH_ON_CLOSE.getKey(), // never flush - always recover from translog
true)));
ensureYellow();
// Index some documents
int numDocs = scaledRandomIntBetween(100, 1000);
IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
for (int i = 0; i < builders.length; i++) {
builders[i] = client().prepareIndex("test", "type").setSource("foo", "bar");
}
disableTranslogFlush("test");
indexRandom(false, false, false, Arrays.asList(builders));
Set<Path> translogDirs = getTranslogDirs("test");
TruncateTranslogCommand ttc = new TruncateTranslogCommand();
MockTerminal t = new MockTerminal();
OptionParser parser = ttc.getParser();
for (Path translogDir : translogDirs) {
OptionSet options = parser.parse("-d", translogDir.toAbsolutePath().toString(), "-b");
// Try running it before the shard is closed, it should flip out because it can't acquire the lock
try {
logger.info("--> running truncate while index is open on [{}]", translogDir.toAbsolutePath());
ttc.execute(t, options, null);
fail("expected the truncate command to fail not being able to acquire the lock");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("Failed to lock shard's directory"));
}
}
// Corrupt the translog file(s)
logger.info("--> corrupting translog");
corruptRandomTranslogFiles("test");
// Restart the single node
logger.info("--> restarting node");
internalCluster().fullRestart();
client().admin().cluster().prepareHealth().setWaitForYellowStatus().setTimeout(new TimeValue(1000, TimeUnit.MILLISECONDS)).setWaitForEvents(Priority.LANGUID).get();
try {
client().prepareSearch("test").setQuery(matchAllQuery()).get();
fail("all shards should be failed due to a corrupted translog");
} catch (SearchPhaseExecutionException e) {
// Good, all shards should be failed because there is only a
// single shard and its translog is corrupt
}
// Close the index so we can actually truncate the translog
logger.info("--> closing 'test' index");
client().admin().indices().prepareClose("test").get();
for (Path translogDir : translogDirs) {
final Path idxLocation = translogDir.getParent().resolve("index");
assertBusy(() -> {
logger.info("--> checking that lock has been released for {}", idxLocation);
try (Directory dir = FSDirectory.open(idxLocation, NativeFSLockFactory.INSTANCE);
Lock writeLock = dir.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
// Great, do nothing, we just wanted to obtain the lock
} catch (LockObtainFailedException lofe) {
logger.info("--> failed acquiring lock for {}", idxLocation);
fail("still waiting for lock release at [" + idxLocation + "]");
} catch (IOException ioe) {
fail("Got an IOException: " + ioe);
}
});
OptionSet options = parser.parse("-d", translogDir.toAbsolutePath().toString(), "-b");
logger.info("--> running truncate translog command for [{}]", translogDir.toAbsolutePath());
ttc.execute(t, options, null);
logger.info("--> output:\n{}", t.getOutput());
}
// Re-open index
logger.info("--> opening 'test' index");
client().admin().indices().prepareOpen("test").get();
ensureYellow("test");
// Run a search and make sure it succeeds
SearchResponse resp = client().prepareSearch("test").setQuery(matchAllQuery()).get();
ElasticsearchAssertions.assertNoFailures(resp);
}
use of joptsimple.OptionSet in project PayFile by mikehearn.
the class Main method main.
public static void main(String[] args) throws Exception {
// allow client to choose another network for testing by passing through an argument.
OptionParser parser = new OptionParser();
parser.accepts("network").withRequiredArg().withValuesConvertedBy(regex("(mainnet)|(testnet)|(regtest)")).defaultsTo("mainnet");
parser.accepts("help").forHelp();
parser.formatHelpWith(new BuiltinHelpFormatter(120, 10));
OptionSet options;
try {
options = parser.parse(args);
} catch (OptionException e) {
System.err.println(e.getMessage());
System.err.println("");
parser.printHelpOn(System.err);
return;
}
if (options.has("help")) {
parser.printHelpOn(System.out);
return;
}
if (options.valueOf("network").equals(("testnet"))) {
params = TestNet3Params.get();
filePrefix = "testnet-";
} else if (options.valueOf("network").equals(("mainnet"))) {
params = MainNetParams.get();
filePrefix = "";
} else if (options.valueOf("network").equals(("regtest"))) {
params = RegTestParams.get();
filePrefix = "regtest-";
}
launch(args);
}
Aggregations