mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-12-04 08:07:04 +00:00
Merge pull request #8052 from VersatusHPC/codex/r220-syslog-test-io
test(syslog): reuse repository and binary file helpers
This commit is contained in:
@@ -71,7 +71,7 @@ jobs:
|
||||
-v "$PWD:/src:ro" -w /src ubuntu:24.04 bash -ec '
|
||||
apt-get update -qq
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
|
||||
perl rsyslog iproute2 util-linux systemd gawk
|
||||
perl libfile-slurper-perl rsyslog iproute2 util-linux systemd gawk
|
||||
prove -v xCAT-test/native/syslog_openeuler.t' 2>&1 | tee "$log"
|
||||
! grep -Eq 'skipped:|Result: NOTESTS' "$log"
|
||||
- name: Run Ubuntu image fixture tests
|
||||
|
||||
@@ -164,8 +164,9 @@ the kernel log, drops CAP_SYS_ADMIN and CAP_SYSLOG after setup, and uses a
|
||||
private PID namespace. The native rsyslog binary is bound at a fixture path
|
||||
to avoid the host service's AppArmor
|
||||
attachment; this test does not validate that service profile. CI runs it in a
|
||||
disposable Ubuntu container and rejects skips. It needs rsyslog, util-linux and
|
||||
iproute2; service activation uses a stub that starts only the test daemon.
|
||||
disposable Ubuntu container and rejects skips. It needs rsyslog, util-linux,
|
||||
iproute2 and File::Slurper (`libfile-slurper-perl` or `perl-File-Slurper`, from EPEL on EL).
|
||||
Service activation uses a stub that starts only the test daemon.
|
||||
On EL10, install `util-linux`; `util-linux-core` alone does not provide `setpriv`.
|
||||
Use the CI container on AppArmor hosts instead of the direct-root command above.
|
||||
|
||||
|
||||
@@ -4,37 +4,32 @@ use warnings;
|
||||
use Cwd qw(abs_path);
|
||||
use File::Copy qw(copy);
|
||||
use File::Path qw(make_path);
|
||||
use File::Slurper qw(read_binary write_binary);
|
||||
use File::Temp qw(tempdir);
|
||||
use FindBin;
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
use XCAT::Test::File qw(repo_path);
|
||||
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 -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 $source = $ENV{XCAT_SYSLOG_SOURCE_ROOT} || repo_path('.');
|
||||
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: $!";
|
||||
print {$fh} $contents;
|
||||
close($fh) or die "$path: $!";
|
||||
}
|
||||
sub read_file {
|
||||
my ($path) = @_;
|
||||
return '' unless -f $path;
|
||||
open(my $fh, '<', $path) or die "$path: $!";
|
||||
local $/;
|
||||
return <$fh> // '';
|
||||
return scalar read_binary($path);
|
||||
}
|
||||
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');
|
||||
write_binary("$tmp/bin/native-rsyslogd", '');
|
||||
write_binary("$tmp/bin/systemctl", <<'SH');
|
||||
#!/bin/bash
|
||||
printf '%s\n' "$*" >> "$XCAT_SYSLOG_FIXTURE/services.log"
|
||||
[ "$XCAT_SYSLOG_FAIL" = restart ] && exit 42
|
||||
@@ -45,11 +40,11 @@ if [ -s /run/rsyslog-test.pid ]; then
|
||||
fi
|
||||
exec "$XCAT_SYSLOG_BIN/native-rsyslogd" -i /run/rsyslog-test.pid -f /etc/rsyslog.conf
|
||||
SH
|
||||
write_file("$tmp/bin/logger", <<'SH');
|
||||
write_binary("$tmp/bin/logger", <<'SH');
|
||||
#!/bin/bash
|
||||
printf '%s\n' "$*" >> "$XCAT_SYSLOG_FIXTURE/logger.log"
|
||||
SH
|
||||
write_file("$tmp/bin/mktemp", <<'SH');
|
||||
write_binary("$tmp/bin/mktemp", <<'SH');
|
||||
#!/bin/sh
|
||||
printf 'allocate\n' >>"$XCAT_SYSLOG_FIXTURE/allocation-attempts"
|
||||
[ "$XCAT_SYSLOG_FAIL" != allocation ] || exit 43
|
||||
@@ -57,12 +52,12 @@ path=$(/usr/bin/mktemp "$@") || exit $?
|
||||
printf '%s\n' "$path" >>"$XCAT_SYSLOG_FIXTURE/allocated"
|
||||
printf '%s\n' "$path"
|
||||
SH
|
||||
write_file("$tmp/bin/rsyslogd", <<'SH');
|
||||
write_binary("$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');
|
||||
write_binary("$tmp/bin/namespace", <<'SH');
|
||||
#!/bin/bash
|
||||
set -eu
|
||||
mount --make-rprivate /
|
||||
@@ -84,7 +79,7 @@ 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');
|
||||
write_binary("$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
|
||||
@@ -155,31 +150,31 @@ for my $case (@cases) {
|
||||
$admin //= ''; $failure //= '';
|
||||
my $fixture = "$tmp/" . ++$number;
|
||||
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);
|
||||
write_binary("$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", '');
|
||||
write_binary("$fixture/etc/rsyslog.d/local.conf", "*.info /var/log/messages\n");
|
||||
write_binary("$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';
|
||||
write_binary("$fixture/etc/os-release", 'ID="' . ($osver =~ /^rhels/ ? 'rhel' : 'openEuler') . "\"\n");
|
||||
write_binary("$fixture/etc/xCATMN", '') if $role eq 'mn';
|
||||
if ($admin ne '') {
|
||||
write_file("$fixture/etc/rsyslog.d/admin.conf", "include(file=\"/etc/admin/receiver.conf\")\n");
|
||||
write_file("$fixture/etc/admin/receiver.conf", $admin);
|
||||
write_binary("$fixture/etc/rsyslog.d/admin.conf", "include(file=\"/etc/admin/receiver.conf\")\n");
|
||||
write_binary("$fixture/etc/admin/receiver.conf", $admin);
|
||||
}
|
||||
if ($role eq 'cn') {
|
||||
write_file("$fixture/etc/rsyslog.d/admin.conf", "module(load=\"imudp\")\ninput(type=\"imudp\" port=\"1515\")\n");
|
||||
write_binary("$fixture/etc/rsyslog.d/admin.conf", "module(load=\"imudp\")\ninput(type=\"imudp\" port=\"1515\")\n");
|
||||
}
|
||||
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");
|
||||
write_binary("$fixture/etc/rsyslog.conf", $config);
|
||||
write_binary("$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, TMPDIR => '/tmp',
|
||||
OSVER => $osver, NTYPE => ($role =~ /^sn/ ? 'service' : 'compute'),
|
||||
|
||||
@@ -4,36 +4,31 @@ use warnings;
|
||||
use Cwd qw(abs_path);
|
||||
use File::Copy qw(copy);
|
||||
use File::Path qw(make_path);
|
||||
use File::Slurper qw(read_binary write_binary);
|
||||
use File::Temp qw(tempdir);
|
||||
use FindBin;
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
use XCAT::Test::File qw(repo_path);
|
||||
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, systemctl, systemd-detect-virt and chroot'
|
||||
if system('command -v rsyslogd systemctl systemd-detect-virt chroot >/dev/null');
|
||||
my $source = $ENV{XCAT_SYSLOG_SOURCE_ROOT} || abs_path("$FindBin::Bin/../..");
|
||||
my $source = $ENV{XCAT_SYSLOG_SOURCE_ROOT} || repo_path('.');
|
||||
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, $text) = @_;
|
||||
open(my $fh, '>', $path) or die "$path: $!";
|
||||
print {$fh} $text;
|
||||
close($fh) or die "$path: $!";
|
||||
}
|
||||
sub read_file {
|
||||
my ($path) = @_;
|
||||
return '' unless -f $path;
|
||||
open(my $fh, '<', $path) or die "$path: $!";
|
||||
local $/;
|
||||
return <$fh> // '';
|
||||
return scalar read_binary($path);
|
||||
}
|
||||
for my $name (qw(syslog xcatlib.sh)) {
|
||||
copy("$source/xCAT/postscripts/$name", "$tmp/bin/$name") or die $!;
|
||||
}
|
||||
write_file("$tmp/bin/systemctl", <<'SH');
|
||||
write_binary("$tmp/bin/systemctl", <<'SH');
|
||||
#!/bin/bash
|
||||
printf '%s\n' "$*" >> "/evidence/services-$XCAT_SYSLOG_PHASE.log"
|
||||
/usr/bin/systemctl "$@" > "/evidence/systemctl-$XCAT_SYSLOG_PHASE.log" 2>&1
|
||||
@@ -42,11 +37,11 @@ echo "$rc" > "/evidence/systemctl-$XCAT_SYSLOG_PHASE.rc"
|
||||
cat "/evidence/systemctl-$XCAT_SYSLOG_PHASE.log"
|
||||
exit "$rc"
|
||||
SH
|
||||
write_file("$tmp/bin/logger", <<'SH');
|
||||
write_binary("$tmp/bin/logger", <<'SH');
|
||||
#!/bin/bash
|
||||
printf '%s\n' "$*" >> /evidence/logger.log
|
||||
SH
|
||||
write_file("$tmp/bin/run", <<'SH');
|
||||
write_binary("$tmp/bin/run", <<'SH');
|
||||
#!/bin/bash
|
||||
set -eu
|
||||
mount --make-rprivate /
|
||||
@@ -89,9 +84,9 @@ for my $case (['mn', 0], ['snlocal', 0], ['snforward', 0], ['cn', 0], ['mn', 1],
|
||||
make_path("$fixture/evidence", map { "$fixture/root/$_" }
|
||||
qw(usr bin sbin lib lib64 postscripts evidence proc dev run tmp etc/rsyslog.d var/log var/lib/rsyslog));
|
||||
copy('/etc/ld.so.cache', "$fixture/root/etc/ld.so.cache") or die $!;
|
||||
write_file("$fixture/root/etc/os-release", "ID=openEuler\n");
|
||||
write_file("$fixture/root/etc/xCATMN", '') if $role eq 'mn';
|
||||
write_file("$fixture/root/etc/rsyslog.conf", $minimal . ($invalid ? "invalid_rsyslog_directive()\n" : ''));
|
||||
write_binary("$fixture/root/etc/os-release", "ID=openEuler\n");
|
||||
write_binary("$fixture/root/etc/xCATMN", '') if $role eq 'mn';
|
||||
write_binary("$fixture/root/etc/rsyslog.conf", $minimal . ($invalid ? "invalid_rsyslog_directive()\n" : ''));
|
||||
local %ENV = (%ENV, XCAT_SYSLOG_FIXTURE => $fixture, XCAT_SYSLOG_BIN => "$tmp/bin",
|
||||
OSVER => ($ENV{XCAT_SYSLOG_OSVER} || 'openeuler24.03'), NTYPE => ($role =~ /^sn/ ? 'service' : 'compute'),
|
||||
SVLOGLOCAL => ($role eq 'snlocal' ? 1 : 0), MASTER => '127.0.0.1:1514', SYSLOG => '');
|
||||
|
||||
Reference in New Issue
Block a user