Merge lp:~percona-toolkit-dev/percona-toolkit/stabilize-test-suite r130.

This commit is contained in:
Daniel Nichter
2012-02-27 17:08:15 -07:00
39 changed files with 484 additions and 337 deletions

View File

@@ -55,11 +55,12 @@ sub daemonize {
PTDEBUG && _d('About to fork and daemonize');
defined (my $pid = fork()) or die "Cannot fork: $OS_ERROR";
if ( $pid ) {
PTDEBUG && _d('I am the parent and now I die');
PTDEBUG && _d('Parent PID', $PID, 'exiting after forking child PID',$pid);
exit;
}
# I'm daemonized now.
PTDEBUG && _d('Daemonizing child PID', $PID);
$self->{PID_owner} = $PID;
$self->{child} = 1;
@@ -70,15 +71,20 @@ sub daemonize {
$OUTPUT_AUTOFLUSH = 1;
# Only reopen STDIN to /dev/null if it's a tty. It may be a pipe,
# in which case we don't want to break it.
if ( -t STDIN ) {
close STDIN;
open STDIN, '/dev/null'
or die "Cannot reopen STDIN to /dev/null: $OS_ERROR";
}
# We used to only reopen STDIN to /dev/null if it's a tty because
# otherwise it may be a pipe, in which case we didn't want to break
# it. However, Perl -t is not reliable. This is true and false on
# various boxes even when the same code is ran, or it depends on if
# the code is ran via cron, Jenkins, etc. Since there should be no
# sane reason to `foo | pt-tool --daemonize` for a tool that reads
# STDIN, we now just always close STDIN.
PTDEBUG && _d('Redirecting STDIN to /dev/null');
close STDIN;
open STDIN, '/dev/null'
or die "Cannot reopen STDIN to /dev/null: $OS_ERROR";
if ( $self->{log_file} ) {
PTDEBUG && _d('Redirecting STDOUT and STDERR to', $self->{log_file});
close STDOUT;
open STDOUT, '>>', $self->{log_file}
or die "Cannot open log file $self->{log_file}: $OS_ERROR";
@@ -93,18 +99,21 @@ sub daemonize {
}
else {
if ( -t STDOUT ) {
PTDEBUG && _d('No log file and STDOUT is a terminal;',
'redirecting to /dev/null');
close STDOUT;
open STDOUT, '>', '/dev/null'
or die "Cannot reopen STDOUT to /dev/null: $OS_ERROR";
}
if ( -t STDERR ) {
PTDEBUG && _d('No log file and STDERR is a terminal;',
'redirecting to /dev/null');
close STDERR;
open STDERR, '>', '/dev/null'
or die "Cannot reopen STDERR to /dev/null: $OS_ERROR";
}
}
PTDEBUG && _d('I am the child and now I live daemonized');
return;
}

View File

@@ -31,10 +31,10 @@ package PerconaTest;
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
use constant PTDEVDEBUG => $ENV{PTDEVDEBUG} || 0;
use Test::More;
use Time::HiRes qw(sleep);
use Time::HiRes qw(sleep time);
use POSIX qw(signal_h);
use Data::Dumper;
$Data::Dumper::Indent = 1;
@@ -220,14 +220,16 @@ sub parse_file {
# Wait until code returns true.
sub wait_until {
my ( $code, $t, $max_t ) = @_;
$t ||= .5;
$max_t ||= 10;
$t ||= .25;
$max_t ||= 5;
my $slept = 0;
while ( $slept <= $max_t ) {
return 1 if $code->();
PTDEVDEBUG && _d('wait_until sleeping', $t);
sleep $t;
$slept += $t;
PTDEVDEBUG && _d('wait_until slept', $slept, 'of', $max_t);
}
return 0;
}
@@ -262,14 +264,18 @@ sub wait_for_table {
sub {
my $r;
eval { $r = $dbh->selectrow_arrayref($sql); };
return 0 if $EVAL_ERROR;
if ( $where ) {
return 0 unless $r && @$r;
if ( $EVAL_ERROR ) {
PTDEVDEBUG && _d('Waiting on', $dbh, 'for table', $tbl,
'error:', $EVAL_ERROR);
return 0;
}
if ( $where && (!$r || !scalar @$r) ) {
PTDEVDEBUG && _d('Waiting on', $dbh, 'for table', $tbl,
'WHERE', $where);
return 0;
}
return 1;
},
0.25,
15,
);
}
@@ -278,12 +284,13 @@ sub wait_for_files {
return wait_until(
sub {
foreach my $file (@files) {
return 0 if ! -f $file;
if ( ! -f $file ) {
PTDEVDEBUG && _d('Waiting for file', $file);
return 0;
}
}
return 1;
},
0.25,
15,
);
}
@@ -323,17 +330,18 @@ sub test_log_parser {
close $fh;
};
my ($base_file_name) = $args{file} =~ m/([^\/]+)$/;
is(
$EVAL_ERROR,
'',
"No error on $args{file}"
"$base_file_name: no errors"
);
if ( defined $args{result} ) {
is_deeply(
\@e,
$args{result},
$args{file}
"$base_file_name: results"
) or print "Got: ", Dumper(\@e);
}
@@ -341,7 +349,7 @@ sub test_log_parser {
is(
scalar @e,
$args{num_events},
"$args{file} num_events"
"$base_file_name: $args{num_events} events"
);
}
@@ -379,17 +387,18 @@ sub test_protocol_parser {
close $fh;
};
my ($base_file_name) = $args{file} =~ m/([^\/]+)$/;
is(
$EVAL_ERROR,
'',
"No error on $args{file}"
"$base_file_name: no errors"
);
if ( defined $args{result} ) {
is_deeply(
\@e,
$args{result},
$args{file} . ($args{desc} ? ": $args{desc}" : '')
"$base_file_name: " . ($args{desc} || "results")
) or print "Got: ", Dumper(\@e);
}
@@ -397,7 +406,7 @@ sub test_protocol_parser {
is(
scalar @e,
$args{num_events},
"$args{file} num_events"
"$base_file_name: $args{num_events} events"
);
}
@@ -617,6 +626,15 @@ sub get_master_binlog_pos {
return $ms->{position};
}
sub _d {
my ($package, undef, $line) = caller 0;
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
map { defined $_ ? $_ : 'undef' }
@_;
my $t = sprintf '%.3f', time;
print STDERR "# $package:$line $PID $t ", join(' ', @_), "\n";
}
1;
}
# ###########################################################################

View File

@@ -50,9 +50,10 @@ check_disk_space() {
local bytes_margin="${4:-0}"
# Real/actual bytes used and bytes free.
local used_bytes=$(cat "$file" | awk '/^\//{printf("%d",$3 * 1024)}');
local free_bytes=$(cat "$file" | awk '/^\//{printf("%d",$4 * 1024)}');
local pct_used=$(cat "$file" | awk '/^\//{print $5}' | sed -e 's/%//g');
local used_bytes=$(perl -ane 'm!^/! && print $F[2] * 1024' "$file")
local free_bytes=$(perl -ane 'm!^/! && print $F[3] * 1024' "$file")
local pct_used=$(perl -ane 'm!^/! && print ($F[4] =~ m/(\d+)/)' "$file")
local pct_free=$((100 - $pct_used))
# Report the real values to the user.
@@ -63,7 +64,7 @@ check_disk_space() {
if [ $bytes_margin -gt 0 ]; then
used_bytes=$(($used_bytes + $bytes_margin))
free_bytes=$(($free_bytes - $bytes_margin))
pct_used=$(awk "BEGIN { printf(\"%d\", ($used_bytes/($used_bytes + $free_bytes)) * 100) }")
pct_used=$(perl -e "print int(($used_bytes/($used_bytes + $free_bytes)) * 100)")
pct_free=$((100 - $pct_used))
fi

71
sandbox/jenkins-test Executable file
View File

@@ -0,0 +1,71 @@
#!/bin/sh
######################################
# Source our alt_cmds.sh for _seq(). #
######################################
. lib/bash/alt_cmds.sh
##############
# Set modes. #
##############
set +u
set -e
set -x
##################################
# Check for needed Perl modules. #
##################################
util/check-dev-env
#####################################
# Install barebones MySQL binaries. #
#####################################
if ! [ -d "mysql-${MYSQL}-barebones" ]; then
wget -q http://hackmysql.com/mysql-${MYSQL}-barebones.tar.gz
tar xvfz mysql-${MYSQL}-barebones.tar.gz
fi
##########################
# Set required env vars. #
##########################
export PERCONA_TOOLKIT_BRANCH="$PWD"
export PERCONA_TOOLKIT_SANDBOX="$PWD/mysql-${MYSQL}-barebones"
export PATH="$PATH:/usr/sbin:$PWD/mysql-${MYSQL}-barebones/bin"
#############################
# Check and start test env. #
#############################
sandbox/test-env checkconfig || exit 1
sandbox/test-env restart || exit 1
#######################
# Set debug env vars. #
#######################
if [ "$DEBUG_CODE" = "true" ]; then
export PTDEBUG=1
fi
if [ "$DEBUG_TEST" = "true" ]; then
export PTDEVDEBUG=1
fi
##################
# Run the tests. #
##################
EXIT_STATUS=0
ITERATIONS="${ITERATIONS:-1}"
for iter in $(_seq $ITERATIONS); do
(
$TEST_CMD
)
EXIT_STATUS=$(($? | 0))
done
#############
# Clean up. #
#############
set +x
sandbox/test-env stop
set -x
exit $EXIT_STATUS

View File

@@ -9,7 +9,7 @@
err() {
echo
for msg; do
echo "$msg"
echo "$msg" >&2
done
}

View File

@@ -46,8 +46,6 @@ else {
plan tests => 56;
}
$sb->create_dbs($dbh1, ['test']);
Transformers->import(qw(make_checksum));
my $vp = new VersionParser();
@@ -107,7 +105,8 @@ sub get_id {
# Test the checksum method.
# #############################################################################
diag(`/tmp/12345/use < $trunk/t/lib/samples/compare-results.sql`);
$sb->load_file('master', "t/lib/samples/compare-results.sql");
PerconaTest::wait_for_table($dbh2, "test.t3", "f > 1");
$cr = new CompareResults(
method => 'checksum',
@@ -132,21 +131,6 @@ isa_ok($cr, 'CompareResults');
},
);
$i = 0;
PerconaTest::wait_until(
sub {
my $r;
eval {
$r = $dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"');
};
return 1 if ($r->[0] || '') eq 'dropme';
diag('Waiting for CREATE TABLE...') unless $i++;
return 0;
},
0.5,
30,
);
is_deeply(
$dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"'),
['dropme'],
@@ -326,9 +310,10 @@ is_deeply(
# #############################################################################
my $tmpdir = '/tmp/mk-upgrade-res';
diag(`rm -rf $tmpdir 2>/dev/null; mkdir $tmpdir`);
diag(`/tmp/12345/use < $trunk/t/lib/samples/compare-results.sql`);
diag(`rm -rf $tmpdir; mkdir $tmpdir`);
$sb->load_file('master', "t/lib/samples/compare-results.sql");
PerconaTest::wait_for_table($dbh2, "test.t3", "f > 1");
$cr = new CompareResults(
method => 'rows',
@@ -351,21 +336,6 @@ isa_ok($cr, 'CompareResults');
},
);
$i = 0;
PerconaTest::wait_until(
sub {
my $r;
eval {
$r = $dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"');
};
return 1 if ($r->[0] || '') eq 'dropme';
diag('Waiting for CREATE TABLE...') unless $i++;
return 0;
},
0.5,
30,
);
is_deeply(
$dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"'),
['dropme'],

View File

@@ -9,12 +9,14 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 22;
use Test::More tests => 21;
use Time::HiRes qw(sleep);
use Daemon;
use OptionParser;
use PerconaTest;
use constant PTDEVDEBUG => $ENV{PTDEVDEBUG} || 0;
my $o = new OptionParser(file => "$trunk/t/lib/samples/daemonizes.pl");
my $d = new Daemon(o=>$o);
@@ -30,7 +32,7 @@ sub rm_tmp_files() {
rm_tmp_files();
my $cmd = "$trunk/t/lib/samples/daemonizes.pl";
my $ret_val = system("$cmd 2 --daemonize --pid $pid_file");
my $ret_val = system("$cmd 2 --daemonize --pid $pid_file >/dev/null 2>&1");
die 'Cannot test Daemon.pm because t/daemonizes.pl is not working'
unless $ret_val == 0;
@@ -52,17 +54,19 @@ ok(! -f $pid_file, 'Removes PID file upon exit');
# ############################################################################
rm_tmp_files();
system("$cmd 2 --daemonize --log $log_file");
system("$cmd 0 --daemonize --log $log_file");
PerconaTest::wait_for_files($log_file);
ok(-f $log_file, 'Log file exists');
sleep 2;
$output = `cat $log_file`;
like($output, qr/STDOUT\nSTDERR\n/, 'STDOUT and STDERR went to log file');
my $log_size = -s $log_file;
PTDEVDEBUG && PerconaTest::_d('log size', $log_size);
# Check that the log file is appended to.
system("$cmd 0 --daemonize --log $log_file");
PerconaTest::wait_for_files($log_file);
PerconaTest::wait_until(sub { -s $log_file > $log_size });
$output = `cat $log_file`;
like(
$output,
@@ -80,7 +84,7 @@ ok(
'PID file already exists'
);
$output = `PTDEBUG=1 $cmd 0 --daemonize --pid $pid_file 2>&1`;
$output = `$cmd 2 --daemonize --pid $pid_file 2>&1`;
like(
$output,
qr{The PID file $pid_file already exists},
@@ -99,8 +103,8 @@ 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 1 --daemonize --pid $pid_file --log $log_file");
PerconaTest::wait_for_files($pid_file);
@@ -108,26 +112,30 @@ SKIP: {
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";
PTDEVDEBUG && PerconaTest::_d('pid_file', $pid_file,
'pid', $pid, 'proc_fd_0', $proc_fd_0, `ls -l $proc_fd_0`);
my $stdin = readlink $proc_fd_0;
is(
$stdin,
'/dev/null',
'Reopens STDIN to /dev/null if not piped',
'Reopens STDIN to /dev/null'
);
sleep 1;
system("echo foo | $cmd 1 --daemonize --pid $pid_file --log $log_file");
PerconaTest::wait_for_files($pid_file, $log_file);
chomp($pid = `cat $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";
$stdin = readlink $proc_fd_0;
like(
$stdin,
qr/pipe/,
'Does not reopen STDIN to /dev/null when piped',
);
# sleep 1;
# system("echo foo | $cmd 1 --daemonize --pid $pid_file --log $log_file");
# PerconaTest::wait_for_files($pid_file, $log_file);
# chomp($pid = `cat $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',
# );
sleep 1;
};
@@ -136,7 +144,7 @@ SKIP: {
# pid-file is still running
# ##########################################################################
rm_tmp_files();
system("$cmd 5 --daemonize --pid $pid_file 2>&1");
system("$cmd 5 --daemonize --pid $pid_file >/dev/null 2>&1");
PerconaTest::wait_for_files($pid_file);
chomp($pid = `cat $pid_file`);
kill 9, $pid;

View File

@@ -15,7 +15,7 @@ use MySQLProtocolParser;
use TcpdumpParser;
use PerconaTest;
my $sample = "t/lib/samples/tcpdump/";
my $sample = "t/lib/samples/tcpdump";
my $tcpdump = new TcpdumpParser();
my $protocol; # Create a new MySQLProtocolParser for each test.
@@ -487,79 +487,83 @@ test_protocol_parser(
],
);
# Check data decompression.
$protocol = new MySQLProtocolParser(
server => '127.0.0.1',
port => '12345',
);
test_protocol_parser(
parser => $tcpdump,
protocol => $protocol,
file => "$sample/tcpdump015.txt",
desc => 'compressed data',
result => [
{
Error_no => 'none',
No_good_index_used => 'No',
No_index_used => 'No',
Query_time => '0.006415',
Rows_affected => 0,
Thread_id => 20,
Warning_count => 0,
arg => 'administrator command: Connect',
bytes => 30,
cmd => 'Admin',
db => 'mysql',
host => '127.0.0.1',
ip => '127.0.0.1',
port => '44489',
pos_in_log => 664,
ts => '090612 08:39:05.316805',
user => 'msandbox',
},
{
Error_no => 'none',
No_good_index_used => 'No',
No_index_used => 'Yes',
Query_time => '0.002884',
Rows_affected => 0,
Thread_id => 20,
Warning_count => 0,
arg => 'select * from help_relation',
bytes => 27,
cmd => 'Query',
db => 'mysql',
host => '127.0.0.1',
ip => '127.0.0.1',
port => '44489',
pos_in_log => 1637,
ts => '090612 08:39:08.428913',
user => 'msandbox',
},
{
Error_no => 'none',
No_good_index_used => 'No',
No_index_used => 'No',
Query_time => '0.000000',
Rows_affected => 0,
Thread_id => 20,
Warning_count => 0,
arg => 'administrator command: Quit',
bytes => 27,
cmd => 'Admin',
db => 'mysql',
host => '127.0.0.1',
ip => '127.0.0.1',
port => '44489',
pos_in_log => 15782,
ts => '090612 08:39:09.145334',
user => 'msandbox',
},
],
);
eval { require IO::Uncompress::Inflate; };
SKIP: {
skip "IO::Uncompress::Inflate not installed", 2 if $EVAL_ERROR;
# Check data decompression.
$protocol = new MySQLProtocolParser(
server => '127.0.0.1',
port => '12345',
);
test_protocol_parser(
parser => $tcpdump,
protocol => $protocol,
file => "$sample/tcpdump015.txt",
desc => 'compressed data',
result => [
{
Error_no => 'none',
No_good_index_used => 'No',
No_index_used => 'No',
Query_time => '0.006415',
Rows_affected => 0,
Thread_id => 20,
Warning_count => 0,
arg => 'administrator command: Connect',
bytes => 30,
cmd => 'Admin',
db => 'mysql',
host => '127.0.0.1',
ip => '127.0.0.1',
port => '44489',
pos_in_log => 664,
ts => '090612 08:39:05.316805',
user => 'msandbox',
},
{
Error_no => 'none',
No_good_index_used => 'No',
No_index_used => 'Yes',
Query_time => '0.002884',
Rows_affected => 0,
Thread_id => 20,
Warning_count => 0,
arg => 'select * from help_relation',
bytes => 27,
cmd => 'Query',
db => 'mysql',
host => '127.0.0.1',
ip => '127.0.0.1',
port => '44489',
pos_in_log => 1637,
ts => '090612 08:39:08.428913',
user => 'msandbox',
},
{
Error_no => 'none',
No_good_index_used => 'No',
No_index_used => 'No',
Query_time => '0.000000',
Rows_affected => 0,
Thread_id => 20,
Warning_count => 0,
arg => 'administrator command: Quit',
bytes => 27,
cmd => 'Admin',
db => 'mysql',
host => '127.0.0.1',
ip => '127.0.0.1',
port => '44489',
pos_in_log => 15782,
ts => '090612 08:39:09.145334',
user => 'msandbox',
},
],
);
}
# TCP retransmission.
# Check data decompression.
$protocol = new MySQLProtocolParser(
server => '10.55.200.15',
);

View File

@@ -15,7 +15,7 @@ use SlowLogParser;
use PerconaTest;
my $p = new SlowLogParser;
my $sample = "t/lib/samples/slowlogs/";
my $sample = "t/lib/samples/slowlogs";
# Check that I can parse a slow log in the default slow log format.
test_log_parser(

View File

@@ -102,21 +102,15 @@ is(make_checksum('hello world'), '93CB22BB8F5ACDC3', 'make_checksum');
# #############################################################################
# crc32() tests.
# #############################################################################
eval {
require Digest::Crc32;
};
SKIP: {
skip "Digest::Crc32 is not installed", 1 if $EVAL_ERROR;
my $crc = new Digest::Crc32();
# Our crc32 should match the one from which we copied it.
is(
crc32('Hello world'),
$crc->strcrc32("Hello world"),
"crc32"
);
};
# Our crc32 is copied from Digest::Crc32 which is not a common module,
# so we don't rely on it being installed. Instead, I just copied the
# original value from Digest::Crc32 into this test.
is(
crc32('Hello world'), # our crc32()
'2346098258', # original value from Digest::Crc32::strcrc32()
"crc32"
);
# #############################################################################
# any_unix_timestamp() tests.

View File

@@ -1,25 +0,0 @@
#!/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 PerconaTest;
my ($tool) = $PROGRAM_NAME =~ m/([\w-]+)\.t$/;
push @ARGV, "$trunk/t/lib/bash/*.sh" unless @ARGV;
$ENV{BIN_DIR} = "$trunk/bin";
$ENV{LIB_DIR} = "$trunk/lib/bash";
$ENV{T_LIB_DIR} = "$trunk/t/lib";
$ENV{SANDBOX_VERSION} = "$sandbox_version";
system("$trunk/util/test-bash-functions $trunk/t/lib/samples/bash/dummy.sh @ARGV");
exit;

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
TESTS=1
plan 1
source "$LIB_DIR/alt_cmds.sh"

1
t/lib/bash/alt_cmds.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
TESTS=20
plan 20
TMPFILE="$TEST_TMPDIR/parse-opts-output"
TMPDIR="$TEST_TMPDIR"
@@ -18,7 +18,7 @@ source "$LIB_DIR/collect.sh"
parse_options "$BIN_DIR/pt-stalk" --run-time 1 -- --defaults-file=/tmp/12345/my.sandbox.cnf
# Prefix (with path) for the collect files.
local p="$TMPDIR/collect/2011_12_05"
p="$TMPDIR/collect/2011_12_05"
# Default collect, no extras like gdb, tcpdump, etc.
collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1
@@ -67,15 +67,19 @@ cmd_ok \
if [[ "$SANDBOX_VERSION" > "5.0" ]]; then
cmd_ok \
"grep -q 'Status information:' $p-log_error" \
"grep -qE 'Memory status|Open streams|Begin safemalloc' $p-log_error" \
"debug"
else
is "1" "1" "SKIP Can't determine MySQL 5.0 error log"
fi
cmd_ok \
"grep -q 'COMMAND[ ]\+PID[ ]\+USER' $p-lsof" \
"lsof"
if [ "$(which lsof 2>/dev/null)" ]; then
cmd_ok \
"grep -q 'COMMAND[ ]\+PID[ ]\+USER' $p-lsof" \
"lsof"
else
is "1" "1" "SKIP lsof not in PATH"
fi
cmd_ok \
"grep -q 'buf0buf.c' $p-mutex-status1" \
@@ -109,7 +113,7 @@ cmd_ok \
"grep -qP '^wait_timeout\t\d' $p-variables" \
"variables"
local iters=$(cat $p-df | grep -c '^TS ')
iters=$(cat $p-df | grep -c '^TS ')
is "$iters" "1" "1 iteration/1s run time"
empty_files=0
@@ -136,7 +140,7 @@ rm $TMPDIR/collect/*
collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1
local iters=$(cat $p-df | grep -c '^TS ')
iters=$(cat $p-df | grep -c '^TS ')
is "$iters" "2" "2 iteration/2s run time"
# ############################################################################

1
t/lib/bash/collect.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -1,9 +1,9 @@
#!/usr/bin/env bash
TESTS=9
plan 9
TMPDIR="$TEST_TMPDIR"
local file="$TMPDIR/pid-file"
file="$TMPDIR/pid-file"
source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/daemon.sh"
@@ -18,7 +18,7 @@ cmd_ok \
"test -f $file" \
"PID file created"
local pid=`cat $file`
pid=`cat $file`
is \
"$pid" \
"$$" \

1
t/lib/bash/daemon.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
TESTS=6
plan 6
source "$LIB_DIR/log_warn_die.sh"

1
t/lib/bash/log_warn_die.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
TESTS=78
plan 78
TMPFILE="$TEST_TMPDIR/parse-opts-output"
TOOL="pt-stalk"
@@ -93,7 +93,7 @@ parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1
cmd_ok "grep -q 'Unknown option: --foo' $TMPFILE" "Error on unknown option"
usage_or_errors "$T_LIB_DIR/samples/bash/po001.sh" >$TMPFILE 2>&1
local err=$?
err=$?
is "$err" "1" "Non-zero exit on unknown option"
# ###########################################################################

1
t/lib/bash/parse_options.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
TESTS=11
plan 11
source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/safeguards.sh"

1
t/lib/bash/safeguards.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
TESTS=9
plan 9
source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/tmpdir.sh"
@@ -21,7 +21,7 @@ is "$TMPDIR" "" "rm_tmpdir resets TMPDIR"
# User-specified tmpdir.
# ###########################################################################
local dir="/tmp/use--tmpdir"
dir="/tmp/use--tmpdir"
is "$TMPDIR" "" "TMPDIR not defined"

1
t/lib/bash/tmpdir.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -1,4 +0,0 @@
#!/bin/sh
# This is a dummy script for testing the Bash libs. t/lib/bashLibs.t
# calls "util/test-bash-functions dummy.sh <lib-test-files.sh>".

View File

@@ -11,9 +11,10 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG};
use constant PTDEVDEBUG => $ENV{PTDEVDEBUG};
use Time::HiRes qw(sleep);
use Daemon;
use OptionParser;
@@ -23,7 +24,8 @@ my $o = new OptionParser(file => "$trunk/t/lib/samples/daemonizes.pl");
$o->get_specs();
$o->get_opts();
if ( scalar @ARGV < 1 ) {
my ($sleep_time) = shift @ARGV;
if ( !defined $sleep_time ) {
$o->save_error('No SLEEP_TIME specified');
}
@@ -31,15 +33,22 @@ $o->usage_or_errors();
my $daemon;
if ( $o->get('daemonize') ) {
PTDEVDEBUG && PerconaTest::_d('daemonizing');
$OUTPUT_AUTOFLUSH = 1;
$daemon = new Daemon(o=>$o);
$daemon->daemonize();
PTDEVDEBUG && PerconaTest::_d('daemonized');
print "STDOUT\n";
print STDERR "STDERR\n";
sleep $ARGV[0];
PTDEVDEBUG && PerconaTest::_d('daemon sleep', $sleep_time);
sleep $sleep_time;
}
PTDEVDEBUG && PerconaTest::_d('daemon done');
exit;
# ############################################################################
@@ -50,7 +59,7 @@ exit;
=head1 SYNOPSIS
Usage: daemonizes.pl SLEEP_TIME [ARGS]
Usage: daemonizes.pl SLEEP_TIME
daemonizes.pl daemonizes, prints to STDOUT and STDERR, sleeps and exits.

View File

@@ -9,6 +9,7 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Time::HiRes qw(sleep);
use Test::More;
use PerconaTest;
@@ -44,6 +45,7 @@ $sb->load_file('master', 't/pt-fk-error-logger/samples/fke_tbl.sql', 'test');
# Then get and save that fke.
output(sub { pt_fk_error_logger::main('h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
sleep 0.1;
# And then test that it was actually saved.
my $today = $dbh->selectall_arrayref('SELECT NOW()')->[0]->[0];
@@ -64,6 +66,7 @@ like(
# Check again to make sure that the same fke isn't saved twice.
my $first_ts = $fke->[0]->[0];
output(sub { pt_fk_error_logger::main('h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
sleep 0.1;
$fke = $dbh->selectall_arrayref('SELECT * FROM test.foreign_key_errors');
is(
$fke->[0]->[0], # Timestamp
@@ -84,6 +87,7 @@ eval {
$dbh->do('DELETE FROM parent WHERE id = 2'); # Causes foreign key error.
};
output( sub { pt_fk_error_logger::main('h=127.1,P=12345,u=msandbox,p=msandbox', '--dest', 'h=127.1,P=12345,D=test,t=foreign_key_errors'); } );
sleep 0.1;
$fke = $dbh->selectall_arrayref('SELECT * FROM test.foreign_key_errors');
like(
$fke->[1]->[1], # Error
@@ -99,7 +103,6 @@ is(
# ##########################################################################
# Test printing the errors.
# ##########################################################################
sleep 1;
$dbh->do('USE test');
eval {
$dbh->do('DELETE FROM parent WHERE id = 2'); # Causes foreign key error.

View File

@@ -29,12 +29,13 @@ else {
$sb->create_dbs($dbh, ['test']);
my $output;
my $cnf = '/tmp/12345/my.sandbox.cnf';
my $cmd = "$trunk/bin/pt-heartbeat -F $cnf ";
my $pid_file = "/tmp/__pt-heartbeat-test.pid";
my $cnf = '/tmp/12345/my.sandbox.cnf';
my $cmd = "$trunk/bin/pt-heartbeat -F $cnf ";
my $pid_file = "/tmp/__pt-heartbeat-test.pid";
my $sent_file = "/tmp/pt-heartbeat-sentinel";
my $ps_grep_cmd = "ps x | grep pt-heartbeat | grep daemonize | grep -v grep";
`rm /tmp/pt-heartbeat-sentinel 2>/dev/null`;
`rm $sent_file 2>/dev/null`;
$dbh->do('drop table if exists test.heartbeat');
$dbh->do(q{CREATE TABLE test.heartbeat (
@@ -102,8 +103,8 @@ like($output, qr/Successfully created/, 'Created sentinel');
sleep(2);
$output = `$ps_grep_cmd`;
unlike($output, qr/$cmd/, 'It is not running');
ok(-f '/tmp/pt-heartbeat-sentinel', 'Sentinel file is there');
unlink('/tmp/pt-heartbeat-sentinel');
ok(-f $sent_file, 'Sentinel file is there');
unlink($sent_file);
$dbh->do('drop table if exists test.heartbeat'); # This will kill it
# #############################################################################
@@ -137,6 +138,6 @@ like(
# #############################################################################
# Done.
# #############################################################################
diag(`rm /tmp/pt-heartbeat-sentinel 2>/dev/null`);
`rm $pid_file $sent_file 2>/dev/null`;
$sb->wipe_clean($dbh);
exit;

View File

@@ -48,6 +48,7 @@ like(
`rm -rf /tmp/mk-script.pid`;
# #############################################################################
# Done.
# Doe.
# #############################################################################
$sb->wipe_clean($dbh);
exit;

View File

@@ -11,6 +11,8 @@ use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use Data::Dumper;
use PerconaTest;
use Sandbox;
require "$trunk/bin/pt-kill";
@@ -32,7 +34,7 @@ my $cmd = "$trunk/bin/pt-kill -F $cnf -h 127.1";
# Shell out to a sleep(10) query and try to capture the query.
# Backticks don't work here.
system("/tmp/12345/use -h127.1 -P12345 -umsandbox -pmsandbox -e 'select sleep(5)' >/dev/null&");
system("/tmp/12345/use -h127.1 -P12345 -umsandbox -pmsandbox -e 'select sleep(5)' >/dev/null &");
$output = `$cmd --busy-time 1s --print --run-time 10`;
@@ -47,7 +49,10 @@ $output = `$cmd --busy-time 1s --print --run-time 10`;
# 2009-05-27T22:19:47 KILL 5 (Query 8 sec) select sleep(10)
# 2009-05-27T22:19:48 KILL 5 (Query 9 sec) select sleep(10)
my @times = $output =~ m/\(Query (\d+) sec\)/g;
ok(@times > 2 && @times < 7, "There were 2 to 5 captures");
ok(
@times > 2 && @times < 7,
"There were 2 to 5 captures"
) or print STDERR Dumper($output);
# This is to catch a bad bug where there wasn't any sleep time when
# --iterations was 0, and another bug when --run-time was not respected.
@@ -56,7 +61,10 @@ ok(@times > 2 && @times < 7, "There were 2 to 5 captures");
system("/tmp/12345/use -h127.1 -P12345 -umsandbox -pmsandbox -e 'select sleep(10)' >/dev/null&");
$output = `$cmd --busy-time 1s --print --run-time 11s`;
@times = $output =~ m/\(Query (\d+) sec\)/g;
ok(@times > 7 && @times < 12, 'Approximately 9 or 10 captures with --iterations 0');
ok(
@times > 7 && @times < 12,
'Approximately 9 or 10 captures with --iterations 0'
) or print STDERR Dumper($output);
# ############################################################################

View File

@@ -116,7 +116,7 @@ is(
# #############################################################################
# No --alter and --drop-old-table.
# #############################################################################
$dbh->do('drop table mkosc.__old_a'); # from previous run
$dbh->do('drop table if exists mkosc.__old_a'); # from previous run
$sb->load_file('master', "t/pt-online-schema-change/samples/small_table.sql");
output(

View File

@@ -9,6 +9,7 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Time::HiRes qw(sleep);
use Test::More tests => 6;
use PerconaTest;
@@ -21,17 +22,20 @@ my $dbh = $sb->get_dbh_for('master');
my $output;
my $pid_file = '/tmp/pt-query-digest.test.pid';
`rm $pid_file >/dev/null 2>&1`;
# #########################################################################
# 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`;
`touch $pid_file`;
$output = `$trunk/bin/pt-query-digest $trunk/commont/t/samples/slow002.txt --pid $pid_file 2>&1`;
like(
$output,
qr{PID file /tmp/pt-script.pid already exists},
qr{PID file $pid_file already exists},
'Dies if PID file already exists (--pid without --daemonize) (issue 391)'
);
`rm -rf /tmp/pt-script.pid`;
`rm $pid_file >/dev/null 2>&1`;
# #########################################################################
# Daemonizing and pid creation
@@ -39,22 +43,22 @@ like(
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";
my $cmd = "$trunk/bin/pt-query-digest --daemonize --pid $pid_file --processlist h=127.1,P=12345,u=msandbox,p=msandbox --log /dev/null";
`$cmd`;
$output = `ps x | grep 'pt-query-digest --daemonize --pid' | grep -v grep`;
$output = `ps xw | grep -v grep | grep '$cmd'`;
like($output, qr/$cmd/, 'It is running');
ok(-f '/tmp/pt-query-digest.pid', 'PID file created');
ok(-f $pid_file, 'PID file created');
my ($pid) = $output =~ /\s+(\d+)\s+/;
$output = `cat /tmp/pt-query-digest.pid`;
my ($pid) = $output =~ /^\s*(\d+)/;
chomp($output = `cat $pid_file`);
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');
sleep 0.25;
$output = `ps xw | grep -v grep | grep '$cmd'`;
is($output, "", 'It is not running');
ok(
!-f '/tmp/pt-query-digest.pid',
!-f $pid_file,
'Removes its PID file'
);
};
@@ -62,4 +66,5 @@ SKIP: {
# #############################################################################
# Done.
# #############################################################################
`rm $pid_file >/dev/null 2>&1`;
exit;

View File

@@ -68,6 +68,8 @@ $cnf .= ',D=test';
# TODO: This test is a PITA because every time the mqd output
# changes the -n of tail has to be adjusted.
#
# We tail to get everything from "Exec orig" onward. The lines
# above have the real execution time will will vary. The last 18 lines
# are sufficient to see that it actually executed without errors.
@@ -77,6 +79,7 @@ ok(
"$trunk/t/lib/samples/slowlogs/slow018.txt") },
't/pt-query-digest/samples/slow018_execute_report_2.txt',
trf => 'tail -n 30',
sed => ["-e 's/s ##*/s/g'"],
),
'--execute with default database'
);

View File

@@ -10,6 +10,7 @@ use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use Time::HiRes qw(sleep);
use PerconaTest;
use DSNParser;
@@ -36,6 +37,9 @@ my $pid_file = "/tmp/pt-query-digest-mirror-test.pid";
diag(`rm $pid_file 2>/dev/null`);
my $pid_file = '/tmp/pt-query-digest.test.pid';
`rm -rf $pid_file >/dev/null`;
# ##########################################################################
# Tests for swapping --processlist and --execute
# ##########################################################################
@@ -48,23 +52,33 @@ $cmd = "$trunk/bin/pt-query-digest "
$ENV{PTDEBUG}=1;
`$cmd > /tmp/read_only.txt 2>&1 &`;
$ENV{PTDEBUG}=0;
sleep 5;
$ENV{MKDEBUG}=0;
sleep 3;
$dbh1->do('select sleep(1)');
sleep 1;
$dbh1->do('set global read_only=1');
$dbh2->do('set global read_only=0');
$dbh1->do('select sleep(1)');
sleep 2;
chomp($output = `cat $pid_file`);
kill 15, $output;
sleep 1;
chomp(my $pid = `cat $pid_file`);
kill 15, $pid;
sleep 0.25;
# Verify that it's dead...
$output = `ps x | grep '^[ ]*$pid'`;
is(
$output,
'',
'It is stopped now'
);
=======
$output = `ps -p $output`;
unlike($output, qr/pt-query-digest/, 'It is stopped now');
>>>>>>> MERGE-SOURCE
$dbh1->do('set global read_only=0');
$dbh2->do('set global read_only=1');
$output = `grep read_only /tmp/read_only.txt`;
# Sample output:
# # main:3619 6897 read_only on execute for --execute: 1 (want 1)

View File

@@ -55,6 +55,12 @@ my $output = output(
);
($exec) = $output =~ m/^(# Exec time.+?)$/ms;
# The end of the line is like "786ms 3s". The 2nd to last value is
# stddev which can vary slightly depending on the real exec time. The
# other int values should always round to the correct values. 786ms is
# the usual stddev. -- stddev doesn't matter much. It's the other vals
# that indicate that --processlist works.
$exec =~ s/(\S+) 3s$/786ms 3s/;
ok(
no_diff(
$exec,

View File

@@ -10,7 +10,7 @@
# Query_time distribution
# 1us
# 10us
# 100us ################################################################
# 100us
# 1ms
# 10ms
# 100ms

View File

@@ -1,42 +1,64 @@
#!/usr/bin/env perl
# This pseudo-script is for developers to see if their box has all
# the modules necessary for testing Percona Toolkit. Any missing
# modules will cause an error like "Can't locate Foo.pm in @INC ...".
# Else the version for each module used by this script will be printed.
use warnings FATAL => 'all';
use English;
# This script checks if all modules necessary for testing Percona Toolkit
# are installed. It exits 0 if all modules are installed, else it exits
# non-zero.
#
# In addition to these modules, many non-standard programs are needed
# for other tasks, like building packages, writing test coverage, etc.
#
# Exits 0 if all modules are installed, else exits non-zero.
# This check is just for testing Percona Toolkit. Other dev tasks, like
# building release packages, require other modules and programs.
use Data::Dumper;
use DBD::mysql;
use DBI;
use Digest::Crc32;
use Digest::MD5;
use File::Basename;
use File::Find;
use File::Spec;
use File::Temp;
use Getopt::Long;
use IO::File;
use IO::Uncompress::Inflate;
use List::Util;
use POSIX;
use Socket;
use Term::ReadKey;
use Test::More;
use Time::HiRes;
use Time::Local;
my @required_modules = qw(
Data::Dumper
DBD::mysql
DBI
Digest::MD5
File::Basename
File::Find
File::Spec
File::Temp
Getopt::Long
IO::File
List::Util
POSIX
Socket
Test::More
Time::HiRes
Time::Local
);
my $file = __FILE__;
my $m = `cat $file | grep '^use'`;
my @modules = map { m/use (.+?);/; $1 } split("\n", $m);
# CentOS doesn't seem to have this in its repo.
my @optional_modules = qw(
IO::Uncompress::Inflate
);
foreach my $module ( @modules ) {
my $version = "${module}::VERSION";
print "$module " . ${$version} . "\n";
my $exit_status = 0;
my $fmt = "%-23s %8s %s\n";
# Not a module but we want to know the Perl version.
printf $fmt, "Perl", `perl -v | perl -ne '/v([\\d\\.]+)/ && print \$1'`, "";
foreach my $module (@required_modules) {
my $version = "NA";
eval "require $module";
if ( $EVAL_ERROR ) {
$exit_status = 1;
}
else {
$version = ${"${module}::VERSION"};
}
printf $fmt, $module, $version, "";
}
exit 0;
foreach my $module (@optional_modules) {
my $version = "NA";
eval "require $module";
if ( !$EVAL_ERROR ) {
$version = ${"${module}::VERSION"};
}
printf $fmt, $module, $version, "MySQLProtocolParser, ProtocolParser"
}
exit $exit_status;

View File

@@ -14,21 +14,26 @@ die() {
exit 255
}
(
if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then
BRANCH=$PERCONA_TOOLKIT_BRANCH
cd $BRANCH
else
while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do
cd ..
done
if [ ! -f Makefile.PL ]; then
die "Cannot find the root directory of the Percona Toolkit branch"
exit 1
fi
BRANCH=`pwd`
cwd="$PWD"
if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then
BRANCH=$PERCONA_TOOLKIT_BRANCH
cd $BRANCH
else
while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do
cd ..
done
if [ ! -f Makefile.PL ]; then
die "Cannot find the root directory of the Percona Toolkit branch"
exit 1
fi
)
BRANCH="$PWD"
fi
cd "$cwd"
BIN_DIR="$BRANCH/bin";
LIB_DIR="$BRANCH/lib/bash";
T_LIB_DIR="$BRANCH/t/lib";
SANDBOX_VERSION="$($BRANCH/sandbox/test-env version)"
# ############################################################################
# Paths
@@ -108,6 +113,13 @@ result() {
return $result
}
plan() {
local n_tests=${1:-""}
if [ "$n_tests" ]; then
echo "1..$n_tests"
fi
}
#
# The following subs are for the test files to call.
#
@@ -141,32 +153,38 @@ cmd_ok() {
# Script starts here
# ############################################################################
if [ $# -lt 2 ]; then
die "Usage: test-back-functions FILE TESTS"
fi
# Check and source the bash file. This is the code being tested.
# All its global vars and subs will be imported.
bash_file=$1
shift
if [ ! -f "$bash_file" ]; then
die "$bash_file does not exist"
fi
head -n1 $bash_file | grep -q -E 'bash|sh' || die "$bash_file is not a bash file"
source $bash_file
# Load (count) the tests so that we can write a TAP test plan like 1..5
# for expecting 5 tests. Perl prove needs this.
declare -a tests
load_tests "$@"
# Run the test files.
testno=1
failed_tests=0
for t in "${tests[@]}"; do
run_test $t
done
if [ $# -eq 0 ]; then
TEST_FILE=$(basename "$0")
TEST="${TEST_FILE%".t"}"
source "$BRANCH/t/lib/bash/$TEST.sh"
else
if [ $# -lt 2 ]; then
die "Usage: test-bash-functions FILE TESTS"
fi
# Check and source the bash file. This is the code being tested.
# All its global vars and subs will be imported.
bash_file=$1
shift
if [ ! -f "$bash_file" ]; then
die "$bash_file does not exist"
fi
head -n1 $bash_file | grep -q -E 'bash|sh' || die "$bash_file is not a bash file"
source $bash_file
# Load (count) the tests so that we can write a TAP test plan like 1..5
# for expecting 5 tests. Perl prove needs this.
declare -a tests
load_tests "$@"
# Run the test files.
for t in "${tests[@]}"; do
run_test $t
done
fi
rm -rf $TEST_TMPDIR
exit $failed_tests