Back to Repositories

Testing LTSV Formatter Implementation in Fluentd

This test suite validates the LabeledTSV Formatter functionality in Fluentd, focusing on proper formatting of log data into LTSV (Labeled Tab-Separated Values) format. The tests ensure correct delimiter handling, newline configurations, and label formatting across different scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the LabeledTSV Formatter’s core functionality.

  • Configuration parameter validation for delimiters and newline settings
  • Format verification with different newline types (LF, CRLF)
  • Custom delimiter handling
  • Edge cases for newline omission

Implementation Analysis

The testing approach utilizes Fluentd’s Test::Driver::Formatter framework to validate formatter behavior.

Tests are structured using Ruby’s Test::Unit framework with data-driven test cases for newline variations. The implementation follows a setup-execute-assert pattern with clear separation of configuration and formatting tests.

Technical Details

  • Testing Framework: Test::Unit
  • Fluentd Test Driver: Fluent::Test::Driver::Formatter
  • Test Setup: Configurable formatter instance with customizable delimiters
  • Test Data: Sample records with message and greeting fields

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby.

  • Isolated test setup using setup method
  • Data-driven testing using Test::Unit’s data method
  • Comprehensive configuration testing
  • Clear test case organization and naming
  • Explicit assertion messages

fluent/fluentd

test/plugin/test_formatter_ltsv.rb

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

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

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

  def tag
    "tag"
  end

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

  def test_config_params
    d = create_driver
    assert_equal "\t", d.instance.delimiter
    assert_equal  ":", d.instance.label_delimiter
    assert_equal  true, d.instance.add_newline

    d = create_driver(
      'delimiter'       => ',',
      'label_delimiter' => '=',
      'add_newline' => false,
    )

    assert_equal ",", d.instance.delimiter
    assert_equal "=", d.instance.label_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({"newline" => newline_conf})
    formatted = d.instance.format(tag, @time, record)

    assert_equal("message:awesome\tgreeting:hello#{newline}", formatted)
  end

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

    assert_equal("message:awesome\tgreeting:hello", 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(
      'delimiter'       => ',',
      'label_delimiter' => '=',
      'newline' => newline_conf,
    )
    formatted = d.instance.format(tag, @time, record)

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