-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Migrate filter_stdout plugin to v0.14 API #1058
Merged
tagomoris
merged 18 commits into
fluent:master
from
okkez:migrate-v0.14-api-filter_stdout
Aug 1, 2016
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c54be0a
Migrate filter_stdout plugin to v0.14 API
okkez b3c2c73
Use data-driven style
okkez 735bc93
Add configure test group
okkez abac73f
Indent
okkez 7442a49
Shorten redundant test name
okkez b4bd89b
Remove unused local variable
okkez 0228edf
Use UTC in StdoutFilterTest
okkez eb7dd4e
Use event_time test helper method
okkez 22a0214
Use helpers formatter
okkez e45a537
Extract `capture_log`
okkez 9f9d176
Add group "flat style parameters"
okkez 97e9343
Indent
okkez 713937f
Use flat style configuration properly
okkez 3e6150a
Add test using <format> sub section
okkez c1d6d1c
Use `formatter_create` properly
okkez 7ce0b5c
Use `localtime` parameter
okkez bf61d3c
Use `default_type` keyword
okkez 97816ad
Use `config_element` to build configuration for test
okkez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,230 @@ | ||
require_relative '../helper' | ||
require 'fluent/test/driver/filter' | ||
require 'fluent/plugin/filter_stdout' | ||
require 'timecop' | ||
require 'flexmock/test_unit' | ||
|
||
class StdoutFilterTest < Test::Unit::TestCase | ||
include Fluent | ||
include FlexMock::TestCase | ||
|
||
def setup | ||
Fluent::Test.setup | ||
@old_tz = ENV["TZ"] | ||
ENV["TZ"] = "UTC" | ||
Timecop.freeze | ||
end | ||
|
||
def teardown | ||
super # FlexMock::TestCase requires this | ||
# http://flexmock.rubyforge.org/FlexMock/TestCase.html | ||
Timecop.return | ||
ENV["TZ"] = @old_tz | ||
end | ||
|
||
CONFIG = %[ | ||
] | ||
|
||
def create_driver(conf = CONFIG) | ||
Test::FilterTestDriver.new(StdoutFilter, 'filter.test').configure(conf) | ||
Fluent::Test::Driver::Filter.new(Fluent::Plugin::StdoutFilter).configure(conf) | ||
end | ||
|
||
def emit(d, msg, time) | ||
def filter(d, time, record) | ||
d.run { | ||
d.emit(msg, time) | ||
}.filtered_as_array[0][2] | ||
d.feed("filter.test", time, record) | ||
} | ||
d.filtered_records | ||
end | ||
|
||
def test_through_record | ||
d = create_driver | ||
time = Time.now | ||
filtered = emit(d, {'test' => 'test'}, Fluent::EventTime.from_time(time)) | ||
assert_equal({'test' => 'test'}, filtered) | ||
filtered = filter(d, event_time, {'test' => 'test'}) | ||
assert_equal([{'test' => 'test'}], filtered) | ||
end | ||
|
||
def test_configure_default | ||
d = create_driver | ||
assert_equal 'json', d.instance.formatter.output_type | ||
end | ||
|
||
def test_configure_output_type | ||
d = create_driver(CONFIG + "\noutput_type json") | ||
assert_equal 'json', d.instance.formatter.output_type | ||
|
||
d = create_driver(CONFIG + "\noutput_type hash") | ||
assert_equal 'hash', d.instance.formatter.output_type | ||
|
||
d = create_driver(CONFIG + "\noutput_type ltsv") | ||
assert_equal 'ltsv', d.instance.formatter.output_type | ||
|
||
assert_raise(Fluent::ConfigError) do | ||
d = create_driver(CONFIG + "\noutput_type foo") | ||
sub_test_case "flat style parameters" do | ||
sub_test_case "configure" do | ||
def test_configure_default | ||
d = create_driver | ||
d.run {} | ||
assert_equal 'json', d.instance.formatter.output_type | ||
end | ||
|
||
data(json: "json", | ||
hash: "hash", | ||
ltsv: "ltsv") | ||
def test_output_type(data) | ||
d = create_driver(CONFIG + "\noutput_type #{data}") | ||
d.run {} | ||
assert_equal data, d.instance.formatter.output_type | ||
end | ||
|
||
def test_invalid_output_type | ||
assert_raise(Fluent::ConfigError) do | ||
d = create_driver(CONFIG + "\noutput_type foo") | ||
d.run {} | ||
end | ||
end | ||
end | ||
end | ||
|
||
def test_output_type_json | ||
d = create_driver(CONFIG + "\noutput_type json") | ||
time = Time.now | ||
out = capture_log(d) { emit(d, {'test' => 'test'}, Fluent::EventTime.from_time(time)) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\":\"test\"}\n", out | ||
def test_output_type_json | ||
d = create_driver(CONFIG + "\noutput_type json") | ||
etime = event_time | ||
time = Time.at(etime.sec) | ||
out = capture_log(d) { filter(d, etime, {'test' => 'test'}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\":\"test\"}\n", out | ||
|
||
# NOTE: Float::NAN is not jsonable | ||
d = create_driver(CONFIG + "\noutput_type json") | ||
flexmock(d.instance.router).should_receive(:emit_error_event) | ||
filter(d, etime, {'test' => Float::NAN}) | ||
end | ||
|
||
# NOTE: Float::NAN is not jsonable | ||
d = create_driver(CONFIG + "\noutput_type json") | ||
flexmock(d.instance.router).should_receive(:emit_error_event) | ||
emit(d, {'test' => Float::NAN}, time) | ||
end | ||
def test_output_type_hash | ||
d = create_driver(CONFIG + "\noutput_type hash") | ||
etime = event_time | ||
time = Time.at(etime.sec) | ||
out = capture_log(d) { filter(d, etime, {'test' => 'test'}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\"=>\"test\"}\n", out | ||
|
||
# NOTE: Float::NAN is not jsonable, but hash string can output it. | ||
d = create_driver(CONFIG + "\noutput_type hash") | ||
out = capture_log(d) { filter(d, etime, {'test' => Float::NAN}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\"=>NaN}\n", out | ||
end | ||
|
||
def test_output_type_hash | ||
d = create_driver(CONFIG + "\noutput_type hash") | ||
time = Time.now | ||
out = capture_log(d) { emit(d, {'test' => 'test'}, Fluent::EventTime.from_time(time)) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\"=>\"test\"}\n", out | ||
# Use include_time_key to output the message's time | ||
def test_include_time_key | ||
d = create_driver(CONFIG + "\noutput_type json\ninclude_time_key true\nlocaltime false") | ||
etime = event_time | ||
time = Time.at(etime.sec) | ||
message_time = event_time("2011-01-02 13:14:15 UTC") | ||
out = capture_log(d) { filter(d, message_time, {'test' => 'test'}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\":\"test\",\"time\":\"2011-01-02T13:14:15Z\"}\n", out | ||
end | ||
|
||
# NOTE: Float::NAN is not jsonable, but hash string can output it. | ||
d = create_driver(CONFIG + "\noutput_type hash") | ||
out = capture_log(d) { emit(d, {'test' => Float::NAN}, Fluent::EventTime.from_time(time)) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\"=>NaN}\n", out | ||
# out_stdout formatter itself can also be replaced | ||
def test_format_json | ||
d = create_driver(CONFIG + "\nformat json") | ||
out = capture_log(d) { filter(d, event_time, {'test' => 'test'}) } | ||
assert_equal "{\"test\":\"test\"}\n", out | ||
end | ||
end | ||
|
||
# Use include_time_key to output the message's time | ||
def test_include_time_key | ||
d = create_driver(CONFIG + "\noutput_type json\ninclude_time_key true\nutc") | ||
time = Time.now | ||
message_time = Fluent::EventTime.parse("2011-01-02 13:14:15 UTC") | ||
out = capture_log(d) { emit(d, {'test' => 'test'}, message_time) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\":\"test\",\"time\":\"2011-01-02T13:14:15Z\"}\n", out | ||
end | ||
sub_test_case "with <format> sub section" do | ||
sub_test_case "configure" do | ||
def test_default | ||
conf = format_config(%[ | ||
@type stdout | ||
]) | ||
d = create_driver(conf) | ||
d.run {} | ||
assert_equal("json", d.instance.formatter.output_type) | ||
end | ||
|
||
data(json: "json", | ||
hash: "hash", | ||
ltsv: "ltsv") | ||
def test_output_type(data) | ||
conf = format_config(%[ | ||
@type stdout | ||
output_type #{data} | ||
]) | ||
d = create_driver(conf) | ||
d.run {} | ||
assert_equal(data, d.instance.formatter.output_type) | ||
end | ||
|
||
def test_invalid_output_type | ||
assert_raise(Fluent::ConfigError) do | ||
conf = format_config(%[ | ||
@type stdout | ||
output_type foo | ||
]) | ||
d = create_driver(conf) | ||
d.run {} | ||
end | ||
end | ||
end | ||
|
||
# out_stdout formatter itself can also be replaced | ||
def test_format_json | ||
d = create_driver(CONFIG + "\nformat json") | ||
time = Time.now | ||
out = capture_log(d) { emit(d, {'test' => 'test'}, Fluent::EventTime.from_time(time)) } | ||
assert_equal "{\"test\":\"test\"}\n", out | ||
end | ||
sub_test_case "output_type" do | ||
def test_json | ||
d = create_driver(format_config(%[ | ||
@type stdout | ||
output_type json | ||
])) | ||
etime = event_time | ||
time = Time.at(etime.sec) | ||
out = capture_log(d) { filter(d, etime, {'test' => 'test'}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\":\"test\"}\n", out | ||
end | ||
|
||
def test_json_nan | ||
# NOTE: Float::NAN is not jsonable | ||
d = create_driver(format_config(%[ | ||
@type stdout | ||
output_type json | ||
])) | ||
etime = event_time | ||
flexmock(d.instance.router).should_receive(:emit_error_event) | ||
filter(d, etime, {'test' => Float::NAN}) | ||
end | ||
|
||
def test_hash | ||
d = create_driver(format_config(%[ | ||
@type stdout | ||
output_type hash | ||
])) | ||
etime = event_time | ||
time = Time.at(etime.sec) | ||
out = capture_log(d) { filter(d, etime, {'test' => 'test'}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\"=>\"test\"}\n", out | ||
end | ||
|
||
def test_hash_nan | ||
# NOTE: Float::NAN is not jsonable, but hash string can output it. | ||
d = create_driver(format_config(%[ | ||
@type stdout | ||
output_type hash | ||
])) | ||
etime = event_time | ||
time = Time.at(etime.sec) | ||
out = capture_log(d) { filter(d, etime, {'test' => Float::NAN}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\"=>NaN}\n", out | ||
end | ||
|
||
# Use include_time_key to output the message's time | ||
def test_include_time_key | ||
d = create_driver(format_config(%[ | ||
@type stdout | ||
output_type json | ||
]) + | ||
inject_config(%[ | ||
time_key time | ||
time_type string | ||
localtime false | ||
])) | ||
etime = event_time | ||
time = Time.at(etime.sec) | ||
message_time = event_time("2011-01-02 13:14:15 UTC") | ||
out = capture_log(d) { filter(d, message_time, {'test' => 'test'}) } | ||
assert_equal "#{time.localtime} filter.test: {\"test\":\"test\",\"time\":\"2011-01-02T13:14:15Z\"}\n", out | ||
end | ||
end | ||
|
||
private | ||
def format_config(config) | ||
%[ | ||
<format> | ||
#{config} | ||
</format> | ||
] | ||
end | ||
|
||
# Capture the log output of the block given | ||
def capture_log(d, &block) | ||
tmp = d.instance.log.out | ||
d.instance.log.out = StringIO.new | ||
yield | ||
return d.instance.log.out.string | ||
ensure | ||
d.instance.log.out = tmp | ||
def inject_config(config) | ||
%[ | ||
<inject> | ||
#{config} | ||
</inject> | ||
] | ||
end | ||
end | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do this operations.
Please use
default_type
keyword argument offormatter_create
method instead.