mtr_cases.pl 11.8 KB
Newer Older
kent@mysql.com's avatar
kent@mysql.com committed
1 2 3 4 5 6
# -*- cperl -*-

# This is a library file used by the Perl version of mysql-test-run,
# and is part of the translation of the Bourne shell script with the
# same name.

kent@mysql.com's avatar
kent@mysql.com committed
7
use File::Basename;
kent@mysql.com's avatar
kent@mysql.com committed
8 9 10
use strict;

sub collect_test_cases ($);
11
sub collect_one_test_case ($$$$$$$);
kent@mysql.com's avatar
kent@mysql.com committed
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

##############################################################################
#
#  Collect information about test cases we are to run
#
##############################################################################

sub collect_test_cases ($) {
  my $suite= shift;             # Test suite name

  my $testdir;
  my $resdir;

  if ( $suite eq "main" )
  {
    $testdir= "$::glob_mysql_test_dir/t";
    $resdir=  "$::glob_mysql_test_dir/r";
  }
  else
  {
    $testdir= "$::glob_mysql_test_dir/suite/$suite/t";
    $resdir=  "$::glob_mysql_test_dir/suite/$suite/r";
  }

  my $cases = [];           # Array of hash, will be array of C struct

  opendir(TESTDIR, $testdir) or mtr_error("Can't open dir \"$testdir\": $!");

  if ( @::opt_cases )
  {
    foreach my $tname ( @::opt_cases ) { # Run in specified order, no sort
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
      my $elem= undef;
      my $component_id= undef;
      
      # Get rid of directory part (path). Leave the extension since it is used
      # to understand type of the test.

      $tname = basename($tname);

      # Check if the extenstion has been specified.

      if ( mtr_match_extension($tname, "test") )
      {
        $elem= $tname;
        $tname=~ s/\.test$//;
        $component_id= 'mysqld';
      }
      elsif ( mtr_match_extension($tname, "imtest") )
      {
        $elem= $tname;
        $tname =~ s/\.imtest$//;
        $component_id= 'im';
      }

      # If target component is known, check that the specified test case
      # exists.
      # 
      # Otherwise, try to guess the target component.

kent@mysql.com's avatar
kent@mysql.com committed
71
      if ( $component_id )
kent@mysql.com's avatar
kent@mysql.com committed
72
      {
73 74 75 76
        if ( ! -f "$testdir/$elem")
        {
          mtr_error("Test case $tname ($testdir/$elem) is not found");
        }
kent@mysql.com's avatar
kent@mysql.com committed
77
      }
78 79 80 81 82
      else
      {
        my $mysqld_test_exists = -f "$testdir/$tname.test";
        my $im_test_exists = -f "$testdir/$tname.imtest";

kent@mysql.com's avatar
kent@mysql.com committed
83
        if ( $mysqld_test_exists and $im_test_exists )
84
        {
cmiller@maint1.mysql.com's avatar
cmiller@maint1.mysql.com committed
85
          mtr_error("Ambiguous test case name ($tname)");
86
        }
kent@mysql.com's avatar
kent@mysql.com committed
87
        elsif ( ! $mysqld_test_exists and ! $im_test_exists )
88 89 90 91 92 93 94 95 96 97 98 99 100 101
        {
          mtr_error("Test case $tname is not found");
        }
        elsif ( $mysqld_test_exists )
        {
          $elem= "$tname.test";
          $component_id= 'mysqld';
        }
        elsif ( $im_test_exists )
        {
          $elem= "$tname.imtest";
          $component_id= 'im';
        }
      }
102

103 104
      collect_one_test_case($testdir,$resdir,$tname,$elem,$cases,{},
        $component_id);
kent@mysql.com's avatar
kent@mysql.com committed
105 106 107 108 109
    }
    closedir TESTDIR;
  }
  else
  {
kent@mysql.com's avatar
kent@mysql.com committed
110
    # ----------------------------------------------------------------------
kent@mysql.com's avatar
kent@mysql.com committed
111
    # Disable some tests listed in disabled.def
kent@mysql.com's avatar
kent@mysql.com committed
112
    # ----------------------------------------------------------------------
kent@mysql.com's avatar
kent@mysql.com committed
113
    my %disabled;
kent@mysql.com's avatar
kent@mysql.com committed
114
    if ( ! $::opt_ignore_disabled_def and open(DISABLED, "$testdir/disabled.def" ) )
kent@mysql.com's avatar
kent@mysql.com committed
115
    {
kent@mysql.com's avatar
kent@mysql.com committed
116
      while ( <DISABLED> )
kent@mysql.com's avatar
kent@mysql.com committed
117 118
      {
        chomp;
119
        if ( /^\s*([^\s:]+)\s*:\s*(.*?)\s*$/ )
kent@mysql.com's avatar
kent@mysql.com committed
120
        {
kent@mysql.com's avatar
kent@mysql.com committed
121
          $disabled{$1}= $2;
kent@mysql.com's avatar
kent@mysql.com committed
122 123
        }
      }
kent@mysql.com's avatar
kent@mysql.com committed
124
      close DISABLED;
kent@mysql.com's avatar
kent@mysql.com committed
125 126
    }

kent@mysql.com's avatar
kent@mysql.com committed
127
    foreach my $elem ( sort readdir(TESTDIR) ) {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
      my $component_id= undef;
      my $tname= undef;

      if ($tname= mtr_match_extension($elem, 'test'))
      {
        $component_id = 'mysqld';
      }
      elsif ($tname= mtr_match_extension($elem, 'imtest'))
      {
        $component_id = 'im';
      }
      else
      {
        next;
      }
      
kent@mysql.com's avatar
kent@mysql.com committed
144 145
      next if $::opt_do_test and ! defined mtr_match_prefix($elem,$::opt_do_test);

146 147
      collect_one_test_case($testdir,$resdir,$tname,$elem,$cases,\%disabled,
        $component_id);
kent@mysql.com's avatar
kent@mysql.com committed
148 149 150 151 152 153 154
    }
    closedir TESTDIR;
  }

  # To speed things up, we sort first in if the test require a restart
  # or not, second in alphanumeric order.

kent@mysql.com's avatar
kent@mysql.com committed
155 156
  if ( $::opt_reorder )
  {
157

cmiller@maint1.mysql.com's avatar
cmiller@maint1.mysql.com committed
158 159
    my %sort_criteria;
    my $tinfo;
160

cmiller@maint1.mysql.com's avatar
cmiller@maint1.mysql.com committed
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
    # Make a mapping of test name to a string that represents how that test
    # should be sorted among the other tests.  Put the most important criterion
    # first, then a sub-criterion, then sub-sub-criterion, et c.
    foreach $tinfo (@$cases) 
    {
      my @this_criteria = ();

      # Append the criteria for sorting, in order of importance.
      push(@this_criteria, join("!", sort @{$tinfo->{'master_opt'}}) . "~");  # Ending with "~" makes empty sort later than filled
      push(@this_criteria, "ndb=" . ($tinfo->{'ndb_test'} ? "1" : "0"));
      push(@this_criteria, "restart=" . ($tinfo->{'master_restart'} ? "1" : "0"));
      push(@this_criteria, "big_test=" . ($tinfo->{'big_test'} ? "1" : "0"));
      push(@this_criteria, join("|", sort keys %{$tinfo}));  # Group similar things together.  The values may differ substantially.  FIXME?
      push(@this_criteria, $tinfo->{'name'});   # Finally, order by the name
      
      $sort_criteria{$tinfo->{"name"}} = join(" ", @this_criteria);
    }

    @$cases = sort { $sort_criteria{$a->{"name"}} cmp $sort_criteria{$b->{"name"}}; } @$cases;

###  For debugging the sort-order
#    foreach $tinfo (@$cases) 
#    {
#      print $sort_criteria{$tinfo->{"name"}};
#      print " -> \t";
#      print $tinfo->{"name"};
#      print "\n";
#    }
kent@mysql.com's avatar
kent@mysql.com committed
189
  }
kent@mysql.com's avatar
kent@mysql.com committed
190 191 192 193 194 195 196 197 198 199 200 201

  return $cases;
}


