fluent-plugin-geoip 0.8.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,31 +1,7 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- begin
4
- Bundler.setup(:default, :development)
5
- rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
9
- end
1
+ require 'bundler/setup'
10
2
  require 'test/unit'
11
3
 
12
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
- $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
5
+ $LOAD_PATH.unshift(__dir__)
14
6
  require 'fluent/test'
15
- unless ENV.has_key?('VERBOSE')
16
- nulllogger = Object.new
17
- nulllogger.instance_eval {|obj|
18
- def method_missing(method, *args)
19
- # pass
20
- end
21
- }
22
- $log = nulllogger
23
- end
24
-
25
- require 'fluent/plugin/out_geoip'
26
- if Fluent.const_defined?(:Filter)
27
- require 'fluent/plugin/filter_geoip'
28
- end
29
-
30
- class Test::Unit::TestCase
31
- end
7
+ require 'fluent/test/helpers'
@@ -1,35 +1,30 @@
1
1
  require 'helper'
2
+ require 'fluent/plugin/filter_geoip'
3
+ require 'fluent/test/driver/filter'
2
4
 
3
5
  class GeoipFilterTest < Test::Unit::TestCase
4
6
  def setup
5
- omit_unless(Fluent.const_defined?(:Filter))
6
7
  Fluent::Test.setup
7
8
  @time = Fluent::Engine.now
8
9
  end
9
10
 
10
- def create_driver(conf='', tag='test', use_v1=false)
11
- Fluent::Test::FilterTestDriver.new(Fluent::GeoipFilter, tag).configure(conf, use_v1)
11
+ CONFIG = %[
12
+ geoip_lookup_key host
13
+ enable_key_city geoip_city
14
+ ]
15
+
16
+ def create_driver(conf = CONFIG, syntax: :v1)
17
+ Fluent::Test::Driver::Filter.new(Fluent::Plugin::GeoipFilter).configure(conf, syntax: syntax)
12
18
  end
13
19
 
