fluent-plugin-geoip 0.6.1 → 0.7.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.
@@ -5,436 +5,1086 @@ class GeoipOutputTest < Test::Unit::TestCase
5
5
  Fluent::Test.setup
6
6
  end
7
7
 
8
- CONFIG = %[
9
- geoip_lookup_key host
10
- enable_key_city geoip_city
11
- remove_tag_prefix input.
12
- tag geoip.${tag}
13
- ]
14
-
15
- def create_driver(conf=CONFIG,tag='test',use_v1=false)
16
- Fluent::Test::BufferedOutputTestDriver.new(Fluent::GeoipOutput, tag).configure(conf, use_v1)
8
+ def create_driver(conf='',tag='test',use_v1=false)
9
+ require 'fluent/version'
10
+ if Gem::Version.new(Fluent::VERSION) < Gem::Version.new('0.12')
11
+ Fluent::Test::OutputTestDriver.new(Fluent::GeoipOutput, tag).configure(conf, use_v1)
12
+ else
13
+ Fluent::Test::BufferedOutputTestDriver.new(Fluent::GeoipOutput, tag).configure(conf, use_v1)
14
+ end
17
15
  end
18
16
 
19
- def test_configure
20
- assert_raise(Fluent::ConfigError) {
21
- d = create_driver('')
22
- }
23
- assert_raise(Fluent::ConfigError) {
24
- d = create_driver('enable_key_cities')
25
- }
26
- d = create_driver %[
27
- enable_key_city geoip_city
28
- remove_tag_prefix input.
29
- tag geoip.${tag}
30
- ]
31
- assert_equal 'geoip_city', d.instance.config['enable_key_city']
17
+ sub_test_case "configure" do
18
+ test "empty" do
19
+ assert_raise(Fluent::ConfigError) {
20
+ create_driver('')
21
+ }
22
+ end
32
23
 
33
- # multiple key config
34
- d = create_driver %[
35
- geoip_lookup_key from.ip, to.ip
36
- enable_key_city from_city, to_city
37
- remove_tag_prefix input.
38
- tag geoip.${tag}
39
- ]
40
- assert_equal 'from_city, to_city', d.instance.config['enable_key_city']
24
+ test "missing required parameters" do
25
+ assert_raise(Fluent::ConfigError) {
26
+ create_driver('enable_key_cities')
27
+ }
28
+ end
41
29
 
42
- # multiple key config (bad configure)
43
- assert_raise(Fluent::ConfigError) {
30
+ test "minimum" do
44
31
  d = create_driver %[
45
- geoip_lookup_key from.ip, to.ip
46
- enable_key_city from_city
47
- enable_key_region from_region
32
+ enable_key_city geoip_city
48
33
  remove_tag_prefix input.
49
34
  tag geoip.${tag}
50
35
  ]
51
- }
36
+ assert_equal 'geoip_city', d.instance.config['enable_key_city']
37
+ end
52
38
 
53
- # invalid json structure
54
- assert_raise(Fluent::ConfigError) {
39
+ test "multiple key config" do
55
40
  d = create_driver %[
56
- geoip_lookup_key host
41
+ geoip_lookup_key from.ip, to.ip
42
+ enable_key_city from_city, to_city
43
+ remove_tag_prefix input.
44
+ tag geoip.${tag}
45
+ ]
46
+ assert_equal 'from_city, to_city', d.instance.config['enable_key_city']
47
+ end
48
+
49
+ test "multiple key config (bad configure)" do
50
+ assert_raise(Fluent::ConfigError) {
51
+ create_driver %[
52
+ geoip_lookup_key from.ip, to.ip
53
+ enable_key_city from_city
54
+ enable_key_region from_region
55
+ remove_tag_prefix input.
56
+ tag geoip.${tag}
57
+ ]
58
+ }
59
+ end
60
+
61
+ test "invalid json structure w/ Ruby hash like" do
62
+ assert_raise(Fluent::ConfigError) {
63
+ create_driver %[
64
+ geoip_lookup_key host
65
+ <record>
66
+ invalid_json {"foo" => 123}
67
+ </record>
68
+ remove_tag_prefix input.
69
+ tag geoip.${tag}
70
+ ]
71
+ }
72
+ end
73
+
74
+ test "invalid json structure w/ unquoted string literal" do
75
+ assert_raise(Fluent::ConfigError) {
76
+ create_driver %[
77
+ geoip_lookup_key host
78
+ <record>
79
+ invalid_json {"foo" : string, "bar" : 123}
80
+ </record>
81
+ remove_tag_prefix input.
82
+ tag geoip.${tag}
83
+ ]
84
+ }
85
+ end
86
+
87
+ data(geoip: "geoip",
88
+ geoip2_compat: "geoip2_compat")
89
+ test "unsupported key" do |backend|
90
+ assert_raise(Fluent::ConfigError.new("#{backend}: unsupported key unknown")) do
91
+ create_driver %[
92
+ backend_library #{backend}
93
+ <record>
94
+ city ${unknown["host"]}
95
+ </record>
96
+ remove_tag_prefix input.
97
+ tag geoip.${tag}
98
+ ]
99
+ end
100
+ end
101
+
102
+ data(geoip: ["geoip", '${city["host"]}'],
103
+ geoip2_compat: ["geoip2_compat", '${city["host"]}'],
104
+ geoip2_c: ["geoip2_c", '${city.names.en["host"]}'])
105
+ test "supported backend" do |(backend, placeholder)|
106
+ create_driver %[
107
+ backend_library #{backend}
57
108
  <record>
58
- invalid_json {"foo" => 123}
109
+ city #{placeholder}
59
110
  </record>
60
111
  remove_tag_prefix input.
61
112
  tag geoip.${tag}
62
113
  ]
63
- }
64
- assert_raise(Fluent::ConfigError) {
65
- d = create_driver %[
114
+ end
115
+
116
+ test "unsupported backend" do
117
+ assert_raise(Fluent::ConfigError) do
118
+ create_driver %[
119
+ backend_library hive_geoip2
120
+ <record>
121
+ city ${city["host"]}
122
+ </record>
123
+ remove_tag_prefix input.
124
+ tag geoip.${tag}
125
+ ]
126
+ end
127
+ end
128
+ end
129
+
130
+ sub_test_case "geoip2_c" do
131
+ def test_emit_tag_option
132
+ d1 = create_driver(%[
133
+ backend_library geoip2_c
66
134
  geoip_lookup_key host
67
135
  <record>
68
- invalid_json {"foo" : string, "bar" : 123}
136
+ geoip_city ${city.names.en['host']}
69
137
  </record>
70
138
  remove_tag_prefix input.
71
139
  tag geoip.${tag}
72
- ]
73
- }
74
- end
140
+ ], 'input.access')
141
+ d1.run do
142
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
143
+ d1.emit({'message' => 'missing field'})
144
+ end
145
+ emits = d1.emits
146
+ assert_equal 2, emits.length
147
+ assert_equal 'geoip.access', emits[0][0] # tag
148
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
149
+ assert_equal nil, emits[1][2]['geoip_city']
150
+ end
75
151
 
76
- def test_emit
77
- d1 = create_driver(CONFIG, 'input.access')
78
- d1.run do
79
- d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
80
- d1.emit({'message' => 'missing field'})
81
- end
82
- emits = d1.emits
83
- assert_equal 2, emits.length
84
- assert_equal 'geoip.access', emits[0][0] # tag
85
- assert_equal 'Mountain View', emits[0][2]['geoip_city']
86
- assert_equal nil, emits[1][2]['geoip_city']
87
- end
152
+ def test_emit_tag_parts
153
+ d1 = create_driver(%[
154
+ backend_library geoip2_c
155
+ geoip_lookup_key host
156
+ <record>
157
+ geoip_city ${city.names.en['host']}
158
+ </record>
159
+ tag geoip.${tag_parts[1]}.${tag_parts[2..3]}.${tag_parts[-1]}
160
+ ], '0.1.2.3')
161
+ d1.run do
162
+ d1.emit({'host' => '66.102.3.80'})
163
+ end
164
+ emits = d1.emits
165
+ assert_equal 1, emits.length
166
+ assert_equal 'geoip.1.2.3.3', emits[0][0] # tag
167
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
168
+ end
88
169
 
