2
# Copyright (C) 2005-2006 MySQL AB
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
# This is a library file used by the Perl version of mysql-test-run,
18
# and is part of the translation of the Bourne shell script with the
27
sub collect_test_cases ($);
28
sub collect_one_suite ($);
29
sub collect_one_test_case ($$$$$$$$$);
31
sub mtr_options_from_test_file($$);
37
my ($from, $what)= @_;
38
if ( $from =~ /^[a-z0-9]$/ ) {
39
# Does not contain any regex, make the pattern match
43
# Check that pattern is a valid regex
44
eval { "" =~/$from/; 1 } or
45
mtr_error("Invalid regex '$from' passed to $what\nPerl says: $@");
51
##############################################################################
53
# Collect information about test cases we are to run
55
##############################################################################
57
sub collect_test_cases ($) {
58
$do_test= init_pattern($::opt_do_test, "--do-test");
59
$skip_test= init_pattern($::opt_skip_test, "--skip-test");
61
my $suites= shift; # Semicolon separated list of test suites
62
my $cases = []; # Array of hash
64
foreach my $suite (split(",", $suites))
66
push(@$cases, collect_one_suite($suite));
72
# Check that the tests specified was found
73
# in at least one suite
74
foreach my $test_name_spec ( @::opt_cases )
77
my ($sname, $tname, $extension)= split_testname($test_name_spec);
78
foreach my $test ( @$cases )
80
# test->{name} is always in suite.name format
81
if ( $test->{name} =~ /.*\.$tname/ )
88
mtr_error("Could not find $tname in any suite");
95
# Reorder the test cases in an order that will make them faster to run
98
# Make a mapping of test name to a string that represents how that test
99
# should be sorted among the other tests. Put the most important criterion
100
# first, then a sub-criterion, then sub-sub-criterion, et c.
101
foreach my $tinfo (@$cases)
105
# Look for tests that muct be in run in a defined order
106
# that is defined by test having the same name except for
109
# Put variables into hash
110
my $test_name= $tinfo->{'name'};
111
my $depend_on_test_name;
112
if ( $test_name =~ /^([\D]+)([0-9]{1})$/ )
116
mtr_verbose("$test_name => $base_name idx=$idx");
120
$base_name= "$base_name$idx";
121
mtr_verbose("New basename $base_name");
124
foreach my $tinfo2 (@$cases)
126
if ( $tinfo2->{'name'} eq $base_name )
128
mtr_verbose("found dependent test $tinfo2->{'name'}");
129
$depend_on_test_name=$base_name;
134
if ( defined $depend_on_test_name )
136
mtr_verbose("Giving $test_name same critera as $depend_on_test_name");
137
$sort_criteria{$test_name} = $sort_criteria{$depend_on_test_name};
142
# Append the criteria for sorting, in order of importance.
144
push(@criteria, "ndb=" . ($tinfo->{'ndb_test'} ? "1" : "0"));
145
# Group test with equal options together.
146
# Ending with "~" makes empty sort later than filled
147
push(@criteria, join("!", sort @{$tinfo->{'master_opt'}}) . "~");
149
$sort_criteria{$test_name} = join(" ", @criteria);
154
$sort_criteria{$a->{'name'}} . $a->{'name'} cmp
155
$sort_criteria{$b->{'name'}} . $b->{'name'}; } @$cases;
157
if ( $::opt_script_debug )
159
# For debugging the sort-order
160
foreach my $tinfo (@$cases)
162
print("$sort_criteria{$tinfo->{'name'}} -> \t$tinfo->{'name'}\n");
171
# Valid extensions and their corresonding component id
172
my %exts = ( 'test' => 'mysqld',
177
# Returns (suitename, testname, extension)
181
# Get rid of directory part and split name on .'s
182
my @parts= split(/\./, basename($test_name));
185
# Only testname given, ex: alias
186
return (undef , $parts[0], undef);
187
} elsif (@parts == 2) {
188
# Either testname.test or suite.testname given
189
# Ex. main.alias or alias.test
191
if (defined $exts{$parts[1]})
193
return (undef , $parts[0], $parts[1]);
197
return ($parts[0], $parts[1], undef);
200
} elsif (@parts == 3) {
201
# Fully specified suitename.testname.test
203
return ( $parts[0], $parts[1], $parts[2]);
206
mtr_error("Illegal format of test name: $test_name");
210
sub collect_one_suite($)
212
my $suite= shift; # Test suite name
213
my @cases; # Array of hash
215
mtr_verbose("Collecting: $suite");
217
my $suitedir= "$::glob_mysql_test_dir"; # Default
218
if ( $suite ne "main" )
220
$suitedir= mtr_path_exists("$suitedir/suite/$suite",
222
mtr_verbose("suitedir: $suitedir");
225
my $testdir= "$suitedir/t";
226
my $resdir= "$suitedir/r";
228
# ----------------------------------------------------------------------
229
# Build a hash of disabled testcases for this suite
230
# ----------------------------------------------------------------------
232
if ( open(DISABLED, "$testdir/disabled.def" ) )
237
if ( /^\s*(\S+)\s*:\s*(.*?)\s*$/ )
245
# Read suite.opt file
246
my $suite_opt_file= "$testdir/suite.opt";
248
if ( -f $suite_opt_file )
250
$suite_opts= mtr_get_opts_from_file($suite_opt_file);
255
# Collect in specified order
256
foreach my $test_name_spec ( @::opt_cases )
258
my ($sname, $tname, $extension)= split_testname($test_name_spec);
260
# The test name parts have now been defined
261
#print " suite_name: $sname\n";
262
#print " tname: $tname\n";
263
#print " extension: $extension\n";
265
# Check cirrect suite if suitename is defined
266
next if (defined $sname and $suite ne $sname);
269
if ( defined $extension )
271
my $full_name= "$testdir/$tname.$extension";
272
# Extension was specified, check if the test exists
273
if ( ! -f $full_name)
275
# This is only an error if suite was specified, otherwise it
276
# could exist in another suite
277
mtr_error("Test '$full_name' was not found in suite '$sname'")
282
$component_id= $exts{$extension};
286
# No extension was specified
287
my ($ext, $component);
288
while (($ext, $component)= each %exts) {
289
my $full_name= "$testdir/$tname.$ext";
291
if ( ! -f $full_name ) {
294
$component_id= $component;
297
# Test not found here, could exist in other suite
298
next unless $component_id;
301
collect_one_test_case($testdir,$resdir,$suite,$tname,
302
"$tname.$extension",\@cases,\%disabled,
303
$component_id,$suite_opts);
308
opendir(TESTDIR, $testdir) or mtr_error("Can't open dir \"$testdir\": $!");
310
foreach my $elem ( sort readdir(TESTDIR) )
312
my $component_id= undef;
315
if ($tname= mtr_match_extension($elem, 'test'))
317
$component_id = 'mysqld';
319
elsif ($tname= mtr_match_extension($elem, 'imtest'))
321
$component_id = 'im';
328
# Skip tests that does not match the --do-test= filter
329
next if ($do_test and not $tname =~ /$do_test/o);
331
collect_one_test_case($testdir,$resdir,$suite,$tname,
332
$elem,\@cases,\%disabled,$component_id,
339
# Return empty list if no testcases found
340
return if (@cases == 0);
342
# ----------------------------------------------------------------------
343
# Read combinations for this suite and build testcases x combinations
344
# if any combinations exists
345
# ----------------------------------------------------------------------
346
if ( ! $::opt_skip_combination )
349
my $combination_file= "$suitedir/combinations";
350
#print "combination_file: $combination_file\n";
351
if (@::opt_combinations)
353
# take the combination from command-line
354
mtr_verbose("Take the combination from command line");
355
foreach my $combination (@::opt_combinations) {
357
$comb->{name}= $combination;
358
push(@{$comb->{comb_opt}}, $combination);
359
push(@combinations, $comb);
362
elsif (-f $combination_file )
364
# Read combinations file in my.cnf format
365
mtr_verbose("Read combinations file");
366
my $config= My::Config->new($combination_file);
368
foreach my $group ($config->groups()) {
370
$comb->{name}= $group->name();
371
foreach my $option ( $group->options() ) {
372
push(@{$comb->{comb_opt}}, $option->name()."=".$option->value());
374
push(@combinations, $comb);
380
print " - adding combinations\n";
381
#print_testcases(@cases);
384
foreach my $comb (@combinations)
386
foreach my $test (@cases)
388
#print $test->{name}, " ", $comb, "\n";
391
while (my ($key, $value) = each(%$test)) {
392
if (ref $value eq "ARRAY") {
393
push(@{$new_test->{$key}}, @$value);
395
$new_test->{$key}= $value;
399
# Append the combination options to master_opt and slave_opt
400
push(@{$new_test->{master_opt}}, @{$comb->{comb_opt}});
401
push(@{$new_test->{slave_opt}}, @{$comb->{comb_opt}});
403
# Add combination name shrt name
404
$new_test->{combination}= $comb->{name};
406
# Add the new test to new test cases list
407
push(@new_cases, $new_test);
410
#print_testcases(@new_cases);
412
#print_testcases(@cases);
416
optimize_cases(\@cases);
417
#print_testcases(@cases);
424
# Loop through all test cases
425
# - optimize which test to run by skipping unnecessary ones
426
# - update settings if necessary
431
foreach my $tinfo ( @$cases )
433
# Skip processing if already marked as skipped
434
next if $tinfo->{skip};
436
# Replication test needs an adjustment of binlog format
437
if (mtr_match_prefix($tinfo->{'name'}, "rpl"))
440
# =======================================================
441
# Get binlog-format used by this test from master_opt
442
# =======================================================
443
my $test_binlog_format;
444
foreach my $opt ( @{$tinfo->{master_opt}} ) {
445
$test_binlog_format= $test_binlog_format ||
446
mtr_match_prefix($opt, "--binlog-format=");
448
# print $tinfo->{name}." uses ".$test_binlog_format."\n";
450
# =======================================================
451
# If a special binlog format was selected with
452
# --mysqld=--binlog-format=x, skip all test with different
454
# =======================================================
455
if (defined $::used_binlog_format and
456
$test_binlog_format and
457
$::used_binlog_format ne $test_binlog_format)
460
$tinfo->{'comment'}= "Requires --binlog-format='$test_binlog_format'";
464
# =======================================================
465
# Check that testcase supports the designated binlog-format
466
# =======================================================
467
if ($test_binlog_format and defined $tinfo->{'sup_binlog_formats'} )
470
grep { $_ eq $test_binlog_format } @{$tinfo->{'sup_binlog_formats'}};
475
"Doesn't support --binlog-format='$test_binlog_format'";
480
# =======================================================
481
# Use dynamic switching of binlog-format if mtr started
482
# w/o --mysqld=--binlog-format=xxx and combinations.
483
# =======================================================
484
if (!defined $tinfo->{'combination'} and
485
!defined $::used_binlog_format)
487
$test_binlog_format= $tinfo->{'sup_binlog_formats'}->[0];
490
# Save binlog format for dynamic switching
491
$tinfo->{binlog_format}= $test_binlog_format;
497
##############################################################################
499
# Collect information about a single test case
501
##############################################################################
504
sub collect_one_test_case($$$$$$$$$) {
512
my $component_id= shift;
513
my $suite_opts= shift;
515
my $path= "$testdir/$elem";
517
# ----------------------------------------------------------------------
518
# Skip some tests silently
519
# ----------------------------------------------------------------------
521
if ( $::opt_start_from and $tname lt $::opt_start_from )
528
$tinfo->{'name'}= basename($suite) . ".$tname";
529
$tinfo->{'result_file'}= "$resdir/$tname.result";
530
$tinfo->{'component_id'} = $component_id;
531
push(@$cases, $tinfo);
533
# ----------------------------------------------------------------------
534
# Skip some tests but include in list, just mark them to skip
535
# ----------------------------------------------------------------------
537
if ( $skip_test and $tname =~ /$skip_test/o )
543
# ----------------------------------------------------------------------
544
# Collect information about test case
545
# ----------------------------------------------------------------------
547
$tinfo->{'path'}= $path;
548
$tinfo->{'timezone'}= "GMT-3"; # for UNIX_TIMESTAMP tests to work
550
$tinfo->{'slave_num'}= 0; # Default, no slave
551
$tinfo->{'master_num'}= 1; # Default, 1 master
552
if ( defined mtr_match_prefix($tname,"rpl") )
554
if ( $::opt_skip_rpl )
557
$tinfo->{'comment'}= "No replication tests(--skip-rpl)";
561
$tinfo->{'slave_num'}= 1; # Default for rpl* tests, use one slave
565
if ( defined mtr_match_prefix($tname,"federated") )
567
# Default, federated uses the first slave as it's federated database
568
$tinfo->{'slave_num'}= 1;
571
my $master_opt_file= "$testdir/$tname-master.opt";
572
my $slave_opt_file= "$testdir/$tname-slave.opt";
573
my $slave_mi_file= "$testdir/$tname.slave-mi";
574
my $master_sh= "$testdir/$tname-master.sh";
575
my $slave_sh= "$testdir/$tname-slave.sh";
576
my $disabled_file= "$testdir/$tname.disabled";
577
my $im_opt_file= "$testdir/$tname-im.opt";
579
$tinfo->{'master_opt'}= [];
580
$tinfo->{'slave_opt'}= [];
581
$tinfo->{'slave_mi'}= [];
585
foreach my $opt ( @$suite_opts )
588
push(@{$tinfo->{'master_opt'}}, $opt);
589
push(@{$tinfo->{'slave_opt'}}, $opt);
593
if ( -f $master_opt_file )
596
my $master_opt= mtr_get_opts_from_file($master_opt_file);
598
foreach my $opt ( @$master_opt )
602
# The opt file is used both to send special options to the mysqld
603
# as well as pass special test case specific options to this
606
$value= mtr_match_prefix($opt, "--timezone=");
607
if ( defined $value )
609
$tinfo->{'timezone'}= $value;
613
$value= mtr_match_prefix($opt, "--slave-num=");
614
if ( defined $value )
616
$tinfo->{'slave_num'}= $value;
620
$value= mtr_match_prefix($opt, "--result-file=");
621
if ( defined $value )
623
# Specifies the file mysqltest should compare
625
$tinfo->{'result_file'}= "r/$value.result";
629
# If we set default time zone, remove the one we have
630
$value= mtr_match_prefix($opt, "--default-time-zone=");
631
if ( defined $value )
633
# Set timezone for this test case to something different
634
$tinfo->{'timezone'}= "GMT-8";
635
# Fallthrough, add the --default-time-zone option
638
# The --restart option forces a restart even if no special
639
# option is set. If the options are the same as next testcase
640
# there is no need to restart after the testcase
642
if ( $opt eq "--force-restart" )
644
$tinfo->{'force_restart'}= 1;
648
# Ok, this was a real option, add it
649
push(@{$tinfo->{'master_opt'}}, $opt);
654
if ( -f $slave_opt_file )
656
my $slave_opt= mtr_get_opts_from_file($slave_opt_file);
658
foreach my $opt ( @$slave_opt )
660
# If we set default time zone, remove the one we have
661
my $value= mtr_match_prefix($opt, "--default-time-zone=");
662
$tinfo->{'slave_opt'}= [] if defined $value;
664
push(@{$tinfo->{'slave_opt'}}, @$slave_opt);
667
if ( -f $slave_mi_file )
669
$tinfo->{'slave_mi'}= mtr_get_opts_from_file($slave_mi_file);
674
if ( $::glob_win32_perl )
677
$tinfo->{'comment'}= "No tests with sh scripts on Windows";
682
$tinfo->{'master_sh'}= $master_sh;
688
if ( $::glob_win32_perl )
691
$tinfo->{'comment'}= "No tests with sh scripts on Windows";
696
$tinfo->{'slave_sh'}= $slave_sh;
700
if ( -f $im_opt_file )
702
$tinfo->{'im_opts'} = mtr_get_opts_from_file($im_opt_file);
706
$tinfo->{'im_opts'} = [];
709
# FIXME why this late?
710
my $marked_as_disabled= 0;
711
if ( $disabled->{$tname} )
713
$marked_as_disabled= 1;
714
$tinfo->{'comment'}= $disabled->{$tname};
717
if ( -f $disabled_file )
719
$marked_as_disabled= 1;
720
$tinfo->{'comment'}= mtr_fromfile($disabled_file);
723
# If test was marked as disabled, either opt_enable_disabled is off and then
724
# we skip this test, or it is on and then we run this test but warn
726
if ( $marked_as_disabled )
728
if ( $::opt_enable_disabled )
730
$tinfo->{'dont_skip_though_disabled'}= 1;
735
$tinfo->{'disable'}= 1; # Sub type of 'skip'
740
if ( $component_id eq 'im' )
742
if ( $::glob_use_embedded_server )
745
$tinfo->{'comment'}= "No IM with embedded server";
748
elsif ( $::opt_ps_protocol )
751
$tinfo->{'comment'}= "No IM with --ps-protocol";
754
elsif ( $::opt_skip_im )
757
$tinfo->{'comment'}= "No IM tests(--skip-im)";
763
mtr_options_from_test_file($tinfo,"$testdir/${tname}.test");
765
if ( defined $::used_default_engine )
767
# Different default engine is used
768
# tag test to require that engine
769
$tinfo->{'ndb_test'}= 1
770
if ( $::used_default_engine =~ /^ndb/i );
772
$tinfo->{'innodb_test'}= 1
773
if ( $::used_default_engine =~ /^innodb/i );
776
if ( $tinfo->{'big_test'} and ! $::opt_big_test )
779
$tinfo->{'comment'}= "Test need 'big-test' option";
783
if ( $tinfo->{'ndb_extra'} and ! $::opt_ndb_extra_test )
786
$tinfo->{'comment'}= "Test need 'ndb_extra' option";
790
if ( $tinfo->{'require_manager'} )
793
$tinfo->{'comment'}= "Test need the _old_ manager(to be removed)";
797
if ( $tinfo->{'need_debug'} && ! $::debug_compiled_binaries )
800
$tinfo->{'comment'}= "Test need debug binaries";
804
if ( $tinfo->{'ndb_test'} )
807
if ( ! $::glob_ndbcluster_supported )
809
# Ndb is not supported, skip it
811
$tinfo->{'comment'}= "No ndbcluster support";
814
elsif ( $::opt_skip_ndbcluster )
816
# All ndb test's should be skipped
818
$tinfo->{'comment'}= "No ndbcluster tests(--skip-ndbcluster)";
821
# Ndb tests run with two mysqld masters
822
$tinfo->{'master_num'}= 2;
826
# This is not a ndb test
827
if ( $::opt_with_ndbcluster_only )
829
# Only the ndb test should be run, all other should be skipped
831
$tinfo->{'comment'}= "Only ndbcluster tests(--with-ndbcluster-only)";
836
if ( $tinfo->{'innodb_test'} )
838
# This is a test that need innodb
839
if ( $::mysqld_variables{'innodb'} ne "TRUE" )
841
# innodb is not supported, skip it
843
$tinfo->{'comment'}= "No innodb support";
848
if ( $tinfo->{'need_binlog'} )
850
if (grep(/^--skip-log-bin/, @::opt_extra_mysqld_opt) )
853
$tinfo->{'comment'}= "Test need binlog";
859
if ( $::mysql_version_id >= 50100 )
861
# Test does not need binlog, add --skip-binlog to
862
# the options used when starting it
863
push(@{$tinfo->{'master_opt'}}, "--skip-log-bin");
871
# List of tags in the .test files that if found should set
872
# the specified value in "tinfo"
875
["include/have_innodb.inc", "innodb_test", 1],
876
["include/have_binlog_format_row.inc", "sup_binlog_formats", ["row"]],
877
["include/have_log_bin.inc", "need_binlog", 1],
878
["include/have_binlog_format_statement.inc",
879
"sup_binlog_formats", ["statement"]],
880
["include/have_binlog_format_mixed.inc", "sup_binlog_formats", ["mixed"]],
881
["include/have_binlog_format_mixed_or_row.inc",
882
"sup_binlog_formats", ["mixed","row"]],
883
["include/have_binlog_format_mixed_or_statement.inc",
884
"sup_binlog_formats", ["mixed","statement"]],
885
["include/have_binlog_format_row_or_statement.inc",
886
"sup_binlog_formats", ["row","statement"]],
887
["include/big_test.inc", "big_test", 1],
888
["include/have_debug.inc", "need_debug", 1],
889
["include/have_ndb.inc", "ndb_test", 1],
890
["include/have_multi_ndb.inc", "ndb_test", 1],
891
["include/have_ndb_extra.inc", "ndb_extra", 1],
892
["include/ndb_master-slave.inc", "ndb_test", 1],
893
["require_manager", "require_manager", 1],
896
sub mtr_options_from_test_file($$) {
899
#mtr_verbose("$file");
900
my $F= IO::File->new($file) or mtr_error("can't open file \"$file\": $!");
902
while ( my $line= <$F> )
905
# Skip line if it start's with #
906
next if ( $line =~ /^#/ );
908
# Match this line against tag in "tags" array
909
foreach my $tag (@tags)
911
if ( index($line, $tag->[0]) >= 0 )
913
# Tag matched, assign value to "tinfo"
914
$tinfo->{"$tag->[1]"}= $tag->[2];
918
# If test sources another file, open it as well
919
if ( $line =~ /^\-\-([[:space:]]*)source(.*)$/ or
920
$line =~ /^([[:space:]]*)source(.*);$/ )
923
$value =~ s/^\s+//; # Remove leading space
924
$value =~ s/[[:space:]]+$//; # Remove ending space
926
my $sourced_file= "$::glob_mysql_test_dir/$value";
927
if ( -f $sourced_file )
929
# Only source the file if it exists, we may get
930
# false positives in the regexes above if someone
931
# writes "source nnnn;" in a test case(such as mysqltest.test)
932
mtr_options_from_test_file($tinfo, $sourced_file);
939
sub print_testcases {
942
print "=" x 60, "\n";
943
foreach my $test (@cases){
944
print "[", $test->{name}, "]", "\n";
945
while ((my ($key, $value)) = each(%$test)) {
946
print " ", $key, "=";
947
if (ref $value eq "ARRAY") {
948
print join(", ", @$value);
956
print "=" x 60, "\n";