Back to Repositories

Testing Boolean Parameter Configuration Implementation in Fluentd

This test suite validates the configuration handling of boolean parameters in Fluentd plugins, focusing on default values and multi-section configurations. It ensures proper parsing and initialization of boolean flags both at the root level and within nested child sections.

Test Coverage Overview

The test suite provides comprehensive coverage of boolean parameter configuration in Fluentd plugins:

  • Validates default boolean parameter values at root level
  • Tests multi-section child configurations with boolean parameters
  • Verifies parameter parsing with and without explicit values
  • Covers configuration scenarios with comments and whitespace

Implementation Analysis

The testing approach utilizes Fluentd’s Test::Driver::Input framework to validate plugin configuration behavior. It implements a structured pattern for testing boolean parameters through:

  • Mock plugin class definition with config_param declarations
  • Configuration string parsing validation
  • Nested config_section testing with multi-parameter verification

Technical Details

  • Testing Framework: Test::Unit
  • Fluentd Test Driver: Fluent::Test::Driver::Input
  • Configuration Method: config_param and config_section
  • Test Environment: Ruby-based unit testing

Best Practices Demonstrated

The test implementation showcases several testing best practices for plugin configuration:

  • Comprehensive parameter state verification
  • Structured test case organization
  • Clear separation of configuration and assertions
  • Effective use of helper methods and test drivers

fluent/fluentd

test/config/test_plugin_configuration.rb

            
require_relative '../helper'
require 'fluent/plugin/input'
require 'fluent/test/driver/input'

module ConfigurationForPlugins
  class AllBooleanParams < Fluent::Plugin::Input
    config_param :flag1, :bool, default: true
    config_param :flag2, :bool, default: true
    config_param :flag3, :bool, default: false
    config_param :flag4, :bool, default: false

    config_section :child, param_name: :children, multi: true, required: true do
      config_param :flag1, :bool, default: true
      config_param :flag2, :bool, default: true
      config_param :flag3, :bool, default: false
      config_param :flag4, :bool, default: false
    end
  end

  class BooleanParamsWithoutValue < ::Test::Unit::TestCase
    CONFIG = <<CONFIG
    flag1 
    flag2 # yaaaaaaaaaay
    flag3 
    flag4 # yaaaaaaaaaay
    <child>
      flag1
      flag2 # yaaaaaaaaaay
      flag3
      flag4 # yaaaaaaaaaay
    </child>
    <child>
      flag1 # yaaaaaaaaaay
      flag2
      flag3 # yaaaaaaaaaay
      flag4
    </child>
    # with following whitespace
    <child>
      flag1 
      flag2 
      flag3 
      flag4 
    </child>
CONFIG

    test 'create plugin via driver' do
      d = Fluent::Test::Driver::Input.new(AllBooleanParams)
      d.configure(CONFIG)
      assert_equal([true] * 4, [d.instance.flag1, d.instance.flag2, d.instance.flag3, d.instance.flag4])
      num_of_sections = 3
      assert_equal num_of_sections, d.instance.children.size
      assert_equal([true] * (num_of_sections * 4), d.instance.children.map{|c| [c.flag1, c.flag2, c.flag3, c.flag4]}.flatten)
    end
  end
end