Skip to content

Commit 44a2ae4

Browse files
authored
Add GeoIP CLI integration test (#71381)
This change adds additional test to GeoIpDownloaderIT which tests that artifacts produces by GeoIP CLI tool can be consumed by cluster the same way as from our original service. It does so by running the tool from fixture which then simply serves the generated files (this is exactly the way users are supposed to use the tool as well). Relates to #68920
1 parent 4b38b29 commit 44a2ae4

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.ingest.geoip;
10+
11+
import org.elasticsearch.common.settings.Settings;
12+
13+
public class GeoIpDownloaderCliIT extends GeoIpDownloaderIT {
14+
15+
@Override
16+
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
17+
Settings.Builder settings = Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings));
18+
if (ENDPOINT != null) {
19+
settings.put(GeoIpDownloader.ENDPOINT_SETTING.getKey(), ENDPOINT + "cli/overview.json");
20+
}
21+
return settings.build();
22+
}
23+
24+
public void testUseGeoIpProcessorWithDownloadedDBs() {
25+
assumeTrue("this test can't work with CLI (some expected files are missing)", false);
26+
}
27+
}

modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
public class GeoIpDownloaderIT extends AbstractGeoIpIT {
6363

64-
private static final String ENDPOINT = System.getProperty("geoip_endpoint");
64+
protected static final String ENDPOINT = System.getProperty("geoip_endpoint");
6565

6666
@Override
6767
protected Collection<Class<? extends Plugin>> nodePlugins() {
@@ -247,8 +247,8 @@ public void testUseGeoIpProcessorWithDownloadedDBs() throws Exception {
247247
try (Stream<Path> list = Files.list(geoipTmpDir)) {
248248
List<String> files = list.map(Path::getFileName).map(Path::toString).collect(Collectors.toList());
249249
assertThat(files, containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-Country.mmdb", "GeoLite2-ASN.mmdb",
250-
"GeoLite2-City.mmdb_COPYRIGHT.txt","GeoLite2-Country.mmdb_COPYRIGHT.txt","GeoLite2-ASN.mmdb_COPYRIGHT.txt",
251-
"GeoLite2-City.mmdb_LICENSE.txt","GeoLite2-Country.mmdb_LICENSE.txt","GeoLite2-ASN.mmdb_LICENSE.txt",
250+
"GeoLite2-City.mmdb_COPYRIGHT.txt", "GeoLite2-Country.mmdb_COPYRIGHT.txt", "GeoLite2-ASN.mmdb_COPYRIGHT.txt",
251+
"GeoLite2-City.mmdb_LICENSE.txt", "GeoLite2-Country.mmdb_LICENSE.txt", "GeoLite2-ASN.mmdb_LICENSE.txt",
252252
"GeoLite2-ASN.mmdb_README.txt"));
253253
}
254254
}

test/fixtures/geoip-fixture/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ tasks.named("test").configure { enabled = false }
1313

1414
dependencies {
1515
api project(':server')
16+
api project(':distribution:tools:geoip-cli')
17+
api project(":libs:elasticsearch-cli")
18+
api project(":libs:elasticsearch-x-content")
1619
}
1720

1821
tasks.named("preProcessFixture").configure {

test/fixtures/geoip-fixture/src/main/java/fixture/geoip/GeoIpHttpFixture.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
package fixture.geoip;
1010

1111
import com.sun.net.httpserver.HttpServer;
12+
import org.elasticsearch.cli.Terminal;
13+
import org.elasticsearch.geoip.GeoIpCli;
1214

1315
import java.io.BufferedWriter;
1416
import java.io.InputStream;
@@ -17,12 +19,15 @@
1719
import java.net.InetAddress;
1820
import java.net.InetSocketAddress;
1921
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
2024

2125
public class GeoIpHttpFixture {
2226

2327
private final HttpServer server;
2428

2529
GeoIpHttpFixture(final String[] args) throws Exception {
30+
copyFiles();
2631
String data = new String(GeoIpHttpFixture.class.getResourceAsStream("/data.json").readAllBytes(), StandardCharsets.UTF_8);
2732
this.server = HttpServer.create(new InetSocketAddress(InetAddress.getByName(args[0]), Integer.parseInt(args[1])), 0);
2833
this.server.createContext("/", exchange -> {
@@ -45,6 +50,38 @@ public class GeoIpHttpFixture {
4550
db.transferTo(outputStream);
4651
}
4752
});
53+
this.server.createContext("/cli", exchange -> {
54+
String fileName = exchange.getRequestURI().getPath().replaceAll(".*/cli/", "");
55+
Path target = Path.of("target").resolve(fileName);
56+
if (Files.isRegularFile(target)) {
57+
try (OutputStream outputStream = exchange.getResponseBody();
58+
InputStream db = Files.newInputStream(target)) {
59+
exchange.sendResponseHeaders(200, 0);
60+
db.transferTo(outputStream);
61+
} catch (Exception e) {
62+
exchange.sendResponseHeaders(500, 0);
63+
exchange.getResponseBody().close();
64+
}
65+
} else {
66+
exchange.sendResponseHeaders(404, 0);
67+
exchange.getResponseBody().close();
68+
}
69+
});
70+
}
71+
72+
private void copyFiles() throws Exception {
73+
Path source = Path.of("source");
74+
Files.createDirectory(source);
75+
76+
Path target = Path.of("target");
77+
Files.createDirectory(target);
78+
79+
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-ASN.tgz"), source.resolve("GeoLite2-ASN.tgz"));
80+
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-City.mmdb"), source.resolve("GeoLite2-City.mmdb"));
81+
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-Country.mmdb"), source.resolve("GeoLite2-Country.mmdb"));
82+
83+
new GeoIpCli().main(new String[]{"-s", source.toAbsolutePath().toString(), "-t", target.toAbsolutePath().toString()},
84+
Terminal.DEFAULT);
4885
}
4986

5087
final void start() throws Exception {
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)