6
6
package org .elasticsearch .xpack .watcher .test ;
7
7
8
8
import org .elasticsearch .action .admin .indices .alias .Alias ;
9
+ import org .elasticsearch .action .admin .indices .alias .get .GetAliasesResponse ;
9
10
import org .elasticsearch .action .admin .indices .create .CreateIndexResponse ;
11
+ import org .elasticsearch .action .admin .indices .get .GetIndexResponse ;
10
12
import org .elasticsearch .action .admin .indices .template .get .GetIndexTemplatesResponse ;
11
13
import org .elasticsearch .action .search .SearchRequestBuilder ;
12
14
import org .elasticsearch .action .search .SearchResponse ;
13
15
import org .elasticsearch .action .support .IndicesOptions ;
14
16
import org .elasticsearch .analysis .common .CommonAnalysisPlugin ;
15
17
import org .elasticsearch .cluster .ClusterState ;
16
18
import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
19
+ import org .elasticsearch .cluster .metadata .MappingMetaData ;
17
20
import org .elasticsearch .cluster .routing .IndexRoutingTable ;
18
21
import org .elasticsearch .common .collect .Tuple ;
19
22
import org .elasticsearch .common .network .NetworkModule ;
@@ -194,7 +197,7 @@ public void _setup() throws Exception {
194
197
internalCluster ().setDisruptionScheme (ice );
195
198
ice .startDisrupting ();
196
199
}
197
-
200
+ stopWatcher ();
198
201
createWatcherIndicesOrAliases ();
199
202
startWatcher ();
200
203
}
@@ -221,13 +224,19 @@ private void createWatcherIndicesOrAliases() throws Exception {
221
224
// alias for .watches, setting the index template to the same as well
222
225
String watchIndexName ;
223
226
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 )
227
231
.setCause ("Index to test aliases with .watches index" )
228
232
.addAlias (new Alias (Watch .INDEX ))
229
233
.get ();
230
234
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
+
231
240
logger .info ("set alias for .watches index to [{}]" , watchIndexName );
232
241
} else {
233
242
watchIndexName = Watch .INDEX ;
@@ -239,13 +248,19 @@ private void createWatcherIndicesOrAliases() throws Exception {
239
248
}
240
249
241
250
// 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 )
245
254
.setCause ("Index to test aliases with .triggered-watches index" )
246
255
.addAlias (new Alias (TriggeredWatchStoreField .INDEX_NAME ))
247
256
.get ();
248
257
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 );
249
264
logger .info ("set alias for .triggered-watches index to [{}]" , triggeredWatchIndexName );
250
265
} else {
251
266
triggeredWatchIndexName = TriggeredWatchStoreField .INDEX_NAME ;
@@ -259,6 +274,38 @@ private void createWatcherIndicesOrAliases() throws Exception {
259
274
}
260
275
}
261
276
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
+
262
309
protected TimeWarp timeWarp () {
263
310
assert timeWarped () : "cannot access TimeWarp when test context is not time warped" ;
264
311
return timeWarp ;
0 commit comments