pve-installer/test/run-command.pl
Christoph Heiss 152bbef439 sys: command: factor out kill() + waitpid() from run_command()
This moves the kill() + waitpid() combo into a separate subroutine,
avoiding open-coding that sequence. wait_for_process() also handles
properly unkillable process (e.g. in D-state) and avoids completely
locking up the installer in such cases. See [0].

For the latter case, a timeout exists (with a default of 5 seconds) in
which to wait for the process to exit after sending an optional
TERM/KILL signal.

Also while at it, add a few basic tests for run_command().

[0] https://lists.proxmox.com/pipermail/pve-devel/2024-February/061697.html

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
2024-02-23 14:19:56 +01:00

36 lines
860 B
Perl
Executable File

#!/usr/bin/perl
use strict;
use warnings;
use File::Temp;
use Test::More;
use Proxmox::Sys::Command qw(run_command CMD_FINISHED);
use Proxmox::Sys::File qw(file_read_all);
use Proxmox::UI;
my $log_file = File::Temp->new();
Proxmox::Log::init($log_file->filename);
Proxmox::UI::init_stdio();
is(run_command('echo test'), "test\n", 'basic usage');
is(run_command('echo test', undef, undef, 1), 0, 'system()-mode');
my $ret = run_command('bash -c "echo test; sleep 1000; echo test"', sub {
my $line = shift;
is($line, 'test', 'using CMD_FINISHED - produced correct log line');
return CMD_FINISHED;
});
is($ret, '', 'using CMD_FINISHED');
# Check the log for errors/warnings
my $log = file_read_all($log_file->filename);
ok($log !~ m/(WARN|ERROR): /, 'no warnings or errors logged');
print $log if $log =~ m/(WARN|ERROR): /;
done_testing();