rsyslog/tests/rscript_split-vg.sh
20syldev deb9119e99
rainerscript: add split() function
Users need to parse delimited strings (CSV, tags, paths) into arrays
for iteration or JSON output without external processing.

Impact: New RainerScript function available to all users.

Before: No native way to split strings into arrays in RainerScript.
After: split(string, separator) returns a JSON array of substrings.

Technical overview:
Implements doFunct_split() in grammar/rainerscript.c
Registers "split" in scriptFunct table with 2 required args
Adds CNFFUNC_SPLIT enum in rainerscript.h
Uses unified strstr-based iteration for all separator lengths
Handles edge cases: empty input, leading/trailing/consecutive delimiters
Includes error handling for json-c memory allocation failures
Returns empty JSON array on null/empty input or separator
Includes documentation (rs-split.rst) and test scripts
2025-12-24 14:03:34 +01:00

71 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# Added 2025-12-19 by 20syldev, released under ASL 2.0
export USE_VALGRIND="YES"
. ${srcdir:=.}/diag.sh init
generate_conf
add_conf '
module(load="../plugins/imtcp/.libs/imtcp")
input(type="imtcp" port="0" listenPortFileName="'$RSYSLOG_DYNNAME'.tcpflood_port")
template(name="outfmt" type="string" string="%$!result%\n")
if $msg contains "msgnum:00000000" then {
# Basic case: normal split with multi-char separator
set $!input = "abc@example.com, def@example.com, ghi@example.com";
set $!result = split($!input, ", ");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
if $msg contains "msgnum:00000001" then {
# Edge case: empty input string
set $!result = split("", ",");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
if $msg contains "msgnum:00000002" then {
# Edge case: trailing separator
set $!result = split("a,b,", ",");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
if $msg contains "msgnum:00000003" then {
# Edge case: leading separator
set $!result = split(",a,b", ",");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
if $msg contains "msgnum:00000004" then {
# Edge case: multiple separators together (empty field)
set $!result = split("a,,b", ",");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
if $msg contains "msgnum:00000005" then {
# Edge case: input string is just the separator
set $!result = split(",", ",");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
if $msg contains "msgnum:00000006" then {
# Edge case: input string with no separators
set $!result = split("abc", ",");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
if $msg contains "msgnum:00000007" then {
# Edge case: empty separator string (returns empty array)
set $!result = split("abc", "");
action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
}
'
startup_vg
tcpflood -m8
shutdown_when_empty
wait_shutdown_vg
check_exit_vg
# Verify all test cases
export EXPECTED='[ "abc@example.com", "def@example.com", "ghi@example.com" ]
[ "" ]
[ "a", "b", "" ]
[ "", "a", "b" ]
[ "a", "", "b" ]
[ "", "" ]
[ "abc" ]
[ ]'
cmp_exact $RSYSLOG_OUT_LOG
exit_test