make-xilog-testcase.pl [plain text]
use File::Basename;
sub script_to_xilog
{
my $scriptname = shift;
my $xiname = "";
my @components;
$scriptname =~ s/\.[a-z]{2,3}$//;
foreach my $part (split /[_-]/, $scriptname) {
$xiname .= "\u$part";
}
return $xiname;
}
sub add_script_symlink
{
use Cwd;
my $progname = shift;
my $xiname = shift;
my $cwd = getcwd;
print "cwd=$cwd\n" if ($debug);
unless (-e $progname) {
warn "checking for $progname: ", $!;
}
chdir "Tools/$xiname" || \
die "chdir Tools/$xiname: ", $!;
print "symlink ../../$progname $progname\n" if ($debug);
symlink "../../$progname", basename($progname) || die $!;
symlink 'null.log', '/dev/null';
chdir $cwd;
}
my @CMDLINE = @ARGV;
my $CMD = basename $CMDLINE[0];
my $XICMD = script_to_xilog($CMD);
my $smbtorture = defined($ENV{SMBTORTURE}) ? 1 : 0;
my $xilogwrap = defined($ENV{NOXILOG}) ? 0 : 1;
my $debug = defined($ENV{DEBUG}) ? 1 : 0;
print "generating XILog tool $XICMD for $CMD\n";
rmdir "Tools/$XICMD";
mkdir "Tools/$XICMD" or die "mkdir Tools/$XICMD: ", $!;
add_script_symlink($CMDLINE[0], $XICMD);
$CMDLINE[0] = "./$CMD";
open XILOG, ">Tools/$XICMD/$XICMD" or die "open Tools/$XICMD/$XICMD: ", $!;
print XILOG <<"EOF";
use strict;
use lib "/AppleInternal/Library/Perl";
use XILog qw(:api);
\$ENV{SCRIPTBASE} = '$ENV{SCRIPTBASE}';
my \$debug = defined(\$ENV{DEBUG});
sub search_for_test
{
my \$name;
while (my \$line = <TESTCASE>) {
chomp \$line;
if ((\$name) = (\$line =~ m/Running smbtorture test ([\\w-]+)/)) {
print "found test \$name\\n" if (\$debug);
return \$name;
}
}
return
}
sub process_test
{
my \$name = shift;
my \$lines = [];
my \$status = 'unknown';
while (my \$line = <TESTCASE>) {
chomp \$line;
push \@{\$lines}, \$line;
if ((\$status) = (\$line =~ m/Finished smbtorture test \\Q\$name\\E: (\\w+)/)) {
print "found test \$name\\n" if (\$debug);
return (\$status, \$lines);
}
}
return (\$status, \$lines);
}
sub parse_smb_torture_results
{
my \$xilog = shift;
while (1) {
my (\$testname, \$lines, \$status);
\$testname = search_for_test();
last unless \$testname;
XILogBeginTestCase(\$xilog, \$testname, \$testname);
(\$status, \$lines) = process_test(\$testname);
foreach my \$l (\@{\$lines}) {
print "XILogMsg(\$l)\\n";
XILogMsg(\$l);
}
if (\$status ne 'OK') {
XILogErr("smbtorture \$testname: \$status");
}
XILogEndTestCase(\$xilog);
}
close TESTCASE;
}
sub parse_simple_results
{
my \$xilog = shift;
XILogBeginTestCase(\$xilog, "$XICMD", "$XICMD");
while (my \$line = <TESTCASE>) {
chomp \$line;
if (\$line =~ m/FAILED/) {
XILogErr(\$line);
} else {
XILogMsg(\$line);
}
}
close TESTCASE
or \$! ? XILogErr("pipe error: \$!")
: XILogErr("failed with exit status: " . (\$? >> 8));
XILogEndTestCase(\$xilog);
}
my \$log;
my \$logpath = "Logs/$XICMD.log";
my \$smbtorture = $smbtorture;
my \$xilogwrap = $xilogwrap;
sub failtest
{
print STDERR \@_, "\\n";
XILogCloseLog(\$log) if (\$xilogwrap);
exit 1;
}
\$ENV{USERNAME} = \$ENV{BB_USER};
\$ENV{PASSWORD} = \$ENV{BB_PASS};
if (\$xilogwrap) {
\$log = XILogOpenLogFromPath(\$kXILogStyleXML, 1, \$logpath)
or die "opening \$logpath";
\$ENV{XILOG_PATH} = './null.log';
}
open(TESTCASE, "@CMDLINE 2>&1 |") or failtest "running $CMDLINE[0]: ", \$!;
if (\$xilogwrap) {
if (\$smbtorture) {
parse_smb_torture_results(\$log);
} else {
parse_simple_results(\$log);
}
XILogCloseLog(\$log);
} else {
print <TESTCASE>;
}
exit 0;
EOF
chmod 0755, "Tools/$XICMD/$XICMD";
exit 0;