|
1 | 1 | # encoding: utf-8
|
| 2 | +require 'pathname' |
2 | 3 | require "logstash/devutils/rspec/spec_helper"
|
3 | 4 | require "insist"
|
4 | 5 | require "logstash/filters/geoip"
|
5 | 6 | require_relative 'test_helper'
|
6 | 7 |
|
7 | 8 | describe LogStash::Filters::GeoIP do
|
| 9 | + context "when no database_path is given" do |
8 | 10 |
|
9 |
| - before(:each) do |
10 |
| - ::File.delete(METADATA_PATH) if ::File.exist?(METADATA_PATH) |
11 |
| - end |
| 11 | + let(:last_db_path_recorder) do |
| 12 | + Module.new do |
| 13 | + attr_reader :last_db_path |
| 14 | + def setup_filter(db_path) |
| 15 | + @last_db_path = db_path |
| 16 | + super |
| 17 | + end |
| 18 | + end |
| 19 | + end |
12 | 20 |
|
13 |
| - describe "config without database path in LS >= 7.14", :aggregate_failures do |
14 |
| - before(:each) do |
15 |
| - dir_path = Stud::Temporary.directory |
16 |
| - File.open(dir_path + '/uuid', 'w') { |f| f.write(SecureRandom.uuid) } |
17 |
| - allow(LogStash::SETTINGS).to receive(:get).and_call_original |
18 |
| - allow(LogStash::SETTINGS).to receive(:get).with("xpack.geoip.downloader.enabled").and_return(true) |
19 |
| - allow(LogStash::SETTINGS).to receive(:get).with("xpack.geoip.download.endpoint").and_return(nil) |
20 |
| - allow(LogStash::SETTINGS).to receive(:get).with("path.data").and_return(dir_path) |
| 21 | + let(:plugin_config) { Hash["source" => "[source][ip]", "target" => "[target]"] } |
| 22 | + let(:plugin) { described_class.new(plugin_config).extend(last_db_path_recorder) } |
| 23 | + let(:event) { LogStash::Event.new("source" => { "ip" => "173.9.34.107" }) } |
| 24 | + |
| 25 | + shared_examples "event enrichment" do |
| 26 | + it 'enriches events' do |
| 27 | + plugin.register |
| 28 | + plugin.filter(event) |
| 29 | + |
| 30 | + expect(event.get("target")).to include('ip') |
| 31 | + end |
21 | 32 | end
|
22 | 33 |
|
23 |
| - let(:plugin) { LogStash::Filters::GeoIP.new("source" => "[target][ip]") } |
| 34 | + database_management_available = (MAJOR >= 8 || (MAJOR == 7 && MINOR >= 14)) && !LogStash::OSS |
| 35 | + if database_management_available |
| 36 | + context "when geoip database management is available" do |
| 37 | + |
| 38 | + let(:mock_manager) do |
| 39 | + double('LogStash::Filters::Geoip::DatabaseManager').tap do |m| |
| 40 | + allow(m).to receive(:subscribe_database_path) do |db_type, explicit_path, plugin_instance| |
| 41 | + explicit_path || mock_managed[db_type] |
| 42 | + end |
| 43 | + allow(m).to receive(:unsubscribe_database_path).with(any_args) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + # The extension to this plugin that lives in Logstash core will _always_ provide a valid |
| 48 | + # database path, and how it does so is not the concern of this plugin. We emulate this |
| 49 | + # behaviour here by copying the vendored CC-licensed db's into a temporary path |
| 50 | + let(:mock_managed) do |
| 51 | + managed_path = Pathname.new(temp_data_path).join("managed", Time.now.to_i.to_s).tap(&:mkpath) |
| 52 | + |
| 53 | + managed_city_db_path = Pathname.new(DEFAULT_CITY_DB_PATH).basename.expand_path(managed_path).to_path |
| 54 | + FileUtils.cp(DEFAULT_CITY_DB_PATH, managed_city_db_path) |
24 | 55 |
|
25 |
| - context "restart the plugin" do |
26 |
| - let(:event) { LogStash::Event.new("target" => { "ip" => "173.9.34.107" }) } |
27 |
| - let(:event2) { LogStash::Event.new("target" => { "ip" => "55.159.212.43" }) } |
| 56 | + managed_asn_db_path = Pathname.new(DEFAULT_ASN_DB_PATH).basename.expand_path(managed_path).to_path |
| 57 | + FileUtils.cp(DEFAULT_ASN_DB_PATH, managed_asn_db_path) |
28 | 58 |
|
29 |
| - it "should use the same database" do |
30 |
| - unless plugin.load_database_manager? |
31 |
| - logstash_path = ENV['LOGSTASH_PATH'] || '/usr/share/logstash' # docker logstash home |
32 |
| - stub_const('LogStash::Environment::LOGSTASH_HOME', logstash_path) |
| 59 | + { |
| 60 | + 'City' => managed_city_db_path, |
| 61 | + 'ASN' => managed_asn_db_path, |
| 62 | + } |
33 | 63 | end
|
34 | 64 |
|
35 |
| - plugin.register |
36 |
| - plugin.filter(event) |
37 |
| - plugin.close |
38 |
| - first_dirname = get_metadata_city_database_name |
39 |
| - plugin.register |
40 |
| - plugin.filter(event2) |
41 |
| - plugin.close |
42 |
| - second_dirname = get_metadata_city_database_name |
| 65 | + before(:each) do |
| 66 | + allow_any_instance_of(described_class).to receive(:load_database_manager?).and_return(true) |
| 67 | + stub_const("LogStash::Filters::Geoip::DatabaseManager", double("DatabaseManager.Class", :instance => mock_manager)) |
| 68 | + end |
43 | 69 |
|
44 |
| - expect(first_dirname).not_to be_nil |
45 |
| - expect(first_dirname).to eq(second_dirname) |
46 |
| - expect(File).to exist(get_file_path(first_dirname)) |
| 70 | + let(:temp_data_path) { Stud::Temporary.directory } |
| 71 | + after(:each) do |
| 72 | + FileUtils.rm_rf(temp_data_path) if File.exist?(temp_data_path) |
| 73 | + end |
| 74 | + |
| 75 | + it "uses a managed database" do |
| 76 | + plugin.register |
| 77 | + plugin.filter(event) |
| 78 | + expect(plugin.last_db_path).to_not be_nil |
| 79 | + expect(plugin.last_db_path).to start_with(temp_data_path) |
| 80 | + end |
| 81 | + |
| 82 | + include_examples "event enrichment" |
47 | 83 | end
|
48 |
| - end |
49 |
| - end if MAJOR >= 8 || (MAJOR == 7 && MINOR >= 14) |
50 |
| - |
51 |
| - describe "config without database path in LS < 7.14" do |
52 |
| - context "should run in offline mode" do |
53 |
| - config <<-CONFIG |
54 |
| - filter { |
55 |
| - geoip { |
56 |
| - source => "ip" |
57 |
| - } |
58 |
| - } |
59 |
| - CONFIG |
60 |
| - |
61 |
| - sample("ip" => "173.9.34.107") do |
62 |
| - insist { subject.get("geoip") }.include?("ip") |
63 |
| - expect(::File.exist?(METADATA_PATH)).to be_falsey |
| 84 | + else |
| 85 | + context "when geoip database management is not available" do |
| 86 | + |
| 87 | + include_examples "event enrichment" |
| 88 | + |
| 89 | + it "uses a plugin-vendored database" do |
| 90 | + plugin.register |
| 91 | + expect(plugin.last_db_path).to_not be_nil |
| 92 | + expect(plugin.last_db_path).to include("/vendor/") |
| 93 | + end |
64 | 94 | end
|
65 | 95 | end
|
66 |
| - end if MAJOR < 7 || (MAJOR == 7 && MINOR < 14) |
| 96 | + end |
67 | 97 | end
|
0 commit comments