Test: assertAlertTrigger should wait for active shards in alert history index.

Original commit: elastic/x-pack-elasticsearch@bdf6370aee
This commit is contained in:
Martijn van Groningen 2014-11-07 19:59:44 +01:00
parent c9eae998b5
commit c081a9b62c
1 changed files with 7 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.actions.AlertActionState;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.plugin.AlertsPlugin;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
@ -23,11 +24,10 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.After;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
@ -94,19 +94,22 @@ public abstract class AbstractAlertingTests extends ElasticsearchIntegrationTest
assertBusy(new Runnable() {
@Override
public void run() {
// The alerthistory index gets created in the background when the first alert fires, so we to check first is this index is created and shards are started
IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(AlertActionManager.ALERT_HISTORY_INDEX).get();
assertThat(indicesExistsResponse.isExists(), is(true));
ClusterState state = client().admin().cluster().prepareState().get().getState();
assertThat(state.getRoutingTable().index(AlertActionManager.ALERT_HISTORY_INDEX).allPrimaryShardsActive(), is(true));
SearchResponse searchResponse = client().prepareSearch(AlertActionManager.ALERT_HISTORY_INDEX)
.setIndicesOptions(IndicesOptions.lenientExpandOpen())
.setQuery(boolQuery().must(termQuery("alert_name", alertName)).must(termQuery("state", AlertActionState.ACTION_PERFORMED.toString())))
.setQuery(boolQuery().must(matchQuery("alert_name", alertName)).must(matchQuery("state", AlertActionState.ACTION_PERFORMED.toString())))
.addField("response.hits.total")
.setSize(1)
.get();
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
assertThat((Integer) searchResponse.getHits().getAt(0).field("response.hits.total").getValue(), equalTo(1));
}
}, 30, TimeUnit.SECONDS);
});
}
}