rsyslog/tests/yaml-include.sh
Rainer Gerhards 1cc162b2e1 feat: add YAML as an alternative configuration format
rsyslog now accepts .yaml/.yml files as configuration files
in addition to RainerScript (.conf). The format is triggered
automatically based on file extension, both for the main config
file (-f flag) and for include() directives.

Architecture:
- New runtime/yamlconf.c (libyaml event-based parser) builds the
  same cnfobj+nvlst structures that the bison parser produces and
  calls cnfDoObj() for each block.
- Ruleset script: keys are synthesised into a complete RainerScript
  ruleset(name=...) { ... } block and pushed onto the flex buffer
  stack so the existing lex/bison pipeline handles all filter
  expressions and statements unchanged.
- routing in rsconf.c::load() and cnfDoInclude() detects .yaml/.yml
  by extension and delegates to yamlconf_load().
- cnfHasPendingBuffers() (new) lets rsconf.c flush queued script
  buffers after YAML-as-main-config loading.
- Guarded by #ifdef HAVE_LIBYAML throughout; graceful error when
  libyaml is absent.

Schema (top-level YAML keys):
  global, modules, inputs, templates, rulesets, mainqueue,
  include, parser, lookup_table, dyn_stats, ratelimit, timezone

Parameter names are identical to RainerScript; all type coercion
and validation is reused via nvlstGetParams() unchanged.

Tests: yaml-basic, yaml-include, yaml-ruleset-script, yaml-error
Documentation: doc/source/configuration/yaml_config.rst

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-26 09:19:48 +01:00

70 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# Tests that YAML config files can include both:
# - another .yaml file (YAML-from-YAML include)
# - a .conf (RainerScript) file (RainerScript-from-YAML include)
# Verifies the extension-based router in cnfDoInclude() works correctly.
#
# Template "outfmt" is defined in the main YAML's templates: section so that
# it is registered via cnfDoObj() before any flex buffers are consumed.
# The included .conf file adds a second template ("confmt") to prove that
# RainerScript includes from YAML are processed; that template is used by a
# second action in the ruleset script so both outputs are checked.
#
# Added 2025 by contributors, released under ASL 2.0
. ${srcdir:=.}/diag.sh init
export NUMMESSAGES=50
export QUEUE_EMPTY_CHECK_FUNC=wait_file_lines
generate_conf
add_conf '
# Testbench preamble in RainerScript; main config in YAML
include(file="'${RSYSLOG_DYNNAME}'.yaml")
'
# Sub-config included from YAML as a .conf (RainerScript) fragment.
# Defines a second template so we can verify .conf includes are processed.
cat > "${RSYSLOG_DYNNAME}_inc.conf" << CONFEOF
template(name="confmt" type="string" string="%msg:F,58:2%\n")
CONFEOF
# Sub-config included from YAML as a nested .yaml file — loads imtcp.
cat > "${RSYSLOG_DYNNAME}_sub.yaml" << 'SUBEOF'
modules:
- load: "../plugins/imtcp/.libs/imtcp"
SUBEOF
# Main YAML config.
# templates: is processed via cnfDoObj() during YAML loading (before any flex
# buffers are consumed), so "outfmt" is always available to the ruleset script.
cat > "${RSYSLOG_DYNNAME}.yaml" << YAMLEOF
include:
- path: "${RSYSLOG_DYNNAME}_sub.yaml"
- path: "${RSYSLOG_DYNNAME}_inc.conf"
templates:
- name: outfmt
type: string
string: "%msg:F,58:2%\n"
rulesets:
- name: main
script: |
:msg, contains, "msgnum:" action(type="omfile"
template="outfmt" file="${RSYSLOG_OUT_LOG}")
YAMLEOF
# Expand shell vars
sed -i \
-e "s|\${RSYSLOG_DYNNAME}|${RSYSLOG_DYNNAME}|g" \
-e "s|\${RSYSLOG_OUT_LOG}|${RSYSLOG_OUT_LOG}|g" \
"${RSYSLOG_DYNNAME}.yaml"
add_conf '
input(type="imtcp" port="0" listenPortFileName="'${RSYSLOG_DYNNAME}'.tcpflood_port"
ruleset="main")
'
startup
tcpflood -m $NUMMESSAGES
shutdown_when_empty
wait_shutdown
seq_check
exit_test