Test: Fixed and cleanup bootstrap test

Original commit: elastic/x-pack-elasticsearch@7455bd8c00
This commit is contained in:
Martijn van Groningen 2014-11-19 21:27:51 +01:00
parent 19622d4f0b
commit c2e4fe375d
2 changed files with 15 additions and 44 deletions

View File

@ -141,13 +141,13 @@ public class AlertActionManager extends AbstractComponent {
public void loadQueue() { public void loadQueue() {
client.admin().indices().refresh(new RefreshRequest(ALERT_HISTORY_INDEX)).actionGet(); client.admin().indices().refresh(new RefreshRequest(ALERT_HISTORY_INDEX)).actionGet();
SearchResponse response = client.prepareSearch() SearchResponse response = client.prepareSearch(ALERT_HISTORY_INDEX)
.setQuery(QueryBuilders.termQuery(AlertActionState.FIELD_NAME, AlertActionState.SEARCH_NEEDED.toString())) .setQuery(QueryBuilders.termQuery(AlertActionState.FIELD_NAME, AlertActionState.SEARCH_NEEDED.toString()))
.setSearchType(SearchType.SCAN) .setSearchType(SearchType.SCAN)
.setScroll(scrollTimeout) .setScroll(scrollTimeout)
.setSize(scrollSize) .setSize(scrollSize)
.setTypes(ALERT_HISTORY_TYPE) .setTypes(ALERT_HISTORY_TYPE)
.setIndices(ALERT_HISTORY_INDEX).get(); .get();
try { try {
if (response.getHits().getTotalHits() > 0) { if (response.getHits().getTotalHits() > 0) {
response = client.prepareSearchScroll(response.getScrollId()).setScroll(scrollTimeout).get(); response = client.prepareSearchScroll(response.getScrollId()).setScroll(scrollTimeout).get();

View File

@ -6,7 +6,7 @@
package org.elasticsearch.alerts; package org.elasticsearch.alerts;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.alerts.actions.AlertAction; import org.elasticsearch.alerts.actions.AlertAction;
@ -26,7 +26,6 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery;
@ -35,30 +34,21 @@ import static org.hamcrest.core.IsEqual.equalTo;
/** /**
*/ */
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.TEST, numClientNodes = 0, transportClientRatio = 0, numDataNodes = 3) @ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0)
public class BootStrapTest extends AbstractAlertingTests { public class BootStrapTest extends AbstractAlertingTests {
@Test @Test
public void testBootStrapAlerts() throws Exception { public void testBootStrapAlerts() throws Exception {
ensureGreen(); ensureAlertingStarted();
SearchRequest searchRequest = createTriggerSearchRequest("my-index").source(searchSource().query(termQuery("field", "value"))); SearchRequest searchRequest = createTriggerSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
BytesReference alertSource = createAlertSource("0 0/5 * * * ? *", searchRequest, "hits.total == 1"); BytesReference alertSource = createAlertSource("0 0/5 * * * ? *", searchRequest, "hits.total == 1");
alertClient().prepareIndexAlert("my-first-alert") client().prepareIndex(AlertsStore.ALERT_INDEX, AlertsStore.ALERT_TYPE, "my-first-alert")
.setAlertSource(alertSource) .setSource(alertSource)
.setConsistencyLevel(WriteConsistencyLevel.ALL)
.get(); .get();
AlertsStatsRequest alertsStatsRequest = alertClient().prepareAlertsStats().request();
AlertsStatsResponse response = alertClient().alertsStats(alertsStatsRequest).actionGet();
assertTrue(response.isAlertActionManagerStarted());
assertTrue(response.isAlertManagerStarted());
assertThat(response.getNumberOfRegisteredAlerts(), equalTo(1L));
refresh();
String oldMaster = internalTestCluster().getMasterName(); String oldMaster = internalTestCluster().getMasterName();
try { try {
internalTestCluster().stopCurrentMasterNode(); internalTestCluster().stopCurrentMasterNode();
} catch (IOException ioe) { } catch (IOException ioe) {
@ -70,12 +60,11 @@ public class BootStrapTest extends AbstractAlertingTests {
Thread.sleep(maxTime.getMillis()); Thread.sleep(maxTime.getMillis());
String newMaster = internalTestCluster().getMasterName(); String newMaster = internalTestCluster().getMasterName();
assertFalse(newMaster.equals(oldMaster)); assertFalse(newMaster.equals(oldMaster));
logger.info("Switched master from [{}] to [{}]",oldMaster,newMaster); logger.info("Switched master from [{}] to [{}]",oldMaster,newMaster);
alertsStatsRequest = alertClient().prepareAlertsStats().request(); AlertsStatsRequest alertsStatsRequest = alertClient().prepareAlertsStats().request();
response = alertClient().alertsStats(alertsStatsRequest).actionGet(); AlertsStatsResponse response = alertClient().alertsStats(alertsStatsRequest).actionGet();
assertTrue(response.isAlertActionManagerStarted()); assertTrue(response.isAlertActionManagerStarted());
assertTrue(response.isAlertManagerStarted()); assertTrue(response.isAlertManagerStarted());
@ -85,7 +74,8 @@ public class BootStrapTest extends AbstractAlertingTests {
@Test @Test
public void testBootStrapHistory() throws Exception { public void testBootStrapHistory() throws Exception {
ensureGreen(); ensureAlertingStarted();
internalTestCluster().ensureAtLeastNumDataNodes(2);
SearchRequest searchRequest = createTriggerSearchRequest("my-index").source(searchSource().query(termQuery("field", "value"))); SearchRequest searchRequest = createTriggerSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
AlertsStatsRequest alertsStatsRequest = alertClient().prepareAlertsStats().request(); AlertsStatsRequest alertsStatsRequest = alertClient().prepareAlertsStats().request();
@ -104,31 +94,14 @@ public class BootStrapTest extends AbstractAlertingTests {
0, 0,
true ); true );
AlertActionEntry entry = AlertActionEntry entry = new AlertActionEntry(alert, new DateTime(), new DateTime(), AlertActionState.SEARCH_NEEDED);
new AlertActionEntry(alert, IndexResponse indexResponse = client().prepareIndex(AlertActionManager.ALERT_HISTORY_INDEX, AlertActionManager.ALERT_HISTORY_TYPE, entry.getId())
new DateTime(), .setConsistencyLevel(WriteConsistencyLevel.ALL)
new DateTime(),
AlertActionState.SEARCH_NEEDED);
IndexResponse indexResponse = client().prepareIndex().setIndex(AlertActionManager.ALERT_HISTORY_INDEX).setType(AlertActionManager.ALERT_HISTORY_TYPE).setId(entry.getId())
.setSource(XContentFactory.jsonBuilder().value(entry)) .setSource(XContentFactory.jsonBuilder().value(entry))
.get(); .get();
assertTrue(indexResponse.isCreated()); assertTrue(indexResponse.isCreated());
GetResponse getResponse = client().prepareGet(AlertActionManager.ALERT_HISTORY_INDEX, AlertActionManager.ALERT_HISTORY_TYPE, entry.getId()).get();
assertTrue(getResponse.isExists());
assertEquals(getResponse.getId(), entry.getId());
logger.info("Successfully indexed [{}]", entry.getId());
Map<String,Object> responseMap = getResponse.getSourceAsMap();
logger.info("State [{}]", responseMap.get(AlertActionState.FIELD_NAME) );
//client().admin().indices().prepareRefresh(AlertActionManager.ALERT_HISTORY_INDEX).get();
String oldMaster = internalTestCluster().getMasterName(); String oldMaster = internalTestCluster().getMasterName();
try { try {
internalTestCluster().stopCurrentMasterNode(); internalTestCluster().stopCurrentMasterNode();
} catch (IOException ioe) { } catch (IOException ioe) {
@ -140,7 +113,6 @@ public class BootStrapTest extends AbstractAlertingTests {
Thread.sleep(maxTime.getMillis()); Thread.sleep(maxTime.getMillis());
String newMaster = internalTestCluster().getMasterName(); String newMaster = internalTestCluster().getMasterName();
assertFalse(newMaster.equals(oldMaster)); assertFalse(newMaster.equals(oldMaster));
logger.info("Switched master from [{}] to [{}]",oldMaster,newMaster); logger.info("Switched master from [{}] to [{}]",oldMaster,newMaster);
@ -152,7 +124,6 @@ public class BootStrapTest extends AbstractAlertingTests {
assertThat(response.getNumberOfRegisteredAlerts(), equalTo(0L)); assertThat(response.getNumberOfRegisteredAlerts(), equalTo(0L));
assertThat(response.getAlertActionManagerLargestQueueSize(), equalTo(1L)); assertThat(response.getAlertActionManagerLargestQueueSize(), equalTo(1L));
} }
} }