14
- def filter(config, messages, use_v1=false)
15
- d = create_driver(config, 'test', use_v1)
16
- yield d if block_given?
17
- d.run {
20
+ def filter(config, messages, syntax: :v1)
21
+ d = create_driver(config, syntax: syntax)
22
+ d.run(default_tag: "input.access") {
18
23
  messages.each {|message|
19
- d.filter(message, @time)
24
+ d.feed(@time, message)
20
25
  }
21
26
  }
22
- filtered = d.filtered_as_array
23
- filtered.map {|m| m[2] }
24
- end
25
-
26
- def setup_geoip_mock(d)
27
- geoip = d.instance.instance_variable_get(:@geoip)
28
- db = Object.new
29
- def db.lookup(ip)
30
- {}
31
- end
32
- geoip.instance_variable_set(:@geoip, db)
27
+ d.filtered_records
33
28
  end
34
29
 
35
30
  sub_test_case "configure" do
@@ -71,7 +66,8 @@ class GeoipFilterTest < Test::Unit::TestCase
71
66
  end
72
67
 
73
68
  test "invalid json structure w/ Ruby hash like" do
74
- assert_raise(Fluent::ConfigError) {
69
+
70
+ assert_raise(Fluent::ConfigParseError) {
75
71
  create_driver %[
76
72
  geoip_lookup_key host
77
73
  <record>
@@ -82,7 +78,7 @@ class GeoipFilterTest < Test::Unit::TestCase
82
78
  end
83
79
 
84
80
  test "invalid json structure w/ unquoted string literal" do
85
- assert_raise(Fluent::ConfigError) {
81
+ assert_raise(Fluent::ConfigParseError) {
86
82
  create_driver %[
87
83
  geoip_lookup_key host
88
84
  <record>
@@ -130,27 +126,6 @@ class GeoipFilterTest < Test::Unit::TestCase
130
126
  end
131
127
 
132
128
  sub_test_case "geoip2_c" do
133
- def test_filter_with_include_tag_key
134
- config = %[
135
- backend_library geoip2_c
136
- geoip_lookup_key ip.origin, ip.dest
137
- <record>
138
- origin_country ${country.iso_code['ip.origin']}
139
- dest_country ${country.iso_code['ip.dest']}
140
- </record>
141
- include_tag_key true
142
- ]
143
- messages = [
144
- {'ip.origin' => '66.102.3.80', 'ip.dest' => '8.8.8.8'}
145
- ]
146
- expected = [
147
- {'ip.origin' => '66.102.3.80', 'ip.dest' => '8.8.8.8',
148
- 'origin_country' => 'US', 'dest_country' => 'US', 'tag' => 'test' }
149
- ]
150
- filtered = filter(config, messages)
151
- assert_equal(expected, filtered)
152
- end
153
-
154
129
  def test_filter_with_dot_key
155
130
  config = %[
156
131
  backend_library geoip2_c
@@ -190,7 +165,7 @@ class GeoipFilterTest < Test::Unit::TestCase
190
165
  {'host' => '203.0.113.1', 'message' => 'invalid ip', 'geoip_city' => nil, 'geopoint' => [nil, nil]},
191
166
  {'host' => '0', 'message' => 'invalid ip', 'geoip_city' => nil, 'geopoint' => [nil, nil]}
192
167
  ]
193
- filtered = filter(config, messages)
168
+ filtered = filter(config, messages, syntax: :v0)
194
169
  assert_equal(expected, filtered)
195
170
  end
196
171
 
@@ -216,7 +191,7 @@ class GeoipFilterTest < Test::Unit::TestCase
216
191
  {'host' => '66.102.3.80', 'message' => 'google bot',
217
192
  'geoip_city' => 'Mountain View', 'geopoint' => [-122.0574, 37.419200000000004]}
218
193
  ]
219
- filtered = filter(config, messages)
194
+ filtered = filter(config, messages, syntax: :v0)
220
195
  assert_equal(expected, filtered)
221
196
  end
222
197
 
@@ -281,7 +256,7 @@ class GeoipFilterTest < Test::Unit::TestCase
281
256
  'broken_array2' => [nil, nil]
282
257
  },
283
258
  ]
284
- filtered = filter(config, messages)
259
+ filtered = filter(config, messages, syntax: :v0)
285
260
  # test-unit cannot calculate diff between large Array
286
261
  assert_equal(expected[0], filtered[0])
287
262
  assert_equal(expected[1], filtered[1])
@@ -322,7 +297,7 @@ class GeoipFilterTest < Test::Unit::TestCase
322
297
  'string_array' => [nil, nil]
323
298
  }
324
299
  ]
325
- filtered = filter(config, messages)
300
+ filtered = filter(config, messages, syntax: :v0)
326
301
  assert_equal(expected, filtered)
327
302
  end
328
303
 
@@ -383,7 +358,7 @@ class GeoipFilterTest < Test::Unit::TestCase
383
358
  'peculiar_pattern' => '[GEOIP] message => {"lat":37.419200000000004, "lon":-122.0574}'
384
359
  }
385
360
  ]
386
- filtered = filter(config_quoted_record, messages, true)
361
+ filtered = filter(config_quoted_record, messages)
387
362
  assert_equal(expected, filtered)
388
363
  end
389
364
 
@@ -414,33 +389,7 @@ class GeoipFilterTest < Test::Unit::TestCase
414
389
  }
415
390
  }
416
391
  ]
417
- filtered = filter(config, messages, true)
418
- assert_equal(expected, filtered)
419
- end
420
-
421
- def test_filter_when_latitude_longitude_is_nil
422
- config = %[
423
- backend_library geoip2_c
424
- geoip_lookup_key host
425
- <record>
426
- latitude ${location.latitude['host']}
427
- longitude ${location.longitude['host']}
428
- </record>
429
- ]
430
- messages = [
431
- { "host" => "180.94.85.84", "message" => "nil latitude and longitude" }
432
- ]
433
- expected = [
434
- {
435
- "host" => "180.94.85.84",
436
- "message" => "nil latitude and longitude",
437
- "latitude" => 0.0,
438
- "longitude" => 0.0
439
- }
440
- ]
441
- filtered = filter(config, messages) do |d|
442
- setup_geoip_mock(d)
443
- end
392
+ filtered = filter(config, messages)
444
393
  assert_equal(expected, filtered)
445
394
  end
