Test pt-stalk. Fix disk space+margin check. Fix checking --iterations. Add EXIT_REASON. Fix grepping status var.

This commit is contained in:
Daniel Nichter
2011-12-15 14:28:10 -07:00
parent ce63ce77cd
commit 78603f5d03
2 changed files with 164 additions and 15 deletions

View File

@@ -646,6 +646,7 @@ lock_waits() {
# ########################################################################### # ###########################################################################
# Global variables # Global variables
# ########################################################################### # ###########################################################################
EXIT_REASON=""
TOOL=`basename $0` TOOL=`basename $0`
OKTORUN=1 OKTORUN=1
ITER=1 ITER=1
@@ -709,7 +710,7 @@ set_trg_func() {
trg_status() { trg_status() {
local var=$1 local var=$1
mysqladmin $EXT_ARGV extended-status | grep "$OPT_VARIABLE" | awk '{print $4}' mysqladmin $EXT_ARGV extended-status | grep "$OPT_VARIABLE " | awk '{print $4}'
} }
trg_processlist() { trg_processlist() {
@@ -729,10 +730,12 @@ trg_magic() {
oktorun() { oktorun() {
if [ $OKTORUN -eq 0 ]; then if [ $OKTORUN -eq 0 ]; then
EXIT_REASON="OKTORUN is false, exiting"
return 1 # stop running return 1 # stop running
fi fi
if [ -n "$OPT_ITERATIONS" ] && [ $ITER -ge $OPT_ITERATIONS ]; then if [ -n "$OPT_ITERATIONS" ] && [ $ITER -gt $OPT_ITERATIONS ]; then
EXIT_REASON="No more iterations, exiting"
return 1 # stop running return 1 # stop running
fi fi
@@ -808,15 +811,14 @@ stalk() {
# is also checked every interval while collecting. # is also checked every interval while collecting.
local margin="20" # default 20M margin, unless: local margin="20" # default 20M margin, unless:
if [ -n "$last_prefix" ]; then if [ -n "$last_prefix" ]; then
margin=$(du -mc $d/$last_prefix-* | tail -n 1 | awk '{print $1'}) margin=$(du -mc $OPT_DEST/$last_prefix-* | tail -n 1 | awk '{print $1'})
fi fi
disk_space $d > $d/$p-disk-space # Get real disk space. disk_space $OPT_DEST > $OPT_DEST/$prefix-disk-space
check_disk_space \ # Then check it plus... check_disk_space \
$d/$p-disk-space \ $OPT_DEST/$prefix-disk-space \
"$OPT_DISK_BYTE_LIMIT" \ "$OPT_DISK_BYTE_LIMIT" \
"$OPT_DISK_PCT_LIMIT" \ "$OPT_DISK_PCT_LIMIT" \
"$margin" \ # ... the margin. "$margin" # real used MB + margin MB
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
# There should be enough disk space, so collect. # There should be enough disk space, so collect.
log "$msg" >> "$OPT_DEST/$prefix-trigger" log "$msg" >> "$OPT_DEST/$prefix-trigger"
@@ -893,6 +895,7 @@ main() {
rm_tmpdir rm_tmpdir
remove_pid_file "$OPT_PID" remove_pid_file "$OPT_PID"
log "$EXIT_REASON"
log "$0 exit status $EXIT_STATUS" log "$0 exit status $EXIT_STATUS"
exit $EXIT_STATUS exit $EXIT_STATUS
} }

View File

@@ -9,16 +9,162 @@ BEGIN {
use strict; use strict;
use warnings FATAL => 'all'; use warnings FATAL => 'all';
use English qw(-no_match_vars); use English qw(-no_match_vars);
use Test::More tests => 1; use Test::More;
use PerconaTest; use PerconaTest;
use DSNParser;
use Sandbox;
TODO: { my $dp = new DSNParser(opts=>$dsn_opts);
local $TODO = "Test pt-stalk"; my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
ok(1, 'ok'); my $dbh = $sb->get_dbh_for('master');
};
if ( !$dbh ) {
plan skip_all => 'Cannot connect to sandbox master';
}
else {
plan tests => 14;
}
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $pid_file = "/tmp/pt-stalk.pid.$PID";
my $log_file = "/tmp/pt-stalk.log.$PID";
my $dest = "/tmp/pt-stalk.collect.$PID";
my $pid;
diag(`rm $pid_file 2>/dev/null`);
diag(`rm $log_file 2>/dev/null`);
diag(`rm -rf $dest 2>/dev/null`);
# ###########################################################################
# Test that it won't run if can't connect to MySQL.
# ###########################################################################
my $retval = system("$trunk/bin/pt-stalk >$log_file 2>&1");
my $output = `cat $log_file`;
like(
$output,
qr/Cannot connect to MySQL/,
"Cannot connect to MySQL"
);
is(
$retval >> 8,
1,
"Exit 1"
);
# ###########################################################################
# Test that it runs and dies normally.
# ###########################################################################
diag(`rm $pid_file 2>/dev/null`);
diag(`rm $log_file 2>/dev/null`);
diag(`rm -rf $dest 2>/dev/null`);
$retval = system("$trunk/bin/pt-stalk --pid $pid_file --log $log_file --dest $dest -- --defaults-file=$cnf");
is(
$retval >> 8,
0,
"Parent exit 0"
);
PerconaTest::wait_for_files($pid_file, $log_file);
ok(
-f $pid_file,
"Creates PID file"
);
ok(
-f $log_file,
"Creates log file"
);
sleep 1;
ok(
-d $dest,
"Creates --dest (collect) dir"
);
chomp($pid = `cat $pid_file`);
$retval = system("kill -0 $pid");
is(
$retval >> 0,
0,
"pt-stalk is running ($pid)"
);
$output = `cat $log_file`;
like(
$output,
qr/Check results: Threads_running=\d+, matched=no, cycles_true=0/,
"Check results logged"
);
$retval = system("kill $pid 2>/dev/null");
is(
$retval >> 0,
0,
"Killed pt-stalk"
);
sleep 1;
ok(
! -f $pid_file,
"Removes PID file"
);
$output = `cat $log_file`;
like(
$output,
qr/Caught signal, exiting/,
"Caught signal logged"
);
# ###########################################################################
# Test collect.
# ###########################################################################
diag(`rm $pid_file 2>/dev/null`);
diag(`rm $log_file 2>/dev/null`);
diag(`rm $dest/* 2>/dev/null`);
# We'll have to watch Uptime since it's the only status var that's going
# to be predictable.
my (undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'");
my $threshold = $uptime + 2;
$retval = system("$trunk/bin/pt-stalk --no-daemonize --iterations 1 --dest $dest --variable Uptime --threshold $threshold --cycles 2 --run-time 2 -- --defaults-file=$cnf >$log_file 2>&1");
sleep 3;
$output = `cat $dest/*-trigger`;
like(
$output,
qr/Check results: Uptime=\d+, matched=yes, cycles_true=2/,
"Collect triggered"
);
chomp($output = `cat $dest/*-df | grep -c '^TS'`);
is(
$output,
2,
"Collect ran for --run-time"
);
$output = `ps x | grep -v grep | grep 'pt-stalk pt-stalk --no-daemonize --iterations 1 --dest $dest'`;
is(
$output,
"",
"pt-stalk is not running"
);
# ############################################################################# # #############################################################################
# Done. # Done.
# ############################################################################# # #############################################################################
diag(`rm $pid_file 2>/dev/null`);
diag(`rm $log_file 2>/dev/null`);
diag(`rm -rf $dest 2>/dev/null`);
exit; exit;