From 07e591424f435ab1732ac114d894afc69a8e33b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Tue, 25 Aug 2026 18:34:15 -0300 Subject: [PATCH] test(blade): prepare direct helper loading --- xCAT-test/unit/blade_findme_chassis_filter.t | 47 +++++++++++++------- xCAT-test/unit/blade_findme_dispatch.t | 41 ++++++++++------- 2 files changed, 58 insertions(+), 30 deletions(-) diff --git a/xCAT-test/unit/blade_findme_chassis_filter.t b/xCAT-test/unit/blade_findme_chassis_filter.t index ed29eaa67..2fb8d7e90 100644 --- a/xCAT-test/unit/blade_findme_chassis_filter.t +++ b/xCAT-test/unit/blade_findme_chassis_filter.t @@ -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(); diff --git a/xCAT-test/unit/blade_findme_dispatch.t b/xCAT-test/unit/blade_findme_dispatch.t index a119ee1f4..86e096198 100644 --- a/xCAT-test/unit/blade_findme_dispatch.t +++ b/xCAT-test/unit/blade_findme_dispatch.t @@ -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();