446
395
  end
@@ -485,7 +434,7 @@ class GeoipFilterTest < Test::Unit::TestCase
485
434
  {'host' => '203.0.113.1', 'message' => 'invalid ip', 'geoip_city' => nil, 'geopoint' => [nil, nil]},
486
435
  {'host' => '0', 'message' => 'invalid ip', 'geoip_city' => nil, 'geopoint' => [nil, nil]}
487
436
  ]
488
- filtered = filter(config, messages)
437
+ filtered = filter(config, messages, syntax: :v0)
489
438
  assert_equal(expected, filtered)
490
439
  end
491
440
 
@@ -511,7 +460,7 @@ class GeoipFilterTest < Test::Unit::TestCase
511
460
  {'host' => '66.102.3.80', 'message' => 'google bot',
512
461
  'geoip_city' => 'Mountain View', 'geopoint' => [-122.0574, 37.419200000000004]}
513
462
  ]
514
- filtered = filter(config, messages)
463
+ filtered = filter(config, messages, syntax: :v0)
515
464
  assert_equal(expected, filtered)
516
465
  end
517
466
 
@@ -576,7 +525,7 @@ class GeoipFilterTest < Test::Unit::TestCase
576
525
  'broken_array2' => [nil, nil]
577
526
  },
578
527
  ]
579
- filtered = filter(config, messages)
528
+ filtered = filter(config, messages, syntax: :v0)
580
529
  # test-unit cannot calculate diff between large Array
581
530
  assert_equal(expected[0], filtered[0])
582
531
  assert_equal(expected[1], filtered[1])
@@ -617,7 +566,7 @@ class GeoipFilterTest < Test::Unit::TestCase
617
566
  'string_array' => [nil, nil]
618
567
  }
619
568
  ]
620
- filtered = filter(config, messages)
569
+ filtered = filter(config, messages, syntax: :v0)
621
570
  assert_equal(expected, filtered)
622
571
  end
623
572
 
@@ -678,7 +627,7 @@ class GeoipFilterTest < Test::Unit::TestCase
678
627
  'peculiar_pattern' => '[GEOIP] message => {"lat":37.419200000000004, "lon":-122.0574}'
679
628
  }
680
629
  ]
681
- filtered = filter(config_quoted_record, messages, true)
630
+ filtered = filter(config_quoted_record, messages)
682
631
  assert_equal(expected, filtered)
683
632
  end
684
633
 
@@ -709,51 +658,6 @@ class GeoipFilterTest < Test::Unit::TestCase
709
658
  }
710
659
  }
711
660
  ]
712
- filtered = filter(config, messages, true)
713
- assert_equal(expected, filtered)
714
- end
715
-
716
- def test_filter_when_latitude_longitude_is_nil
717
- config = %[
718
- backend_library geoip2_compat
719
- geoip_lookup_key host
720
- <record>
721
- latitude ${latitude['host']}
722
- longitude ${longitude['host']}
723
- </record>
724
- ]
725
- messages = [
726
- { "host" => "180.94.85.84", "message" => "nil latitude and longitude" }
727
- ]
728
- expected = [
729
- {
730
- "host" => "180.94.85.84",
731
- "message" => "nil latitude and longitude",
732
- "latitude" => 0.0,
733
- "longitude" => 0.0
734
- }
735
- ]
736
- filtered = filter(config, messages) do |d|
737
- setup_geoip_mock(d)
738
- end
739
- assert_equal(expected, filtered)
740
- end
741
-
742
- def test_filter_with_include_tag_key
743
- config = %[
744
- backend_library geoip2_compat
745
- geoip_lookup_key host
746
- enable_key_city geoip_city
747
- include_tag_key true
748
- ]
749
- messages = [
750
- {'host' => '66.102.3.80', 'message' => 'valid ip'},
751
- {'message' => 'missing field'},
752
- ]
753
- expected = [
754
- {'host' => '66.102.3.80', 'message' => 'valid ip', 'geoip_city' => 'Mountain View', 'tag' => 'test'},
755
- {'message' => 'missing field', 'geoip_city' => nil, 'tag' => 'test'},
756
- ]
757
661
  filtered = filter(config, messages)
