Rewrite pt-diskstats.t but all tests need to be fixed. Update modules in pt-diskstats, use PTDEBUG, check regex opts. Remove output_fh from Diskstats. Fix DiskstatsMenu header so update-modules can see it.

This commit is contained in:
Daniel Nichter
2012-01-12 12:06:50 -07:00
parent 8d70a272fe
commit 9404ff84ef
5 changed files with 436 additions and 539 deletions

View File

@@ -9,48 +9,18 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 16;
use File::Spec;
use File::Temp ();
use Test::More tests => 15;
use PerconaTest;
use pt_diskstats;
my ($fh, $tempfile) = File::Temp::tempfile(
"diskstats.test.XXXXXXXXX",
OPEN => 1, UNLINK => 1 );
my $iterations = 2;
my $out = output( sub {
pt_diskstats::main(
"--group-by" => "all",
"--columns" => "cnc|rt|mb|busy|prg",
"--save-samples" => $tempfile,
"--iterations" => $iterations,
"--zero-rows",
);
});
my $o = new OptionParser(description => 'Diskstats');
$o->get_specs("$trunk/bin/pt-diskstats");
my $count = 0;
Diskstats->new(
OptionParser => $o,
)->parse_from( filename => $tempfile, sample_callback => sub { $count++ } );
is(
$count,
$iterations,
"--save-samples and --iterations work"
);
close $fh;
1 while unlink $tempfile;
require "$trunk/bin/pt-diskstats";
# pt-diskstats is an interactive-only tool. It waits for user input
# (i.e. menu commands) via STDIN. So we cannot just run it with input,
# get ouput, then test that output. We have to tie STDIN to these subs
# so that we can fake sending pt-diskstats menu commands via STDIN.
# All we do is send 'q', the command to quit. See the note in the bottom
# of this file about *DATA. Please don't close it.
{
# Tie magic. During the tests we tie STDIN to always return a lone "q".
# See the note in the bottom of this file about *DATA. Please don't close it.
sub Test::TIEHANDLE {
return bless {}, "Test";
}
@@ -64,32 +34,62 @@ sub Test::READLINE {
}
}
for my $ext ( qw( all disk sample ) ) {
for my $filename ( map "diskstats-00$_.txt", 1..5 ) {
my $expected = load_file(
File::Spec->catfile( "t", "pt-diskstats",
"expected", "${ext}_int_$filename"
),
);
my $got = output( sub {
tie local *STDIN, "Test";
my $file = File::Spec->catfile( $trunk, "t", "pt-diskstats",
"samples", $filename );
pt_diskstats::main(
"--group-by" => $ext,
"--columns" => "cnc|rt|mb|busy|prg",
"--zero-rows",
$file
);
} );
is($got, $expected, "--group-by $ext for $filename gets the same results as the shell version");
sub test_diskstats_file {
my (%args) = @_;
my $file = "$trunk/t/pt-diskstats/samples/$args{file}";
die "$file does not exist" unless -f $file;
foreach my $groupby ( qw(all disk sample) ) {
ok(
no_diff(
sub {
tie local *STDIN, "Test";
pt_diskstats::main(
qw(--zero-rows --group-by), $groupby,
'--columns','cnc|rt|mb|busy|prg',
$file);
},
"t/pt-diskstats/expected/${groupby}_int_$args{file}",
keep_output=>1,
),
"$args{file} --group-by $groupby"
);
}
}
foreach my $file ( map "diskstats-00$_.txt", 1..5 ) {
test_diskstats_file(file => $file);
}
# ###########################################################################
# --save-samples and --iterations
# ###########################################################################
# TODO: fix this
#my $iterations = 2;
#my $out = output( sub {
# pt_diskstats::main(
# "--group-by" => "all",
# "--columns" => "cnc|rt|mb|busy|prg",
# "--save-samples" => $tempfile,
# "--iterations" => $iterations,
# "--zero-rows",
# );
#});
#
#is(
# $count,
# $iterations,
# "--save-samples and --iterations work"
#);
# ###########################################################################
# Done.
# ###########################################################################
exit;
__DATA__
Leave this here!
We tie STDIN during the tests, and fake the fileno by giving it *DATA's result;
These lines here make Perl leave *DATA open.