Class ESRestTestCase

java.lang.Object
org.junit.Assert
org.apache.lucene.tests.util.LuceneTestCase
org.elasticsearch.test.ESTestCase
org.elasticsearch.test.rest.ESRestTestCase
Direct Known Subclasses:
AbstractFullClusterRestartTestCase, 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

    • ESRestTestCase

      public ESRestTestCase()
  • Method Details

    • entityAsMap

      public static Map<String,Object> entityAsMap(Response response) throws IOException
      Convert the entity from a Response into a map of maps.
      Throws:
      IOException
    • entityAsList

      public static List<Object> entityAsList(Response response) throws IOException
      Convert the entity from a Response into a list of maps.
      Throws:
      IOException
    • hasXPack

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

      public void initClient() throws IOException
      Throws:
      IOException
    • getTestRestCluster

      protected String getTestRestCluster()
    • getTestReadinessPorts

      protected String getTestReadinessPorts()
    • expectVersionSpecificWarnings

      public static RequestOptions expectVersionSpecificWarnings(Consumer<ESRestTestCase.VersionSensitiveWarningsHandler> expectationsSetter)
    • expectWarnings

      public static RequestOptions expectWarnings(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.
    • basicAuthHeaderValue

      public static String basicAuthHeaderValue(String username, SecureString passwd)
      Construct a Basic auth header
      Parameters:
      username - user name
      passwd - user password
    • buildHttpHost

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

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

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

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

      protected static 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(RestClient restClient) throws 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:
      restClient - the admin client
      Throws:
      Exception - if an exception is thrown while checking the outstanding tasks
    • waitForPendingTasks

      public static void waitForPendingTasks(RestClient restClient, Predicate<String> taskFilter) throws 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:
      restClient - the admin client
      taskFilter - predicate used to filter tasks that are expected to be there
      Throws:
      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.
    • preserveILMPolicyIds

      protected Set<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.
    • 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.
    • preserveSearchableSnapshotsIndicesUponCompletion

      protected boolean preserveSearchableSnapshotsIndicesUponCompletion()
      Returns whether to preserve searchable snapshots indices. 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.
    • deleteAllNodeShutdownMetadata

      protected void deleteAllNodeShutdownMetadata() throws IOException
      If any nodes are registered for shutdown, removes their metadata.
      Throws:
      IOException
    • wipeAllIndices

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

      protected static void wipeDataStreams() throws IOException
      Throws:
      IOException
    • wipeSearchableSnapshotsIndices

      protected void wipeSearchableSnapshotsIndices() throws IOException
      Throws:
      IOException
    • wipeSnapshots

      protected Map<String,List<Map<?,?>>> wipeSnapshots() throws 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:
      IOException
    • deleteRepository

      protected void deleteRepository(String repoName) throws IOException
      Throws:
      IOException
    • refreshAllIndices

      protected void refreshAllIndices() throws IOException
      Throws:
      IOException
    • refresh

      protected static RefreshResponse refresh(String index) throws IOException
      Throws:
      IOException
    • refresh

      protected static RefreshResponse refresh(RestClient client, String index) throws IOException
      Throws:
      IOException
    • restClientSettings

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

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

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

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

      protected RestClient buildClient(Settings settings, org.apache.http.HttpHost[] hosts) throws IOException
      Throws:
      IOException
    • configureClient

      protected static void configureClient(RestClientBuilder builder, Settings settings) throws IOException
      Throws:
      IOException
    • assertOK

      public static void assertOK(Response response)
    • assertDocCount

      public static void assertDocCount(RestClient client, String indexName, long docCount) throws IOException
      Assert that the index in question has the given number of documents present
      Throws:
      IOException
    • assertAcknowledged

      public static void assertAcknowledged(Response response) throws IOException
      Throws:
      IOException
    • updateClusterSettings

      public static void updateClusterSettings(Settings settings) throws IOException
      Updates the cluster with the provided settings (as persistent settings)
      Throws:
      IOException
    • updateClusterSettings

      public static void updateClusterSettings(RestClient client, Settings settings) throws IOException
      Updates the cluster with the provided settings (as persistent settings)
      Throws:
      IOException
    • getEnsureGreenTimeout

      @Nullable protected String getEnsureGreenTimeout()
      Permits subclasses to increase the default timeout when waiting for green health
    • ensureGreen

      public final void ensureGreen(String index) throws 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:
      IOException
    • ensureHealth

      protected static void ensureHealth(Consumer<Request> requestConsumer) throws IOException
      Throws:
      IOException
    • ensureHealth

      public static void ensureHealth(String index, Consumer<Request> requestConsumer) throws IOException
      Throws:
      IOException
    • ensureHealth

      public static void ensureHealth(RestClient restClient, Consumer<Request> requestConsumer) throws IOException
      Throws:
      IOException
    • ensureHealth

      protected static void ensureHealth(RestClient restClient, String index, Consumer<Request> requestConsumer) throws IOException
      Throws:
      IOException
    • ensureNoInitializingShards

      protected static void ensureNoInitializingShards() throws 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:
      IOException
    • createIndex

      protected static CreateIndexResponse createIndex(String name) throws IOException
      Throws:
      IOException
    • createIndex

      protected static CreateIndexResponse createIndex(String name, Settings settings) throws IOException
      Throws:
      IOException
    • createIndex

      protected static CreateIndexResponse createIndex(RestClient client, String name, Settings settings) throws IOException
      Throws:
      IOException
    • createIndex

      protected static CreateIndexResponse createIndex(String name, Settings settings, String mapping) throws IOException
      Throws:
      IOException
    • createIndex

      protected static CreateIndexResponse createIndex(String name, Settings settings, String mapping, String aliases) throws IOException
      Throws:
      IOException
    • createIndex

      public static CreateIndexResponse createIndex(RestClient client, String name, Settings settings, String mapping, String aliases) throws IOException
      Throws:
      IOException
    • deleteIndex

      protected static AcknowledgedResponse deleteIndex(String name) throws IOException
      Throws:
      IOException
    • deleteIndex

      protected static AcknowledgedResponse deleteIndex(RestClient restClient, String name) throws IOException
      Throws:
      IOException
    • updateIndexSettings

      protected static void updateIndexSettings(String index, Settings.Builder settings) throws IOException
      Throws:
      IOException
    • expectSoftDeletesWarning

      protected static void expectSoftDeletesWarning(Request request, String indexName)
    • getIndexSettings

      protected static Map<String,Object> getIndexSettings(String index) throws IOException
      Throws:
      IOException
    • getIndexSettingsAsMap

      protected Map<String,Object> getIndexSettingsAsMap(String index) throws IOException
      Throws:
      IOException
    • getIndexMapping

      protected static Map<String,Object> getIndexMapping(String index) throws IOException
      Throws:
      IOException
    • getIndexMappingAsMap

      protected Map<String,Object> getIndexMappingAsMap(String index) throws IOException
      Throws:
      IOException
    • indexExists

      protected static boolean indexExists(String index) throws IOException
      Throws:
      IOException
    • closeIndex

      protected static void closeIndex(String index) throws IOException
      Throws:
      IOException
    • openIndex

      protected static void openIndex(String index) throws IOException
      Throws:
      IOException
    • aliasExists

      protected static boolean aliasExists(String alias) throws IOException
      Throws:
      IOException
    • aliasExists

      protected static boolean aliasExists(String index, String alias) throws IOException
      Throws:
      IOException
    • getAlias

      protected static Map<String,Object> getAlias(String index, String alias) throws IOException
      Throws:
      IOException
    • getAsMap

      protected static Map<String,Object> getAsMap(String endpoint) throws IOException
      Throws:
      IOException
    • getAsMap

      protected static Map<String,Object> getAsMap(RestClient client, String endpoint) throws IOException
      Throws:
      IOException
    • responseAsMap

      protected static Map<String,Object> responseAsMap(Response response) throws IOException
      Throws:
      IOException
    • responseAsParser

      protected static org.elasticsearch.xcontent.XContentParser responseAsParser(Response response) throws IOException
      Throws:
      IOException
    • responseAsBytes

      protected static BytesReference responseAsBytes(Response response) throws IOException
      Throws:
      IOException
    • registerRepository

      protected static void registerRepository(String repository, String type, boolean verify, Settings settings) throws IOException
      Throws:
      IOException
    • registerRepository

      protected static void registerRepository(RestClient restClient, String repository, String type, boolean verify, Settings settings) throws IOException
      Throws:
      IOException
    • createSnapshot

      protected static void createSnapshot(String repository, String snapshot, boolean waitForCompletion) throws IOException
      Throws:
      IOException
    • createSnapshot

      protected static void createSnapshot(RestClient restClient, String repository, String snapshot, boolean waitForCompletion) throws IOException
      Throws:
      IOException
    • restoreSnapshot

      protected static void restoreSnapshot(String repository, String snapshot, boolean waitForCompletion) throws IOException
      Throws:
      IOException
    • deleteSnapshot

      protected static void deleteSnapshot(String repository, String snapshot, boolean ignoreMissing) throws IOException
      Throws:
      IOException
    • deleteSnapshot

      protected static void deleteSnapshot(RestClient restClient, String repository, String snapshot, boolean ignoreMissing) throws IOException
      Throws:
      IOException
    • isXPackTemplate

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

      public void flush(String index, boolean force) throws IOException
      Throws:
      IOException
    • assertNoFileBasedRecovery

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

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

      public void ensurePeerRecoveryRetentionLeasesRenewedAndSynced(String index) throws 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:
      Exception
    • minimumNodeVersion

      protected static Version minimumNodeVersion() throws IOException
      Returns the minimum node version among all nodes of the cluster
      Throws:
      IOException
    • waitForActiveLicense

      protected static void waitForActiveLicense(RestClient restClient) throws 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:
      Exception - if an exception is thrown while checking the status of the license
    • useIgnoreMultipleMatchingTemplatesWarningsHandler

      protected static void useIgnoreMultipleMatchingTemplatesWarningsHandler(Request request) throws IOException
      Throws:
      IOException
    • isNotFoundResponseException

      protected static boolean isNotFoundResponseException(IOException ioe)
    • fieldCaps

      protected FieldCapabilitiesResponse fieldCaps(List<String> indices, List<String> fields, QueryBuilder indexFilter, String fieldTypes, String fieldFilters) throws IOException
      Throws:
      IOException
    • fieldCaps

      protected FieldCapabilitiesResponse fieldCaps(RestClient restClient, List<String> indices, List<String> fields, QueryBuilder indexFilter, String fieldTypes, String fieldFilters) throws IOException
      Throws:
      IOException