[TEST] Don't assert order of data frame analytics audit messages (#48065)

Audit messages are stored with millisecond timestamps. If two
messages have the same millisecond timestamp then asserting on
their order is impossible given the information available.

This PR changes the assertion on audit messages in the native
data frame analytics tests to assert that the expected audit
messages exist in any order.

Fixes #48035
This commit is contained in:
David Roberts 2019-10-15 19:58:48 +01:00
parent 3a7c2a4d17
commit d9c7e3847e
1 changed files with 12 additions and 10 deletions

View File

@ -32,17 +32,21 @@ import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
import org.elasticsearch.xpack.core.ml.notifications.AuditorField; import org.elasticsearch.xpack.core.ml.notifications.AuditorField;
import org.elasticsearch.xpack.core.ml.utils.PhaseProgress; import org.elasticsearch.xpack.core.ml.utils.PhaseProgress;
import org.elasticsearch.xpack.ml.dataframe.DataFrameAnalyticsTask; import org.elasticsearch.xpack.ml.dataframe.DataFrameAnalyticsTask;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
/** /**
* Base class of ML integration tests that use a native data_frame_analytics process * Base class of ML integration tests that use a native data_frame_analytics process
@ -187,18 +191,16 @@ abstract class MlNativeDataFrameAnalyticsIntegTestCase extends MlNativeIntegTest
// Since calls to write the AbstractAuditor are sent and forgot (async) we could have returned from the start, // Since calls to write the AbstractAuditor are sent and forgot (async) we could have returned from the start,
// finished the job (as this is a very short analytics job), all without the audit being fully written. // finished the job (as this is a very short analytics job), all without the audit being fully written.
assertBusy(() -> assertTrue(indexExists(AuditorField.NOTIFICATIONS_INDEX))); assertBusy(() -> assertTrue(indexExists(AuditorField.NOTIFICATIONS_INDEX)));
@SuppressWarnings("unchecked")
Matcher<String>[] itemMatchers = Arrays.stream(expectedAuditMessagePrefixes).map(Matchers::startsWith).toArray(Matcher[]::new);
assertBusy(() -> { assertBusy(() -> {
String[] actualAuditMessages = fetchAllAuditMessages(configId); final List<String> allAuditMessages = fetchAllAuditMessages(configId);
assertThat("Messages: " + Arrays.toString(actualAuditMessages), actualAuditMessages.length, assertThat(allAuditMessages, hasItems(itemMatchers));
equalTo(expectedAuditMessagePrefixes.length)); assertThat("Messages: " + allAuditMessages, allAuditMessages, hasSize(expectedAuditMessagePrefixes.length));
for (int i = 0; i < actualAuditMessages.length; i++) {
assertThat(actualAuditMessages[i], startsWith(expectedAuditMessagePrefixes[i]));
}
}); });
} }
@SuppressWarnings("unchecked") private static List<String> fetchAllAuditMessages(String dataFrameAnalyticsId) {
private static String[] fetchAllAuditMessages(String dataFrameAnalyticsId) throws Exception {
RefreshRequest refreshRequest = new RefreshRequest(AuditorField.NOTIFICATIONS_INDEX); RefreshRequest refreshRequest = new RefreshRequest(AuditorField.NOTIFICATIONS_INDEX);
RefreshResponse refreshResponse = client().execute(RefreshAction.INSTANCE, refreshRequest).actionGet(); RefreshResponse refreshResponse = client().execute(RefreshAction.INSTANCE, refreshRequest).actionGet();
assertThat(refreshResponse.getStatus().getStatus(), anyOf(equalTo(200), equalTo(201))); assertThat(refreshResponse.getStatus().getStatus(), anyOf(equalTo(200), equalTo(201)));
@ -212,6 +214,6 @@ abstract class MlNativeDataFrameAnalyticsIntegTestCase extends MlNativeIntegTest
return Arrays.stream(searchResponse.getHits().getHits()) return Arrays.stream(searchResponse.getHits().getHits())
.map(hit -> (String) hit.getSourceAsMap().get("message")) .map(hit -> (String) hit.getSourceAsMap().get("message"))
.toArray(String[]::new); .collect(Collectors.toList());
} }
} }