Skip to content

Commit ee11eba

Browse files
committed
share test code
1 parent 348494c commit ee11eba

File tree

4 files changed

+31
-88
lines changed

4 files changed

+31
-88
lines changed

modules/ingest-geoip/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.elasticsearch.gradle.OS
1212
apply plugin: 'elasticsearch.internal-yaml-rest-test'
1313
apply plugin: 'elasticsearch.yaml-rest-compat-test'
1414
apply plugin: 'elasticsearch.internal-cluster-test'
15+
apply plugin: 'elasticsearch.internal-test-artifact'
1516

1617
esplugin {
1718
description = 'Ingest processor that uses lookup geo data based on IP addresses using the MaxMind geo database'

modules/ingest-geoip/qa/multi-project/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
javaRestTestImplementation project(':test:external-modules:test-multi-project')
1717
javaRestTestImplementation project(':test:fixtures:geoip-fixture')
1818

19+
yamlRestTestImplementation(testArtifact(project(":modules:ingest-geoip"), "yamlRestTest")) // includes yaml test code from ingest-geoip
1920
yamlRestTestImplementation project(':modules:ingest-geoip')
2021
yamlRestTestImplementation project(':test:external-modules:test-multi-project')
2122
yamlRestTestImplementation project(':test:fixtures:geoip-fixture')

modules/ingest-geoip/qa/multi-project/src/yamlRestTest/java/org/elasticsearch/ingest/geoip/IngestGeoIpClientMultiProjectYamlTestSuiteIT.java

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,18 @@
1414
import com.carrotsearch.randomizedtesting.annotations.Name;
1515
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
1616

17-
import org.apache.http.entity.ByteArrayEntity;
18-
import org.apache.http.entity.ContentType;
19-
import org.elasticsearch.client.Request;
20-
import org.elasticsearch.common.bytes.BytesReference;
2117
import org.elasticsearch.core.Booleans;
2218
import org.elasticsearch.multiproject.test.MultipleProjectsClientYamlSuiteTestCase;
2319
import org.elasticsearch.test.cluster.ElasticsearchCluster;
2420
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
2521
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
26-
import org.elasticsearch.xcontent.XContentBuilder;
27-
import org.elasticsearch.xcontent.json.JsonXContent;
2822
import org.junit.Before;
2923
import org.junit.ClassRule;
3024
import org.junit.rules.RuleChain;
3125
import org.junit.rules.TestRule;
3226

33-
import java.io.IOException;
34-
import java.util.List;
35-
import java.util.Map;
36-
import java.util.concurrent.TimeUnit;
37-
38-
import static org.hamcrest.Matchers.containsInAnyOrder;
39-
import static org.hamcrest.Matchers.equalTo;
40-
import static org.hamcrest.Matchers.notNullValue;
27+
import static org.elasticsearch.ingest.geoip.IngestGeoIpClientYamlTestSuiteIT.assertDatabasesLoaded;
28+
import static org.elasticsearch.ingest.geoip.IngestGeoIpClientYamlTestSuiteIT.putGeoipPipeline;
4129

4230
public class IngestGeoIpClientMultiProjectYamlTestSuiteIT extends MultipleProjectsClientYamlSuiteTestCase {
4331

@@ -79,58 +67,7 @@ public static Iterable<Object[]> parameters() throws Exception {
7967

8068
@Before
8169
public void waitForDatabases() throws Exception {
82-
putGeoipPipeline();
83-
assertBusy(() -> {
84-
Request request = new Request("GET", "/_ingest/geoip/stats");
85-
Map<String, Object> response = entityAsMap(client().performRequest(request));
86-
// assert databases are downloaded
87-
Map<?, ?> downloadStats = (Map<?, ?>) response.get("stats");
88-
assertThat(downloadStats.get("databases_count"), equalTo(4));
89-
// assert databases are loaded to node
90-
Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");
91-
assertThat(nodes.size(), equalTo(1));
92-
Map<?, ?> node = (Map<?, ?>) nodes.values().iterator().next();
93-
List<?> databases = ((List<?>) node.get("databases"));
94-
assertThat(databases, notNullValue());
95-
List<String> databaseNames = databases.stream().map(o -> (String) ((Map<?, ?>) o).get("name")).toList();
96-
assertThat(
97-
databaseNames,
98-
containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-Country.mmdb", "GeoLite2-ASN.mmdb", "MyCustomGeoLite2-City.mmdb")
99-
);
100-
}, 10, TimeUnit.SECONDS);
101-
}
102-
103-
/**
104-
* This creates a pipeline with a geoip processor so that the GeoipDownloader will download its databases.
105-
* @throws IOException
106-
*/
107-
private void putGeoipPipeline() throws IOException {
108-
final BytesReference bytes;
109-
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
110-
builder.startObject();
111-
{
112-
builder.startArray("processors");
113-
{
114-
builder.startObject();
115-
{
116-
builder.startObject("geoip");
117-
{
118-
builder.field("field", "ip");
119-
builder.field("target_field", "ip-city");
120-
builder.field("database_file", "GeoLite2-City.mmdb");
121-
}
122-
builder.endObject();
123-
}
124-
builder.endObject();
125-
}
126-
builder.endArray();
127-
}
128-
builder.endObject();
129-
bytes = BytesReference.bytes(builder);
130-
}
131-
Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/pipeline-with-geoip");
132-
putPipelineRequest.setEntity(new ByteArrayEntity(bytes.array(), ContentType.APPLICATION_JSON));
133-
client().performRequest(putPipelineRequest);
70+
putGeoipPipeline("pipeline-with-geoip");
71+
assertDatabasesLoaded();
13472
}
135-
13673
}

modules/ingest-geoip/src/yamlRestTest/java/org/elasticsearch/ingest/geoip/IngestGeoIpClientYamlTestSuiteIT.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,15 @@ public static Iterable<Object[]> parameters() throws Exception {
7474

7575
@Before
7676
public void waitForDatabases() throws Exception {
77-
putGeoipPipeline();
78-
assertBusy(() -> {
79-
Request request = new Request("GET", "/_ingest/geoip/stats");
80-
Map<String, Object> response = entityAsMap(client().performRequest(request));
81-
82-
Map<?, ?> downloadStats = (Map<?, ?>) response.get("stats");
83-
assertThat(downloadStats.get("databases_count"), equalTo(4));
84-
85-
Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");
86-
assertThat(nodes.size(), equalTo(1));
87-
Map<?, ?> node = (Map<?, ?>) nodes.values().iterator().next();
88-
List<?> databases = ((List<?>) node.get("databases"));
89-
assertThat(databases, notNullValue());
90-
List<String> databaseNames = databases.stream().map(o -> (String) ((Map<?, ?>) o).get("name")).toList();
91-
assertThat(
92-
databaseNames,
93-
containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-Country.mmdb", "GeoLite2-ASN.mmdb", "MyCustomGeoLite2-City.mmdb")
94-
);
95-
});
77+
putGeoipPipeline("pipeline-with-geoip");
78+
assertDatabasesLoaded();
9679
}
9780

9881
/**
9982
* This creates a pipeline with a geoip processor so that the GeoipDownloader will download its databases.
10083
* @throws IOException
10184
*/
102-
private void putGeoipPipeline() throws IOException {
85+
static void putGeoipPipeline(String pipelineName) throws Exception {
10386
final BytesReference bytes;
10487
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
10588
builder.startObject();
@@ -123,9 +106,30 @@ private void putGeoipPipeline() throws IOException {
123106
builder.endObject();
124107
bytes = BytesReference.bytes(builder);
125108
}
126-
Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/pipeline-with-geoip");
109+
Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/" + pipelineName);
127110
putPipelineRequest.setEntity(new ByteArrayEntity(bytes.array(), ContentType.APPLICATION_JSON));
128111
client().performRequest(putPipelineRequest);
129112
}
130113

114+
static void assertDatabasesLoaded() throws Exception {
115+
// assert that the databases are downloaded and loaded
116+
assertBusy(() -> {
117+
Request request = new Request("GET", "/_ingest/geoip/stats");
118+
Map<String, Object> response = entityAsMap(client().performRequest(request));
119+
120+
Map<?, ?> downloadStats = (Map<?, ?>) response.get("stats");
121+
assertThat(downloadStats.get("databases_count"), equalTo(4));
122+
123+
Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");
124+
assertThat(nodes.size(), equalTo(1));
125+
Map<?, ?> node = (Map<?, ?>) nodes.values().iterator().next();
126+
List<?> databases = ((List<?>) node.get("databases"));
127+
assertThat(databases, notNullValue());
128+
List<String> databaseNames = databases.stream().map(o -> (String) ((Map<?, ?>) o).get("name")).toList();
129+
assertThat(
130+
databaseNames,
131+
containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-Country.mmdb", "GeoLite2-ASN.mmdb", "MyCustomGeoLite2-City.mmdb")
132+
);
133+
});
134+
}
131135
}

0 commit comments

Comments
 (0)