|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.rest.action.cat; |
| 21 | + |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; |
| 24 | +import org.elasticsearch.action.admin.indices.stats.CommonStats; |
| 25 | +import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; |
| 26 | +import org.elasticsearch.action.admin.indices.stats.IndicesStatsTests; |
| 27 | +import org.elasticsearch.action.admin.indices.stats.ShardStats; |
| 28 | +import org.elasticsearch.cluster.ClusterName; |
| 29 | +import org.elasticsearch.cluster.ClusterState; |
| 30 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 31 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 32 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 33 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 34 | +import org.elasticsearch.cluster.routing.UnassignedInfo; |
| 35 | +import org.elasticsearch.common.Table; |
| 36 | +import org.elasticsearch.common.UUIDs; |
| 37 | +import org.elasticsearch.common.settings.Settings; |
| 38 | +import org.elasticsearch.common.unit.TimeValue; |
| 39 | +import org.elasticsearch.index.Index; |
| 40 | +import org.elasticsearch.index.cache.query.QueryCacheStats; |
| 41 | +import org.elasticsearch.index.cache.request.RequestCacheStats; |
| 42 | +import org.elasticsearch.index.engine.SegmentsStats; |
| 43 | +import org.elasticsearch.index.fielddata.FieldDataStats; |
| 44 | +import org.elasticsearch.index.flush.FlushStats; |
| 45 | +import org.elasticsearch.index.get.GetStats; |
| 46 | +import org.elasticsearch.index.merge.MergeStats; |
| 47 | +import org.elasticsearch.index.refresh.RefreshStats; |
| 48 | +import org.elasticsearch.index.search.stats.SearchStats; |
| 49 | +import org.elasticsearch.index.shard.DocsStats; |
| 50 | +import org.elasticsearch.index.shard.IndexingStats; |
| 51 | +import org.elasticsearch.index.shard.ShardId; |
| 52 | +import org.elasticsearch.index.shard.ShardPath; |
| 53 | +import org.elasticsearch.index.store.StoreStats; |
| 54 | +import org.elasticsearch.index.warmer.WarmerStats; |
| 55 | +import org.elasticsearch.rest.RestController; |
| 56 | +import org.elasticsearch.search.suggest.completion.CompletionStats; |
| 57 | +import org.elasticsearch.test.ESTestCase; |
| 58 | + |
| 59 | +import java.nio.file.Path; |
| 60 | +import java.util.ArrayList; |
| 61 | +import java.util.List; |
| 62 | + |
| 63 | +import static java.util.Collections.emptyList; |
| 64 | +import static org.hamcrest.Matchers.equalTo; |
| 65 | + |
| 66 | +/** |
| 67 | + * Tests for {@link RestIndicesAction} |
| 68 | + */ |
| 69 | +public class RestIndicesActionTests extends ESTestCase { |
| 70 | + |
| 71 | + public void testBuildTable() { |
| 72 | + final Settings settings = Settings.EMPTY; |
| 73 | + final RestController restController = new RestController(settings); |
| 74 | + final RestIndicesAction action = new RestIndicesAction(settings, restController, new IndexNameExpressionResolver(settings)); |
| 75 | + |
| 76 | + // build a (semi-)random table |
| 77 | + final int numIndices = randomIntBetween(0, 5); |
| 78 | + Index[] indices = new Index[numIndices]; |
| 79 | + for (int i = 0; i < numIndices; i++) { |
| 80 | + indices[i] = new Index(randomAsciiOfLength(5), UUIDs.randomBase64UUID()); |
| 81 | + } |
| 82 | + |
| 83 | + final MetaData.Builder metaDataBuilder = MetaData.builder(); |
| 84 | + for (final Index index : indices) { |
| 85 | + metaDataBuilder.put(IndexMetaData.builder(index.getName()) |
| 86 | + .settings(Settings.builder() |
| 87 | + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 88 | + .put(IndexMetaData.SETTING_INDEX_UUID, index.getUUID())) |
| 89 | + .creationDate(System.currentTimeMillis()) |
| 90 | + .numberOfShards(1) |
| 91 | + .numberOfReplicas(1) |
| 92 | + .state(IndexMetaData.State.OPEN)); |
| 93 | + } |
| 94 | + final MetaData metaData = metaDataBuilder.build(); |
| 95 | + |
| 96 | + final ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) |
| 97 | + .metaData(metaData) |
| 98 | + .build(); |
| 99 | + final String[] indicesStr = new String[indices.length]; |
| 100 | + for (int i = 0; i < indices.length; i++) { |
| 101 | + indicesStr[i] = indices[i].getName(); |
| 102 | + } |
| 103 | + final ClusterHealthResponse clusterHealth = new ClusterHealthResponse( |
| 104 | + clusterState.getClusterName().value(), indicesStr, clusterState, 0, 0, 0, TimeValue.timeValueMillis(1000L) |
| 105 | + ); |
| 106 | + |
| 107 | + final Table table = action.buildTable(null, indices, clusterHealth, randomIndicesStatsResponse(indices), metaData); |
| 108 | + |
| 109 | + // now, verify the table is correct |
| 110 | + int count = 0; |
| 111 | + List<Table.Cell> headers = table.getHeaders(); |
| 112 | + assertThat(headers.get(count++).value, equalTo("health")); |
| 113 | + assertThat(headers.get(count++).value, equalTo("status")); |
| 114 | + assertThat(headers.get(count++).value, equalTo("index")); |
| 115 | + assertThat(headers.get(count++).value, equalTo("uuid")); |
| 116 | + |
| 117 | + List<List<Table.Cell>> rows = table.getRows(); |
| 118 | + assertThat(rows.size(), equalTo(indices.length)); |
| 119 | + // TODO: more to verify (e.g. randomize cluster health, num primaries, num replicas, etc) |
| 120 | + for (int i = 0; i < rows.size(); i++) { |
| 121 | + count = 0; |
| 122 | + final List<Table.Cell> row = rows.get(i); |
| 123 | + assertThat(row.get(count++).value, equalTo("red*")); // all are red because cluster state doesn't have routing entries |
| 124 | + assertThat(row.get(count++).value, equalTo("open")); // all are OPEN for now |
| 125 | + assertThat(row.get(count++).value, equalTo(indices[i].getName())); |
| 126 | + assertThat(row.get(count++).value, equalTo(indices[i].getUUID())); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private IndicesStatsResponse randomIndicesStatsResponse(final Index[] indices) { |
| 131 | + List<ShardStats> shardStats = new ArrayList<>(); |
| 132 | + for (final Index index : indices) { |
| 133 | + for (int i = 0; i < 2; i++) { |
| 134 | + ShardId shardId = new ShardId(index, i); |
| 135 | + Path path = createTempDir().resolve("indices").resolve(index.getUUID()).resolve(String.valueOf(i)); |
| 136 | + ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, null, i == 0, |
| 137 | + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null)); |
| 138 | + shardRouting = shardRouting.initialize("node-0", null, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE); |
| 139 | + shardRouting = shardRouting.moveToStarted(); |
| 140 | + CommonStats stats = new CommonStats(); |
| 141 | + stats.fieldData = new FieldDataStats(); |
| 142 | + stats.queryCache = new QueryCacheStats(); |
| 143 | + stats.docs = new DocsStats(); |
| 144 | + stats.store = new StoreStats(); |
| 145 | + stats.indexing = new IndexingStats(); |
| 146 | + stats.search = new SearchStats(); |
| 147 | + stats.segments = new SegmentsStats(); |
| 148 | + stats.merge = new MergeStats(); |
| 149 | + stats.refresh = new RefreshStats(); |
| 150 | + stats.completion = new CompletionStats(); |
| 151 | + stats.requestCache = new RequestCacheStats(); |
| 152 | + stats.get = new GetStats(); |
| 153 | + stats.flush = new FlushStats(); |
| 154 | + stats.warmer = new WarmerStats(); |
| 155 | + shardStats.add(new ShardStats(shardRouting, new ShardPath(false, path, path, shardId), stats, null)); |
| 156 | + } |
| 157 | + } |
| 158 | + return IndicesStatsTests.newIndicesStatsResponse( |
| 159 | + shardStats.toArray(new ShardStats[shardStats.size()]), shardStats.size(), shardStats.size(), 0, emptyList() |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments