|
343 | 343 | }
|
344 | 344 | }]
|
345 | 345 | }
|
346 |
| - end |
| 346 | + end |
347 | 347 |
|
348 | 348 | before(:each) do
|
349 | 349 | allow(subject.client).to receive(:bulk_send).with(instance_of(StringIO), instance_of(Array)) do |stream, actions|
|
|
771 | 771 |
|
772 | 772 | context 'when getting any other exception' do
|
773 | 773 | it 'should log at WARN level' do
|
774 |
| - dlog = double_logger = double("logger").as_null_object |
775 |
| - subject.instance_variable_set(:@logger, dlog) |
776 |
| - expect(dlog).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) |
| 774 | + logger = double("logger").as_null_object |
| 775 | + subject.instance_variable_set(:@logger, logger) |
| 776 | + expect(logger).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) |
777 | 777 | mock_response = { 'index' => { 'error' => { 'type' => 'illegal_argument_exception' } } }
|
778 | 778 | subject.handle_dlq_status("Could not index event to Elasticsearch.",
|
779 | 779 | [:action, :params, :event], :some_status, mock_response)
|
|
782 | 782 |
|
783 | 783 | context 'when the response does not include [error]' do
|
784 | 784 | it 'should not fail, but just log a warning' do
|
785 |
| - dlog = double_logger = double("logger").as_null_object |
786 |
| - subject.instance_variable_set(:@logger, dlog) |
787 |
| - expect(dlog).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) |
| 785 | + logger = double("logger").as_null_object |
| 786 | + subject.instance_variable_set(:@logger, logger) |
| 787 | + expect(logger).to receive(:warn).with(/Could not index/, hash_including(:status, :action, :response)) |
788 | 788 | mock_response = { 'index' => {} }
|
789 | 789 | expect do
|
790 | 790 | subject.handle_dlq_status("Could not index event to Elasticsearch.",
|
|
804 | 804 | # We should still log when sending to the DLQ.
|
805 | 805 | # This shall be solved by another issue, however: logstash-output-elasticsearch#772
|
806 | 806 | it 'should send the event to the DLQ instead, and not log' do
|
807 |
| - expect(dlq_writer).to receive(:write).once.with(:event, /Could not index/) |
| 807 | + event = LogStash::Event.new("foo" => "bar") |
| 808 | + expect(dlq_writer).to receive(:write).once.with(event, /Could not index/) |
808 | 809 | mock_response = { 'index' => { 'error' => { 'type' => 'illegal_argument_exception' } } }
|
809 |
| - subject.handle_dlq_status("Could not index event to Elasticsearch.", |
810 |
| - [:action, :params, :event], :some_status, mock_response) |
| 810 | + action = LogStash::Outputs::ElasticSearch::EventActionTuple.new(:action, :params, event) |
| 811 | + subject.handle_dlq_status("Could not index event to Elasticsearch.", action, 404, mock_response) |
811 | 812 | end
|
812 | 813 | end
|
| 814 | + |
| 815 | + context 'with response status 400' do |
| 816 | + |
| 817 | + let(:options) { super().merge 'document_id' => '%{foo}' } |
| 818 | + |
| 819 | + let(:events) { [ LogStash::Event.new("foo" => "bar") ] } |
| 820 | + |
| 821 | + let(:dlq_writer) { subject.instance_variable_get(:@dlq_writer) } |
| 822 | + |
| 823 | + let(:bulk_response) do |
| 824 | + { |
| 825 | + "took"=>1, "ingest_took"=>11, "errors"=>true, "items"=> |
| 826 | + [{ |
| 827 | + "index"=>{"_index"=>"bar", "_type"=>"_doc", "_id"=>'bar', "status"=>400, |
| 828 | + "error"=>{"type" => "illegal_argument_exception", "reason" => "TEST" } |
| 829 | + } |
| 830 | + }] |
| 831 | + } |
| 832 | + end |
| 833 | + |
| 834 | + before(:each) do |
| 835 | + allow(subject.client).to receive(:bulk_send).and_return(bulk_response) |
| 836 | + end |
| 837 | + |
| 838 | + it "should write event to DLQ" do |
| 839 | + expect(dlq_writer).to receive(:write).and_wrap_original do |method, *args| |
| 840 | + expect( args.size ).to eql 2 |
| 841 | + |
| 842 | + event, reason = *args |
| 843 | + expect( event ).to be_a LogStash::Event |
| 844 | + expect( event ).to be events.first |
| 845 | + expect( reason ).to start_with 'Could not index event to Elasticsearch. status: 400, action: ["index"' |
| 846 | + expect( reason ).to match /_id=>"bar".*"foo"=>"bar".*response:.*"reason"=>"TEST"/ |
| 847 | + |
| 848 | + method.call(*args) # won't hurt to call LogStash::Util::DummyDeadLetterQueueWriter |
| 849 | + end.once |
| 850 | + |
| 851 | + event_action_tuples = subject.map_events(events) |
| 852 | + subject.send(:submit, event_action_tuples) |
| 853 | + end |
| 854 | + |
| 855 | + end if LOGSTASH_VERSION > '7.0' |
813 | 856 | end
|
814 | 857 |
|
815 | 858 | describe "custom headers" do
|
|
0 commit comments