Skip to content

Commit

Permalink
remove timezone related tests from formatter, and add it to tests of …
Browse files Browse the repository at this point in the history
…inject plugin helper
  • Loading branch information
tagomoris committed Jul 21, 2016
1 parent 7a61e21 commit fc380f8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 92 deletions.
48 changes: 0 additions & 48 deletions test/plugin/test_formatter_ltsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,52 +49,4 @@ def test_format_with_customized_delimiters

assert_equal("message=awesome,greeting=hello\n", formatted)
end

sub_test_case "time config" do
def setup
@time = event_time("2014-09-27 00:00:00 +00:00").to_i
end

def with_timezone(tz)
oldtz, ENV['TZ'] = ENV['TZ'], tz
yield
ensure
ENV['TZ'] = oldtz
end

def format(conf)
d = create_driver({'include_time_key' => true}.merge(conf))
formatted = d.instance.format("tag", @time, {})
# Drop the leading "time:" and the trailing "\n".
formatted[5..-2]
end

def test_none
with_timezone("UTC-01") do
# 'localtime' is true by default.
assert_equal("2014-09-27T01:00:00+01:00", format({}))
end
end

def test_utc
with_timezone("UTC-01") do
# 'utc' takes precedence over 'localtime'.
assert_equal("2014-09-27T00:00:00Z", format("utc" => true))
end
end

def test_timezone
with_timezone("UTC-01") do
# 'timezone' takes precedence over 'localtime'.
assert_equal("2014-09-27T02:00:00+02:00", format("timezone" => "+02"))
end
end

def test_utc_timezone
with_timezone("UTC-01") do
# 'timezone' takes precedence over 'utc'.
assert_equal("2014-09-27T09:00:00+09:00", format("utc" => true, "timezone" => "Asia/Tokyo"))
end
end
end
end
81 changes: 81 additions & 0 deletions test/plugin_helper/test_inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def config_inject_section(hash = {})
assert_equal record.merge(injected), @d.inject_values_to_record('tag', time, record)
end
end

sub_test_case 'using inject_values_to_event_stream' do
local_timezone = Time.now.strftime('%z')
time_in_unix = Time.parse("2016-06-21 08:10:11 #{local_timezone}").to_i
Expand Down Expand Up @@ -403,4 +404,84 @@ def config_inject_section(hash = {})
assert_equal expected_es, @d.inject_values_to_event_stream('tag', data)
end
end

sub_test_case 'time formatting with modified timezone' do
setup do
@time = event_time("2014-09-27 00:00:00 +00:00").to_i
end

def with_timezone(tz)
oldtz, ENV['TZ'] = ENV['TZ'], tz
yield
ensure
ENV['TZ'] = oldtz
end

def format(conf)
@d.configure(config_inject_section(
"hostname_key" => "hostnamedata",
"hostname" => "myname.local",
"tag_key" => "tagdata",
"time_key" => "timedata",
"time_type" => "string",
"time_format" => "%Y_%m_%d %H:%M:%S.%N %z",
"timezone" => "+0000",
))
@d.start

record = {"key1" => "value1", "key2" => 2}
injected = {"hostnamedata" => "myname.local", "tagdata" => "tag", "timedata" => "2016_06_20 23:10:11.320101224 +0000"}
assert_equal record.merge(injected), @d.inject_values_to_record('tag', time, record)


d = create_driver({'include_time_key' => true}.merge(conf))
formatted = d.instance.format("tag", @time, {})
# Drop the leading "time:" and the trailing "\n".
formatted[5..-2]
end

def test_nothing_specified_about_time_formatting
with_timezone("UTC-01") do
# 'localtime' is true by default.
@d.configure(config_inject_section("time_key" => "t", "time_type" => "string"))
@d.start
record = @d.inject_values_to_record('tag', @time, {"message" => "yay"})

assert_equal("2014-09-27T01:00:00+01:00", record['t'])
end
end

def test_utc
with_timezone("UTC-01") do
# 'utc' takes precedence over 'localtime'.
@d.configure(config_inject_section("time_key" => "t", "time_type" => "string", "utc" => "true"))
@d.start
record = @d.inject_values_to_record('tag', @time, {"message" => "yay"})

assert_equal("2014-09-27T00:00:00Z", record['t'])
end
end

def test_timezone
with_timezone("UTC-01") do
# 'timezone' takes precedence over 'localtime'.
@d.configure(config_inject_section("time_key" => "t", "time_type" => "string", "timezone" => "+02"))
@d.start
record = @d.inject_values_to_record('tag', @time, {"message" => "yay"})

assert_equal("2014-09-27T02:00:00+02:00", record['t'])
end
end

def test_utc_timezone
with_timezone("UTC-01") do
# 'timezone' takes precedence over 'utc'.
@d.configure(config_inject_section("time_key" => "t", "time_type" => "string", "timezone" => "Asia/Tokyo", "utc" => "true"))
@d.start
record = @d.inject_values_to_record('tag', @time, {"message" => "yay"})

assert_equal("2014-09-27T09:00:00+09:00", record['t'])
end
end
end
end
44 changes: 0 additions & 44 deletions test/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,48 +316,4 @@ def test_find_formatter(data)
$LOAD_PATH.shift
end
end

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

def setup
@formatter = TextFormatter::LabeledTSVFormatter.new
@time = Time.new(2014, 9, 27, 0, 0, 0, 0).to_i
end

def format(conf)
@formatter.configure({'include_time_key' => true}.merge(conf))
formatted = @formatter.format("tag", @time, {})
# Drop the leading "time:" and the trailing "\n".
formatted[5..-2]
end

def test_none
with_timezone("UTC-01") do
# 'localtime' is true by default.
assert_equal("2014-09-27T01:00:00+01:00", format({}))
end
end

def test_utc
with_timezone("UTC-01") do
# 'utc' takes precedence over 'localtime'.
assert_equal("2014-09-27T00:00:00Z", format("utc" => true))
end
end

def test_timezone
with_timezone("UTC-01") do
# 'timezone' takes precedence over 'localtime'.
assert_equal("2014-09-27T02:00:00+02:00", format("timezone" => "+02"))
end
end

def test_utc_timezone
with_timezone("UTC-01") do
# 'timezone' takes precedence over 'utc'.
assert_equal("2014-09-27T09:00:00+09:00", format("utc" => true, "timezone" => "Asia/Tokyo"))
end
end
end
end

0 comments on commit fc380f8

Please sign in to comment.