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'); PTDEBUG && _d('About to fork and daemonize');
defined (my $pid = fork()) or die "Cannot fork: $OS_ERROR"; defined (my $pid = fork()) or die "Cannot fork: $OS_ERROR";
if ( $pid ) { if ( $pid ) {
PTDEBUG && _d('I am the parent and now I die'); PTDEBUG && _d('Parent PID', $PID, 'exiting after forking child PID',$pid);
exit; exit;
} }
# I'm daemonized now. # I'm daemonized now.
PTDEBUG && _d('Daemonizing child PID', $PID);
$self->{PID_owner} = $PID; $self->{PID_owner} = $PID;
$self->{child} = 1; $self->{child} = 1;
@@ -70,15 +71,20 @@ sub daemonize {
$OUTPUT_AUTOFLUSH = 1; $OUTPUT_AUTOFLUSH = 1;
# Only reopen STDIN to /dev/null if it's a tty. It may be a pipe, # We used to only reopen STDIN to /dev/null if it's a tty because
# in which case we don't want to break it. # otherwise it may be a pipe, in which case we didn't want to break
if ( -t STDIN ) { # 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; close STDIN;
open STDIN, '/dev/null' open STDIN, '/dev/null'
or die "Cannot reopen STDIN to /dev/null: $OS_ERROR"; or die "Cannot reopen STDIN to /dev/null: $OS_ERROR";
}
if ( $self->{log_file} ) { if ( $self->{log_file} ) {
PTDEBUG && _d('Redirecting STDOUT and STDERR to', $self->{log_file});
close STDOUT; close STDOUT;
open STDOUT, '>>', $self->{log_file} open STDOUT, '>>', $self->{log_file}
or die "Cannot open log file $self->{log_file}: $OS_ERROR"; or die "Cannot open log file $self->{log_file}: $OS_ERROR";
@@ -93,18 +99,21 @@ sub daemonize {
} }
else { else {
if ( -t STDOUT ) { if ( -t STDOUT ) {
PTDEBUG && _d('No log file and STDOUT is a terminal;',
'redirecting to /dev/null');
close STDOUT; close STDOUT;
open STDOUT, '>', '/dev/null' open STDOUT, '>', '/dev/null'
or die "Cannot reopen STDOUT to /dev/null: $OS_ERROR"; or die "Cannot reopen STDOUT to /dev/null: $OS_ERROR";
} }
if ( -t STDERR ) { if ( -t STDERR ) {
PTDEBUG && _d('No log file and STDERR is a terminal;',
'redirecting to /dev/null');
close STDERR; close STDERR;
open STDERR, '>', '/dev/null' open STDERR, '>', '/dev/null'
or die "Cannot reopen STDERR to /dev/null: $OS_ERROR"; or die "Cannot reopen STDERR to /dev/null: $OS_ERROR";
} }
} }
PTDEBUG && _d('I am the child and now I live daemonized');
return; return;
} }

View File

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

View File

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

View File

