Marvel: Add Cluster Stats tests
Original commit: elastic/x-pack-elasticsearch@d6eb8160d0
This commit is contained in:
parent
ae3c569bea
commit
b8da63deba
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* 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.collector.cluster;
|
||||
|
||||
import org.elasticsearch.cluster.ClusterName;
|
||||
import org.elasticsearch.cluster.ClusterService;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
|
||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
public class ClusterStatsCollectorTests extends ElasticsearchIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testClusterStatsCollector() throws Exception {
|
||||
Collection<MarvelDoc> results = newClusterStatsCollector().doCollect();
|
||||
assertThat(results, hasSize(1));
|
||||
|
||||
MarvelDoc marvelDoc = results.iterator().next();
|
||||
assertNotNull(marvelDoc);
|
||||
assertThat(marvelDoc, instanceOf(ClusterStatsMarvelDoc.class));
|
||||
|
||||
ClusterStatsMarvelDoc clusterStatsMarvelDoc = (ClusterStatsMarvelDoc) marvelDoc;
|
||||
assertThat(clusterStatsMarvelDoc.clusterName(), equalTo(client().admin().cluster().prepareHealth().get().getClusterName()));
|
||||
assertThat(clusterStatsMarvelDoc.timestamp(), greaterThan(0L));
|
||||
assertThat(clusterStatsMarvelDoc.type(), equalTo(ClusterStatsCollector.TYPE));
|
||||
|
||||
ClusterStatsMarvelDoc.Payload payload = clusterStatsMarvelDoc.payload();
|
||||
assertNotNull(payload);
|
||||
assertNotNull(payload.getClusterStats());
|
||||
assertThat(payload.getClusterStats().getNodesStats().getCounts().getTotal(), equalTo(internalCluster().getNodeNames().length));
|
||||
}
|
||||
|
||||
private ClusterStatsCollector newClusterStatsCollector() {
|
||||
return new ClusterStatsCollector(internalCluster().getInstance(Settings.class), internalCluster().getInstance(ClusterService.class),
|
||||
internalCluster().getInstance(ClusterName.class), client());
|
||||
}
|
||||
}
|
|
@ -1,92 +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.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@Ignore
|
||||
public abstract class AbstractRendererTests extends ElasticsearchTestCase {
|
||||
|
||||
@Test
|
||||
public void testSample() throws IOException {
|
||||
logger.debug("--> creating the sample document");
|
||||
MarvelDoc marvelDoc = newMarvelDoc();
|
||||
assertNotNull(marvelDoc);
|
||||
|
||||
logger.debug("--> creating the renderer");
|
||||
Renderer renderer = newRenderer();
|
||||
assertNotNull(renderer);
|
||||
|
||||
try (BytesStreamOutput os = new BytesStreamOutput()) {
|
||||
logger.debug("--> rendering the document");
|
||||
renderer.render(marvelDoc, XContentType.JSON, os);
|
||||
|
||||
String sample = sampleFilePath();
|
||||
assertTrue(Strings.hasText(sample));
|
||||
|
||||
logger.debug("--> loading expected document from file {}", sample);
|
||||
String expected = Streams.copyToStringFromClasspath(sample);
|
||||
|
||||
logger.debug("--> comparing both document, they must be identical");
|
||||
assertContent(os.bytes(), expected);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Renderer newRenderer();
|
||||
|
||||
protected abstract MarvelDoc newMarvelDoc();
|
||||
|
||||
protected abstract String sampleFilePath();
|
||||
|
||||
protected void assertContent(BytesReference result, String expected) {
|
||||
assertNotNull(result);
|
||||
assertNotNull(expected);
|
||||
|
||||
try {
|
||||
XContentParser resultParser = XContentFactory.xContent(result).createParser(result);
|
||||
XContentParser expectedParser = XContentFactory.xContent(expected).createParser(expected);
|
||||
|
||||
while (true) {
|
||||
XContentParser.Token token1 = resultParser.nextToken();
|
||||
XContentParser.Token token2 = expectedParser.nextToken();
|
||||
if (token1 == null) {
|
||||
assertThat(token2, nullValue());
|
||||
return;
|
||||
}
|
||||
assertThat(token1, Matchers.equalTo(token2));
|
||||
switch (token1) {
|
||||
case FIELD_NAME:
|
||||
assertThat(resultParser.currentName(), Matchers.equalTo(expectedParser.currentName()));
|
||||
break;
|
||||
case VALUE_STRING:
|
||||
assertThat(resultParser.text(), Matchers.equalTo(expectedParser.text()));
|
||||
break;
|
||||
case VALUE_NUMBER:
|
||||
assertThat(resultParser.numberType(), Matchers.equalTo(expectedParser.numberType()));
|
||||
assertThat(resultParser.numberValue(), Matchers.equalTo(expectedParser.numberValue()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
fail("Fail to verify the result of the renderer: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* 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.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class RendererTestUtils {
|
||||
|
||||
/**
|
||||
* Verifies that two JSON documents have the same structure/fields once parsed.
|
||||
* This method does not verify that fields values are identical.
|
||||
*/
|
||||
public static void assertJSONStructure(String result, String expected) {
|
||||
assertContent(result, expected, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that two JSON documents are identical once parsed.
|
||||
* This method verifies that fields values are the same.
|
||||
*/
|
||||
public static void assertJSONStructureAndValues(String result, String expected) {
|
||||
assertContent(result, expected, false);
|
||||
}
|
||||
|
||||
private static void assertContent(String result, String expected, boolean verifyValues) {
|
||||
assertNotNull(result);
|
||||
assertNotNull(expected);
|
||||
|
||||
try {
|
||||
XContentParser resultParser = XContentFactory.xContent(result).createParser(result);
|
||||
XContentParser expectedParser = XContentFactory.xContent(expected).createParser(expected);
|
||||
|
||||
while (true) {
|
||||
XContentParser.Token token1 = resultParser.nextToken();
|
||||
XContentParser.Token token2 = expectedParser.nextToken();
|
||||
if (token1 == null) {
|
||||
assertThat(token2, nullValue());
|
||||
return;
|
||||
}
|
||||
assertThat(token1, Matchers.equalTo(token2));
|
||||
switch (token1) {
|
||||
case FIELD_NAME:
|
||||
assertThat("field name for property '" + resultParser.currentName() + "' must be identical",
|
||||
resultParser.currentName(), Matchers.equalTo(expectedParser.currentName()));
|
||||
break;
|
||||
case VALUE_STRING:
|
||||
if (verifyValues) {
|
||||
assertThat("string value for property '" + resultParser.currentName() + "' must be identical",
|
||||
resultParser.text(), Matchers.equalTo(expectedParser.text()));
|
||||
} else {
|
||||
assertThat("string value for property '" + resultParser.currentName() + "' must be empty or non empty",
|
||||
Strings.hasLength(resultParser.text()), Matchers.equalTo(Strings.hasLength(expectedParser.text())));
|
||||
}
|
||||
break;
|
||||
case VALUE_NUMBER:
|
||||
assertThat("numeric type for property '" + resultParser.currentName() + "' must be identical",
|
||||
resultParser.numberType(), Matchers.equalTo(expectedParser.numberType()));
|
||||
if (verifyValues) {
|
||||
assertThat("numeric value for property '" + resultParser.currentName() + "' must be identical",
|
||||
resultParser.numberValue(), Matchers.equalTo(expectedParser.numberValue()));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
fail("Fail to verify the result of the renderer: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String renderAsJSON(MarvelDoc marvelDoc, Renderer renderer) throws IOException {
|
||||
assertNotNull(marvelDoc);
|
||||
assertNotNull(renderer);
|
||||
|
||||
try (BytesStreamOutput os = new BytesStreamOutput()) {
|
||||
renderer.render(marvelDoc, XContentType.JSON, os);
|
||||
return os.bytes().toUtf8();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* 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.cluster;
|
||||
|
||||
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
|
||||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.marvel.agent.collector.cluster.ClusterStatsMarvelDoc;
|
||||
import org.elasticsearch.marvel.agent.renderer.Renderer;
|
||||
import org.elasticsearch.marvel.agent.renderer.RendererTestUtils;
|
||||
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ClusterStatsRendererTests extends ElasticsearchSingleNodeTest {
|
||||
|
||||
private static final String SAMPLE_FILE = "/samples/marvel_cluster_stats.json";
|
||||
|
||||
@Test
|
||||
public void testClusterStatsRenderer() throws Exception {
|
||||
createIndex("index-0");
|
||||
|
||||
logger.debug("--> retrieving cluster stats response");
|
||||
ClusterStatsResponse clusterStats = client().admin().cluster().prepareClusterStats().get();
|
||||
|
||||
logger.debug("--> creating the cluster stats marvel document");
|
||||
ClusterStatsMarvelDoc marvelDoc = ClusterStatsMarvelDoc.createMarvelDoc("test", "marvel_cluster_stats", 1437580442979L, clusterStats);
|
||||
|
||||
logger.debug("--> rendering the document");
|
||||
Renderer renderer = new ClusterStatsRenderer();
|
||||
String result = RendererTestUtils.renderAsJSON(marvelDoc, renderer);
|
||||
|
||||
logger.debug("--> loading sample document from file {}", SAMPLE_FILE);
|
||||
String expected = Streams.copyToStringFromClasspath(SAMPLE_FILE);
|
||||
|
||||
logger.debug("--> comparing both documents, they must have the same structure");
|
||||
RendererTestUtils.assertJSONStructure(result, expected);
|
||||
}
|
||||
}
|
|
@ -8,24 +8,24 @@ package org.elasticsearch.marvel.agent.renderer.indices;
|
|||
import org.elasticsearch.action.admin.indices.stats.CommonStats;
|
||||
import org.elasticsearch.action.admin.indices.stats.IndexStats;
|
||||
import org.elasticsearch.action.admin.indices.stats.ShardStats;
|
||||
import org.elasticsearch.common.io.Streams;
|
||||
import org.elasticsearch.index.indexing.IndexingStats;
|
||||
import org.elasticsearch.index.shard.DocsStats;
|
||||
import org.elasticsearch.index.store.StoreStats;
|
||||
import org.elasticsearch.marvel.agent.collector.indices.IndexStatsMarvelDoc;
|
||||
import org.elasticsearch.marvel.agent.exporter.MarvelDoc;
|
||||
import org.elasticsearch.marvel.agent.renderer.AbstractRendererTests;
|
||||
import org.elasticsearch.marvel.agent.renderer.Renderer;
|
||||
import org.elasticsearch.marvel.agent.renderer.RendererTestUtils;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IndexStatsRendererTests extends AbstractRendererTests {
|
||||
public class IndexStatsRendererTests extends ElasticsearchTestCase {
|
||||
|
||||
@Override
|
||||
protected Renderer newRenderer() {
|
||||
return new IndexStatsRenderer();
|
||||
}
|
||||
private static final String SAMPLE_FILE = "/samples/marvel_index_stats.json";
|
||||
|
||||
@Override
|
||||
protected MarvelDoc newMarvelDoc() {
|
||||
return IndexStatsMarvelDoc.createMarvelDoc("test", "marvel_index_stats", 1437580442979L,
|
||||
@Test
|
||||
public void testIndexStatsRenderer() throws Exception {
|
||||
logger.debug("--> creating the cluster stats marvel document");
|
||||
IndexStatsMarvelDoc marvelDoc = IndexStatsMarvelDoc.createMarvelDoc("test", "marvel_index_stats", 1437580442979L,
|
||||
new IndexStats("index-0", new ShardStats[0]) {
|
||||
@Override
|
||||
public CommonStats getTotal() {
|
||||
|
@ -46,10 +46,15 @@ public class IndexStatsRendererTests extends AbstractRendererTests {
|
|||
return stats;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String sampleFilePath() {
|
||||
return "/samples/index_stats.json";
|
||||
logger.debug("--> rendering the document");
|
||||
Renderer renderer = new IndexStatsRenderer();
|
||||
String result = RendererTestUtils.renderAsJSON(marvelDoc, renderer);
|
||||
|
||||
logger.debug("--> loading sample document from file {}", SAMPLE_FILE);
|
||||
String expected = Streams.copyToStringFromClasspath(SAMPLE_FILE);
|
||||
|
||||
logger.debug("--> comparing both documents, they must be identical");
|
||||
RendererTestUtils.assertJSONStructureAndValues(result, expected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue