25
25
import org .elasticsearch .action .admin .indices .stats .IndicesStatsResponse ;
26
26
import org .elasticsearch .action .admin .indices .stats .IndicesStatsTests ;
27
27
import org .elasticsearch .action .admin .indices .stats .ShardStats ;
28
+ import org .elasticsearch .action .support .IndicesOptions ;
28
29
import org .elasticsearch .cluster .ClusterName ;
29
30
import org .elasticsearch .cluster .ClusterState ;
30
31
import org .elasticsearch .cluster .metadata .IndexMetaData ;
38
39
import org .elasticsearch .common .UUIDs ;
39
40
import org .elasticsearch .common .settings .Settings ;
40
41
import org .elasticsearch .common .unit .TimeValue ;
41
- import org .elasticsearch .index .Index ;
42
42
import org .elasticsearch .index .cache .query .QueryCacheStats ;
43
43
import org .elasticsearch .index .cache .request .RequestCacheStats ;
44
44
import org .elasticsearch .index .engine .SegmentsStats ;
62
62
63
63
import java .nio .file .Path ;
64
64
import java .util .ArrayList ;
65
+ import java .util .Arrays ;
65
66
import java .util .Collections ;
66
67
import java .util .List ;
67
68
73
74
*/
74
75
public class RestIndicesActionTests extends ESTestCase {
75
76
76
- public void testBuildTable () {
77
- final Settings settings = Settings .EMPTY ;
78
- UsageService usageService = new UsageService ();
79
- final RestController restController = new RestController (Collections .emptySet (), null , null , null , usageService );
80
- final RestIndicesAction action = new RestIndicesAction (settings , restController , new IndexNameExpressionResolver ());
81
-
77
+ private IndexMetaData [] buildRandomIndicesMetaData (int numIndices ) {
82
78
// build a (semi-)random table
83
- final int numIndices = randomIntBetween (0 , 5 );
84
- Index [] indices = new Index [numIndices ];
79
+ final IndexMetaData [] indicesMetaData = new IndexMetaData [numIndices ];
85
80
for (int i = 0 ; i < numIndices ; i ++) {
86
- indices [i ] = new Index (randomAlphaOfLength (5 ), UUIDs .randomBase64UUID ());
87
- }
88
-
89
- final MetaData .Builder metaDataBuilder = MetaData .builder ();
90
- for (final Index index : indices ) {
91
- metaDataBuilder .put (IndexMetaData .builder (index .getName ())
81
+ indicesMetaData [i ] = IndexMetaData .builder (randomAlphaOfLength (5 ) + i )
92
82
.settings (Settings .builder ()
93
- .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
94
- .put (IndexMetaData .SETTING_INDEX_UUID , index . getUUID ()))
83
+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
84
+ .put (IndexMetaData .SETTING_INDEX_UUID , UUIDs . randomBase64UUID ()))
95
85
.creationDate (System .currentTimeMillis ())
96
86
.numberOfShards (1 )
97
87
.numberOfReplicas (1 )
98
- .state (IndexMetaData .State .OPEN ));
88
+ .state (IndexMetaData .State .OPEN )
89
+ .build ();
99
90
}
100
- final MetaData metaData = metaDataBuilder .build ();
91
+ return indicesMetaData ;
92
+ }
101
93
94
+ private ClusterState buildClusterState (IndexMetaData [] indicesMetaData ) {
95
+ final MetaData .Builder metaDataBuilder = MetaData .builder ();
96
+ for (IndexMetaData indexMetaData : indicesMetaData ) {
97
+ metaDataBuilder .put (indexMetaData , false );
98
+ }
99
+ final MetaData metaData = metaDataBuilder .build ();
102
100
final ClusterState clusterState = ClusterState .builder (ClusterName .CLUSTER_NAME_SETTING .getDefault (Settings .EMPTY ))
103
101
.metaData (metaData )
104
102
.build ();
105
- final String [] indicesStr = new String [indices .length ];
106
- for (int i = 0 ; i < indices .length ; i ++) {
107
- indicesStr [i ] = indices [i ].getName ();
103
+ return clusterState ;
104
+ }
105
+
106
+ private ClusterHealthResponse buildClusterHealthResponse (ClusterState clusterState , IndexMetaData [] indicesMetaData ) {
107
+ final String [] indicesStr = new String [indicesMetaData .length ];
108
+ for (int i = 0 ; i < indicesMetaData .length ; i ++) {
109
+ indicesStr [i ] = indicesMetaData [i ].getIndex ().getName ();
108
110
}
109
- final ClusterHealthResponse clusterHealth = new ClusterHealthResponse (
111
+ final ClusterHealthResponse clusterHealthResponse = new ClusterHealthResponse (
110
112
clusterState .getClusterName ().value (), indicesStr , clusterState , 0 , 0 , 0 , TimeValue .timeValueMillis (1000L )
111
113
);
114
+ return clusterHealthResponse ;
115
+ }
112
116
113
- final Table table = action .buildTable (new FakeRestRequest (), indices , clusterHealth , randomIndicesStatsResponse (indices ), metaData );
117
+ public void testBuildTable () {
118
+ final Settings settings = Settings .EMPTY ;
119
+ UsageService usageService = new UsageService ();
120
+ final RestController restController = new RestController (Collections .emptySet (), null , null , null , usageService );
121
+ final RestIndicesAction action = new RestIndicesAction (settings , restController , new IndexNameExpressionResolver ());
122
+
123
+ final IndexMetaData [] generatedIndicesMetaData = buildRandomIndicesMetaData (randomIntBetween (1 , 5 ));
124
+ final ClusterState clusterState = buildClusterState (generatedIndicesMetaData );
125
+ final ClusterHealthResponse clusterHealthResponse = buildClusterHealthResponse (clusterState , generatedIndicesMetaData );
126
+
127
+ final IndexMetaData [] sortedIndicesMetaData = action .getOrderedIndexMetaData (new String [0 ], clusterState ,
128
+ IndicesOptions .strictExpand ());
129
+ final IndexMetaData [] smallerSortedIndicesMetaData = removeRandomElement (sortedIndicesMetaData );
130
+ final Table table = action .buildTable (new FakeRestRequest (), sortedIndicesMetaData , clusterHealthResponse ,
131
+ randomIndicesStatsResponse (smallerSortedIndicesMetaData ));
114
132
115
133
// now, verify the table is correct
116
134
int count = 0 ;
@@ -121,27 +139,27 @@ public void testBuildTable() {
121
139
assertThat (headers .get (count ++).value , equalTo ("uuid" ));
122
140
123
141
List <List <Table .Cell >> rows = table .getRows ();
124
- assertThat (rows .size (), equalTo (indices .length ));
142
+ assertThat (rows .size (), equalTo (smallerSortedIndicesMetaData .length ));
125
143
// TODO: more to verify (e.g. randomize cluster health, num primaries, num replicas, etc)
126
144
for (int i = 0 ; i < rows .size (); i ++) {
127
145
count = 0 ;
128
146
final List <Table .Cell > row = rows .get (i );
129
147
assertThat (row .get (count ++).value , equalTo ("red*" )); // all are red because cluster state doesn't have routing entries
130
148
assertThat (row .get (count ++).value , equalTo ("open" )); // all are OPEN for now
131
- assertThat (row .get (count ++).value , equalTo (indices [i ].getName ()));
132
- assertThat (row .get (count ++).value , equalTo (indices [i ].getUUID ()));
149
+ assertThat (row .get (count ++).value , equalTo (smallerSortedIndicesMetaData [i ]. getIndex () .getName ()));
150
+ assertThat (row .get (count ++).value , equalTo (smallerSortedIndicesMetaData [i ].getIndexUUID ()));
133
151
}
134
152
}
135
153
136
- private IndicesStatsResponse randomIndicesStatsResponse (final Index [] indices ) {
154
+ private IndicesStatsResponse randomIndicesStatsResponse (final IndexMetaData [] indices ) {
137
155
List <ShardStats > shardStats = new ArrayList <>();
138
- for (final Index index : indices ) {
139
- int numShards = randomInt ( 5 );
156
+ for (final IndexMetaData index : indices ) {
157
+ int numShards = randomIntBetween ( 1 , 3 );
140
158
int primaryIdx = randomIntBetween (-1 , numShards - 1 ); // -1 means there is no primary shard.
141
159
for (int i = 0 ; i < numShards ; i ++) {
142
- ShardId shardId = new ShardId (index , i );
160
+ ShardId shardId = new ShardId (index . getIndex () , i );
143
161
boolean primary = (i == primaryIdx );
144
- Path path = createTempDir ().resolve ("indices" ).resolve (index .getUUID ()).resolve (String .valueOf (i ));
162
+ Path path = createTempDir ().resolve ("indices" ).resolve (index .getIndexUUID ()).resolve (String .valueOf (i ));
145
163
ShardRouting shardRouting = ShardRouting .newUnassigned (shardId , primary ,
146
164
primary ? RecoverySource .EmptyStoreRecoverySource .INSTANCE : PeerRecoverySource .INSTANCE ,
147
165
new UnassignedInfo (UnassignedInfo .Reason .INDEX_CREATED , null )
@@ -170,4 +188,14 @@ private IndicesStatsResponse randomIndicesStatsResponse(final Index[] indices) {
170
188
shardStats .toArray (new ShardStats [shardStats .size ()]), shardStats .size (), shardStats .size (), 0 , emptyList ()
171
189
);
172
190
}
191
+
192
+ private IndexMetaData [] removeRandomElement (IndexMetaData [] array ) {
193
+ assert array != null ;
194
+ assert array .length > 0 ;
195
+ final List <IndexMetaData > collectionLessAnItem = new ArrayList <>();
196
+ collectionLessAnItem .addAll (Arrays .asList (array ));
197
+ final int toRemoveIndex = randomIntBetween (0 , array .length - 1 );
198
+ collectionLessAnItem .remove (toRemoveIndex );
199
+ return collectionLessAnItem .toArray (new IndexMetaData [0 ]);
200
+ }
173
201
}
0 commit comments