Class ESRestTestCase

java.lang.Object
org.junit.Assert
org.apache.lucene.util.LuceneTestCase
org.elasticsearch.test.ESTestCase
org.elasticsearch.test.rest.ESRestTestCase
Direct Known Subclasses:
AbstractFullClusterRestartTestCase, ESClientYamlSuiteTestCase, JsonLogsIntegTestCase

public abstract class ESRestTestCase
extends ESTestCase
Superclass for tests that interact with an external test cluster using Elasticsearch's RestClient.
  • Field Details

  • Constructor Details

  • Method Details

    • entityAsMap

      public static java.util.Map<java.lang.String,​java.lang.Object> entityAsMap​(org.elasticsearch.client.Response response) throws java.io.IOException
      Convert the entity from a Response into a map of maps.
      Throws:
      java.io.IOException
    • entityAsList

      public static java.util.List<java.lang.Object> entityAsList​(org.elasticsearch.client.Response response) throws java.io.IOException
      Convert the entity from a Response into a list of maps.
      Throws:
      java.io.IOException
    • hasXPack

      public static boolean hasXPack()
      Does any node in the cluster being tested have x-pack installed?
    • initClient

      public void initClient() throws java.io.IOException
      Throws:
      java.io.IOException
    • getTestRestCluster

      protected java.lang.String getTestRestCluster()
    • expectVersionSpecificWarnings

      public static org.elasticsearch.client.RequestOptions expectVersionSpecificWarnings​(java.util.function.Consumer<ESRestTestCase.VersionSensitiveWarningsHandler> expectationsSetter)
    • expectWarnings

      public static org.elasticsearch.client.RequestOptions expectWarnings​(java.lang.String... warnings)
      Creates request options designed to be used when making a call that can return warnings, for example a deprecated request. The options will ensure that the given warnings are returned if all nodes are on Version.CURRENT and will allow (but not require) the warnings if any node is running an older version.
      Parameters:
      warnings - The expected warnings.
    • allowTypesRemovalWarnings

      @Deprecated public static org.elasticsearch.client.RequestOptions allowTypesRemovalWarnings()
      Deprecated.
      this method is only required while we deprecate types and can be removed in 8.0
      Creates RequestOptions designed to ignore [types removal] warnings but nothing else
    • buildHttpHost

      protected org.apache.http.HttpHost buildHttpHost​(java.lang.String host, int port)
      Construct an HttpHost from the given host and port
    • cleanUpCluster

      public final void cleanUpCluster() throws java.lang.Exception
      Clean up after the test case.
      Throws:
      java.lang.Exception
    • closeClients

      public static void closeClients() throws java.io.IOException
      Throws:
      java.io.IOException
    • client

      protected static org.elasticsearch.client.RestClient client()
      Get the client used for ordinary api calls while writing a test
    • adminClient

      protected static org.elasticsearch.client.RestClient adminClient()
      Get the client used for test administrative actions. Do not use this while writing a test. Only use it for cleaning up after tests.
    • waitForPendingTasks

      public static void waitForPendingTasks​(org.elasticsearch.client.RestClient adminClient) throws java.lang.Exception
      Wait for outstanding tasks to complete. The specified admin client is used to check the outstanding tasks and this is done using ESTestCase.assertBusy(CheckedRunnable) to give a chance to any outstanding tasks to complete.
      Parameters:
      adminClient - the admin client
      Throws:
      java.lang.Exception - if an exception is thrown while checking the outstanding tasks
    • waitForPendingTasks

      public static void waitForPendingTasks​(org.elasticsearch.client.RestClient adminClient, java.util.function.Predicate<java.lang.String> taskFilter) throws java.lang.Exception
      Wait for outstanding tasks to complete. The specified admin client is used to check the outstanding tasks and this is done using ESTestCase.assertBusy(CheckedRunnable) to give a chance to any outstanding tasks to complete. The specified filter is used to filter out outstanding tasks that are expected to be there.
      Parameters:
      adminClient - the admin client
      taskFilter - predicate used to filter tasks that are expected to be there
      Throws:
      java.lang.Exception - if an exception is thrown while checking the outstanding tasks
    • preserveClusterUponCompletion

      protected boolean preserveClusterUponCompletion()
      Returns whether to preserve the state of the cluster upon completion of this test. Defaults to false. If true, overrides the value of preserveIndicesUponCompletion(), preserveTemplatesUponCompletion(), preserveReposUponCompletion(), preserveSnapshotsUponCompletion(),preserveRollupJobsUponCompletion(), and preserveILMPoliciesUponCompletion().
      Returns:
      true if the state of the cluster should be preserved
    • preserveIndicesUponCompletion

      protected boolean preserveIndicesUponCompletion()
      Returns whether to preserve the indices created during this test on completion of this test. Defaults to false. Override this method if indices should be preserved after the test, with the assumption that some other process or test will clean up the indices afterward. This is useful if the data directory and indices need to be preserved between test runs (for example, when testing rolling upgrades).
    • preserveTemplatesUponCompletion

      protected boolean preserveTemplatesUponCompletion()
      Controls whether or not to preserve templates upon completion of this test. The default implementation is to delete not preserve templates.
      Returns:
      whether or not to preserve templates
    • preserveDataStreamsUponCompletion

      protected boolean preserveDataStreamsUponCompletion()
      Determines if data streams are preserved upon completion of this test. The default implementation wipes data streams.
      Returns:
      whether or not to preserve data streams
    • preserveClusterSettings

      protected boolean preserveClusterSettings()
      Controls whether or not to preserve cluster settings upon completion of the test. The default implementation is to remove all cluster settings.
      Returns:
      true if cluster settings should be preserved and otherwise false
    • preserveReposUponCompletion

      protected boolean preserveReposUponCompletion()
      Returns whether to preserve the repositories on completion of this test. Defaults to not preserving repos. See also preserveSnapshotsUponCompletion().
    • preserveSnapshotsUponCompletion

      protected boolean preserveSnapshotsUponCompletion()
      Returns whether to preserve the snapshots in repositories on completion of this test. Defaults to not preserving snapshots. Only works for fs repositories.
    • preserveRollupJobsUponCompletion

      protected boolean preserveRollupJobsUponCompletion()
      Returns whether to preserve the rollup jobs of this test. Defaults to not preserving them. Only runs at all if xpack is installed on the cluster being tested.
    • preserveILMPoliciesUponCompletion

      protected boolean preserveILMPoliciesUponCompletion()
      Returns whether to preserve ILM Policies of this test. Defaults to not preserving them. Only runs at all if xpack is installed on the cluster being tested.
    • preserveSLMPoliciesUponCompletion

      protected boolean preserveSLMPoliciesUponCompletion()
      Returns whether to preserve SLM Policies of this test. Defaults to not preserving them. Only runs at all if xpack is installed on the cluster being tested.
    • preserveILMPolicyIds

      protected java.util.Set<java.lang.String> preserveILMPolicyIds()
      A set of ILM policies that should be preserved between runs.
    • preserveAutoFollowPatternsUponCompletion

      protected boolean preserveAutoFollowPatternsUponCompletion()
      Returns whether to preserve auto-follow patterns. Defaults to not preserving them. Only runs at all if xpack is installed on the cluster being tested.
    • waitForAllSnapshotsWiped

      protected boolean waitForAllSnapshotsWiped()
      Returns whether to wait to make absolutely certain that all snapshots have been deleted.
    • wipeAllIndices

      protected static void wipeAllIndices() throws java.io.IOException
      Throws:
      java.io.IOException
    • wipeDataStreams

      protected static void wipeDataStreams() throws java.io.IOException
      Throws:
      java.io.IOException
    • wipeSnapshots

      protected java.util.Map<java.lang.String,​java.util.List<java.util.Map<?,​?>>> wipeSnapshots() throws java.io.IOException
      Wipe fs snapshots we created one by one and all repositories so that the next test can create the repositories fresh and they'll start empty. There isn't an API to delete all snapshots. There is an API to delete all snapshot repositories but that leaves all of the snapshots intact in the repository.
      Returns:
      Map of repository name to list of snapshots found in unfinished state
      Throws:
      java.io.IOException
    • deleteRepository

      protected void deleteRepository​(java.lang.String repoName) throws java.io.IOException
      Throws:
      java.io.IOException
    • refreshAllIndices

      protected void refreshAllIndices() throws java.io.IOException
      Throws:
      java.io.IOException
    • restClientSettings

      protected org.elasticsearch.common.settings.Settings restClientSettings()
      Used to obtain settings for the REST client that is used to send REST requests.
    • restAdminSettings

      protected org.elasticsearch.common.settings.Settings restAdminSettings()
      Returns the REST client settings used for admin actions like cleaning up after the test has completed.
    • getClusterHosts

      protected final java.util.List<org.apache.http.HttpHost> getClusterHosts()
      Get the list of hosts in the cluster.
    • getProtocol

      protected java.lang.String getProtocol()
      Override this to switch to testing https.
    • buildClient

      protected org.elasticsearch.client.RestClient buildClient​(org.elasticsearch.common.settings.Settings settings, org.apache.http.HttpHost[] hosts) throws java.io.IOException
      Throws:
      java.io.IOException
    • configureClient

      protected static void configureClient​(org.elasticsearch.client.RestClientBuilder builder, org.elasticsearch.common.settings.Settings settings) throws java.io.IOException
      Throws:
      java.io.IOException
    • assertOK

      protected static void assertOK​(org.elasticsearch.client.Response response)
    • ensureGreen

      protected static void ensureGreen​(java.lang.String index) throws java.io.IOException
      checks that the specific index is green. we force a selection of an index as the tests share a cluster and often leave indices in an non green state
      Parameters:
      index - index to test for
      Throws:
      java.io.IOException
    • ensureHealth

      protected static void ensureHealth​(java.util.function.Consumer<org.elasticsearch.client.Request> requestConsumer) throws java.io.IOException
      Throws:
      java.io.IOException
    • ensureHealth

      protected static void ensureHealth​(java.lang.String index, java.util.function.Consumer<org.elasticsearch.client.Request> requestConsumer) throws java.io.IOException
      Throws:
      java.io.IOException
    • ensureHealth

      protected static void ensureHealth​(org.elasticsearch.client.RestClient client, java.lang.String index, java.util.function.Consumer<org.elasticsearch.client.Request> requestConsumer) throws java.io.IOException
      Throws:
      java.io.IOException
    • ensureNoInitializingShards

      protected static void ensureNoInitializingShards() throws java.io.IOException
      waits until all shard initialization is completed. This is a handy alternative to ensureGreen as it relates to all shards in the cluster and doesn't require to know how many nodes/replica there are.
      Throws:
      java.io.IOException
    • createIndex

      protected static void createIndex​(java.lang.String name, org.elasticsearch.common.settings.Settings settings) throws java.io.IOException
      Throws:
      java.io.IOException
    • createIndex

      protected static void createIndex​(java.lang.String name, org.elasticsearch.common.settings.Settings settings, java.lang.String mapping) throws java.io.IOException
      Throws:
      java.io.IOException
    • createIndex

      protected static void createIndex​(java.lang.String name, org.elasticsearch.common.settings.Settings settings, java.lang.String mapping, java.lang.String aliases) throws java.io.IOException
      Throws:
      java.io.IOException
    • deleteIndex

      protected static void deleteIndex​(java.lang.String name) throws java.io.IOException
      Throws:
      java.io.IOException
    • updateIndexSettings

      protected static void updateIndexSettings​(java.lang.String index, org.elasticsearch.common.settings.Settings.Builder settings) throws java.io.IOException
      Throws:
      java.io.IOException
    • expectSoftDeletesWarning

      protected static void expectSoftDeletesWarning​(org.elasticsearch.client.Request request, java.lang.String indexName)
    • expectTranslogRetentionWarning

      protected static void expectTranslogRetentionWarning​(org.elasticsearch.client.Request request)
    • getIndexSettings

      protected static java.util.Map<java.lang.String,​java.lang.Object> getIndexSettings​(java.lang.String index) throws java.io.IOException
      Throws:
      java.io.IOException
    • getIndexSettingsAsMap

      protected java.util.Map<java.lang.String,​java.lang.Object> getIndexSettingsAsMap​(java.lang.String index) throws java.io.IOException
      Throws:
      java.io.IOException
    • indexExists

      protected static boolean indexExists​(java.lang.String index) throws java.io.IOException
      Throws:
      java.io.IOException
    • closeIndex

      protected static void closeIndex​(java.lang.String index) throws java.io.IOException
      Throws:
      java.io.IOException
    • openIndex

      protected static void openIndex​(java.lang.String index) throws java.io.IOException
      Throws:
      java.io.IOException
    • aliasExists

      protected static boolean aliasExists​(java.lang.String alias) throws java.io.IOException
      Throws:
      java.io.IOException
    • aliasExists

      protected static boolean aliasExists​(java.lang.String index, java.lang.String alias) throws java.io.IOException
      Throws:
      java.io.IOException
    • getAlias

      protected static java.util.Map<java.lang.String,​java.lang.Object> getAlias​(java.lang.String index, java.lang.String alias) throws java.io.IOException
      Throws:
      java.io.IOException
    • getAsMap

      protected static java.util.Map<java.lang.String,​java.lang.Object> getAsMap​(java.lang.String endpoint) throws java.io.IOException
      Throws:
      java.io.IOException
    • isXPackTemplate

      protected static boolean isXPackTemplate​(java.lang.String name)
      Is this template one that is automatically created by xpack?
    • flush

      public void flush​(java.lang.String index, boolean force) throws java.io.IOException
      Throws:
      java.io.IOException
    • assertNoFileBasedRecovery

      public void assertNoFileBasedRecovery​(java.lang.String indexName, java.util.function.Predicate<java.lang.String> targetNode) throws java.io.IOException
      Asserts that replicas on nodes satisfying the targetNode should have perform operation-based recoveries.
      Throws:
      java.io.IOException
    • assertEmptyTranslog

      public void assertEmptyTranslog​(java.lang.String index) throws java.lang.Exception
      Asserts that we do not retain any extra translog for the given index (i.e., turn off the translog retention)
      Throws:
      java.lang.Exception
    • ensurePeerRecoveryRetentionLeasesRenewedAndSynced

      public void ensurePeerRecoveryRetentionLeasesRenewedAndSynced​(java.lang.String index) throws java.lang.Exception
      Peer recovery retention leases are renewed and synced to replicas periodically (every 30 seconds). This ensures that we have renewed every PRRL to the global checkpoint of the corresponding copy and properly synced to all copies.
      Throws:
      java.lang.Exception
    • getHasXPack

      public static java.lang.Boolean getHasXPack()
    • minimumNodeVersion

      protected static org.elasticsearch.Version minimumNodeVersion() throws java.io.IOException
      Returns the minimum node version among all nodes of the cluster
      Throws:
      java.io.IOException
    • performSyncedFlush

      protected static org.elasticsearch.client.Response performSyncedFlush​(java.lang.String indexName) throws java.io.IOException
      Throws:
      java.io.IOException
    • waitForActiveLicense

      protected static void waitForActiveLicense​(org.elasticsearch.client.RestClient restClient) throws java.lang.Exception
      Wait for the license to be applied and active. The specified admin client is used to check the license and this is done using ESTestCase.assertBusy(CheckedRunnable) to give some time to the License to be applied on nodes.
      Parameters:
      restClient - the client to use
      Throws:
      java.lang.Exception - if an exception is thrown while checking the status of the license
    • useIgnoreMultipleMatchingTemplatesWarningsHandler

      protected static void useIgnoreMultipleMatchingTemplatesWarningsHandler​(org.elasticsearch.client.Request request) throws java.io.IOException
      Throws:
      java.io.IOException