Test: Added helper method that checks if an alert has triggered

Original commit: elastic/x-pack-elasticsearch@351d26618f
This commit is contained in:
Martijn van Groningen 2014-11-07 13:45:48 +01:00
parent f2453f53ac
commit a4dbc7315a
3 changed files with 32 additions and 59 deletions

View File

@ -5,7 +5,12 @@
*/
package org.elasticsearch.alerts;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.actions.AlertActionState;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.common.bytes.BytesReference;
@ -18,8 +23,13 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.After;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
/**
*/
@ -80,4 +90,23 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
return internalCluster().getInstance(AlertsClient.class);
}
protected void assertAlertTriggered(final String alertName) throws Exception {
assertBusy(new Runnable() {
@Override
public void run() {
IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(AlertActionManager.ALERT_HISTORY_INDEX).get();
assertThat(indicesExistsResponse.isExists(), is(true));
SearchResponse searchResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
.setIndicesOptions(IndicesOptions.lenientExpandOpen())
.setQuery(boolQuery().must(termQuery("alert_name", alertName)).must(termQuery("state", AlertActionState.ACTION_PERFORMED.toString())))
.addField("response.hits.total")
.setSize(1)
.get();
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), equalTo(1));
}
}, 30, TimeUnit.SECONDS);
}
}

View File

@ -5,11 +5,7 @@
*/
package org.elasticsearch.alerts;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.actions.AlertActionState;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
@ -17,14 +13,10 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
/**
*/
@ -42,22 +34,7 @@ public class BasicAlertingTest extends AbstractAlertingTests {
alertsClient.prepareIndexAlert("my-first-alert")
.setAlertSource(alertSource)
.get();
assertBusy(new Runnable() {
@Override
public void run() {
IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(AlertActionManager.ALERT_HISTORY_INDEX).get();
assertThat(indicesExistsResponse.isExists(), is(true));
SearchResponse searchResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
.setQuery(termQuery("state", AlertActionState.ACTION_PERFORMED.toString()))
.addField("response.hits.total")
.setSize(1)
.get();
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), equalTo(1));
}
}, 30, TimeUnit.SECONDS);
assertAlertTriggered("my-first-alert");
}
@Test

View File

@ -6,11 +6,7 @@
package org.elasticsearch.alerts;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.actions.AlertActionState;
import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.cluster.ClusterState;
@ -25,9 +21,6 @@ import org.elasticsearch.discovery.zen.elect.ElectMasterService;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.hamcrest.Matchers.equalTo;
@ -60,22 +53,7 @@ public class NoMasterNodeTests extends AbstractAlertingTests {
alertsClient.prepareIndexAlert("my-first-alert")
.setAlertSource(alertSource)
.get();
assertBusy(new Runnable() {
@Override
public void run() {
IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(AlertActionManager.ALERT_HISTORY_INDEX).get();
assertThat(indicesExistsResponse.isExists(), is(true));
SearchResponse searchResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
.setQuery(termQuery("state", AlertActionState.ACTION_PERFORMED.toString()))
.addField("response.hits.total")
.setSize(1)
.get();
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), equalTo(1));
}
}, 30, TimeUnit.SECONDS);
assertAlertTriggered("my-first-alert");
// Stop the elected master, no new master will be elected b/c of m_m_n is set to 2
internalCluster().stopCurrentMasterNode();
@ -107,18 +85,7 @@ public class NoMasterNodeTests extends AbstractAlertingTests {
alertsClient.prepareIndexAlert("my-second-alert")
.setAlertSource(alertSource)
.get();
assertBusy(new Runnable() {
@Override
public void run() {
SearchResponse searchResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
.setQuery(boolQuery().must(termQuery("state", AlertActionState.ACTION_PERFORMED.toString())).must(termQuery("alert_name", "my-second-alert")))
.addField("response.hits.total")
.setSize(1)
.get();
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), equalTo(1));
}
}, 30, TimeUnit.SECONDS);
assertAlertTriggered("my-second-alert");
}
}