Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use Fluent::Config::Element in test #4074

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions test/compat/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ def test_lookup_known_parser(data)

def test_parse_with_return
parser = Fluent::TextParser.new
parser.configure('format' => 'none')
parser.configure(config_element('test', '', 'format' => 'none'))
_time, record = parser.parse('log message!')
assert_equal({'message' => 'log message!'}, record)
end

def test_parse_with_block
parser = Fluent::TextParser.new
parser.configure('format' => 'none')
parser.configure(config_element('test', '', 'format' => 'none'))
parser.parse('log message!') { |time, record|
assert_equal({'message' => 'log message!'}, record)
}
end

def test_multi_event_parser
parser = Fluent::TextParser.new
parser.configure('format' => 'multi_event_test')
parser.configure(config_element('test', '', 'format' => 'multi_event_test'))
i = 0
parser.parse('log message!') { |time, record|
assert_equal('log message!', record['message'])
Expand All @@ -67,7 +67,7 @@ def test_setting_estimate_current_event_value
assert_nil p1.estimate_current_event
assert_nil p1.parser

p1.configure('format' => 'none')
p1.configure(config_element('test', '', 'format' => 'none'))
assert_equal true, p1.parser.estimate_current_event

p2 = Fluent::TextParser.new
Expand All @@ -76,7 +76,7 @@ def test_setting_estimate_current_event_value

p2.estimate_current_event = false

p2.configure('format' => 'none')
p2.configure(config_element('test', '', 'format' => 'none'))
assert_equal false, p2.parser.estimate_current_event
end

Expand Down
54 changes: 29 additions & 25 deletions test/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ def record
{'message' => 'awesome', 'greeting' => 'hello'}
end

def config_element_in_params(params)
config_element('test', '', params)
end

class BaseFormatterTest < ::Test::Unit::TestCase
include FormatterTest

def test_call
formatter = Formatter.new
formatter.configure({})
formatter.configure(config_element())
ashie marked this conversation as resolved.
Show resolved Hide resolved
assert_raise NotImplementedError do
formatter.format('tag', Engine.now, {})
end
Expand Down Expand Up @@ -135,7 +139,7 @@ def setup
end

def test_format
@formatter.configure({})
@formatter.configure(config_element())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the one we should use Fluent::Test::FormatterTestDriver for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry I've already merged this although you are right...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! I'm making another PR for this now!

formatted = @formatter.format(tag, @time, record)

assert_equal(record.to_msgpack, formatted)
Expand All @@ -159,27 +163,27 @@ def test_config_params
assert_equal "\t", @formatter.delimiter
assert_equal ":", @formatter.label_delimiter

@formatter.configure(
@formatter.configure(config_element_in_params(
'delimiter' => ',',
'label_delimiter' => '=',
)
))

assert_equal ",", @formatter.delimiter
assert_equal "=", @formatter.label_delimiter
end

def test_format
@formatter.configure({})
@formatter.configure(config_element())
formatted = @formatter.format(tag, @time, record)

assert_equal("message:awesome\tgreeting:hello#{@newline}", formatted)
end

def test_format_with_customized_delimiters
@formatter.configure(
@formatter.configure(config_element_in_params(
'delimiter' => ',',
'label_delimiter' => '=',
)
))
formatted = @formatter.format(tag, @time, record)

assert_equal("message=awesome,greeting=hello#{@newline}", formatted)
Expand All @@ -190,26 +194,26 @@ def record_with_tab
end

def test_format_suppresses_tab
@formatter.configure({})
@formatter.configure(config_element())
formatted = @formatter.format(tag, @time, record_with_tab)

assert_equal("message:awe some\tgreeting:hello #{@newline}", formatted)
end

def test_format_suppresses_tab_custom_replacement
@formatter.configure(
@formatter.configure(config_element_in_params(
'replacement' => 'X',
)
))
formatted = @formatter.format(tag, @time, record_with_tab)

assert_equal("message:aweXsome\tgreeting:helloX#{@newline}", formatted)
end

def test_format_suppresses_custom_delimiter
@formatter.configure(
@formatter.configure(config_element_in_params(
'delimiter' => 'w',
'label_delimiter' => '=',
)
))
formatted = @formatter.format(tag, @time, record)

assert_equal("message=a esomewgreeting=hello#{@newline}", formatted)
Expand All @@ -236,13 +240,13 @@ def test_config_params
'pipe' => ['|', '|'])
def test_config_params_with_customized_delimiters(data)
expected, target = data
@formatter.configure('delimiter' => target, 'fields' => 'a,b,c')
@formatter.configure(config_element_in_params('delimiter' => target, 'fields' => 'a,b,c'))
assert_equal expected, @formatter.delimiter
assert_equal ['a', 'b', 'c'], @formatter.fields
end

def test_format
@formatter.configure('fields' => 'message,message2')
@formatter.configure(config_element_in_params('fields' => 'message,message2'))
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -251,10 +255,10 @@ def test_format
end

def test_format_with_customized_delimiters
@formatter.configure(
@formatter.configure(config_element_in_params(
'fields' => 'message,message2',
'delimiter' => '\t'
)
))
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -263,10 +267,10 @@ def test_format_with_customized_delimiters
end

def test_format_with_non_quote
@formatter.configure(
@formatter.configure(config_element_in_params(
'fields' => 'message,message2',
'force_quotes' => 'false'
)
))
formatted = @formatter.format(tag, @time, {
'message' => 'awesome',
'message2' => 'awesome2'
Expand All @@ -286,9 +290,9 @@ def test_format_with_non_quote
'message3' => 'awesome3'
})
def test_format_with_empty_fields(data)
@formatter.configure(
@formatter.configure(config_element_in_params(
'fields' => 'message,message2,message3'
)
))
formatted = @formatter.format(tag, @time, data)
assert_equal("\"awesome\",\"\",\"awesome3\"\n", formatted)
end
Expand All @@ -298,7 +302,7 @@ def test_format_with_empty_fields(data)
'white_space' => 'one , two , three',
'blank' => 'one,,two,three')
def test_config_params_with_fields(data)
@formatter.configure('fields' => data)
@formatter.configure(config_element_in_params('fields' => data))
assert_equal %w(one two three), @formatter.fields
end
end
Expand All @@ -318,27 +322,27 @@ def test_config_params
formatter = TextFormatter::SingleValueFormatter.new
assert_equal "message", formatter.message_key

formatter.configure('message_key' => 'foobar')
formatter.configure(config_element_in_params('message_key' => 'foobar'))
assert_equal "foobar", formatter.message_key
end

def test_format
formatter = Fluent::Plugin.new_formatter('single_value')
formatter.configure({})
formatter.configure(config_element())
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal("awesome#{@newline}", formatted)
end

def test_format_without_newline
formatter = Fluent::Plugin.new_formatter('single_value')
formatter.configure('add_newline' => 'false')
formatter.configure(config_element_in_params('add_newline' => 'false'))
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal("awesome", formatted)
end

def test_format_with_message_key
formatter = TextFormatter::SingleValueFormatter.new
formatter.configure('message_key' => 'foobar')
formatter.configure(config_element_in_params('message_key' => 'foobar'))
formatted = formatter.format('tag', Engine.now, {'foobar' => 'foo'})

assert_equal("foo#{@newline}", formatted)
Expand Down