TESTS : Add stats test and bootstrap test

This commit adds tests for the stats API along with a bootstrap test.
The bootstrap test is currently failing outside of a debugger for me so I'm digging into it.

Original commit: elastic/x-pack-elasticsearch@db497a6b51
This commit is contained in:
Brian Murphy 2014-11-18 11:17:00 +00:00
parent 43043ce3ce
commit 5491e8e4b3
6 changed files with 186 additions and 10 deletions

View File

@ -259,6 +259,7 @@
<includes>
<include>**/*Tests.class</include>
<include>**/*Test.class</include>
<include>**/Test*.class</include>
</includes>
<excludes>
<exclude>**/Abstract*.class</exclude>

View File

@ -169,10 +169,23 @@ public class Alert implements ToXContent {
if (version != alert.version) return false;
if (!actions.equals(alert.actions)) return false;
if (!alertName.equals(alert.alertName)) return false;
if (lastActionFire != null ? !lastActionFire.equals(alert.lastActionFire) : alert.lastActionFire != null)
if ( (lastActionFire == null || alert.lastActionFire == null)) {
if (lastActionFire != alert.lastActionFire) {
return false;
}
} else if (lastActionFire.getMillis() != alert.lastActionFire.getMillis()) {
return false;
}
if (!schedule.equals(alert.schedule)) return false;
if (!searchRequest.equals(alert.searchRequest)) return false;
if ( (searchRequest.source() == null || alert.searchRequest.source() == null)) {
if (searchRequest.source() != alert.searchRequest.source()) {
return false;
}
} else if (!searchRequest.source().equals(alert.searchRequest.source())) {
return false;
}
if (!trigger.equals(alert.trigger)) return false;
return true;

View File

@ -65,4 +65,25 @@ public class EmailAlertAction implements AlertAction {
return builder;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EmailAlertAction that = (EmailAlertAction) o;
if (displayField != null ? !displayField.equals(that.displayField) : that.displayField != null) return false;
if (emailAddresses != null ? !emailAddresses.equals(that.emailAddresses) : that.emailAddresses != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = displayField != null ? displayField.hashCode() : 0;
result = 31 * result + (emailAddresses != null ? emailAddresses.hashCode() : 0);
return result;
}
}

View File

@ -5,32 +5,41 @@
*/
package org.elasticsearch.alerts;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.alerts.actions.AlertAction;
import org.elasticsearch.alerts.actions.EmailAlertAction;
import org.elasticsearch.alerts.triggers.ScriptedTrigger;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestAlertSerialization extends ElasticsearchIntegrationTest {
@Test
public void testAlertSerialization() throws Exception {
SearchRequest request = new SearchRequest();
request.indices("my-index");
List<AlertAction> actions = new ArrayList<>();
actions.add(new EmailAlertAction("message", "foo@bar.com"));
Alert alert = new Alert("test-serialization",
new SearchRequest(),
new ScriptedTrigger("return true", null, null),
new ArrayList<AlertAction>(), "0/5 * * * * ? *",
request,
new ScriptedTrigger("return true", ScriptService.ScriptType.INLINE, "groovy"),
actions,
"0/5 * * * * ? *",
new DateTime(),
0,
false);
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
alert.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
@ -42,10 +51,6 @@ public class TestAlertSerialization extends ElasticsearchIntegrationTest {
logger.error(XContentHelper.convertToJson(jsonBuilder.bytes(),false,true));
if (true) {
throw new ElasticsearchException("foobarbaz");
}
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.alerts;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsRequest;
import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsResponse;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.hamcrest.core.IsEqual.equalTo;
/**
*/
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0, numDataNodes = 3)
public class TestBootStrap extends AbstractAlertingTests {
@Test
@LuceneTestCase.AwaitsFix(bugUrl = "This test fails but shouldn't")
public void testBootStrapAlerts() throws Exception {
ensureGreen();
SearchRequest searchRequest = createTriggerSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
BytesReference alertSource = createAlertSource("0 0/5 * * * ? *", searchRequest, "hits.total == 1");
alertClient().prepareIndexAlert("my-first-alert")
.setAlertSource(alertSource)
.get();
AlertsStatsRequest alertsStatsRequest = alertClient().prepareAlertsStats().request();
AlertsStatsResponse response = alertClient().alertsStats(alertsStatsRequest).actionGet();
assertTrue(response.isAlertActionManagerStarted());
assertTrue(response.isAlertManagerStarted());
assertThat(response.getNumberOfRegisteredAlerts(), equalTo(1L));
String oldMaster = internalTestCluster().getMasterName();
try {
internalTestCluster().stopCurrentMasterNode();
} catch (IOException ioe) {
throw new ElasticsearchException("Failed to stop current master", ioe);
}
//Wait for alerts to start
TimeValue maxTime = new TimeValue(30, TimeUnit.SECONDS);
Thread.sleep(maxTime.getMillis());
String newMaster = internalTestCluster().getMasterName();
assertFalse(newMaster.equals(oldMaster));
logger.info("Switched master from [{}] to [{}]",oldMaster,newMaster);
alertsStatsRequest = alertClient().prepareAlertsStats().request();
response = alertClient().alertsStats(alertsStatsRequest).actionGet();
assertTrue(response.isAlertActionManagerStarted());
assertTrue(response.isAlertManagerStarted());
assertThat(response.getNumberOfRegisteredAlerts(), equalTo(1L));
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.alerts.actions;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.alerts.AbstractAlertingTests;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsRequest;
import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsResponse;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.hamcrest.core.IsEqual.equalTo;
/**
*/
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0)
public class TestAlertStats extends AbstractAlertingTests {
@Test
public void testStartedStats() throws Exception {
AlertsStatsRequest alertsStatsRequest = alertClient().prepareAlertsStats().request();
AlertsStatsResponse response = alertClient().alertsStats(alertsStatsRequest).actionGet();
assertTrue(response.isAlertActionManagerStarted());
assertTrue(response.isAlertManagerStarted());
assertThat(response.getAlertActionManagerQueueSize(), equalTo(0L));
assertThat(response.getNumberOfRegisteredAlerts(), equalTo(0L));
}
@Test
public void testAlertCountStats() throws Exception {
AlertsClient alertsClient = alertClient();
AlertsStatsRequest alertsStatsRequest = alertsClient.prepareAlertsStats().request();
AlertsStatsResponse response = alertsClient.alertsStats(alertsStatsRequest).actionGet();
assertTrue(response.isAlertActionManagerStarted());
assertTrue(response.isAlertManagerStarted());
SearchRequest searchRequest = createTriggerSearchRequest("my-index").source(searchSource().query(termQuery("field", "value")));
BytesReference alertSource = createAlertSource("0/5 * * * * ? *", searchRequest, "hits.total == 1");
alertClient().prepareIndexAlert("testAlert")
.setAlertSource(alertSource)
.get();
response = alertClient().alertsStats(alertsStatsRequest).actionGet();
assertTrue(response.isAlertActionManagerStarted());
assertTrue(response.isAlertManagerStarted());
assertThat(response.getNumberOfRegisteredAlerts(), equalTo(1L));
}
}