2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-22 02:12:16 +00:00
Files
xcat-core/xCAT-test/bats/ipv6_syslog_forwarding.bats
T
Daniel Hilst 2371e2a98e fix(xcat-core): a syslog target with a port is bracketed as if it were an IPv6 literal
site.master may carry a port. format_syslog_forward_target in
xCAT/postscripts/syslog bracketed any value containing a colon, so
MASTER=127.0.0.1:1515 rendered the rsyslog line as @[127.0.0.1:1515], which
rsyslog does not accept, and syslog_ng_udp_driver chose udp6 for an IPv4 peer.
format_syslog_server in the dracut_105 rsyslogd-start.sh carries its own copy of
the rule and had the same defect; it runs inside the initramfs and cannot source
the postscript, so the rule is held down twice on purpose.

An IPv4 address or a hostname with a port has exactly one colon and an IPv6
literal has at least two, so the test is now on two colons rather than one. A
value already starting with "[" is left alone whether or not a port follows it,
which the old pattern missed: it required a trailing "]" and so double-bracketed
[2001:db8::1]:514.

ipv6_syslog_forwarding.bats gains four cases. Without the fix, "a target with a
port is not mistaken for an IPv6 literal", "an already-bracketed target with a
port is left alone" and "the initrd launcher does not mistake a port for an IPv6
literal" all fail; the compressed-literal case passes either way and is there to
show the bracketing of ::1 did not regress.

Also: grub2_ipv6.t could not load grub2.pm once xcat-core/#7980 made it use
xCAT::DHCP::BootPolicy, which lives in perl-xCAT. The file died at compile time
and reported no plan at all, which in a prove summary reads as a count
discrepancy rather than a failure. It now puts perl-xCAT and the server perl
directory on @INC, as every other plugin test does.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
2026-10-26 15:27:21 -03:00

129 lines
5.0 KiB
Bash