89
- def test_emit_tag_option
90
- d1 = create_driver(%[
91
- geoip_lookup_key host
92
- <record>
93
- geoip_city ${city['host']}
94
- </record>
95
- remove_tag_prefix input.
96
- tag geoip.${tag}
97
- ], 'input.access')
98
- d1.run do
99
- d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
100
- d1.emit({'message' => 'missing field'})
101
- end
102
- emits = d1.emits
103
- assert_equal 2, emits.length
104
- assert_equal 'geoip.access', emits[0][0] # tag
105
- assert_equal 'Mountain View', emits[0][2]['geoip_city']
106
- assert_equal nil, emits[1][2]['geoip_city']
107
- end
170
+ def test_emit_with_dot_key
171
+ d1 = create_driver(%[
172
+ backend_library geoip2_c
173
+ geoip_lookup_key ip.origin, ip.dest
174
+ <record>
175
+ origin_country ${country.iso_code['ip.origin']}
176
+ dest_country ${country.iso_code['ip.dest']}
177
+ </record>
178
+ remove_tag_prefix input.
179
+ tag geoip.${tag}
180
+ ], 'input.access')
181
+ d1.run do
182
+ d1.emit({'ip.origin' => '66.102.3.80', 'ip.dest' => '8.8.8.8'})
183
+ end
184
+ emits = d1.emits
185
+ assert_equal 1, emits.length
186
+ assert_equal 'geoip.access', emits[0][0] # tag
187
+ assert_equal 'US', emits[0][2]['origin_country']
188
+ assert_equal 'US', emits[0][2]['dest_country']
189
+ end
108
190
 
109
- def test_emit_tag_parts
110
- d1 = create_driver(%[
111
- geoip_lookup_key host
112
- <record>
113
- geoip_city ${city['host']}
114
- </record>
115
- tag geoip.${tag_parts[1]}.${tag_parts[2..3]}.${tag_parts[-1]}
116
- ], '0.1.2.3')
117
- d1.run do
118
- d1.emit({'host' => '66.102.3.80'})
119
- end
120
- emits = d1.emits
121
- assert_equal 1, emits.length
122
- assert_equal 'geoip.1.2.3.3', emits[0][0] # tag
123
- assert_equal 'Mountain View', emits[0][2]['geoip_city']
124
- end
191
+ def test_emit_with_unknown_address
192
+ d1 = create_driver(%[
193
+ backend_library geoip2_c
194
+ geoip_lookup_key host
195
+ <record>
196
+ geoip_city ${city.names.en['host']}
197
+ geopoint [${location.longitude['host']}, ${location.latitude['host']}]
198
+ </record>
199
+ skip_adding_null_record false
200
+ remove_tag_prefix input.
201
+ tag geoip.${tag}
202
+ ], 'input.access')
203
+ d1.run do
204
+ # 203.0.113.1 is a test address described in RFC5737
205
+ d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
206
+ d1.emit({'host' => '0', 'message' => 'invalid ip'})
207
+ end
208
+ emits = d1.emits
209
+ assert_equal 2, emits.length
210
+ assert_equal 'geoip.access', emits[0][0] # tag
211
+ assert_equal nil, emits[0][2]['geoip_city']
212
+ assert_equal 'geoip.access', emits[1][0] # tag
213
+ assert_equal nil, emits[1][2]['geoip_city']
214
+ end
125
215
 
126
- def test_emit_with_dot_key
127
- d1 = create_driver(%[
128
- geoip_lookup_key ip.origin, ip.dest
129
- <record>
130
- origin_country ${country_code['ip.origin']}
131
- dest_country ${country_code['ip.dest']}
132
- </record>
133
- remove_tag_prefix input.
134
- tag geoip.${tag}
135
- ], 'input.access')
136
- d1.run do
137
- d1.emit({'ip.origin' => '66.102.3.80', 'ip.dest' => '8.8.8.8'})
138
- end
139
- emits = d1.emits
140
- assert_equal 1, emits.length
141
- assert_equal 'geoip.access', emits[0][0] # tag
142
- assert_equal 'US', emits[0][2]['origin_country']
143
- assert_equal 'US', emits[0][2]['dest_country']
144
- end
216
+ def test_emit_with_skip_unknown_address
217
+ d1 = create_driver(%[
218
+ backend_library geoip2_c
219
+ geoip_lookup_key host
220
+ <record>
221
+ geoip_city ${city.names.en['host']}
222
+ geopoint [${location.longitude['host']}, ${location.latitude['host']}]
223
+ </record>
224
+ skip_adding_null_record true
225
+ remove_tag_prefix input.
226
+ tag geoip.${tag}
227
+ ], 'input.access')
228
+ d1.run do
229
+ # 203.0.113.1 is a test address described in RFC5737
230
+ d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
231
+ d1.emit({'host' => '0', 'message' => 'invalid ip'})
232
+ d1.emit({'host' => '8.8.8.8', 'message' => 'google public dns'})
233
+ end
234
+ emits = d1.emits
235
+ assert_equal 3, emits.length
236
+ assert_equal 'geoip.access', emits[0][0] # tag
237
+ assert_equal nil, emits[0][2]['geoip_city']
238
+ assert_equal nil, emits[0][2]['geopoint']
239
+ assert_equal 'geoip.access', emits[1][0] # tag
240
+ assert_equal nil, emits[1][2]['geoip_city']
241
+ assert_equal nil, emits[1][2]['geopoint']
242
+ assert_equal 'Mountain View', emits[2][2]['geoip_city']
243
+ assert_equal [-122.0838, 37.386], emits[2][2]['geopoint']
244
+ end
145
245
 
146
- def test_emit_nested_attr
147
- d1 = create_driver(%[
148
- geoip_lookup_key host.ip
149
- enable_key_city geoip_city
150
- remove_tag_prefix input.
151
- add_tag_prefix geoip.
152
- ], 'input.access')
153
- d1.run do
154
- d1.emit({'host' => {'ip' => '66.102.3.80'}, 'message' => 'valid ip'})
155
- d1.emit({'message' => 'missing field'})
156
- end
157
- emits = d1.emits
158
- assert_equal 2, emits.length
159
- assert_equal 'geoip.access', emits[0][0] # tag
160
- assert_equal 'Mountain View', emits[0][2]['geoip_city']
161
- assert_equal nil, emits[1][2]['geoip_city']
162
- end
246
+ def test_emit_record_directive
247
+ d1 = create_driver(%[
248
+ backend_library geoip2_c
249
+ geoip_lookup_key from.ip
250
+ <record>
251
+ from_city ${city.names.en['from.ip']}
252
+ from_country ${country.names.en['from.ip']}
253
+ latitude ${location.latitude['from.ip']}
254
+ longitude ${location.longitude['from.ip']}
255
+ float_concat ${location.latitude['from.ip']},${location.longitude['from.ip']}
256
+ float_array [${location.longitude['from.ip']}, ${location.latitude['from.ip']}]
257
+ float_nest { "lat" : ${location.latitude['from.ip']}, "lon" : ${location.longitude['from.ip']}}
258
+ string_concat ${location.latitude['from.ip']},${location.longitude['from.ip']}
259
+ string_array [${city.names.en['from.ip']}, ${country.names.en['from.ip']}]
260
+ string_nest { "city" : ${city.names.en['from.ip']}, "country_name" : ${country.names.en['from.ip']}}
261
+ unknown_city ${city.names.en['unknown_key']}
262
+ undefined ${city.names.en['undefined']}
263
+ broken_array1 [${location.longitude['from.ip']}, ${location.latitude['undefined']}]
264
+ broken_array2 [${location.longitude['undefined']}, ${location.latitude['undefined']}]
265
+ </record>
266
+ remove_tag_prefix input.
267
+ tag geoip.${tag}
268
+ ], 'input.access')
269
+ d1.run do
270
+ d1.emit({'from' => {'ip' => '66.102.3.80'}})
271
+ d1.emit({'message' => 'missing field'})
272
+ end
273
+ emits = d1.emits
274
+ assert_equal 2, emits.length
275
+
276
+ assert_equal 'geoip.access', emits[0][0] # tag
277
+ assert_equal 'Mountain View', emits[0][2]['from_city']
278
+ assert_equal 'United States', emits[0][2]['from_country']
279
+ assert_equal 37.419200000000004, emits[0][2]['latitude']
280
+ assert_equal -122.0574, emits[0][2]['longitude']
281
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['float_concat']
282
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['float_array']
283
+ float_nest = {"lat" => 37.419200000000004, "lon" => -122.0574 }
284
+ assert_equal float_nest, emits[0][2]['float_nest']
285
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['string_concat']
286
+ assert_equal ["Mountain View", "United States"], emits[0][2]['string_array']
287
+ string_nest = {"city" => "Mountain View", "country_name" => "United States"}
288
+ assert_equal string_nest, emits[0][2]['string_nest']
289
+ assert_equal nil, emits[0][2]['unknown_city']
290
+ assert_equal nil, emits[0][2]['undefined']
291
+ assert_equal [-122.0574, nil], emits[0][2]['broken_array1']
292
+ assert_equal [nil, nil], emits[0][2]['broken_array2']
293
+
294
+ assert_equal nil, emits[1][2]['from_city']
295
+ assert_equal nil, emits[1][2]['from_country']
296
+ assert_equal nil, emits[1][2]['latitude']
297
+ assert_equal nil, emits[1][2]['longitude']
298
+ assert_equal ',', emits[1][2]['float_concat']
299
+ assert_equal [nil, nil], emits[1][2]['float_array']
300
+ float_nest = {"lat" => nil, "lon" => nil}
301
+ assert_equal float_nest, emits[1][2]['float_nest']
302
+ assert_equal ',', emits[1][2]['string_concat']
303
+ assert_equal [nil, nil], emits[1][2]['string_array']
304
+ string_nest = {"city" => nil, "country_name" => nil}
305
+ assert_equal string_nest, emits[1][2]['string_nest']
306
+ assert_equal nil, emits[1][2]['unknown_city']
307
+ assert_equal nil, emits[1][2]['undefined']
308
+ assert_equal [nil, nil], emits[1][2]['broken_array1']
309
+ assert_equal [nil, nil], emits[1][2]['broken_array2']
310
+ end
163
311
 
