From fc380f8a1534ba36333efa1ab9983362940bbe96 Mon Sep 17 00:00:00 2001 From: TAGOMORI Satoshi Date: Thu, 21 Jul 2016 16:13:15 +0900 Subject: [PATCH] remove timezone related tests from formatter, and add it to tests of inject plugin helper --- test/plugin/test_formatter_ltsv.rb | 48 ------------------ test/plugin_helper/test_inject.rb | 81 ++++++++++++++++++++++++++++++ test/test_formatter.rb | 44 ---------------- 3 files changed, 81 insertions(+), 92 deletions(-) diff --git a/test/plugin/test_formatter_ltsv.rb b/test/plugin/test_formatter_ltsv.rb index 65371662ee..87008b294c 100644 --- a/test/plugin/test_formatter_ltsv.rb +++ b/test/plugin/test_formatter_ltsv.rb @@ -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 diff --git a/test/plugin_helper/test_inject.rb b/test/plugin_helper/test_inject.rb index 6eb8fb39a1..fc896994f8 100644 --- a/test/plugin_helper/test_inject.rb +++ b/test/plugin_helper/test_inject.rb @@ -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 @@ -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 diff --git a/test/test_formatter.rb b/test/test_formatter.rb index a856517ee4..d65d3eb74a 100644 --- a/test/test_formatter.rb +++ b/test/test_formatter.rb @@ -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