Added base alerting test class

Original commit: elastic/x-pack-elasticsearch@a3d258318f
This commit is contained in:
Martijn van Groningen 2014-11-06 23:58:30 +01:00
parent 0f0436e128
commit 1dc9021fbe
2 changed files with 84 additions and 66 deletions

View File

@ -0,0 +1,83 @@
/*
* 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.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.After;
import java.io.IOException;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
*/
public class AbstractAlertingTests extends ElasticsearchIntegrationTest {
@Override
protected Settings nodeSettings(int nodeOrdinal) {
return ImmutableSettings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put("scroll.size", randomIntBetween(1, 100))
.put("plugin.mandatory", "alerts")
.put("plugin.types", AlertsPlugin.class.getName())
.put("node.mode", "network")
.put("http.enabled", true)
.put("plugins.load_classpath_plugins", false)
.build();
}
@After
public void clearAlerts() {
// Clear all in-memory alerts on all nodes, perhaps there isn't an elected master at this point
for (AlertManager manager : internalCluster().getInstances(AlertManager.class)) {
manager.clear();
}
}
protected BytesReference createAlertSource(String cron, SearchRequest request, String scriptTrigger) throws IOException {
XContentBuilder builder = jsonBuilder().startObject();
builder.field("schedule", cron);
builder.field("enable", true);
builder.startObject("request");
XContentHelper.writeRawField("body", request.source(), builder, ToXContent.EMPTY_PARAMS);
builder.startArray("indices");
for (String index : request.indices()) {
builder.value(index);
}
builder.endArray();
builder.endObject();
builder.startObject("trigger");
builder.startObject("script");
builder.field("script", scriptTrigger);
builder.endObject();
builder.endObject();
builder.startObject("actions");
builder.startObject("index");
builder.field("index", "my-index");
builder.field("type", "trail");
builder.endObject();
builder.endObject();
return builder.endObject().bytes();
}
protected AlertsClient alertClient() {
return internalCluster().getInstance(AlertsClient.class);
}
}

View File

@ -10,25 +10,15 @@ import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.alerts.actions.AlertActionManager; import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.actions.AlertActionState; import org.elasticsearch.alerts.actions.AlertActionState;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.client.AlertsClientInterface; import org.elasticsearch.alerts.client.AlertsClientInterface;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse; import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
@ -39,20 +29,7 @@ import static org.hamcrest.core.Is.is;
/** /**
*/ */
@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0) @ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numClientNodes = 0, transportClientRatio = 0)
public class BasicAlertingTest extends ElasticsearchIntegrationTest { public class BasicAlertingTest extends AbstractAlertingTests {
@Override
protected Settings nodeSettings(int nodeOrdinal) {
return ImmutableSettings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put("scroll.size", randomIntBetween(1, 100))
.put("plugin.mandatory", "alerts")
.put("plugin.types", AlertsPlugin.class.getName())
.put("node.mode", "network")
.put("http.enabled", true)
.put("plugins.load_classpath_plugins", false)
.build();
}
@Test @Test
public void testIndexAlert() throws Exception { public void testIndexAlert() throws Exception {
@ -103,46 +80,4 @@ public class BasicAlertingTest extends ElasticsearchIntegrationTest {
assertHitCount(client().prepareCount(AlertsStore.ALERT_INDEX).get(), 0l); assertHitCount(client().prepareCount(AlertsStore.ALERT_INDEX).get(), 0l);
} }
@After
public void clearAlerts() {
// Clear all in-memory alerts on all nodes, perhaps there isn't an elected master at this point
for (AlertManager manager : internalCluster().getInstances(AlertManager.class)) {
manager.clear();
}
}
private BytesReference createAlertSource(String cron, SearchRequest request, String scriptTrigger) throws IOException {
XContentBuilder builder = jsonBuilder().startObject();
builder.field("schedule", cron);
builder.field("enable", true);
builder.startObject("request");
XContentHelper.writeRawField("body", request.source(), builder, ToXContent.EMPTY_PARAMS);
builder.startArray("indices");
for (String index : request.indices()) {
builder.value(index);
}
builder.endArray();
builder.endObject();
builder.startObject("trigger");
builder.startObject("script");
builder.field("script", scriptTrigger);
builder.endObject();
builder.endObject();
builder.startObject("actions");
builder.startObject("index");
builder.field("index", "my-index");
builder.field("type", "trail");
builder.endObject();
builder.endObject();
return builder.endObject().bytes();
}
private AlertsClient alertClient() {
return internalCluster().getInstance(AlertsClient.class);
}
} }