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