spec/out_grepcounter_spec.rb in fluent-plugin-grepcounter-0.1.4 vs spec/out_grepcounter_spec.rb in fluent-plugin-grepcounter-0.2.0

- old
+ new

@@ -22,33 +22,21 @@ let(:config) { '' } it { expect { driver }.to raise_error(Fluent::ConfigError) } end context 'invalid aggregate' do - let(:config) do - CONFIG + %[ - aggregate foo - ] - end + let(:config) { CONFIG + %[aggregate foo] } it { expect { driver }.to raise_error(Fluent::ConfigError) } end context 'no tag for aggregate all' do - let(:config) do - CONFIG + %[ - aggregate all - ] - end + let(:config) { CONFIG + %[aggregate all] } it { expect { driver }.to raise_error(Fluent::ConfigError) } end context 'invalid comparator' do - let(:config) do - CONFIG + %[ - comparator foo - ] - end + let(:config) { CONFIG + %[comparator foo] } it { expect { driver }.to raise_error(Fluent::ConfigError) } end end describe 'good configuration' do @@ -80,222 +68,287 @@ end let(:emit) do driver.run { messages.each {|message| driver.emit({'message' => message}, time) } } driver.instance.flush_emit(0) end - - context 'count_interval' do - pending + let(:expected) do + { + "count"=>4, + "message"=>[ + "2013/01/13T07:02:11.124202 INFO GET /ping", + "2013/01/13T07:02:13.232645 WARN POST /auth", + "2013/01/13T07:02:21.542145 WARN GET /favicon.ico", + "2013/01/13T07:02:43.632145 WARN POST /login", + ], + "input_tag" => tag, + "input_tag_last" => tag.split('.').last, + } end context 'default' do let(:config) { CONFIG } before do Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, {"count"=>4, - "message"=>["2013/01/13T07:02:11.124202 INFO GET /ping","2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:21.542145 WARN GET /favicon.ico","2013/01/13T07:02:43.632145 WARN POST /login"], - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) end it { emit } end context 'regexp' do let(:config) { CONFIG + %[ regexp WARN ] } before do Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, {"count"=>3, - "message"=>["2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:21.542145 WARN GET /favicon.ico","2013/01/13T07:02:43.632145 WARN POST /login"], - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected.merge({ + "count"=>3, + "message"=>[ + "2013/01/13T07:02:13.232645 WARN POST /auth", + "2013/01/13T07:02:21.542145 WARN GET /favicon.ico", + "2013/01/13T07:02:43.632145 WARN POST /login" + ], + })) end it { emit } end context 'exclude' do - let(:config) do - CONFIG + %[ - regexp WARN - exclude favicon - ] - end + let(:config) { CONFIG + %[regexp WARN \n exclude favicon] } before do Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, {"count"=>2, - "message"=>["2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:43.632145 WARN POST /login"], - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected.merge({ + "count"=>2, + "message"=>[ + "2013/01/13T07:02:13.232645 WARN POST /auth", + "2013/01/13T07:02:43.632145 WARN POST /login" + ], + })) end it { emit } end - context 'threshold (hit)' do - let(:config) do - CONFIG + %[ - regexp WARN - threshold 3 - ] + context "threshold and comparator" do + context '>= threshold' do + let(:config) { CONFIG + %[threshold 4] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) + end + it { emit } end - before do - Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, {"count"=>3, - "message"=>["2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:21.542145 WARN GET /favicon.ico","2013/01/13T07:02:43.632145 WARN POST /login"], - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) + + context 'not >= threshold' do + let(:config) { CONFIG + %[threshold 5] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_not_receive(:emit) + end + it { emit } end - it { emit } + + context '<= threshold' do + let(:config) { CONFIG + %[threshold 4 \n comparator <=] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) + end + it { emit } + end + + context 'not <= threshold' do + let(:config) { CONFIG + %[threshold 3 \n comparator <=] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_not_receive(:emit) + end + it { emit } + end end - context 'threshold (miss)' do - let(:config) do - CONFIG + %[ - regexp WARN - threshold 4 - ] + context "less|greater_than|equal" do + context 'greater_equal' do + let(:config) { CONFIG + %[greater_equal 4] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) + end + it { emit } end - before do - Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_not_receive(:emit) + + context 'not greater_equal' do + let(:config) { CONFIG + %[greater_equal 5] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_not_receive(:emit) + end + it { emit } end - it { emit } + + context 'greater_than' do + let(:config) { CONFIG + %[greater_than 3] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) + end + it { emit } + end + + context 'not greater_than' do + let(:config) { CONFIG + %[greater_than 4] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_not_receive(:emit) + end + it { emit } + end + + context 'less_equal' do + let(:config) { CONFIG + %[less_equal 4] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) + end + it { emit } + end + + context 'not less_equal' do + let(:config) { CONFIG + %[less_equal 3] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_not_receive(:emit) + end + it { emit } + end + + context 'less_than' do + let(:config) { CONFIG + %[less_than 5] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) + end + it { emit } + end + + context 'not less_than' do + let(:config) { CONFIG + %[less_than 4] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_not_receive(:emit) + end + it { emit } + end + + context 'between' do + let(:config) { CONFIG + %[greater_than 1 \n less_than 5] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected) + end + it { emit } + end + + context 'not between' do + let(:config) { CONFIG + %[greater_than 1 \n less_than 4] } + before do + Fluent::Engine.stub(:now).and_return(time) + Fluent::Engine.should_not_receive(:emit) + end + it { emit } + end end context 'output_tag' do - let(:config) do - CONFIG + %[ - regexp WARN - output_tag foo - ] - end + let(:config) { CONFIG + %[output_tag foo] } before do Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("foo", time, {"count"=>3, - "message"=>["2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:21.542145 WARN GET /favicon.ico","2013/01/13T07:02:43.632145 WARN POST /login"], - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) + Fluent::Engine.should_receive(:emit).with("foo", time, expected) end it { emit } end context 'add_tag_prefix' do - let(:config) do - CONFIG + %[ - regexp WARN - add_tag_prefix foo - ] - end + let(:config) { CONFIG + %[add_tag_prefix foo] } before do Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("foo.#{tag}", time, {"count"=>3, - "message"=>["2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:21.542145 WARN GET /favicon.ico","2013/01/13T07:02:43.632145 WARN POST /login"], - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) + Fluent::Engine.should_receive(:emit).with("foo.#{tag}", time, expected) end it { emit } end context 'output_with_joined_delimiter' do - let(:config) do - # \\n shall be \n in config file - CONFIG + %[ - regexp WARN - output_with_joined_delimiter \\n - ] - end + # \\n shall be \n in config file + let(:config) { CONFIG + %[output_with_joined_delimiter \\n] } before do Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, {"count"=>3, - "message"=>"2013/01/13T07:02:13.232645 WARN POST /auth\\n2013/01/13T07:02:21.542145 WARN GET /favicon.ico\\n2013/01/13T07:02:43.632145 WARN POST /login", - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) + message = expected["message"].join('\n') + Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, expected.merge("message" => message)) end it { emit } end context 'aggregate all' do + let(:messages) { ['foobar', 'foobar'] } let(:emit) do driver.run { messages.each {|message| driver.emit_with_tag({'message' => message}, time, 'foo.bar') } } driver.run { messages.each {|message| driver.emit_with_tag({'message' => message}, time, 'foo.bar2') } } driver.instance.flush_emit(0) end - - let(:config) do - CONFIG + %[ - regexp WARN - aggregate all - output_tag count - ] + let(:expected) do + { + "count"=>messages.size*2, + "message"=>messages*2, + } end + + let(:config) { CONFIG + %[aggregate all \n output_tag count] } before do Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("count", time, {"count"=>3*2, - "message"=>["2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:21.542145 WARN GET /favicon.ico","2013/01/13T07:02:43.632145 WARN POST /login"]*2, - }) + Fluent::Engine.should_receive(:emit).with("count", time, expected) end it { emit } end context 'replace_invalid_sequence' do - let(:config) do - CONFIG + %[ - regexp WARN - replace_invalid_sequence true - ] - end - let(:messages) do - [ - "\xff".force_encoding('UTF-8'), - ] - end + let(:config) { CONFIG + %[regexp WARN \n replace_invalid_sequence true] } + let(:messages) { [ "\xff".force_encoding('UTF-8') ] } before do Fluent::Engine.stub(:now).and_return(time) end - it { expect { emit }.not_to raise_error(ArgumentError) } + it { expect { emit }.not_to raise_error } end - describe "comparator <=" do - context 'threshold (hit)' do - let(:config) do - CONFIG + %[ - regexp WARN - threshold 3 - comparator <= - ] - end - before do - Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_receive(:emit).with("count.#{tag}", time, {"count"=>3, - "message"=>["2013/01/13T07:02:13.232645 WARN POST /auth","2013/01/13T07:02:21.542145 WARN GET /favicon.ico","2013/01/13T07:02:43.632145 WARN POST /login"], - "input_tag" => tag, - "input_tag_last" => tag.split('.').last, - }) - end - it { emit } + describe "store_file" do + let(:store_file) do + dirname = "tmp" + Dir.mkdir dirname unless Dir.exist? dirname + filename = "#{dirname}/test.dat" + File.unlink filename if File.exist? filename + filename end - context 'threshold (miss)' do - let(:config) do - CONFIG + %[ - regexp WARN - threshold 2 - comparator <= - ] - end - before do - Fluent::Engine.stub(:now).and_return(time) - Fluent::Engine.should_not_receive(:emit) - end - it { emit } + let(:config) { CONFIG + %[store_file #{store_file}] } + + it 'stored_data and loaded_data should equal' do + driver.run { messages.each {|message| driver.emit({'message' => message}, time) } } + driver.instance.shutdown + stored_counts = driver.instance.counts + stored_matches = driver.instance.matches + stored_saved_at = driver.instance.saved_at + stored_saved_duration = driver.instance.saved_duration + driver.instance.counts = {} + driver.instance.matches = {} + driver.instance.saved_at = nil + driver.instance.saved_duration = nil + + driver.instance.start + loaded_counts = driver.instance.counts + loaded_matches = driver.instance.matches + loaded_saved_at = driver.instance.saved_at + loaded_saved_duration = driver.instance.saved_duration + + loaded_counts.should == stored_counts + loaded_matches.should == stored_matches + loaded_saved_at.should == stored_saved_at + loaded_saved_duration.should == stored_saved_duration end end + end end - - -