~percona-toolkit-dev/percona-toolkit/fix-change-master-bug-932614

« back to all changes in this revision

Viewing changes to t/lib/SysLogParser.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 => 26;
 
13
 
 
14
use SysLogParser;
 
15
use MaatkitTest;
 
16
 
 
17
my $p = new SysLogParser;
 
18
 
 
19
# The final line is broken across two lines in the actual log, but it's one
 
20
# logical event.
 
21
test_log_parser(
 
22
   parser => $p,
 
23
   file   => 't/lib/samples/pg/pg-syslog-005.txt',
 
24
   result => [
 
25
      '2010-02-10 09:03:26.918 EST c=4b72bcae.d01,u=[unknown],D=[unknown] LOG:  connection received: host=[local]',
 
26
      '2010-02-10 09:03:26.922 EST c=4b72bcae.d01,u=fred,D=fred LOG:  connection authorized: user=fred database=fred',
 
27
      '2010-02-10 09:03:36.645 EST c=4b72bcae.d01,u=fred,D=fred LOG:  duration: 0.627 ms  statement: select 1;',
 
28
      '2010-02-10 09:03:39.075 EST c=4b72bcae.d01,u=fred,D=fred LOG:  disconnection: session time: 0:00:12.159 user=fred database=fred host=[local]',
 
29
   ],
 
30
);
 
31
 
 
32
# This test case examines $tell and sees whether it's correct or not.  It also
 
33
# tests whether we can correctly pass in a callback that lets the caller
 
34
# override the rules about when a new event is seen.  In this example, we want
 
35
# to break the last event up into two parts, even though they are the same event
 
36
# in the syslog entry.
 
37
{
 
38
   my $file = "$trunk/t/lib/samples/pg/pg-syslog-002.txt";
 
39
   eval {
 
40
      open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
 
41
      my %parser_args = (
 
42
         next_event => sub { return <$fh>; },
 
43
         tell       => sub { return tell($fh);  },
 
44
         fh         => $fh,
 
45
         misc       => {
 
46
            new_event_test => sub {
 
47
               # A simplified PgLogParser::$log_line_regex
 
48
               defined $_[0] && $_[0] =~ m/STATEMENT/;
 
49
            },
 
50
         }
 
51
      );
 
52
      my ( $next_event, $tell, $is_syslog )
 
53
         = $p->generate_wrappers(%parser_args);
 
54
 
 
55
      is ($tell->(),
 
56
         0,
 
57
         'pg-syslog-002.txt $tell 0 ok');
 
58
      is ($next_event->(),
 
59
         '2010-02-08 09:52:41.526 EST c=4b701056.1dc6,u=fred,D=fred LOG: '
 
60
         . ' statement: select * from pg_stat_bgwriter;',
 
61
         'pg-syslog-002.txt $next_event 0 ok');
 
62
 
 
63
      is ($tell->(),
 
64
         153,
 
65
         'pg-syslog-002.txt $tell 1 ok');
 
66
      is ($next_event->(),
 
67
         '2010-02-08 09:52:41.533 EST c=4b701056.1dc6,u=fred,D=fred LOG:  '
 
68
         . 'duration: 8.309 ms',
 
69
         'pg-syslog-002.txt $next_event 1 ok');
 
70
 
 
71
      is ($tell->(),
 
72
         282,
 
73
         'pg-syslog-002.txt $tell 2 ok');
 
74
      is ($next_event->(),
 
75
         '2010-02-08 09:52:57.807 EST c=4b701056.1dc6,u=fred,D=fred LOG:  '
 
76
         . 'statement: create index ix_a on foo (a);',
 
77
         'pg-syslog-002.txt $next_event 2 ok');
 
78
 
 
79
      is ($tell->(),
 
80
         433,
 
81
         'pg-syslog-002.txt $tell 3 ok');
 
82
      is ($next_event->(),
 
83
         '2010-02-08 09:52:57.864 EST c=4b701056.1dc6,u=fred,D=fred ERROR:  '
 
84
         . 'relation "ix_a" already exists',
 
85
         'pg-syslog-002.txt $next_event 3 ok');
 
86
 
 
87
      is ($tell->(),
 
88
         576,
 
89
         'pg-syslog-002.txt $tell 4 ok');
 
90
      is ($next_event->(),
 
91
         '2010-02-08 09:52:57.864 EST c=4b701056.1dc6,u=fred,D=fred STATEMENT:  '
 
92
         . 'create index ix_a on foo (a);',
 
93
         'pg-syslog-002.txt $next_event 4 ok');
 
94
 
 
95
      close $fh;
 
96
   };
 
97
   is(
 
98
      $EVAL_ERROR,
 
99
      '',
 
100
      "No error on samples/pg/pg-syslog-002.txt",
 
101
   );
 
102
 
 
103
}
 
