|
| 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 com.carrotsearch.randomizedtesting.annotations.Name; |
| 9 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 10 | +import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; |
| 11 | +import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse; |
| 12 | +import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; |
| 13 | +import org.elasticsearch.xpack.core.watcher.support.WatcherIndexTemplateRegistryField; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | + |
| 17 | +import static java.util.Collections.emptyList; |
| 18 | +import static java.util.Collections.emptyMap; |
| 19 | +import static java.util.Collections.singletonMap; |
| 20 | +import static org.hamcrest.Matchers.is; |
| 21 | + |
| 22 | +/** Runs rest tests against external cluster */ |
| 23 | +public class WatcherRestIT extends ESClientYamlSuiteTestCase { |
| 24 | + |
| 25 | + public WatcherRestIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { |
| 26 | + super(testCandidate); |
| 27 | + } |
| 28 | + |
| 29 | + @ParametersFactory |
| 30 | + public static Iterable<Object[]> parameters() throws Exception { |
| 31 | + return ESClientYamlSuiteTestCase.createParameters(); |
| 32 | + } |
| 33 | + |
| 34 | + @Before |
| 35 | + public void startWatcher() throws Exception { |
| 36 | + assertBusy(() -> { |
| 37 | + ClientYamlTestResponse response = |
| 38 | + getAdminExecutionContext().callApi("xpack.watcher.stats", emptyMap(), emptyList(), emptyMap()); |
| 39 | + String state = (String) response.evaluate("stats.0.watcher_state"); |
| 40 | + |
| 41 | + switch (state) { |
| 42 | + case "stopped": |
| 43 | + ClientYamlTestResponse startResponse = |
| 44 | + getAdminExecutionContext().callApi("xpack.watcher.start", emptyMap(), emptyList(), emptyMap()); |
| 45 | + boolean isAcknowledged = (boolean) startResponse.evaluate("acknowledged"); |
| 46 | + assertThat(isAcknowledged, is(true)); |
| 47 | + break; |
| 48 | + case "stopping": |
| 49 | + throw new AssertionError("waiting until stopping state reached stopped state to start again"); |
| 50 | + case "starting": |
| 51 | + throw new AssertionError("waiting until starting state reached started state"); |
| 52 | + case "started": |
| 53 | + // all good here, we are done |
| 54 | + break; |
| 55 | + default: |
| 56 | + throw new AssertionError("unknown state[" + state + "]"); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + assertBusy(() -> { |
| 61 | + for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) { |
| 62 | + ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template", |
| 63 | + singletonMap("name", template), emptyList(), emptyMap()); |
| 64 | + assertThat(templateExistsResponse.getStatusCode(), is(200)); |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @After |
| 70 | + public void stopWatcher() throws Exception { |
| 71 | + assertBusy(() -> { |
| 72 | + ClientYamlTestResponse response = |
| 73 | + getAdminExecutionContext().callApi("xpack.watcher.stats", emptyMap(), emptyList(), emptyMap()); |
| 74 | + String state = (String) response.evaluate("stats.0.watcher_state"); |
| 75 | + |
| 76 | + switch (state) { |
| 77 | + case "stopped": |
| 78 | + // all good here, we are done |
| 79 | + break; |
| 80 | + case "stopping": |
| 81 | + throw new AssertionError("waiting until stopping state reached stopped state"); |
| 82 | + case "starting": |
| 83 | + throw new AssertionError("waiting until starting state reached started state to stop"); |
| 84 | + case "started": |
| 85 | + ClientYamlTestResponse stopResponse = |
| 86 | + getAdminExecutionContext().callApi("xpack.watcher.stop", emptyMap(), emptyList(), emptyMap()); |
| 87 | + boolean isAcknowledged = (boolean) stopResponse.evaluate("acknowledged"); |
| 88 | + assertThat(isAcknowledged, is(true)); |
| 89 | + break; |
| 90 | + default: |
| 91 | + throw new AssertionError("unknown state[" + state + "]"); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
0 commit comments