1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
#!/usr/bin/perl
BEGIN {
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use Quoter;
use QueryParser;
use ReportFormatter;
use Transformers;
use DSNParser;
use Sandbox;
use CompareWarnings;
use PerconaTest;
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master', {no_lc=>1});
if ( !$dbh ) {
plan skip_all => "Cannot connect to sandbox master";
} elsif ($sandbox_version ge '5.7') {
plan skip_all => "MySQL 5.7 use a more strict sql_mode";
} else {
plan tests => 21;
}
$sb->create_dbs($dbh, ['test']);
Transformers->import(qw(make_checksum));
my $q = new Quoter();
my $qp = new QueryParser();
my %modules = (
Quoter => $q,
QueryParser => $qp,
);
my $cw;
my $report;
my @events;
my $hosts = [
{ dbh => $dbh, name => 'dbh-1' },
{ dbh => $dbh, name => 'dbh-2' },
];
sub proc {
my ( $when, %args ) = @_;
die "I don't know when $when is"
unless $when eq 'before_execute'
|| $when eq 'execute'
|| $when eq 'after_execute';
for my $i ( 0..$#events ) {
$events[$i] = $cw->$when(
event => $events[$i],
dbh => $hosts->[$i]->{dbh},
%args,
);
}
};
sub get_id {
return make_checksum(@_);
}
# #############################################################################
# Test it.
# #############################################################################
diag(`/tmp/12345/use < $trunk/t/lib/samples/compare-warnings.sql`);
@events = (
{
arg => 'select * from test.t',
fingerprint => 'select * from test.t',
sampleno => 1,
},
{
arg => 'select * from test.t',
fingerprint => 'select * from test.t',
sampleno => 1,
},
);
$cw = new CompareWarnings(
'clear-warnings' => 1,
'clear-warnings-table' => 'mysql.bad',
get_id => \&get_id,
%modules,
);
isa_ok($cw, 'CompareWarnings');
eval {
$cw->before_execute(
event => $events[0],
dbh => $dbh,
);
};
like(
$EVAL_ERROR,
qr/^Failed/,
"Can't clear warnings with bad table"
);
$cw = new CompareWarnings(
'clear-warnings' => 1,
get_id => \&get_id,
%modules,
);
eval {
$cw->before_execute(
event => { arg => 'select * from bad.db' },
dbh => $dbh,
);
};
like(
$EVAL_ERROR,
qr/^Failed/,
"Can't clear warnings with query with bad tables"
);
proc('before_execute', db=>'test');
$events[0]->{Query_time} = 123;
proc('execute');
is(
$events[0]->{Query_time},
123,
"execute() doesn't execute if Query_time already exists"
);
ok(
exists $events[1]->{Query_time}
&& $events[1]->{Query_time} >= 0,
"execute() will execute if Query_time doesn't exist ($events[1]->{Query_time})"
);
proc('after_execute');
is(
$events[0]->{warning_count},
0,
'Zero warning count'
);
is_deeply(
$events[0]->{warnings},
{},
'No warnings'
);
# #############################################################################
# Test with the same warning on both hosts.
# #############################################################################
@events = (
{
arg => "insert into test.t values (-2,'hi2',2)",
fingerprint => 'insert into test.t values (?,?,?)',
sampleno => 1,
},
{
arg => "insert into test.t values (-2,'hi2',2)",
fingerprint => 'insert into test.t values (?,?,?)',
sampleno => 1,
},
);
proc('before_execute');
proc('execute');
proc('after_execute');
ok(
$events[0]->{warning_count} == 1 && $events[1]->{warning_count} == 1,
'Both events had 1 warning'
);
is_deeply(
$events[0]->{warnings},
{
'1264' => {
Code => '1264',
Level => 'Warning',
Message => 'Out of range value for column \'i\' at row 1'
}
},
'Event 0 has 1264 warning'
);
is_deeply(
$events[1]->{warnings},
{
'1264' => {
Code => '1264',
Level => 'Warning',
Message => 'Out of range value for column \'i\' at row 1'
}
},
'Event 1 has same 1264 warning'
);
# Compare the warnings: there should be no diffs since they're the same.
is_deeply(
[ $cw->compare(events => \@events, hosts => $hosts) ],
[qw(
different_warning_counts 0
different_warnings 0
different_warning_levels 0
)],
'compare(), no differences'
);
ok(
!exists $events[0]->{warnings}
&& !exists $events[1]->{warnings},
'compare() deletes the warnings hashes from the events'
);
# Add the warnings back with an increased level on the second event.
my $w1 = {
'1264' => {
Code => '1264',
Level => 'Warning',
Message => 'Out of range value for column \'i\' at row 1'
},
};
my $w2 = {
'1264' => {
Code => '1264',
Level => 'Error', # diff
Message => 'Out of range value for column \'i\' at row 1'
},
};
%{$events[0]->{warnings}} = %{$w1};
%{$events[1]->{warnings}} = %{$w2};
is_deeply(
[ $cw->compare(events => \@events, hosts => $hosts) ],
[qw(
different_warning_counts 0
different_warnings 0
different_warning_levels 1
)],
'compare(), same warnings but different levels'
);
$report = <<EOF;
# Warning level differences
# Query ID Code host1 host2 Message
# ================== ==== ======= ===== ======================================
# 4336C4AAA4EEF76B-1 1264 Warning Error Out of range value for column 'i' at row 1
EOF
is(
$cw->report(hosts => $hosts),
$report,
'report warning level difference'
);
$w2->{1264}->{Level} = 'Warning';
$cw->reset();
# Make like the warning didn't happen on the 2nd event.
%{$events[0]->{warnings}} = %{$w1};
$events[0]->{warning_count} = 1;
delete $events[1]->{warnings};
$events[1]->{warning_count} = 0;
is_deeply(
[ $cw->compare(events => \@events, hosts => $hosts) ],
[qw(
different_warning_counts 1
different_warnings 1
different_warning_levels 0
)],
'compare(), warning only on event 0'
);
$report = <<EOF;
# New warnings
# Query ID Host Code Message
# ================== ===== ==== ==========================================
# 4336C4AAA4EEF76B-1 host1 1264 Out of range value for column 'i' at row 1
# Warning count differences
# Query ID host1 host2
# ================== ===== =====
# 4336C4AAA4EEF76B-1 1 0
EOF
is(
$cw->report(hosts => $hosts),
$report,
'report new warning on host 1'
);
# Make like the warning didn't happen on the first event;
delete $events[0]->{warnings};
$events[0]->{warning_count} = 0;
%{$events[1]->{warnings}} = %{$w2};
$events[1]->{warning_count} = 1;
is_deeply(
[ $cw->compare(events => \@events, hosts => $hosts) ],
[qw(
different_warning_counts 1
different_warnings 1
different_warning_levels 0
)],
'compare(), warning only on event 1'
);
$report = <<EOF;
# New warnings
# Query ID Host Code Message
# ================== ===== ==== ==========================================
# 4336C4AAA4EEF76B-1 host2 1264 Out of range value for column 'i' at row 1
# Warning count differences
# Query ID host1 host2
# ================== ===== =====
# 4336C4AAA4EEF76B-1 0 1
EOF
is(
$cw->report(hosts => $hosts),
$report,
'report new warning on host 2'
);
is_deeply(
[ $cw->samples('insert into test.t values (?,?,?)') ],
[ '1', "insert into test.t values (-2,'hi2',2)" ],
'samples()'
);
# #############################################################################
# Done.
# #############################################################################
my $output = '';
{
local *STDERR;
open STDERR, '>', \$output;
$cw->_d('Complete test coverage');
}
like(
$output,
qr/Complete test coverage/,
'_d() works'
);
$sb->wipe_clean($dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;
|