@@ -185,23 +185,32 @@ public void testCreateJobsWithIndexNameOption() throws Exception {
185185 + "anomaly_detectors/" + jobId2 , Collections .emptyMap (), new StringEntity (jobConfig , ContentType .APPLICATION_JSON ));
186186 assertEquals (200 , response .getStatusLine ().getStatusCode ());
187187
188- response = client ().performRequest ("get" , "_aliases" );
189- assertEquals (200 , response .getStatusLine ().getStatusCode ());
190- String responseAsString = responseEntityToString (response );
188+ // With security enabled GET _aliases throws an index_not_found_exception
189+ // if no aliases have been created. In multi-node tests the alias may not
190+ // appear immediately so wait here.
191+ assertBusy (() -> {
192+ try {
193+ Response aliasesResponse = client ().performRequest ("get" , "_aliases" );
194+ assertEquals (200 , aliasesResponse .getStatusLine ().getStatusCode ());
195+ String responseAsString = responseEntityToString (aliasesResponse );
196+ assertThat (responseAsString ,
197+ containsString ("\" " + AnomalyDetectorsIndex .jobResultsAliasedName ("custom-" + indexName ) + "\" :{\" aliases\" :{" ));
198+ assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .jobResultsAliasedName (jobId1 )
199+ + "\" :{\" filter\" :{\" term\" :{\" job_id\" :{\" value\" :\" " + jobId1 + "\" ,\" boost\" :1.0}}}}" ));
200+ assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .resultsWriteAlias (jobId1 ) + "\" :{}" ));
201+ assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .jobResultsAliasedName (jobId2 )
202+ + "\" :{\" filter\" :{\" term\" :{\" job_id\" :{\" value\" :\" " + jobId2 + "\" ,\" boost\" :1.0}}}}" ));
203+ assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .resultsWriteAlias (jobId2 ) + "\" :{}" ));
204+ } catch (ResponseException e ) {
205+ throw new AssertionError (e );
206+ }
207+ });
191208
209+ Response indicesResponse = client ().performRequest ("get" , "_cat/indices" );
210+ assertEquals (200 , indicesResponse .getStatusLine ().getStatusCode ());
211+ String responseAsString = responseEntityToString (indicesResponse );
192212 assertThat (responseAsString ,
193- containsString ("\" " + AnomalyDetectorsIndex .jobResultsAliasedName ("custom-" + indexName ) + "\" :{\" aliases\" :{" ));
194- assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .jobResultsAliasedName (jobId1 )
195- + "\" :{\" filter\" :{\" term\" :{\" job_id\" :{\" value\" :\" " + jobId1 + "\" ,\" boost\" :1.0}}}}" ));
196- assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .resultsWriteAlias (jobId1 ) + "\" :{}" ));
197- assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .jobResultsAliasedName (jobId2 )
198- + "\" :{\" filter\" :{\" term\" :{\" job_id\" :{\" value\" :\" " + jobId2 + "\" ,\" boost\" :1.0}}}}" ));
199- assertThat (responseAsString , containsString ("\" " + AnomalyDetectorsIndex .resultsWriteAlias (jobId2 ) + "\" :{}" ));
200-
201- response = client ().performRequest ("get" , "_cat/indices" );
202- assertEquals (200 , response .getStatusLine ().getStatusCode ());
203- responseAsString = responseEntityToString (response );
204- assertThat (responseAsString , containsString (AnomalyDetectorsIndexFields .RESULTS_INDEX_PREFIX + "custom-" + indexName ));
213+ containsString (AnomalyDetectorsIndexFields .RESULTS_INDEX_PREFIX + "custom-" + indexName ));
205214 assertThat (responseAsString , not (containsString (AnomalyDetectorsIndex .jobResultsAliasedName (jobId1 ))));
206215 assertThat (responseAsString , not (containsString (AnomalyDetectorsIndex .jobResultsAliasedName (jobId2 ))));
207216
@@ -445,15 +454,24 @@ public void testDeleteJobAfterMissingAliases() throws Exception {
445454 String indexName = AnomalyDetectorsIndexFields .RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields .RESULTS_INDEX_DEFAULT ;
446455 createFarequoteJob (jobId );
447456
448- Response response = client ().performRequest ("get" , "_cat/aliases" );
449- assertEquals (200 , response .getStatusLine ().getStatusCode ());
450- String responseAsString = responseEntityToString (response );
451- assertThat (responseAsString , containsString (readAliasName ));
452- assertThat (responseAsString , containsString (writeAliasName ));
457+ // With security enabled cat aliases throws an index_not_found_exception
458+ // if no aliases have been created. In multi-node tests the alias may not
459+ // appear immediately so wait here.
460+ assertBusy (() -> {
461+ try {
462+ Response aliasesResponse = client ().performRequest ("get" , "_cat/aliases" );
463+ assertEquals (200 , aliasesResponse .getStatusLine ().getStatusCode ());
464+ String responseAsString = responseEntityToString (aliasesResponse );
465+ assertThat (responseAsString , containsString (readAliasName ));
466+ assertThat (responseAsString , containsString (writeAliasName ));
467+ } catch (ResponseException e ) {
468+ throw new AssertionError (e );
469+ }
470+ });
453471
454472 // Manually delete the aliases so that we can test that deletion proceeds
455473 // normally anyway
456- response = client ().performRequest ("delete" , indexName + "/_alias/" + readAliasName );
474+ Response response = client ().performRequest ("delete" , indexName + "/_alias/" + readAliasName );
457475 assertEquals (200 , response .getStatusLine ().getStatusCode ());
458476 response = client ().performRequest ("delete" , indexName + "/_alias/" + writeAliasName );
459477 assertEquals (200 , response .getStatusLine ().getStatusCode ());
0 commit comments