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

File diff suppressed because it is too large Load Diff

View File

@@ -77,7 +77,6 @@ sub new {
# Defaults # Defaults
filename => '/proc/diskstats', filename => '/proc/diskstats',
block_size => 512, block_size => 512,
output_fh => \*STDOUT,
zero_rows => $o->get('zero-rows'), zero_rows => $o->get('zero-rows'),
sample_time => $o->get('sample-time') || 0, sample_time => $o->get('sample-time') || 0,
column_regex => qr/$columns/, column_regex => qr/$columns/,
@@ -181,25 +180,6 @@ sub set_interactive {
} }
} }
# Checks whenever said filehandle is open. If it's not, defaults to STDOUT.
sub output_fh {
my ( $self ) = @_;
if ( !$self->{output_fh} || !$self->{output_fh}->opened ) {
$self->{output_fh} = \*STDOUT;
}
return $self->{output_fh};
}
# It sets or returns the currently set filehandle, kind of like a poor man's
# select().
sub set_output_fh {
my ( $self, $new_fh ) = @_;
# ->opened comes from IO::Handle.
if ( $new_fh && ref($new_fh) && $new_fh->opened ) {
$self->{output_fh} = $new_fh;
}
}
sub column_regex { sub column_regex {
my ( $self ) = @_; my ( $self ) = @_;
return $self->{column_regex}; return $self->{column_regex};
@@ -812,7 +792,7 @@ sub _calc_deltas {
sub print_header { sub print_header {
my ($self, $header, @args) = @_; my ($self, $header, @args) = @_;
if ( $self->{_print_header} ) { if ( $self->{_print_header} ) {
printf { $self->output_fh() } $header . "\n", @args; printf $header . "\n", @args;
} }
} }
@@ -830,8 +810,7 @@ sub print_rows {
sprintf("%7.1f", $_) != 0 sprintf("%7.1f", $_) != 0
} @{ $stat }{ @$cols }; } @{ $stat }{ @$cols };
} }
printf { $self->output_fh() } $format . "\n", printf $format . "\n", @{ $stat }{ qw( line_ts dev ), @$cols };
@{ $stat }{ qw( line_ts dev ), @$cols };
} }
sub print_deltas { sub print_deltas {

View File

@@ -15,7 +15,7 @@
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA. # Place, Suite 330, Boston, MA 02111-1307 USA.
# ########################################################################### # ###########################################################################
# DiskstatsMenu # DiskstatsMenu package
# ########################################################################### # ###########################################################################
{ {
package DiskstatsMenu; package DiskstatsMenu;
@@ -478,4 +478,4 @@ sub _d {
} }
# ########################################################################### # ###########################################################################
# End DiskstatsMenu package # End DiskstatsMenu package
# ########################################################################### # ###########################################################################

View File

@@ -9,7 +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 Test::More tests => 112; use Test::More tests => 108;
use PerconaTest; use PerconaTest;
@@ -33,7 +33,7 @@ $o->get_opts();
my $obj = new Diskstats(OptionParser => $o); my $obj = new Diskstats(OptionParser => $o);
can_ok( $obj, qw( can_ok( $obj, qw(
output_fh column_regex device_regex filename column_regex device_regex filename
block_size ordered_devs clear_state clear_ordered_devs block_size ordered_devs clear_state clear_ordered_devs
stats_for prev_stats_for first_stats_for stats_for prev_stats_for first_stats_for
has_stats design_print_formats parse_diskstats_line has_stats design_print_formats parse_diskstats_line
@@ -49,7 +49,6 @@ for my $attr (
[ column_regex => qr/!!!/ ], [ column_regex => qr/!!!/ ],
[ device_regex => qr/!!!/ ], [ device_regex => qr/!!!/ ],
[ block_size => 215 ], [ block_size => 215 ],
[ output_fh => \*STDERR ],
[ zero_rows => 1 ], [ zero_rows => 1 ],
[ sample_time => 1 ], [ sample_time => 1 ],
[ interactive => 1 ], [ interactive => 1 ],
@@ -249,23 +248,6 @@ for my $method ( qw( curr_ts prev_ts first_ts ) ) {
ok(!$obj->$method(), "Diskstats->clear_ts does as advertized"); ok(!$obj->$method(), "Diskstats->clear_ts does as advertized");
} }
# ############################################################################
# output_fh
# ############################################################################
is($obj->output_fh(), \*STDOUT, "by default, outputs to STDOUT");
open my $fh, "<", \my $tmp;
$obj->set_output_fh($fh);
is($obj->output_fh(), $fh, "Changing it works");
close($fh);
is(
$obj->output_fh(),
\*STDOUT,
"and if we close the set filehandle, it reverts to STDOUT"
);
# ############################################################################ # ############################################################################
# Adding, removing and listing devices. # Adding, removing and listing devices.
# ############################################################################ # ############################################################################

View File

@@ -9,48 +9,18 @@ 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 => 16; use Test::More tests => 15;
use File::Spec;
use File::Temp ();
use PerconaTest; use PerconaTest;
use pt_diskstats; require "$trunk/bin/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;
# 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 { sub Test::TIEHANDLE {
return bless {}, "Test"; return bless {}, "Test";
} }
@@ -64,32 +34,62 @@ sub Test::READLINE {
} }
} }
for my $ext ( qw( all disk sample ) ) { sub test_diskstats_file {
for my $filename ( map "diskstats-00$_.txt", 1..5 ) { my (%args) = @_;
my $expected = load_file( my $file = "$trunk/t/pt-diskstats/samples/$args{file}";
File::Spec->catfile( "t", "pt-diskstats", die "$file does not exist" unless -f $file;
"expected", "${ext}_int_$filename" foreach my $groupby ( qw(all disk sample) ) {
), ok(
); no_diff(
sub {
my $got = output( sub { tie local *STDIN, "Test";
tie local *STDIN, "Test"; pt_diskstats::main(
my $file = File::Spec->catfile( $trunk, "t", "pt-diskstats", qw(--zero-rows --group-by), $groupby,
"samples", $filename ); '--columns','cnc|rt|mb|busy|prg',
pt_diskstats::main( $file);
"--group-by" => $ext, },
"--columns" => "cnc|rt|mb|busy|prg", "t/pt-diskstats/expected/${groupby}_int_$args{file}",
"--zero-rows", keep_output=>1,
$file ),
); "$args{file} --group-by $groupby"
} ); );
is($got, $expected, "--group-by $ext for $filename gets the same results as the shell version");
} }
} }
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__ __DATA__
Leave this here! Leave this here!
We tie STDIN during the tests, and fake the fileno by giving it *DATA's result; We tie STDIN during the tests, and fake the fileno by giving it *DATA's result;
These lines here make Perl leave *DATA open. These lines here make Perl leave *DATA open.