Skip to content

Commit 79e9dc6

Browse files
authored
[TEST] Don't assert order of data frame analytics audit messages (elastic#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 elastic#48035
1 parent 3d5d96d commit 79e9dc6

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@
3232
import org.elasticsearch.xpack.core.ml.notifications.AuditorField;
3333
import org.elasticsearch.xpack.core.ml.utils.PhaseProgress;
3434
import org.elasticsearch.xpack.ml.dataframe.DataFrameAnalyticsTask;
35+
import org.hamcrest.Matcher;
36+
import org.hamcrest.Matchers;
3537

3638
import java.util.ArrayList;
3739
import java.util.Arrays;
3840
import java.util.List;
3941
import java.util.concurrent.TimeUnit;
42+
import java.util.stream.Collectors;
4043

4144
import static org.hamcrest.Matchers.anyOf;
4245
import static org.hamcrest.Matchers.equalTo;
46+
import static org.hamcrest.Matchers.hasItems;
47+
import static org.hamcrest.Matchers.hasSize;
4348
import static org.hamcrest.Matchers.is;
4449
import static org.hamcrest.Matchers.nullValue;
45-
import static org.hamcrest.Matchers.startsWith;
4650

4751
/**
4852
* Base class of ML integration tests that use a native data_frame_analytics process
@@ -187,18 +191,16 @@ protected static void assertThatAuditMessagesMatch(String configId, String... ex
187191
// Since calls to write the AbstractAuditor are sent and forgot (async) we could have returned from the start,
188192
// finished the job (as this is a very short analytics job), all without the audit being fully written.
189193
assertBusy(() -> assertTrue(indexExists(AuditorField.NOTIFICATIONS_INDEX)));
194+
@SuppressWarnings("unchecked")
195+
Matcher<String>[] itemMatchers = Arrays.stream(expectedAuditMessagePrefixes).map(Matchers::startsWith).toArray(Matcher[]::new);
190196
assertBusy(() -> {
191-
String[] actualAuditMessages = fetchAllAuditMessages(configId);
192-
assertThat("Messages: " + Arrays.toString(actualAuditMessages), actualAuditMessages.length,
193-
equalTo(expectedAuditMessagePrefixes.length));
194-
for (int i = 0; i < actualAuditMessages.length; i++) {
195-
assertThat(actualAuditMessages[i], startsWith(expectedAuditMessagePrefixes[i]));
196-
}
197+
final List<String> allAuditMessages = fetchAllAuditMessages(configId);
198+
assertThat(allAuditMessages, hasItems(itemMatchers));
199+
assertThat("Messages: " + allAuditMessages, allAuditMessages, hasSize(expectedAuditMessagePrefixes.length));
197200
});
198201
}
199202

200-
@SuppressWarnings("unchecked")
201-
private static String[] fetchAllAuditMessages(String dataFrameAnalyticsId) throws Exception {
203+
private static List<String> fetchAllAuditMessages(String dataFrameAnalyticsId) {
202204
RefreshRequest refreshRequest = new RefreshRequest(AuditorField.NOTIFICATIONS_INDEX);
203205
RefreshResponse refreshResponse = client().execute(RefreshAction.INSTANCE, refreshRequest).actionGet();
204206
assertThat(refreshResponse.getStatus().getStatus(), anyOf(equalTo(200), equalTo(201)));
@@ -212,6 +214,6 @@ private static String[] fetchAllAuditMessages(String dataFrameAnalyticsId) throw
212214

213215
return Arrays.stream(searchResponse.getHits().getHits())
214216
.map(hit -> (String) hit.getSourceAsMap().get("message"))
215-
.toArray(String[]::new);
217+
.collect(Collectors.toList());
216218
}
217219
}

0 commit comments

Comments
 (0)