Search in sources :

Example 71 with Environment

use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.

the class OpenSearchCliTestCase method runTest.

void runTest(final int expectedStatus, final boolean expectedInit, final BiConsumer<String, String> outputConsumer, final InitConsumer initConsumer, final String... args) throws Exception {
    final MockTerminal terminal = new MockTerminal();
    final Path home = createTempDir();
    try {
        final AtomicBoolean init = new AtomicBoolean();
        final int status = OpenSearch.main(args, new OpenSearch() {

            @Override
            protected Environment createEnv(final Map<String, String> settings) throws UserException {
                Settings.Builder builder = Settings.builder().put("path.home", home);
                settings.forEach((k, v) -> builder.put(k, v));
                final Settings realSettings = builder.build();
                return new Environment(realSettings, home.resolve("config"));
            }

            @Override
            void init(final boolean daemonize, final Path pidFile, final boolean quiet, Environment initialEnv) {
                init.set(true);
                initConsumer.accept(!daemonize, pidFile, quiet, initialEnv);
            }

            @Override
            protected boolean addShutdownHook() {
                return false;
            }
        }, terminal);
        assertThat(status, equalTo(expectedStatus));
        assertThat(init.get(), equalTo(expectedInit));
        outputConsumer.accept(terminal.getOutput(), terminal.getErrorOutput());
    } catch (Exception e) {
        // if an unexpected exception is thrown, we log
        // terminal output to aid debugging
        logger.info("Stdout:\n" + terminal.getOutput());
        logger.info("Stderr:\n" + terminal.getErrorOutput());
        // rethrow so the test fails
        throw e;
    }
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) UserException(org.opensearch.cli.UserException) MockTerminal(org.opensearch.cli.MockTerminal) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Settings(org.opensearch.common.settings.Settings) Path(java.nio.file.Path) MockTerminal(org.opensearch.cli.MockTerminal) UserException(org.opensearch.cli.UserException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Environment(org.opensearch.env.Environment) UserException(org.opensearch.cli.UserException) Settings(org.opensearch.common.settings.Settings)

Example 72 with Environment

use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.

the class AnalysisTestsHelper method createTestAnalysisFromSettings.

public static OpenSearchTestCase.TestAnalysis createTestAnalysisFromSettings(final Settings settings, final Path configPath, final AnalysisPlugin... plugins) throws IOException {
    final Settings actualSettings;
    if (settings.get(IndexMetadata.SETTING_VERSION_CREATED) == null) {
        actualSettings = Settings.builder().put(settings).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
    } else {
        actualSettings = settings;
    }
    final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("test", actualSettings);
    final AnalysisRegistry analysisRegistry = new AnalysisModule(new Environment(actualSettings, configPath), Arrays.asList(plugins)).getAnalysisRegistry();
    return new OpenSearchTestCase.TestAnalysis(analysisRegistry.build(indexSettings), analysisRegistry.buildTokenFilterFactories(indexSettings), analysisRegistry.buildTokenizerFactories(indexSettings), analysisRegistry.buildCharFilterFactories(indexSettings));
}
Also used : IndexSettings(org.opensearch.index.IndexSettings) Environment(org.opensearch.env.Environment) AnalysisModule(org.opensearch.indices.analysis.AnalysisModule) IndexSettings(org.opensearch.index.IndexSettings) Settings(org.opensearch.common.settings.Settings)

Example 73 with Environment

use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.

the class OpenSearchTestCase method createTestAnalysis.

/**
 * Creates an TestAnalysis with all the default analyzers configured.
 */
public static TestAnalysis createTestAnalysis(IndexSettings indexSettings, Settings nodeSettings, AnalysisPlugin... analysisPlugins) throws IOException {
    Environment env = TestEnvironment.newEnvironment(nodeSettings);
    AnalysisModule analysisModule = new AnalysisModule(env, Arrays.asList(analysisPlugins));
    AnalysisRegistry analysisRegistry = analysisModule.getAnalysisRegistry();
    return new TestAnalysis(analysisRegistry.build(indexSettings), analysisRegistry.buildTokenFilterFactories(indexSettings), analysisRegistry.buildTokenizerFactories(indexSettings), analysisRegistry.buildCharFilterFactories(indexSettings));
}
Also used : AnalysisRegistry(org.opensearch.index.analysis.AnalysisRegistry) NodeEnvironment(org.opensearch.env.NodeEnvironment) Environment(org.opensearch.env.Environment) TestEnvironment(org.opensearch.env.TestEnvironment) AnalysisModule(org.opensearch.indices.analysis.AnalysisModule)

Example 74 with Environment

use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.

the class OpenSearchIntegTestCase method randomRepoPath.

/**
 * Returns path to a random directory that can be used to create a temporary file system repo
 */
public static Path randomRepoPath(Settings settings) {
    Environment environment = TestEnvironment.newEnvironment(settings);
    Path[] repoFiles = environment.repoFiles();
    assert repoFiles.length > 0;
    Path path;
    do {
        path = repoFiles[0].resolve(randomAlphaOfLength(10));
    } while (Files.exists(path));
    return path;
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) TestEnvironment(org.opensearch.env.TestEnvironment)

Example 75 with Environment

use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.

the class AbstractBootstrapCheckTestCase method createTestContext.

protected BootstrapContext createTestContext(Settings settings, Metadata metadata) {
    Path homePath = createTempDir();
    Environment environment = new Environment(settings(Version.CURRENT).put(settings).put(Environment.PATH_HOME_SETTING.getKey(), homePath.toString()).build(), null);
    return new BootstrapContext(environment, metadata);
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) BootstrapContext(org.opensearch.bootstrap.BootstrapContext)

Aggregations

Environment (org.opensearch.env.Environment)142 TestEnvironment (org.opensearch.env.TestEnvironment)98 Path (java.nio.file.Path)80 Settings (org.opensearch.common.settings.Settings)79 Matchers.containsString (org.hamcrest.Matchers.containsString)69 Matchers.hasToString (org.hamcrest.Matchers.hasToString)40 NodeEnvironment (org.opensearch.env.NodeEnvironment)32 IOException (java.io.IOException)27 UserException (org.opensearch.cli.UserException)23 ClusterState (org.opensearch.cluster.ClusterState)22 IndexSettings (org.opensearch.index.IndexSettings)22 MockTerminal (org.opensearch.cli.MockTerminal)19 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)19 OpenSearchException (org.opensearch.OpenSearchException)18 Map (java.util.Map)16 OptionSet (joptsimple.OptionSet)15 DiscoverySettings (org.opensearch.node.Node.DiscoverySettings)14 Version (org.opensearch.Version)13 Files (java.nio.file.Files)11 Arrays (java.util.Arrays)11