164
- def test_emit_with_unknown_address
165
- d1 = create_driver(%[
312
+ def test_emit_record_directive_multiple_record
313
+ d1 = create_driver(%[
314
+ backend_library geoip2_c
315
+ geoip_lookup_key from.ip, to.ip
316
+ <record>
317
+ from_city ${city.names.en['from.ip']}
318
+ to_city ${city.names.en['to.ip']}
319
+ from_country ${country.names.en['from.ip']}
320
+ to_country ${country.names.en['to.ip']}
321
+ string_array [${country.names.en['from.ip']}, ${country.names.en['to.ip']}]
322
+ </record>
323
+ remove_tag_prefix input.
324
+ tag geoip.${tag}
325
+ ], 'input.access')
326
+ d1.run do
327
+ d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
328
+ d1.emit({'message' => 'missing field'})
329
+ end
330
+ emits = d1.emits
331
+ assert_equal 2, emits.length
332
+
333
+ assert_equal 'geoip.access', emits[0][0] # tag
334
+ assert_equal 'Mountain View', emits[0][2]['from_city']
335
+ assert_equal 'United States', emits[0][2]['from_country']
336
+ assert_equal 'Tokorozawa', emits[0][2]['to_city']
337
+ assert_equal 'Japan', emits[0][2]['to_country']
338
+ assert_equal ['United States','Japan'], emits[0][2]['string_array']
339
+
340
+ assert_equal nil, emits[1][2]['from_city']
341
+ assert_equal nil, emits[1][2]['to_city']
342
+ assert_equal nil, emits[1][2]['from_country']
343
+ assert_equal nil, emits[1][2]['to_country']
344
+ assert_equal [nil, nil], emits[1][2]['string_array']
345
+ end
346
+
347
+ def config_quoted_record
348
+ %[
349
+ backend_library geoip2_c
166
350
  geoip_lookup_key host
167
351
  <record>
168
- geoip_city ${city['host']}
169
- geopoint [${longitude['host']}, ${latitude['host']}]
352
+ location_properties '{ "country_code" : "${country.iso_code["host"]}", "lat": ${location.latitude["host"]}, "lon": ${location.longitude["host"]} }'
353
+ location_string ${location.latitude['host']},${location.longitude['host']}
354
+ location_string2 ${country.iso_code["host"]}
355
+ location_array "[${location.longitude['host']},${location.latitude['host']}]"
356
+ location_array2 '[${location.longitude["host"]},${location.latitude["host"]}]'
357
+ peculiar_pattern '[GEOIP] message => {"lat":${location.latitude["host"]}, "lon":${location.longitude["host"]}}'
170
358
  </record>
171
- skip_adding_null_record false
172
359
  remove_tag_prefix input.
173
360
  tag geoip.${tag}
174
- ], 'input.access')
175
- d1.run do
176
- # 203.0.113.1 is a test address described in RFC5737
177
- d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
178
- d1.emit({'host' => '0', 'message' => 'invalid ip'})
179
- end
180
- emits = d1.emits
181
- assert_equal 2, emits.length
182
- assert_equal 'geoip.access', emits[0][0] # tag
183
- assert_equal nil, emits[0][2]['geoip_city']
184
- assert_equal 'geoip.access', emits[1][0] # tag
185
- assert_equal nil, emits[1][2]['geoip_city']
361
+ ]
362
+ end
363
+
364
+ def test_emit_quoted_record
365
+ d1 = create_driver(config_quoted_record, 'input.access')
366
+ d1.run do
367
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
368
+ end
369
+ emits = d1.emits
370
+ assert_equal 1, emits.length
371
+ assert_equal 'geoip.access', emits[0][0] # tag
372
+ location_properties = { "country_code" => "US", "lat" => 37.419200000000004, "lon"=> -122.0574 }
373
+ assert_equal location_properties, emits[0][2]['location_properties']
374
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['location_string']
375
+ assert_equal 'US', emits[0][2]['location_string2']
376
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array']
377
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array2']
378
+ assert_equal '[GEOIP] message => {"lat":37.419200000000004, "lon":-122.0574}', emits[0][2]['peculiar_pattern']
379
+ end
380
+
381
+ def test_emit_v1_config_compatibility
382
+ d1 = create_driver(config_quoted_record, 'input.access', true)
383
+ d1.run do
384
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
385
+ end
386
+ emits = d1.emits
387
+ assert_equal 1, emits.length
388
+ assert_equal 'geoip.access', emits[0][0] # tag
389
+ location_properties = { "country_code" => "US", "lat" => 37.419200000000004, "lon"=> -122.0574 }
390
+ assert_equal location_properties, emits[0][2]['location_properties']
391
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['location_string']
392
+ assert_equal 'US', emits[0][2]['location_string2']
393
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array']
394
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array2']
395
+ assert_equal '[GEOIP] message => {"lat":37.419200000000004, "lon":-122.0574}', emits[0][2]['peculiar_pattern']
396
+ end
397
+
398
+ def test_emit_multiline_v1_config
399
+ d1 = create_driver(%[
400
+ backend_library geoip2_c
401
+ geoip_lookup_key host
402
+ <record>
403
+ location_properties {
404
+ "city": "${city.names.en['host']}",
405
+ "country_code": "${country.iso_code['host']}",
406
+ "latitude": "${location.latitude['host']}",
407
+ "longitude": "${location.longitude['host']}"
408
+ }
409
+ </record>
410
+ remove_tag_prefix input.
411
+ tag geoip.${tag}
412
+ ], 'input.access', true)
413
+ d1.run do
414
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
415
+ end
416
+ emits = d1.emits
417
+ assert_equal 1, emits.length
418
+ assert_equal 'geoip.access', emits[0][0] # tag
419
+ location_properties = { "city"=>"Mountain View", "country_code"=>"US", "latitude"=>37.419200000000004, "longitude"=>-122.0574 }
420
+ assert_equal location_properties, emits[0][2]['location_properties']
421
+ end
186
422
  end
187
423
 
