66package org .elasticsearch .xpack .watcher .test ;
77
88import org .elasticsearch .action .admin .indices .alias .Alias ;
9+ import org .elasticsearch .action .admin .indices .alias .get .GetAliasesResponse ;
910import org .elasticsearch .action .admin .indices .create .CreateIndexResponse ;
11+ import org .elasticsearch .action .admin .indices .get .GetIndexResponse ;
1012import org .elasticsearch .action .admin .indices .template .get .GetIndexTemplatesResponse ;
1113import org .elasticsearch .action .search .SearchRequestBuilder ;
1214import org .elasticsearch .action .search .SearchResponse ;
1315import org .elasticsearch .action .support .IndicesOptions ;
1416import org .elasticsearch .analysis .common .CommonAnalysisPlugin ;
1517import org .elasticsearch .cluster .ClusterState ;
1618import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
19+ import org .elasticsearch .cluster .metadata .MappingMetaData ;
1720import org .elasticsearch .cluster .routing .IndexRoutingTable ;
1821import org .elasticsearch .common .collect .Tuple ;
1922import org .elasticsearch .common .network .NetworkModule ;
@@ -194,7 +197,7 @@ public void _setup() throws Exception {
194197 internalCluster ().setDisruptionScheme (ice );
195198 ice .startDisrupting ();
196199 }
197-
200+ stopWatcher ();
198201 createWatcherIndicesOrAliases ();
199202 startWatcher ();
200203 }
@@ -221,13 +224,19 @@ private void createWatcherIndicesOrAliases() throws Exception {
221224 // alias for .watches, setting the index template to the same as well
222225 String watchIndexName ;
223226 String triggeredWatchIndexName ;
224- if (rarely ()) {
225- watchIndexName = ".watches-alias-index" ;
226- CreateIndexResponse response = client ().admin ().indices ().prepareCreate (watchIndexName )
227+ if (randomBoolean ()) {
228+ // Create an index to get the template
229+ String tempIndex = ".watches" + randomAlphaOfLength (5 ).toLowerCase (Locale .ROOT );
230+ CreateIndexResponse response = client ().admin ().indices ().prepareCreate (tempIndex )
227231 .setCause ("Index to test aliases with .watches index" )
228232 .addAlias (new Alias (Watch .INDEX ))
229233 .get ();
230234 assertAcked (response );
235+
236+ // Now replace it with a randomly named index
237+ watchIndexName = randomAlphaOfLengthBetween (5 ,10 ).toLowerCase (Locale .ROOT );
238+ replaceWatcherIndexWithRandomlyNamedIndex (Watch .INDEX , watchIndexName , Watch .DOC_TYPE );
239+
231240 logger .info ("set alias for .watches index to [{}]" , watchIndexName );
232241 } else {
233242 watchIndexName = Watch .INDEX ;
@@ -239,13 +248,19 @@ private void createWatcherIndicesOrAliases() throws Exception {
239248 }
240249
241250 // alias for .triggered-watches, ensuring the index template is set appropriately
242- if (rarely ()) {
243- triggeredWatchIndexName = ".triggered_watches-alias-index" ;
244- CreateIndexResponse response = client ().admin ().indices ().prepareCreate (triggeredWatchIndexName )
251+ if (randomBoolean ()) {
252+ String tempIndex = ".triggered_watches-alias-index" ;
253+ CreateIndexResponse response = client ().admin ().indices ().prepareCreate (tempIndex )
245254 .setCause ("Index to test aliases with .triggered-watches index" )
246255 .addAlias (new Alias (TriggeredWatchStoreField .INDEX_NAME ))
247256 .get ();
248257 assertAcked (response );
258+
259+ // Now replace it with a randomly-named index
260+ triggeredWatchIndexName = randomValueOtherThan (watchIndexName ,
261+ () -> randomAlphaOfLengthBetween (5 ,10 ).toLowerCase (Locale .ROOT ));
262+ replaceWatcherIndexWithRandomlyNamedIndex (TriggeredWatchStoreField .INDEX_NAME , triggeredWatchIndexName ,
263+ TriggeredWatchStoreField .DOC_TYPE );
249264 logger .info ("set alias for .triggered-watches index to [{}]" , triggeredWatchIndexName );
250265 } else {
251266 triggeredWatchIndexName = TriggeredWatchStoreField .INDEX_NAME ;
@@ -259,6 +274,38 @@ private void createWatcherIndicesOrAliases() throws Exception {
259274 }
260275 }
261276
277+ public void replaceWatcherIndexWithRandomlyNamedIndex (String originalIndexOrAlias , String to , String docType ) {
278+ GetIndexResponse index = client ().admin ().indices ().prepareGetIndex ().setIndices (originalIndexOrAlias ).get ();
279+ MappingMetaData mapping = index .getMappings ().get (index .getIndices ()[0 ]).get (docType );
280+
281+ Settings settings = index .getSettings ().get (index .getIndices ()[0 ]);
282+ Settings .Builder newSettings = Settings .builder ().put (settings );
283+ newSettings .remove ("index.provided_name" );
284+ newSettings .remove ("index.uuid" );
285+ newSettings .remove ("index.creation_date" );
286+ newSettings .remove ("index.version.created" );
287+
288+ CreateIndexResponse createIndexResponse = client ().admin ().indices ().prepareCreate (to )
289+ .addMapping (docType , mapping .sourceAsMap ())
290+ .setSettings (newSettings )
291+ .get ();
292+ assertTrue (createIndexResponse .isAcknowledged ());
293+ ensureGreen (to );
294+
295+ AtomicReference <String > originalIndex = new AtomicReference <>(originalIndexOrAlias );
296+ boolean watchesIsAlias = client ().admin ().indices ().prepareAliasesExist (originalIndexOrAlias ).get ().isExists ();
297+ if (watchesIsAlias ) {
298+ GetAliasesResponse aliasesResponse = client ().admin ().indices ().prepareGetAliases (originalIndexOrAlias ).get ();
299+ assertEquals (1 , aliasesResponse .getAliases ().size ());
300+ aliasesResponse .getAliases ().forEach ((aliasRecord ) -> {
301+ assertEquals (1 , aliasRecord .value .size ());
302+ originalIndex .set (aliasRecord .key );
303+ });
304+ }
305+ client ().admin ().indices ().prepareDelete (originalIndex .get ()).get ();
306+ client ().admin ().indices ().prepareAliases ().addAlias (to , originalIndexOrAlias ).get ();
307+ }
308+
262309 protected TimeWarp timeWarp () {
263310 assert timeWarped () : "cannot access TimeWarp when test context is not time warped" ;
264311 return timeWarp ;
0 commit comments