Skip to content

Commit b4179a8

Browse files
authored
[7.x] Refactor watcher tests (#52799) (#52844)
This PR moves the majority of the Watcher REST tests under the Watcher x-pack plugin. Specifically, moves the Watcher tests from: x-pack/plugin/test x-pack/qa/smoke-test-watcher x-pack/qa/smoke-test-watcher-with-security x-pack/qa/smoke-test-monitoring-with-watcher to: x-pack/plugin/watcher/qa/rest (/test and /qa/smoke-test-watcher) x-pack/plugin/watcher/qa/with-security x-pack/plugin/watcher/qa/with-monitoring Additionally, this disables Watcher from the main x-pack test cluster and consolidates the stop/start logic for the tests listed. No changes to the tests (beyond moving them) are included. 3rd party tests and doc tests (which also touch Watcher) are not included in the changes here.
1 parent 305c834 commit b4179a8

File tree

64 files changed

+220
-371
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+220
-371
lines changed

x-pack/docs/src/test/java/org/elasticsearch/smoketest/XDocsClientYamlTestSuiteIT.java

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public void reenableWatcher() throws Exception {
103103
}
104104
}
105105

106-
@Override
107106
protected boolean isWatcherTest() {
108107
String testName = getTestName();
109108
return testName != null && (testName.contains("watcher/") || testName.contains("watcher\\"));

x-pack/plugin/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ subprojects {
6363
}
6464
}
6565