##############################################################################
#
#  Collect information about a single test case
#
##############################################################################


202
sub collect_one_test_case($$$$$$$) {
kent@mysql.com's avatar
kent@mysql.com committed
203 204 205 206 207
  my $testdir= shift;
  my $resdir=  shift;
  my $tname=   shift;
  my $elem=    shift;
  my $cases=   shift;
kent@mysql.com's avatar
kent@mysql.com committed
208
  my $disabled=shift;
209
  my $component_id= shift;
kent@mysql.com's avatar
kent@mysql.com committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

  my $path= "$testdir/$elem";

  # ----------------------------------------------------------------------
  # Skip some tests silently
  # ----------------------------------------------------------------------

  if ( $::opt_start_from and $tname lt $::opt_start_from )
  {
    return;
  }

  # ----------------------------------------------------------------------
  # Skip some tests but include in list, just mark them to skip
  # ----------------------------------------------------------------------

  my $tinfo= {};
  $tinfo->{'name'}= $tname;
  $tinfo->{'result_file'}= "$resdir/$tname.result";
229
  $tinfo->{'component_id'} = $component_id;
kent@mysql.com's avatar
kent@mysql.com committed
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
  push(@$cases, $tinfo);

  if ( $::opt_skip_test and defined mtr_match_prefix($tname,$::opt_skip_test) )
  {
    $tinfo->{'skip'}= 1;
    return;
  }

  # ----------------------------------------------------------------------
  # Collect information about test case
  # ----------------------------------------------------------------------

  $tinfo->{'path'}= $path;
  $tinfo->{'timezone'}= "GMT-3"; # for UNIX_TIMESTAMP tests to work

  if ( defined mtr_match_prefix($tname,"rpl") )
  {
    if ( $::opt_skip_rpl )
    {
      $tinfo->{'skip'}= 1;
      return;
    }

    $tinfo->{'slave_num'}= 1;           # Default, use one slave

    # FIXME currently we always restart slaves
    $tinfo->{'slave_restart'}= 1;

    if ( $tname eq 'rpl_failsafe' or $tname eq 'rpl_chain_temp_table' )
    {
#      $tinfo->{'slave_num'}= 3;         # Not 3 ? Check old code, strange
    }
  }

kent@mysql.com's avatar
kent@mysql.com committed
264 265 266 267 268 269 270 271
  if ( defined mtr_match_prefix($tname,"federated") )
  {
    $tinfo->{'slave_num'}= 1;           # Default, use one slave

    # FIXME currently we always restart slaves
    $tinfo->{'slave_restart'}= 1;
  }

272
  # Cluster is needed by test case if testname contains ndb
273
  if ( defined mtr_match_substring($tname,"ndb") )
274
  {
275
    $tinfo->{'ndb_test'}= 1;
276 277
    if ( $::opt_skip_ndbcluster )
    {
278 279 280 281 282 283 284
      # Skip all ndb tests
      $tinfo->{'skip'}= 1;
      return;
    }
    if ( ! $::opt_with_ndbcluster )
    {
      # Ndb is not supported, skip them
285 286 287 288 289 290 291 292 293
      $tinfo->{'skip'}= 1;
      return;
    }
  }
  else
  {
    $tinfo->{'ndb_test'}= 0;
  }

kent@mysql.com's avatar
kent@mysql.com committed
294 295 296 297 298 299 300
  # FIXME what about embedded_server + ndbcluster, skip ?!

  my $master_opt_file= "$testdir/$tname-master.opt";
  my $slave_opt_file=  "$testdir/$tname-slave.opt";
  my $slave_mi_file=   "$testdir/$tname.slave-mi";
  my $master_sh=       "$testdir/$tname-master.sh";
  my $slave_sh=        "$testdir/$tname-slave.sh";
kent@mysql.com's avatar
kent@mysql.com committed
301
  my $disabled_file=   "$testdir/$tname.disabled";
302
  my $im_opt_file=     "$testdir/$tname-im.opt";
kent@mysql.com's avatar
kent@mysql.com committed
303

kent@mysql.com's avatar
kent@mysql.com committed
304 305
  $tinfo->{'master_opt'}= [];
  $tinfo->{'slave_opt'}=  [];
kent@mysql.com's avatar
kent@mysql.com committed
306 307 308 309 310 311
  $tinfo->{'slave_mi'}=   [];

  if ( -f $master_opt_file )
  {
    $tinfo->{'master_restart'}= 1;    # We think so for now

kent@mysql.com's avatar
kent@mysql.com committed
312
  MASTER_OPT:
kent@mysql.com's avatar
kent@mysql.com committed
313
    {
kent@mysql.com's avatar
kent@mysql.com committed
314
      my $master_opt= mtr_get_opts_from_file($master_opt_file);
kent@mysql.com's avatar
kent@mysql.com committed
315

kent@mysql.com's avatar
kent@mysql.com committed
316
      foreach my $opt ( @$master_opt )
kent@mysql.com's avatar
kent@mysql.com committed
317
      {
kent@mysql.com's avatar
kent@mysql.com committed
318
        my $value;
kent@mysql.com's avatar
kent@mysql.com committed
319

kent@mysql.com's avatar
kent@mysql.com committed
320 321 322
        # This is a dirty hack from old mysql-test-run, we use the opt
        # file to flag other things as well, it is not a opt list at
        # all
kent@mysql.com's avatar
kent@mysql.com committed
323

kent@mysql.com's avatar
kent@mysql.com committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        $value= mtr_match_prefix($opt, "--timezone=");
        if ( defined $value )
        {
          $tinfo->{'timezone'}= $value;
          last MASTER_OPT;
        }

        $value= mtr_match_prefix($opt, "--result-file=");
        if ( defined $value )
        {
          $tinfo->{'result_file'}= "r/$value.result";
          if ( $::opt_result_ext and $::opt_record or
               -f "$tinfo->{'result_file'}$::opt_result_ext")
          {
            $tinfo->{'result_file'}.= $::opt_result_ext;
          }
          $tinfo->{'master_restart'}= 0;
          last MASTER_OPT;
        }

        # If we set default time zone, remove the one we have
        $value= mtr_match_prefix($opt, "--default-time-zone=");
        if ( defined $value )
kent@mysql.com's avatar
kent@mysql.com committed
347
        {
kent@mysql.com's avatar
kent@mysql.com committed
348
          $tinfo->{'master_opt'}= [];
kent@mysql.com's avatar
kent@mysql.com committed
349
        }
kent@mysql.com's avatar
kent@mysql.com committed
350

kent@mysql.com's avatar
kent@mysql.com committed
351
      }
kent@mysql.com's avatar
kent@mysql.com committed
352 353 354

      # Ok, this was a real option list, add it
      push(@{$tinfo->{'master_opt'}}, @$master_opt);
kent@mysql.com's avatar
kent@mysql.com committed
355 356 357 358 359 360
    }
  }

  if ( -f $slave_opt_file )
  {
    $tinfo->{'slave_restart'}= 1;
kent@mysql.com's avatar
kent@mysql.com committed
361 362 363 364 365 366 367 368 369
    my $slave_opt= mtr_get_opts_from_file($slave_opt_file);

    foreach my $opt ( @$slave_opt )
    {
      # If we set default time zone, remove the one we have
      my $value= mtr_match_prefix($opt, "--default-time-zone=");
      $tinfo->{'slave_opt'}= [] if defined $value;
    }
    push(@{$tinfo->{'slave_opt'}}, @$slave_opt);
kent@mysql.com's avatar
kent@mysql.com committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  }

  if ( -f $slave_mi_file )
  {
    $tinfo->{'slave_mi'}= mtr_get_opts_from_file($slave_mi_file);
    $tinfo->{'slave_restart'}= 1;
  }

  if ( -f $master_sh )
  {
    if ( $::glob_win32_perl )
    {
      $tinfo->{'skip'}= 1;
    }
    else
    {
      $tinfo->{'master_sh'}= $master_sh;
      $tinfo->{'master_restart'}= 1;
    }
  }

  if ( -f $slave_sh )
  {
    if ( $::glob_win32_perl )
    {
      $tinfo->{'skip'}= 1;
    }
    else
    {
      $tinfo->{'slave_sh'}= $slave_sh;
      $tinfo->{'slave_restart'}= 1;
    }
  }

404 405 406 407 408 409 410 411 412
  if ( -f $im_opt_file )
  {
    $tinfo->{'im_opts'} = mtr_get_opts_from_file($im_opt_file);
  }
  else
  {
    $tinfo->{'im_opts'} = [];
  }

kent@mysql.com's avatar
kent@mysql.com committed
413
  # FIXME why this late?
kent@mysql.com's avatar
kent@mysql.com committed
414
  if ( $disabled->{$tname} )
kent@mysql.com's avatar
kent@mysql.com committed
415 416 417
  {
    $tinfo->{'skip'}= 1;
    $tinfo->{'disable'}= 1;   # Sub type of 'skip'
kent@mysql.com's avatar
kent@mysql.com committed
418
    $tinfo->{'comment'}= $disabled->{$tname} if $disabled->{$tname};
kent@mysql.com's avatar
kent@mysql.com committed
419 420
  }

kent@mysql.com's avatar
kent@mysql.com committed
421
  if ( -f $disabled_file )
kent@mysql.com's avatar
kent@mysql.com committed
422 423 424
  {
    $tinfo->{'skip'}= 1;
    $tinfo->{'disable'}= 1;   # Sub type of 'skip'
kent@mysql.com's avatar
kent@mysql.com committed
425
    $tinfo->{'comment'}= mtr_fromfile($disabled_file);
kent@mysql.com's avatar
kent@mysql.com committed
426 427
  }

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  if ( $component_id eq 'im' )
  {
    if ( $::glob_use_embedded_server )
    {
      $tinfo->{'skip'}= 1;
      
      mtr_report(
        "Instance Manager tests are not available in embedded mode. " .
        "Test case '$tname' is skipped.");
    }
    elsif ( $::opt_ps_protocol )
    {
      $tinfo->{'skip'}= 1;
      
      mtr_report(
        "Instance Manager tests are not run with --ps-protocol. " .
        "Test case '$tname' is skipped.");
    }
kent@mysql.com's avatar
kent@mysql.com committed
446
    elsif ( $::opt_skip_im )
447 448 449 450 451 452 453 454 455
    {
      $tinfo->{'skip'}= 1;

      mtr_report(
        "Instance Manager executable is unavailable." .
        "Test case '$tname' is skipped.");
    }
  }

kent@mysql.com's avatar
kent@mysql.com committed
456 457 458 459 460 461 462 463 464 465 466
  # We can't restart a running server that may be in use

  if ( $::glob_use_running_server and
       ( $tinfo->{'master_restart'} or $tinfo->{'slave_restart'} ) )
  {
    $tinfo->{'skip'}= 1;
  }
}


1;