Skip to content

Commit f144387

Browse files
authored
[7.x] Add GeoIP CLI integration test (#71381) (#71465)
* 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 # Conflicts: # test/fixtures/geoip-fixture/src/main/java/fixture/geoip/GeoIpHttpFixture.java * fix compilation
1 parent 9a250c0 commit f144387

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-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
@@ -58,7 +58,7 @@
5858

5959
public class GeoIpDownloaderIT extends AbstractGeoIpIT {
6060

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

6363
@Override
6464
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
@@ -240,8 +240,8 @@ public void testUseGeoIpProcessorWithDownloadedDBs() throws Exception {
240240
try (Stream<Path> list = Files.list(geoipTmpDir)) {
241241
List<String> files = list.map(Path::getFileName).map(Path::toString).collect(Collectors.toList());
242242
assertThat(files, containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-Country.mmdb", "GeoLite2-ASN.mmdb",
243-
"GeoLite2-City.mmdb_COPYRIGHT.txt","GeoLite2-Country.mmdb_COPYRIGHT.txt","GeoLite2-ASN.mmdb_COPYRIGHT.txt",
244-
"GeoLite2-City.mmdb_LICENSE.txt","GeoLite2-Country.mmdb_LICENSE.txt","GeoLite2-ASN.mmdb_LICENSE.txt",
243+
"GeoLite2-City.mmdb_COPYRIGHT.txt", "GeoLite2-Country.mmdb_COPYRIGHT.txt", "GeoLite2-ASN.mmdb_COPYRIGHT.txt",
244+
"GeoLite2-City.mmdb_LICENSE.txt", "GeoLite2-Country.mmdb_LICENSE.txt", "GeoLite2-ASN.mmdb_LICENSE.txt",
245245
"GeoLite2-ASN.mmdb_README.txt"));
246246
}
247247
}

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@
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.ByteArrayOutputStream;
17+
import java.io.File;
1518
import java.io.InputStream;
1619
import java.io.OutputStream;
1720
import java.io.OutputStreamWriter;
1821
import java.net.InetAddress;
1922
import java.net.InetSocketAddress;
2023
import java.nio.charset.StandardCharsets;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
2126

2227
public class GeoIpHttpFixture {
2328

2429
private final HttpServer server;
2530

2631
GeoIpHttpFixture(final String[] args) throws Exception {
32+
copyFiles();
2733
byte[] bytes = new byte[4096];
2834
int read;
2935
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
@@ -57,6 +63,41 @@ public class GeoIpHttpFixture {
5763
}
5864
}
5965
});
66+
this.server.createContext("/cli", exchange -> {
67+
String fileName = exchange.getRequestURI().getPath().replaceAll(".*/cli/", "");
68+
Path target = new File("target").toPath().resolve(fileName);
69+
if (Files.isRegularFile(target)) {
70+
try (OutputStream outputStream = exchange.getResponseBody();
71+
InputStream db = Files.newInputStream(target)) {
72+
exchange.sendResponseHeaders(200, 0);
73+
int read3;
74+
while((read3 = db.read(bytes)) != -1) {
75+
outputStream.write(bytes, 0, read3);
76+
}
77+
} catch (Exception e) {
78+
exchange.sendResponseHeaders(500, 0);
79+
exchange.getResponseBody().close();
80+
}
81+
} else {
82+
exchange.sendResponseHeaders(404, 0);
83+
exchange.getResponseBody().close();
84+
}
85+
});
86+
}
87+
88+
private void copyFiles() throws Exception {
89+
Path source = new File("source").toPath();
90+
Files.createDirectory(source);
91+
92+
Path target = new File("target").toPath();
93+
Files.createDirectory(target);
94+
95+
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-ASN.tgz"), source.resolve("GeoLite2-ASN.tgz"));
96+
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-City.mmdb"), source.resolve("GeoLite2-City.mmdb"));
97+
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-Country.mmdb"), source.resolve("GeoLite2-Country.mmdb"));
98+
99+
new GeoIpCli().main(new String[]{"-s", source.toAbsolutePath().toString(), "-t", target.toAbsolutePath().toString()},
100+
Terminal.DEFAULT);
60101
}
61102

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

0 commit comments

Comments
 (0)