Stabilize t/lib/Daemon.t. Add PerconaTest::get_cmd_pid().

This commit is contained in:
Daniel Nichter
2013-08-10 08:27:09 -07:00
parent a87b62b494
commit 8ec7ccdbe7
2 changed files with 61 additions and 44 deletions

View File

@@ -819,6 +819,20 @@ sub tables_used {
return [ sort keys %tables ]; return [ sort keys %tables ];
} }
sub get_cmd_pid {
my $cmd = shift;
$cmd =~ s/\./\\./g;
$cmd =~ s/-/\\-/g;
my $output = `ps wx | grep -v grep | grep '$cmd'`;
my @cmds = $output =~ m/\n/g;
if ( @cmds > 1 ) {
die "Found " . scalar @cmds . " commands matching '$cmd': @cmds";
}
my ($pid) = $output =~ m/^\s*(\d+)/;
return wantarray ? ($pid, $output) : $pid;
}
sub can_load_data { sub can_load_data {
my $output = `/tmp/12345/use -e "SELECT * FROM percona_test.load_data" 2>/dev/null`; my $output = `/tmp/12345/use -e "SELECT * FROM percona_test.load_data" 2>/dev/null`;
return ($output || '') =~ /1/; return ($output || '') =~ /1/;

View File

@@ -31,13 +31,14 @@ sub rm_tmp_files() {
# Test that it daemonizes, creates a PID file, and removes that PID file. # Test that it daemonizes, creates a PID file, and removes that PID file.
# ############################################################################ # ############################################################################
my $ret_val = system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1"); my $ret_val = system("$cmd 3 --daemonize --pid $pid_file >/dev/null 2>&1");
die 'Cannot test Daemon.pm because t/daemonizes.pl is not working' die 'Cannot test Daemon.pm because t/daemonizes.pl is not working'
unless $ret_val == 0; unless $ret_val == 0;
PerconaTest::wait_for_files($pid_file); PerconaTest::wait_for_files($pid_file)
or die "$cmd did not create $pid_file";
my $output = `ps wx | grep '$cmd 5' | grep -v grep`; my ($pid, $output) = PerconaTest::get_cmd_pid("$cmd 3");
like( like(
$output, $output,
@@ -50,7 +51,6 @@ ok(
'Creates PID file' 'Creates PID file'
); );
my ($pid) = $output =~ /^\s*(\d+)\s+/;
$output = slurp_file($pid_file); $output = slurp_file($pid_file);
chomp($output) if $output; chomp($output) if $output;
@@ -60,12 +60,16 @@ is(
'PID file has correct PID' 'PID file has correct PID'
); );
# Wait until the process goes away SKIP: {
PerconaTest::wait_until(sub { !kill(0, $pid) }); skip "Previous tests failed", 1 unless $pid;
ok(
! -f $pid_file, # Wait until the process goes away
'Removes PID file upon exit' PerconaTest::wait_until(sub { !kill(0, $pid) });
); ok(
! -f $pid_file,
'Removes PID file upon exit'
);
}
# ############################################################################ # ############################################################################
# Check that STDOUT can be redirected # Check that STDOUT can be redirected
@@ -132,12 +136,14 @@ unlike(
# ########################################################################## # ##########################################################################
rm_tmp_files(); rm_tmp_files();
SKIP: { SKIP: {
skip 'No /proc', 2 unless -d '/proc'; skip 'No /proc', 1 unless -d '/proc';
skip 'No fd in /proc', 2 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0"; skip 'No fd in /proc', 1 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0";
system("$cmd 5 --daemonize --pid $pid_file --log $log_file"); system("$cmd 10 --daemonize --pid $pid_file --log $log_file");
PerconaTest::wait_for_files($pid_file); PerconaTest::wait_for_files($pid_file)
or die "$cmd did not create $pid_file";
chomp($pid = slurp_file($pid_file)); chomp($pid = slurp_file($pid_file));
die "$pid_file is empty" unless $pid;
my $proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0" my $proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0"
: -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0" : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0"
: die "Cannot find fd 0 symlink in /proc/$pid"; : die "Cannot find fd 0 symlink in /proc/$pid";
@@ -150,26 +156,28 @@ SKIP: {
'Reopens STDIN to /dev/null' 'Reopens STDIN to /dev/null'
); );
SKIP: { PerconaTest::kill_program(pid => $pid);
skip "-t is not reliable", 1;
rm_tmp_files(); # SKIP: {
system("echo foo | $cmd 5 --daemonize --pid $pid_file --log $log_file"); # skip "-t is not reliable", 1;
PerconaTest::wait_for_files($pid_file, $log_file); # rm_tmp_files();
chomp($pid = slurp_file($pid_file)); # system("echo foo | $cmd 5 --daemonize --pid $pid_file --log $log_file");
$proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0" # PerconaTest::wait_for_files($pid_file, $log_file);
: -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0" # chomp($pid = slurp_file($pid_file));
: die "Cannot find fd 0 symlink in /proc/$pid"; # $proc_fd_0 = -l "/proc/$pid/0" ? "/proc/$pid/0"
PTDEVDEBUG && PerconaTest::_d('pid_file', $pid_file, # : -l "/proc/$pid/fd/0" ? "/proc/$pid/fd/0"
'pid', $pid, 'proc_fd_0', $proc_fd_0, `ls -l $proc_fd_0`); # : die "Cannot find fd 0 symlink in /proc/$pid";
$stdin = readlink $proc_fd_0; # PTDEVDEBUG && PerconaTest::_d('pid_file', $pid_file,
like( # 'pid', $pid, 'proc_fd_0', $proc_fd_0, `ls -l $proc_fd_0`);
$stdin, # $stdin = readlink $proc_fd_0;
qr/pipe/, # like(
'Does not reopen STDIN to /dev/null when piped', # $stdin,
); # qr/pipe/,
rm_tmp_files(); # 'Does not reopen STDIN to /dev/null when piped',
} # );
}; # rm_tmp_files();
# }
}
# ########################################################################## # ##########################################################################
# Issue 419: Daemon should check wether process with pid obtained from # Issue 419: Daemon should check wether process with pid obtained from
@@ -181,7 +189,7 @@ PerconaTest::wait_for_files($pid_file);
chomp($pid = slurp_file($pid_file)); chomp($pid = slurp_file($pid_file));
kill 9, $pid; kill 9, $pid;
sleep 0.25; sleep 0.25;
$output = `ps wx | grep '^[ ]*$pid' | grep -v grep`; (undef, $output) = PerconaTest::get_cmd_pid("$cmd 5");
unlike( unlike(
$output, $output,
qr/daemonize/, qr/daemonize/,
@@ -196,9 +204,8 @@ my (undef, $tempfile) = tempfile();
system("$cmd 5 --daemonize --log $log_file --pid $pid_file > $tempfile 2>&1"); system("$cmd 5 --daemonize --log $log_file --pid $pid_file > $tempfile 2>&1");
PerconaTest::wait_for_files($log_file, $pid_file, $tempfile); PerconaTest::wait_for_files($log_file, $pid_file, $tempfile);
my $new_pid;
$output = `ps wx | grep '$cmd 5' | grep -v grep`; ($new_pid, $output) = PerconaTest::get_cmd_pid("$cmd 5");
chomp(my $new_pid = slurp_file($pid_file));
like( like(
$output, $output,
@@ -217,7 +224,7 @@ ok(
'Overwrites PID file with new PID (issue 419)' 'Overwrites PID file with new PID (issue 419)'
); );
PerconaTest::wait_until(sub { !-e $pid_file }); PerconaTest::wait_until(sub { !-f $pid_file });
ok( ok(
!-f $pid_file, !-f $pid_file,
'Re-used PID file still removed (issue 419)' 'Re-used PID file still removed (issue 419)'
@@ -239,11 +246,7 @@ like(
'Says that PID is running (issue 419)' 'Says that PID is running (issue 419)'
); );
if ( $pid ) { PerconaTest::kill_program(pid => $pid);
kill 9, $pid;
}
sleep 0.25;
rm_tmp_files(); rm_tmp_files();
# ############################################################################# # #############################################################################