require 'fluent/test' require 'fluent/plugin/out_s3' class S3OutputTest < Test::Unit::TestCase def setup Fluent::Test.setup end CONFIG = %[ aws_key_id test_key_id aws_sec_key test_sec_key s3_bucket test_bucket path log utc buffer_type memory ] def create_driver(conf = CONFIG) Fluent::Test::BufferedOutputTestDriver.new(Fluent::S3Output) do def write(chunk) chunk.read end end.configure(conf) end def test_configure d = create_driver assert_equal 'test_key_id', d.instance.aws_key_id assert_equal 'test_sec_key', d.instance.aws_sec_key assert_equal 'test_bucket', d.instance.s3_bucket assert_equal 'log', d.instance.path end def test_format d = create_driver time = Time.parse("2011-01-02 13:14:15 UTC").to_i d.emit({"a"=>1}, time) d.emit({"a"=>2}, time) d.expect_format %[2011-01-02T13:14:15Z\ttest\t{"a":1}\n] d.expect_format %[2011-01-02T13:14:15Z\ttest\t{"a":2}\n] d.run end def test_format_included_tag_and_time config = [CONFIG, 'include_tag_key true', 'include_time_key true'].join("\n") d = create_driver(config) time = Time.parse("2011-01-02 13:14:15 UTC").to_i d.emit({"a"=>1}, time) d.emit({"a"=>2}, time) d.expect_format %[2011-01-02T13:14:15Z\ttest\t{"a":1,"tag":"test","time":"2011-01-02T13:14:15Z"}\n] d.expect_format %[2011-01-02T13:14:15Z\ttest\t{"a":2,"tag":"test","time":"2011-01-02T13:14:15Z"}\n] d.run end def test_format_with_format_json config = [CONFIG, 'format_json true'].join("\n") d = create_driver(config) time = Time.parse("2011-01-02 13:14:15 UTC").to_i d.emit({"a"=>1}, time) d.emit({"a"=>2}, time) d.expect_format %[{"a":1}\n] d.expect_format %[{"a":2}\n] d.run end def test_format_with_format_json_included_tag config = [CONFIG, 'format_json true', 'include_tag_key true'].join("\n") d = create_driver(config) time = Time.parse("2011-01-02 13:14:15 UTC").to_i d.emit({"a"=>1}, time) d.emit({"a"=>2}, time) d.expect_format %[{"a":1,"tag":"test"}\n] d.expect_format %[{"a":2,"tag":"test"}\n] d.run end def test_format_with_format_json_included_time config = [CONFIG, 'format_json true', 'include_time_key true'].join("\n") d = create_driver(config) time = Time.parse("2011-01-02 13:14:15 UTC").to_i d.emit({"a"=>1}, time) d.emit({"a"=>2}, time) d.expect_format %[{"a":1,"time":"2011-01-02T13:14:15Z"}\n] d.expect_format %[{"a":2,"time":"2011-01-02T13:14:15Z"}\n] d.run end def test_format_with_format_json_included_tag_and_time config = [CONFIG, 'format_json true', 'include_tag_key true', 'include_time_key true'].join("\n") d = create_driver(config) time = Time.parse("2011-01-02 13:14:15 UTC").to_i d.emit({"a"=>1}, time) d.emit({"a"=>2}, time) d.expect_format %[{"a":1,"tag":"test","time":"2011-01-02T13:14:15Z"}\n] d.expect_format %[{"a":2,"tag":"test","time":"2011-01-02T13:14:15Z"}\n] d.run end def test_write d = create_driver time = Time.parse("2011-01-02 13:14:15 UTC").to_i d.emit({"a"=>1}, time) d.emit({"a"=>2}, time) # S3OutputTest#write returns chunk.read data = d.run assert_equal %[2011-01-02T13:14:15Z\ttest\t{"a":1}\n] + %[2011-01-02T13:14:15Z\ttest\t{"a":2}\n], data end end