Debian Codegraph @

storage/maria/unittest/ma_test_all-t

raw
1 #!/usr/bin/env perl
2 #
3 # Run various unit tests.
4 #
5
6 use Getopt::Long;
7 use File::Basename;
8
9 $|= 1;
10 $^W = 1; # warnings, because env cannot parse 'perl -w'
11 $VER= "1.5";
12
13 $opt_version= 0;
14 $opt_help= 0;
15 $opt_verbose= 0;
16 $opt_abort_on_error= 0;
17 $opt_valgrind= "valgrind --alignment=8 --leak-check=yes";
18 $opt_silent= "-s";
19 $opt_number_of_tests= 0;
20 $opt_run_tests= undef();
21
22 my $maria_path; # path to "storage/maria"
23 my $maria_exe_path; # path to executables (ma_test1, aria_chk etc)
24 my $my_progname= $0;
25 $my_progname=~ s/.*[\/]//;
26 my $runtime_error= 0; # Return 1 if error(s) occur during run
27 my $NEW_TEST= 0; # Test group separator in an array of tests
28 my $test_begin= 0;
29 my $test_end= 0;
30 my $test_counter= 0;
31 my $using_internal_tmpdir= 0;
32 my $full_tmpdir;
33 my $tmpdir="tmp";
34 my $exec_dir="TMP-ma_test_all"; # Run test in this directory
35 run_tests();
36
37 ####
38 #### Initialise variables, clean temporary files and run the tests
39 ####
40
41 sub run_tests
42 {
43 my $nr_tests= 0;
44 my $flag_exit= 0;
45
46 if (!GetOptions("help" => \$opt_help,
47 "version" => \$opt_version,
48 "verbose" => \$opt_verbose,
49 "abort-on-error" => \$opt_abort_on_error,
50 "valgrind=s" => \$opt_valgrind,
51 "silent=s" => \$opt_silent,
52 "tmpdir=s" => \$full_tmpdir,
53 "number-of-tests" => \$opt_number_of_tests,
54 "run-tests=s" => \$opt_run_tests,
55 "start-from=s" => \$opt_run_tests))
56 {
57 $flag_exit= 1;
58 }
59 if ($opt_version)
60 {
61 print "$my_progname version $VER\n";
62 exit(0);
63 }
64
65 if (! -d $exec_dir)
66 {
67 die if (!mkdir("$exec_dir"));
68 }
69 chdir($exec_dir);
70
71 $maria_path= "../" . dirname($0) . "/..";
72
73 my $suffix= ( $^O =~ /win/i && $^O !~ /darwin/i ) ? ".exe" : "";
74 $maria_exe_path= "$maria_path/release";
75 # we use -f, sometimes -x is unexpectedly false in Cygwin
76 if ( ! -f "$maria_exe_path/ma_test1$suffix" )
77 {
78 $maria_exe_path= "$maria_path/relwithdebinfo";
79 if ( ! -f "$maria_exe_path/ma_test1$suffix" )
80 {
81 $maria_exe_path= "$maria_path/debug";
82 if ( ! -f "$maria_exe_path/ma_test1$suffix" )
83 {
84 $maria_exe_path= $maria_path;
85 if ( ! -f "$maria_exe_path/ma_test1$suffix" )
86 {
87 die("Cannot find ma_test1 executable in $maria_path\n");
88 }
89 }
90 }
91 }
92
93 usage() if ($opt_help || $flag_exit);
94
95 if (defined($full_tmpdir))
96 {
97 $tmpdir= $full_tmpdir;
98 }
99 else
100 {
101 $full_tmpdir= $tmpdir;
102 $using_internal_tmpdir= 1;
103 if (! -d "$full_tmpdir")
104 {
105 die if (!mkdir("$full_tmpdir"));
106 }
107 }
108
109 #
110 # IMPORTANT: If you modify this file, please read this:
111 #
112 # Count total number of tests. Make sure that the functions return
113 # number of unit tests correctly, e.g. calls to ok(). The last argument
114 # for each function is a flag counter and will return the number of
115 # unit tests in each. Please see comments on function ok() at the end.
116 #
117 # If you modify any functions or add any new ones, please make sure the
118 # unit tests are appropriately detected here. A wrong count will
119 # make the unit test fail during 'make test'. $nr_tests must be right.
120 #
121
122 $nr_tests+= run_check_tests(0, 0, 0, 0, 1) * 5; #
123 $nr_tests+= run_repair_tests(0, 0, 0, 0, 1) * 5; # called 4 times
124 $nr_tests+= run_pack_tests(0, 0, 0, 0, 1) * 5; #
125 $nr_tests+= run_tests_on_warnings_and_errors(0, 0, 0, 1);
126 $nr_tests+= run_ma_test_recovery(0, 1);
127 $nr_tests+= run_tests_on_clrs(0, 0, 1);
128
129 if ($opt_number_of_tests)
130 {
131 print "Total number of tests is $nr_tests\n";
132 exit(0);
133 }
134
135 if (defined($opt_run_tests))
136 {
137 if ($opt_run_tests =~ m/^(\d+)$/ ||
138 $opt_run_tests =~ m/^(\d+)\.+$/)
139 {
140 $test_begin= $1;
141 }
142 elsif ($opt_run_tests =~ m/^(\d+)\.+(\d+)$/)
143 {
144 $test_begin= $1;
145 $test_end= $2;
146 }
147 else
148 {
149 print "Wrong syntax for option --run-tests=$opt_run_tests\n";
150 print "Please use --run-tests=<begin>..<end>\nwhere 'begin' is the ";
151 print "first test to be run and 'end' is the last.\n";
152 exit(1);
153 }
154 if ($test_end > $nr_tests)
155 {
156 print "Test range ($test_begin..$test_end) out of range. ";
157 print "There are only $nr_tests in the test suite.\n";
158 exit(1);
159 }
160 $test_begin++ if (!$test_begin); # Handle zero, if user gave that
161 if ($test_end && $test_begin > $test_end)
162 {
163 print "Bad test range ($test_begin..$test_end)\n";
164 exit(1);
165 }
166 # Now adjust number of tests
167 $nr_tests= ($test_end ? $test_end : $nr_tests) - $test_begin + 1;
168 }
169
170 #
171 # clean-up
172 #
173
174 unlink_all_possible_tmp_files();
175
176 #
177 # Run tests
178 #
179
180 if (!$opt_verbose)
181 {
182 print "1..$nr_tests\n";
183 }
184 else
185 {
186 print "Total tests: $nr_tests\n";
187 }
188
189 if ($opt_verbose)
190 {
191 print "Running tests with dynamic row format\n"
192 }
193 run_check_tests($suffix, $opt_silent, "", $opt_verbose, 0);
194 run_repair_tests($suffix, $opt_silent, "", $opt_verbose, 0);
195 run_pack_tests($suffix, $opt_silent, "", $opt_verbose, 0);
196
197 if ($opt_verbose)
198 {
199 print "\nRunning tests with static row format\n";
200 }
201 run_check_tests($suffix, $opt_silent, "-S", $opt_verbose, 0);
202 run_repair_tests($suffix, $opt_silent, "-S", $opt_verbose, 0);
203 run_pack_tests($suffix, $opt_silent, "-S", $opt_verbose, 0);
204
205 if ($opt_verbose)
206 {
207 print "\nRunning tests with block row format\n";
208 }
209 run_check_tests($suffix, $opt_silent, "-M", $opt_verbose, 0);
210 run_repair_tests($suffix, $opt_silent, "-M", $opt_verbose, 0);
211 run_pack_tests($suffix, $opt_silent, "-M", $opt_verbose, 0);
212
213 if ($opt_verbose)
214 {
215 print "\nRunning tests with block row format and transactions\n";
216 }
217 run_check_tests($suffix, $opt_silent, "-M -T", $opt_verbose, 0);
218 run_repair_tests($suffix, $opt_silent, "-M -T", $opt_verbose, 0);
219 run_pack_tests($suffix, $opt_silent, "-M -T", $opt_verbose, 0);
220
221 if ($opt_verbose)
222 {
223 print "\nRunning tests with block row format, transactions and versioning\n";
224 }
225 run_check_tests($suffix, $opt_silent, "-M -T -C", $opt_verbose, 0);
226 run_repair_tests($suffix, $opt_silent, "-M -T -C", $opt_verbose, 0);
227 run_pack_tests($suffix, $opt_silent, "-M -T -C", $opt_verbose, 0);
228
229
230 if ($opt_verbose)
231 {
232 print "\nRunning tests with warnings and recovery\n";
233 }
234 run_tests_on_warnings_and_errors($suffix, $opt_silent, $opt_verbose, 0);
235 run_ma_test_recovery($opt_verbose, 0);
236 run_tests_on_clrs($suffix, $opt_verbose, 0);
237
238 unlink_all_possible_tmp_files();
239 if ($using_internal_tmpdir)
240 {
241 rmdir($tmpdir);
242 }
243 rmdir($exec_dir);
244 chdir("..");
245 rmdir($exec_dir);
246 exit($runtime_error);
247 }
248
249 ####
250 #### regular tests
251 ####
252
253 sub run_check_tests
254 {
255 my ($suffix, $silent, $row_type, $verbose, $count)= @_;
256 my ($i, $nr_tests);
257 my @ma_test1_opt= ( ["","-se"],
258 ["-N","-se"],
259 ["-P --checksum","-se"],
260 ["-P -N","-se"],
261 ["-B -N -R2","-sm"],
262 ["-a -k 480 --unique","-sm"],
263 ["-a -N -R1 ","-sm"],
264 ["-p","-sm"],
265 ["-p -N --unique","-sm"],
266 ["-p -N --key_length=127 --checksum","-sm"],
267 ["-p -N --key_length=128","-sm"],
268 ["-p --key_length=480","-sm"],
269 ["-a -B","-sm"],
270 ["-a -B --key_length=64 --unique","-sm"],
271 ["-a -B -k 480 --checksum","-sm"],
272 ["-a -B -k 480 -N --unique --checksum","-sm"],
273 ["-a -m","-sm"],
274 ["-a -m -P --unique --checksum","-sm"],
275 ["-a -m -P --key_length=480 --key_cache","-sm"],
276 ["-m -p","-sm"],
277 ["-w --unique","-sm"],
278 ["-a -w --key_length=64 --checksum","-sm"],
279 ["-a -w -N --key_length=480","-sm"],
280 ["-a -w --key_length=480 --checksum","-sm"],
281 ["-a -b -N","-sm"],
282 ["-a -b --key_length=480","-sm"],
283 ["-p -B --key_length=480","-sm"],
284 ["--checksum --unique","-se"],
285 ["--unique","-se"],
286 ["--rows-no-data", "-s"],
287 ["--key_multiple -N -S","-sm"],
288 ["--key_multiple -a -p --key_length=480","-sm"],
289 ["--key_multiple -a -B --key_length=480","-sm"],
290 ["--key_multiple -P -S","-sm"] );
291 my @ma_test2_opt= ( ["-L -K -W -P","-sm"],
292 ["-L -K -W -P -A","-sm"],
293 ["-L -K -W -P -b32768", "-sm"],
294 ["-L -K -W -P -M -T -c -b32768 -t4 -m300", "-sm"],
295 ["-L -K -P -R3 -m50 -b1000000", "-sm"],
296 ["-L -B","-sm"],
297 ["-D -B -c","-sm"],
298 ["-m10000 -e4096 -K","-sm"],
299 ["-m10000 -e8192 -K","-sm"],
300 ["-m10000 -e16384 -E16384 -K -L","-sm"],
301 ["-L -K -W -P -b32768", "-se"],
302 ["-c -b65000","-se"] );
303 my @ma_rt_test_opt= ( ); # (["--checksum", "-se"] );
304
305
306 if ($count)
307 {
308 $nr_tests= 2; # Number of tests outside loops
309 for ($i= 0; defined($ma_test1_opt[$i]); $i++) { $nr_tests+=2; }
310 for ($i= 0; defined($ma_test2_opt[$i]); $i++) { $nr_tests+=2; }
311 for ($i= 0; defined($ma_rt_test_opt[$i]); $i++) { $nr_tests+=2; }
312 return $nr_tests;
313 }
314
315 for ($i= 0; defined($ma_test1_opt[$i]); $i++)
316 {
317 unlink_log_files();
318 ok("$maria_exe_path/ma_test1$suffix $silent -h$tmpdir $ma_test1_opt[$i][0] $row_type",
319 $verbose, $i + 1);
320 ok("$maria_exe_path/aria_chk$suffix -h$tmpdir $ma_test1_opt[$i][1] $tmpdir/test1",
321 $verbose, $i + 1);
322 }
323 #
324 # These tests are outside the loops. Make sure to include them in
325 # nr_tests manually
326 #
327 ok("$maria_exe_path/aria_pack$suffix --force -s $tmpdir/test1", $verbose, 0);
328 ok("$maria_exe_path/aria_chk$suffix -ess $tmpdir/test1", $verbose, 0);
329
330 for ($i= 0; defined($ma_test2_opt[$i]); $i++)
331 {
332 unlink_log_files();
333 ok("$maria_exe_path/ma_test2$suffix $silent -h$tmpdir $ma_test2_opt[$i][0] $row_type",
334 $verbose, $i + 1);
335 ok("$maria_exe_path/aria_chk$suffix -h$tmpdir $ma_test2_opt[$i][1] $tmpdir/test2",
336 $verbose, $i + 1);
337 }
338
339 for ($i= 0; defined($ma_rt_test_opt[$i]); $i++)
340 {
341 unlink_log_files();
342 ok("$maria_exe_path/ma_rt_test$suffix $silent -h$tmpdir $ma_rt_test_opt[$i][0] $row_type",
343 $verbose, $i + 1);
344 ok("$maria_exe_path/aria_chk$suffix -h$tmpdir $ma_rt_test_opt[$i][1] $tmpdir/rt_test",
345 $verbose, $i + 1);
346 }
347
348 unlink_log_files();
349
350 return 0;
351 }
352
353 ####
354 #### repair tests
355 ####
356
357 sub run_repair_tests()
358 {
359 my ($suffix, $silent, $row_type, $verbose, $count)= @_;
360 my ($i);
361
362 my @t= ($NEW_TEST,
363 "$maria_exe_path/ma_test1$suffix $silent --checksum $row_type",
364 "$maria_exe_path/aria_chk$suffix -se test1",
365 "$maria_exe_path/aria_chk$suffix --silent -re --transaction-log test1",
366 "$maria_exe_path/aria_chk$suffix -rs test1",
367 "$maria_exe_path/aria_chk$suffix -se test1",
368 "$maria_exe_path/aria_chk$suffix -rqs test1",
369 "$maria_exe_path/aria_chk$suffix -se test1",
370 "$maria_exe_path/aria_chk$suffix -rs --correct-checksum test1",
371 "$maria_exe_path/aria_chk$suffix -se test1",
372 "$maria_exe_path/aria_chk$suffix -rqs --correct-checksum test1",
373 "$maria_exe_path/aria_chk$suffix -se test1",
374 "$maria_exe_path/aria_chk$suffix -ros --correct-checksum test1",
375 "$maria_exe_path/aria_chk$suffix -se test1",
376 "$maria_exe_path/aria_chk$suffix -rqos --correct-checksum test1",
377 "$maria_exe_path/aria_chk$suffix -se test1",
378 "$maria_exe_path/aria_chk$suffix -sz test1",
379 "$maria_exe_path/aria_chk$suffix -se test1",
380 "$maria_exe_path/ma_test2$suffix $silent -c -d1 $row_type",
381 "$maria_exe_path/aria_chk$suffix -s --parallel-recover test2",
382 "$maria_exe_path/aria_chk$suffix -se test2",
383 "$maria_exe_path/aria_chk$suffix -s --parallel-recover --quick test2",
384 "$maria_exe_path/aria_chk$suffix -se test2",
385 "$maria_exe_path/ma_test2$suffix $silent -c $row_type",
386 "$maria_exe_path/aria_chk$suffix -se test2",
387 "$maria_exe_path/aria_chk$suffix -sr test2",
388 "$maria_exe_path/aria_chk$suffix -se test2",
389 "$maria_exe_path/ma_test2$suffix $silent -c -t4 -b32768 $row_type",
390 "$maria_exe_path/aria_chk$suffix -s --zerofill test1",
391 "$maria_exe_path/aria_chk$suffix -se test1"
392 );
393
394 return &count_tests(\@t) if ($count);
395 &run_test_bunch(\@t, $verbose, 0);
396 return 0;
397 }
398
399 ####
400 #### pack tests
401 ####
402
403 sub run_pack_tests()
404 {
405 my ($suffix, $silent, $row_type, $verbose, $count)= @_;
406 my ($i);
407
408 my @t= ($NEW_TEST,
409 "$maria_exe_path/ma_test1$suffix $silent --checksum $row_type",
410 "$maria_exe_path/aria_pack$suffix --force -s test1",
411 "$maria_exe_path/aria_chk$suffix -ess test1",
412 "$maria_exe_path/aria_chk$suffix -rqs test1",
413 "$maria_exe_path/aria_chk$suffix -es test1",
414 "$maria_exe_path/aria_chk$suffix -rs test1",
415 "$maria_exe_path/aria_chk$suffix -es test1",
416 "$maria_exe_path/aria_chk$suffix -rus test1",
417 "$maria_exe_path/aria_chk$suffix -es test1",
418 $NEW_TEST,
419 "$maria_exe_path/ma_test1$suffix $silent --checksum $row_type",
420 "$maria_exe_path/aria_pack$suffix --force -s test1",
421 "$maria_exe_path/aria_chk$suffix -rus --safe-recover test1",
422 "$maria_exe_path/aria_chk$suffix -es test1",
423 $NEW_TEST,
424 "$maria_exe_path/ma_test1$suffix $silent --checksum -S $row_type",
425 "$maria_exe_path/aria_chk$suffix -se test1",
426 "$maria_exe_path/aria_chk$suffix -ros test1",
427 "$maria_exe_path/aria_chk$suffix -rqs test1",
428 "$maria_exe_path/aria_chk$suffix -se test1",
429 $NEW_TEST,
430 "$maria_exe_path/aria_pack$suffix --force -s test1",
431 "$maria_exe_path/aria_chk$suffix -rqs test1",
432 "$maria_exe_path/aria_chk$suffix -es test1",
433 "$maria_exe_path/aria_chk$suffix -rus test1",
434 "$maria_exe_path/aria_chk$suffix -es test1",
435 $NEW_TEST,
436 "$maria_exe_path/ma_test2$suffix $silent -c -d1 $row_type",
437 "$maria_exe_path/aria_chk$suffix -s --parallel-recover test2",
438 "$maria_exe_path/aria_chk$suffix -se test2",
439 "$maria_exe_path/aria_chk$suffix -s --unpack --parallel-recover test2",
440 "$maria_exe_path/aria_chk$suffix -se test2",
441 "$maria_exe_path/aria_pack$suffix --force -s test1",
442 "$maria_exe_path/aria_chk$suffix -s --unpack --parallel-recover test2",
443 "$maria_exe_path/aria_chk$suffix -se test2",
444 $NEW_TEST,
445 "$maria_exe_path/ma_test1$suffix $silent -c $row_type",
446 "cp test1.MAD test2.MAD",
447 "cp test1.MAI test2.MAI",
448 "$maria_exe_path/aria_pack$suffix --force -s --join=test3 test1 test2",
449 );
450
451 return (&count_tests(\@t) + 3) if ($count);
452 &run_test_bunch(\@t, $verbose, 0);
453
454 ok("$maria_exe_path/aria_chk -s test3", $verbose, 0, 1);
455 @t= ("$maria_exe_path/aria_chk -s --safe-recover test3",
456 "$maria_exe_path/aria_chk -s test3");
457 &run_test_bunch(\@t, $verbose, 0);
458
459 return 0;
460 }
461
462 ####
463 #### Tests that gives warnings or errors
464 ####
465
466 sub run_tests_on_warnings_and_errors
467 {
468 my ($suffix, $silent, $verbose, $count)= @_;
469 my ($com);
470
471 return 9 if ($count); # Number of tests in this function, e.g. calls to ok()
472
473 ok("$maria_exe_path/ma_test2$suffix -h$tmpdir $silent -L -K -W -P -S -R1 -m500",
474 $verbose, 0);
475 ok("$maria_exe_path/aria_chk$suffix -h$tmpdir -sm $tmpdir/test2", $verbose, 0);
476 # ma_test2$suffix $silent -L -K -R1 -m2000 ; Should give error 135\n
477 # In the following a failure is a success and success is a failure
478 $com= "$maria_exe_path/ma_test2$suffix -h$tmpdir $silent -L -K -R1 -m2000 ";
479 $com.= ">ma_test2_message.txt 2>&1";
480 ok($com, $verbose, 0, 1);
481 ok("cat ma_test2_message.txt", $verbose, 0);
482 ok("grep \"Error: 135\" ma_test2_message.txt > /dev/null", $verbose, 0);
483 # maria_exe_path/aria_chk$suffix -h$tmpdir -sm $tmpdir/test2 will warn that
484 # Datafile is almost full
485 ok("$maria_exe_path/aria_chk$suffix -h$tmpdir -sm $tmpdir/test2 >ma_test2_message.txt 2>&1",
486 $verbose, 0, 1);
487 ok("cat ma_test2_message.txt", $verbose, 0);
488 ok("grep \"warning: Datafile is almost full\" ma_test2_message.txt>/dev/null",
489 $verbose, 0);
490 unlink <ma_test2_message.txt>;
491 ok("$maria_exe_path/aria_chk$suffix -h$tmpdir -ssm $tmpdir/test2", $verbose, 0);
492
493 return 0;
494 }
495
496 ####
497 #### Test that removing tables and applying the log leads to identical tables
498 ####
499
500 sub run_ma_test_recovery
501 {
502 my ($verbose, $count)= @_;
503
504 return 1 if ($count); # Number of tests in this function
505 ok("$maria_path/unittest/ma_test_recovery.pl", $verbose, 0);
506 return 0;
507 }
508
509 ####
510 #### Tests on CLR's
511 ####
512
513 sub run_tests_on_clrs
514 {
515 my ($suffix, $verbose, $count)= @_;
516 my ($i);
517
518 my @t= ($NEW_TEST,
519 "$maria_exe_path/ma_test2$suffix -h$tmpdir -s -L -K -W -P -M -T -c -b -t2 -A1",
520 "cp $tmpdir/aria_log_control $tmpdir/aria_log_control.backup",
521 "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
522 "$maria_exe_path/aria_chk$suffix -h$tmpdir -s -e $tmpdir/test2",
523 "mv $tmpdir/aria_log_control.backup $tmpdir/aria_log_control",
524 "rm $tmpdir/test2.MA?",
525 "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
526 "$maria_exe_path/aria_chk$suffix -h$tmpdir -s -e $tmpdir/test2",
527 "rm $tmpdir/test2.MA?",
528 $NEW_TEST,
529 "$maria_exe_path/ma_test2$suffix -h$tmpdir -s -L -K -W -P -M -T -c -b -t2 -A1",
530 "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir ",
531 "$maria_exe_path/aria_chk$suffix -h$tmpdir -s -e $tmpdir/test2",
532 "rm $tmpdir/test2.MA?",
533 "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
534 "$maria_exe_path/aria_chk$suffix -h$tmpdir -e -s $tmpdir/test2",
535 "rm $tmpdir/test2.MA?",
536 $NEW_TEST,
537 "$maria_exe_path/ma_test2$suffix -h$tmpdir -s -L -K -W -P -M -T -c -b32768 -t4 -A1",
538 "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
539 "$maria_exe_path/aria_chk$suffix -h$tmpdir -es $tmpdir/test2",
540 "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir ",
541 "$maria_exe_path/aria_chk$suffix -h$tmpdir -es $tmpdir/test2",
542 "rm $tmpdir/test2.MA?",
543 "$maria_exe_path/aria_read_log$suffix -a -s -h$tmpdir",
544 "$maria_exe_path/aria_chk$suffix -h$tmpdir -es $tmpdir/test2",
545 "rm $tmpdir/test2.MA?"
546 );
547
548 return &count_tests(\@t) if ($count);
549 &run_test_bunch(\@t, $verbose, 1);
550 return 0;
551 }
552
553 #
554 # Print "ok" on success and "not ok" on error
555 #
556 # Note: Every time this function is called it will be counted
557 # as a unit test.
558 #
559 # Args: $com: The actual command run. Will be printed on a failure
560 # $verbose: Be more verbose.
561 # $iteration: Number of iterations in a loop when the error
562 # occurred. If not in loop, this should be blank
563 # (e.g. send zero).
564 # $expected_error: Optional; put here expected error code. Test
565 # will pass with this result only.
566 #
567 # Return value: Will return 1 on success and 0 on an error
568 #
569
570 sub ok
571 {
572 my ($com, $verbose, $iteration, $expected_error)= @_;
573 my ($msg, $output, $err, $errcode, $len);
574
575 $test_counter++;
576 if ($test_begin > $test_counter)
577 {
578 return 0;
579 }
580 if ($test_end && $test_end < $test_counter)
581 {
582 exit(0);
583 }
584
585 $msg= "";
586 $expected_error= 0 if (!defined($expected_error));
587
588 if ($verbose)
589 {
590 # Print command with out the long unittest/../ prefix
591 my $tmp;
592 $tmp= $com;
593 $tmp =~ s|^unittest/../||;
594 print "$tmp ";
595 $len= length($tmp);
596 }
597 $output= `$com 2>&1`;
598 if ($verbose)
599 {
600 print " " x (62 - $len);
601 }
602 $err= $?;
603 $errcode= ($? >> 8);
604 if ((!$err && !$expected_error) ||
605 ($errcode == $expected_error && $expected_error))
606 {
607 print "[ " if ($verbose);
608 print "ok";
609 if ($verbose)
610 {
611 print " ]";
612 print " " x (5 - length("$test_counter"));
613 print "$test_counter";
614 }
615 else
616 {
617 print " $test_counter - $com"
618 }
619 print "\n";
620 return 1;
621 }
622 print "[ " if ($verbose);
623 print "not ok";
624 print " ]" if ($verbose);
625 print " $test_counter - $com" unless $verbose;
626 print "\n";
627 if ($verbose && defined($output) && length($output))
628 {
629 print "$output\n";
630 }
631 if (!$verbose)
632 {
633 $msg= "\n"; # Get a nicer output in perl unit test mode
634 }
635 $msg.= "Failed test '$com' ";
636 if ($iteration)
637 {
638 $msg.= "(loop iteration $iteration.) ";
639 }
640 $msg.= "at line ";
641 $msg.= (caller)[2];
642 $msg.= "\n(errcode: $errcode, test: $test_counter)\n";
643 if ($expected_error)
644 {
645 $msg.= "Was expecting errcode: $expected_error\n";
646 }
647 warn $msg;
648 $runtime_error= 1;
649 if ($opt_abort_on_error)
650 {
651 exit 1;
652 }
653 # Unlink all files so that we can continue on error
654 unlink_all_possible_tmp_files();
655 return 0;
656 }
657
658 #
659 # Print "skip" and the reason
660 #
661 # Note: Every time this function is called it will be counted
662 # as a unit test.
663 #
664 # Args: $com: The actual command run. Will be printed on a failure
665 # $reason: The reason to skip a test
666 # $verbose: Be more verbose.
667 #
668
669 sub skip
670 {
671 my ($com, $reason, $verbose)= @_;
672
673 $test_counter++;
674 return 0 if $test_begin > $test_counter;
675 exit 0 if $test_end && $test_end < $test_counter;
676 printf '%-64s[ skipped ]%5d', $com, $test_counter if $verbose;
677 print "ok $test_counter # skip $reason" unless $verbose;
678 print "\n";
679 return 1;
680 }
681
682 ####
683 #### Count tests
684 #### Arguments: $t: an array of the tests
685 ####
686
687 sub count_tests
688 {
689 my ($t)= @_;
690 my ($i, $nr_tests);
691
692 $nr_tests= 0;
693 for ($i= 0; defined(@$t[$i]); $i++) { $nr_tests++ if (@$t[$i]); }
694 return $nr_tests;
695 }
696
697 sub unlink_log_files
698 {
699 unlink "$full_tmpdir/aria_log_control", "$full_tmpdir/aria_log.00000001", "$full_tmpdir/aria_log.00000002";
700 }
701
702 sub unlink_all_possible_tmp_files()
703 {
704 unlink_log_files();
705
706 # Unlink tmp files that may have been created when testing the test programs
707 unlink <$full_tmpdir/*.TMD $full_tmpdir/aria_read_log_test1.txt $full_tmpdir/test1*.MA? $full_tmpdir/ma_test_recovery.output aria_log_control aria_log.00000001 aria_log.00000002 aria_logtest1.MA? test1.MA? test2.MA? test3.MA? *.TMD>;
708 }
709
710 ####
711 #### Run a bunch of tests
712 #### Arguments: $t: an array of the tests
713 #### $verbose: to be passed to ok()
714 #### $clear: clear log files if set
715 ####
716
717 sub run_test_bunch
718 {
719 my ($t, $verbose, $clear)= @_;
720 my ($i);
721
722 for ($i= 0; defined(@$t[$i]); $i++)
723 {
724 if ($clear && @$t[$i] eq $NEW_TEST)
725 {
726 unlink_log_files();
727 }
728 if (@$t[$i] ne $NEW_TEST)
729 {
730 ok(@$t[$i], $verbose, $i + 1);
731 }
732 }
733 }
734
735 ####
736 #### usage
737 ####
738
739 sub usage
740 {
741 print <<EOF;
742 $my_progname version $VER
743
744 Description:
745
746 Run various Aria related tests. Typically used via make test as a unittest.
747
748 Options
749 --help Show this help and exit.
750 --abort-on-error Abort at once in case of error.
751 --number-of-tests Print the total number of tests and exit.
752 --run-tests=... Test range that should be run. You can give just
753 one number, to start tests from this test, or a range.
754 For example 45..89. To run a specific test alone,
755 for example test 215, use --run-tests=215..215
756 Use this option with caution, because some of the tests
757 might depend on previous ones.
758 --start-from=... Alias for --run-tests
759 --silent=... Silent option passed to ma_test* tests ('$opt_silent')
760 --tmpdir=... Store tests data in this directory (works for most tests)
761 --valgrind=... Options for valgrind.
762 ('$opt_valgrind')
763 --verbose Be more verbose. Will print each unittest on a line
764 and result after. This mode cannot be used with unit.pl
765 when running in normal unit test mode.
766 --version Show version number and exit.
767 EOF
768 exit(0);
769 }