66-
// https://github.com/elastic/x-plugins/issues/724
6766
configurations {
6867
testArtifacts.extendsFrom testRuntime
6968
restXpackSpecs
@@ -141,6 +140,7 @@ testClusters.integTest {
141140
testDistribution = 'DEFAULT' // this is important since we use the reindex module in ML
142141
setting 'xpack.ml.enabled', 'true'
143142
setting 'xpack.security.enabled', 'true'
143+
setting 'xpack.watcher.enabled', 'false'
144144
// Integration tests are supposed to enable/disable exporters before/after each test
145145
setting 'xpack.monitoring.exporters._local.type', 'local'
146146
setting 'xpack.monitoring.exporters._local.enabled', 'false'

x-pack/plugin/ml/qa/ml-with-security/src/test/java/org/elasticsearch/smoketest/MlWithSecurityIT.java

-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ protected boolean isMonitoringTest() {
5858
return false;
5959
}
6060

61-
@Override
62-
protected boolean isWatcherTest() {
63-
return false;
64-
}
65-
6661
@Override
6762
protected boolean isMachineLearningTest() {
6863
return true;

x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

-65
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
package org.elasticsearch.xpack.test.rest;
77

88
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
9-
109
import org.apache.http.HttpStatus;
1110
import org.elasticsearch.ElasticsearchException;
12-
import org.elasticsearch.client.Request;
13-
import org.elasticsearch.client.Response;
1411
import org.elasticsearch.common.CheckedFunction;
1512
import org.elasticsearch.common.settings.Settings;
1613
import org.elasticsearch.common.util.concurrent.ThreadContext;
@@ -21,14 +18,12 @@
2118
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
2219
import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse;
2320
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
24-
import org.elasticsearch.test.rest.yaml.ObjectPath;
2521
import org.elasticsearch.xpack.core.ml.MlMetaIndex;
2622
import org.elasticsearch.xpack.core.ml.integration.MlRestTestStateCleaner;
2723
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
2824
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields;
2925
import org.elasticsearch.xpack.core.ml.notifications.NotificationsIndex;
3026
import org.elasticsearch.xpack.core.rollup.job.RollupJob;
31-
import org.elasticsearch.xpack.core.watcher.support.WatcherIndexTemplateRegistryField;
3227
import org.junit.After;
3328
import org.junit.Before;
3429

@@ -50,7 +45,6 @@
5045
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
5146
import static org.hamcrest.Matchers.equalTo;
5247
import static org.hamcrest.Matchers.hasSize;
53-
import static org.hamcrest.Matchers.is;
5448

5549
/** Runs rest tests against external cluster */
5650
public class XPackRestIT extends ESClientYamlSuiteTestCase {
@@ -77,7 +71,6 @@ protected Settings restClientSettings() {
7771
@Before
7872
public void setupForTests() throws Exception {
7973
waitForTemplates();
80-
waitForWatcher();
8174
enableMonitoring();
8275
}
8376

@@ -103,60 +96,7 @@ private void waitForTemplates() throws Exception {
10396
}
10497
}
10598

106-
private void waitForWatcher() throws Exception {
107-
// ensure watcher is started, so that a test can stop watcher and everything still works fine
108-
if (isWatcherTest()) {
109-
assertBusy(() -> {
110-
ClientYamlTestResponse response =
111-
getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
112-
String state = (String) response.evaluate("stats.0.watcher_state");
113-
114-
switch (state) {
115-
case "stopped":
116-
ClientYamlTestResponse startResponse =
117-
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());
118-
boolean isAcknowledged = (boolean) startResponse.evaluate("acknowledged");
119-
assertThat(isAcknowledged, is(true));
120-
throw new AssertionError("waiting until stopped state reached started state");
121-
case "stopping":
122-
throw new AssertionError("waiting until stopping state reached stopped state to start again");
123-
case "starting":
124-
throw new AssertionError("waiting until starting state reached started state");
125-
case "started":
126-
// all good here, we are done
127-
break;
128-
default:
129-
throw new AssertionError("unknown state[" + state + "]");
130-
}
131-
});
13299

133-
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
134-
awaitCallApi("indices.exists_template", singletonMap("name", template), emptyList(),
135-
response -> true,
136-
() -> "Exception when waiting for [" + template + "] template to be created");
137-
}
138-
139-
boolean existsWatcherIndex = adminClient()
140-
.performRequest(new Request("HEAD", ".watches"))
141-
.getStatusLine().getStatusCode() == 200;
142-
if (existsWatcherIndex == false) {
143-
return;
144-
}
145-
Request searchWatchesRequest = new Request("GET", ".watches/_search");
146-
searchWatchesRequest.addParameter(TOTAL_HITS_AS_INT_PARAM, "true");
147-
searchWatchesRequest.addParameter("size", "1000");
148-
Response response = adminClient().performRequest(searchWatchesRequest);
149-
ObjectPath objectPathResponse = ObjectPath.createFromResponse(response);
150-
int totalHits = objectPathResponse.evaluate("hits.total");
151-
if (totalHits > 0) {
152-
List<Map<String, Object>> hits = objectPathResponse.evaluate("hits.hits");
153-
for (Map<String, Object> hit : hits) {
154-
String id = (String) hit.get("_id");
155-
adminClient().performRequest(new Request("DELETE", "_watcher/watch/" + id));
156-
}
157-
}
158-
}
159-
}
160100

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

317-
protected boolean isWatcherTest() {
318-
String testName = getTestName();
319-
return testName != null && (testName.contains("=watcher/") || testName.contains("=watcher\\"));
320-
}
321-
322257
protected boolean isMachineLearningTest() {
323258
String testName = getTestName();
324259
return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));

x-pack/plugin/watcher/build.gradle

+9
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,12 @@ test {
139139
// installing them as individual plugins for integ tests doesn't make sense,
140140
// so we disable integ tests
141141
integTest.enabled = false
142+
143+
// add all sub-projects of the qa sub-project
144+
gradle.projectsEvaluated {
145+
project.subprojects
146+
.find { it.path == project.path + ":qa" }
147+
.subprojects
148+
.findAll { it.path.startsWith(project.path + ":qa") }
149+
.each { check.dependsOn it.check }
150+
}

x-pack/plugin/watcher/qa/build.gradle

Whitespace-only changes.

x-pack/qa/smoke-test-watcher/build.gradle renamed to x-pack/plugin/watcher/qa/rest/build.gradle

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ dependencies {
66
testCompile project(':x-pack:qa')
77
}
88

9+
configurations {
10+
testArtifacts.extendsFrom testRuntime
11+
}
12+
13+
task testJar(type: Jar) {
14+
appendix 'test'
15+
from sourceSets.test.output
16+
}
17+
18+
artifacts {
19+
testArtifacts testJar
20+
}
21+
922
restResources {
1023
restApi {
11-
includeXpack 'watcher'
24+
includeXpack 'watcher', 'xpack'
1225
}
1326
}
1427

+2-66
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
import org.elasticsearch.common.util.concurrent.ThreadContext;
1515
import org.elasticsearch.common.xcontent.XContentBuilder;
1616
import org.elasticsearch.common.xcontent.XContentType;
17-
import org.elasticsearch.test.rest.ESRestTestCase;
1817
import org.elasticsearch.test.rest.yaml.ObjectPath;
19-
import org.elasticsearch.xpack.test.rest.XPackRestTestConstants;
20-
import org.junit.After;
21-
import org.junit.Before;
18+
import org.elasticsearch.xpack.watcher.WatcherRestTestCase;
2219

2320
import java.io.IOException;
2421
import java.util.Map;
25-
import java.util.concurrent.TimeUnit;
2622
import java.util.concurrent.atomic.AtomicReference;
2723

2824
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@@ -33,71 +29,11 @@
3329
import static org.hamcrest.Matchers.is;
3430
import static org.hamcrest.Matchers.notNullValue;
3531

36-
public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
32+
public class SmokeTestWatcherTestSuiteIT extends WatcherRestTestCase {
3733

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

41-
@Before
42-
public void startWatcher() throws Exception {
43-
// delete the watcher history to not clutter with entries from other test
44-
assertOK(adminClient().performRequest(new Request("DELETE", "/.watcher-history-*")));
45-
46-
assertBusy(() -> {
47-
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
48-
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");
49-
50-
switch (state) {
51-
case "stopped":
52-
Response startResponse = adminClient().performRequest(new Request("POST", "/_watcher/_start"));
53-
boolean isAcknowledged = ObjectPath.createFromResponse(startResponse).evaluate("acknowledged");
54-
assertThat(isAcknowledged, is(true));
55-
throw new AssertionError("waiting until stopped state reached started state");
56-
case "stopping":
57-
throw new AssertionError("waiting until stopping state reached stopped state to start again");
58-
case "starting":
59-
throw new AssertionError("waiting until starting state reached started state");
60-
case "started":
61-
// all good here, we are done
62-
break;
63-
default:
64-
throw new AssertionError("unknown state[" + state + "]");
65-
}
66-
});
67-
68-
assertBusy(() -> {
69-
for (String template : XPackRestTestConstants.TEMPLATE_NAMES_NO_ILM) {
70-
Response templateExistsResponse = adminClient().performRequest(new Request("HEAD", "/_template/" + template));
71-
assertThat(templateExistsResponse.getStatusLine().getStatusCode(), is(200));
72-
}
73-
});
74-
}
75-
76-
@After
77-
public void stopWatcher() throws Exception {
78-
assertBusy(() -> {
79-
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
80-
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");
81-
82-
switch (state) {
83-
case "stopped":
84-
// all good here, we are done
85-
break;
86-
case "stopping":
87-
throw new AssertionError("waiting until stopping state reached stopped state");
88-
case "starting":
89-
throw new AssertionError("waiting until starting state reached started state to stop");
90-
case "started":
91-
Response stopResponse = adminClient().performRequest(new Request("POST", "/_watcher/_stop"));
92-
boolean isAcknowledged = ObjectPath.createFromResponse(stopResponse).evaluate("acknowledged");
93-
assertThat(isAcknowledged, is(true));
94-
throw new AssertionError("waiting until started state reached stopped state");
95-
default:
96-
throw new AssertionError("unknown state[" + state + "]");
97-
}
98-
}, 60, TimeUnit.SECONDS);
99-
}
100-
10137
@Override
10238
protected Settings restClientSettings() {
10339
String token = basicAuthHeaderValue("watcher_manager", new SecureString("x-pack-test-password".toCharArray()));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.smoketest;
7+
8+
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
9+
import org.elasticsearch.xpack.watcher.WatcherYamlSuiteTestCase;
10+
11+
/**
12+
* Runs the YAML rest tests against an external cluster
13+
*/
14+
public class WatcherYamlRestIT extends WatcherYamlSuiteTestCase {
15+
public WatcherYamlRestIT(ClientYamlTestCandidate testCandidate) {
16+
super(testCandidate);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.watcher;
7+
8+
import org.elasticsearch.client.Request;
9+
import org.elasticsearch.client.Response;
10+
import org.elasticsearch.test.rest.ESRestTestCase;
11+
import org.elasticsearch.test.rest.yaml.ObjectPath;
12+
import org.junit.After;
13+
import org.junit.Before;
14+
15+
import java.util.concurrent.TimeUnit;
16+
17+
import static org.hamcrest.Matchers.is;
18+
19+
/**
20+
* Parent test class for Watcher (not-YAML) based REST tests
21+
*/
22+
public abstract class WatcherRestTestCase extends ESRestTestCase {
23+
24+
@Before
25+
public final void startWatcher() throws Exception {
26+
assertBusy(() -> {
27+
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
28+
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");
29+
30+
switch (state) {
31+
case "stopped":
32+
Response startResponse = adminClient().performRequest(new Request("POST", "/_watcher/_start"));
33+
boolean isAcknowledged = ObjectPath.createFromResponse(startResponse).evaluate("acknowledged");
34+
assertThat(isAcknowledged, is(true));
35+
throw new AssertionError("waiting until stopped state reached started state");
36+
case "stopping":
37+
throw new AssertionError("waiting until stopping state reached stopped state to start again");
38+
case "starting":
39+
throw new AssertionError("waiting until starting state reached started state");
40+
case "started":
41+
// all good here, we are done
42+
break;
43+
default:
44+
throw new AssertionError("unknown state[" + state + "]");
45+
}
46+
});
47+
}
48+
49+
@After
50+
public final void stopWatcher() throws Exception {
51+
assertBusy(() -> {
52+
Response response = adminClient().performRequest(new Request("GET", "/_watcher/stats"));
53+
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");
54+
55+
switch (state) {
56+
case "stopped":
57+
// all good here, we are done
58+
break;
59+
case "stopping":
60+
throw new AssertionError("waiting until stopping state reached stopped state");
61+
case "starting":
62+
throw new AssertionError("waiting until starting state reached started state to stop");
63+
case "started":
64+
Response stopResponse = adminClient().performRequest(new Request("POST", "/_watcher/_stop"));
65+
boolean isAcknowledged = ObjectPath.createFromResponse(stopResponse).evaluate("acknowledged");
66+
assertThat(isAcknowledged, is(true));
67+
throw new AssertionError("waiting until started state reached stopped state");
68+
default:
69+
throw new AssertionError("unknown state[" + state + "]");
70+
}
71+
}, 60, TimeUnit.SECONDS);
72+
73+
Request deleteWatchesIndexRequest = new Request("DELETE", ".watches");
74+
deleteWatchesIndexRequest.addParameter("ignore_unavailable", "true");
75+
adminClient().performRequest(deleteWatchesIndexRequest);
76+
77+
Request deleteWatchHistoryRequest = new Request("DELETE", ".watcher-history-*");
78+
deleteWatchHistoryRequest.addParameter("ignore_unavailable", "true");
79+
adminClient().performRequest(deleteWatchHistoryRequest);
80+
}
81+
}

0 commit comments

Comments
 (0)