188
- def test_emit_with_skip_unknown_address
189
- d1 = create_driver(%[
424
+ sub_test_case "geoip2_compat" do
425
+ def test_emit_tag_option
426
+ d1 = create_driver(%[
427
+ backend_library geoip2_compat
428
+ geoip_lookup_key host
429
+ <record>
430
+ geoip_city ${city['host']}
431
+ </record>
432
+ remove_tag_prefix input.
433
+ tag geoip.${tag}
434
+ ], 'input.access')
435
+ d1.run do
436
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
437
+ d1.emit({'message' => 'missing field'})
438
+ end
439
+ emits = d1.emits
440
+ assert_equal 2, emits.length
441
+ assert_equal 'geoip.access', emits[0][0] # tag
442
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
443
+ assert_equal nil, emits[1][2]['geoip_city']
444
+ end
445
+
446
+ def test_emit_tag_parts
447
+ d1 = create_driver(%[
448
+ backend_library geoip2_compat
449
+ geoip_lookup_key host
450
+ <record>
451
+ geoip_city ${city['host']}
452
+ </record>
453
+ tag geoip.${tag_parts[1]}.${tag_parts[2..3]}.${tag_parts[-1]}
454
+ ], '0.1.2.3')
455
+ d1.run do
456
+ d1.emit({'host' => '66.102.3.80'})
457
+ end
458
+ emits = d1.emits
459
+ assert_equal 1, emits.length
460
+ assert_equal 'geoip.1.2.3.3', emits[0][0] # tag
461
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
462
+ end
463
+
464
+ def test_emit_with_dot_key
465
+ d1 = create_driver(%[
466
+ backend_library geoip2_compat
467
+ geoip_lookup_key ip.origin, ip.dest
468
+ <record>
469
+ origin_country ${country_code['ip.origin']}
470
+ dest_country ${country_code['ip.dest']}
471
+ </record>
472
+ remove_tag_prefix input.
473
+ tag geoip.${tag}
474
+ ], 'input.access')
475
+ d1.run do
476
+ d1.emit({'ip.origin' => '66.102.3.80', 'ip.dest' => '8.8.8.8'})
477
+ end
478
+ emits = d1.emits
479
+ assert_equal 1, emits.length
480
+ assert_equal 'geoip.access', emits[0][0] # tag
481
+ assert_equal 'US', emits[0][2]['origin_country']
482
+ assert_equal 'US', emits[0][2]['dest_country']
483
+ end
484
+
485
+ def test_emit_with_unknown_address
486
+ d1 = create_driver(%[
487
+ backend_library geoip2_compat
488
+ geoip_lookup_key host
489
+ <record>
490
+ geoip_city ${city['host']}
491
+ geopoint [${longitude['host']}, ${latitude['host']}]
492
+ </record>
493
+ skip_adding_null_record false
494
+ remove_tag_prefix input.
495
+ tag geoip.${tag}
496
+ ], 'input.access')
497
+ d1.run do
498
+ # 203.0.113.1 is a test address described in RFC5737
499
+ d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
500
+ d1.emit({'host' => '0', 'message' => 'invalid ip'})
501
+ end
502
+ emits = d1.emits
503
+ assert_equal 2, emits.length
504
+ assert_equal 'geoip.access', emits[0][0] # tag
505
+ assert_equal nil, emits[0][2]['geoip_city']
506
+ assert_equal 'geoip.access', emits[1][0] # tag
507
+ assert_equal nil, emits[1][2]['geoip_city']
508
+ end
509
+
510
+ def test_emit_with_skip_unknown_address
511
+ d1 = create_driver(%[
512
+ backend_library geoip2_compat
513
+ geoip_lookup_key host
514
+ <record>
515
+ geoip_city ${city['host']}
516
+ geopoint [${longitude['host']}, ${latitude['host']}]
517
+ </record>
518
+ skip_adding_null_record true
519
+ remove_tag_prefix input.
520
+ tag geoip.${tag}
521
+ ], 'input.access')
522
+ d1.run do
523
+ # 203.0.113.1 is a test address described in RFC5737
524
+ d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
525
+ d1.emit({'host' => '0', 'message' => 'invalid ip'})
526
+ d1.emit({'host' => '8.8.8.8', 'message' => 'google public dns'})
527
+ end
528
+ emits = d1.emits
529
+ assert_equal 3, emits.length
530
+ assert_equal 'geoip.access', emits[0][0] # tag
531
+ assert_equal nil, emits[0][2]['geoip_city']
532
+ assert_equal nil, emits[0][2]['geopoint']
533
+ assert_equal 'geoip.access', emits[1][0] # tag
534
+ assert_equal nil, emits[1][2]['geoip_city']
535
+ assert_equal nil, emits[1][2]['geopoint']
536
+ assert_equal 'Mountain View', emits[2][2]['geoip_city']
537
+ assert_equal [-122.0838, 37.386], emits[2][2]['geopoint']
538
+ end
539
+
540
+ def test_emit_record_directive
541
+ d1 = create_driver(%[
542
+ backend_library geoip2_compat
543
+ geoip_lookup_key from.ip
544
+ <record>
545
+ from_city ${city['from.ip']}
546
+ from_country ${country_name['from.ip']}
547
+ latitude ${latitude['from.ip']}
548
+ longitude ${longitude['from.ip']}
549
+ float_concat ${latitude['from.ip']},${longitude['from.ip']}
550
+ float_array [${longitude['from.ip']}, ${latitude['from.ip']}]
551
+ float_nest { "lat" : ${latitude['from.ip']}, "lon" : ${longitude['from.ip']}}
552
+ string_concat ${latitude['from.ip']},${longitude['from.ip']}
553
+ string_array [${city['from.ip']}, ${country_name['from.ip']}]
554
+ string_nest { "city" : ${city['from.ip']}, "country_name" : ${country_name['from.ip']}}
555
+ unknown_city ${city['unknown_key']}
556
+ undefined ${city['undefined']}
557
+ broken_array1 [${longitude['from.ip']}, ${latitude['undefined']}]
558
+ broken_array2 [${longitude['undefined']}, ${latitude['undefined']}]
559
+ </record>
560
+ remove_tag_prefix input.
561
+ tag geoip.${tag}
562
+ ], 'input.access')
563
+ d1.run do
564
+ d1.emit({'from' => {'ip' => '66.102.3.80'}})
565
+ d1.emit({'message' => 'missing field'})
566
+ end
567
+ emits = d1.emits
568
+ assert_equal 2, emits.length
569
+
570
+ assert_equal 'geoip.access', emits[0][0] # tag
571
+ assert_equal 'Mountain View', emits[0][2]['from_city']
572
+ assert_equal 'United States', emits[0][2]['from_country']
573
+ assert_equal 37.419200000000004, emits[0][2]['latitude']
574
+ assert_equal -122.0574, emits[0][2]['longitude']
575
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['float_concat']
576
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['float_array']
577
+ float_nest = {"lat" => 37.419200000000004, "lon" => -122.0574 }
578
+ assert_equal float_nest, emits[0][2]['float_nest']
579
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['string_concat']
580
+ assert_equal ["Mountain View", "United States"], emits[0][2]['string_array']
581
+ string_nest = {"city" => "Mountain View", "country_name" => "United States"}
582
+ assert_equal string_nest, emits[0][2]['string_nest']
583
+ assert_equal nil, emits[0][2]['unknown_city']
584
+ assert_equal nil, emits[0][2]['undefined']
585
+ assert_equal [-122.0574, nil], emits[0][2]['broken_array1']
586
+ assert_equal [nil, nil], emits[0][2]['broken_array2']
587
+
588
+ assert_equal nil, emits[1][2]['from_city']
589
+ assert_equal nil, emits[1][2]['from_country']
590
+ assert_equal nil, emits[1][2]['latitude']
591
+ assert_equal nil, emits[1][2]['longitude']
592
+ assert_equal ',', emits[1][2]['float_concat']
593
+ assert_equal [nil, nil], emits[1][2]['float_array']
594
+ float_nest = {"lat" => nil, "lon" => nil}
595
+ assert_equal float_nest, emits[1][2]['float_nest']
596
+ assert_equal ',', emits[1][2]['string_concat']
597
+ assert_equal [nil, nil], emits[1][2]['string_array']
598
+ string_nest = {"city" => nil, "country_name" => nil}
599
+ assert_equal string_nest, emits[1][2]['string_nest']
600
+ assert_equal nil, emits[1][2]['unknown_city']
601
+ assert_equal nil, emits[1][2]['undefined']
602
+ assert_equal [nil, nil], emits[1][2]['broken_array1']
603
+ assert_equal [nil, nil], emits[1][2]['broken_array2']
604
+ end
605
+
606
+ def test_emit_record_directive_multiple_record
607
+ d1 = create_driver(%[
608
+ backend_library geoip2_compat
609
+ geoip_lookup_key from.ip, to.ip
610
+ <record>
611
+ from_city ${city['from.ip']}
612
+ to_city ${city['to.ip']}
613
+ from_country ${country_name['from.ip']}
614
+ to_country ${country_name['to.ip']}
615
+ string_array [${country_name['from.ip']}, ${country_name['to.ip']}]
616
+ </record>
617
+ remove_tag_prefix input.
618
+ tag geoip.${tag}
619
+ ], 'input.access')
620
+ d1.run do
621
+ d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
622
+ d1.emit({'message' => 'missing field'})
623
+ end
624
+ emits = d1.emits
625
+ assert_equal 2, emits.length
626
+
627
+ assert_equal 'geoip.access', emits[0][0] # tag
628
+ assert_equal 'Mountain View', emits[0][2]['from_city']
629
+ assert_equal 'United States', emits[0][2]['from_country']
630
+ assert_equal 'Tokorozawa', emits[0][2]['to_city']
631
+ assert_equal 'Japan', emits[0][2]['to_country']
632
+ assert_equal ['United States','Japan'], emits[0][2]['string_array']
633
+
634
+ assert_equal nil, emits[1][2]['from_city']
635
+ assert_equal nil, emits[1][2]['to_city']
636
+ assert_equal nil, emits[1][2]['from_country']
637
+ assert_equal nil, emits[1][2]['to_country']
638
+ assert_equal [nil, nil], emits[1][2]['string_array']
639
+ end
640
+
641
+ def config_quoted_record
642
+ %[
643
+ backend_library geoip2_compat
190
644
  geoip_lookup_key host
191
645
  <record>
192
- geoip_city ${city['host']}
193
- geopoint [${longitude['host']}, ${latitude['host']}]
646
+ location_properties '{ "country_code" : "${country_code["host"]}", "lat": ${latitude["host"]}, "lon": ${longitude["host"]} }'
647
+ location_string ${latitude['host']},${longitude['host']}
648
+ location_string2 ${country_code["host"]}
649
+ location_array "[${longitude['host']},${latitude['host']}]"
650
+ location_array2 '[${longitude["host"]},${latitude["host"]}]'
651
+ peculiar_pattern '[GEOIP] message => {"lat":${latitude["host"]}, "lon":${longitude["host"]}}'
194
652
  </record>
195
- skip_adding_null_record true
196
653
  remove_tag_prefix input.
197
654
  tag geoip.${tag}
198
- ], 'input.access')
199
- d1.run do
200
- # 203.0.113.1 is a test address described in RFC5737
201
- d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
202
- d1.emit({'host' => '0', 'message' => 'invalid ip'})
203
- d1.emit({'host' => '8.8.8.8', 'message' => 'google public dns'})
204
- end
205
- emits = d1.emits
206
- assert_equal 3, emits.length
207
- assert_equal 'geoip.access', emits[0][0] # tag
208
- assert_equal nil, emits[0][2]['geoip_city']
209
- assert_equal nil, emits[0][2]['geopoint']
210
- assert_equal 'geoip.access', emits[1][0] # tag
211
- assert_equal nil, emits[1][2]['geoip_city']
212
- assert_equal nil, emits[1][2]['geopoint']
213
- assert_equal 'Mountain View', emits[2][2]['geoip_city']
214
- assert_equal [-122.08380126953125, 37.38600158691406], emits[2][2]['geopoint']
215
- end
655
+ ]
656
+ end
216
657
 
