Back to Repositories

Testing Apache Log Parser Implementation in Fluentd

This test suite validates the Apache log parser functionality in Fluentd, ensuring accurate parsing of Apache access log formats. The tests verify timestamp parsing, HTTP request details extraction, and proper record field mapping.

Test Coverage Overview

The test suite provides comprehensive coverage of Apache log parsing functionality in Fluentd.

Key areas tested include:
  • Standard Apache log format parsing
  • Timestamp extraction and formatting
  • HTTP request component parsing
  • Record field mapping validation
  • Time key preservation options

Implementation Analysis

The testing approach uses Fluentd’s parser driver framework to validate log parsing capabilities.

Implementation features:
  • Test driver configuration for parser initialization
  • Method call verification for both parse and call interfaces
  • Custom configuration testing for time format handling
  • Record field validation for parsed log components

Technical Details

Testing infrastructure includes:
  • Fluentd Test::Unit framework integration
  • Parser driver implementation
  • Apache parser plugin configuration
  • Time format configuration options
  • Test setup and driver initialization patterns

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test setup and configuration
  • Comprehensive assertion coverage
  • Multiple interface testing (parse/call methods)
  • Configuration validation
  • Time handling verification

fluent/fluentd

test/plugin/test_parser_apache.rb

            
require_relative '../helper'
require 'fluent/test/driver/parser'
require 'fluent/plugin/parser_apache'

class ApacheParserTest < ::Test::Unit::TestCase
  def setup
    Fluent::Test.setup
  end

  def create_driver(conf = {})
    Fluent::Test::Driver::Parser.new(Fluent::Plugin::ApacheParser.new).configure(conf)
  end

  data('parse' => :parse, 'call' => :call)
  def test_call(method_name)
    d = create_driver
    m = d.instance.method(method_name)
    m.call('192.168.0.1 - - [28/Feb/2013:12:00:00 +0900] "GET / HTTP/1.1" 200 777') { |time, record|
      assert_equal(event_time('28/Feb/2013:12:00:00 +0900', format: '%d/%b/%Y:%H:%M:%S %z'), time)
      assert_equal({
                     'user'    => '-',
                     'method'  => 'GET',
                     'code'    => '200',
                     'size'    => '777',
                     'host'    => '192.168.0.1',
                     'path'    => '/'
                   }, record)
    }
  end

  def test_parse_with_keep_time_key
    conf = {
      'time_format' => "%d/%b/%Y:%H:%M:%S %z",
      'keep_time_key' => 'true',
    }
    d = create_driver(conf)
    text = '192.168.0.1 - - [28/Feb/2013:12:00:00 +0900] "GET / HTTP/1.1" 200 777'
    d.instance.parse(text) do |_time, record|
      assert_equal "28/Feb/2013:12:00:00 +0900", record['time']
    end
  end
end