Back to Repositories

Testing TSV Formatter Configuration and Formatting Workflow in Fluentd

This test suite validates the TSV (Tab-Separated Values) formatter functionality in Fluentd, ensuring proper handling of data formatting with configurable delimiters and newline characters. The tests verify both basic and customized formatting configurations for log event data.

Test Coverage Overview

The test suite provides comprehensive coverage of the TSV formatter plugin’s functionality.

Key areas tested include:
  • Configuration parameter validation
  • Default delimiter behavior
  • Custom delimiter handling
  • Newline character options (LF and CRLF)
  • Optional newline addition

Implementation Analysis

The testing approach utilizes Fluent’s test driver framework to validate formatter behavior. The implementation employs a modular structure with separate test cases for configuration and formatting scenarios.

Notable patterns include:
  • Test driver initialization with configurable parameters
  • Data-driven testing for newline variants
  • Isolated test cases for specific formatting features

Technical Details

Testing tools and setup:
  • Fluent::Test::Driver::Formatter for formatter testing
  • Test::Unit framework for assertions
  • Helper methods for event time and record generation
  • Configuration-based test initialization

Best Practices Demonstrated

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

Notable practices include:
  • Separate setup method for test initialization
  • Helper methods for common operations
  • Comprehensive configuration testing
  • Edge case handling for different newline types
  • Clear test case organization and naming

fluent/fluentd

test/plugin/test_formatter_tsv.rb

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

class TSVFormatterTest < ::Test::Unit::TestCase
  def setup
    @time = event_time
  end

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

  def tag
    "tag"
  end

  def record
    {'message' => 'awesome', 'greeting' => 'hello'}
  end

  def test_config_params
    d = create_driver(
      'keys' => 'message,greeting',
    )
    assert_equal ["message", "greeting"], d.instance.keys
    assert_equal "\t", d.instance.delimiter
    assert_equal true, d.instance.add_newline

    d = create_driver(
      'keys' => 'message,greeting',
      'delimiter' => ',',
      'add_newline' => false,
    )
    assert_equal ["message", "greeting"], d.instance.keys
    assert_equal ",", d.instance.delimiter
    assert_equal false, d.instance.add_newline
  end

  data("newline (LF)" => ["lf", "\n"],
       "newline (CRLF)" => ["crlf", "\r\n"])
  def test_format(data)
    newline_conf, newline = data
    d = create_driver(
      'keys' => 'message,greeting',
      'newline' => newline_conf
    )
    formatted = d.instance.format(tag, @time, record)

    assert_equal("awesome\thello#{newline}", formatted)
  end

  def test_format_without_newline
    d = create_driver(
      'keys' => 'message,greeting',
      'add_newline' => false,
    )
    formatted = d.instance.format(tag, @time, record)

    assert_equal("awesome\thello", formatted)
  end

  data("newline (LF)" => ["lf", "\n"],
       "newline (CRLF)" => ["crlf", "\r\n"])
  def test_format_with_customized_delimiters(data)
    newline_conf, newline = data
    d = create_driver(
      'keys' => 'message,greeting',
      'delimiter' => ',',
      'newline' => newline_conf,
    )
    formatted = d.instance.format(tag, @time, record)

    assert_equal("awesome,hello#{newline}", formatted)
  end
end