spec/inputs/s3_spec.rb in logstash-input-s3-0.1.9 vs spec/inputs/s3_spec.rb in logstash-input-s3-0.1.10

- old
+ new

@@ -1,27 +1,52 @@ # encoding: utf-8 require "logstash/devutils/rspec/spec_helper" require "logstash/inputs/s3" require "logstash/errors" -require "aws-sdk" -require "stud/temporary" require_relative "../support/helpers" +require "stud/temporary" +require "aws-sdk" +require "fileutils" describe LogStash::Inputs::S3 do - before do - AWS.stub! - Thread.abort_on_exception = true - end + let(:temporary_directory) { Stud::Temporary.pathname } let(:day) { 3600 * 24 } let(:settings) { { "access_key_id" => "1234", "secret_access_key" => "secret", - "bucket" => "logstash-test" + "bucket" => "logstash-test", + "temporary_directory" => temporary_directory } } + before do + AWS.stub! + Thread.abort_on_exception = true + end + + describe "#register" do + subject { LogStash::Inputs::S3.new(settings) } + + context "with temporary directory" do + let(:settings) { + { + "access_key_id" => "1234", + "secret_access_key" => "secret", + "bucket" => "logstash-test", + "temporary_directory" => temporary_directory + } + } + + let(:temporary_directory) { Stud::Temporary.pathname } + + it "creates the direct when it doesn't exist" do + expect { subject.register }.to change { Dir.exist?(temporary_directory) }.from(false).to(true) + end + end + end + describe "#list_new_files" do before { allow_any_instance_of(AWS::S3::ObjectCollection).to receive(:with_prefix).with(nil) { objects_list } } let!(:present_object) { double(:key => 'this-should-be-present', :last_modified => Time.now) } let(:objects_list) { @@ -158,59 +183,77 @@ expect{ config.register }.not_to raise_error end end end + shared_examples "generated events" do + it 'should process events' do + events = fetch_events(settings) + expect(events.size).to eq(2) + end + + it "deletes the temporary file" do + events = fetch_events(settings) + expect(Dir.glob(File.join(temporary_directory, "*")).size).to eq(0) + end + end + context 'when working with logs' do let(:objects) { [log] } let(:log) { double(:key => 'uncompressed.log', :last_modified => Time.now - 2 * day) } before do allow_any_instance_of(AWS::S3::ObjectCollection).to receive(:with_prefix).with(nil) { objects } allow_any_instance_of(AWS::S3::ObjectCollection).to receive(:[]).with(log.key) { log } expect(log).to receive(:read) { |&block| block.call(File.read(log_file)) } end + context "when event doesn't have a `message` field" do + let(:log_file) { File.join(File.dirname(__FILE__), '..', 'fixtures', 'json.log') } + let(:settings) { + { + "access_key_id" => "1234", + "secret_access_key" => "secret", + "bucket" => "logstash-test", + "codec" => "json", + } + } + + include_examples "generated events" + end + context 'compressed' do let(:log) { double(:key => 'log.gz', :last_modified => Time.now - 2 * day) } let(:log_file) { File.join(File.dirname(__FILE__), '..', 'fixtures', 'compressed.log.gz') } - it 'should process events' do - events = fetch_events(settings) - expect(events.size).to eq(2) - end + + include_examples "generated events" end context 'plain text' do let(:log_file) { File.join(File.dirname(__FILE__), '..', 'fixtures', 'uncompressed.log') } - it 'should process events' do - events = fetch_events(settings) - expect(events.size).to eq(2) - end + include_examples "generated events" end context 'encoded' do let(:log_file) { File.join(File.dirname(__FILE__), '..', 'fixtures', 'invalid_utf8.log') } - it 'should work with invalid utf-8 log event' do - events = fetch_events(settings) - expect(events.size).to eq(2) - end + include_examples "generated events" end context 'cloudfront' do let(:log_file) { File.join(File.dirname(__FILE__), '..', 'fixtures', 'cloudfront.log') } it 'should extract metadata from cloudfront log' do events = fetch_events(settings) - expect(events.size).to eq(2) - events.each do |event| expect(event['cloudfront_fields']).to eq('date time x-edge-location c-ip x-event sc-bytes x-cf-status x-cf-client-id cs-uri-stem cs-uri-query c-referrer x-page-url​ c-user-agent x-sname x-sname-query x-file-ext x-sid') expect(event['cloudfront_version']).to eq('1.0') end end + + include_examples "generated events" end end end