test/appender/elasticsearch_test.rb in semantic_logger-4.1.1 vs test/appender/elasticsearch_test.rb in semantic_logger-4.2.0
- old
+ new
@@ -2,81 +2,243 @@
# Unit Test for SemanticLogger::Appender::Elasticsearch
module Appender
class ElasticsearchTest < Minitest::Test
describe SemanticLogger::Appender::Elasticsearch do
- before do
- skip('Concurrent::TimerTask issue is preventing the process from stopping.') if defined? JRuby
- Elasticsearch::Transport::Client.stub_any_instance(:bulk, true) do
- @appender = SemanticLogger::Appender::Elasticsearch.new(
- url: 'http://localhost:9200',
- batch_size: 1 # immediate flush
- )
+ describe 'providing a url' do
+ let :appender do
+ if ENV['ELASTICSEARCH']
+ SemanticLogger::Appender::Elasticsearch.new(url: 'http://localhost:9200')
+ else
+ Elasticsearch::Transport::Client.stub_any_instance(:bulk, true) do
+ SemanticLogger::Appender::Elasticsearch.new(url: 'http://localhost:9200')
+ end
+ end
end
- @message = 'AppenderElasticsearchTest log message'
- end
- after do
- @appender.close if @appender
- end
+ let :log_message do
+ 'AppenderElasticsearchTest log message'
+ end
- it 'logs to daily indexes' do
- index = nil
- @appender.stub(:enqueue, ->(ind, json) { index = ind['index']['_index'] }) do
- @appender.info @message
+ let :log do
+ log = SemanticLogger::Log.new('User', :info)
+ log.message = log_message
+ log
end
- assert_equal "semantic_logger-#{Time.now.strftime('%Y.%m.%d')}", index
- end
- SemanticLogger::LEVELS.each do |level|
- it "send #{level}" do
+ let :exception do
+ exc = nil
+ begin
+ Uh oh
+ rescue Exception => e
+ exc = e
+ end
+ exc
+ end
+
+ after do
+ appender.close
+ end
+
+ describe 'synchronous' do
+ it 'logs to daily indexes' do
+ bulk_index = nil
+ appender.stub(:write_to_elasticsearch, ->(messages) { bulk_index = messages.first }) do
+ appender.info log_message
+ end
+ index = bulk_index['index']['_index']
+ assert_equal "semantic_logger-#{Time.now.strftime('%Y.%m.%d')}", index
+ end
+
+ it 'logs message' do
+ request = stub_client { appender.log(log) }
+
+ assert hash = request[:body][1]
+ assert_equal log_message, hash[:message]
+ end
+
+ it 'logs level' do
+ request = stub_client { appender.log(log) }
+
+ assert hash = request[:body][1]
+ assert_equal :info, hash[:level]
+ end
+
+ it 'logs exception' do
+ log.exception = exception
+ request = stub_client { appender.log(log) }
+
+ assert hash = request[:body][1]
+ assert exception = hash[:exception]
+ assert_equal 'NameError', exception[:name]
+ assert_match 'undefined local variable or method', exception[:message]
+ assert exception[:stack_trace].first.include?(__FILE__), exception
+ end
+
+ it 'logs payload' do
+ h = {key1: 1, key2: 'a'}
+ log.payload = h
+ request = stub_client { appender.log(log) }
+
+ assert hash = request[:body][1]
+ refute hash[:stack_trace]
+ assert_equal h, hash[:payload], hash
+ end
+ end
+
+ describe 'async batch' do
+ it 'logs message' do
+ request = stub_client { appender.batch([log]) }
+
+ assert hash = request[:body][1]
+ assert_equal log_message, hash[:message]
+ assert_equal :info, hash[:level]
+ end
+
+ let :logs do
+ Array.new(3) do |i|
+ l = log.dup
+ l.message = "hello world#{i + 1}"
+ l
+ end
+ end
+
+ it 'logs multiple messages' do
+ request = stub_client { appender.batch(logs) }
+
+ assert body = request[:body]
+ assert_equal 4, body.size, body
+ index = body[0]['index']['_index']
+ assert_equal "semantic_logger-#{Time.now.strftime('%Y.%m.%d')}", index
+
+ assert_equal 'hello world1', body[1][:message]
+ assert_equal 'hello world2', body[2][:message]
+ assert_equal 'hello world3', body[3][:message]
+ end
+ end
+
+ def stub_client(&block)
request = nil
- @appender.client.stub(:bulk, -> r { request = r; {"status" => 201} }) do
- @appender.send(level, @message)
+ appender.client.stub(:bulk, ->(r) { request = r; {'status' => 201} }, &block)
+ request
+ end
+ end
+
+ describe 'elasticsearch parameters' do
+ let :appender do
+ Elasticsearch::Transport::Client.stub_any_instance(:bulk, true) do
+ SemanticLogger::Appender::Elasticsearch.new(
+ hosts: [{host: 'localhost', port: 9200}]
+ )
end
+ end
- message = request[:body][1]
- assert_equal @message, message[:message]
- assert_equal level, message[:level]
+ let :log_message do
+ 'AppenderElasticsearchTest log message'
end
- it "sends #{level} exceptions" do
+ let :log do
+ log = SemanticLogger::Log.new('User', :info)
+ log.message = log_message
+ log
+ end
+
+ let :exception do
exc = nil
begin
Uh oh
rescue Exception => e
exc = e
end
- request = nil
- @appender.client.stub(:bulk, -> r { request = r; {"status" => 201} }) do
- @appender.send(level, 'Reading File', exc)
+ exc
+ end
+
+ after do
+ appender.close
+ end
+
+ describe 'synchronous' do
+ it 'logs to daily indexes' do
+ bulk_index = nil
+ appender.stub(:write_to_elasticsearch, ->(messages) { bulk_index = messages.first }) do
+ appender.info log_message
+ end
+ index = bulk_index['index']['_index']
+ assert_equal "semantic_logger-#{Time.now.strftime('%Y.%m.%d')}", index
end
- hash = request[:body][1]
+ it 'logs message' do
+ request = stub_client { appender.log(log) }
- assert 'Reading File', hash[:message]
- assert exception = hash[:exception]
- assert 'NameError', exception[:name]
- assert 'undefined local variable or method', exception[:message]
- assert_equal level, hash[:level]
- assert exception[:stack_trace].first.include?(__FILE__), exception
+ assert hash = request[:body][1]
+ assert_equal log_message, hash[:message]
+ end
+
+ it 'logs level' do
+ request = stub_client { appender.log(log) }
+
+ assert hash = request[:body][1]
+ assert_equal :info, hash[:level]
+ end
+
+ it 'logs exception' do
+ log.exception = exception
+ request = stub_client { appender.log(log) }
+
+ assert hash = request[:body][1]
+ assert exception = hash[:exception]
+ assert_equal 'NameError', exception[:name]
+ assert_match 'undefined local variable or method', exception[:message]
+ assert exception[:stack_trace].first.include?(__FILE__), exception
+ end
+
+ it 'logs payload' do
+ h = {key1: 1, key2: 'a'}
+ log.payload = h
+ request = stub_client { appender.log(log) }
+
+ assert hash = request[:body][1]
+ refute hash[:stack_trace]
+ assert_equal h, hash[:payload], hash
+ end
end
- it "sends #{level} custom attributes" do
- request = nil
- @appender.client.stub(:bulk, -> r { request = r; {"status" => 201} }) do
- @appender.send(level, @message, {key1: 1, key2: 'a'})
+ describe 'async batch' do
+ it 'logs message' do
+ request = stub_client { appender.batch([log]) }
+
+ assert hash = request[:body][1]
+ assert_equal log_message, hash[:message]
+ assert_equal :info, hash[:level]
end
- message = request[:body][1]
- assert_equal @message, message[:message]
- assert_equal level, message[:level]
- refute message[:stack_trace]
- assert payload = message[:payload], message
- assert_equal 1, payload[:key1], message
- assert_equal 'a', payload[:key2], message
+ let :logs do
+ Array.new(3) do |i|
+ l = log.dup
+ l.message = "hello world#{i + 1}"
+ l
+ end
+ end
+
+ it 'logs multiple messages' do
+ request = stub_client { appender.batch(logs) }
+
+ assert body = request[:body]
+ assert_equal 4, body.size, body
+ index = body[0]['index']['_index']
+ assert_equal "semantic_logger-#{Time.now.strftime('%Y.%m.%d')}", index
+
+ assert_equal 'hello world1', body[1][:message]
+ assert_equal 'hello world2', body[2][:message]
+ assert_equal 'hello world3', body[3][:message]
+ end
end
- end
+ def stub_client(&block)
+ request = nil
+ appender.client.stub(:bulk, ->(r) { request = r; {'status' => 201} }, &block)
+ request
+ end
+ end
end
end
end