758
662
  assert_equal(expected, filtered)
759
663
  end
@@ -762,6 +666,7 @@ class GeoipFilterTest < Test::Unit::TestCase
762
666
  sub_test_case "geoip legacy" do
763
667
  def test_filter
764
668
  config = %[
669
+ backend_library geoip
765
670
  geoip_lookup_key host
766
671
  enable_key_city geoip_city
767
672
  ]
@@ -779,6 +684,7 @@ class GeoipFilterTest < Test::Unit::TestCase
779
684
 
780
685
  def test_filter_with_dot_key
781
686
  config = %[
687
+ backend_library geoip
782
688
  geoip_lookup_key ip.origin, ip.dest
783
689
  <record>
784
690
  origin_country ${country_code['ip.origin']}
@@ -798,6 +704,7 @@ class GeoipFilterTest < Test::Unit::TestCase
798
704
 
799
705
  def test_filter_nested_attr
800
706
  config = %[
707
+ backend_library geoip
801
708
  geoip_lookup_key host.ip
802
709
  enable_key_city geoip_city
803
710
  ]
@@ -815,6 +722,7 @@ class GeoipFilterTest < Test::Unit::TestCase
815
722
 
816
723
  def test_filter_with_unknown_address
817
724
  config = %[
725
+ backend_library geoip
818
726
  geoip_lookup_key host
819
727
  <record>
820
728
  geoip_city ${city['host']}
@@ -831,12 +739,13 @@ class GeoipFilterTest < Test::Unit::TestCase
831
739
  {'host' => '203.0.113.1', 'message' => 'invalid ip', 'geoip_city' => nil, 'geopoint' => [nil, nil]},
832
740
  {'host' => '0', 'message' => 'invalid ip', 'geoip_city' => nil, 'geopoint' => [nil, nil]}
833
741
  ]
834
- filtered = filter(config, messages)
742
+ filtered = filter(config, messages, syntax: :v0)
835
743
  assert_equal(expected, filtered)
836
744
  end
837
745
 
838
746
  def test_filter_with_skip_unknown_address
839
747
  config = %[
748
+ backend_library geoip
840
749
  geoip_lookup_key host
841
750
  <record>
842
751
  geoip_city ${city['host']}
@@ -856,12 +765,13 @@ class GeoipFilterTest < Test::Unit::TestCase
856
765
  {'host' => '66.102.3.80', 'message' => 'google bot',
857
766
  'geoip_city' => 'Mountain View', 'geopoint' => [-122.05740356445312, 37.4192008972168]}
858
767
  ]
859
- filtered = filter(config, messages)
768
+ filtered = filter(config, messages, syntax: :v0)
860
769
  assert_equal(expected, filtered)
861
770
  end
862
771
 
863
772
  def test_filter_multiple_key
864
773
  config = %[
774
+ backend_library geoip
865
775
  geoip_lookup_key from.ip, to.ip
866
776
  enable_key_city from_city, to_city
867
777
  ]
@@ -874,12 +784,13 @@ class GeoipFilterTest < Test::Unit::TestCase
874
784
  'from_city' => 'Mountain View', 'to_city' => 'Tokorozawa'},
875
785
  {'message' => 'missing field', 'from_city' => nil, 'to_city' => nil}
876
786
  ]
877
- filtered = filter(config, messages)
787
+ filtered = filter(config, messages, syntax: :v0)
878
788
  assert_equal(expected, filtered)
879
789
  end
880
790
 
881
791
  def test_filter_multiple_key_multiple_record
882
792
  config = %[
793
+ backend_library geoip
883
794
  geoip_lookup_key from.ip, to.ip
884
795
  enable_key_city from_city, to_city
885
796
  enable_key_country_name from_country, to_country
@@ -913,12 +824,13 @@ class GeoipFilterTest < Test::Unit::TestCase
913
824
  'to_country' => nil
914
825
  }
915
826
  ]
916
- filtered = filter(config, messages)
827
+ filtered = filter(config, messages, syntax: :v0)
917
828
  assert_equal(expected, filtered)
918
829
  end
919
830
 
920
831
  def test_filter_record_directive
