2323import org .opensearch .test .OpenSearchIntegTestCase ;
2424
2525import java .io .IOException ;
26+ import java .util .Arrays ;
2627import java .util .List ;
2728import java .util .concurrent .CountDownLatch ;
2829import java .util .concurrent .ExecutionException ;
@@ -169,6 +170,7 @@ public void testListShardsWithClosedAndHiddenIndices() throws InterruptedExcepti
169170 // close index "test-closed-idx"
170171 client ().admin ().indices ().close (Requests .closeIndexRequest ("test-closed-idx" )).get ();
171172
173+ // Verifying responses for default queries: /_cat/shards and /_list/shards
172174 CatShardsRequest shardsRequest = new CatShardsRequest ();
173175 shardsRequest .setCancelAfterTimeInterval (NO_TIMEOUT );
174176 shardsRequest .setIndices (Strings .EMPTY_ARRAY );
@@ -185,6 +187,82 @@ public void testListShardsWithClosedAndHiddenIndices() throws InterruptedExcepti
185187 catShardsResponse .get ().getIndicesStatsResponse ().getShards ().length ,
186188 listShardsResponse .get ().getIndicesStatsResponse ().getShards ().length
187189 );
190+
191+ // Verifying responses when hidden indices are explicitly queried: /_cat/shards/test-hidden-idx and /_list/shards/test-hidden-idx
192+ // Shards for hidden index should appear in response along with stats
193+ shardsRequest = new CatShardsRequest ();
194+ shardsRequest .setCancelAfterTimeInterval (NO_TIMEOUT );
195+ shardsRequest .setIndices (List .of ("test-hidden-idx" ).toArray (new String [0 ]));
196+ catShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
197+ assertTrue (catShardsResponse .get ().getResponseShards ().stream ().allMatch (shard -> shard .getIndexName ().equals ("test-hidden-idx" )));
198+ assertTrue (
199+ Arrays .stream (catShardsResponse .get ().getIndicesStatsResponse ().getShards ())
200+ .allMatch (shardStats -> shardStats .getShardRouting ().getIndexName ().equals ("test-hidden-idx" ))
201+ );
202+ assertEquals (
203+ catShardsResponse .get ().getResponseShards ().size (),
204+ catShardsResponse .get ().getIndicesStatsResponse ().getShards ().length
205+ );
206+
207+ shardsRequest .setPageParams (new PageParams (null , PageParams .PARAM_ASC_SORT_VALUE , pageSize ));
208+ listShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
209+ assertTrue (listShardsResponse .get ().getResponseShards ().stream ().allMatch (shard -> shard .getIndexName ().equals ("test-hidden-idx" )));
210+ assertTrue (
211+ Arrays .stream (listShardsResponse .get ().getIndicesStatsResponse ().getShards ())
212+ .allMatch (shardStats -> shardStats .getShardRouting ().getIndexName ().equals ("test-hidden-idx" ))
213+ );
214+ assertEquals (
215+ listShardsResponse .get ().getResponseShards ().size (),
216+ listShardsResponse .get ().getIndicesStatsResponse ().getShards ().length
217+ );
218+
219+ // Verifying responses when hidden indices are queried with wildcards: /_cat/shards/test-hidden-idx* and
220+ // /_list/shards/test-hidden-idx*
221+ // Shards for hidden index should appear in response without stats
222+ shardsRequest = new CatShardsRequest ();
223+ shardsRequest .setCancelAfterTimeInterval (NO_TIMEOUT );
224+ shardsRequest .setIndices (List .of ("test-hidden-idx*" ).toArray (new String [0 ]));
225+ catShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
226+ assertTrue (catShardsResponse .get ().getResponseShards ().stream ().allMatch (shard -> shard .getIndexName ().equals ("test-hidden-idx" )));
227+ assertEquals (0 , catShardsResponse .get ().getIndicesStatsResponse ().getShards ().length );
228+
229+ shardsRequest .setPageParams (new PageParams (null , PageParams .PARAM_ASC_SORT_VALUE , pageSize ));
230+ listShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
231+ assertTrue (listShardsResponse .get ().getResponseShards ().stream ().allMatch (shard -> shard .getIndexName ().equals ("test-hidden-idx" )));
232+ assertEquals (0 , listShardsResponse .get ().getIndicesStatsResponse ().getShards ().length );
233+
234+ // Explicitly querying for closed index: /_cat/shards/test-closed-idx and /_list/shards/test-closed-idx
235+ // /_cat/shards/test-closed-idx should result in IndexClosedException
236+ // while /_list/shards/test-closed-idx should output closed shards without stats.
237+ shardsRequest = new CatShardsRequest ();
238+ shardsRequest .setCancelAfterTimeInterval (NO_TIMEOUT );
239+ shardsRequest .setIndices (List .of ("test-closed-idx" ).toArray (new String [0 ]));
240+ try {
241+ catShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
242+ catShardsResponse .get ();
243+ fail ("Expected IndexClosedException" );
244+ } catch (Exception exception ) {
245+ assertTrue (exception .getMessage ().contains ("IndexClosedException" ));
246+ }
247+
248+ shardsRequest .setPageParams (new PageParams (null , PageParams .PARAM_ASC_SORT_VALUE , pageSize ));
249+ listShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
250+ assertTrue (listShardsResponse .get ().getResponseShards ().stream ().allMatch (shard -> shard .getIndexName ().equals ("test-closed-idx" )));
251+ assertEquals (0 , listShardsResponse .get ().getIndicesStatsResponse ().getShards ().length );
252+
253+ // Querying for closed index with wildcards: /_cat/shards/test-closed-idx and /_list/shards/test-closed-idx
254+ // Both the queries should return zero entries
255+ shardsRequest = new CatShardsRequest ();
256+ shardsRequest .setCancelAfterTimeInterval (NO_TIMEOUT );
257+ shardsRequest .setIndices (List .of ("test-closed-idx*" ).toArray (new String [0 ]));
258+ catShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
259+ assertEquals (0 , catShardsResponse .get ().getResponseShards ().size ());
260+ assertEquals (0 , catShardsResponse .get ().getIndicesStatsResponse ().getShards ().length );
261+
262+ shardsRequest .setPageParams (new PageParams (null , PageParams .PARAM_ASC_SORT_VALUE , pageSize ));
263+ listShardsResponse = client ().execute (CatShardsAction .INSTANCE , shardsRequest );
264+ assertEquals (0 , listShardsResponse .get ().getResponseShards ().size ());
265+ assertEquals (0 , listShardsResponse .get ().getIndicesStatsResponse ().getShards ().length );
188266 }
189267
190268}
0 commit comments