104
 
 
105
# This test case checks a $line_filter, and sees whether lines get proper
 
106
# newline-munging.
 
107
{
 
108
   my $file = "$trunk/t/lib/samples/pg/pg-syslog-003.txt";
 
109
   eval {
 
110
      open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
 
111
      my %parser_args = (
 
112
         next_event => sub { return <$fh>; },
 
113
         tell       => sub { return tell($fh);  },
 
114
         fh         => $fh,
 
115
         misc       => {
 
116
            line_filter => sub {
 
117
               # A simplified PgLogParser::$log_line_regex
 
118
               defined $_[0] && $_[0] =~ s/\A\t/\n/; $_[0];
 
119
            },
 
120
         }
 
121
      );
 
122
      my ( $next_event, $tell, $is_syslog )
 
123
         = $p->generate_wrappers(%parser_args);
 
124
 
 
125
      is ($tell->(),
 
126
         0,
 
127
         'pg-syslog-003.txt $tell 0 ok');
 
128
      is ($next_event->(),
 
129
         "2010-02-08 09:53:51.724 EST c=4b701056.1dc6,u=fred,D=fred LOG:  "
 
130
          . "statement: SELECT n.nspname as \"Schema\","
 
131
          . "\n  c.relname as \"Name\","
 
132
          . "\n  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN"
 
133
          . " 'special' END as \"Type\","
 
134
          . "\n  r.rolname as \"Owner\""
 
135
          . "\nFROM pg_catalog.pg_class c"
 
136
          . "\n     JOIN pg_catalog.pg_roles r ON r.oid = c.relowner"
 
137
          . "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace"
 
138
          . "\nWHERE c.relkind IN ('r','v','S','')"
 
139
          . "\n  AND n.nspname <> 'pg_catalog'"
 
140
          . "\n  AND n.nspname !~ '^pg_toast'"
 
141
          . "\n  AND pg_catalog.pg_table_is_visible(c.oid)"
 
142
          . "\nORDER BY 1,2;",
 
143
         'pg-syslog-003.txt $next_event 0 ok');
 
144
 
 
145
      close $fh;
 
146
   };
 
147
   is(
 
148
      $EVAL_ERROR,
 
149
      '',
 
150
      "No error on samples/pg/pg-syslog-003.txt",
 
151
   );
 
152
 
 
153
}
 
154
 
 
155
# This test case checks pos_in_log again, without any filters.
 
156
{
 
157
   my $file = "$trunk/t/lib/samples/pg/pg-syslog-005.txt";
 
158
   eval {
 
159
      open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
 
160
      my %parser_args = (
 
161
         next_event => sub { return <$fh>; },
 
162
         tell       => sub { return tell($fh);  },
 
163
         fh         => $fh,
 
164
      );
 
165
      my ( $next_event, $tell, $is_syslog )
 
166
         = $p->generate_wrappers(%parser_args);
 
167
 
 
168
      my @pairs = (
 
169
         [0,   '2010-02-10 09:03:26.918 EST c=4b72bcae.d01,u=[unknown],D=[unknown] LOG:  connection received: host=[local]'],
 
170
         [152, '2010-02-10 09:03:26.922 EST c=4b72bcae.d01,u=fred,D=fred LOG:  connection authorized: user=fred database=fred'],
 
171
         [307, '2010-02-10 09:03:36.645 EST c=4b72bcae.d01,u=fred,D=fred LOG:  duration: 0.627 ms  statement: select 1;'],
 
172
         [456, '2010-02-10 09:03:39.075 EST c=4b72bcae.d01,u=fred,D=fred LOG:  disconnection: session time: 0:00:12.159 user=fred database=fred host=[local]'],
 
173
      );
 
174
 
 
175
      foreach my $i ( 0 .. $#pairs) {
 
176
         my $pair = $pairs[$i];
 
177
         is ($tell->(), $pair->[0], "pg-syslog-005.txt \$tell $i ok");
 
178
         is ($next_event->(), $pair->[1], "pg-syslog-005.txt \$next_event $i ok");
 
179
      }
 
180
 
 
181
      close $fh;
 
182
   };
 
183
   is(
 
184
      $EVAL_ERROR,
 
185
      '',
 
186
      "No error on samples/pg/pg-syslog-005.txt",
 
187
   );
 
188
 
 
189
}
 
190
 
 
191
# #############################################################################
 
192
# Done.
 
193
# #############################################################################
 
194
my $output = '';
 
195
{
 
196
   local *STDERR;
 
197
   open STDERR, '>', \$output;
 
198
   $p->_d('Complete test coverage');
 
199
}
 
200
like(
 
201
   $output,
 
202
   qr/Complete test coverage/,
 
203
   '_d() works'
 
204
);
 
205
exit;