2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-05-05 16:49:08 +00:00

Address Perl quality findings

This commit is contained in:
Vinícius Ferrão
2026-04-23 03:04:37 -03:00
parent 6f3d9bb9d1
commit 8399d88509
7 changed files with 38 additions and 21 deletions

View File

@@ -16,7 +16,7 @@ sub normalize {
return;
}
sub select {
sub choose {
my ( $class, %args ) = @_;
my $requested = exists $args{requested} ? $args{requested} : $class->_site_backend();
@@ -89,12 +89,16 @@ sub backend_class {
sub new_backend {
my ( $class, %args ) = @_;
my $selection = $class->select(%args);
my $selection = $class->choose(%args);
return $selection if $selection->{error};
my $backend_class = $class->backend_class( $selection->{name} );
eval "require $backend_class";
if ($@) {
( my $backend_file = "$backend_class.pm" ) =~ s{::}{/}g;
my $loaded = eval {
require $backend_file;
1;
};
if (!$loaded) {
return {
%$selection,
error => "Unable to load DHCP backend '$selection->{name}': $@",
@@ -105,19 +109,23 @@ sub new_backend {
}
sub _site_backend {
eval {
my $backend = eval {
require xCAT::TableUtils;
return xCAT::TableUtils->get_site_attribute('dhcpbackend', 'auto');
} || 'auto';
};
return $backend || 'auto';
}
sub _osver {
my ( $class, $type ) = @_;
eval {
my $osver = eval {
require xCAT::Utils;
return defined($type) ? xCAT::Utils->osver($type) : xCAT::Utils->osver();
} || 'unknown';
};
return $osver || 'unknown';
}
sub _command_exists {

View File

@@ -707,7 +707,8 @@ sub _control_agent_not_found {
}
sub _first_defined {
foreach my $value (@_) {
my @values = @_;
foreach my $value (@values) {
return $value if defined $value;
}

View File

@@ -1,5 +1,8 @@
#!/usr/bin/perl -w
#use Data::Dumper;
use strict;
use warnings;
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
@@ -101,7 +104,8 @@ if($help){
}
my $omshell;
open($omshell, "|/usr/bin/omshell >/dev/null");
open($omshell, '|-', '/bin/sh', '-c', '/usr/bin/omshell >/dev/null')
or die "Unable to start omshell: $!";
print $omshell "key "
. $id . " \""
. $passwd . "\"\n";

View File

@@ -50,43 +50,43 @@ is(
);
is(
xCAT::DHCP::Backend->select( requested => 'isc', os => 'rhel10', platform => 'el10' )->{name},
xCAT::DHCP::Backend->choose( requested => 'isc', os => 'rhel10', platform => 'el10' )->{name},
'isc',
'explicit ISC override wins on EL10'
);
is(
xCAT::DHCP::Backend->select( requested => 'kea', os => 'rhel9', platform => 'el9' )->{name},
xCAT::DHCP::Backend->choose( requested => 'kea', os => 'rhel9', platform => 'el9' )->{name},
'kea',
'explicit Kea override wins on EL9'
);
is(
xCAT::DHCP::Backend->select( requested => 'auto', os => 'rhel9', platform => 'el9' )->{name},
xCAT::DHCP::Backend->choose( requested => 'auto', os => 'rhel9', platform => 'el9' )->{name},
'isc',
'auto selects ISC on EL9'
);
is(
xCAT::DHCP::Backend->select( requested => 'auto', os => 'rhel10', platform => 'el10' )->{name},
xCAT::DHCP::Backend->choose( requested => 'auto', os => 'rhel10', platform => 'el10' )->{name},
'kea',
'auto selects Kea on EL10'
);
is(
xCAT::DHCP::Backend->select( requested => 'auto', os => 'ubuntu24.04', os_name => 'ubuntu', version => '24.04' )->{name},
xCAT::DHCP::Backend->choose( requested => 'auto', os => 'ubuntu24.04', os_name => 'ubuntu', version => '24.04' )->{name},
'kea',
'auto selects Kea on Ubuntu 24.04'
);
like(
xCAT::DHCP::Backend->select( requested => 'invalid' )->{error},
xCAT::DHCP::Backend->choose( requested => 'invalid' )->{error},
qr/Invalid site\.dhcpbackend/,
'invalid explicit backend returns a clear error'
);
like(
xCAT::DHCP::Backend->select(
xCAT::DHCP::Backend->choose(
requested => 'kea',
check_available => 1,
available => { kea => 0 },
@@ -96,7 +96,7 @@ like(
);
is(
xCAT::DHCP::Backend->select(
xCAT::DHCP::Backend->choose(
requested => 'kea',
check_available => 1,
available => { kea => 1 },

View File

@@ -202,7 +202,9 @@ sub validation_temp_dir {
my $command = shell_quote($kea_dhcp4) . ' -t ' . shell_quote($path) . ' 2>&1';
my $output = `$command`;
unlink $path;
return undef if $output =~ /Unable to open file/;
if ( $output =~ /Unable to open file/ ) {
return;
}
return '';
}

View File

@@ -137,6 +137,8 @@ sub write_file {
open(my $fh, '>', $path) or die "Unable to write $path: $!";
print $fh $content;
close($fh) or die "Unable to close $path: $!";
return 1;
}
sub start_daemon {

View File

@@ -186,8 +186,8 @@ close($configfh);
my $write_result = $backend->write_dhcp4_json('{"new":true}', path => $config_path, skip_validate => 1, backup_existing => 1);
ok( !$write_result->{error}, 'write_dhcp4_json succeeds with backup_existing' );
is( $write_result->{backup}, "$config_path.xcatbak", 'backup path is reported' );
my $config_mode = ( stat($config_path) )[2] & 07777;
ok( $config_mode == 0640 || $config_mode == 0644, 'written config is readable by the Kea service user' );
my $config_mode = ( stat($config_path) )[2] & oct('7777');
ok( $config_mode == oct('640') || $config_mode == oct('644'), 'written config is readable by the Kea service user' );
open(my $backupfh, '<', "$config_path.xcatbak") or die "Unable to read backup config: $!";
my $backup_content = <$backupfh>;
close($backupfh);