Fix enrich policy test bug.

Backport #63182 to 7.x branch.

The `randomEnrichPolicy(...)` helper method stores the policy and creates the source indices.
If a source index already exists, because it was creates for a random policy created earlier then
skipping the source index fails, but that is ignored and the test continues. However if the policy
has a match field that doesn't exist in the previous random policy then the mapping is never updated
and the put policy api fails with the fact that the match field can't be found.

This pr fixes that by execute a put mapping call in the event that the source index already exists.

Closes #63126
This commit is contained in:
Martijn van Groningen 2020-10-02 19:29:23 +02:00
parent 752ee0288e
commit 0b6e2b8f16
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
2 changed files with 8 additions and 2 deletions

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.enrich;
import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.service.ClusterService;
@ -66,7 +67,11 @@ public abstract class AbstractEnrichTestCase extends ESSingleNodeTestCase {
try {
client.admin().indices().create(createIndexRequest).actionGet();
} catch (ResourceAlreadyExistsException e) {
// and that is okay
// and that is okay, but update the mapping so that there is always a mapping for match field:
PutMappingRequest putMappingRequest = new PutMappingRequest(sourceIndex);
putMappingRequest.type("_doc");
putMappingRequest.source(policy.getMatchField(), "type=keyword");
client.admin().indices().putMapping(putMappingRequest).actionGet();
}
}
}

View File

@ -176,7 +176,8 @@ public class TransportGetEnrichPolicyActionTests extends AbstractEnrichTestCase
assertThat(error.get(), nullValue());
// save a second one to verify the count below on GET
error = saveEnrichPolicy("something-else", randomEnrichPolicy(XContentType.JSON), clusterService);
EnrichPolicy policy2 = randomEnrichPolicy(XContentType.JSON);
error = saveEnrichPolicy("something-else", policy2, clusterService);
assertThat(error.get(), nullValue());
final CountDownLatch latch = new CountDownLatch(1);