217
- def test_emit_multiple_key
218
- d1 = create_driver(%[
219
- geoip_lookup_key from.ip, to.ip
220
- enable_key_city from_city, to_city
221
- remove_tag_prefix input.
222
- add_tag_prefix geoip.
223
- ], 'input.access')
224
- d1.run do
225
- d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
226
- d1.emit({'message' => 'missing field'})
227
- end
228
- emits = d1.emits
229
- assert_equal 2, emits.length
230
- assert_equal 'geoip.access', emits[0][0] # tag
231
- assert_equal 'Mountain View', emits[0][2]['from_city']
232
- assert_equal 'Tokorozawa', emits[0][2]['to_city']
233
- assert_equal nil, emits[1][2]['from_city']
234
- assert_equal nil, emits[1][2]['to_city']
235
- end
658
+ def test_emit_quoted_record
659
+ d1 = create_driver(config_quoted_record, 'input.access')
660
+ d1.run do
661
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
662
+ end
663
+ emits = d1.emits
664
+ assert_equal 1, emits.length
665
+ assert_equal 'geoip.access', emits[0][0] # tag
666
+ location_properties = { "country_code" => "US", "lat" => 37.419200000000004, "lon"=> -122.0574 }
667
+ assert_equal location_properties, emits[0][2]['location_properties']
668
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['location_string']
669
+ assert_equal 'US', emits[0][2]['location_string2']
670
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array']
671
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array2']
672
+ assert_equal '[GEOIP] message => {"lat":37.419200000000004, "lon":-122.0574}', emits[0][2]['peculiar_pattern']
673
+ end
236
674
 
237
- def test_emit_multiple_key_multiple_record
238
- d1 = create_driver(%[
239
- geoip_lookup_key from.ip, to.ip
240
- enable_key_city from_city, to_city
241
- enable_key_country_name from_country, to_country
242
- remove_tag_prefix input.
243
- add_tag_prefix geoip.
244
- ], 'input.access')
245
- d1.run do
246
- d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
247
- d1.emit({'from' => {'ip' => '66.102.3.80'}})
248
- d1.emit({'message' => 'missing field'})
249
- end
250
- emits = d1.emits
251
- assert_equal 3, emits.length
252
- assert_equal 'geoip.access', emits[0][0] # tag
253
- assert_equal 'Mountain View', emits[0][2]['from_city']
254
- assert_equal 'United States', emits[0][2]['from_country']
255
- assert_equal 'Tokorozawa', emits[0][2]['to_city']
256
- assert_equal 'Japan', emits[0][2]['to_country']
257
-
258
- assert_equal 'Mountain View', emits[1][2]['from_city']
259
- assert_equal 'United States', emits[1][2]['from_country']
260
- assert_equal nil, emits[1][2]['to_city']
261
- assert_equal nil, emits[1][2]['to_country']
262
-
263
- assert_equal nil, emits[2][2]['from_city']
264
- assert_equal nil, emits[2][2]['from_country']
265
- assert_equal nil, emits[2][2]['to_city']
266
- assert_equal nil, emits[2][2]['to_country']
267
- end
675
+ def test_emit_v1_config_compatibility
676
+ d1 = create_driver(config_quoted_record, 'input.access', true)
677
+ d1.run do
678
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
679
+ end
680
+ emits = d1.emits
681
+ assert_equal 1, emits.length
682
+ assert_equal 'geoip.access', emits[0][0] # tag
683
+ location_properties = { "country_code" => "US", "lat" => 37.419200000000004, "lon"=> -122.0574 }
684
+ assert_equal location_properties, emits[0][2]['location_properties']
685
+ assert_equal '37.419200000000004,-122.0574', emits[0][2]['location_string']
686
+ assert_equal 'US', emits[0][2]['location_string2']
687
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array']
688
+ assert_equal [-122.0574, 37.419200000000004], emits[0][2]['location_array2']
689
+ assert_equal '[GEOIP] message => {"lat":37.419200000000004, "lon":-122.0574}', emits[0][2]['peculiar_pattern']
690
+ end
268
691
 
