Skip to content

Commit

Permalink
Fix EnrichSecurityIT resource cleanup (#85469)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Mar 30, 2022
1 parent b1b54d5 commit 42b0b39
Showing 1 changed file with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.After;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -33,8 +34,28 @@

public abstract class CommonEnrichRestTestCase extends ESRestTestCase {

private List<String> cleanupPipelines = new ArrayList<>();

/**
* Registers a pipeline for subsequent post-test clean up (i.e. DELETE),
* see {@link CommonEnrichRestTestCase#deletePipelinesAndPolicies()}.
*
* @param pipeline the name of the pipeline to clean up
*/
public void cleanupPipelineAfterTest(String pipeline) {
cleanupPipelines.add(pipeline);
}

@After
public void deletePolicies() throws Exception {
public void deletePipelinesAndPolicies() throws Exception {
// delete pipelines
for (String pipeline : cleanupPipelines) {
String endpoint = "/_ingest/pipeline/" + pipeline;
assertOK(client().performRequest(new Request("DELETE", endpoint)));
}
cleanupPipelines.clear();

// delete all policies
Map<String, Object> responseMap = toMap(adminClient().performRequest(new Request("GET", "/_enrich/policy")));
List<Map<?, ?>> policies = unsafeGetProperty(responseMap, "policies");

Expand All @@ -60,7 +81,7 @@ private <Property> Property unsafeGetProperty(Map<?, ?> map, String key) {
return (Property) map.get(key);
}

private void setupGenericLifecycleTest(boolean deletePipeilne, String field, String type, String value) throws Exception {
private void setupGenericLifecycleTest(String field, String type, String value) throws Exception {
// Create source index:
createSourceIndex("my-source-index");

Expand Down Expand Up @@ -114,6 +135,7 @@ private void setupGenericLifecycleTest(boolean deletePipeilne, String field, Str
"processors": [ { "enrich": { "policy_name": "my_policy", "field": "%s", "target_field": "entry" } } ]
}""", field));
assertOK(client().performRequest(putPipelineRequest));
cleanupPipelineAfterTest("my_pipeline");

// Index document using pipeline with enrich processor:
indexRequest = new Request("PUT", "/my-index/_doc/1");
Expand All @@ -132,45 +154,40 @@ private void setupGenericLifecycleTest(boolean deletePipeilne, String field, Str
assertThat(entry.get("tldRank"), equalTo(7));
Object originalMatchValue = ((Map<?, ?>) response.get("_source")).get(field);
assertThat(originalMatchValue, equalTo(value));

if (deletePipeilne) {
// delete the pipeline so the policies can be deleted
client().performRequest(new Request("DELETE", "/_ingest/pipeline/my_pipeline"));
}
}

public void testBasicFlowKeyword() throws Exception {
setupGenericLifecycleTest(true, "host", "match", "elastic.co");
setupGenericLifecycleTest("host", "match", "elastic.co");
assertBusy(CommonEnrichRestTestCase::verifyEnrichMonitoring, 3, TimeUnit.MINUTES);
}

public void testBasicFlowDate() throws Exception {
setupGenericLifecycleTest(true, "date", "range", "2021-09-06");
setupGenericLifecycleTest("date", "range", "2021-09-06");
assertBusy(CommonEnrichRestTestCase::verifyEnrichMonitoring, 3, TimeUnit.MINUTES);
}

public void testBasicFlowInteger() throws Exception {
setupGenericLifecycleTest(true, "integer", "range", "41");
setupGenericLifecycleTest("integer", "range", "41");
assertBusy(CommonEnrichRestTestCase::verifyEnrichMonitoring, 3, TimeUnit.MINUTES);
}

public void testBasicFlowLong() throws Exception {
setupGenericLifecycleTest(true, "long", "range", "8411017");
setupGenericLifecycleTest("long", "range", "8411017");
assertBusy(CommonEnrichRestTestCase::verifyEnrichMonitoring, 3, TimeUnit.MINUTES);
}

public void testBasicFlowDouble() throws Exception {
setupGenericLifecycleTest(true, "double", "range", "15.15");
setupGenericLifecycleTest("double", "range", "15.15");
assertBusy(CommonEnrichRestTestCase::verifyEnrichMonitoring, 3, TimeUnit.MINUTES);
}

public void testBasicFlowFloat() throws Exception {
setupGenericLifecycleTest(true, "float", "range", "10000.66666");
setupGenericLifecycleTest("float", "range", "10000.66666");
assertBusy(CommonEnrichRestTestCase::verifyEnrichMonitoring, 3, TimeUnit.MINUTES);
}

public void testBasicFlowIp() throws Exception {
setupGenericLifecycleTest(true, "ip", "range", "100.120.140.160");
setupGenericLifecycleTest("ip", "range", "100.120.140.160");
assertBusy(CommonEnrichRestTestCase::verifyEnrichMonitoring, 3, TimeUnit.MINUTES);
}

Expand Down Expand Up @@ -198,15 +215,15 @@ public void testDeleteIsCaseSensitive() throws Exception {
}

public void testDeleteExistingPipeline() throws Exception {
// lets not delete the pipeline at first, to test the failure
setupGenericLifecycleTest(false, "host", "match", "elastic.co");
setupGenericLifecycleTest("host", "match", "elastic.co");

Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/another_pipeline");
putPipelineRequest.setJsonEntity("""
{
"processors": [ { "enrich": { "policy_name": "my_policy", "field": "host", "target_field": "entry" } } ]
}""");
assertOK(client().performRequest(putPipelineRequest));
cleanupPipelineAfterTest("another_pipeline");

ResponseException exc = expectThrows(
ResponseException.class,
Expand All @@ -216,10 +233,6 @@ public void testDeleteExistingPipeline() throws Exception {
assertTrue(exc.getMessage().contains("another_pipeline"));
assertTrue(exc.getMessage().contains("my_pipeline"));

// delete the pipelines so the policies can be deleted
client().performRequest(new Request("DELETE", "/_ingest/pipeline/my_pipeline"));
client().performRequest(new Request("DELETE", "/_ingest/pipeline/another_pipeline"));

// verify the delete did not happen
Request getRequest = new Request("GET", "/_enrich/policy/my_policy");
assertOK(client().performRequest(getRequest));
Expand Down

0 comments on commit 42b0b39

Please sign in to comment.