~percona-toolkit-dev/percona-toolkit/pt-agent

« back to all changes in this revision

Viewing changes to t/lib/TextResultSetParser.t

  • Committer: Daniel Nichter
  • Date: 2011-06-24 17:22:06 UTC
  • Revision ID: daniel@percona.com-20110624172206-c7q4s4ad6r260zz6
Add lib/, t/lib/, and sandbox/.  All modules are updated and passing on MySQL 5.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
BEGIN {
 
4
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
 
5
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
 
6
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
 
7
};
 
8
 
 
9
use strict;
 
10
use warnings FATAL => 'all';
 
11
use English qw(-no_match_vars);
 
12
use Test::More tests => 13;
 
13
 
 
14
use TextResultSetParser;
 
15
use MaatkitTest;
 
16
 
 
17
use Data::Dumper;
 
18
$Data::Dumper::Indent    = 1;
 
19
$Data::Dumper::Sortkeys  = 1;
 
20
$Data::Dumper::Quotekeys = 0;
 
21
 
 
22
my $r = new TextResultSetParser();
 
23
isa_ok($r, 'TextResultSetParser');
 
24
 
 
25
throws_ok(
 
26
   sub { $r->parse(load_file('t/lib/samples/slowlogs/slow002.txt')) },
 
27
   qr/Cannot determine if text is/,
 
28
   "Dies if output type cannot be determined"
 
29
);
 
30
 
 
31
is_deeply(
 
32
   $r->parse( load_file('t/lib/samples/pl/recset001.txt') ),
 
33
   [
 
34
      {
 
35
         Time     => '0',
 
36
         Command  => 'Query',
 
37
         db       => '',
 
38
         Id       => '9',
 
39
         Info     => 'show processlist',
 
40
         User     => 'msandbox',
 
41
         State    => '',
 
42
         Host     => 'localhost'
 
43
      },
 
44
   ],
 
45
   'Basic tablular processlist'
 
46
);
 
47
 
 
48
is_deeply(
 
49
   $r->parse( load_file('t/lib/samples/pl/recset002.txt') ),
 
50
   [
 
51
      {
 
52
         Time     => '4',
 
53
         Command  => 'Query',
 
54
         db       => 'foo',
 
55
         Id       => '1',
 
56
         Info     => 'select * from foo1;',
 
57
         User     => 'user1',
 
58
         State    => 'Locked',
 
59
         Host     => '1.2.3.4:3333'
 
60
      },
 
61
      {
 
62
         Time     => '5',
 
63
         Command  => 'Query',
 
64
         db       => 'foo',
 
65
         Id       => '2',
 
66
         Info     => 'select * from foo2;',
 
67
         User     => 'user1',
 
68
         State    => 'Locked',
 
69
         Host     => '1.2.3.4:5455'
 
70
      },
 
71
   ],
 
72
   '2 row vertical processlist'
 
73
);
 
74
 
 
75
my $recset = $r->parse ( load_file('t/lib/samples/pl/recset003.txt') );
 
76
cmp_ok(
 
77
   scalar @$recset,
 
78
   '==',
 
79
   113,
 
80
   '113 row vertical processlist'
 
81
);
 
82
 
 
83
$recset = $r->parse( load_file('t/lib/samples/pl/recset004.txt') );
 
84
cmp_ok(
 
85
   scalar @$recset,
 
86
   '==',
 
87
   51,
 
88
   '51 row vertical processlist'
 
89
);
 
90
 
 
91
is_deeply(
 
92
   $r->parse( load_file('t/lib/samples/pl/recset005.txt') ),
 
93
   [
 
94
      {
 
95
         Id    => '29392005',
 
96
         User  => 'remote',
 
97
         Host  => '1.2.3.148:49718',
 
98
         db    => 'happy',
 
99
         Command => 'Sleep',
 
100
         Time  => '17',
 
101
         State => undef,
 
102
         Info  => undef,
 
103
      }
 
104
   ],
 
105
   '1 vertical row, No State value'
 
106
);
 
107
 
 
108
is_deeply(
 
109
   $r->parse( load_file('t/lib/samples/pl/recset009.txt') ),
 
110
   [
 
111
      {
 
112
         Id      => '21',
 
113
         User    => 'msandbox',
 
114
         Host    => 'localhost:54732',
 
115
         db      => undef,
 
116
         Command => 'Binlog Dump',
 
117
         Time    => '3081',
 
118
         State   => 'Has sent all binlog to slave; waiting for binlog to be updated',
 
119
         Info    => undef,
 
120
      },
 
121
      {
 
122
         Id      => '41',
 
123
         User    => 'msandbox',
 
124
         Host    => 'localhost',
 
125
         db      => undef,
 
126
         Command => 'Query',
 
127
         Time    => '0',
 
128
         State   => undef,
 
129
         Info    => 'show full processlist',
 
130
      }
 
131
   ],
 
132
   'Horizontal, tab-separated'
 
133
);
 
134
 
 
135
$recset = $r->parse(load_file('t/lib/samples/show-variables/vars001.txt'));
 
136
# Should only get the var once.
 
137
my $got_var = grep { $_->{Variable_name} eq 'warning_count' } @$recset;
 
138
is(
 
139
   $got_var,
 
140
   1,
 
141
   "vars001.txt"
 
142
);
 
143
 
 
144
$recset = $r->parse(load_file('t/lib/samples/show-variables/vars002.txt'));
 
145
$got_var = grep { $_->{Variable_name} eq 'warning_count' } @$recset;
 
146
is(
 
147
   $got_var,
 
148
   1,
 
149
   "vars002.txt"
 
150
);
 
151
 
 
152
 
 
153
# #############################################################################
 
154
# Parse with NAME_lc for lowercase key/col names.
 
155
# #############################################################################
 
156
$r = new TextResultSetParser(NAME_lc => 1);
 
157
 
 
158
$recset = $r->parse(load_file('t/lib/samples/show-variables/vars001.txt'));
 
159
$got_var = grep { $_->{variable_name} eq 'warning_count' } @$recset;
 
160
is(
 
161
   $got_var,
 
162
   1,
 
163
   "NAME_lc tabular"
 
164
);
 
165
 
 
166
$recset = $r->parse(load_file('t/lib/samples/show-variables/vars002.txt'));
 
167
$got_var = grep { $_->{variable_name} eq 'warning_count' } @$recset;
 
168
is(
 
169
   $got_var,
 
170
   1,
 
171
   "NAME_lc tab-separated"
 
172
);
 
173
 
 
174
is_deeply(
 
175
   $r->parse( load_file('t/lib/samples/pl/recset002.txt') ),
 
176
   [
 
177
      {
 
178
         time     => '4',
 
179
         command  => 'Query',
 
180
         db       => 'foo',
 
181
         id       => '1',
 
182
         info     => 'select * from foo1;',
 
183
         user     => 'user1',
 
184
         state    => 'Locked',
 
185
         host     => '1.2.3.4:3333'
 
186
      },
 
187
      {
 
188
         time     => '5',
 
189
         command  => 'Query',
 
190
         db       => 'foo',
 
191
         id       => '2',
 
192
         info     => 'select * from foo2;',
 
193
         user     => 'user1',
 
194
         state    => 'Locked',
 
195
         host     => '1.2.3.4:5455'
 
196
      },
 
197
   ],
 
198
   "NAME_lc vertical"
 
199
);
 
200
 
 
201
# #############################################################################
 
202
# Done.
 
203
# #############################################################################
 
204
exit;