Search in sources :

Example 31 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class AddFileKeyStoreCommandTests method testFileDNE.

public void testFileDNE() throws Exception {
    String password = "keystorepassword";
    createKeystore(password);
    terminal.addSecretInput(password);
    UserException e = expectThrows(UserException.class, () -> execute("foo", "path/dne"));
    assertEquals(ExitCodes.IO_ERROR, e.exitCode);
    assertThat(e.getMessage(), containsString("File [path/dne] does not exist"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.opensearch.cli.UserException)

Example 32 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class AddFileKeyStoreCommandTests method testExtraArguments.

public void testExtraArguments() throws Exception {
    String password = "keystorepassword";
    createKeystore(password);
    Path file = createRandomFile();
    terminal.addSecretInput(password);
    UserException e = expectThrows(UserException.class, () -> execute("foo", file.toString(), "bar"));
    assertEquals(e.getMessage(), ExitCodes.USAGE, e.exitCode);
    assertThat(e.getMessage(), containsString("settings and filenames must come in pairs"));
}
Also used : Path(java.nio.file.Path) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.opensearch.cli.UserException)

Example 33 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class AddFileKeyStoreCommandTests method testMissingSettingName.

public void testMissingSettingName() throws Exception {
    String password = "keystorepassword";
    createKeystore(password);
    terminal.addSecretInput(password);
    UserException e = expectThrows(UserException.class, this::execute);
    assertEquals(ExitCodes.USAGE, e.exitCode);
    assertThat(e.getMessage(), containsString("Missing setting name"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.opensearch.cli.UserException)

Example 34 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class AddFileKeyStoreCommand method executeCommand.

@Override
protected void executeCommand(Terminal terminal, OptionSet options, Environment env) throws Exception {
    final List<String> argumentValues = arguments.values(options);
    if (argumentValues.size() == 0) {
        throw new UserException(ExitCodes.USAGE, "Missing setting name");
    }
    if (argumentValues.size() % 2 != 0) {
        throw new UserException(ExitCodes.USAGE, "settings and filenames must come in pairs");
    }
    final KeyStoreWrapper keyStore = getKeyStore();
    for (int i = 0; i < argumentValues.size(); i += 2) {
        final String setting = argumentValues.get(i);
        if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
            if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
                terminal.println("Exiting without modifying keystore.");
                return;
            }
        }
        final Path file = getPath(argumentValues.get(i + 1));
        if (Files.exists(file) == false) {
            throw new UserException(ExitCodes.IO_ERROR, "File [" + file.toString() + "] does not exist");
        }
        keyStore.setFile(setting, Files.readAllBytes(file));
    }
    keyStore.save(env.configDir(), getKeyStorePassword().getChars());
}
Also used : Path(java.nio.file.Path) UserException(org.opensearch.cli.UserException)

Example 35 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class RemoveCustomsCommandIT method testCustomDoesNotMatch.

public void testCustomDoesNotMatch() throws Exception {
    internalCluster().setBootstrapClusterManagerNodeIndex(0);
    String node = internalCluster().startNode();
    createIndex("test");
    client().admin().indices().prepareDelete("test").get();
    assertEquals(1, client().admin().cluster().prepareState().get().getState().metadata().indexGraveyard().getTombstones().size());
    Settings dataPathSettings = internalCluster().dataPathSettings(node);
    ensureStableCluster(1);
    internalCluster().stopRandomDataNode();
    Environment environment = TestEnvironment.newEnvironment(Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build());
    UserException ex = expectThrows(UserException.class, () -> removeCustoms(environment, false, new String[] { "index-greveyard-with-typos" }));
    assertThat(ex.getMessage(), containsString("No custom metadata matching [index-greveyard-with-typos] were " + "found on this node"));
}
Also used : Environment(org.opensearch.env.Environment) TestEnvironment(org.opensearch.env.TestEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.opensearch.cli.UserException) Settings(org.opensearch.common.settings.Settings)

Aggregations

UserException (org.opensearch.cli.UserException)76 Path (java.nio.file.Path)44 Matchers.containsString (org.hamcrest.Matchers.containsString)38 Environment (org.opensearch.env.Environment)29 Matchers.hasToString (org.hamcrest.Matchers.hasToString)25 TestEnvironment (org.opensearch.env.TestEnvironment)25 IOException (java.io.IOException)16 Settings (org.opensearch.common.settings.Settings)16 ArrayList (java.util.ArrayList)12 BufferedReader (java.io.BufferedReader)11 Files (java.nio.file.Files)11 MockTerminal (org.opensearch.cli.MockTerminal)11 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)11 InputStream (java.io.InputStream)10 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 Terminal (org.opensearch.cli.Terminal)10 Tuple (org.opensearch.common.collect.Tuple)10 StringReader (java.io.StringReader)9 URL (java.net.URL)9