2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-20 09:22:17 +00:00

test(xcat-core): the xcatmn probe case throws away the output it checks

xcatprobe_xcatmn_selinux runs the probe through grep -i selinux. grep
keeps the lines that name SELinux and discards every other checkpoint,
so the one CI failure of this case reported rc 1 with an empty output
and said nothing about which checkpoint the probe stopped at. The second
check, on the word FAIL, read that empty output and passed.

probe_xcatmn_selinux_case.t requires the case to keep the whole output,
to assert at least one pattern the output must hold, and to name the
SELinux checkpoint in every pattern. It takes the marks of a checkpoint
from probe_utils->send_msg, and requires each pattern to tell a passing
checkpoint from a failing one.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
Daniel Hilst
2026-10-21 19:30:14 -03:00
parent c9210eb26e
commit ea658e280e
@@ -0,0 +1,79 @@
#!/usr/bin/env perl
use strict;
use warnings;
# Keep modules out of an installed /opt/xcat, so the checkout is what loads.
BEGIN { $ENV{XCATROOT} = '/nonexistent/xcatroot' }
use Capture::Tiny qw(capture_stdout);
use File::Slurper qw(read_lines);
use FindBin;
use lib "$FindBin::Bin/../../perl-xCAT";
use lib "$FindBin::Bin/../../xCAT-probe/lib/perl";
use Test::More;
require probe_utils;
like($INC{'probe_utils.pm'}, qr/\Q$FindBin::Bin\E/, 'probe_utils comes from this checkout');
# The case ran the probe through grep. ausearch is not involved here: grep kept
# the lines that name SELinux and discarded every other checkpoint, so the one
# CI failure reported an empty output and said nothing about the probe. The
# second check, on the word FAIL, then had nothing to read.
my %case;
my $name;
foreach my $line (read_lines("$FindBin::Bin/../autotest/testcase/probe/xcatmn")) {
$name = $1 if ($line =~ /^start:(\S+)/);
push @{ $case{$name}{cmd} }, $1 if ($line =~ /^cmd:(.+)/);
push @{ $case{$name}{check} }, $1 if ($line =~ /^check:(.+)/);
}
my $probe = $case{xcatprobe_xcatmn_selinux};
ok($probe, 'the testcase file declares xcatprobe_xcatmn_selinux');
my @cmds = @{ $probe->{cmd} || [] };
my @checks = @{ $probe->{check} || [] };
ok(scalar(@cmds) >= 1, "the case runs @{[scalar @cmds]} commands");
ok(scalar(@checks) >= 1, "the case makes @{[scalar @checks]} checks");
foreach my $cmd (@cmds) {
unlike($cmd, qr/\|/,
"the case keeps the whole output of '$cmd', instead of filtering it");
}
my @patterns = map { /^output\s*[=!]~\s*(\S.*)/ ? $1 : () } @checks;
my @positive = map { /^output\s*=~\s*(\S.*)/ ? $1 : () } @checks;
ok(scalar(@positive) >= 1,
"the case asserts @{[scalar @positive]} patterns that the probe output must hold");
foreach my $pattern (@patterns) {
like($pattern, qr/SELinux/,
"the pattern '$pattern' names the SELinux checkpoint, so another checkpoint cannot satisfy it");
}
# The subcommand marks each checkpoint through send_msg, and xcatprobe rewrites
# that mark as a right hand [ OK ] or [FAIL]. Take the subcommand marks by
# calling send_msg, so a change of format fails here.
my $checkpoint = 'Checking SELinux policy for xCAT...';
my %mark;
foreach my $tag (qw(o f)) {
$mark{$tag} = capture_stdout { probe_utils->send_msg('stdout', $tag, $checkpoint) };
chomp($mark{$tag});
}
isnt($mark{o}, $mark{f},
"send_msg marks an ok checkpoint '$mark{o}' apart from a failed one '$mark{f}'");
like($mark{o}, qr/\QSELinux\E/, 'the ok mark carries the checkpoint text');
my %rendered = (
ok => $checkpoint . (' ' x 39) . '[ OK ]',
failed => $checkpoint . (' ' x 39) . '[FAIL]',
);
foreach my $pattern (@positive) {
like($rendered{ok}, qr/$pattern/, "a passing SELinux checkpoint matches '$pattern'");
unlike($rendered{failed}, qr/$pattern/,
"a failing SELinux checkpoint does not match '$pattern'");
}
foreach my $pattern (map { /^output\s*!~\s*(\S.*)/ ? $1 : () } @checks) {
like($rendered{failed}, qr/$pattern/,
"a failing SELinux checkpoint matches the excluded pattern '$pattern'");
unlike($rendered{ok}, qr/$pattern/,
"a passing SELinux checkpoint does not match the excluded pattern '$pattern'");
}
done_testing();