mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-11 05:29:30 +00:00
Test pt-stalk. Fix disk space+margin check. Fix checking --iterations. Add EXIT_REASON. Fix grepping status var.
This commit is contained in:
17
bin/pt-stalk
17
bin/pt-stalk
@@ -646,6 +646,7 @@ lock_waits() {
|
||||
# ###########################################################################
|
||||
# Global variables
|
||||
# ###########################################################################
|
||||
EXIT_REASON=""
|
||||
TOOL=`basename $0`
|
||||
OKTORUN=1
|
||||
ITER=1
|
||||
@@ -729,10 +730,12 @@ trg_magic() {
|
||||
|
||||
oktorun() {
|
||||
if [ $OKTORUN -eq 0 ]; then
|
||||
EXIT_REASON="OKTORUN is false, exiting"
|
||||
return 1 # stop running
|
||||
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
|
||||
fi
|
||||
|
||||
@@ -808,15 +811,14 @@ stalk() {
|
||||
# is also checked every interval while collecting.
|
||||
local margin="20" # default 20M margin, unless:
|
||||
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
|
||||
disk_space $d > $d/$p-disk-space # Get real disk space.
|
||||
check_disk_space \ # Then check it plus...
|
||||
$d/$p-disk-space \
|
||||
disk_space $OPT_DEST > $OPT_DEST/$prefix-disk-space
|
||||
check_disk_space \
|
||||
$OPT_DEST/$prefix-disk-space \
|
||||
"$OPT_DISK_BYTE_LIMIT" \
|
||||
"$OPT_DISK_PCT_LIMIT" \
|
||||
"$margin" \ # ... the margin.
|
||||
|
||||
"$margin" # real used MB + margin MB
|
||||
if [ $? -eq 0 ]; then
|
||||
# There should be enough disk space, so collect.
|
||||
log "$msg" >> "$OPT_DEST/$prefix-trigger"
|
||||
@@ -893,6 +895,7 @@ main() {
|
||||
rm_tmpdir
|
||||
remove_pid_file "$OPT_PID"
|
||||
|
||||
log "$EXIT_REASON"
|
||||
log "$0 exit status $EXIT_STATUS"
|
||||
exit $EXIT_STATUS
|
||||
}
|
||||
|
@@ -9,16 +9,162 @@ BEGIN {
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use Test::More tests => 1;
|
||||
use Test::More;
|
||||
|
||||
use PerconaTest;
|
||||
use DSNParser;
|
||||
use Sandbox;
|
||||
|
||||
TODO: {
|
||||
local $TODO = "Test pt-stalk";
|
||||
ok(1, 'ok');
|
||||
};
|
||||
my $dp = new DSNParser(opts=>$dsn_opts);
|
||||
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
|
||||
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.
|
||||
# #############################################################################
|
||||
diag(`rm $pid_file 2>/dev/null`);
|
||||
diag(`rm $log_file 2>/dev/null`);
|
||||
diag(`rm -rf $dest 2>/dev/null`);
|
||||
exit;
|
||||
|
Reference in New Issue
Block a user