src/collectors/log2journal/tests.d/README.md
This directory contains the comprehensive test suite for log2journal YAML parsing and functionality.
The test framework supports multiple test file formats for different testing scenarios:
{testname}.yaml - YAML configuration file (optional if using internal config){testname}.input - Input log lines to process (optional if testing with empty input){testname}.output - Expected output after processing{testname}.cmd - Command to execute with ${TESTED_LOG2JOURNAL_BIN} variable{testname}.yaml - Base configuration (optional){testname}.input - Input log lines{testname}.output - Expected output{testname}.yaml - Configuration that should fail{testname}.input - Input log lines (optional){testname}.fail - Expected error message (or empty for any failure){testname}.yaml - Configuration file{testname}-final-config.yaml - Expected --show-config outputThe test framework now uses clean, explicit file formats:
# Clean command files:
# inject-append.cmd
${TESTED_LOG2JOURNAL_BIN} -f inject-append.yaml --inject CLINEWKEY1=value1 --inject CLINEWKEY2=value2
# Explicit failure tests:
# error-test.fail
YAML PARSER: syntax error at line 5
./tests.sh
# Set the binary to test (e.g., local build)
export TESTED_LOG2JOURNAL_BIN="../../../build/log2journal"
./tests.sh
logic-*.yaml - 7 tests)Tests fundamental log2journal functionality:
risk-*.yaml, yaml-*.yaml - 13 tests)Tests YAML parsing edge cases:
unicode-*.yaml, encoding-*.yaml - 8 tests)Critical for log processing:
error-*.yaml - 6 tests)Tests that should fail gracefully:
advanced-*.yaml - 5 tests)Complex functionality:
Test CLI/config interaction:
Using -c flag with built-in configs:
boundary-*.yaml, edge-*.yaml - 12 tests)System limits and edge cases:
real-world-*.yaml - 5 tests)Practical log format examples:
# Create input file
echo "your test log line" > tests.d/mytest.input
# Create YAML config
cat > tests.d/mytest.yaml << EOF
pattern: 'your pattern'
# other config...
EOF
# Generate expected output
cat tests.d/mytest.input | $TESTED_LOG2JOURNAL_BIN -f tests.d/mytest.yaml > tests.d/mytest.output
# Create command file
echo '${TESTED_LOG2JOURNAL_BIN} -f mytest.yaml --inject KEY=value' > tests.d/mytest.cmd
# Create config and input
echo 'pattern: "(?P<MESSAGE>.*)"' > tests.d/mytest.yaml
echo 'test input' > tests.d/mytest.input
# Generate expected output (using your build)
cat tests.d/mytest.input | $TESTED_LOG2JOURNAL_BIN -f tests.d/mytest.yaml --inject KEY=value > tests.d/mytest.output
# Create config that should fail
echo 'invalid: yaml: syntax:' > tests.d/fail-test.yaml
# Create expected error message
echo 'YAML PARSER: syntax error' > tests.d/fail-test.fail
# Optionally create input
echo 'test input' > tests.d/fail-test.input
# Test with --show-config to verify CLI/YAML merging
echo "test" | $TESTED_LOG2JOURNAL_BIN -f tests.d/config.yaml --some-arg value --show-config | sed '1,/^$/d' > tests.d/testname-final-config.yaml
(?P<name>...)json: Parse JSON formatted logslogfmt: Parse logfmt formatted logs${VARIABLE}: Replaced with variable value${undefined}: Replaced with empty stringinject: All values are added (allows duplicates)rewrite: All rules processed in order (pipeline)rename: All renames attemptedResults stored in /tmp/log2journal_test_results/:
{testname}.out - Actual output{testname}.err - Error output{testname}.diff - Difference from expected{testname}-config.yaml - Actual config from --show-configThe test framework includes built-in debugging capabilities:
Show exact commands and full diff output:
./tests.sh --verbose --test {test-name}
Test only one specific case:
./tests.sh --test {test-name}
.yaml - Configuration file.input - Input log lines.output - Expected output.cmd - Custom command (overrides default).fail - Expected error message (for failure tests)export TESTED_LOG2JOURNAL_BIN="/path/to/your/log2journal"
cat tests.d/{test}.input | $TESTED_LOG2JOURNAL_BIN -f tests.d/{test}.yaml
cat tests.d/{test}.input | $TESTED_LOG2JOURNAL_BIN -f tests.d/{test}.yaml > tests.d/{test}.output
TESTED_LOG2JOURNAL_BIN to correct path.cmd file for correct syntaxFor tests that include version information (like error-* tests):
v0.0.0-0-g00000000)TESTED_LOG2JOURNAL_BIN - Path to log2journal binary for testing (default: log2journal from PATH)tests.sh to test different buildsThe test framework provides comprehensive coverage of log2journal functionality with clean, maintainable test definitions.