Skip to content

Commit

Permalink
[7.x] Add GeoIP CLI integration test (#71381) (#71465)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
probakowski authored Apr 8, 2021
1 parent 9a250c0 commit f144387
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.ingest.geoip;

import org.elasticsearch.common.settings.Settings;

public class GeoIpDownloaderCliIT extends GeoIpDownloaderIT {

@Override
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
Settings.Builder settings = Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings));
if (ENDPOINT != null) {
settings.put(GeoIpDownloader.ENDPOINT_SETTING.getKey(), ENDPOINT + "cli/overview.json");
}
return settings.build();
}

public void testUseGeoIpProcessorWithDownloadedDBs() {
assumeTrue("this test can't work with CLI (some expected files are missing)", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

public class GeoIpDownloaderIT extends AbstractGeoIpIT {

private static final String ENDPOINT = System.getProperty("geoip_endpoint");
protected static final String ENDPOINT = System.getProperty("geoip_endpoint");

@Override
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
Expand Down Expand Up @@ -240,8 +240,8 @@ public void testUseGeoIpProcessorWithDownloadedDBs() throws Exception {
try (Stream<Path> list = Files.list(geoipTmpDir)) {
List<String> files = list.map(Path::getFileName).map(Path::toString).collect(Collectors.toList());
assertThat(files, containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-Country.mmdb", "GeoLite2-ASN.mmdb",
"GeoLite2-City.mmdb_COPYRIGHT.txt","GeoLite2-Country.mmdb_COPYRIGHT.txt","GeoLite2-ASN.mmdb_COPYRIGHT.txt",
"GeoLite2-City.mmdb_LICENSE.txt","GeoLite2-Country.mmdb_LICENSE.txt","GeoLite2-ASN.mmdb_LICENSE.txt",
"GeoLite2-City.mmdb_COPYRIGHT.txt", "GeoLite2-Country.mmdb_COPYRIGHT.txt", "GeoLite2-ASN.mmdb_COPYRIGHT.txt",
"GeoLite2-City.mmdb_LICENSE.txt", "GeoLite2-Country.mmdb_LICENSE.txt", "GeoLite2-ASN.mmdb_LICENSE.txt",
"GeoLite2-ASN.mmdb_README.txt"));
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/geoip-fixture/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ tasks.named("test").configure { enabled = false }

dependencies {
api project(':server')
api project(':distribution:tools:geoip-cli')
api project(":libs:elasticsearch-cli")
api project(":libs:elasticsearch-x-content")
}

tasks.named("preProcessFixture").configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
package fixture.geoip;

import com.sun.net.httpserver.HttpServer;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.geoip.GeoIpCli;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class GeoIpHttpFixture {

private final HttpServer server;

GeoIpHttpFixture(final String[] args) throws Exception {
copyFiles();
byte[] bytes = new byte[4096];
int read;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Expand Down Expand Up @@ -57,6 +63,41 @@ public class GeoIpHttpFixture {
}
}
});
this.server.createContext("/cli", exchange -> {
String fileName = exchange.getRequestURI().getPath().replaceAll(".*/cli/", "");
Path target = new File("target").toPath().resolve(fileName);
if (Files.isRegularFile(target)) {
try (OutputStream outputStream = exchange.getResponseBody();
InputStream db = Files.newInputStream(target)) {
exchange.sendResponseHeaders(200, 0);
int read3;
while((read3 = db.read(bytes)) != -1) {
outputStream.write(bytes, 0, read3);
}
} catch (Exception e) {
exchange.sendResponseHeaders(500, 0);
exchange.getResponseBody().close();
}
} else {
exchange.sendResponseHeaders(404, 0);
exchange.getResponseBody().close();
}
});
}

private void copyFiles() throws Exception {
Path source = new File("source").toPath();
Files.createDirectory(source);

Path target = new File("target").toPath();
Files.createDirectory(target);

Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-ASN.tgz"), source.resolve("GeoLite2-ASN.tgz"));
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-City.mmdb"), source.resolve("GeoLite2-City.mmdb"));
Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-Country.mmdb"), source.resolve("GeoLite2-Country.mmdb"));

new GeoIpCli().main(new String[]{"-s", source.toAbsolutePath().toString(), "-t", target.toAbsolutePath().toString()},
Terminal.DEFAULT);
}

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

0 comments on commit f144387

Please sign in to comment.