|
7 | 7 |
|
8 | 8 | import org.apache.http.entity.ContentType;
|
9 | 9 | import org.apache.http.entity.StringEntity;
|
| 10 | +import org.elasticsearch.client.Request; |
10 | 11 | import org.elasticsearch.client.Response;
|
11 | 12 | import org.elasticsearch.client.ResponseException;
|
12 | 13 | import org.elasticsearch.common.settings.Settings;
|
@@ -185,23 +186,32 @@ public void testCreateJobsWithIndexNameOption() throws Exception {
|
185 | 186 | + "anomaly_detectors/" + jobId2, Collections.emptyMap(), new StringEntity(jobConfig, ContentType.APPLICATION_JSON));
|
186 | 187 | assertEquals(200, response.getStatusLine().getStatusCode());
|
187 | 188 |
|
188 |
| - response = client().performRequest("get", "_aliases"); |
189 |
| - assertEquals(200, response.getStatusLine().getStatusCode()); |
190 |
| - String responseAsString = responseEntityToString(response); |
| 189 | + // With security enabled GET _aliases throws an index_not_found_exception |
| 190 | + // if no aliases have been created. In multi-node tests the alias may not |
| 191 | + // appear immediately so wait here. |
| 192 | + assertBusy(() -> { |
| 193 | + try { |
| 194 | + Response aliasesResponse = client().performRequest("get", "_aliases"); |
| 195 | + assertEquals(200, aliasesResponse.getStatusLine().getStatusCode()); |
| 196 | + String responseAsString = responseEntityToString(aliasesResponse); |
| 197 | + assertThat(responseAsString, |
| 198 | + containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName("custom-" + indexName) + "\":{\"aliases\":{")); |
| 199 | + assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) |
| 200 | + + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId1 + "\",\"boost\":1.0}}}}")); |
| 201 | + assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId1) + "\":{}")); |
| 202 | + assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) |
| 203 | + + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId2 + "\",\"boost\":1.0}}}}")); |
| 204 | + assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId2) + "\":{}")); |
| 205 | + } catch (ResponseException e) { |
| 206 | + throw new AssertionError(e); |
| 207 | + } |
| 208 | + }); |
191 | 209 |
|
| 210 | + Response indicesResponse = client().performRequest("get", "_cat/indices"); |
| 211 | + assertEquals(200, indicesResponse.getStatusLine().getStatusCode()); |
| 212 | + String responseAsString = responseEntityToString(indicesResponse); |
192 | 213 | 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)); |
| 214 | + containsString(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName)); |
205 | 215 | assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId1))));
|
206 | 216 | assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2))));
|
207 | 217 |
|
@@ -445,15 +455,24 @@ public void testDeleteJobAfterMissingAliases() throws Exception {
|
445 | 455 | String indexName = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT;
|
446 | 456 | createFarequoteJob(jobId);
|
447 | 457 |
|
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)); |
| 458 | + // With security enabled cat aliases throws an index_not_found_exception |
| 459 | + // if no aliases have been created. In multi-node tests the alias may not |
| 460 | + // appear immediately so wait here. |
| 461 | + assertBusy(() -> { |
| 462 | + try { |
| 463 | + Response aliasesResponse = client().performRequest(new Request("get", "_cat/aliases")); |
| 464 | + assertEquals(200, aliasesResponse.getStatusLine().getStatusCode()); |
| 465 | + String responseAsString = responseEntityToString(aliasesResponse); |
| 466 | + assertThat(responseAsString, containsString(readAliasName)); |
| 467 | + assertThat(responseAsString, containsString(writeAliasName)); |
| 468 | + } catch (ResponseException e) { |
| 469 | + throw new AssertionError(e); |
| 470 | + } |
| 471 | + }); |
453 | 472 |
|
454 | 473 | // Manually delete the aliases so that we can test that deletion proceeds
|
455 | 474 | // normally anyway
|
456 |
| - response = client().performRequest("delete", indexName + "/_alias/" + readAliasName); |
| 475 | + Response response = client().performRequest("delete", indexName + "/_alias/" + readAliasName); |
457 | 476 | assertEquals(200, response.getStatusLine().getStatusCode());
|
458 | 477 | response = client().performRequest("delete", indexName + "/_alias/" + writeAliasName);
|
459 | 478 | assertEquals(200, response.getStatusLine().getStatusCode());
|
|
0 commit comments