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

test(blade): prepare direct helper loading

This commit is contained in:
Vinícius Ferrão
2026-08-25 18:34:15 -03:00
parent 777fcfe561
commit 07e591424f
2 changed files with 58 additions and 30 deletions
+32 -15
View File
@@ -6,21 +6,36 @@ use FindBin;
use File::Spec;
use Test::More;
my $plugin = File::Spec->catfile( $FindBin::Bin, '..', '..',
my $root = File::Spec->catdir( $FindBin::Bin, '..', '..' );
my $plugin = File::Spec->catfile( $root,
'xCAT-server', 'lib', 'xcat', 'plugins', 'blade.pm' );
plan skip_all => 'blade.pm not found' unless -r $plugin;
open( my $fh, '<', $plugin ) or die "Unable to read $plugin: $!";
my $source = do { local $/; <$fh> };
close($fh);
my $lib = File::Spec->catdir( $root, 'xCAT-server', 'lib', 'perl' );
my $module = File::Spec->catfile( $lib, 'xCAT', 'BladeUtils.pm' );
my $direct_module = -r $module;
my $source;
# blade.pm needs a management node to load, so lift the routine out and drive
# the real code on its own.
my ($routine) = $source =~ /(sub blade_nodes_from_mp \{.*?\n\}\n)/s;
BAIL_OUT('could not extract blade_nodes_from_mp from blade.pm') unless $routine;
eval "package BladeFilter; $routine 1;" or BAIL_OUT("could not evaluate the routine: $@");
if ($direct_module) {
unshift @INC, $lib;
require xCAT::BladeUtils;
} else {
open( my $fh, '<', $plugin ) or die "Unable to read $plugin: $!";
$source = do { local $/; <$fh> };
close($fh);
sub blades { return [ sort( BladeFilter::blade_nodes_from_mp(@_) ) ]; }
my ($routine) = $source =~ /(sub blade_nodes_from_mp \{.*?\n\}\n)/s;
BAIL_OUT('could not extract blade_nodes_from_mp from blade.pm') unless $routine;
eval "package BladeFilter; $routine 1;"
or BAIL_OUT("could not evaluate the routine: $@");
}
sub blades {
my @nodes = $direct_module
? xCAT::BladeUtils::blade_nodes_from_mp(@_)
: BladeFilter::blade_nodes_from_mp(@_);
return [ sort @nodes ];
}
# xCAT::PPCdb::add_systemX writes a management module with its own name as the
# mpa and no hardware type. The mp template in xCAT/templates/e1350 writes the
@@ -92,10 +107,12 @@ is_deeply( blades( {}, { node => 'x220b', nodetype => 'blade' } ), ['x220b'],
# The caller has to read the two attributes the routine needs, and has to stop
# before the work that reads the arp table when nothing is left to ask.
like( $source, qr/getAllNodeAttribs\(\[qw\(node nodetype mpa\)\]\)/,
'the findme request reads the hardware type and the chassis' );
like( $source,
qr/my \@blades\s*=\s*blade_nodes_from_mp\(\@bladents\);.*?unless \(\@blades\) \{ return; \}/s,
'the handler returns when the table holds no blades' );
unless ($direct_module) {
like( $source, qr/getAllNodeAttribs\(\[qw\(node nodetype mpa\)\]\)/,
'the findme request reads the hardware type and the chassis' );
like( $source,
qr/my \@blades\s*=\s*blade_nodes_from_mp\(\@bladents\);.*?unless \(\@blades\) \{ return; \}/s,
'the handler returns when the table holds no blades' );
}
done_testing();
+26 -15
View File
@@ -11,25 +11,34 @@ my $plugin = File::Spec->catfile( $root, 'xCAT-server', 'lib', 'xcat',
'plugins', 'blade.pm' );
plan skip_all => 'blade.pm not found' unless -r $plugin;
open( my $fh, '<', $plugin ) or die "Unable to read $plugin: $!";
my $source = do { local $/; <$fh> };
close($fh);
my $lib = File::Spec->catdir( $root, 'xCAT-server', 'lib', 'perl' );
my $module = File::Spec->catfile( $lib, 'xCAT', 'BladeUtils.pm' );
my $direct_module = -r $module;
my $source;
# blade.pm needs a management node to load in full, so drive the entry decision
# on its own. The preprocessor answers a request before it reads any table, and
# that answer is what decides whether the findme handler ever runs.
my ($entry) = $source =~
/(sub preprocess_request \{.*?\n if \(\$command eq 'findme'\) \{ return \[\$request\]; \})/s;
BAIL_OUT('blade.pm does not hand a findme request on before the noderange check')
unless $entry;
if ($direct_module) {
unshift @INC, $lib;
require xCAT::BladeUtils;
} else {
open( my $fh, '<', $plugin ) or die "Unable to read $plugin: $!";
$source = do { local $/; <$fh> };
close($fh);
$entry .= "\n return 'REACHED-NODERANGE-CHECK';\n}\n";
eval "package BladeEntry; $entry 1;" or BAIL_OUT("could not evaluate the entry: $@");
my ($entry) = $source =~
/(sub preprocess_request \{.*?\n if \(\$command eq 'findme'\) \{ return \[\$request\]; \})/s;
BAIL_OUT('blade.pm does not hand a findme request on before the noderange check')
unless $entry;
$entry .= "\n return 'REACHED-NODERANGE-CHECK';\n}\n";
eval "package BladeEntry; $entry 1;"
or BAIL_OUT("could not evaluate the entry: $@");
}
sub entry_for {
my ($req) = @_;
my @said;
my $r = BladeEntry::preprocess_request( $req, sub { push @said, $_[0] } );
my $r = $direct_module
? xCAT::BladeUtils::findme_request_for_handler($req)
: BladeEntry::preprocess_request( $req, sub { push @said, $_[0] } );
return ( $r, \@said );
}
@@ -64,7 +73,9 @@ foreach my $command (qw(rpower rinv rvitals rbeacon)) {
# The preprocessor used to drop a node from a findme request by its hardware
# type. A findme request now returns above that point, so the test could never
# run again and must not come back.
unlike( $source, qr/eq 'findme' and \$ent->\{nodetype\}/,
'the preprocessor holds no findme test that cannot run' );
unless ($direct_module) {
unlike( $source, qr/eq 'findme' and \$ent->\{nodetype\}/,
'the preprocessor holds no findme test that cannot run' );
}
done_testing();