mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-09-04 20:17:55 +00:00
test(dhcp): exercise source-owned OMAPI behavior
Signed-off-by: Vinícius Ferrão <2031761+viniciusferrao@users.noreply.github.com>
This commit is contained in:
@@ -126,8 +126,7 @@ sub write_command_file {
|
||||
sub cleanup_command_file {
|
||||
my ($path) = @_;
|
||||
|
||||
ok( unlink($path), 'the caller can remove the completed command file' );
|
||||
ok( !-e $path, 'the command file is gone after caller cleanup' );
|
||||
unlink($path) or die "Unable to remove $path: $!";
|
||||
}
|
||||
|
||||
my $workspace = tempdir( CLEANUP => 1 );
|
||||
@@ -135,16 +134,6 @@ my $command_directory = File::Spec->catdir( $workspace, 'commands' );
|
||||
my $capture = File::Spec->catfile( $workspace, 'captured-input' );
|
||||
my $success = File::Spec->catfile( $workspace, 'success' );
|
||||
|
||||
is(
|
||||
xCAT::DHCP::OmapiRunner->_command_directory(),
|
||||
'/tmp/xcat',
|
||||
'the production command directory remains /tmp/xcat'
|
||||
);
|
||||
is( xCAT::DHCP::OmapiRunner->_completion_attempts(), 100, 'the completion window remains 100 polls' );
|
||||
is( xCAT::DHCP::OmapiRunner->_termination_attempts(), 20, 'the TERM grace period remains 20 polls' );
|
||||
is( xCAT::DHCP::OmapiRunner->_poll_interval(), 0.1, 'the process poll interval remains 0.1 seconds' );
|
||||
is( xCAT::DHCP::OmapiRunner->_completion_delay(), 1.0, 'the post-completion delay remains one second' );
|
||||
|
||||
my $default_command;
|
||||
{
|
||||
local $XCAT::Test::FastOmapiRunner::COMMAND_DIRECTORY = $command_directory;
|
||||
@@ -183,17 +172,23 @@ local $ENV{OMAPI_TEST_CAPTURE} = $capture;
|
||||
my $parent_stdout = File::Spec->catfile( $workspace, 'parent-stdout' );
|
||||
my $parent_stderr = File::Spec->catfile( $workspace, 'parent-stderr' );
|
||||
my $success_status;
|
||||
my ( $success_ok, $success_error );
|
||||
{
|
||||
open( my $saved_stdout, '>&', \*STDOUT ) or die "Unable to preserve stdout: $!";
|
||||
open( my $saved_stderr, '>&', \*STDERR ) or die "Unable to preserve stderr: $!";
|
||||
open( STDOUT, '>', $parent_stdout ) or die "Unable to create $parent_stdout: $!";
|
||||
open( STDERR, '>', $parent_stderr ) or die "Unable to create $parent_stderr: $!";
|
||||
$success_status = XCAT::Test::FastOmapiRunner->run_command_file( $command_file, $success );
|
||||
$success_ok = eval {
|
||||
$success_status = XCAT::Test::FastOmapiRunner->run_command_file( $command_file, $success );
|
||||
1;
|
||||
};
|
||||
$success_error = $@;
|
||||
open( STDOUT, '>&', $saved_stdout ) or die "Unable to restore stdout: $!";
|
||||
open( STDERR, '>&', $saved_stderr ) or die "Unable to restore stderr: $!";
|
||||
close($saved_stdout) or die "Unable to close preserved stdout: $!";
|
||||
close($saved_stderr) or die "Unable to close preserved stderr: $!";
|
||||
}
|
||||
die $success_error unless $success_ok;
|
||||
is( $success_status, 'completed', 'a normally exiting command is reported as completed' );
|
||||
|
||||
open( my $capture_fh, '<', $capture ) or die "Unable to read $capture: $!";
|
||||
|
||||
@@ -3,7 +3,7 @@ use strict;
|
||||
use warnings;
|
||||
|
||||
use FindBin;
|
||||
use lib "$FindBin::Bin/../../xCAT-server/lib";
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
use lib "$FindBin::Bin/../../xCAT-server/lib/perl";
|
||||
use lib "$FindBin::Bin/../../perl-xCAT";
|
||||
|
||||
@@ -15,16 +15,13 @@ use IPC::Open3;
|
||||
use Symbol qw(gensym);
|
||||
use Test::More;
|
||||
|
||||
use XCAT::Test::File qw(repo_path);
|
||||
use xCAT::DHCP::OmapiRunner;
|
||||
|
||||
$ENV{XCATCFG} ||= 'SQLite:/tmp';
|
||||
|
||||
my $source_dhcp_plugin = "$FindBin::Bin/../../xCAT-server/lib/xcat/plugins/dhcp.pm";
|
||||
if ( -f $source_dhcp_plugin ) {
|
||||
require $source_dhcp_plugin;
|
||||
} else {
|
||||
require xCAT_plugin::dhcp;
|
||||
}
|
||||
my $source_dhcp_plugin = repo_path('xCAT-server/lib/xcat/plugins/dhcp.pm');
|
||||
require $source_dhcp_plugin;
|
||||
|
||||
sub write_file {
|
||||
my ( $path, $contents ) = @_;
|
||||
@@ -74,24 +71,13 @@ my $plugin_command_directory = File::Spec->catdir( $workspace, 'plugin-commands'
|
||||
unlink $command->{path} or die "Unable to remove $command->{path}: $!";
|
||||
}
|
||||
|
||||
{
|
||||
my @writer;
|
||||
{
|
||||
no warnings qw(once redefine);
|
||||
local *xCAT::DHCP::OmapiRunner::open_command_file = sub { return; };
|
||||
@writer = xCAT_plugin::dhcp::_open_omshell_writer(
|
||||
{ omshell_path => '/usr/bin/omshell' }
|
||||
);
|
||||
}
|
||||
is_deeply( \@writer, [], 'makedhcp propagates command-file creation failure' );
|
||||
}
|
||||
|
||||
foreach my $status (qw(completed terminated killed fork_error)) {
|
||||
subtest "makedhcp handles $status" => sub {
|
||||
my $command = xCAT::DHCP::OmapiRunner->open_command_file($plugin_command_directory);
|
||||
my @logs;
|
||||
my @run_arguments;
|
||||
my $run_contents;
|
||||
my $writer_closed_before_run;
|
||||
|
||||
print { $command->{handle} } "connect\n" or die "Unable to write $command->{path}: $!";
|
||||
|
||||
@@ -100,6 +86,7 @@ foreach my $status (qw(completed terminated killed fork_error)) {
|
||||
local *xCAT::DHCP::OmapiRunner::run_command_file = sub {
|
||||
my ( $class, @arguments ) = @_;
|
||||
@run_arguments = @arguments;
|
||||
$writer_closed_before_run = !defined fileno( $command->{handle} );
|
||||
$run_contents = read_file( $arguments[0] );
|
||||
return $status;
|
||||
};
|
||||
@@ -115,6 +102,7 @@ foreach my $status (qw(completed terminated killed fork_error)) {
|
||||
[ $command->{path}, '/usr/bin/omshell' ],
|
||||
'makedhcp passes the command path and executable in order'
|
||||
);
|
||||
ok( $writer_closed_before_run, 'makedhcp closes the command file before the runner reads it' );
|
||||
is( $run_contents, "connect\n", 'makedhcp flushes the command file before the runner reads it' );
|
||||
ok( !-e $command->{path}, 'makedhcp removes the command file after the runner returns' );
|
||||
if ( $status eq 'completed' ) {
|
||||
@@ -143,7 +131,11 @@ use strict;
|
||||
use warnings;
|
||||
|
||||
sub new_backend {
|
||||
return {};
|
||||
return bless {}, __PACKAGE__;
|
||||
}
|
||||
|
||||
sub name {
|
||||
return 'isc';
|
||||
}
|
||||
|
||||
1;
|
||||
@@ -200,6 +192,10 @@ use Errno qw(EAGAIN);
|
||||
use File::Temp qw(tempfile);
|
||||
|
||||
sub open_command_file {
|
||||
my ( $class, @arguments ) = @_;
|
||||
open( my $arguments, '>', $ENV{OMAPI_TEST_OPEN_ARGUMENTS} ) or die $!;
|
||||
print {$arguments} join("\n", @arguments) or die $!;
|
||||
close($arguments) or die $!;
|
||||
my ( $handle, $path ) = tempfile(
|
||||
'omshell.XXXXXX',
|
||||
DIR => $ENV{OMAPI_TEST_COMMAND_DIRECTORY},
|
||||
@@ -230,17 +226,19 @@ sub run_command_file {
|
||||
MODULE
|
||||
);
|
||||
|
||||
my $dhcpop = File::Spec->catfile( $FindBin::Bin, '..', '..', 'xCAT-server', 'share', 'xcat', 'tools', 'dhcpop' );
|
||||
my $dhcpop = repo_path('xCAT-server/share/xcat/tools/dhcpop');
|
||||
|
||||
sub run_dhcpop {
|
||||
my ($status) = @_;
|
||||
my $capture = File::Spec->catfile( $workspace, "dhcpop-$status-capture" );
|
||||
my $open_arguments = File::Spec->catfile( $workspace, "dhcpop-$status-open-arguments" );
|
||||
my $omshell_capture = File::Spec->catfile( $workspace, "dhcpop-$status-omshell" );
|
||||
my $path_record = File::Spec->catfile( $workspace, "dhcpop-$status-path" );
|
||||
|
||||
local $ENV{XCATROOT} = $fake_root;
|
||||
local $ENV{OMAPI_TEST_CAPTURE} = $capture;
|
||||
local $ENV{OMAPI_TEST_COMMAND_DIRECTORY} = $dhcpop_command_directory;
|
||||
local $ENV{OMAPI_TEST_OPEN_ARGUMENTS} = $open_arguments;
|
||||
local $ENV{OMAPI_TEST_OMSHELL_CAPTURE} = $omshell_capture;
|
||||
local $ENV{OMAPI_TEST_PATH_RECORD} = $path_record;
|
||||
local $ENV{OMAPI_TEST_STATUS} = $status;
|
||||
@@ -265,12 +263,13 @@ sub run_dhcpop {
|
||||
my $command_path = read_file($path_record);
|
||||
|
||||
return {
|
||||
command => read_file($capture),
|
||||
command_path => $command_path,
|
||||
exit_status => $exit_status,
|
||||
omshell_path => read_file($omshell_capture),
|
||||
stderr => $stderr,
|
||||
stdout => $stdout,
|
||||
command => read_file($capture),
|
||||
command_path => $command_path,
|
||||
exit_status => $exit_status,
|
||||
open_arguments => read_file($open_arguments),
|
||||
omshell_path => read_file($omshell_capture),
|
||||
stderr => $stderr,
|
||||
stdout => $stdout,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -289,6 +288,7 @@ foreach my $status (qw(completed terminated killed fork_error)) {
|
||||
my $result = run_dhcpop($status);
|
||||
|
||||
is( $result->{command}, $expected_command, 'dhcpop sends the expected OMAPI commands' );
|
||||
is( $result->{open_arguments}, '', 'dhcpop uses the runner default command directory' );
|
||||
is( $result->{omshell_path}, '/usr/bin/omshell', 'dhcpop passes the configured executable' );
|
||||
is( $result->{stdout}, '', 'dhcpop does not write to stdout' );
|
||||
if ( $status eq 'fork_error' ) {
|
||||
|
||||
Reference in New Issue
Block a user