#!/usr/bin/env bats
#
# rsyslog and syslog-ng both take a forwarding target after "@", and neither
# parses a bare IPv6 literal there: rsyslog needs the address in brackets, and
# syslog-ng needs the udp6() driver instead of udp(). The syslog postscript and
# the initrd rsyslog launcher each carry a formatter for this.
load 'helpers/shell_source'
load_syslog_helpers()
{
local file lib
file="$(require_repo_file 'xCAT/postscripts/syslog')"
lib="${BATS_TEST_TMPDIR}/syslog.sh"
{ extract_shell_function "$file" 'format_syslog_forward_target'
extract_shell_function "$file" 'syslog_ng_udp_driver'; } >"$lib" || return 1
# shellcheck disable=SC1090
. "$lib"
}
@test "an IPv6 forwarding target is bracketed for rsyslog" {
load_syslog_helpers
[ "$(format_syslog_forward_target '2001:db8::1')" = '[2001:db8::1]' ]
}
@test "an IPv4 target and a hostname are passed through unchanged" {
load_syslog_helpers
[ "$(format_syslog_forward_target '192.0.2.10')" = '192.0.2.10' ]
[ "$(format_syslog_forward_target 'mn01')" = 'mn01' ]
}
# A COLON DOES NOT MEAN IPv6. site.master may carry a port -- xcat-core/#7975 sets
# MASTER=127.0.0.1:1515 for its own forwarding case -- and an IPv4 or hostname target with a port
# has exactly one colon, while an IPv6 literal has at least two. Bracketing on any colon rendered
# @[127.0.0.1:1515], which rsyslog does not accept, and sent syslog-ng to udp6 for an IPv4 peer.
@test "a target with a port is not mistaken for an IPv6 literal" {
load_syslog_helpers
[ "$(format_syslog_forward_target '127.0.0.1:1515')" = '127.0.0.1:1515' ]
[ "$(format_syslog_forward_target '192.0.2.10:514')" = '192.0.2.10:514' ]
[ "$(format_syslog_forward_target 'mn01:514')" = 'mn01:514' ]
[ "$(syslog_ng_udp_driver '127.0.0.1:1515')" = 'udp' ]
[ "$(syslog_ng_udp_driver 'mn01:514')" = 'udp' ]
}
@test "a compressed IPv6 literal is still bracketed and still udp6" {
load_syslog_helpers
[ "$(format_syslog_forward_target '::1')" = '[::1]' ]
[ "$(syslog_ng_udp_driver '::1')" = 'udp6' ]
[ "$(syslog_ng_udp_driver '2001:db8::1')" = 'udp6' ]
}
@test "an already-bracketed target with a port is left alone" {
load_syslog_helpers
[ "$(format_syslog_forward_target '[2001:db8::1]:514')" = '[2001:db8::1]:514' ]
}
@test "an already-bracketed target is not bracketed twice" {
load_syslog_helpers
[ "$(format_syslog_forward_target '[2001:db8::1]')" = '[2001:db8::1]' ]
}
@test "syslog-ng selects udp6 for an IPv6 target and udp otherwise" {
load_syslog_helpers
[ "$(syslog_ng_udp_driver '2001:db8::1')" = 'udp6' ]
[ "$(syslog_ng_udp_driver '192.0.2.10')" = 'udp' ]
[ "$(syslog_ng_udp_driver 'mn01')" = 'udp' ]
}
@test "the initrd rsyslog launcher writes a bracketed forwarding line" {
local file lib template
file="$(require_repo_file 'xCAT-server/share/xcat/netboot/rh/dracut_105/patch/syslog/rsyslogd-start.sh')"
lib="${BATS_TEST_TMPDIR}/rsyslogd.sh"
{ extract_shell_function "$file" 'format_syslog_server'
extract_shell_function "$file" 'rsyslog_config'; } >"$lib"
# shellcheck disable=SC1090
. "$lib"
template="${BATS_TEST_TMPDIR}/rsyslog.conf"
printf '# template\n' >"$template"
run rsyslog_config '2001:db8::1' "$template" 'kern.*'
[ "$status" -eq 0 ]
[ "${lines[${#lines[@]}-1]}" = 'kern.* @[2001:db8::1]' ]
run rsyslog_config '192.0.2.10' "$template" 'kern.*'
[ "$status" -eq 0 ]
[ "${lines[${#lines[@]}-1]}" = 'kern.* @192.0.2.10' ]
}
# The initrd launcher carries its OWN copy of the rule -- it runs inside the dracut initramfs and
# cannot source the postscript -- so the port case has to be held down twice.
@test "the initrd launcher does not mistake a port for an IPv6 literal" {
local file lib template
file="$(require_repo_file 'xCAT-server/share/xcat/netboot/rh/dracut_105/patch/syslog/rsyslogd-start.sh')"
lib="${BATS_TEST_TMPDIR}/rsyslogd.sh"
{ extract_shell_function "$file" 'format_syslog_server'
extract_shell_function "$file" 'rsyslog_config'; } >"$lib"
# shellcheck disable=SC1090
. "$lib"
template="${BATS_TEST_TMPDIR}/rsyslog.conf"
printf '# template\n' >"$template"
run rsyslog_config '127.0.0.1:1515' "$template" 'kern.*'
[ "$status" -eq 0 ]
[ "${lines[${#lines[@]}-1]}" = 'kern.* @127.0.0.1:1515' ]
run rsyslog_config '::1' "$template" 'kern.*'
[ "$status" -eq 0 ]
[ "${lines[${#lines[@]}-1]}" = 'kern.* @[::1]' ]
}
@test "the initrd launcher logs locally when no server is given" {
local file lib template
file="$(require_repo_file 'xCAT-server/share/xcat/netboot/rh/dracut_105/patch/syslog/rsyslogd-start.sh')"
lib="${BATS_TEST_TMPDIR}/rsyslogd.sh"
{ extract_shell_function "$file" 'format_syslog_server'
extract_shell_function "$file" 'rsyslog_config'; } >"$lib"
# shellcheck disable=SC1090
. "$lib"
template="${BATS_TEST_TMPDIR}/rsyslog.conf"
printf '# template\n' >"$template"
run rsyslog_config '' "$template" 'kern.*'
[ "$status" -eq 0 ]
[ "${lines[${#lines[@]}-1]}" = 'kern.* /var/log/messages' ]
}