Back to Repositories

Testing JSON Formatter Plugin Implementation in Fluentd

This test suite validates the JSON formatting functionality in Fluentd’s plugin system, specifically testing the JSONFormatter class. It ensures proper JSON serialization with different parser implementations and newline configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of JSON formatting capabilities in Fluentd.

Key areas tested include:
  • JSON generation with different parsers (oj and yajl)
  • Newline handling (LF and CRLF)
  • Symbolic record conversion
  • Optional newline configuration

Implementation Analysis

The testing approach utilizes Fluentd’s test driver framework for formatter plugins. It implements a systematic verification of JSON formatting with different configurations.

Key patterns include:
  • Test data parameterization using data-driven testing
  • Fixture setup with event time and platform-specific newline handling
  • Direct assertion comparison of generated JSON output

Technical Details

Testing infrastructure includes:
  • Fluentd Test::Unit framework
  • Test::Driver::Formatter for plugin testing
  • JSON parser implementations: oj and yajl
  • Platform-specific newline detection
  • Custom test helper methods for record generation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through careful organization and thorough validation.

Notable practices include:
  • Isolated test setup and teardown
  • Cross-platform compatibility handling
  • Parameterized test cases for comprehensive coverage
  • Clear separation of test data and assertions

fluent/fluentd

test/plugin/test_formatter_json.rb

            
require_relative '../helper'
require 'json'
require 'fluent/test/driver/formatter'
require 'fluent/plugin/formatter_json'

class JsonFormatterTest < ::Test::Unit::TestCase

  def setup
    @time = event_time
    @default_newline = if Fluent.windows?
                         "\r\n"
                       else
                         "\n"
                       end
  end

  def create_driver(conf = "")
    Fluent::Test::Driver::Formatter.new(Fluent::Plugin::JSONFormatter).configure(conf)
  end

  def tag
    "tag"
  end

  def record
    {'message' => 'awesome'}
  end

  def symbolic_record
    {:message => :awesome}
  end

  data('oj with LF' => ['oj', "lf", "\n"],
       'oj with CRLF' => ['oj', "crlf", "\r\n"],
       'yajl with LF' => ['yajl', "lf", "\n"],
       'yajl with CRLF' => ['yajl', "crlf", "\r\n"]
      )
  def test_format(data)
    parser, newline_conf, newline = data
    d = create_driver('json_parser' => parser, 'newline' => newline_conf)
    formatted = d.instance.format(tag, @time, record)

    assert_equal("#{JSON.generate(record)}#{newline}", formatted)
  end

  data('oj' => 'oj', 'yajl' => 'yajl')
  def test_format_without_nl(data)
    d = create_driver('json_parser' => data, 'add_newline' => false)
    formatted = d.instance.format(tag, @time, record)

    assert_equal(JSON.generate(record), formatted)
  end

  data('oj' => 'oj', 'yajl' => 'yajl')
  def test_format_with_symbolic_record(data)
    d = create_driver('json_parser' => data)
    formatted = d.instance.format(tag, @time, symbolic_record)

    assert_equal("#{JSON.generate(record)}#{@default_newline}", formatted)
  end
end