Files
percona-toolkit/t/pt-query-digest/daemon.t
2011-08-02 15:14:06 -06:00

66 lines
2.2 KiB
Perl

#!/usr/bin/env perl
BEGIN {
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 6;
use PerconaTest;
use Sandbox;
use DSNParser;
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');
my $output;
# #########################################################################
# Issue 391: Add --pid option to all scripts
# #########################################################################
`touch /tmp/pt-script.pid`;
$output = `$trunk/bin/pt-query-digest $trunk/commont/t/samples/slow002.txt --pid /tmp/pt-script.pid 2>&1`;
like(
$output,
qr{PID file /tmp/pt-script.pid already exists},
'Dies if PID file already exists (--pid without --daemonize) (issue 391)'
);
`rm -rf /tmp/pt-script.pid`;
# #########################################################################
# Daemonizing and pid creation
# #########################################################################
SKIP: {
skip "Cannot connect to sandbox master", 5 unless $dbh;
my $cmd = "$trunk/bin/pt-query-digest --daemonize --pid /tmp/pt-query-digest.pid --processlist h=127.1,P=12345,u=msandbox,p=msandbox --log /dev/null";
`$cmd`;
$output = `ps -eaf | grep -v grep | grep pt-query-digest`;
like($output, qr/$cmd/, 'It is running');
ok(-f '/tmp/pt-query-digest.pid', 'PID file created');
my ($pid) = $output =~ /\s+(\d+)\s+/;
$output = `cat /tmp/pt-query-digest.pid`;
is($output, $pid, 'PID file has correct PID');
kill 15, $pid;
sleep 1;
$output = `ps -eaf | grep pt-query-digest | grep daemonize`;
unlike($output, qr/$trunk\/pt-query-digest\/pt-query-digest/, 'It is not running');
ok(
!-f '/tmp/pt-query-digest.pid',
'Removes its PID file'
);
};
# #############################################################################
# Done.
# #############################################################################
exit;