Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GeoIP CLI integration test #71381

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -61,7 +61,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 Collection<Class<? extends Plugin>> nodePlugins() {
Expand Down Expand Up @@ -247,8 +247,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,6 +9,8 @@
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.InputStream;
Expand All @@ -17,12 +19,15 @@
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();
String data = new String(GeoIpHttpFixture.class.getResourceAsStream("/data.json").readAllBytes(), StandardCharsets.UTF_8);
this.server = HttpServer.create(new InetSocketAddress(InetAddress.getByName(args[0]), Integer.parseInt(args[1])), 0);
this.server.createContext("/", exchange -> {
Expand All @@ -45,6 +50,38 @@ public class GeoIpHttpFixture {
db.transferTo(outputStream);
}
});
this.server.createContext("/cli", exchange -> {
String fileName = exchange.getRequestURI().getPath().replaceAll(".*/cli/", "");
Path target = Path.of("target").resolve(fileName);
if (Files.isRegularFile(target)) {
try (OutputStream outputStream = exchange.getResponseBody();
InputStream db = Files.newInputStream(target)) {
exchange.sendResponseHeaders(200, 0);
db.transferTo(outputStream);
} 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 = Path.of("source");
Files.createDirectory(source);

Path target = Path.of("target");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these paths end up in the build directory of the geoid-fixture module?
Otherwise perhaps we should create tmp dirs instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixtures are run inside docker container which is then discarded, I think we can get away with using current directory

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.