|
|
|
@@ -9,13 +9,14 @@ use FindBin;
|
|
|
|
|
use Test::More;
|
|
|
|
|
|
|
|
|
|
plan skip_all => 'requires Linux root and mount/network namespaces' unless $^O eq 'linux' && $> == 0;
|
|
|
|
|
plan skip_all => 'namespaces unavailable' if system('unshare -mn -- true >/dev/null 2>&1');
|
|
|
|
|
plan skip_all => 'requires native rsyslogd, logger, ip and ss'
|
|
|
|
|
if system('command -v rsyslogd logger ip ss >/dev/null');
|
|
|
|
|
plan skip_all => 'namespaces unavailable' if system('unshare -mnpf --mount-proc -- true >/dev/null 2>&1');
|
|
|
|
|
plan skip_all => 'requires native rsyslogd, logger, ip, ss and setpriv'
|
|
|
|
|
if system(q{for tool in rsyslogd logger ip ss setpriv; do command -v "$tool" >/dev/null || exit 1; done});
|
|
|
|
|
my $source = $ENV{XCAT_SYSLOG_SOURCE_ROOT} || abs_path("$FindBin::Bin/../..");
|
|
|
|
|
my $tmp = tempdir(DIR => '/var/tmp', CLEANUP => !$ENV{XCAT_SYSLOG_KEEP});
|
|
|
|
|
diag("fixtures: $tmp") if $ENV{XCAT_SYSLOG_KEEP};
|
|
|
|
|
make_path("$tmp/bin");
|
|
|
|
|
symlink(abs_path('/usr/bin/awk'), "$tmp/bin/awk") or die $!;
|
|
|
|
|
sub write_file {
|
|
|
|
|
my ($path, $contents) = @_;
|
|
|
|
|
open(my $fh, '>', $path) or die "$path: $!";
|
|
|
|
@@ -32,6 +33,7 @@ sub read_file {
|
|
|
|
|
for my $name (qw(syslog xcatlib.sh)) {
|
|
|
|
|
copy("$source/xCAT/postscripts/$name", "$tmp/bin/$name") or die $!;
|
|
|
|
|
}
|
|
|
|
|
write_file("$tmp/bin/native-rsyslogd", '');
|
|
|
|
|
write_file("$tmp/bin/systemctl", <<'SH');
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
printf '%s\n' "$*" >> "$XCAT_SYSLOG_FIXTURE/services.log"
|
|
|
|
@@ -41,23 +43,52 @@ if [ -s /run/rsyslog-test.pid ]; then
|
|
|
|
|
kill "$(cat /run/rsyslog-test.pid)"
|
|
|
|
|
for i in {1..50}; do [ -f /run/rsyslog-test.pid ] || break; sleep .1; done
|
|
|
|
|
fi
|
|
|
|
|
exec /usr/sbin/rsyslogd -i /run/rsyslog-test.pid -f /etc/rsyslog.conf
|
|
|
|
|
exec "$XCAT_SYSLOG_BIN/native-rsyslogd" -i /run/rsyslog-test.pid -f /etc/rsyslog.conf
|
|
|
|
|
SH
|
|
|
|
|
write_file("$tmp/bin/logger", <<'SH');
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
printf '%s\n' "$*" >> "$XCAT_SYSLOG_FIXTURE/logger.log"
|
|
|
|
|
SH
|
|
|
|
|
write_file("$tmp/bin/mktemp", <<'SH');
|
|
|
|
|
#!/bin/sh
|
|
|
|
|
printf 'allocate\n' >>"$XCAT_SYSLOG_FIXTURE/allocation-attempts"
|
|
|
|
|
[ "$XCAT_SYSLOG_FAIL" != allocation ] || exit 43
|
|
|
|
|
path=$(/usr/bin/mktemp "$@") || exit $?
|
|
|
|
|
printf '%s\n' "$path" >>"$XCAT_SYSLOG_FIXTURE/allocated"
|
|
|
|
|
printf '%s\n' "$path"
|
|
|
|
|
SH
|
|
|
|
|
write_file("$tmp/bin/rsyslogd", <<'SH');
|
|
|
|
|
#!/bin/sh
|
|
|
|
|
printf '%s\n' "$*" >>"$XCAT_SYSLOG_FIXTURE/rsyslog-calls"
|
|
|
|
|
exec "$XCAT_SYSLOG_BIN/native-rsyslogd" "$@"
|
|
|
|
|
SH
|
|
|
|
|
write_file("$tmp/bin/namespace", <<'SH');
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
set -eu
|
|
|
|
|
mount --make-rprivate /
|
|
|
|
|
# A private executable path avoids the host service's AppArmor attachment.
|
|
|
|
|
mount --bind /usr/sbin/rsyslogd "$XCAT_SYSLOG_BIN/native-rsyslogd"
|
|
|
|
|
for device in null zero random urandom; do
|
|
|
|
|
mount --bind "/dev/$device" "$XCAT_SYSLOG_FIXTURE/dev/$device"
|
|
|
|
|
done
|
|
|
|
|
mount --rbind "$XCAT_SYSLOG_FIXTURE/dev" /dev
|
|
|
|
|
mkfifo "$XCAT_SYSLOG_FIXTURE/kmsg"
|
|
|
|
|
exec {kmsg_writer}<>"$XCAT_SYSLOG_FIXTURE/kmsg"
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/kmsg" /proc/kmsg
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/etc" /etc
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/log" /var/log
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/run" /run
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/state" /var/lib/rsyslog
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/lib" /var/lib
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/spool" /var/spool
|
|
|
|
|
mount --bind "$XCAT_SYSLOG_FIXTURE/tmp" /tmp
|
|
|
|
|
ip link set lo up
|
|
|
|
|
exec setpriv --bounding-set=-sys_admin,-syslog -- "$XCAT_SYSLOG_BIN/run"
|
|
|
|
|
SH
|
|
|
|
|
write_file("$tmp/bin/run", <<'SH');
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
set -eu
|
|
|
|
|
trap 'for f in /run/*pid; do [ ! -f "$f" ] || kill "$(cat "$f")" 2>/dev/null || :; done' EXIT
|
|
|
|
|
/usr/sbin/rsyslogd -i /run/upstream.pid -f "$XCAT_SYSLOG_FIXTURE/upstream.conf"
|
|
|
|
|
"$XCAT_SYSLOG_BIN/native-rsyslogd" -i /run/upstream.pid -f "$XCAT_SYSLOG_FIXTURE/upstream.conf"
|
|
|
|
|
for pass in 1 2; do
|
|
|
|
|
set +e
|
|
|
|
|
bash "$XCAT_SYSLOG_BIN/syslog" > "$XCAT_SYSLOG_FIXTURE/postscript-$pass.log" 2>&1
|
|
|
|
@@ -66,10 +97,11 @@ for pass in 1 2; do
|
|
|
|
|
echo "$rc" > "$XCAT_SYSLOG_FIXTURE/postscript-$pass.rc"
|
|
|
|
|
cp /etc/rsyslog.conf "$XCAT_SYSLOG_FIXTURE/config-$pass"
|
|
|
|
|
cp /etc/rsyslog.d/remote.conf "$XCAT_SYSLOG_FIXTURE/remote-$pass" 2>/dev/null || :
|
|
|
|
|
find /tmp -mindepth 1 -printf '%P\n' >"$XCAT_SYSLOG_FIXTURE/temporary-$pass"
|
|
|
|
|
[ "$rc" -eq 0 ] || break
|
|
|
|
|
done
|
|
|
|
|
set +e
|
|
|
|
|
/usr/sbin/rsyslogd -N1 -f /etc/rsyslog.conf > "$XCAT_SYSLOG_FIXTURE/parser.log" 2>&1
|
|
|
|
|
"$XCAT_SYSLOG_BIN/native-rsyslogd" -N1 -f /etc/rsyslog.conf > "$XCAT_SYSLOG_FIXTURE/parser.log" 2>&1
|
|
|
|
|
echo "$?" > "$XCAT_SYSLOG_FIXTURE/parser.rc"
|
|
|
|
|
set -e
|
|
|
|
|
ss -H -lun 'sport = :514' > "$XCAT_SYSLOG_FIXTURE/udp-514"
|
|
|
|
@@ -92,15 +124,15 @@ for i in {1..30}; do
|
|
|
|
|
sleep .1
|
|
|
|
|
done
|
|
|
|
|
SH
|
|
|
|
|
chmod 0755, "$tmp/bin/$_" for qw(systemctl logger namespace);
|
|
|
|
|
chmod 0755, "$tmp/bin/$_" for qw(systemctl logger mktemp rsyslogd namespace run);
|
|
|
|
|
my $default = read_file('/etc/rsyslog.conf');
|
|
|
|
|
my $minimal = "global(workDirectory=\"/var/lib/rsyslog\")\ninclude(file=\"/etc/rsyslog.d/*.conf\" mode=\"optional\")\n*.info /var/log/messages\n";
|
|
|
|
|
my $modern = "module(\n load=\"imudp\"\n)\ninput(\n address=\"127.0.0.1\"\n port=\"514\"\n type=\"imudp\"\n)\nmodule(load=\"imtcp\")\ninput(type=\"imtcp\" address=\"127.0.0.1\" port=\"514\")\n";
|
|
|
|
|
my $legacy = "\$ModLoad imudp\n\$UDPServerRun 514\n\$ModLoad imtcp\n\$InputTCPServerRun 514\n";
|
|
|
|
|
my @cases = (
|
|
|
|
|
['native default MN', 'mn', 'openeuler24.03', $default],
|
|
|
|
|
['native default SN local', 'snlocal', 'openeuler24.03', $default],
|
|
|
|
|
['native default SN forwarding', 'snforward', 'openeuler24.03', $default],
|
|
|
|
|
['native default MN', 'mn', 'openeuler24.03', $default, '', '', 1],
|
|
|
|
|
['native default SN local', 'snlocal', 'openeuler24.03', $default, '', '', 1],
|
|
|
|
|
['native default SN forwarding', 'snforward', 'openeuler24.03', $default, '', '', 1],
|
|
|
|
|
['native CN forwarding', 'cn', 'openeuler24.03', $minimal],
|
|
|
|
|
['native os-release fallback', 'mn', '', $minimal],
|
|
|
|
|
['native nested modern admin', 'mn', 'openeuler24.03', $minimal, $modern],
|
|
|
|
@@ -114,14 +146,29 @@ my @cases = (
|
|
|
|
|
['legacy commented examples', 'mn', 'rhels9.6', $minimal . join('', map { "#$_\n" } split /\n/, $legacy)],
|
|
|
|
|
['legacy no examples', 'mn', 'rhels9.6', $minimal],
|
|
|
|
|
['native invalid configuration', 'mn', 'openeuler24.03', $minimal . "invalid_rsyslog_directive()\n", '', 'parser'],
|
|
|
|
|
['native allocation failure', 'mn', 'openeuler24.03', $minimal, '', 'allocation'],
|
|
|
|
|
['native restart failure', 'mn', 'openeuler24.03', $minimal, '', 'restart'],
|
|
|
|
|
);
|
|
|
|
|
my $number = 0;
|
|
|
|
|
for my $case (@cases) {
|
|
|
|
|
my ($name, $role, $osver, $config, $admin, $failure) = @$case;
|
|
|
|
|
my ($name, $role, $osver, $config, $admin, $failure, $native_defaults) = @$case;
|
|
|
|
|
$admin //= ''; $failure //= '';
|
|
|
|
|
my $fixture = "$tmp/" . ++$number;
|
|
|
|
|
make_path(map { "$fixture/$_" } qw(etc/rsyslog.d etc/admin log run state));
|
|
|
|
|
make_path(map { "$fixture/$_" } qw(dev etc/rsyslog.d etc/admin log run lib/rsyslog spool/rsyslog tmp));
|
|
|
|
|
write_file("$fixture/dev/$_", '') for qw(null zero random urandom);
|
|
|
|
|
for my $file (qw(passwd group nsswitch.conf)) {
|
|
|
|
|
next unless -f "/etc/$file";
|
|
|
|
|
copy("/etc/$file", "$fixture/etc/$file") or die $!;
|
|
|
|
|
}
|
|
|
|
|
# Some native configurations drop privileges and keep their rules in a separate file.
|
|
|
|
|
if ($native_defaults) {
|
|
|
|
|
write_file("$fixture/etc/rsyslog.d/local.conf", "*.info /var/log/messages\n");
|
|
|
|
|
write_file("$fixture/log/messages", '');
|
|
|
|
|
my ($uid, $gid) = (getpwnam('syslog'))[2, 3];
|
|
|
|
|
if (defined $uid) {
|
|
|
|
|
chown($uid, $gid, map { "$fixture/$_" } qw(log log/messages lib/rsyslog spool/rsyslog)) == 4 or die $!;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
write_file("$fixture/etc/os-release", 'ID="' . ($osver =~ /^rhels/ ? 'rhel' : 'openEuler') . "\"\n");
|
|
|
|
|
write_file("$fixture/etc/xCATMN", '') if $role eq 'mn';
|
|
|
|
|
if ($admin ne '') {
|
|
|
|
@@ -134,17 +181,39 @@ for my $case (@cases) {
|
|
|
|
|
write_file("$fixture/etc/rsyslog.conf", $config);
|
|
|
|
|
write_file("$fixture/upstream.conf", "module(load=\"imudp\")\ninput(type=\"imudp\" port=\"1514\")\n*.* /var/log/upstream\n");
|
|
|
|
|
local %ENV = (%ENV, PATH => "$tmp/bin:$ENV{PATH}", XCAT_SYSLOG_FIXTURE => $fixture,
|
|
|
|
|
XCAT_SYSLOG_BIN => "$tmp/bin", XCAT_SYSLOG_FAIL => $failure, XCAT_SYSLOG_ROLE => $role,
|
|
|
|
|
XCAT_SYSLOG_BIN => "$tmp/bin", XCAT_SYSLOG_FAIL => $failure, XCAT_SYSLOG_ROLE => $role, TMPDIR => '/tmp',
|
|
|
|
|
OSVER => $osver, NTYPE => ($role =~ /^sn/ ? 'service' : 'compute'),
|
|
|
|
|
SVLOGLOCAL => ($role eq 'snlocal' ? 1 : 0), MASTER => '127.0.0.1:1514', SYSLOG => '');
|
|
|
|
|
is(system('unshare', '-mn', '--', "$tmp/bin/namespace"), 0, "$name: isolated driver completed");
|
|
|
|
|
is(system('unshare', '-mnpf', '--mount-proc', '--', "$tmp/bin/namespace"), 0, "$name: isolated driver completed");
|
|
|
|
|
my $rc = read_file("$fixture/postscript-1.rc");
|
|
|
|
|
chomp $rc;
|
|
|
|
|
die "Missing postscript status for $name" unless $rc =~ /^\d+$/;
|
|
|
|
|
my $receiver = $role ne 'cn' && $osver !~ /^rhels/;
|
|
|
|
|
my @allocated = split /\n/, read_file("$fixture/allocated");
|
|
|
|
|
if ($failure eq 'allocation') {
|
|
|
|
|
ok(-s "$fixture/allocation-attempts", "$name: allocation fault was reached");
|
|
|
|
|
is(scalar @allocated, 0, "$name: failed allocation creates no file");
|
|
|
|
|
} elsif ($receiver) {
|
|
|
|
|
ok(scalar @allocated > 0, "$name: receiver allocated a temporary configuration");
|
|
|
|
|
}
|
|
|
|
|
for my $pass ($failure ? (1) : (1, 2)) {
|
|
|
|
|
is(-f "$fixture/temporary-$pass" ? read_file("$fixture/temporary-$pass") : 'missing evidence', '',
|
|
|
|
|
"$name: no temporary artifact remains after invocation $pass");
|
|
|
|
|
}
|
|
|
|
|
for my $path (@allocated) {
|
|
|
|
|
like($path, qr{^/tmp/[^/]+$}, "$name: allocation stays in the private temporary directory");
|
|
|
|
|
}
|
|
|
|
|
if ($failure) {
|
|
|
|
|
isnt($rc, 0, "$name: failure reaches postscript exit");
|
|
|
|
|
is(read_file("$fixture/logger.log"), '', "$name: no success log");
|
|
|
|
|
is(read_file("$fixture/services.log"), '', "$name: invalid configuration is not restarted") if $failure eq 'parser';
|
|
|
|
|
is(read_file("$fixture/services.log"), '', "$name: failed configuration is not restarted") if $failure ne 'restart';
|
|
|
|
|
if ($failure eq 'allocation') {
|
|
|
|
|
is($rc, 43, "$name: allocation status reaches the caller");
|
|
|
|
|
unlike(read_file("$fixture/rsyslog-calls"), qr/^-N1\b/m, "$name: validation is not attempted");
|
|
|
|
|
like(read_file("$fixture/config-1"), qr/^\Q$config\E/, "$name: existing configuration is preserved");
|
|
|
|
|
unlike(read_file("$fixture/config-1"), qr/^(?:module|input)\(/m,
|
|
|
|
|
"$name: no receiver is added after allocation fails");
|
|
|
|
|
}
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
is($rc, 0, "$name: first postscript succeeds");
|
|
|
|
|