2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-10-21 15:18:19 +00:00
Files
Vinícius Ferrão 9653713bdd test(ci): cover Perl and Bats failure combinations
Exercise the full driver with either or both unit suites failing. Check
execution order, both result messages, and the final exit status.

Allow multiple command failures in the fixture. Encode logged commands as
JSON arrays for compatibility with older JSON::PP versions.
2026-10-20 16:20:29 -03:00

75 lines
2.6 KiB
Perl

package Commands;
use strict;
use warnings;
use JSON::PP qw(decode_json encode_json);
my @failed_commands = @{ decode_json($ENV{XCAT_TEST_CI_FAIL_COMMANDS} || '[]') };
sub record_command {
my ($command) = @_;
open(my $log, '>>', $ENV{XCAT_TEST_CI_COMMANDS}) or die "open command log: $!";
print {$log} encode_json([$command]), "\n";
close($log) or die "close command log: $!";
return;
}
BEGIN {
*CORE::GLOBAL::readpipe = sub {
my ($command) = @_;
record_command($command);
my @output = ("ok\n");
my $status = 0;
if (grep { $command eq $_ } @failed_commands) {
@output = ("command failed\n");
$status = 1;
} elsif ($command =~ /^ip -o link /) {
@output = ();
} elsif ($command =~ /^file (.+) 2>&1$/) {
my $file = $1;
@output = ($file . ': ' . ($file =~ /\.txt$/ ? "ASCII text\n" : "Perl script text\n"));
} elsif ($command eq 'hostname') {
@output = ("ci-node\n");
} elsif ($command =~ /xcattest -s "ci_test" -l/) {
@output = ("example_case\n");
} elsif ($command =~ /xcattest -s "(?:prov_wire|dhcp_wire)" -l/) {
@output = ("There are no cases match search expression\n");
} elsif ($command =~ /xcattest -f /) {
my $result = $ENV{XCAT_TEST_CI_FAIL_CASE} ? 'Failed' : 'Passed';
@output = ("------END::example_case::$result\n");
}
$? = $status << 8;
return wantarray ? @output : join('', @output);
};
*CORE::GLOBAL::system = sub {
record_command(join(' ', @_));
return 0;
};
}
# Install the filesystem fixture after the driver compiles, before its main code runs.
INIT {
no warnings qw(redefine once);
*main::get_files_recursive = sub {
my ($directory, $files) = @_;
if ($directory eq '/opt/xcat') {
push @$files,
'/opt/xcat/share/xcat/netboot/genesis/bin/init',
'/opt/xcat/probe/lib/example.pm',
'/opt/xcat/share/xcat/tools/autotest/unit/example.t',
'/opt/xcat/lib/perl/xCAT/Example.pm',
'/opt/xcat/share/xcat/tools/autotest/unit-extra/example.pl',
'/opt/xcat/probe-extra/example.pl',
'/opt/xcat/share/xcat/netboot/genesis-extra/example.pl',
'/opt/xcat/share/readme.txt';
} elsif ($directory eq '/install') {
push @$files, '/install/postscripts/example';
} else {
die "unexpected scan directory: $directory";
}
return 0;
};
}
1;