Back to Repositories

Testing SingleValueFormatter Plugin Implementation in Fluentd

This test suite validates the SingleValueFormatter plugin in Fluentd, focusing on message formatting and newline handling. The tests verify configuration parameters, message key functionality, and different newline formatting options.

Test Coverage Overview

The test suite provides comprehensive coverage of the SingleValueFormatter plugin functionality.

Key areas tested include:
  • Default configuration parameter validation
  • Custom message key configuration
  • Newline handling (LF and CRLF)
  • Format output verification

Implementation Analysis

The testing approach uses Fluent’s Test::Driver::Formatter framework to validate formatter behavior. The tests employ a structured pattern of creating driver instances with different configurations and asserting expected formatting outcomes.

Technical implementation includes:
  • Driver configuration testing
  • Parameterized tests for newline variants
  • Message key customization verification

Technical Details

Testing tools and setup:
  • Test::Unit framework
  • Fluent::Test::Driver::Formatter test driver
  • SingleValueFormatter plugin
  • Custom configuration parameters
  • Event time handling

Best Practices Demonstrated

The test suite demonstrates several testing best practices in Ruby.

Notable practices include:
  • Isolated test cases with clear assertions
  • Data-driven testing for different newline configurations
  • Helper method implementation for driver creation
  • Comprehensive edge case coverage

fluent/fluentd

test/plugin/test_formatter_single_value.rb

            
require_relative '../helper'
require 'fluent/test/driver/formatter'
require 'fluent/plugin/formatter_single_value'

class SingleValueFormatterTest < ::Test::Unit::TestCase
  def create_driver(conf = "")
    Fluent::Test::Driver::Formatter.new(Fluent::Plugin::SingleValueFormatter).configure(conf)
  end

  def test_config_params
    d = create_driver
    assert_equal "message", d.instance.message_key
  end

  def test_config_params_message_key
    d = create_driver('message_key' => 'foobar')
    assert_equal "foobar", d.instance.message_key
  end

  data("newline (LF)" => ["lf", "\n"],
       "newline (CRLF)" => ["crlf", "\r\n"])
  def test_format(data)
    newline_conf, newline = data
    d = create_driver('newline' => newline_conf)
    formatted = d.instance.format('tag', event_time, {'message' => 'awesome'})
    assert_equal("awesome#{newline}", formatted)
  end

  def test_format_without_newline
    d = create_driver('add_newline' => 'false')
    formatted = d.instance.format('tag', event_time, {'message' => 'awesome'})
    assert_equal("awesome", formatted)
  end

  data("newline (LF)" => ["lf", "\n"],
       "newline (CRLF)" => ["crlf", "\r\n"])
  def test_format_with_message_key(data)
    newline_conf, newline = data
    d = create_driver('message_key' => 'foobar', 'newline' => newline_conf)
    formatted = d.instance.format('tag', event_time, {'foobar' => 'foo'})

    assert_equal("foo#{newline}", formatted)
  end
end