Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Do not remove xpack templates when cleaning #31642

Merged
merged 9 commits into from
Jul 5, 2018

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Jun 28, 2018

At the end of every ESRestTestCase we clean the cluster which includes
deleting all of the templates. If xpack is installed it'll automatically
recreate a few templates every time they are removed. Which is slow.

This change stops the cleanup from removing the xpack templates. It cuts
the time to run the docs tests more than in half and it probably saves a
bit more time on other tests as well.

At the end of every `ESRestTestCase` we clean the cluster which includes
deleting all of the templates. If xpack is installed it'll automatically
recreate a few templates every time they are removed. Which is slow.

This change stops the cleanup from removing the xpack templates. It cuts
the time to run the docs tests more than in half and it probably saves a
bit more time on other tests as well.
@nik9000 nik9000 added :Delivery/Build Build or test infrastructure v7.0.0 v6.4.0 labels Jun 28, 2018
@nik9000 nik9000 requested a review from hub-cap June 28, 2018 02:29
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@nik9000
Copy link
Member Author

nik9000 commented Jun 28, 2018

@hub-cap, this is another one in the line after the hasXPack change.

Copy link
Contributor

@hub-cap hub-cap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM minor comment nit

/**
* Is this template one that is automatically created by xpack? Deleting
* these templates doesn't hurt anything but it makes the tests run more
* slowly because it then has to wait for the templates to be recreated.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment seems kind of odd for a true/false isXXX method :) It cares not about a delete.

if (false == "".equals(templates)) {
for (String template : templates.split("\n")) {
if (isXPackTemplate(template)) continue;
if ("".equals(templates)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be teh singular template and not the list,of,templates, templates ?

@nik9000 nik9000 merged commit 1099060 into elastic:master Jul 5, 2018
@nik9000
Copy link
Member Author

nik9000 commented Jul 5, 2018

Thanks for all the reviews @hub-cap! Merged to master and backporting now.

nik9000 added a commit that referenced this pull request Jul 5, 2018
At the end of every `ESRestTestCase` we clean the cluster which includes
deleting all of the templates. If xpack is installed it'll automatically
recreate a few templates every time they are removed. Which is slow.

This change stops the cleanup from removing the xpack templates. It cuts
the time to run the docs tests more than in half and it probably saves a
bit more time on other tests as well.
@nik9000
Copy link
Member Author

nik9000 commented Jul 5, 2018

Backported to 6.x after a passing build!

dnhatn added a commit that referenced this pull request Jul 5, 2018
* 6.x:
  Test: Do not remove xpack templates when cleaning (#31642)
  SQL: Allow long literals (#31777)
  SQL: Fix incorrect message for aliases (#31792)
  Detach Transport from TransportService (#31727)
  6.3.1 release notes (#31829)
  Add unreleased version 6.3.2
  [ML][TEST] Use java 11 valid time format in DataDescriptionTests (#31817)
  [ML] Don't treat stale FAILED jobs as OPENING in job allocation (#31800)
  [ML] Fix calendar and filter updates from non-master nodes (#31804)
  Fix license header generation on Windows (#31790)
  mark XPackRestIT.test {p0=monitoring/bulk/10_basic/Bulk indexing of monitoring data} as AwaitsFix
  Add JDK11 support without enabling in CI (#31644)
  Watcher: Fix check for currently executed watches (#31137)
  [DOCS] Fixes 6.3.0 release notes (#31771)
  Watcher: Ensure correct method is used to read secure settings (#31753)
  [ML] Rate limit established model memory updates (#31768)
  SQL: Update CLI logo
dnhatn added a commit that referenced this pull request Jul 5, 2018
* master:
  REST high-level client: add get index API (#31703)
  SQL: Allow long literals (#31777)
  SQL: Fix incorrect message for aliases (#31792)
  Test: Do not remove xpack templates when cleaning (#31642)
  Reduce more raw types warnings (#31780)
  Add unreleased version 6.3.2
  Scripting: Remove support for deprecated StoredScript contexts (#31394)
  [ML][TEST] Use java 11 valid time format in DataDescriptionTests (#31817)
  [ML] Don't treat stale FAILED jobs as OPENING in job allocation (#31800)
  [ML] Fix calendar and filter updates from non-master nodes (#31804)
  Fix license header generation on Windows (#31790)
  mark RollupIT.testTwoJobsStartStopDeleteOne as AwaitsFix
  mark SearchAsyncActionTests.testFanOutAndCollect as AwaitsFix
  Correct exclusion of test on JDK 11
  Fix doclint jdk 11
  Add JDK11 support and enable in CI (#31644)
  Watcher: Fix check for currently executed watches (#31137)
  Watcher: Ensure correct method is used to read secure settings (#31753)
  SQL: Update CLI logo
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 28, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 29, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 29, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 29, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 29, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 29, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 30, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 30, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 30, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 30, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jun 30, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jul 2, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
karmi added a commit to elastic/go-elasticsearch that referenced this pull request Jul 2, 2019
* Do not delete X-Pack templates
* Use context timeouts
* Check pending tasks

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

ensureNoInitializingShards();
wipeCluster();
waitForClusterStateUpdatesToFinish();
logIfThereAreRunningTasks();

wipeCluster() {
  if (hasXPack) {
      wipeRollupJobs();
      waitForPendingRollupTasks();
  }
  wipeSnapshots();
  adminClient().performRequest(new Request("DELETE", "*"));
  if (hasXPack) {
    Request request = new Request("GET", "_cat/templates");
    request.addParameter("h", "name");
    if (isXPackTemplate(template)) continue;
    adminClient().performRequest(new Request("DELETE", "_template/" + template));
  } else {
      adminClient().performRequest(new Request("DELETE", "_template/*"));
  }
  wipeClusterSettings();
  if (hasXPack) {
      deleteAllPolicies();
  }

wipeRollupJobs() {
  Response response = adminClient().performRequest(new Request("GET", "/_rollup/job/_all"));
  for (Map<String, Object> jobConfig : jobConfigs) {
    String jobId = (String) ((Map<String, Object>) jobConfig.get("config")).get("id");
    Request request = new Request("POST", "/_rollup/job/" + jobId + "/_stop");
    request.addParameter("ignore", "404");
    request.addParameter("wait_for_completion", "true");
    request.addParameter("timeout", "10s");
  }
  for (Map<String, Object> jobConfig : jobConfigs) {
    Request request = new Request("DELETE", "/_rollup/job/" + jobId);
    request.addParameter("ignore", "404");
  }

waitForPendingRollupTasks() {
  waitForPendingTasks(adminClient(), taskName -> taskName.startsWith("xpack/rollup/job") == false);

ensureNoInitializingShards() {
  Request request = new Request("GET", "/_cluster/health");
  request.addParameter("wait_for_no_initializing_shards", "true");
  request.addParameter("timeout", "70s");
  request.addParameter("level", "shards");
  adminClient().performRequest(request);

waitForClusterStateUpdatesToFinish() {
  assertBusy(() -> {
    Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
    List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
    if (false == tasks.isEmpty()) {
      fail(message.toString());
  }, 30, TimeUnit.SECONDS);

// curl -s -k -X POST 'https://elastic:elastic@localhost:9200/_all/_ilm/remove'
deleteAllPolicies() {
  Response response = adminClient().performRequest(new Request("GET", "/_ilm/policy"));
  for (String policyName : policies.keySet()) {
      adminClient().performRequest(new Request("DELETE", "/_ilm/policy/" + policyName));
  }

// elastic/elasticsearch#31642
//
// > At the end of every ESRestTestCase we clean the cluster which includes
// > deleting all of the templates. If xpack is installed it'll automatically
// > recreate a few templates every time they are removed. Which is slow.
//
isXPackTemplate(String name) {
  if (name.startsWith(".monitoring-")) {
      return true;
  }
  if (name.startsWith(".watch") || name.startsWith(".triggered_watches")) {
      return true;
  }
  if (name.startsWith(".ml-")) {
      return true;
  }
  switch (name) {
  case ".triggered_watches":
  case ".watches":
  case "logstash-index-template":
  case "security_audit_log":
      return true;
  default:
      return false;
  }

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

setupForTests() {
  waitForTemplates();
  waitForWatcher();
  enableMonitoring();

cleanup()
  disableMonitoring();
  clearMlState();
  if (isWaitForPendingTasks()) {
    // This waits for pending tasks to complete, so must go last (otherwise
    // it could be waiting for pending tasks while monitoring is still running).
    ESRestTestCase.waitForPendingTasks(adminClient(), task -> {
            // Don't check rollup jobs because we clear them in the superclass.
            return task.contains(RollupJob.NAME);
    });

waitForTemplates() {
  for (String template : templates) {
      awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
      response -> true,
      () -> "Exception when waiting for [" + template + "] template to be created");
  }

waitForWatcher() {
  if (isWatcherTest()) {
    // ensure watcher is started, so that a test can stop watcher and everything still works fine

enableMonitoring() {
  if (isMonitoringTest()) {
    // Enable monitoring and waits for monitoring documents to be collected and indexed

clearMlState() {
  if (isMachineLearningTest()) {
      new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata();
  }

isMonitoringTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

isWatcherTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
}

isMachineLearningTest() {
    String testName = getTestName();
    return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
}

// ----------------------------------------------------------------------------------------------------
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

class MlRestTestStateCleaner { ... }

clearMlMetadata() {
  deleteAllDatafeeds();
  deleteAllJobs();
  deleteAllDataFrameAnalytics();
  // indices will be deleted by the ESRestTestCase class

deleteAllDatafeeds() {
  Request datafeedsRequest = new Request("GET", "/_ml/datafeeds");
  datafeedsRequest.addParameter("filter_path", "datafeeds");
  datafeeds = (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse));
  try {
      adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop"));
  } catch (Exception e1) {
      logger.warn("failed to stop all datafeeds. Forcing stop", e1);
      try {
          adminClient.performRequest(new Request("POST", "/_ml/datafeeds/_all/_stop?force=true"));
      } catch (Exception e2) {
          logger.warn("Force-closing all data feeds failed", e2);
      }
      throw new RuntimeException(
              "Had to resort to force-stopping datafeeds, something went wrong?", e1);
  }
  for (Map<String, Object> datafeed : datafeeds) {
      String datafeedId = (String) datafeed.get("datafeed_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/datafeeds/" + datafeedId));
  }

deleteAllJobs() {
  Request jobsRequest = new Request("GET", "/_ml/anomaly_detectors");
  jobsRequest.addParameter("filter_path", "jobs");
  jobConfigs = (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response));
  if (jobConfigs == null) {
      return;
  }
  adminClient.performRequest(new Request("POST", "/_ml/anomaly_detectors/_all/_close"));
  for (Map<String, Object> jobConfig : jobConfigs) {
      String jobId = (String) jobConfig.get("job_id");
      adminClient.performRequest(new Request("DELETE", "/_ml/anomaly_detectors/" + jobId));
  }

deleteAllDataFrameAnalytics() {
  Request analyticsRequest = new Request("GET", "/_ml/data_frame/analytics?size=10000");
  analyticsRequest.addParameter("filter_path", "data_frame_analytics");
  analytics = (List<Map<String, Object>>) XContentMapValues.extractValue("data_frame_analytics", ESRestTestCase.entityAsMap(analyticsResponse));
  if (analytics == null) {
      return;
  }

  for (Map<String, Object> config : analytics) {
      String id = (String) config.get("id");
      adminClient.performRequest(new Request("DELETE", "/_ml/data_frame/analytics/" + id));
  }
@mark-vieira mark-vieira added the Team:Delivery Meta label for Delivery team label Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Delivery/Build Build or test infrastructure Team:Delivery Meta label for Delivery team v6.4.0 v7.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants