-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.pl
157 lines (125 loc) · 4.52 KB
/
run_tests.pl
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
#!/usr/bin/perl
# runs a list of tests. reports success/failure state for each test on a single line.
# run_tests.pl --suite=suites/suite_volume_tiny.pl --test=2 --repeat=1 --verbose --attr timestamps=0
use warnings;
use strict;
use dt_utils;
use dt_os;
use Getopt::Long;
use File::Spec;
# The attributes hash allows parameterization of the test suites.
# An attribute has a name and a value
%main::attr = ();
my ($suite) = undef; # the name of the suite to load - MANDATORY
my ($test_num) = undef; # The number of test in a suite (starting from 1) or 'all' - MANDATORY
my ($repeat) = -1; # How many times to run the test (0 for infinite)
my $verbose = 0;
my($rc) = GetOptions("suite=s" => \$suite,
"test=s" => \$test_num,
"repeat=i" => \$repeat,
"verbose" => \$verbose,
"attr=s" => \%main::attr);
if (!$rc || !dt_are_all_defined($suite, $test_num)) {
dt_utils_print_error("Missing or bad parameters\n");
exit(1);
}
# @main::tests is an array of anonymous hash references
# Each hash contains 3 pairs, identified by keys 'name', 'script' and 'params'.
# 'name' defines the name of the test, 'script' defines the Perl script that contains
# the test, 'params' defines command-line parameters for the script
@main::tests = ();
# Main tests is now filled from the selected suite
require File::Spec->catfile(".", $suite);
if ($test_num eq 'all') {
# Do nothing, just check that $test_num is valid
} elsif ($test_num >= 1 && $test_num <= scalar(@main::tests)) {
@main::tests = ($main::tests[$test_num - 1]);
} else {
dt_utils_print_error("Illegal test number '$test_num'\n");
exit(1);
}
run_tests(\@main::tests, $repeat, $verbose);
sub run_tests {
my ($tests_ref, $repeat, $verbose) = @_;
my $test_idx = 0;
my ($iteration) = 1;
# this label is for handling repeating test(s) iterations.
while (1) {
if ($repeat == 0) {
dt_utils_print("ITERATION $iteration (repeat == infinite):\n");
} elsif ($repeat > 0) {
dt_utils_print("ITERATION $iteration (of $repeat):\n");
}
for ($test_idx = 0; $test_idx < scalar(@$tests_ref); ++$test_idx) {
if (!run_test($tests_ref->[$test_idx], $test_idx + 1, $verbose)) {
last;
}
}
if ($repeat == -1) {
last;
}
++$iteration;
if ($repeat > 0 && $iteration > $repeat) {
last;
}
}
}
sub run_test {
my $test_ref = $_[0];
my $test_num = $_[1];
my $verbose = $_[2];
my $cmd = "perl " . $test_ref->{script} . " " . $test_ref->{params};
my $test_output_file = undef;
if (!$verbose) {
$test_output_file = File::Spec->catfile(File::Spec->curdir(), ('test_output_' . $$ . '.txt'));
$cmd .= " > $test_output_file 2>&1";
}
my $test_name = $test_ref->{name};
my ($test_dots) = '.' x (60 - length($test_name));
dt_utils_print("RUNNING TEST No. $test_num\n$test_name" . $test_dots . ": ");
# execute the test.
my $time_before = time();
my $exec_err_str = undef;
my $test_rc = dt_os_do_exec(\$exec_err_str, $cmd);
my ($time_formated) = dt_utils_format_elapsed_time(time() - $time_before);
# checking test results...
if ($test_rc) {
dt_utils_print_no_time("OK [elapsed test time: ${time_formated}]\n");
} else {
dt_utils_print_no_time("ERROR [elapsed test time: ${time_formated}]\n");
dt_utils_print "run_test: dt_os_do_exec() failed - $exec_err_str\n";
if (!$verbose) {
emit_test_output($test_output_file);
}
}
if (!$verbose) {
unlink($test_output_file);
}
return $test_rc;
}
sub emit_test_output {
my ($test_output_file) = @_;
dt_utils_print_no_time(" ERRORS:\n ===============================\n");
my (@test_output) = ();
read_test_output_file($test_output_file, \@test_output);
foreach my $line (@test_output) {
print(STDERR $line);
}
dt_utils_print_no_time("===============================\n END ERRORS\n");
}
# read a test output file into memory.
# supplies its contents in the given array on success, or a single error
# line on failure to read the file.
sub read_test_output_file {
my ($test_output_file, $test_output_ref) = @_;
my ($test_fh);
# we use static handles, rather then dynamic, to cope with older perl
# versions.
if (open(TESTOUTFH, "<${test_output_file}")) {
@$test_output_ref = <TESTOUTFH>;
close(TESTOUTFH);
}
else {
push(@$test_output_ref, "read_test_output_file: failed opening test output file '$test_output_file' - $!\n");
}
}