From 5491e8e4b35a95ed58545f05756625a5f4ad8009 Mon Sep 17 00:00:00 2001 From: Brian Murphy Date: Tue, 18 Nov 2014 11:17:00 +0000 Subject: [PATCH] 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@db497a6b519b8eedbc1e77a39991fe3ae9acd771 --- pom.xml | 1 + .../java/org/elasticsearch/alerts/Alert.java | 17 ++++- .../alerts/actions/EmailAlertAction.java | 21 +++++ .../alerts/TestAlertSerialization.java | 21 +++-- .../elasticsearch/alerts/TestBootStrap.java | 76 +++++++++++++++++++ .../alerts/actions/TestAlertStats.java | 60 +++++++++++++++ 6 files changed, 186 insertions(+), 10 deletions(-) create mode 100644 src/test/java/org/elasticsearch/alerts/TestBootStrap.java create mode 100644 src/test/java/org/elasticsearch/alerts/actions/TestAlertStats.java diff --git a/pom.xml b/pom.xml index 7585b6df7df..84e488f925f 100644 --- a/pom.xml +++ b/pom.xml @@ -259,6 +259,7 @@ **/*Tests.class **/*Test.class + **/Test*.class **/Abstract*.class diff --git a/src/main/java/org/elasticsearch/alerts/Alert.java b/src/main/java/org/elasticsearch/alerts/Alert.java index 2790915878e..dd0a58a9210 100644 --- a/src/main/java/org/elasticsearch/alerts/Alert.java +++ b/src/main/java/org/elasticsearch/alerts/Alert.java @@ -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; diff --git a/src/main/java/org/elasticsearch/alerts/actions/EmailAlertAction.java b/src/main/java/org/elasticsearch/alerts/actions/EmailAlertAction.java index 45a738faece..62a697c30f8 100644 --- a/src/main/java/org/elasticsearch/alerts/actions/EmailAlertAction.java +++ b/src/main/java/org/elasticsearch/alerts/actions/EmailAlertAction.java @@ -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; + } + } diff --git a/src/test/java/org/elasticsearch/alerts/TestAlertSerialization.java b/src/test/java/org/elasticsearch/alerts/TestAlertSerialization.java index fa47fc7d734..a3178760af5 100644 --- a/src/test/java/org/elasticsearch/alerts/TestAlertSerialization.java +++ b/src/test/java/org/elasticsearch/alerts/TestAlertSerialization.java @@ -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 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(), "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"); - } - } } diff --git a/src/test/java/org/elasticsearch/alerts/TestBootStrap.java b/src/test/java/org/elasticsearch/alerts/TestBootStrap.java new file mode 100644 index 00000000000..dcea5b84fda --- /dev/null +++ b/src/test/java/org/elasticsearch/alerts/TestBootStrap.java @@ -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)); + + } + +} diff --git a/src/test/java/org/elasticsearch/alerts/actions/TestAlertStats.java b/src/test/java/org/elasticsearch/alerts/actions/TestAlertStats.java new file mode 100644 index 00000000000..c7f682e8be4 --- /dev/null +++ b/src/test/java/org/elasticsearch/alerts/actions/TestAlertStats.java @@ -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)); + + } +}