Skip to content

Commit

Permalink
[ML] Update ML mappings upgrade test and extend to config index
Browse files Browse the repository at this point in the history
The ML mappings upgrade test had become useless as it was
checking a field that has been the same since 6.5. This
commit switches to a field that was changed in 7.9.

Additionally, the test only used to check the results index
mappings.  This commit also adds checking for the config
index.

Relates elastic#61157
  • Loading branch information
droberts195 committed Aug 19, 2020
1 parent 622ac75 commit 8a6ec37
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void testMappingsUpgrade() throws Exception {
// We don't know whether the job is on an old or upgraded node, so cannot assert that the mappings have been upgraded
break;
case UPGRADED:
assertUpgradedMappings();
assertUpgradedResultsMappings();
closeAndReopenTestJob();
assertUpgradedConfigMappings();
break;
default:
throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]");
Expand Down Expand Up @@ -77,8 +79,21 @@ private void createAndOpenTestJob() throws IOException {
assertEquals(200, response.getStatusLine().getStatusCode());
}

// Doing this should force the config index mappings to be upgraded,
// when the finished time is cleared on reopening the job
private void closeAndReopenTestJob() throws IOException {

Request closeJob = new Request("POST", "_ml/anomaly_detectors/" + JOB_ID + "/_close");
Response response = client().performRequest(closeJob);
assertEquals(200, response.getStatusLine().getStatusCode());

Request openJob = new Request("POST", "_ml/anomaly_detectors/" + JOB_ID + "/_open");
response = client().performRequest(openJob);
assertEquals(200, response.getStatusLine().getStatusCode());
}

@SuppressWarnings("unchecked")
private void assertUpgradedMappings() throws Exception {
private void assertUpgradedResultsMappings() throws Exception {

assertBusy(() -> {
Request getMappings = new Request("GET", XPackRestTestHelper.resultsWriteAlias(JOB_ID) + "/_mappings");
Expand All @@ -105,8 +120,40 @@ private void assertUpgradedMappings() throws Exception {
assertNotNull(propertiesLevel);
// TODO: as the years go by, the field we assert on here should be changed
// to the most recent field we've added that is NOT of type "keyword"
Map<String, Object> fieldLevel = (Map<String, Object>) propertiesLevel.get("multi_bucket_impact");
assertEquals(Collections.singletonMap("type", "double"), fieldLevel);
Map<String, Object> objectLevel = (Map<String, Object>) propertiesLevel.get("model_size_stats");
assertNotNull(objectLevel);
Map<String, Object> propertiesLevel2 = (Map<String, Object>) objectLevel.get("properties");
assertNotNull(propertiesLevel2);
Map<String, Object> fieldLevel = (Map<String, Object>) propertiesLevel2.get("peak_model_bytes");
assertEquals(Collections.singletonMap("type", "long"), fieldLevel);
});
}

@SuppressWarnings("unchecked")
private void assertUpgradedConfigMappings() throws Exception {

assertBusy(() -> {
Request getMappings = new Request("GET", ".ml-config/_mappings");
Response response = client().performRequest(getMappings);

Map<String, Object> responseLevel = entityAsMap(response);
assertNotNull(responseLevel);
Map<String, Object> indexLevel = (Map<String, Object>) responseLevel.get(".ml-config");
assertNotNull(indexLevel);
Map<String, Object> mappingsLevel = (Map<String, Object>) indexLevel.get("mappings");
assertNotNull(mappingsLevel);
Map<String, Object> metaLevel = (Map<String, Object>) mappingsLevel.get("_meta");
assertEquals(Collections.singletonMap("version", Version.CURRENT.toString()), metaLevel);
Map<String, Object> propertiesLevel = (Map<String, Object>) mappingsLevel.get("properties");
assertNotNull(propertiesLevel);
// TODO: as the years go by, the field we assert on here should be changed
// to the most recent field we've added that is NOT of type "keyword"
Map<String, Object> objectLevel = (Map<String, Object>) propertiesLevel.get("model_plot_config");
assertNotNull(objectLevel);
Map<String, Object> propertiesLevel2 = (Map<String, Object>) objectLevel.get("properties");
assertNotNull(propertiesLevel2);
Map<String, Object> fieldLevel = (Map<String, Object>) propertiesLevel2.get("annotations_enabled");
assertEquals(Collections.singletonMap("type", "boolean"), fieldLevel);
});
}
}

0 comments on commit 8a6ec37

Please sign in to comment.