269
- def test_emit_record_directive
270
- d1 = create_driver(%[
271
- geoip_lookup_key from.ip
272
- <record>
273
- from_city ${city['from.ip']}
274
- from_country ${country_name['from.ip']}
275
- latitude ${latitude['from.ip']}
276
- longitude ${longitude['from.ip']}
277
- float_concat ${latitude['from.ip']},${longitude['from.ip']}
278
- float_array [${longitude['from.ip']}, ${latitude['from.ip']}]
279
- float_nest { "lat" : ${latitude['from.ip']}, "lon" : ${longitude['from.ip']}}
280
- string_concat ${latitude['from.ip']},${longitude['from.ip']}
281
- string_array [${city['from.ip']}, ${country_name['from.ip']}]
282
- string_nest { "city" : ${city['from.ip']}, "country_name" : ${country_name['from.ip']}}
283
- unknown_city ${city['unknown_key']}
284
- undefined ${city['undefined']}
285
- broken_array1 [${longitude['from.ip']}, ${latitude['undefined']}]
286
- broken_array2 [${longitude['undefined']}, ${latitude['undefined']}]
287
- </record>
288
- remove_tag_prefix input.
289
- tag geoip.${tag}
290
- ], 'input.access')
291
- d1.run do
292
- d1.emit({'from' => {'ip' => '66.102.3.80'}})
293
- d1.emit({'message' => 'missing field'})
294
- end
295
- emits = d1.emits
296
- assert_equal 2, emits.length
297
-
298
- assert_equal 'geoip.access', emits[0][0] # tag
299
- assert_equal 'Mountain View', emits[0][2]['from_city']
300
- assert_equal 'United States', emits[0][2]['from_country']
301
- assert_equal 37.4192008972168, emits[0][2]['latitude']
302
- assert_equal -122.05740356445312, emits[0][2]['longitude']
303
- assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['float_concat']
304
- assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['float_array']
305
- float_nest = {"lat" => 37.4192008972168, "lon" => -122.05740356445312 }
306
- assert_equal float_nest, emits[0][2]['float_nest']
307
- assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['string_concat']
308
- assert_equal ["Mountain View", "United States"], emits[0][2]['string_array']
309
- string_nest = {"city" => "Mountain View", "country_name" => "United States"}
310
- assert_equal string_nest, emits[0][2]['string_nest']
311
- assert_equal nil, emits[0][2]['unknown_city']
312
- assert_equal nil, emits[0][2]['undefined']
313
- assert_equal [-122.05740356445312, nil], emits[0][2]['broken_array1']
314
- assert_equal [nil, nil], emits[0][2]['broken_array2']
315
-
316
- assert_equal nil, emits[1][2]['from_city']
317
- assert_equal nil, emits[1][2]['from_country']
318
- assert_equal nil, emits[1][2]['latitude']
319
- assert_equal nil, emits[1][2]['longitude']
320
- assert_equal ',', emits[1][2]['float_concat']
321
- assert_equal [nil, nil], emits[1][2]['float_array']
322
- float_nest = {"lat" => nil, "lon" => nil}
323
- assert_equal float_nest, emits[1][2]['float_nest']
324
- assert_equal ',', emits[1][2]['string_concat']
325
- assert_equal [nil, nil], emits[1][2]['string_array']
326
- string_nest = {"city" => nil, "country_name" => nil}
327
- assert_equal string_nest, emits[1][2]['string_nest']
328
- assert_equal nil, emits[1][2]['unknown_city']
329
- assert_equal nil, emits[1][2]['undefined']
330
- assert_equal [nil, nil], emits[1][2]['broken_array1']
331
- assert_equal [nil, nil], emits[1][2]['broken_array2']
692
+ def test_emit_multiline_v1_config
693
+ d1 = create_driver(%[
694
+ backend_library geoip2_compat
695
+ geoip_lookup_key host
696
+ <record>
697
+ location_properties {
698
+ "city": "${city['host']}",
699
+ "country_code": "${country_code['host']}",
700
+ "latitude": "${latitude['host']}",
701
+ "longitude": "${longitude['host']}"
702
+ }
703
+ </record>
704
+ remove_tag_prefix input.
705
+ tag geoip.${tag}
706
+ ], 'input.access', true)
707
+ d1.run do
708
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
709
+ end
710
+ emits = d1.emits
711
+ assert_equal 1, emits.length
712
+ assert_equal 'geoip.access', emits[0][0] # tag
713
+ location_properties = { "city"=>"Mountain View", "country_code"=>"US", "latitude"=>37.419200000000004, "longitude"=>-122.0574 }
714
+ assert_equal location_properties, emits[0][2]['location_properties']
715
+ end
332
716
  end
333
717
 
334
- def test_emit_record_directive_multiple_record
335
- d1 = create_driver(%[
336
- geoip_lookup_key from.ip, to.ip
337
- <record>
338
- from_city ${city['from.ip']}
339
- to_city ${city['to.ip']}
340
- from_country ${country_name['from.ip']}
341
- to_country ${country_name['to.ip']}
342
- string_array [${country_name['from.ip']}, ${country_name['to.ip']}]
343
- </record>
344
- remove_tag_prefix input.
345
- tag geoip.${tag}
346
- ], 'input.access')
347
- d1.run do
348
- d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
349
- d1.emit({'message' => 'missing field'})
350
- end
351
- emits = d1.emits
352
- assert_equal 2, emits.length
353
-
354
- assert_equal 'geoip.access', emits[0][0] # tag
355
- assert_equal 'Mountain View', emits[0][2]['from_city']
356
- assert_equal 'United States', emits[0][2]['from_country']
357
- assert_equal 'Tokorozawa', emits[0][2]['to_city']
358
- assert_equal 'Japan', emits[0][2]['to_country']
359
- assert_equal ['United States','Japan'], emits[0][2]['string_array']
360
-
361
- assert_equal nil, emits[1][2]['from_city']
362
- assert_equal nil, emits[1][2]['to_city']
363
- assert_equal nil, emits[1][2]['from_country']
364
- assert_equal nil, emits[1][2]['to_country']
365
- assert_equal [nil, nil], emits[1][2]['string_array']
366
- end
718
+ sub_test_case "geoip legacy" do
719
+ def test_emit
720
+ d1 = create_driver(%[
721
+ geoip_lookup_key host
722
+ enable_key_city geoip_city
723
+ remove_tag_prefix input.
724
+ tag geoip.${tag}
725
+ ], 'input.access')
726
+ d1.run do
727
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
728
+ d1.emit({'message' => 'missing field'})
729
+ end
730
+ emits = d1.emits
731
+ assert_equal 2, emits.length
732
+ assert_equal 'geoip.access', emits[0][0] # tag
733
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
734
+ assert_equal nil, emits[1][2]['geoip_city']
735
+ end
367
736
 
368
- CONFIG_QUOTED_RECORD = %[
369
- geoip_lookup_key host
370
- <record>
371
- location_properties '{ "country_code" : "${country_code["host"]}", "lat": ${latitude["host"]}, "lon": ${longitude["host"]} }'
372
- location_string ${latitude['host']},${longitude['host']}
373
- location_string2 ${country_code["host"]}
374
- location_array "[${longitude['host']},${latitude['host']}]"
375
- location_array2 '[${longitude["host"]},${latitude["host"]}]'
376
- peculiar_pattern '[GEOIP] message => {"lat":${latitude["host"]}, "lon":${longitude["host"]}}'
377
- </record>
378
- remove_tag_prefix input.
379
- tag geoip.${tag}
380
- ]
381
-
382
- def test_emit_quoted_record
383
- d1 = create_driver(CONFIG_QUOTED_RECORD, 'input.access')
384
- d1.run do
385
- d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
386
- end
387
- emits = d1.emits
388
- assert_equal 1, emits.length
389
- assert_equal 'geoip.access', emits[0][0] # tag
390
- location_properties = { "country_code" => "US", "lat" => 37.4192008972168, "lon"=> -122.05740356445312 }
391
- assert_equal location_properties, emits[0][2]['location_properties']
392
- assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string']
393
- assert_equal 'US', emits[0][2]['location_string2']
394
- assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array']
395
- assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array2']
396
- assert_equal '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}', emits[0][2]['peculiar_pattern']
397
- end
737
+ def test_emit_tag_option
738
+ d1 = create_driver(%[
739
+ geoip_lookup_key host
740
+ <record>
741
+ geoip_city ${city['host']}
742
+ </record>
743
+ remove_tag_prefix input.
744
+ tag geoip.${tag}
745
+ ], 'input.access')
746
+ d1.run do
747
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
748
+ d1.emit({'message' => 'missing field'})
749
+ end
750
+ emits = d1.emits
751
+ assert_equal 2, emits.length
752
+ assert_equal 'geoip.access', emits[0][0] # tag
753
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
754
+ assert_equal nil, emits[1][2]['geoip_city']
755
+ end
398
756
 
399
- def test_emit_v1_config_compatibility
400
- d1 = create_driver(CONFIG_QUOTED_RECORD, 'input.access', true)
401
- d1.run do
402
- d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
403
- end
404
- emits = d1.emits
405
- assert_equal 1, emits.length
406
- assert_equal 'geoip.access', emits[0][0] # tag
407
- location_properties = { "country_code" => "US", "lat" => 37.4192008972168, "lon"=> -122.05740356445312 }
408
- assert_equal location_properties, emits[0][2]['location_properties']
409
- assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string']
410
- assert_equal 'US', emits[0][2]['location_string2']
411
- assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array']
412
- assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array2']
413
- assert_equal '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}', emits[0][2]['peculiar_pattern']
414
- end
757
+ def test_emit_tag_parts
758
+ d1 = create_driver(%[
759
+ geoip_lookup_key host
760
+ <record>
761
+ geoip_city ${city['host']}
762
+ </record>
763
+ tag geoip.${tag_parts[1]}.${tag_parts[2..3]}.${tag_parts[-1]}
764
+ ], '0.1.2.3')
765
+ d1.run do
766
+ d1.emit({'host' => '66.102.3.80'})
767
+ end
768
+ emits = d1.emits
769
+ assert_equal 1, emits.length
770
+ assert_equal 'geoip.1.2.3.3', emits[0][0] # tag
771
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
772
+ end
415
773
 
