Sha256: 654a0b383a84ab7407c3a0483a107fca6275e7e575e9fe10e15759ac6e732beb

Contents?: true

Size: 1.89 KB

Versions: 27

Compression:

Stored size: 1.89 KB

Contents

require_relative '../helper'
require 'fluent/test'

class StdoutOutputTest < Test::Unit::TestCase
  def setup
    Fluent::Test.setup
  end

  CONFIG = %[
  ]

  def create_driver(conf = CONFIG)
    Fluent::Test::OutputTestDriver.new(Fluent::StdoutOutput).configure(conf)
  end

  def test_configure
    d = create_driver
    assert_equal 'json', d.instance.output_type
  end

  def test_configure_output_type
    d = create_driver(CONFIG + "\noutput_type json")
    assert_equal 'json', d.instance.output_type

    d = create_driver(CONFIG + "\noutput_type hash")
    assert_equal 'hash', d.instance.output_type

    assert_raise(Fluent::ConfigError) do
      d = create_driver(CONFIG + "\noutput_type foo")
    end
  end

  data('oj' => 'oj', 'yajl' => 'yajl')
  def test_emit_json(data)
    d = create_driver(CONFIG + "\noutput_type json\njson_parser #{data}")
    time = Time.now
    out = capture_log { d.emit({'test' => 'test'}, time) }
    assert_equal "#{time.localtime} test: {\"test\":\"test\"}\n", out

    if data == 'yajl'
      # NOTE: Float::NAN is not jsonable
      assert_raise(Yajl::EncodeError) { d.emit({'test' => Float::NAN}, time) }
    else
      out = capture_log { d.emit({'test' => Float::NAN}, time) }
      assert_equal "#{time.localtime} test: {\"test\":NaN}\n", out
    end
  end

  def test_emit_hash
    d = create_driver(CONFIG + "\noutput_type hash")
    time = Time.now
    out = capture_log { d.emit({'test' => 'test'}, time) }
    assert_equal "#{time.localtime} test: {\"test\"=>\"test\"}\n", out

    # NOTE: Float::NAN is not jsonable, but hash string can output it.
    out = capture_log { d.emit({'test' => Float::NAN}, time) }
    assert_equal "#{time.localtime} test: {\"test\"=>NaN}\n", out
  end

  private

  # Capture the log output of the block given
  def capture_log(&block)
    tmp = $log
    $log = StringIO.new
    yield
    return $log.string
  ensure
    $log = tmp
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
fluentd-0.12.43 test/plugin/test_out_stdout.rb
fluentd-0.12.42 test/plugin/test_out_stdout.rb
fluentd-0.12.41 test/plugin/test_out_stdout.rb
fluentd-0.12.40 test/plugin/test_out_stdout.rb
fluentd-0.12.39 test/plugin/test_out_stdout.rb
fluentd-0.12.38 test/plugin/test_out_stdout.rb
fluentd-0.12.37 test/plugin/test_out_stdout.rb
fluentd-0.12.36 test/plugin/test_out_stdout.rb
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/fluentd-0.12.35/test/plugin/test_out_stdout.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/fluentd-0.12.35/test/plugin/test_out_stdout.rb
fluentd-0.12.35 test/plugin/test_out_stdout.rb
fluentd-0.12.34 test/plugin/test_out_stdout.rb
fluentd-0.12.33 test/plugin/test_out_stdout.rb
fluentd-0.12.32 test/plugin/test_out_stdout.rb
fluentd-0.12.31 test/plugin/test_out_stdout.rb
fluentd-0.12.30 test/plugin/test_out_stdout.rb
fluentd-0.12.29 test/plugin/test_out_stdout.rb
fluentd-0.12.28 test/plugin/test_out_stdout.rb
fluentd-0.12.27 test/plugin/test_out_stdout.rb
fluentd-0.12.26 test/plugin/test_out_stdout.rb