Fix tests

Original commit: elastic/x-pack-elasticsearch@3d76fda9d8
This commit is contained in:
Tanguy Leroux 2015-09-25 18:05:59 +02:00 committed by uboness
parent 047022ab0f
commit 00f8e40a99
14 changed files with 142 additions and 143 deletions

View File

@ -16,6 +16,7 @@ import org.elasticsearch.common.inject.Provider;
import org.elasticsearch.common.inject.ProvisionException;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.DiscoveryService;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.marvel.agent.collector.AbstractCollector;
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
@ -40,6 +41,7 @@ public class NodeStatsCollector extends AbstractCollector<NodeStatsCollector> {
private final NodeService nodeService;
private final DiscoveryService discoveryService;
private final NodeEnvironment nodeEnvironment;
// Use a provider in order to avoid Guice circular injection
// issues because AllocationDecider is not an interface and cannot be proxied
@ -47,14 +49,20 @@ public class NodeStatsCollector extends AbstractCollector<NodeStatsCollector> {
@Inject
public NodeStatsCollector(Settings settings, ClusterService clusterService, MarvelSettings marvelSettings, LicenseService licenseService,
NodeService nodeService, DiscoveryService discoveryService,
NodeService nodeService, DiscoveryService discoveryService, NodeEnvironment nodeEnvironment,
Provider<DiskThresholdDecider> diskThresholdDeciderProvider) {
super(settings, NAME, clusterService, marvelSettings, licenseService);
this.nodeService = nodeService;
this.discoveryService = discoveryService;
this.nodeEnvironment = nodeEnvironment;
this.diskThresholdDeciderProvider = diskThresholdDeciderProvider;
}
@Override
protected boolean canCollect() {
return super.canCollect() && nodeEnvironment.hasNodeFile();
}
@Override
protected Collection<MarvelDoc> doCollect() throws Exception {
List<MarvelDoc> results = new ArrayList<>(1);

View File

@ -48,13 +48,12 @@ public class AbstractCollectorTestCase extends MarvelIntegTestCase {
return Arrays.asList(LicensePluginForCollectors.class, MarvelPlugin.class);
}
@Override
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(Node.HTTP_ENABLED, false)
.put(MarvelSettings.INTERVAL, "60m")
.put(MarvelSettings.INTERVAL, "-1")
.build();
}

View File

@ -54,7 +54,9 @@ public class ClusterStateCollectorTests extends AbstractCollectorTestCase {
for (int i = 0; i < nbDocs; i++) {
client().prepareIndex("test", "test").setSource("num", i).get();
}
client().admin().indices().prepareRefresh().get();
securedFlush();
securedRefresh();
assertHitCount(client().prepareCount().get(), nbDocs);
Collection<MarvelDoc> results = newClusterStateCollector().doCollect();

View File

@ -14,7 +14,7 @@ import org.elasticsearch.marvel.agent.collector.AbstractCollectorTestCase;
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.license.LicenseService;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.junit.Test;
import java.util.Collection;
@ -28,7 +28,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcke
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.*;
@ESIntegTestCase.ClusterScope(numDataNodes = 0)
@ClusterScope(numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0.0)
public class IndexRecoveryCollectorTests extends AbstractCollectorTestCase {
private final boolean activeOnly = false;

View File

@ -11,6 +11,7 @@ import org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider
import org.elasticsearch.common.inject.Provider;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.DiscoveryService;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.marvel.agent.collector.AbstractCollectorTestCase;
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
@ -84,6 +85,7 @@ public class NodeStatsCollectorTests extends AbstractCollectorTestCase {
internalCluster().getInstance(LicenseService.class, nodeId),
internalCluster().getInstance(NodeService.class, nodeId),
internalCluster().getInstance(DiscoveryService.class, nodeId),
internalCluster().getInstance(NodeEnvironment.class, nodeId),
new Provider<DiskThresholdDecider>() {
@Override
public DiskThresholdDecider get() {

View File

@ -1,81 +0,0 @@
/*
* 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.marvel.agent.renderer;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.node.Node;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.junit.After;
import org.junit.Before;
import java.util.Collection;
import java.util.Map;
import static org.hamcrest.Matchers.*;
@ClusterScope(scope = ESIntegTestCase.Scope.TEST, randomDynamicTemplates = false, transportClientRatio = 0.0)
public abstract class AbstractRendererTestCase extends MarvelIntegTestCase {
@Override
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(Node.HTTP_ENABLED, true)
.put(MarvelSettings.STARTUP_DELAY, "3s")
.put(MarvelSettings.INTERVAL, "1s")
.put(MarvelSettings.COLLECTORS, Strings.collectionToCommaDelimitedString(collectors()))
.build();
}
@Before
public void init() throws Exception {
startCollection();
}
@After
public void cleanup() throws Exception {
stopCollection();
}
protected abstract Collection<String> collectors ();
/**
* Checks if a field exist in a map of values. If the field contains a dot like 'foo.bar'
* it checks that 'foo' exists in the map of values and that it points to a sub-map. Then
* it recurses to check if 'bar' exists in the sub-map.
*/
protected void assertContains(String field, Map<String, Object> values) {
assertNotNull("field name should not be null", field);
assertNotNull("values map should not be null", values);
int point = field.indexOf('.');
if (point > -1) {
assertThat(point, allOf(greaterThan(0), lessThan(field.length())));
String segment = field.substring(0, point);
assertTrue(Strings.hasText(segment));
boolean fieldExists = values.containsKey(segment);
assertTrue("expecting field [" + segment + "] to be present in marvel document", fieldExists);
Object value = values.get(segment);
String next = field.substring(point + 1);
if (next.length() > 0) {
assertTrue(value instanceof Map);
assertContains(next, (Map<String, Object>) value);
} else {
assertFalse(value instanceof Map);
}
} else {
assertTrue("expecting field [" + field + "] to be present in marvel document", values.containsKey(field));
}
}
}

View File

@ -8,15 +8,14 @@ package org.elasticsearch.marvel.agent.renderer.cluster;
import org.elasticsearch.Version;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.license.core.License;
import org.elasticsearch.marvel.agent.collector.cluster.ClusterInfoCollector;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
@ -25,15 +24,21 @@ import java.util.concurrent.TimeUnit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.*;
public class ClusterInfoIT extends AbstractRendererTestCase {
public class ClusterInfoIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(ClusterInfoCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, ClusterInfoCollector.NAME)
.build();
}
@Test
public void testClusterInfo() throws Exception {
ensureGreen();
final String clusterUUID = client().admin().cluster().prepareState().setMetaData(true).get().getState().metaData().clusterUUID();
assertTrue(Strings.hasText(clusterUUID));
@ -64,8 +69,9 @@ public class ClusterInfoIT extends AbstractRendererTestCase {
Object licensesList = source.get(ClusterInfoRenderer.Fields.LICENSES.underscore().toString());
assertThat(licensesList, instanceOf(List.class));
logger.warn("--> checking number of licenses [internal cluster:{}, shield enabled:{}]", isInternalCluster(), shieldEnabled);
List licenses = (List) licensesList;
assertThat(licenses.size(), equalTo(shieldEnabled ? 2 : 1));
assertThat(licenses.size(), equalTo(isInternalCluster() && shieldEnabled ? 2 : 1));
Map license = (Map) licenses.iterator().next();
assertThat(license, instanceOf(Map.class));

View File

@ -7,30 +7,36 @@ package org.elasticsearch.marvel.agent.renderer.cluster;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.marvel.agent.collector.cluster.ClusterStateCollector;
import org.elasticsearch.marvel.agent.exporter.http.HttpExporterUtils;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.greaterThan;
public class ClusterStateIT extends AbstractRendererTestCase {
public class ClusterStateIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(ClusterStateCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, ClusterStateCollector.NAME)
.build();
}
@Test
public void testClusterState() throws Exception {
ensureGreen();
awaitMarvelDocsCount(greaterThan(0L), ClusterStateCollector.TYPE);
logger.debug("--> searching for marvel documents of type [{}]", ClusterStateCollector.TYPE);
@ -59,6 +65,8 @@ public class ClusterStateIT extends AbstractRendererTestCase {
*/
@Test
public void testNoNodesIndexing() throws Exception {
ensureGreen();
logger.debug("--> forcing marvel's index template update");
assertAcked(client().admin().indices().preparePutTemplate("marvel").setSource(HttpExporterUtils.loadDefaultTemplate()).execute().actionGet());
@ -66,8 +74,8 @@ public class ClusterStateIT extends AbstractRendererTestCase {
deleteMarvelIndices();
logger.debug("--> checking for template existence");
assertMarvelTemplateInstalled();
awaitMarvelDocsCount(greaterThan(0L), ClusterStateCollector.TYPE);
assertMarvelTemplateInstalled();
logger.debug("--> searching for marvel documents of type [{}]", ClusterStateCollector.TYPE);
SearchResponse response = client().prepareSearch().setTypes(ClusterStateCollector.TYPE).get();

View File

@ -5,41 +5,74 @@
*/
package org.elasticsearch.marvel.agent.renderer.cluster;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsNodes;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.marvel.agent.collector.cluster.ClusterStatsCollector;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.greaterThan;
public class ClusterStatsIT extends AbstractRendererTestCase {
public class ClusterStatsIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(ClusterStatsCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, ClusterStatsCollector.NAME)
.build();
}
@Test
public void testClusterStats() throws Exception {
logger.debug("--> creating some indices so that cluster stats reports data about shards");
ensureGreen();
logger.debug("--> creating some indices so that every data nodes will at leats a shard");
ClusterStatsNodes.Counts counts = client().admin().cluster().prepareClusterStats().get().getNodesStats().getCounts();
assertThat(counts.getTotal(), greaterThan(0));
for (int i = 0; i < randomIntBetween(1, 5); i++) {
createIndex("test-" + i);
assertAcked(prepareCreate("test-" + i).setSettings(Settings.settingsBuilder()
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, counts.getDataOnly() + counts.getMasterData())
.build()));
index("test-" + i, "foo", "1", jsonBuilder().startObject().field("dummy_field", 1).endObject());
}
logger.debug("--> wait for cluster stats to report data about shards");
securedFlush();
securedRefresh();
logger.debug("--> waiting for cluster stats to report data for each node");
assertBusy(new Runnable() {
@Override
public void run() {
ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get();
assertNotNull(response.getIndicesStats().getShards());
assertThat(response.getIndicesStats().getShards().getTotal(), greaterThan(0));
NodesInfoResponse nodesInfoResponse = client().admin().cluster().prepareNodesInfo().get();
for (Map.Entry<String, NodeInfo> node : nodesInfoResponse.getNodesMap().entrySet()) {
// Checks that node has shards
ClusterStatsResponse clusterStatsResponse = client().admin().cluster().prepareClusterStats().setNodesIds(node.getKey()).get();
assertNotNull(clusterStatsResponse.getIndicesStats().getShards());
assertNotNull(clusterStatsResponse.getIndicesStats().getShards());
assertThat(clusterStatsResponse.getIndicesStats().getShards().getTotal(), greaterThan(0));
NodesStatsResponse nodeStatsResponse = client().admin().cluster().prepareNodesStats(node.getKey()).setFs(true).get();
for (NodeStats nodeStats : nodeStatsResponse) {
assertThat(nodeStats.getFs().total().getAvailable().bytes(), greaterThan(-1L));
}
}
}
}, 30L, TimeUnit.SECONDS);

View File

@ -6,22 +6,26 @@
package org.elasticsearch.marvel.agent.renderer.indices;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.marvel.agent.collector.indices.IndexRecoveryCollector;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.greaterThan;
public class IndexRecoveryIT extends AbstractRendererTestCase {
public class IndexRecoveryIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(IndexRecoveryCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, IndexRecoveryCollector.NAME)
.build();
}
@Test

View File

@ -7,23 +7,27 @@ package org.elasticsearch.marvel.agent.renderer.indices;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.marvel.agent.collector.indices.IndexStatsCollector;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.greaterThan;
public class IndexStatsIT extends AbstractRendererTestCase {
public class IndexStatsIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(IndexStatsCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, IndexStatsCollector.NAME)
.build();
}
@Test
@ -44,6 +48,8 @@ public class IndexStatsIT extends AbstractRendererTestCase {
}
}
ensureGreen();
awaitMarvelDocsCount(greaterThan(0L), IndexStatsCollector.TYPE);
logger.debug("--> wait for index stats collector to collect stat for each index");

View File

@ -7,22 +7,26 @@ package org.elasticsearch.marvel.agent.renderer.indices;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.marvel.agent.collector.indices.IndicesStatsCollector;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.greaterThan;
public class IndicesStatsIT extends AbstractRendererTestCase {
public class IndicesStatsIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(IndicesStatsCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, IndicesStatsCollector.NAME)
.build();
}
@Test

View File

@ -6,22 +6,26 @@
package org.elasticsearch.marvel.agent.renderer.node;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.marvel.agent.collector.node.NodeStatsCollector;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.greaterThan;
public class NodeStatsIT extends AbstractRendererTestCase {
public class NodeStatsIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(NodeStatsCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, NodeStatsCollector.NAME)
.build();
}
@Test

View File

@ -6,22 +6,26 @@
package org.elasticsearch.marvel.agent.renderer.shards;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.marvel.agent.collector.shards.ShardsCollector;
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTestCase;
import org.elasticsearch.marvel.agent.settings.MarvelSettings;
import org.elasticsearch.marvel.test.MarvelIntegTestCase;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.greaterThan;
public class ShardsIT extends AbstractRendererTestCase {
public class ShardsIT extends MarvelIntegTestCase {
@Override
protected Collection<String> collectors() {
return Collections.singletonList(ShardsCollector.NAME);
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(super.nodeSettings(nodeOrdinal))
.put(MarvelSettings.INTERVAL, "3s")
.put(MarvelSettings.COLLECTORS, ShardsCollector.NAME)
.build();
}
@Test