@@ -46,8 +46,6 @@ else {
plan tests => 56; plan tests => 56;
} }
$sb->create_dbs($dbh1, ['test']);
Transformers->import(qw(make_checksum)); Transformers->import(qw(make_checksum));
my $vp = new VersionParser(); my $vp = new VersionParser();
@@ -107,7 +105,8 @@ sub get_id {
# Test the checksum method. # 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( $cr = new CompareResults(
method => 'checksum', 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( is_deeply(
$dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"'), $dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"'),
['dropme'], ['dropme'],
@@ -326,9 +310,10 @@ is_deeply(
# ############################################################################# # #############################################################################
my $tmpdir = '/tmp/mk-upgrade-res'; 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`); $sb->load_file('master', "t/lib/samples/compare-results.sql");
diag(`rm -rf $tmpdir; mkdir $tmpdir`); PerconaTest::wait_for_table($dbh2, "test.t3", "f > 1");
$cr = new CompareResults( $cr = new CompareResults(
method => 'rows', 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( is_deeply(
$dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"'), $dbh1->selectrow_arrayref('SHOW TABLES FROM test LIKE "dropme"'),
['dropme'], ['dropme'],

View File

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

View File

@@ -15,7 +15,7 @@ use MySQLProtocolParser;
use TcpdumpParser; use TcpdumpParser;
use PerconaTest; use PerconaTest;
my $sample = "t/lib/samples/tcpdump/"; my $sample = "t/lib/samples/tcpdump";
my $tcpdump = new TcpdumpParser(); my $tcpdump = new TcpdumpParser();
my $protocol; # Create a new MySQLProtocolParser for each test. my $protocol; # Create a new MySQLProtocolParser for each test.
@@ -487,12 +487,16 @@ test_protocol_parser(
], ],
); );
# Check data decompression. eval { require IO::Uncompress::Inflate; };
$protocol = new MySQLProtocolParser( SKIP: {
skip "IO::Uncompress::Inflate not installed", 2 if $EVAL_ERROR;
# Check data decompression.
$protocol = new MySQLProtocolParser(
server => '127.0.0.1', server => '127.0.0.1',
port => '12345', port => '12345',
); );
test_protocol_parser( test_protocol_parser(
parser => $tcpdump, parser => $tcpdump,
protocol => $protocol, protocol => $protocol,
file => "$sample/tcpdump015.txt", file => "$sample/tcpdump015.txt",
@@ -556,10 +560,10 @@ test_protocol_parser(
user => 'msandbox', user => 'msandbox',
}, },
], ],
); );
}
# TCP retransmission. # TCP retransmission.
# Check data decompression.
$protocol = new MySQLProtocolParser( $protocol = new MySQLProtocolParser(
server => '10.55.200.15', server => '10.55.200.15',
); );

View File

@@ -15,7 +15,7 @@ use SlowLogParser;
use PerconaTest; use PerconaTest;
my $p = new SlowLogParser; 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. # Check that I can parse a slow log in the default slow log format.
test_log_parser( test_log_parser(

View File

@@ -102,21 +102,15 @@ is(make_checksum('hello world'), '93CB22BB8F5ACDC3', 'make_checksum');
# ############################################################################# # #############################################################################
# crc32() tests. # crc32() tests.
# ############################################################################# # #############################################################################
eval {
require Digest::Crc32;
};
SKIP: {
skip "Digest::Crc32 is not installed", 1 if $EVAL_ERROR;
my $crc = new Digest::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
# Our crc32 should match the one from which we copied it. # original value from Digest::Crc32 into this test.
is( is(
crc32('Hello world'), crc32('Hello world'), # our crc32()
$crc->strcrc32("Hello world"), '2346098258', # original value from Digest::Crc32::strcrc32()
"crc32" "crc32"
); );
};
# ############################################################################# # #############################################################################
# any_unix_timestamp() tests. # 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 #!/usr/bin/env bash
TESTS=1 plan 1
source "$LIB_DIR/alt_cmds.sh" 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 #!/usr/bin/env bash
TESTS=20 plan 20
TMPFILE="$TEST_TMPDIR/parse-opts-output" TMPFILE="$TEST_TMPDIR/parse-opts-output"
TMPDIR="$TEST_TMPDIR" 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 parse_options "$BIN_DIR/pt-stalk" --run-time 1 -- --defaults-file=/tmp/12345/my.sandbox.cnf
# Prefix (with path) for the collect files. # 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. # Default collect, no extras like gdb, tcpdump, etc.
collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1 collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1
@@ -67,15 +67,19 @@ cmd_ok \
if [[ "$SANDBOX_VERSION" > "5.0" ]]; then if [[ "$SANDBOX_VERSION" > "5.0" ]]; then
cmd_ok \ cmd_ok \
"grep -q 'Status information:' $p-log_error" \ "grep -qE 'Memory status|Open streams|Begin safemalloc' $p-log_error" \
"debug" "debug"
else else
is "1" "1" "SKIP Can't determine MySQL 5.0 error log" is "1" "1" "SKIP Can't determine MySQL 5.0 error log"
fi fi
cmd_ok \ if [ "$(which lsof 2>/dev/null)" ]; then
cmd_ok \
"grep -q 'COMMAND[ ]\+PID[ ]\+USER' $p-lsof" \ "grep -q 'COMMAND[ ]\+PID[ ]\+USER' $p-lsof" \
"lsof" "lsof"
else
is "1" "1" "SKIP lsof not in PATH"
fi
cmd_ok \ cmd_ok \
"grep -q 'buf0buf.c' $p-mutex-status1" \ "grep -q 'buf0buf.c' $p-mutex-status1" \
@@ -109,7 +113,7 @@ cmd_ok \
"grep -qP '^wait_timeout\t\d' $p-variables" \ "grep -qP '^wait_timeout\t\d' $p-variables" \
"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" is "$iters" "1" "1 iteration/1s run time"
empty_files=0 empty_files=0
@@ -136,7 +140,7 @@ rm $TMPDIR/collect/*
collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1 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" 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 #!/usr/bin/env bash
TESTS=9 plan 9
TMPDIR="$TEST_TMPDIR" TMPDIR="$TEST_TMPDIR"
local file="$TMPDIR/pid-file" file="$TMPDIR/pid-file"
source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/daemon.sh" source "$LIB_DIR/daemon.sh"
@@ -18,7 +18,7 @@ cmd_ok \
"test -f $file" \ "test -f $file" \
"PID file created" "PID file created"
local pid=`cat $file` pid=`cat $file`
is \ is \
"$pid" \ "$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 #!/usr/bin/env bash
TESTS=6 plan 6
source "$LIB_DIR/log_warn_die.sh" 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 #!/usr/bin/env bash
TESTS=78 plan 78
TMPFILE="$TEST_TMPDIR/parse-opts-output" TMPFILE="$TEST_TMPDIR/parse-opts-output"
TOOL="pt-stalk" 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" 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 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" 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 #!/usr/bin/env bash
TESTS=11 plan 11
source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/safeguards.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 #!/usr/bin/env bash
TESTS=9 plan 9
source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/tmpdir.sh" source "$LIB_DIR/tmpdir.sh"
@@ -21,7 +21,7 @@ is "$TMPDIR" "" "rm_tmpdir resets TMPDIR"
# User-specified tmpdir. # User-specified tmpdir.
# ########################################################################### # ###########################################################################
local dir="/tmp/use--tmpdir" dir="/tmp/use--tmpdir"
is "$TMPDIR" "" "TMPDIR not defined" 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 strict;
use warnings FATAL => 'all'; use warnings FATAL => 'all';
use English qw(-no_match_vars); use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG}; use constant PTDEVDEBUG => $ENV{PTDEVDEBUG};
use Time::HiRes qw(sleep);
use Daemon; use Daemon;
use OptionParser; use OptionParser;
@@ -23,7 +24,8 @@ my $o = new OptionParser(file => "$trunk/t/lib/samples/daemonizes.pl");
$o->get_specs(); $o->get_specs();
$o->get_opts(); $o->get_opts();
if ( scalar @ARGV < 1 ) { my ($sleep_time) = shift @ARGV;
if ( !defined $sleep_time ) {
$o->save_error('No SLEEP_TIME specified'); $o->save_error('No SLEEP_TIME specified');
} }
@@ -31,15 +33,22 @@ $o->usage_or_errors();
my $daemon; my $daemon;
if ( $o->get('daemonize') ) { if ( $o->get('daemonize') ) {
PTDEVDEBUG && PerconaTest::_d('daemonizing');
$OUTPUT_AUTOFLUSH = 1;
$daemon = new Daemon(o=>$o); $daemon = new Daemon(o=>$o);
$daemon->daemonize(); $daemon->daemonize();
PTDEVDEBUG && PerconaTest::_d('daemonized');
print "STDOUT\n"; print "STDOUT\n";
print STDERR "STDERR\n"; print STDERR "STDERR\n";
sleep $ARGV[0]; PTDEVDEBUG && PerconaTest::_d('daemon sleep', $sleep_time);
sleep $sleep_time;
} }
PTDEVDEBUG && PerconaTest::_d('daemon done');
exit; exit;
# ############################################################################ # ############################################################################
@@ -50,7 +59,7 @@ exit;
=head1 SYNOPSIS =head1 SYNOPSIS
Usage: daemonizes.pl SLEEP_TIME [ARGS] Usage: daemonizes.pl SLEEP_TIME
daemonizes.pl daemonizes, prints to STDOUT and STDERR, sleeps and exits. daemonizes.pl daemonizes, prints to STDOUT and STDERR, sleeps and exits.

View File

@@ -9,6 +9,7 @@ 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 Time::HiRes qw(sleep);
use Test::More; use Test::More;
use PerconaTest; 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. # 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'); } ); 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. # And then test that it was actually saved.
my $today = $dbh->selectall_arrayref('SELECT NOW()')->[0]->[0]; 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. # Check again to make sure that the same fke isn't saved twice.
my $first_ts = $fke->[0]->[0]; 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'); } ); 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'); $fke = $dbh->selectall_arrayref('SELECT * FROM test.foreign_key_errors');
is( is(
$fke->[0]->[0], # Timestamp $fke->[0]->[0], # Timestamp
@@ -84,6 +87,7 @@ eval {
$dbh->do('DELETE FROM parent WHERE id = 2'); # Causes foreign key error. $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'); } ); 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'); $fke = $dbh->selectall_arrayref('SELECT * FROM test.foreign_key_errors');
like( like(
$fke->[1]->[1], # Error $fke->[1]->[1], # Error
@@ -99,7 +103,6 @@ is(
# ########################################################################## # ##########################################################################
# Test printing the errors. # Test printing the errors.
# ########################################################################## # ##########################################################################
sleep 1;
$dbh->do('USE test'); $dbh->do('USE test');
eval { eval {
$dbh->do('DELETE FROM parent WHERE id = 2'); # Causes foreign key error. $dbh->do('DELETE FROM parent WHERE id = 2'); # Causes foreign key error.

View File

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

View File

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

View File

@@ -11,6 +11,8 @@ use warnings FATAL => 'all';
use English qw(-no_match_vars); use English qw(-no_match_vars);
use Test::More; use Test::More;
use Data::Dumper;
use PerconaTest; use PerconaTest;
use Sandbox; use Sandbox;
require "$trunk/bin/pt-kill"; 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. # Shell out to a sleep(10) query and try to capture the query.
# Backticks don't work here. # 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`; $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:47 KILL 5 (Query 8 sec) select sleep(10)
# 2009-05-27T22:19:48 KILL 5 (Query 9 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; 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 # 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. # --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&"); system("/tmp/12345/use -h127.1 -P12345 -umsandbox -pmsandbox -e 'select sleep(10)' >/dev/null&");
$output = `$cmd --busy-time 1s --print --run-time 11s`; $output = `$cmd --busy-time 1s --print --run-time 11s`;
@times = $output =~ m/\(Query (\d+) sec\)/g; @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. # 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"); $sb->load_file('master', "t/pt-online-schema-change/samples/small_table.sql");
output( output(

View File

@@ -9,6 +9,7 @@ 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 Time::HiRes qw(sleep);
use Test::More tests => 6; use Test::More tests => 6;
use PerconaTest; use PerconaTest;
@@ -21,17 +22,20 @@ my $dbh = $sb->get_dbh_for('master');
my $output; 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 # Issue 391: Add --pid option to all scripts
# ######################################################################### # #########################################################################
`touch /tmp/pt-script.pid`; `touch $pid_file`;
$output = `$trunk/bin/pt-query-digest $trunk/commont/t/samples/slow002.txt --pid /tmp/pt-script.pid 2>&1`; $output = `$trunk/bin/pt-query-digest $trunk/commont/t/samples/slow002.txt --pid $pid_file 2>&1`;
like( like(
$output, $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)' '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 # Daemonizing and pid creation
@@ -39,22 +43,22 @@ like(
SKIP: { SKIP: {
skip "Cannot connect to sandbox master", 5 unless $dbh; 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`; `$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'); 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+/; my ($pid) = $output =~ /^\s*(\d+)/;
$output = `cat /tmp/pt-query-digest.pid`; chomp($output = `cat $pid_file`);
is($output, $pid, 'PID file has correct PID'); is($output, $pid, 'PID file has correct PID');
kill 15, $pid; kill 15, $pid;
sleep 1; sleep 0.25;
$output = `ps -eaf | grep pt-query-digest | grep daemonize`; $output = `ps xw | grep -v grep | grep '$cmd'`;
unlike($output, qr/$trunk\/pt-query-digest\/pt-query-digest/, 'It is not running'); is($output, "", 'It is not running');
ok( ok(
!-f '/tmp/pt-query-digest.pid', !-f $pid_file,
'Removes its PID file' 'Removes its PID file'
); );
}; };
@@ -62,4 +66,5 @@ SKIP: {
# ############################################################################# # #############################################################################
# Done. # Done.
# ############################################################################# # #############################################################################
`rm $pid_file >/dev/null 2>&1`;
exit; exit;

View File

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

View File

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

View File

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

View File

@@ -1,42 +1,64 @@
#!/usr/bin/env perl #!/usr/bin/env perl
# This pseudo-script is for developers to see if their box has all use warnings FATAL => 'all';
# the modules necessary for testing Percona Toolkit. Any missing use English;
# 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. # 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 # This check is just for testing Percona Toolkit. Other dev tasks, like
# for other tasks, like building packages, writing test coverage, etc. # building release packages, require other modules and programs.
#
# Exits 0 if all modules are installed, else exits non-zero.
use Data::Dumper; my @required_modules = qw(
use DBD::mysql; Data::Dumper
use DBI; DBD::mysql
use Digest::Crc32; DBI
use Digest::MD5; Digest::MD5
use File::Basename; File::Basename
use File::Find; File::Find
use File::Spec; File::Spec
use File::Temp; File::Temp
use Getopt::Long; Getopt::Long
use IO::File; IO::File
use IO::Uncompress::Inflate; List::Util
use List::Util; POSIX
use POSIX; Socket
use Socket; Test::More
use Term::ReadKey; Time::HiRes
use Test::More; Time::Local
use Time::HiRes; );
use Time::Local;
my $file = __FILE__; # CentOS doesn't seem to have this in its repo.
my $m = `cat $file | grep '^use'`; my @optional_modules = qw(
my @modules = map { m/use (.+?);/; $1 } split("\n", $m); IO::Uncompress::Inflate
);
foreach my $module ( @modules ) { my $exit_status = 0;
my $version = "${module}::VERSION"; my $fmt = "%-23s %8s %s\n";
print "$module " . ${$version} . "\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,11 +14,11 @@ die() {
exit 255 exit 255
} }
( cwd="$PWD"
if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then
BRANCH=$PERCONA_TOOLKIT_BRANCH BRANCH=$PERCONA_TOOLKIT_BRANCH
cd $BRANCH cd $BRANCH
else else
while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do
cd .. cd ..
done done
@@ -26,9 +26,14 @@ die() {
die "Cannot find the root directory of the Percona Toolkit branch" die "Cannot find the root directory of the Percona Toolkit branch"
exit 1 exit 1
fi fi
BRANCH=`pwd` BRANCH="$PWD"
fi 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 # Paths
@@ -108,6 +113,13 @@ result() {
return $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. # The following subs are for the test files to call.
# #
@@ -141,32 +153,38 @@ cmd_ok() {
# Script starts here # 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 testno=1
failed_tests=0 failed_tests=0
for t in "${tests[@]}"; do
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 run_test $t
done done
fi
rm -rf $TEST_TMPDIR rm -rf $TEST_TMPDIR
exit $failed_tests exit $failed_tests