mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-11-22 02:12:16 +00:00
test(xcat-probe): the probe case measures a checkpoint line the test wrote itself
xcatprobe_xcatmn_selinux asserts the text of the SELinux checkpoint, and probe_xcatmn_selinux_case.t built that line by hand. The line xcatprobe prints in the GitHub Actions container carries no text, so the test passed while the case failed. xcatprobe held the layout in format_cmd_output, which no test can call. This moves the layout to probe_format and leaves xcatprobe the printing. The rendering is unchanged at every width: an identical column of output for widths 7, 40, 79, 80, 120, 121 and 200, truncated and wrapped, with and without colour. The test now renders the checkpoint through probe_format at the width the container gives, and it fails here. screen_width returns 0 for a terminal 'tput cols' cannot measure, the message is cut to the empty string, and three assertions read '[ OK ]' where they expect the checkpoint text. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
||||
package probe_format;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $DESIRED_WIDTH = 120;
|
||||
my $DEFAULT_WIDTH = 80;
|
||||
my $FLAG_WIDTH = 6;
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
=head3 screen_width
|
||||
|
||||
Descriptions:
|
||||
Return the number of columns to format a message in.
|
||||
Arguments:
|
||||
raw: the output of 'tput cols', or undef when there is no terminal
|
||||
Returns:
|
||||
the column count
|
||||
|
||||
=cut
|
||||
|
||||
#-----------------------------------
|
||||
sub screen_width {
|
||||
my ($raw) = @_;
|
||||
|
||||
return $DEFAULT_WIDTH unless (defined($raw));
|
||||
return $raw =~ /(\d+)/ ? $1 + 0 : 0;
|
||||
}
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
=head3 render
|
||||
|
||||
Descriptions:
|
||||
Lay out one message line of a probe subcommand.
|
||||
Arguments:
|
||||
line: the line the subcommand printed, "[<flag>]:<message>"
|
||||
fullmsg: true to wrap a long message, false to truncate it
|
||||
screenwidth: the column count from screen_width()
|
||||
Returns:
|
||||
(lines, mark). lines is an array reference. Print every line but the
|
||||
last with a newline. mark is what follows the last line: undef when
|
||||
the line carries no flag, and otherwise the mark to print, which
|
||||
already holds its newline.
|
||||
|
||||
=cut
|
||||
|
||||
#-----------------------------------
|
||||
sub render {
|
||||
my ($line, $fullmsg, $screenwidth) = @_;
|
||||
|
||||
my $msg = $line;
|
||||
my $flag;
|
||||
if ($line =~ /\[(\w+)\]\s*:(.+)/) {
|
||||
$flag = $1;
|
||||
$msg = $2;
|
||||
}
|
||||
|
||||
$msg =~ s/\t/ /g;
|
||||
my $maxlen = ($screenwidth > $DESIRED_WIDTH ? $DESIRED_WIDTH : $screenwidth);
|
||||
|
||||
my @finalmsg = ();
|
||||
my $msglen = length($msg);
|
||||
if ($msglen <= $maxlen) {
|
||||
if (!$fullmsg && $flag) {
|
||||
my $leftspace = $maxlen - length($msg);
|
||||
if ($leftspace < $FLAG_WIDTH) {
|
||||
$msg = substr($msg, 0, $maxlen - $FLAG_WIDTH);
|
||||
$msg =~ s/(.*).../$1\.\.\./g;
|
||||
}
|
||||
}
|
||||
push @finalmsg, $msg;
|
||||
} else {
|
||||
my $head = 0;
|
||||
my $tail = $maxlen;
|
||||
while ($head < $msglen) {
|
||||
push @finalmsg, substr($msg, $head, ($tail - $head));
|
||||
if (!$fullmsg) {
|
||||
if ($flag) {
|
||||
$finalmsg[0] =~ s/(.*).........$/$1\.\.\./g;
|
||||
} else {
|
||||
$finalmsg[0] =~ s/(.*)...$/$1\.\.\./g;
|
||||
}
|
||||
last;
|
||||
}
|
||||
$head = $tail;
|
||||
$tail = $head + ($maxlen <= ($msglen - $tail) ? $maxlen : ($msglen - $tail));
|
||||
}
|
||||
}
|
||||
|
||||
return (\@finalmsg, undef) unless ($flag);
|
||||
|
||||
my $last = $finalmsg[-1];
|
||||
my $leftspace = $maxlen - length($last);
|
||||
my $debug = ($flag =~ /debug/i) ? 1 : 0;
|
||||
my $mark = '';
|
||||
if ($flag =~ /failed/i) {
|
||||
$mark = "[FAIL]\n";
|
||||
} elsif ($flag =~ /warning/i) {
|
||||
$mark = "[WARN]\n";
|
||||
} elsif ($flag =~ /ok/i) {
|
||||
$mark = "[ OK ]\n";
|
||||
} elsif ($debug) {
|
||||
$mark = "\n";
|
||||
} elsif ($flag =~ /info/i) {
|
||||
$mark = "[INFO]\n";
|
||||
}
|
||||
|
||||
my $spacenum = 0;
|
||||
if (!$debug) {
|
||||
$spacenum = (($leftspace >= $FLAG_WIDTH) ? ($leftspace - $FLAG_WIDTH) : ($screenwidth - length($last) + $maxlen - $FLAG_WIDTH));
|
||||
}
|
||||
$finalmsg[-1] = $last . (" " x $spacenum);
|
||||
|
||||
return (\@finalmsg, $mark);
|
||||
}
|
||||
|
||||
1;
|
||||
+40
-94
@@ -1,6 +1,10 @@
|
||||
#!/usr/bin/env perl
|
||||
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
||||
|
||||
BEGIN { $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; }
|
||||
|
||||
use lib "$::XCATROOT/probe/lib/perl";
|
||||
use probe_format;
|
||||
use File::Basename;
|
||||
use Data::Dumper;
|
||||
use File::Path;
|
||||
@@ -8,8 +12,6 @@ use POSIX qw(WNOHANG setsid :errno_h);
|
||||
use Term::ANSIColor qw(:constants);
|
||||
$Term::ANSIColor::AUTORESET = 1;
|
||||
|
||||
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
|
||||
|
||||
my $program_name = basename($0);
|
||||
|
||||
my $pro_dir = "$::XCATROOT/probe/";
|
||||
@@ -74,6 +76,20 @@ sub loadsubcmds {
|
||||
}
|
||||
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
=head3
|
||||
Description:
|
||||
Ask the terminal how wide it is. 'tput cols' writes nothing when TERM
|
||||
names a terminal terminfo does not carry, so probe_format decides what an
|
||||
unusable answer means.
|
||||
=cut
|
||||
|
||||
#-----------------------------------
|
||||
sub terminal_width {
|
||||
return probe_format::screen_width($ENV{'TERM'} ? `tput cols 2>/dev/null` : undef);
|
||||
}
|
||||
|
||||
#-----------------------------------
|
||||
|
||||
=head3
|
||||
@@ -87,96 +103,29 @@ sub format_cmd_output {
|
||||
my $color = shift;
|
||||
my $fullmsg = shift;
|
||||
|
||||
my $msg;
|
||||
my $flag;
|
||||
my ($lines, $mark) = probe_format::render($line, $fullmsg, terminal_width());
|
||||
|
||||
if ($line =~ /\[(\w+)\]\s*:(.+)/) {
|
||||
$flag = $1;
|
||||
$msg = $2;
|
||||
my $last = pop @$lines;
|
||||
print "$_\n" foreach (@$lines);
|
||||
|
||||
unless (defined($mark)) {
|
||||
print "$last\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
print $last;
|
||||
return 0 unless (length($mark));
|
||||
|
||||
if (!$color) {
|
||||
print $mark;
|
||||
} elsif ($mark =~ /FAIL/) {
|
||||
print BOLD RED $mark;
|
||||
} elsif ($mark =~ /WARN/) {
|
||||
print BOLD BLUE $mark;
|
||||
} elsif ($mark =~ /OK/) {
|
||||
print BOLD GREEN $mark;
|
||||
} else {
|
||||
$msg = $line;
|
||||
}
|
||||
|
||||
$msg =~ s/\t/ /g;
|
||||
my $flaglen = 6;
|
||||
my $desiredwidth = 120;
|
||||
my $screenwidth = 80;
|
||||
if ($ENV{'TERM'}) {
|
||||
$screenwidth = (`tput cols` + 0);
|
||||
}
|
||||
my $maxlen = ($screenwidth > $desiredwidth ? $desiredwidth : $screenwidth);
|
||||
|
||||
my @finalmsg = ();
|
||||
my $msglen = length($msg);
|
||||
if ($msglen <= $maxlen) {
|
||||
if (!$fullmsg && $flag) {
|
||||
my $leftspace = $maxlen - length($msg);
|
||||
if ($leftspace < $flaglen) {
|
||||
$msg = substr($msg, 0, $maxlen - $flaglen);
|
||||
$msg =~ s/(.*).../$1\.\.\./g;
|
||||
}
|
||||
}
|
||||
push @finalmsg, $msg;
|
||||
} else {
|
||||
my @tmpmsg = split("", $msg);
|
||||
my $head = 0;
|
||||
my $tail = $maxlen;
|
||||
while ($head < $msglen) {
|
||||
push @finalmsg, substr($msg, $head, ($tail - $head));
|
||||
if (!$fullmsg) {
|
||||
if ($flag) {
|
||||
$finalmsg[0] =~ s/(.*).........$/$1\.\.\./g;
|
||||
} else {
|
||||
$finalmsg[0] =~ s/(.*)...$/$1\.\.\./g;
|
||||
}
|
||||
last;
|
||||
}
|
||||
$head = $tail;
|
||||
$tail = $head + ($maxlen <= ($msglen - $tail) ? $maxlen : ($msglen - $tail));
|
||||
}
|
||||
}
|
||||
|
||||
for (my $i = 0 ; $i < $#finalmsg + 1 ; ++$i) {
|
||||
if ($i ne $#finalmsg) {
|
||||
print "$finalmsg[$i]\n";
|
||||
next;
|
||||
}
|
||||
|
||||
if ($flag) {
|
||||
my $leftspace = $maxlen - length($finalmsg[$i]);
|
||||
my $spacenum = 0;
|
||||
if($flag !~ /debug/i) {
|
||||
$spacenum = (($leftspace >= $flaglen) ? ($leftspace - $flaglen) : ($screenwidth - length($finalmsg[$i]) + $maxlen - $flaglen));
|
||||
}
|
||||
my $spacestr = " " x $spacenum;
|
||||
print "$finalmsg[$i]$spacestr";
|
||||
|
||||
if ($flag =~ /failed/i) {
|
||||
if ($color) {
|
||||
print BOLD RED "[FAIL]\n";
|
||||
} else {
|
||||
print "[FAIL]\n";
|
||||
}
|
||||
} elsif ($flag =~ /warning/i) {
|
||||
if ($color) {
|
||||
print BOLD BLUE "[WARN]\n";
|
||||
} else {
|
||||
print "[WARN]\n";
|
||||
}
|
||||
} elsif ($flag =~ /ok/i) {
|
||||
if ($color) {
|
||||
print BOLD GREEN "[ OK ]\n";
|
||||
} else {
|
||||
print "[ OK ]\n";
|
||||
}
|
||||
} elsif ($flag =~ /debug/i) {
|
||||
print "\n";
|
||||
} elsif ($flag =~ /info/i) {
|
||||
print "[INFO]\n";
|
||||
}
|
||||
} else {
|
||||
print "$finalmsg[$i]\n";
|
||||
}
|
||||
print $mark;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -197,10 +146,7 @@ sub listvalidsubcmd {
|
||||
$maxlen += 4;
|
||||
|
||||
my $desiredwidth = 120;
|
||||
my $screenwidth = 80;
|
||||
if ($ENV{'TERM'}) {
|
||||
$screenwidth = (`tput cols` + 0);
|
||||
}
|
||||
my $screenwidth = terminal_width();
|
||||
my $finallen = ($screenwidth > $desiredwidth ? $desiredwidth : $screenwidth);
|
||||
|
||||
print "Supported sub commands are:\n";
|
||||
|
||||
@@ -13,6 +13,7 @@ use lib "$FindBin::Bin/../../xCAT-probe/lib/perl";
|
||||
use Test::More;
|
||||
|
||||
require probe_utils;
|
||||
require probe_format;
|
||||
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
|
||||
@@ -60,10 +61,18 @@ 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]',
|
||||
);
|
||||
# Lay the marks out the way xcatprobe does, at the width the GitHub Actions
|
||||
# container gives: it sets TERM to a name terminfo does not carry, so
|
||||
# 'tput cols' writes nothing. The case reads what this renders.
|
||||
my $width = probe_format::screen_width('');
|
||||
my %rendered;
|
||||
foreach my $tag (qw(o f)) {
|
||||
my ($lines, $flag) = probe_format::render($mark{$tag}, 0, $width);
|
||||
chomp($flag);
|
||||
$rendered{ $tag eq 'o' ? 'ok' : 'failed' } = join("\n", @$lines) . $flag;
|
||||
}
|
||||
like($rendered{ok}, qr/\Q$checkpoint\E/,
|
||||
"the rendered checkpoint keeps its text without a terminal: '$rendered{ok}'");
|
||||
foreach my $pattern (@positive) {
|
||||
like($rendered{ok}, qr/$pattern/, "a passing SELinux checkpoint matches '$pattern'");
|
||||
unlike($rendered{failed}, qr/$pattern/,
|
||||
|
||||
Reference in New Issue
Block a user