Search in sources :

Example 31 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class EvilLoggerConfigurationTests method testMissingConfigFile.

public void testMissingConfigFile() {
    final Path configDir = getDataPath("does_not_exist");
    final Settings settings = Settings.builder().put(Environment.PATH_CONF_SETTING.getKey(), configDir.toAbsolutePath()).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    final Environment environment = new Environment(settings);
    UserException e = expectThrows(UserException.class, () -> LogConfigurator.configure(environment));
    assertThat(e, hasToString(containsString("no log4j2.properties found; tried")));
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) UserException(org.elasticsearch.cli.UserException) Settings(org.elasticsearch.common.settings.Settings)

Example 32 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class EvilLoggerTests method testPrefixLogger.

public void testPrefixLogger() throws IOException, IllegalAccessException, UserException {
    setupLogging("prefix");
    final String prefix = randomBoolean() ? null : randomAsciiOfLength(16);
    final Logger logger = Loggers.getLogger("prefix", prefix);
    logger.info("test");
    logger.info("{}", "test");
    final Exception e = new Exception("exception");
    logger.info(new ParameterizedMessage("{}", "test"), e);
    final String path = System.getProperty("es.logs.base_path") + System.getProperty("file.separator") + System.getProperty("es.logs.cluster_name") + ".log";
    final List<String> events = Files.readAllLines(PathUtils.get(path));
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    final int stackTraceLength = sw.toString().split(System.getProperty("line.separator")).length;
    final int expectedLogLines = 3;
    assertThat(events.size(), equalTo(expectedLogLines + stackTraceLength));
    for (int i = 0; i < expectedLogLines; i++) {
        if (prefix == null) {
            assertThat(events.get(i), startsWith("test"));
        } else {
            assertThat(events.get(i), startsWith("[" + prefix + "] test"));
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Logger(org.apache.logging.log4j.Logger) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) UserException(org.elasticsearch.cli.UserException) PrintWriter(java.io.PrintWriter)

Example 33 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testConfigNotDir.

public void testConfigNotDir() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    Path configDir = pluginDir.resolve("config");
    Files.createFile(configDir);
    String pluginZip = createPlugin("fake", pluginDir);
    UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains("not a directory"));
    assertInstallCleaned(env.v2());
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.elasticsearch.cli.UserException)

Example 34 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testConfigContainsDir.

public void testConfigContainsDir() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    Path dirInConfigDir = pluginDir.resolve("config").resolve("foo");
    Files.createDirectories(dirInConfigDir);
    Files.createFile(dirInConfigDir.resolve("myconfig.yml"));
    String pluginZip = createPlugin("fake", pluginDir);
    UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains("Directories not allowed in config dir for plugin"));
    assertInstallCleaned(env.v2());
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.elasticsearch.cli.UserException)

Example 35 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testExistingPlugin.

public void testExistingPlugin() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    String pluginZip = createPlugin("fake", pluginDir);
    installPlugin(pluginZip, env.v1());
    UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains("already exists"));
    assertInstallCleaned(env.v2());
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.elasticsearch.cli.UserException)

Aggregations

UserException (org.elasticsearch.cli.UserException)39 Path (java.nio.file.Path)24 Environment (org.elasticsearch.env.Environment)14 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IOException (java.io.IOException)5 Settings (org.elasticsearch.common.settings.Settings)4 BufferedReader (java.io.BufferedReader)3 ArrayList (java.util.ArrayList)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 FileVisitOption (java.nio.file.FileVisitOption)2 FileVisitResult (java.nio.file.FileVisitResult)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 ZipInputStream (java.util.zip.ZipInputStream)2 Logger (org.apache.logging.log4j.Logger)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)2 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)2 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)2 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)2