|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.upgrades; |
| 10 | + |
| 11 | +import org.opensearch.Version; |
| 12 | +import org.opensearch.client.Request; |
| 13 | +import org.opensearch.client.Response; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +public class ClusterStatsIT extends AbstractRollingTestCase { |
| 21 | + |
| 22 | + private final List<String> nodeStatsMetrics = List.of("os", "process", "jvm", "fs", "plugins", "ingest", "network_types", "discovery_types", "packaging_types"); |
| 23 | + |
| 24 | + private final List<String> indicesStatsMetrics = List.of("shards", "docs", "store", "fielddata", "query_cache", "completion", "segments", "analysis", "mappings"); |
| 25 | + |
| 26 | + public void testClusterStats() throws IOException { |
| 27 | + Response response = client().performRequest(new Request("GET", "/_cluster/stats")); |
| 28 | + validateClusterStatsWithFilterResponse(response, nodeStatsMetrics, indicesStatsMetrics); |
| 29 | + if (AbstractRollingTestCase.UPGRADE_FROM_VERSION.onOrAfter(Version.V_3_0_0) || ( |
| 30 | + CLUSTER_TYPE == ClusterType.UPGRADED && Version.CURRENT.onOrAfter(Version.V_3_0_0))) { |
| 31 | + response = client().performRequest(new Request("GET", "/_cluster/stats/os/nodes/_all")); |
| 32 | + validateClusterStatsWithFilterResponse(response, List.of("os"), Collections.emptyList()); |
| 33 | + response = client().performRequest(new Request("GET", "/_cluster/stats/indices/mappings/nodes/_all")); |
| 34 | + validateClusterStatsWithFilterResponse(response, Collections.emptyList(), List.of("mappings")); |
| 35 | + response = client().performRequest(new Request("GET", "/_cluster/stats/os,indices/mappings/nodes/_all")); |
| 36 | + validateClusterStatsWithFilterResponse(response, List.of("os"), List.of("mappings")); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private void validateClusterStatsWithFilterResponse(Response response, List<String> requestedNodesStatsMetrics, List<String> requestedIndicesStatsMetrics) throws IOException { |
| 41 | + assertEquals(200, response.getStatusLine().getStatusCode()); |
| 42 | + Map<String, Object> entity = entityAsMap(response); |
| 43 | + if (requestedNodesStatsMetrics != null && !requestedNodesStatsMetrics.isEmpty()) { |
| 44 | + assertTrue(entity.containsKey("nodes")); |
| 45 | + Map<?, ?> nodesStats = (Map<?, ?>) entity.get("nodes"); |
| 46 | + for (String metric : nodeStatsMetrics) { |
| 47 | + if (requestedNodesStatsMetrics.contains(metric)) { |
| 48 | + assertTrue(nodesStats.containsKey(metric)); |
| 49 | + } else { |
| 50 | + assertFalse(nodesStats.containsKey(metric)); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (requestedIndicesStatsMetrics != null && !requestedIndicesStatsMetrics.isEmpty()) { |
| 56 | + assertTrue(entity.containsKey("indices")); |
| 57 | + Map<?, ?> indicesStats = (Map<?, ?>) entity.get("indices"); |
| 58 | + for (String metric : indicesStatsMetrics) { |
| 59 | + if (requestedIndicesStatsMetrics.contains(metric)) { |
| 60 | + assertTrue(indicesStats.containsKey(metric)); |
| 61 | + } else { |
| 62 | + assertFalse(indicesStats.containsKey(metric)); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments