Back to Repositories

Testing Plugin Ownership Inheritance in Fluentd

This test suite validates the OwnedByMixin functionality in Fluentd, ensuring proper inheritance of plugin properties between parent and child plugins. It focuses on verifying plugin ID and logger inheritance mechanisms.

Test Coverage Overview

The test suite covers essential ownership relationships between Fluentd plugins, specifically focusing on parent-child plugin inheritance patterns.

Key areas tested include:
  • Plugin ID inheritance from parent to child
  • Logger configuration inheritance
  • Object ownership verification
  • Plugin configuration management

Implementation Analysis

The testing approach uses Test::Unit framework with sub-test cases to isolate specific functionality testing. It implements a dummy parent-child plugin structure to validate the OwnedByMixin behavior in a controlled environment.

Technical patterns include:
  • Mock plugin class creation
  • Plugin registration verification
  • Configuration element testing
  • Object reference validation

Technical Details

Testing infrastructure includes:
  • Test::Unit as the primary testing framework
  • Fluent::Test setup for environment initialization
  • Custom dummy plugin classes for isolation testing
  • Configuration element creation for plugin setup
  • Object ID comparison for ownership validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby plugin development.

Notable practices include:
  • Proper test isolation using sub_test_case
  • Clear setup and teardown management
  • Explicit assertion statements
  • Comprehensive plugin property validation
  • Effective use of Ruby’s module system for testing inheritance

fluent/fluentd

test/plugin/test_owned_by.rb

            
require_relative '../helper'
require 'fluent/plugin/base'
require 'fluent/plugin/input'
require 'fluent/plugin/owned_by_mixin'

module OwnedByMixinTestEnv
  class DummyParent < Fluent::Plugin::Input
    Fluent::Plugin.register_input('dummy_parent', self)
  end
  class DummyChild < Fluent::Plugin::Base
    include Fluent::Plugin::OwnedByMixin
    Fluent::Plugin.register_parser('dummy_child', self)
  end
end

class OwnedByMixinTest < Test::Unit::TestCase
  sub_test_case 'Owned plugins' do
    setup do
      Fluent::Test.setup
    end

    test 'inherits plugin id and logger from parent' do
      parent = Fluent::Plugin.new_input('dummy_parent')
      parent.configure(config_element('ROOT', '', {'@id' => 'my_parent_id', '@log_level' => 'trace'}))
      child = Fluent::Plugin.new_parser('dummy_child', parent: parent)

      assert_equal parent.object_id, child.owner.object_id

      assert_equal 'my_parent_id', child.instance_eval{ @_plugin_id }

      assert_equal Fluent::Log::LEVEL_TRACE, child.log.level
    end
  end
end