Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix-template-merging-…
Browse files Browse the repository at this point in the history
…regression
  • Loading branch information
felixbarny committed Apr 8, 2024
2 parents c0c5333 + 9496fa3 commit 5ec1a75
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void reenableWatcher() throws Exception {
if (isWatcherTest()) {
assertBusy(() -> {
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");
String state = response.evaluate("stats.0.watcher_state");

switch (state) {
case "stopped":
Expand All @@ -261,7 +261,7 @@ public void reenableWatcher() throws Exception {
emptyList(),
emptyMap()
);
boolean isAcknowledged = (boolean) startResponse.evaluate("acknowledged");
boolean isAcknowledged = startResponse.evaluate("acknowledged");
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until stopped state reached started state");
case "stopping":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ public boolean isError() {
/**
* Parses the response body and extracts a specific value from it (identified by the provided path)
*/
public Object evaluate(String path) throws IOException {
return evaluate(path, Stash.EMPTY);
@SuppressWarnings("unchecked")
public <T> T evaluate(String path) throws IOException {
return (T) evaluate(path, Stash.EMPTY);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,7 @@ static String readOsFromNodesInfo(RestClient restClient) throws IOException {
ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
SortedSet<String> osPrettyNames = new TreeSet<>();

@SuppressWarnings("unchecked")
final Map<String, Object> nodes = (Map<String, Object>) restTestResponse.evaluate("nodes");
final Map<String, Object> nodes = restTestResponse.evaluate("nodes");

for (Entry<String, Object> node : nodes.entrySet()) {
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void execute(ClientYamlTestExecutionContext executionContext) throws IOEx
original.getApiCallSection().getNodeSelector()
);

String id = (String) startResponse.evaluate("id");
String id = startResponse.evaluate("id");
boolean finishedEarly = id == null;
if (finishedEarly) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ObjectPath;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.Matchers.is;

/**
* Parent test class for Watcher (not-YAML) based REST tests
*/
Expand All @@ -36,7 +36,7 @@ public final void startWatcher() throws Exception {
case "stopped":
Response startResponse = ESRestTestCase.adminClient().performRequest(new Request("POST", "/_watcher/_start"));
boolean isAcknowledged = ObjectPath.createFromResponse(startResponse).evaluate("acknowledged");
Assert.assertThat(isAcknowledged, Matchers.is(true));
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");
Expand Down Expand Up @@ -68,7 +68,7 @@ public final void stopWatcher() throws Exception {
case "started":
Response stopResponse = ESRestTestCase.adminClient().performRequest(new Request("POST", "/_watcher/_stop"));
boolean isAcknowledged = ObjectPath.createFromResponse(stopResponse).evaluate("acknowledged");
Assert.assertThat(isAcknowledged, Matchers.is(true));
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until started state reached stopped state");
default:
throw new AssertionError("unknown state[" + state + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse;
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static org.elasticsearch.xpack.watcher.WatcherRestTestCase.deleteAllWatcherData;
import static org.hamcrest.Matchers.is;

/**
* Parent test class for Watcher YAML based REST tests
Expand All @@ -40,25 +39,25 @@ public static Iterable<Object[]> parameters() throws Exception {
@Before
public final void startWatcher() throws Exception {
ESTestCase.assertBusy(() -> {
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", Map.of(), List.of(), Map.of());
String state = response.evaluate("stats.0.watcher_state");

switch (state) {
case "stopped" -> {
ClientYamlTestResponse startResponse = getAdminExecutionContext().callApi(
"watcher.start",
emptyMap(),
emptyList(),
emptyMap()
Map.of(),
List.of(),
Map.of()
);
boolean isAcknowledged = (boolean) startResponse.evaluate("acknowledged");
Assert.assertThat(isAcknowledged, Matchers.is(true));
boolean isAcknowledged = 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" -> {
int watcherCount = (int) response.evaluate("stats.0.watch_count");
int watcherCount = response.evaluate("stats.0.watch_count");
if (watcherCount > 0) {
logger.info("expected 0 active watches, but got [{}], deleting watcher indices again", watcherCount);
deleteAllWatcherData();
Expand All @@ -73,8 +72,8 @@ public final void startWatcher() throws Exception {
@After
public final void stopWatcher() throws Exception {
ESTestCase.assertBusy(() -> {
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", Map.of(), List.of(), Map.of());
String state = response.evaluate("stats.0.watcher_state");
switch (state) {
case "stopped":
// all good here, we are done
Expand All @@ -84,14 +83,9 @@ public final void stopWatcher() throws Exception {
case "starting":
throw new AssertionError("waiting until starting state reached started state to stop");
case "started":
ClientYamlTestResponse stopResponse = getAdminExecutionContext().callApi(
"watcher.stop",
emptyMap(),
emptyList(),
emptyMap()
);
boolean isAcknowledged = (boolean) stopResponse.evaluate("acknowledged");
Assert.assertThat(isAcknowledged, Matchers.is(true));
ClientYamlTestResponse stopResponse = getAdminExecutionContext().callApi("watcher.stop", Map.of(), List.of(), Map.of());
boolean isAcknowledged = stopResponse.evaluate("acknowledged");
assertThat(isAcknowledged, is(true));
throw new AssertionError("waiting until started state reached stopped state");
default:
throw new AssertionError("unknown state[" + state + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void postIndex(ShardId shardId, Engine.Index operation, Engine.IndexResul
logger.debug("adding watch [{}] to trigger service", watch.id());
triggerService.add(watch);
} else {
logger.debug("removing watch [{}] to trigger service", watch.id());
logger.debug("removing watch [{}] from trigger service", watch.id());
triggerService.remove(watch.id());
}
} else {
Expand Down Expand Up @@ -179,7 +179,7 @@ public void postIndex(ShardId shardId, Engine.Index index, Exception ex) {
@Override
public Engine.Delete preDelete(ShardId shardId, Engine.Delete delete) {
if (isWatchDocument(shardId.getIndexName())) {
logger.debug("removing watch [{}] to trigger service via delete", delete.id());
logger.debug("removing watch [{}] from trigger service via delete", delete.id());
triggerService.remove(delete.id());
}
return delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class WatcherLifeCycleService implements ClusterStateListener {
private final AtomicReference<WatcherState> state = new AtomicReference<>(WatcherState.STARTED);
private final AtomicReference<List<ShardRouting>> previousShardRoutings = new AtomicReference<>(Collections.emptyList());
private volatile boolean shutDown = false; // indicates that the node has been shutdown and we should never start watcher after this.
private volatile WatcherService watcherService;
private final WatcherService watcherService;
private final EnumSet<WatcherState> stopStates = EnumSet.of(WatcherState.STOPPED, WatcherState.STOPPING);

WatcherLifeCycleService(ClusterService clusterService, WatcherService watcherService) {
Expand Down Expand Up @@ -123,7 +123,6 @@ public void clusterChanged(ClusterChangedEvent event) {
} else {
logger.info("watcher has not been stopped. not currently in a stopping state, current state [{}]", state.get());
}

});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import org.junit.Before;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.is;

/** Runs rest tests against external cluster */
Expand All @@ -40,23 +37,23 @@ public static Iterable<Object[]> parameters() throws Exception {

@Before
public void startWatcher() throws Exception {
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
final List<String> watcherTemplates = List.of(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
assertBusy(() -> {
try {
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());
getAdminExecutionContext().callApi("watcher.start", Map.of(), List.of(), Map.of());

for (String template : watcherTemplates) {
ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi(
"indices.exists_template",
singletonMap("name", template),
emptyList(),
emptyMap()
Map.of("name", template),
List.of(),
Map.of()
);
assertThat(templateExistsResponse.getStatusCode(), is(200));
}

ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", Map.of(), List.of(), Map.of());
String state = response.evaluate("stats.0.watcher_state");
assertThat(state, is("started"));
} catch (IOException e) {
throw new AssertionError(e);
Expand All @@ -68,9 +65,9 @@ public void startWatcher() throws Exception {
public void stopWatcher() throws Exception {
assertBusy(() -> {
try {
getAdminExecutionContext().callApi("watcher.stop", emptyMap(), emptyList(), emptyMap());
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");
getAdminExecutionContext().callApi("watcher.stop", Map.of(), List.of(), Map.of());
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", Map.of(), List.of(), Map.of());
String state = response.evaluate("stats.0.watcher_state");
assertThat(state, is("stopped"));
} catch (IOException e) {
throw new AssertionError(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import org.junit.Before;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.is;

/** Runs rest tests against external cluster */
Expand All @@ -40,23 +37,23 @@ public static Iterable<Object[]> parameters() throws Exception {

@Before
public void startWatcher() throws Exception {
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
final List<String> watcherTemplates = List.of(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
assertBusy(() -> {
try {
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());
getAdminExecutionContext().callApi("watcher.start", Map.of(), List.of(), Map.of());

for (String template : watcherTemplates) {
ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi(
"indices.exists_template",
singletonMap("name", template),
emptyList(),
emptyMap()
Map.of("name", template),
List.of(),
Map.of()
);
assertThat(templateExistsResponse.getStatusCode(), is(200));
}

ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", Map.of(), List.of(), Map.of());
String state = response.evaluate("stats.0.watcher_state");
assertThat(state, is("started"));
} catch (IOException e) {
throw new AssertionError(e);
Expand All @@ -68,9 +65,9 @@ public void startWatcher() throws Exception {
public void stopWatcher() throws Exception {
assertBusy(() -> {
try {
getAdminExecutionContext().callApi("watcher.stop", emptyMap(), emptyList(), emptyMap());
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap());
String state = (String) response.evaluate("stats.0.watcher_state");
getAdminExecutionContext().callApi("watcher.stop", Map.of(), List.of(), Map.of());
ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", Map.of(), List.of(), Map.of());
String state = response.evaluate("stats.0.watcher_state");
assertThat(state, is("stopped"));
} catch (IOException e) {
throw new AssertionError(e);
Expand Down

0 comments on commit 5ec1a75

Please sign in to comment.