44
44
import java .util .Map ;
45
45
import java .util .Set ;
46
46
47
+ import static org .hamcrest .Matchers .containsString ;
47
48
import static org .hamcrest .Matchers .equalTo ;
49
+ import static org .hamcrest .Matchers .hasToString ;
48
50
import static org .hamcrest .Matchers .sameInstance ;
49
51
50
52
public class GeoIpProcessorFactoryTests extends ESTestCase {
@@ -60,17 +62,13 @@ public static void loadDatabaseReaders() throws IOException {
60
62
return ;
61
63
}
62
64
63
- Path configDir = createTempDir ();
64
- Path geoIpConfigDir = configDir .resolve ("ingest-geoip" );
65
+ final Path geoIpDir = createTempDir ();
66
+ final Path configDir = createTempDir ();
67
+ final Path geoIpConfigDir = configDir .resolve ("ingest-geoip" );
65
68
Files .createDirectories (geoIpConfigDir );
66
- Files .copy (new ByteArrayInputStream (StreamsUtils .copyToBytesFromClasspath ("/GeoLite2-City.mmdb" )),
67
- geoIpConfigDir .resolve ("GeoLite2-City.mmdb" ));
68
- Files .copy (new ByteArrayInputStream (StreamsUtils .copyToBytesFromClasspath ("/GeoLite2-Country.mmdb" )),
69
- geoIpConfigDir .resolve ("GeoLite2-Country.mmdb" ));
70
- Files .copy (new ByteArrayInputStream (StreamsUtils .copyToBytesFromClasspath ("/GeoLite2-ASN.mmdb" )),
71
- geoIpConfigDir .resolve ("GeoLite2-ASN.mmdb" ));
72
-
73
- databaseReaders = IngestGeoIpPlugin .loadDatabaseReaders (geoIpConfigDir );
69
+ copyDatabaseFiles (geoIpDir );
70
+
71
+ databaseReaders = IngestGeoIpPlugin .loadDatabaseReaders (geoIpDir , geoIpConfigDir );
74
72
}
75
73
76
74
@ AfterClass
@@ -297,21 +295,16 @@ public void testLazyLoading() throws Exception {
297
295
// This test uses a MappedByteBuffer which will keep the file mappings active until it is garbage-collected.
298
296
// As a consequence, the corresponding file appears to be still in use and Windows cannot delete it.
299
297
assumeFalse ("windows deletion behavior is asinine" , Constants .WINDOWS );
300
- Path configDir = createTempDir ();
301
- Path geoIpConfigDir = configDir .resolve ("ingest-geoip" );
298
+ final Path geoIpDir = createTempDir ();
299
+ final Path configDir = createTempDir ();
300
+ final Path geoIpConfigDir = configDir .resolve ("ingest-geoip" );
302
301
Files .createDirectories (geoIpConfigDir );
303
- Files .copy (new ByteArrayInputStream (StreamsUtils .copyToBytesFromClasspath ("/GeoLite2-City.mmdb" )),
304
- geoIpConfigDir .resolve ("GeoLite2-City.mmdb" ));
305
- Files .copy (new ByteArrayInputStream (StreamsUtils .copyToBytesFromClasspath ("/GeoLite2-Country.mmdb" )),
306
- geoIpConfigDir .resolve ("GeoLite2-Country.mmdb" ));
307
- Files .copy (new ByteArrayInputStream (StreamsUtils .copyToBytesFromClasspath ("/GeoLite2-ASN.mmdb" )),
308
- geoIpConfigDir .resolve ("GeoLite2-ASN.mmdb" ));
302
+ copyDatabaseFiles (geoIpDir );
309
303
310
304
// Loading another database reader instances, because otherwise we can't test lazy loading as the
311
305
// database readers used at class level are reused between tests. (we want to keep that otherwise running this
312
306
// test will take roughly 4 times more time)
313
- Map <String , DatabaseReaderLazyLoader > databaseReaders =
314
- IngestGeoIpPlugin .loadDatabaseReaders (geoIpConfigDir );
307
+ Map <String , DatabaseReaderLazyLoader > databaseReaders = IngestGeoIpPlugin .loadDatabaseReaders (geoIpDir , geoIpConfigDir );
315
308
GeoIpProcessor .Factory factory = new GeoIpProcessor .Factory (databaseReaders , new GeoIpCache (1000 ));
316
309
for (DatabaseReaderLazyLoader lazyLoader : databaseReaders .values ()) {
317
310
assertNull (lazyLoader .databaseReader .get ());
@@ -354,4 +347,79 @@ public void testLazyLoading() throws Exception {
354
347
assertNotNull (databaseReaders .get ("GeoLite2-ASN.mmdb" ).databaseReader .get ());
355
348
}
356
349
350
+ public void testLoadingCustomDatabase () throws IOException {
351
+ final Path geoIpDir = createTempDir ();
352
+ final Path configDir = createTempDir ();
353
+ final Path geoIpConfigDir = configDir .resolve ("ingest-geoip" );
354
+ Files .createDirectories (geoIpConfigDir );
355
+ copyDatabaseFiles (geoIpDir );
356
+ // fake the GeoIP2-City database
357
+ copyDatabaseFile (geoIpConfigDir , "GeoLite2-City.mmdb" );
358
+ Files .move (geoIpConfigDir .resolve ("GeoLite2-City.mmdb" ), geoIpConfigDir .resolve ("GeoIP2-City.mmdb" ));
359
+
360
+ /*
361
+ * Loading another database reader instances, because otherwise we can't test lazy loading as the database readers used at class
362
+ * level are reused between tests. (we want to keep that otherwise running this test will take roughly 4 times more time).
363
+ */
364
+ final Map <String , DatabaseReaderLazyLoader > databaseReaders = IngestGeoIpPlugin .loadDatabaseReaders (geoIpDir , geoIpConfigDir );
365
+ final GeoIpProcessor .Factory factory = new GeoIpProcessor .Factory (databaseReaders , new GeoIpCache (1000 ));
366
+ for (DatabaseReaderLazyLoader lazyLoader : databaseReaders .values ()) {
367
+ assertNull (lazyLoader .databaseReader .get ());
368
+ }
369
+
370
+ final Map <String , Object > field = Collections .singletonMap ("_field" , "1.1.1.1" );
371
+ final IngestDocument document = new IngestDocument ("index" , "type" , "id" , "routing" , 1L , VersionType .EXTERNAL , field );
372
+
373
+ Map <String , Object > config = new HashMap <>();
374
+ config .put ("field" , "_field" );
375
+ config .put ("database_file" , "GeoIP2-City.mmdb" );
376
+ final GeoIpProcessor city = factory .create (null , "_tag" , config );
377
+
378
+ // these are lazy loaded until first use so we expect null here
379
+ assertNull (databaseReaders .get ("GeoIP2-City.mmdb" ).databaseReader .get ());
380
+ city .execute (document );
381
+ // the first ingest should trigger a database load
382
+ assertNotNull (databaseReaders .get ("GeoIP2-City.mmdb" ).databaseReader .get ());
383
+ }
384
+
385
+ public void testDatabaseNotExistsInDir () throws IOException {
386
+ final Path geoIpDir = createTempDir ();
387
+ final Path configDir = createTempDir ();
388
+ final Path geoIpConfigDir = configDir .resolve ("ingest-geoip" );
389
+ if (randomBoolean ()) {
390
+ Files .createDirectories (geoIpConfigDir );
391
+ }
392
+ copyDatabaseFiles (geoIpDir );
393
+ final String databaseFilename = randomFrom (IngestGeoIpPlugin .DEFAULT_DATABASE_FILENAMES );
394
+ Files .delete (geoIpDir .resolve (databaseFilename ));
395
+ final IOException e =
396
+ expectThrows (IOException .class , () -> IngestGeoIpPlugin .loadDatabaseReaders (geoIpDir , geoIpConfigDir ));
397
+ assertThat (e , hasToString (containsString ("expected database [" + databaseFilename + "] to exist in [" + geoIpDir + "]" )));
398
+ }
399
+
400
+ public void testDatabaseExistsInConfigDir () throws IOException {
401
+ final Path geoIpDir = createTempDir ();
402
+ final Path configDir = createTempDir ();
403
+ final Path geoIpConfigDir = configDir .resolve ("ingest-geoip" );
404
+ Files .createDirectories (geoIpConfigDir );
405
+ copyDatabaseFiles (geoIpDir );
406
+ final String databaseFilename = randomFrom (IngestGeoIpPlugin .DEFAULT_DATABASE_FILENAMES );
407
+ copyDatabaseFile (geoIpConfigDir , databaseFilename );
408
+ final IOException e =
409
+ expectThrows (IOException .class , () -> IngestGeoIpPlugin .loadDatabaseReaders (geoIpDir , geoIpConfigDir ));
410
+ assertThat (e , hasToString (containsString ("expected database [" + databaseFilename + "] to not exist in [" + geoIpConfigDir + "]" )));
411
+ }
412
+
413
+ private static void copyDatabaseFile (final Path path , final String databaseFilename ) throws IOException {
414
+ Files .copy (
415
+ new ByteArrayInputStream (StreamsUtils .copyToBytesFromClasspath ("/" + databaseFilename )),
416
+ path .resolve (databaseFilename ));
417
+ }
418
+
419
+ private static void copyDatabaseFiles (final Path path ) throws IOException {
420
+ for (final String databaseFilename : IngestGeoIpPlugin .DEFAULT_DATABASE_FILENAMES ) {
421
+ copyDatabaseFile (path , databaseFilename );
422
+ }
423
+ }
424
+
357
425
}
0 commit comments