test/appender/splunk_test.rb in semantic_logger-3.1.0 vs test/appender/splunk_test.rb in semantic_logger-3.2.0

- old
+ new

@@ -2,48 +2,79 @@ # Unit Test for SemanticLogger::Appender::Splunk # module Appender class SplunkTest < Minitest::Test - describe SemanticLogger::Appender::Splunk do - describe '#parse_options' do - describe 'argument errors' do - it 'raise argument error for missing username' do - error = assert_raises ArgumentError do - SemanticLogger::Appender::Splunk.new({}) - end + class Mock + attr_accessor :message, :event - assert_equal 'Must supply a username.', error.message - end + def submit(message, event) + self.message = message + self.event = event + end + end - it 'raise argument error for missing password' do - error = assert_raises ArgumentError do - SemanticLogger::Appender::Splunk.new(username: 'username') - end + describe SemanticLogger::Appender::Splunk do + before do + SemanticLogger::Appender::Splunk.stub_any_instance(:reopen, nil) do + @appender = SemanticLogger::Appender::Splunk.new(level: :info) + end + @message = 'AppenderSplunkTest log message' + end - assert_equal 'Must supply a password.', error.message - end + it 'not send :trace notifications to Splunk when set to :error' do + mock = Mock.new + @appender.stub(:service_index, mock) do + @appender.trace('AppenderSplunkTest trace message') end + assert_nil mock.event + assert_nil mock.message + end - describe 'set default values' do - it 'have default values' do - appender = Splunk.stub(:connect, Splunk::Service.new({})) do - Splunk::Service.stub_any_instance(:indexes, {}) do - SemanticLogger::Appender::Splunk.new(username: 'username', password: 'password') - end - end - config = appender.config - # Default host - assert_equal 'localhost', config[:host] - # Default port - assert_equal 8089, config[:port] - # Default scheme - assert_equal :https, config[:scheme] - #Default index - assert_equal 'main', appender.index - end + it 'send exception notifications to Splunk with severity' do + hash = nil + exc = nil + begin + Uh oh + rescue Exception => e + exc = e end + mock = Mock.new + @appender.stub(:service_index, mock) do + @appender.error 'Reading File', exc + end + assert_equal 'Reading File', mock.message + hash = mock.event + refute hash[:message] + assert 'NameError', hash[:exception][:name] + assert 'undefined local variable or method', hash[:exception][:message] + assert_equal 4, hash[:level_index], 'Should be error level (4)' + assert_equal :error, hash[:level] + assert hash[:exception][:stack_trace].first.include?(__FILE__), hash[:exception] + end + + it 'send error notifications to Splunk with severity' do + mock = Mock.new + @appender.stub(:service_index, mock) do + @appender.error @message + end + assert_equal @message, mock.message + assert_equal :error, mock.event[:level] + refute mock.event[:stack_trace] + end + + it 'send notification to Splunk with custom attributes' do + mock = Mock.new + @appender.stub(:service_index, mock) do + @appender.error @message, {key1: 1, key2: 'a'} + end + assert_equal @message, mock.message + hash = mock.event + assert_equal :error, hash[:level] + refute hash[:stack_trace] + assert_equal(1, hash[:key1], hash) + assert_equal('a', hash[:key2], hash) end end end end