921
832
  config = %[
833
+ backend_library geoip
922
834
  geoip_lookup_key from.ip
923
835
  <record>
924
836
  from_city ${city['from.ip']}
@@ -977,7 +889,7 @@ class GeoipFilterTest < Test::Unit::TestCase
977
889
  'broken_array2' => [nil, nil]
978
890
  },
979
891
  ]
980
- filtered = filter(config, messages)
892
+ filtered = filter(config, messages, syntax: :v0)
981
893
  # test-unit cannot calculate diff between large Array
982
894
  assert_equal(expected[0], filtered[0])
983
895
  assert_equal(expected[1], filtered[1])
@@ -985,6 +897,7 @@ class GeoipFilterTest < Test::Unit::TestCase
985
897
 
986
898
  def test_filter_record_directive_multiple_record
987
899
  config = %[
900
+ backend_library geoip
988
901
  geoip_lookup_key from.ip, to.ip
989
902
  <record>
990
903
  from_city ${city['from.ip']}
@@ -1017,12 +930,13 @@ class GeoipFilterTest < Test::Unit::TestCase
1017
930
  'string_array' => [nil, nil]
1018
931
  }
1019
932
  ]
1020
- filtered = filter(config, messages)
933
+ filtered = filter(config, messages, syntax: :v0)
1021
934
  assert_equal(expected, filtered)
1022
935
  end
1023
936
 
1024
937
  def config_quoted_record
1025
938
  %[
939
+ backend_library geoip
1026
940
  geoip_lookup_key host
1027
941
  <record>
1028
942
  location_properties '{ "country_code" : "${country_code["host"]}", "lat": ${latitude["host"]}, "lon": ${longitude["host"]} }'
@@ -1077,12 +991,13 @@ class GeoipFilterTest < Test::Unit::TestCase
1077
991
  'peculiar_pattern' => '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}'
1078
992
  }
1079
993
  ]
1080
- filtered = filter(config_quoted_record, messages, true)
994
+ filtered = filter(config_quoted_record, messages)
1081
995
  assert_equal(expected, filtered)
1082
996
  end
1083
997
 
1084
998
  def test_filter_multiline_v1_config
1085
999
  config = %[
1000
+ backend_library geoip
1086
1001
  geoip_lookup_key host
1087
1002
  <record>
1088
1003
  location_properties {
@@ -1107,50 +1022,6 @@ class GeoipFilterTest < Test::Unit::TestCase
1107
1022
  }
1108
1023
  }
1109
1024
  ]
1110
- filtered = filter(config, messages, true)
1111
- assert_equal(expected, filtered)
1112
- end
1113
-
1114
- def test_filter_when_latitude_longitude_is_nil
1115
- config = %[
1116
- backend_library geoip
1117
- geoip_lookup_key host
1118
- <record>
1119
- latitude ${latitude['host']}
1120
- longitude ${longitude['host']}
1121
- </record>
1122
- ]
1123
- messages = [
1124
- { "host" => "180.94.85.84", "message" => "nil latitude and longitude" }
1125
- ]
1126
- expected = [
1127
- {
1128
- "host" => "180.94.85.84",
1129
- "message" => "nil latitude and longitude",
1130
- "latitude" => 0.0,
1131
- "longitude" => 0.0
1132
- }
1133
- ]
1134
- filtered = filter(config, messages) do |d|
1135
- setup_geoip_mock(d)
1136
- end
1137
- assert_equal(expected, filtered)
1138
- end
1139
-
1140
- def test_filter_with_include_tag_key
1141
- config = %[
1142
- geoip_lookup_key host
1143
- enable_key_city geoip_city
1144
- include_tag_key true
1145
- ]
1146
- messages = [
1147
- {'host' => '66.102.3.80', 'message' => 'valid ip'},
1148
- {'message' => 'missing field'},
1149
- ]
1150
- expected = [
1151
- {'host' => '66.102.3.80', 'message' => 'valid ip', 'geoip_city' => 'Mountain View', 'tag' => 'test'},
1152
- {'message' => 'missing field', 'geoip_city' => nil, 'tag' => 'test'},
1153
- ]
1154
1025
  filtered = filter(config, messages)
1155
1026
  assert_equal(expected, filtered)
1156
1027
  end