Back to Repositories

Testing LocalMetrics Plugin Implementation in Fluentd

This test suite validates the LocalMetrics plugin functionality in Fluentd, focusing on counter and gauge metric operations. It ensures proper configuration and metric manipulation for monitoring and measurement capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of LocalMetrics functionality:
  • Counter mode configuration and operations
  • Gauge mode configuration and operations
  • Label management and default settings
  • Edge cases for metric value manipulation

Implementation Analysis

The testing approach uses Test::Unit with sub-test cases to organize related test scenarios. It implements setup methods for test isolation and employs assertion patterns specific to Ruby testing conventions.
  • Separate test groups for counter and gauge modes
  • Configuration validation tests
  • Comprehensive operation testing

Technical Details

Testing infrastructure includes:
  • Test::Unit framework
  • Fluent plugin helper modules
  • System configuration utilities
  • Socket hostname integration
  • Configuration element builders

Best Practices Demonstrated

The test suite exemplifies quality testing practices:
  • Isolated test cases with proper setup
  • Comprehensive positive and negative testing
  • Clear test method naming
  • Proper assertion usage
  • Organized test structure with logical grouping

fluent/fluentd

test/plugin/test_metrics_local.rb

            
require_relative '../helper'
require 'fluent/plugin/metrics_local'
require 'fluent/system_config'

class LocalMetricsTest < ::Test::Unit::TestCase
  sub_test_case 'configure' do
    test "configured for counter mode" do
      m = Fluent::Plugin::LocalMetrics.new
      m.configure(config_element('metrics', '', {"labels" => {test: "test-unit", language: "Ruby"}}))

      assert_false m.use_gauge_metric
      assert_equal({agent: "Fluentd", hostname: "#{Socket.gethostname}"}, m.default_labels)
      assert_equal({test: "test-unit", language: "Ruby"}, m.labels)
      assert_true m.has_methods_for_counter
      assert_false m.has_methods_for_gauge
    end

    test "configured for gauge mode" do
      m = Fluent::Plugin::LocalMetrics.new
      m.use_gauge_metric = true
      m.configure(config_element('metrics', '', {"labels" => {test: "test-unit", language: "Ruby"}}))

      assert_true m.use_gauge_metric
      assert_equal({agent: "Fluentd", hostname: "#{Socket.gethostname}"}, m.default_labels)
      assert_equal({test: "test-unit", language: "Ruby"}, m.labels)
      assert_false m.has_methods_for_counter
      assert_true m.has_methods_for_gauge
    end
  end

  sub_test_case 'LocalMetric' do
    sub_test_case "counter" do
      setup do
        @m = Fluent::Plugin::LocalMetrics.new
        @m.configure(config_element('metrics', '', {}))
      end

      test '#configure' do
        assert_true @m.has_methods_for_counter
        assert_false @m.has_methods_for_gauge
      end

      test 'all local counter operations work well' do
        assert_equal 0, @m.get
        assert_equal 1, @m.inc

        @m.add(20)
        assert_equal 21, @m.get
        assert_raise NotImplementedError do
          @m.dec
        end

        @m.set(100)
        assert_equal 100, @m.get

        @m.set(10)
        assert_equal 100, @m.get # On counter, value should be overwritten bigger than stored one.
        assert_raise NotImplementedError do
          @m.sub(11)
        end
      end
    end

    sub_test_case "gauge" do
      setup do
        @m = Fluent::Plugin::LocalMetrics.new
        @m.use_gauge_metric = true
        @m.configure(config_element('metrics', '', {}))
      end

      test '#configure' do
        assert_false @m.has_methods_for_counter
        assert_true @m.has_methods_for_gauge
      end

      test 'all local gauge operations work well' do
        assert_equal 0, @m.get
        assert_equal 1, @m.inc

        @m.add(20)
        assert_equal 21, @m.get
        @m.dec
        assert_equal 20, @m.get

        @m.set(100)
        assert_equal 100, @m.get

        @m.sub(11)
        assert_equal 89, @m.get

        @m.set(10)
        assert_equal 10, @m.get # On gauge, value always should be overwritten.
      end
    end
  end
end