416
- def test_emit_multiline_v1_config
417
- d1 = create_driver(%[
418
- geoip_lookup_key host
419
- <record>
420
- location_properties {
421
- "city": "${city['host']}",
422
- "country_code": "${country_code['host']}",
423
- "latitude": "${latitude['host']}",
424
- "longitude": "${longitude['host']}"
425
- }
426
- </record>
427
- remove_tag_prefix input.
428
- tag geoip.${tag}
429
- ], 'input.access', true)
430
- d1.run do
431
- d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
432
- end
433
- emits = d1.emits
434
- assert_equal 1, emits.length
435
- assert_equal 'geoip.access', emits[0][0] # tag
436
- location_properties = { "city"=>"Mountain View", "country_code"=>"US", "latitude"=>37.4192008972168, "longitude"=>-122.05740356445312 }
437
- assert_equal location_properties, emits[0][2]['location_properties']
774
+ def test_emit_with_dot_key
775
+ d1 = create_driver(%[
776
+ geoip_lookup_key ip.origin, ip.dest
777
+ <record>
778
+ origin_country ${country_code['ip.origin']}
779
+ dest_country ${country_code['ip.dest']}
780
+ </record>
781
+ remove_tag_prefix input.
782
+ tag geoip.${tag}
783
+ ], 'input.access')
784
+ d1.run do
785
+ d1.emit({'ip.origin' => '66.102.3.80', 'ip.dest' => '8.8.8.8'})
786
+ end
787
+ emits = d1.emits
788
+ assert_equal 1, emits.length
789
+ assert_equal 'geoip.access', emits[0][0] # tag
790
+ assert_equal 'US', emits[0][2]['origin_country']
791
+ assert_equal 'US', emits[0][2]['dest_country']
792
+ end
793
+
794
+ def test_emit_nested_attr
795
+ d1 = create_driver(%[
796
+ geoip_lookup_key host.ip
797
+ enable_key_city geoip_city
798
+ remove_tag_prefix input.
799
+ add_tag_prefix geoip.
800
+ ], 'input.access')
801
+ d1.run do
802
+ d1.emit({'host' => {'ip' => '66.102.3.80'}, 'message' => 'valid ip'})
803
+ d1.emit({'message' => 'missing field'})
804
+ end
805
+ emits = d1.emits
806
+ assert_equal 2, emits.length
807
+ assert_equal 'geoip.access', emits[0][0] # tag
808
+ assert_equal 'Mountain View', emits[0][2]['geoip_city']
809
+ assert_equal nil, emits[1][2]['geoip_city']
810
+ end
811
+
812
+ def test_emit_with_unknown_address
813
+ d1 = create_driver(%[
814
+ geoip_lookup_key host
815
+ <record>
816
+ geoip_city ${city['host']}
817
+ geopoint [${longitude['host']}, ${latitude['host']}]
818
+ </record>
819
+ skip_adding_null_record false
820
+ remove_tag_prefix input.
821
+ tag geoip.${tag}
822
+ ], 'input.access')
823
+ d1.run do
824
+ # 203.0.113.1 is a test address described in RFC5737
825
+ d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
826
+ d1.emit({'host' => '0', 'message' => 'invalid ip'})
827
+ end
828
+ emits = d1.emits
829
+ assert_equal 2, emits.length
830
+ assert_equal 'geoip.access', emits[0][0] # tag
831
+ assert_equal nil, emits[0][2]['geoip_city']
832
+ assert_equal 'geoip.access', emits[1][0] # tag
833
+ assert_equal nil, emits[1][2]['geoip_city']
834
+ end
835
+
836
+ def test_emit_with_skip_unknown_address
837
+ d1 = create_driver(%[
838
+ geoip_lookup_key host
839
+ <record>
840
+ geoip_city ${city['host']}
841
+ geopoint [${longitude['host']}, ${latitude['host']}]
842
+ </record>
843
+ skip_adding_null_record true
844
+ remove_tag_prefix input.
845
+ tag geoip.${tag}
846
+ ], 'input.access')
847
+ d1.run do
848
+ # 203.0.113.1 is a test address described in RFC5737
849
+ d1.emit({'host' => '203.0.113.1', 'message' => 'invalid ip'})
850
+ d1.emit({'host' => '0', 'message' => 'invalid ip'})
851
+ d1.emit({'host' => '8.8.8.8', 'message' => 'google public dns'})
852
+ end
853
+ emits = d1.emits
854
+ assert_equal 3, emits.length
855
+ assert_equal 'geoip.access', emits[0][0] # tag
856
+ assert_equal nil, emits[0][2]['geoip_city']
857
+ assert_equal nil, emits[0][2]['geopoint']
858
+ assert_equal 'geoip.access', emits[1][0] # tag
859
+ assert_equal nil, emits[1][2]['geoip_city']
860
+ assert_equal nil, emits[1][2]['geopoint']
861
+ assert_equal 'Mountain View', emits[2][2]['geoip_city']
862
+ assert_equal [-122.08380126953125, 37.38600158691406], emits[2][2]['geopoint']
863
+ end
864
+
865
+ def test_emit_multiple_key
866
+ d1 = create_driver(%[
867
+ geoip_lookup_key from.ip, to.ip
868
+ enable_key_city from_city, to_city
869
+ remove_tag_prefix input.
870
+ add_tag_prefix geoip.
871
+ ], 'input.access')
872
+ d1.run do
873
+ d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
874
+ d1.emit({'message' => 'missing field'})
875
+ end
876
+ emits = d1.emits
877
+ assert_equal 2, emits.length
878
+ assert_equal 'geoip.access', emits[0][0] # tag
879
+ assert_equal 'Mountain View', emits[0][2]['from_city']
880
+ assert_equal 'Tokorozawa', emits[0][2]['to_city']
881
+ assert_equal nil, emits[1][2]['from_city']
882
+ assert_equal nil, emits[1][2]['to_city']
883
+ end
884
+
885
+ def test_emit_multiple_key_multiple_record
886
+ d1 = create_driver(%[
887
+ geoip_lookup_key from.ip, to.ip
888
+ enable_key_city from_city, to_city
889
+ enable_key_country_name from_country, to_country
890
+ remove_tag_prefix input.
891
+ add_tag_prefix geoip.
892
+ ], 'input.access')
893
+ d1.run do
894
+ d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
895
+ d1.emit({'from' => {'ip' => '66.102.3.80'}})
896
+ d1.emit({'message' => 'missing field'})
897
+ end
898
+ emits = d1.emits
899
+ assert_equal 3, emits.length
900
+ assert_equal 'geoip.access', emits[0][0] # tag
901
+ assert_equal 'Mountain View', emits[0][2]['from_city']
902
+ assert_equal 'United States', emits[0][2]['from_country']
903
+ assert_equal 'Tokorozawa', emits[0][2]['to_city']
904
+ assert_equal 'Japan', emits[0][2]['to_country']
905
+
906
+ assert_equal 'Mountain View', emits[1][2]['from_city']
907
+ assert_equal 'United States', emits[1][2]['from_country']
908
+ assert_equal nil, emits[1][2]['to_city']
909
+ assert_equal nil, emits[1][2]['to_country']
910
+
911
+ assert_equal nil, emits[2][2]['from_city']
912
+ assert_equal nil, emits[2][2]['from_country']
913
+ assert_equal nil, emits[2][2]['to_city']
914
+ assert_equal nil, emits[2][2]['to_country']
915
+ end
916
+
917
+ def test_emit_record_directive
918
+ d1 = create_driver(%[
919
+ geoip_lookup_key from.ip
920
+ <record>
921
+ from_city ${city['from.ip']}
922
+ from_country ${country_name['from.ip']}
923
+ latitude ${latitude['from.ip']}
924
+ longitude ${longitude['from.ip']}
925
+ float_concat ${latitude['from.ip']},${longitude['from.ip']}
926
+ float_array [${longitude['from.ip']}, ${latitude['from.ip']}]
927
+ float_nest { "lat" : ${latitude['from.ip']}, "lon" : ${longitude['from.ip']}}
928
+ string_concat ${latitude['from.ip']},${longitude['from.ip']}
929
+ string_array [${city['from.ip']}, ${country_name['from.ip']}]
930
+ string_nest { "city" : ${city['from.ip']}, "country_name" : ${country_name['from.ip']}}
931
+ unknown_city ${city['unknown_key']}
932
+ undefined ${city['undefined']}
933
+ broken_array1 [${longitude['from.ip']}, ${latitude['undefined']}]
934
+ broken_array2 [${longitude['undefined']}, ${latitude['undefined']}]
935
+ </record>
936
+ remove_tag_prefix input.
937
+ tag geoip.${tag}
938
+ ], 'input.access')
939
+ d1.run do
940
+ d1.emit({'from' => {'ip' => '66.102.3.80'}})
941
+ d1.emit({'message' => 'missing field'})
942
+ end
943
+ emits = d1.emits
944
+ assert_equal 2, emits.length
945
+
946
+ assert_equal 'geoip.access', emits[0][0] # tag
947
+ assert_equal 'Mountain View', emits[0][2]['from_city']
948
+ assert_equal 'United States', emits[0][2]['from_country']
949
+ assert_equal 37.4192008972168, emits[0][2]['latitude']
950
+ assert_equal -122.05740356445312, emits[0][2]['longitude']
951
+ assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['float_concat']
952
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['float_array']
953
+ float_nest = {"lat" => 37.4192008972168, "lon" => -122.05740356445312 }
954
+ assert_equal float_nest, emits[0][2]['float_nest']
955
+ assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['string_concat']
956
+ assert_equal ["Mountain View", "United States"], emits[0][2]['string_array']
957
+ string_nest = {"city" => "Mountain View", "country_name" => "United States"}
958
+ assert_equal string_nest, emits[0][2]['string_nest']
959
+ assert_equal nil, emits[0][2]['unknown_city']
960
+ assert_equal nil, emits[0][2]['undefined']
961
+ assert_equal [-122.05740356445312, nil], emits[0][2]['broken_array1']
962
+ assert_equal [nil, nil], emits[0][2]['broken_array2']
963
+
964
+ assert_equal nil, emits[1][2]['from_city']
965
+ assert_equal nil, emits[1][2]['from_country']
966
+ assert_equal nil, emits[1][2]['latitude']
967
+ assert_equal nil, emits[1][2]['longitude']
968
+ assert_equal ',', emits[1][2]['float_concat']
969
+ assert_equal [nil, nil], emits[1][2]['float_array']
970
+ float_nest = {"lat" => nil, "lon" => nil}
971
+ assert_equal float_nest, emits[1][2]['float_nest']
972
+ assert_equal ',', emits[1][2]['string_concat']
973
+ assert_equal [nil, nil], emits[1][2]['string_array']
974
+ string_nest = {"city" => nil, "country_name" => nil}
975
+ assert_equal string_nest, emits[1][2]['string_nest']
976
+ assert_equal nil, emits[1][2]['unknown_city']
977
+ assert_equal nil, emits[1][2]['undefined']
978
+ assert_equal [nil, nil], emits[1][2]['broken_array1']
979
+ assert_equal [nil, nil], emits[1][2]['broken_array2']
980
+ end
981
+
982
+ def test_emit_record_directive_multiple_record
983
+ d1 = create_driver(%[
984
+ geoip_lookup_key from.ip, to.ip
985
+ <record>
986
+ from_city ${city['from.ip']}
987
+ to_city ${city['to.ip']}
988
+ from_country ${country_name['from.ip']}
989
+ to_country ${country_name['to.ip']}
990
+ string_array [${country_name['from.ip']}, ${country_name['to.ip']}]
991
+ </record>
992
+ remove_tag_prefix input.
993
+ tag geoip.${tag}
994
+ ], 'input.access')
995
+ d1.run do
996
+ d1.emit({'from' => {'ip' => '66.102.3.80'}, 'to' => {'ip' => '125.54.15.42'}})
997
+ d1.emit({'message' => 'missing field'})
998
+ end
999
+ emits = d1.emits
1000
+ assert_equal 2, emits.length
1001
+
1002
+ assert_equal 'geoip.access', emits[0][0] # tag
1003
+ assert_equal 'Mountain View', emits[0][2]['from_city']
1004
+ assert_equal 'United States', emits[0][2]['from_country']
1005
+ assert_equal 'Tokorozawa', emits[0][2]['to_city']
1006
+ assert_equal 'Japan', emits[0][2]['to_country']
1007
+ assert_equal ['United States','Japan'], emits[0][2]['string_array']
1008
+
1009
+ assert_equal nil, emits[1][2]['from_city']
1010
+ assert_equal nil, emits[1][2]['to_city']
1011
+ assert_equal nil, emits[1][2]['from_country']
1012
+ assert_equal nil, emits[1][2]['to_country']
1013
+ assert_equal [nil, nil], emits[1][2]['string_array']
1014
+ end
1015
+
1016
+ def config_quoted_record
1017
+ %[
1018
+ geoip_lookup_key host
1019
+ <record>
1020
+ location_properties '{ "country_code" : "${country_code["host"]}", "lat": ${latitude["host"]}, "lon": ${longitude["host"]} }'
1021
+ location_string ${latitude['host']},${longitude['host']}
1022
+ location_string2 ${country_code["host"]}
1023
+ location_array "[${longitude['host']},${latitude['host']}]"
1024
+ location_array2 '[${longitude["host"]},${latitude["host"]}]'
1025
+ peculiar_pattern '[GEOIP] message => {"lat":${latitude["host"]}, "lon":${longitude["host"]}}'
1026
+ </record>
1027
+ remove_tag_prefix input.
1028
+ tag geoip.${tag}
1029
+ ]
1030
+ end
1031
+
1032
+ def test_emit_quoted_record
1033
+ d1 = create_driver(config_quoted_record, 'input.access')
1034
+ d1.run do
1035
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
1036
+ end
1037
+ emits = d1.emits
1038
+ assert_equal 1, emits.length
1039
+ assert_equal 'geoip.access', emits[0][0] # tag
1040
+ location_properties = { "country_code" => "US", "lat" => 37.4192008972168, "lon"=> -122.05740356445312 }
1041
+ assert_equal location_properties, emits[0][2]['location_properties']
1042
+ assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string']
1043
+ assert_equal 'US', emits[0][2]['location_string2']
1044
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array']
1045
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array2']
1046
+ assert_equal '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}', emits[0][2]['peculiar_pattern']
1047
+ end
1048
+
1049
+ def test_emit_v1_config_compatibility
1050
+ d1 = create_driver(config_quoted_record, 'input.access', true)
1051
+ d1.run do
1052
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
1053
+ end
1054
+ emits = d1.emits
1055
+ assert_equal 1, emits.length
1056
+ assert_equal 'geoip.access', emits[0][0] # tag
1057
+ location_properties = { "country_code" => "US", "lat" => 37.4192008972168, "lon"=> -122.05740356445312 }
1058
+ assert_equal location_properties, emits[0][2]['location_properties']
1059
+ assert_equal '37.4192008972168,-122.05740356445312', emits[0][2]['location_string']
1060
+ assert_equal 'US', emits[0][2]['location_string2']
1061
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array']
1062
+ assert_equal [-122.05740356445312, 37.4192008972168], emits[0][2]['location_array2']
1063
+ assert_equal '[GEOIP] message => {"lat":37.4192008972168, "lon":-122.05740356445312}', emits[0][2]['peculiar_pattern']
1064
+ end
1065
+
1066
+ def test_emit_multiline_v1_config
1067
+ d1 = create_driver(%[
1068
+ geoip_lookup_key host
1069
+ <record>
1070
+ location_properties {
1071
+ "city": "${city['host']}",
1072
+ "country_code": "${country_code['host']}",
1073
+ "latitude": "${latitude['host']}",
1074
+ "longitude": "${longitude['host']}"
1075
+ }
1076
+ </record>
1077
+ remove_tag_prefix input.
1078
+ tag geoip.${tag}
1079
+ ], 'input.access', true)
1080
+ d1.run do
1081
+ d1.emit({'host' => '66.102.3.80', 'message' => 'valid ip'})
1082
+ end
1083
+ emits = d1.emits
1084
+ assert_equal 1, emits.length
1085
+ assert_equal 'geoip.access', emits[0][0] # tag
1086
+ location_properties = { "city"=>"Mountain View", "country_code"=>"US", "latitude"=>37.4192008972168, "longitude"=>-122.05740356445312 }
1087
+ assert_equal location_properties, emits[0][2]['location_properties']
1088
+ end
438
1089
  end
439
1090
  end
440
-