Skip to content

Refactor watcher tests #52799

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

Merged
merged 3 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public void reenableWatcher() throws Exception {
}
}

@Override
protected boolean isWatcherTest() {
String testName = getTestName();
return testName != null && (testName.contains("watcher/") || testName.contains("watcher\\"));
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ dependencies {
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
}

// https://github.com/elastic/x-plugins/issues/724
configurations {
testArtifacts.extendsFrom testRuntime
restXpackSpecs
Expand Down Expand Up @@ -88,6 +87,7 @@ testClusters.integTest {
testDistribution = 'DEFAULT' // this is important since we use the reindex module in ML
setting 'xpack.ml.enabled', 'true'
setting 'xpack.security.enabled', 'true'
setting 'xpack.watcher.enabled', 'false'
// Integration tests are supposed to enable/disable exporters before/after each test
setting 'xpack.monitoring.exporters._local.type', 'local'
setting 'xpack.monitoring.exporters._local.enabled', 'false'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ protected boolean isMonitoringTest() {
return false;
}

@Override
protected boolean isWatcherTest() {
return false;
}

@Override
protected boolean isMachineLearningTest() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
package org.elasticsearch.xpack.test.rest;

import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.apache.http.HttpStatus;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
Expand All @@ -21,14 +18,12 @@
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse;
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath;
import org.elasticsearch.xpack.core.ml.MlMetaIndex;
import org.elasticsearch.xpack.core.ml.integration.MlRestTestStateCleaner;
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields;
import org.elasticsearch.xpack.core.ml.notifications.NotificationsIndex;
import org.elasticsearch.xpack.core.rollup.job.RollupJob;
import org.elasticsearch.xpack.core.watcher.support.WatcherIndexTemplateRegistryField;
import org.junit.After;
import org.junit.Before;

Expand All @@ -50,7 +45,6 @@
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;

/** Runs rest tests against external cluster */
public class XPackRestIT extends ESClientYamlSuiteTestCase {
Expand All @@ -77,7 +71,6 @@ protected Settings restClientSettings() {
@Before
public void setupForTests() throws Exception {
waitForTemplates();
waitForWatcher();
enableMonitoring();
}

Expand All @@ -103,60 +96,7 @@ private void waitForTemplates() throws Exception {
}
}

private void waitForWatcher() throws Exception {
// ensure watcher is started, so that a test can stop watcher and everything still works fine
if (isWatcherTest()) {
assertBusy(() -> {
ClientYamlTestResponse response =
getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");

switch (state) {
case "stopped":
ClientYamlTestResponse startResponse =
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());
boolean isAcknowledged = (boolean) startResponse.evaluate("acknowledged");
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until stopped state reached started state");
case "stopping":
throw new AssertionError("waiting until stopping state reached stopped state to start again");
case "starting":
throw new AssertionError("waiting until starting state reached started state");
case "started":
// all good here, we are done
break;
default:
throw new AssertionError("unknown state[" + state + "]");
}
});

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

boolean existsWatcherIndex = adminClient()
.performRequest(new Request("HEAD", ".watches"))
.getStatusLine().getStatusCode() == 200;
if (existsWatcherIndex == false) {
return;
}
Request searchWatchesRequest = new Request("GET", ".watches/_search");
searchWatchesRequest.addParameter(TOTAL_HITS_AS_INT_PARAM, "true");
searchWatchesRequest.addParameter("size", "1000");
Response response = adminClient().performRequest(searchWatchesRequest);
ObjectPath objectPathResponse = ObjectPath.createFromResponse(response);
int totalHits = objectPathResponse.evaluate("hits.total");
if (totalHits > 0) {
List<Map<String, Object>> hits = objectPathResponse.evaluate("hits.hits");
for (Map<String, Object> hit : hits) {
String id = (String) hit.get("_id");
adminClient().performRequest(new Request("DELETE", "_watcher/watch/" + id));
}
}
}
}

/**
* Enable monitoring and waits for monitoring documents to be collected and indexed in
Expand Down Expand Up @@ -314,11 +254,6 @@ protected boolean isMonitoringTest() {
return testName != null && (testName.contains("=monitoring/") || testName.contains("=monitoring\\"));
}

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

protected boolean isMachineLearningTest() {
String testName = getTestName();
return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugin/watcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,12 @@ test {
// installing them as individual plugins for integ tests doesn't make sense,
// so we disable integ tests
integTest.enabled = false

// add all sub-projects of the qa sub-project
gradle.projectsEvaluated {
project.subprojects
.find { it.path == project.path + ":qa" }
.subprojects
.findAll { it.path.startsWith(project.path + ":qa") }
.each { check.dependsOn it.check }
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@ dependencies {
testCompile project(':x-pack:qa')
}

configurations {
testArtifacts.extendsFrom testRuntime
}

task testJar(type: Jar) {
appendix 'test'
from sourceSets.test.output
}

artifacts {
testArtifacts testJar
}

restResources {
restApi {
includeXpack 'watcher'
includeXpack 'watcher', 'xpack'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath;
import org.elasticsearch.xpack.test.rest.XPackRestTestConstants;
import org.junit.After;
import org.junit.Before;
import org.elasticsearch.xpack.watcher.WatcherRestTestCase;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
Expand All @@ -33,71 +29,11 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
public class SmokeTestWatcherTestSuiteIT extends WatcherRestTestCase {

private static final String TEST_ADMIN_USERNAME = "test_admin";
private static final String TEST_ADMIN_PASSWORD = "x-pack-test-password";

@Before
public void startWatcher() throws Exception {
// delete the watcher history to not clutter with entries from other test
assertOK(adminClient().performRequest(new Request("DELETE", "/.watcher-history-*")));

assertBusy(() -> {
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");

switch (state) {
case "stopped":
Response startResponse = adminClient().performRequest(new Request("POST", "/_watcher/_start"));
boolean isAcknowledged = ObjectPath.createFromResponse(startResponse).evaluate("acknowledged");
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until stopped state reached started state");
case "stopping":
throw new AssertionError("waiting until stopping state reached stopped state to start again");
case "starting":
throw new AssertionError("waiting until starting state reached started state");
case "started":
// all good here, we are done
break;
default:
throw new AssertionError("unknown state[" + state + "]");
}
});

assertBusy(() -> {
for (String template : XPackRestTestConstants.TEMPLATE_NAMES_NO_ILM) {
Response templateExistsResponse = adminClient().performRequest(new Request("HEAD", "/_template/" + template));
assertThat(templateExistsResponse.getStatusLine().getStatusCode(), is(200));
}
});
}

@After
public void stopWatcher() throws Exception {
assertBusy(() -> {
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");

switch (state) {
case "stopped":
// all good here, we are done
break;
case "stopping":
throw new AssertionError("waiting until stopping state reached stopped state");
case "starting":
throw new AssertionError("waiting until starting state reached started state to stop");
case "started":
Response stopResponse = adminClient().performRequest(new Request("POST", "/_watcher/_stop"));
boolean isAcknowledged = ObjectPath.createFromResponse(stopResponse).evaluate("acknowledged");
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until started state reached stopped state");
default:
throw new AssertionError("unknown state[" + state + "]");
}
}, 60, TimeUnit.SECONDS);
}

@Override
protected Settings restClientSettings() {
String token = basicAuthHeaderValue("watcher_manager", new SecureString("x-pack-test-password".toCharArray()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.smoketest;

import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.xpack.watcher.WatcherYamlSuiteTestCase;

/**
* Runs the YAML rest tests against an external cluster
*/
public class WatcherYamlRestIT extends WatcherYamlSuiteTestCase {
public WatcherYamlRestIT(ClientYamlTestCandidate testCandidate) {
super(testCandidate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.watcher;

import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath;
import org.junit.After;
import org.junit.Before;

import java.util.concurrent.TimeUnit;

import static org.hamcrest.Matchers.is;

/**
* Parent test class for Watcher (not-YAML) based REST tests
*/
public abstract class WatcherRestTestCase extends ESRestTestCase {

@Before
public final void startWatcher() throws Exception {
assertBusy(() -> {
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");

switch (state) {
case "stopped":
Response startResponse = adminClient().performRequest(new Request("POST", "/_watcher/_start"));
boolean isAcknowledged = ObjectPath.createFromResponse(startResponse).evaluate("acknowledged");
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until stopped state reached started state");
case "stopping":
throw new AssertionError("waiting until stopping state reached stopped state to start again");
case "starting":
throw new AssertionError("waiting until starting state reached started state");
case "started":
// all good here, we are done
break;
default:
throw new AssertionError("unknown state[" + state + "]");
}
});
}

@After
public final void stopWatcher() throws Exception {
assertBusy(() -> {
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");

switch (state) {
case "stopped":
// all good here, we are done
break;
case "stopping":
throw new AssertionError("waiting until stopping state reached stopped state");
case "starting":
throw new AssertionError("waiting until starting state reached started state to stop");
case "started":
Response stopResponse = adminClient().performRequest(new Request("POST", "/_watcher/_stop"));
boolean isAcknowledged = ObjectPath.createFromResponse(stopResponse).evaluate("acknowledged");
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until started state reached stopped state");
default:
throw new AssertionError("unknown state[" + state + "]");
}
}, 60, TimeUnit.SECONDS);

Request deleteWatchesIndexRequest = new Request("DELETE", ".watches");
deleteWatchesIndexRequest.addParameter("ignore_unavailable", "true");
adminClient().performRequest(deleteWatchesIndexRequest);

Request deleteWatchHistoryRequest = new Request("DELETE", ".watcher-history-*");
deleteWatchHistoryRequest.addParameter("ignore_unavailable", "true");
adminClient().performRequest(deleteWatchHistoryRequest);
}
}
Loading