Test: Added trigger search test
Core: Modified TriggerManager#prepareTriggerSearch to always produce a new instance. Original commit: elastic/x-pack-elasticsearch@4041898645
This commit is contained in:
parent
8b2c6ba39d
commit
d801537c9e
|
@ -119,18 +119,21 @@ public class TriggerManager extends AbstractComponent {
|
|||
}
|
||||
|
||||
private SearchRequest prepareTriggerSearch(Alert alert, DateTime scheduledFireTime, DateTime fireTime) throws IOException {
|
||||
SearchRequest request = alert.getSearchRequest();
|
||||
if (Strings.hasLength(request.source())) {
|
||||
String requestSource = XContentHelper.convertToJson(request.source(), false);
|
||||
SearchRequest triggerSearchRequest = new SearchRequest(alert.getSearchRequest())
|
||||
.indicesOptions(alert.getSearchRequest().indicesOptions())
|
||||
.indices(alert.getSearchRequest().indices());
|
||||
if (Strings.hasLength(alert.getSearchRequest().source())) {
|
||||
String requestSource = XContentHelper.convertToJson(alert.getSearchRequest().source(), false);
|
||||
if (requestSource.contains(fireTimePlaceHolder)) {
|
||||
requestSource = requestSource.replace(fireTimePlaceHolder, dateTimeFormatter.printer().print(fireTime));
|
||||
}
|
||||
if (requestSource.contains(scheduledFireTimePlaceHolder)) {
|
||||
requestSource = requestSource.replace(scheduledFireTimePlaceHolder, dateTimeFormatter.printer().print(scheduledFireTime));
|
||||
}
|
||||
request.source(requestSource);
|
||||
} else if (Strings.hasLength(request.templateSource())) {
|
||||
Tuple<XContentType, Map<String, Object>> tuple = XContentHelper.convertToMap(request.templateSource(), false);
|
||||
|
||||
triggerSearchRequest.source(requestSource);
|
||||
} else if (Strings.hasLength(alert.getSearchRequest().templateSource())) {
|
||||
Tuple<XContentType, Map<String, Object>> tuple = XContentHelper.convertToMap(alert.getSearchRequest().templateSource(), false);
|
||||
Map<String, Object> templateSourceAsMap = tuple.v2();
|
||||
Map<String, Object> templateObject = (Map<String, Object>) templateSourceAsMap.get("template");
|
||||
if (templateObject != null) {
|
||||
|
@ -140,17 +143,17 @@ public class TriggerManager extends AbstractComponent {
|
|||
|
||||
XContentBuilder builder = XContentFactory.contentBuilder(tuple.v1());
|
||||
builder.map(templateSourceAsMap);
|
||||
request.templateSource(builder.bytes(), false);
|
||||
triggerSearchRequest.templateSource(builder.bytes(), false);
|
||||
}
|
||||
} else if (request.templateName() != null) {
|
||||
MapBuilder<String, String> templateParams = MapBuilder.newMapBuilder(request.templateParams())
|
||||
} else if (alert.getSearchRequest().templateName() != null) {
|
||||
MapBuilder<String, String> templateParams = MapBuilder.newMapBuilder(alert.getSearchRequest().templateParams())
|
||||
.put("scheduled_fire_time", dateTimeFormatter.printer().print(scheduledFireTime))
|
||||
.put("fire_time", dateTimeFormatter.printer().print(fireTime));
|
||||
request.templateParams(templateParams.map());
|
||||
triggerSearchRequest.templateParams(templateParams.map());
|
||||
} else {
|
||||
throw new ElasticsearchIllegalStateException("Search requests needs either source, template source or template name");
|
||||
}
|
||||
return request;
|
||||
return triggerSearchRequest;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
|
||||
|
@ -125,7 +126,7 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
|
|||
.setSize(1)
|
||||
.get();
|
||||
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
|
||||
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), equalTo(1));
|
||||
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), greaterThanOrEqualTo(1));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ package org.elasticsearch.alerts;
|
|||
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.alerts.actions.AlertActionManager;
|
||||
import org.elasticsearch.alerts.client.AlertsClientInterface;
|
||||
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
|
||||
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
|
||||
|
@ -15,10 +16,14 @@ import org.elasticsearch.common.bytes.BytesReference;
|
|||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.rangeFilter;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
@ -94,7 +99,7 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
|||
client().prepareIndex("my-index", "my-type").setSource("field", "value").get();
|
||||
BytesReference alertSource = jsonBuilder().startObject()
|
||||
.field("schedule", "0/5 * * * * ? *")
|
||||
.startObject("trigger").startObject("script").field("script", "return trie").endObject().endObject()
|
||||
.startObject("trigger").startObject("script").field("script", "return true").endObject().endObject()
|
||||
.field("enable", true)
|
||||
.field("malformed_field", "x")
|
||||
.endObject().bytes();
|
||||
|
@ -115,4 +120,61 @@ public class BasicAlertingTest extends AbstractAlertingTests {
|
|||
// The alert index template the mapping is defined as strict
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTriggerSearch() throws Exception {
|
||||
assertAcked(prepareCreate("my-index")
|
||||
.addMapping("my-type", "_timestamp", "enabled=true", "event_type", "type=string"));
|
||||
|
||||
String alertName = "red-alert";
|
||||
long scheduleTimeInMs = 5000;
|
||||
List<SearchRequest> searchRequests = new ArrayList<>();
|
||||
searchRequests.add(
|
||||
new SearchRequest("my-index")
|
||||
.source(searchSource().query(
|
||||
filteredQuery(matchQuery("event_type", "a"), rangeFilter("_timestamp").from("<<<SCHEDULED_FIRE_TIME>>>||-30s").to("<<<SCHEDULED_FIRE_TIME>>>"))
|
||||
)
|
||||
)
|
||||
// TODO: add template based search requests
|
||||
);
|
||||
|
||||
for (SearchRequest request : searchRequests) {
|
||||
alertClient().prepareDeleteAlert(alertName).get();
|
||||
alertClient().prepareIndexAlert(alertName)
|
||||
.setAlertSource(createAlertSource(String.format("0/%s * * * * ? *", (scheduleTimeInMs / 1000)), request, "return hits.total >= 3"))
|
||||
.get();
|
||||
|
||||
long time1 = System.currentTimeMillis();
|
||||
client().prepareIndex("my-index", "my-type")
|
||||
.setCreate(true)
|
||||
.setSource("event_type", "a")
|
||||
.get();
|
||||
client().prepareIndex("my-index", "my-type")
|
||||
.setCreate(true)
|
||||
.setSource("event_type", "a")
|
||||
.get();
|
||||
long timeLeft = scheduleTimeInMs - (System.currentTimeMillis() - time1);
|
||||
Thread.sleep(timeLeft);
|
||||
assertNoAlertTrigger(alertName);
|
||||
cluster().wipeIndices(AlertActionManager.ALERT_HISTORY_INDEX);
|
||||
|
||||
time1 = System.currentTimeMillis();
|
||||
client().prepareIndex("my-index", "my-type")
|
||||
.setCreate(true)
|
||||
.setSource("event_type", "b")
|
||||
.get();
|
||||
timeLeft = scheduleTimeInMs - (System.currentTimeMillis() - time1);
|
||||
Thread.sleep(timeLeft);
|
||||
assertNoAlertTrigger(alertName);
|
||||
|
||||
time1 = System.currentTimeMillis();
|
||||
client().prepareIndex("my-index", "my-type")
|
||||
.setCreate(true)
|
||||
.setSource("event_type", "a")
|
||||
.get();
|
||||
timeLeft = scheduleTimeInMs - (System.currentTimeMillis() - time1);
|
||||
Thread.sleep(timeLeft);
|
||||
assertAlertTriggered(alertName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue