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";
10
use warnings FATAL => 'all';
11
use English qw(-no_match_vars);
12
use Test::More tests => 26;
17
my $p = new SysLogParser;
19
# The final line is broken across two lines in the actual log, but it's one
23
file => 't/lib/samples/pg/pg-syslog-005.txt',
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]',
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.
38
my $file = "$trunk/t/lib/samples/pg/pg-syslog-002.txt";
40
open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
42
next_event => sub { return <$fh>; },
43
tell => sub { return tell($fh); },
46
new_event_test => sub {
47
# A simplified PgLogParser::$log_line_regex
48
defined $_[0] && $_[0] =~ m/STATEMENT/;
52
my ( $next_event, $tell, $is_syslog )
53
= $p->generate_wrappers(%parser_args);
57
'pg-syslog-002.txt $tell 0 ok');
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');
65
'pg-syslog-002.txt $tell 1 ok');
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');
73
'pg-syslog-002.txt $tell 2 ok');
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');
81
'pg-syslog-002.txt $tell 3 ok');
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');
89
'pg-syslog-002.txt $tell 4 ok');
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');
100
"No error on samples/pg/pg-syslog-002.txt",
105
# This test case checks a $line_filter, and sees whether lines get proper
108
my $file = "$trunk/t/lib/samples/pg/pg-syslog-003.txt";
110
open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
112
next_event => sub { return <$fh>; },
113
tell => sub { return tell($fh); },
117
# A simplified PgLogParser::$log_line_regex
118
defined $_[0] && $_[0] =~ s/\A\t/\n/; $_[0];
122
my ( $next_event, $tell, $is_syslog )
123
= $p->generate_wrappers(%parser_args);
127
'pg-syslog-003.txt $tell 0 ok');
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)"
143
'pg-syslog-003.txt $next_event 0 ok');
150
"No error on samples/pg/pg-syslog-003.txt",
155
# This test case checks pos_in_log again, without any filters.
157
my $file = "$trunk/t/lib/samples/pg/pg-syslog-005.txt";
159
open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
161
next_event => sub { return <$fh>; },
162
tell => sub { return tell($fh); },
165
my ( $next_event, $tell, $is_syslog )
166
= $p->generate_wrappers(%parser_args);
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]'],
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");
186
"No error on samples/pg/pg-syslog-005.txt",
191
# #############################################################################
193
# #############################################################################
197
open STDERR, '>', \$output;
198
$p->_d('Complete test coverage');
202
qr/Complete test coverage/,