|
60 | 60 | end
|
61 | 61 |
|
62 | 62 | it "indexes using the proper pipeline" do
|
63 |
| - results = @es.search(:index => 'logstash-*', :q => "message:\"netcat\"") |
| 63 | + results = @es.search(:index => "logstash-*", :q => "message:\"netcat\"") |
64 | 64 | expect(results).to have_hits(1)
|
65 | 65 | expect(results["hits"]["hits"][0]["_source"]["response"]).to eq("200")
|
66 | 66 | expect(results["hits"]["hits"][0]["_source"]["bytes"]).to eq("182")
|
|
72 | 72 | expect(results["hits"]["hits"][0]["_source"]["junkfieldaaaa"]).to eq(nil)
|
73 | 73 | end
|
74 | 74 | end
|
| 75 | + |
| 76 | +describe "Ingest pipeline execution behavior with metadata", :integration => true do |
| 77 | + subject! do |
| 78 | + require "logstash/outputs/elasticsearch" |
| 79 | + settings = { |
| 80 | + "hosts" => "#{get_host_port()}", |
| 81 | + "pipeline" => "apache-logs", |
| 82 | + "data_stream" => "false", |
| 83 | + "use_metadata" => true, |
| 84 | + } |
| 85 | + next LogStash::Outputs::ElasticSearch.new(settings) |
| 86 | + end |
| 87 | + |
| 88 | + let(:http_client) { Manticore::Client.new } |
| 89 | + let(:ingest_url) { "http://#{get_host_port()}/_ingest/pipeline/apache-logs" } |
| 90 | + let(:apache_logs_pipeline) { |
| 91 | + ' |
| 92 | + { |
| 93 | + "description" : "Pipeline to parse Apache logs", |
| 94 | + "processors" : [ |
| 95 | + { |
| 96 | + "grok": { |
| 97 | + "field": "message", |
| 98 | + "patterns": ["%{COMBINEDAPACHELOG}"] |
| 99 | + } |
| 100 | + } |
| 101 | + ] |
| 102 | + }' |
| 103 | + } |
| 104 | + |
| 105 | + let(:add_field_ingest_url) { "http://#{get_host_port()}/_ingest/pipeline/add-field" } |
| 106 | + let(:add_field_logs_pipeline) { |
| 107 | + ' |
| 108 | + { |
| 109 | + "description": "Add field foo with value bar", |
| 110 | + "processors": [ |
| 111 | + { |
| 112 | + "set": { |
| 113 | + "field": "foo", |
| 114 | + "value": "bar" |
| 115 | + } |
| 116 | + } |
| 117 | + ] |
| 118 | + }' |
| 119 | + } |
| 120 | + |
| 121 | + before :each do |
| 122 | + # Delete all templates first. |
| 123 | + require "elasticsearch" |
| 124 | + |
| 125 | + # Clean ES of data before we start. |
| 126 | + @es = get_client |
| 127 | + @es.indices.delete_template(:name => "*") |
| 128 | + |
| 129 | + # This can fail if there are no indexes, ignore failure. |
| 130 | + @es.indices.delete(:index => "*") rescue nil |
| 131 | + |
| 132 | + # delete existing ingest pipeline |
| 133 | + http_client.delete(ingest_url).call |
| 134 | + |
| 135 | + # register pipelines |
| 136 | + http_client.put(ingest_url, :body => apache_logs_pipeline, :headers => { "Content-Type" => "application/json" }).call |
| 137 | + http_client.put(add_field_ingest_url, :body => add_field_logs_pipeline, :headers => { "Content-Type" => "application/json" }).call |
| 138 | + |
| 139 | + #TODO: Use esclient |
| 140 | + #@es.ingest.put_pipeline :id => 'apache_pipeline', :body => pipeline_defintion |
| 141 | + |
| 142 | + subject.register |
| 143 | + subject.multi_receive([ |
| 144 | + LogStash::Event.new("message" => '183.60.215.50 - - [01/Jun/2015:18:00:00 +0000] "GET /scripts/netcat-webserver HTTP/1.1" 200 182 "-" "Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)"'), |
| 145 | + LogStash::Event.new("message" => '183.60.215.50 - - [01/Jun/2015:18:00:00 +0000] "GET /scripts/netcat-webserver HTTP/1.1" 200 182 "-" "Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)"', "@metadata" => { "_id" => "id1", "_index" => "index1", "pipeline" => "add-field" }), |
| 146 | + ]) |
| 147 | + @es.indices.refresh |
| 148 | + |
| 149 | + #Wait or fail until everything's indexed. |
| 150 | + Stud::try(10.times) do |
| 151 | + r = @es.search(index: "logstash-*") |
| 152 | + expect(r).to have_hits(1) |
| 153 | + r = @es.search(index: "index1") |
| 154 | + expect(r).to have_hits(1) |
| 155 | + sleep(0.1) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + it "indexes using the correct pipeline when @metadata.pipeline not defined" do |
| 160 | + results = @es.search(:index => "logstash-*", :q => "message:\"netcat\"") |
| 161 | + expect(results).to have_hits(1) |
| 162 | + expect(results["hits"]["hits"][0]["_source"]["response"]).to eq("200") |
| 163 | + expect(results["hits"]["hits"][0]["_source"]["bytes"]).to eq("182") |
| 164 | + expect(results["hits"]["hits"][0]["_source"]["verb"]).to eq("GET") |
| 165 | + expect(results["hits"]["hits"][0]["_source"]["request"]).to eq("/scripts/netcat-webserver") |
| 166 | + expect(results["hits"]["hits"][0]["_source"]["auth"]).to eq("-") |
| 167 | + expect(results["hits"]["hits"][0]["_source"]["ident"]).to eq("-") |
| 168 | + expect(results["hits"]["hits"][0]["_source"]["clientip"]).to eq("183.60.215.50") |
| 169 | + expect(results["hits"]["hits"][0]["_source"]["junkfieldaaaa"]).to eq(nil) |
| 170 | + end |
| 171 | + |
| 172 | + it "indexes using the @metadata._index, @metadata._id, and @metadata.pipeline when defined" do |
| 173 | + results = @es.search(:index => "index1", :q => "message:\"netcat\"") |
| 174 | + expect(results).to have_hits(1) |
| 175 | + expect(results["hits"]["hits"][0]["_id"]).to eq("id1") |
| 176 | + expect(results["hits"]["hits"][0]["_index"]).to eq("index1") |
| 177 | + expect(results["hits"]["hits"][0]["_source"]["foo"]).to eq("bar") |
| 178 | + end |
| 179 | +end |
0 commit comments