Skip to content

Commit a04db2e

Browse files
committed
[CCR] Add monitoring mapping verification test (#33662)
Added test that verifies that all fields in ShardFollowNodeTaskStatus are mapped in monitoring-es.json
1 parent 38ac844 commit a04db2e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsMonitoringDocTests.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,34 @@
77
package org.elasticsearch.xpack.monitoring.collector.ccr;
88

99
import org.elasticsearch.ElasticsearchException;
10+
import org.elasticsearch.common.Strings;
1011
import org.elasticsearch.common.bytes.BytesReference;
12+
import org.elasticsearch.common.xcontent.XContentBuilder;
1113
import org.elasticsearch.common.xcontent.XContentHelper;
1214
import org.elasticsearch.common.xcontent.XContentType;
15+
import org.elasticsearch.common.xcontent.support.XContentMapValues;
1316
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus;
1417
import org.elasticsearch.xpack.core.monitoring.MonitoredSystem;
1518
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
19+
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils;
1620
import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase;
1721
import org.joda.time.DateTime;
1822
import org.joda.time.DateTimeZone;
1923
import org.junit.Before;
2024

2125
import java.io.IOException;
2226
import java.util.Collections;
27+
import java.util.Map;
2328
import java.util.NavigableMap;
2429
import java.util.TreeMap;
2530

31+
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
32+
import static org.hamcrest.Matchers.anyOf;
2633
import static org.hamcrest.Matchers.containsString;
2734
import static org.hamcrest.Matchers.equalTo;
2835
import static org.hamcrest.Matchers.hasToString;
2936
import static org.hamcrest.Matchers.is;
37+
import static org.hamcrest.Matchers.notNullValue;
3038
import static org.hamcrest.Matchers.nullValue;
3139
import static org.mockito.Mockito.mock;
3240

@@ -174,4 +182,65 @@ public void testToXContent() throws IOException {
174182
+ "}"));
175183
}
176184

185+
public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException {
186+
final NavigableMap<Long, ElasticsearchException> fetchExceptions =
187+
new TreeMap<>(Collections.singletonMap(1L, new ElasticsearchException("shard is sad")));
188+
final ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus(
189+
"cluster_alias:leader_index",
190+
"follower_index",
191+
0,
192+
1,
193+
1,
194+
1,
195+
1,
196+
1,
197+
1,
198+
1,
199+
1,
200+
1,
201+
100,
202+
10,
203+
0,
204+
10,
205+
100,
206+
10,
207+
10,
208+
0,
209+
10,
210+
fetchExceptions,
211+
2);
212+
XContentBuilder builder = jsonBuilder();
213+
builder.value(status);
214+
Map<String, Object> serializedStatus = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false);
215+
216+
Map<String, Object> template =
217+
XContentHelper.convertToMap(XContentType.JSON.xContent(), MonitoringTemplateUtils.loadTemplate("es"), false);
218+
Map<?, ?> ccrStatsMapping = (Map<?, ?>) XContentMapValues.extractValue("mappings.doc.properties.ccr_stats.properties", template);
219+
220+
assertThat(serializedStatus.size(), equalTo(ccrStatsMapping.size()));
221+
for (Map.Entry<String, Object> entry : serializedStatus.entrySet()) {
222+
String fieldName = entry.getKey();
223+
Map<?, ?> fieldMapping = (Map<?, ?>) ccrStatsMapping.get(fieldName);
224+
assertThat(fieldMapping, notNullValue());
225+
226+
Object fieldValue = entry.getValue();
227+
String fieldType = (String) fieldMapping.get("type");
228+
if (fieldValue instanceof Long || fieldValue instanceof Integer) {
229+
assertThat("expected long field type for field [" + fieldName + "]", fieldType,
230+
anyOf(equalTo("long"), equalTo("integer")));
231+
} else if (fieldValue instanceof String) {
232+
assertThat("expected keyword field type for field [" + fieldName + "]", fieldType,
233+
anyOf(equalTo("keyword"), equalTo("text")));
234+
} else {
235+
// Manual test specific object fields and if not just fail:
236+
if (fieldName.equals("fetch_exceptions")) {
237+
assertThat(XContentMapValues.extractValue("properties.from_seq_no.type", fieldMapping), equalTo("long"));
238+
assertThat(XContentMapValues.extractValue("properties.exception.type", fieldMapping), equalTo("text"));
239+
} else {
240+
fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]");
241+
}
242+
}
243+
}
244+
}
245+
177246
}

0 commit comments

Comments
 (0)