-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.pl
executable file
·254 lines (221 loc) · 6.62 KB
/
run.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
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
#----------------------------------------------------------------------------
# ZetaScale
# Copyright (c) 2016, SanDisk Corp. and/or all its affiliates.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License version 2.1 as published by the Free
# Software Foundation;
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License v2.1 for more details.
#
# A copy of the GNU Lesser General Public License v2.1 is provided with this package and
# can also be found at: http:#opensource.org/licenses/LGPL-2.1
# You should have received a copy of the GNU Lesser General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA.
#----------------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/wrapper/lib";
use Log::Log4perl;
use Getopt::Long qw(GetOptions);
use Global::Config qw($config);
use Global::CaseMap;
use Global::Utils;
use IO::File;
use File::Basename qw(fileparse);
use File::Path qw(mkpath rmtree);
use File::Spec::Functions;
use TAP::Harness;
use TAP::Formatter::Console;
use TAP::Parser;
use TAP::Parser::Aggregator;
$| = 1;
### version ###
my $VERSION = 0.01;
chomp(my $root_dir = `echo \$(hostname | cut -d'.' -f 1-1):\$(pwd)`);
my $config_file = "conf/log4perl.conf";
my $status = 0;
my %options = (
single_case => "",
help => 0,
plan => "",
cname => "",
build => "",
project => "Admin",
verbosity => 0,
exit_imm => 0,
priority => 0,
);
our $shared = 0;
if (
!GetOptions (
"verbose" => \$options{verbosity},
"case=s" => \$options{single_case},
"project=s" => \$options{project},
"plan=s" => \$options{plan},
"cname=s" => \$options{cname},
"build=s" => \$options{build},
"help" => \$options{help},
"exit_imm" => \$options{exit_imm},
"PR=s" => \$options{priority},
"shared" => \$shared,
)
)
{
usage ();
exit 1;
}
if ($options{help}) {
usage ();
exit 1;
}
unless ( -d "r" ) {
mkdir "r", 0770 or die $!;
}
if($shared) {
$ENV{FDF_TEST_FRAMEWORK_SHARED} = 1;
}
Log::Log4perl->init_and_watch ($config_file);
if ( (not $options{single_case})
and (not $options{cname})
and ((not $options{plan}) or (not $options{build})))
{
print "No test case/suite specified.\n";
usage ();
exit 1;
}
if ($options{cname}) {
listCaseIDByName ();
exit 1;
}
if ($options{priority}) {
die "Wrong priority specified" unless ($options{priority} =~ m/^([hml])/);
$options{pr} = $1;
}
my $last_suit = "";
my $harness = TAP::Harness->new;
my $aggregator = TAP::Parser::Aggregator->new;
$aggregator->start;
if ($options{single_case}) {
chomp $options{single_case};
my @cases = getTestCasesFromLocal ();
foreach my $case (@cases) {
my $parser = runSingleCase ($case);
$aggregator->add (
"# Test Scripts: $case\n" .
"# Log Location: " . catfile($root_dir, get_log_dir ($case)) . "\n ",
$parser);
my $failed = is_test_failed ($parser);
print "Log location " . catfile($root_dir, get_log_dir ($case)) . "\n" if ($failed);
if ($options{plan} and $options{build}) {
my ($case_id) =
grep { $getCaseScriptByID{$_} eq $case }
keys %getCaseScriptByID;
updateResult ($case_id, $failed) if (defined $case_id);
}
$status |= $failed;
last if ($status and $options{exit_imm});
}
}
$aggregator->stop;
$harness->summary ($aggregator);
exit $status;
# Subroutines
sub is_test_failed {
my $parser = shift;
return 1 unless (defined $parser->tests_planned && defined $parser->passed);
return 1 unless ($parser->tests_planned == $parser->passed and $parser->failed == 0);
return 0;
}
sub runSingleCase {
my $case = shift;
my $logger = Log::Log4perl->get_logger;
print "\n=== [" . localtime() . "] Running: $case\n";
$logger->info ("Running: $case");
# Identify log dir
my $log_dir = get_log_dir ($case);
my $log_file = catfile ($log_dir, "tap.log");
mkpath ("$log_dir", 0, 0777) unless (-e "$log_dir");
# Run case
my $hole = IO::File->new ("> /dev/null");
my $cmd = "perl $case 2>&1 | tee $log_file";
my $parser = TAP::Parser->new (
{
exec => [$cmd],
spool => $options{verbosity} ? \*STDOUT : $hole,
merge => 1,
}
);
$parser->run;
$hole->flush ();
close($hole);
return $parser;
}
sub getTestCasesFromLocal {
my @cases;
if (-d $options{single_case}) {
my @files;
my $dh;
push(@files, $options{single_case});
while (@files) {
if (-d $files[0]) {
opendir $dh, $files[0] or die $!;
@_ = grep { /^[^\.]/ } readdir $dh;
foreach (@_) {
push(@files, File::Spec->catfile ($files[0], $_));
}
closedir $dh;
}
elsif ($files[0] =~ /\.t$/) {
push(@cases, $files[0]);
}
shift @files;
}
}
else {
@cases = ($options{single_case});
}
return @cases;
}
sub filterCaseByPriority {
my $result = shift;
for my $key (keys %{$result}) {
my $tc_pr = int($result->{$key}->{priority});
if ($tc_pr >= 6) {
$tc_pr = "high";
} elsif ($tc_pr < 3) {
$tc_pr = "low";
} else {
$tc_pr = "medium";
}
delete $result->{$key} if ($tc_pr !~ qr/$options{pr}/);
}
}
sub sortCaseByTestSuite {
my $result = shift;
my %cases_h = %$result;
my @cases_id = sort { $cases_h{$a}->{testsuite_id} <=> $cases_h{$b}->{testsuite_id} } keys %cases_h;
my @cases_a;
foreach (@cases_id) {
push @cases_a, $cases_h{$_};
}
return @cases_a;
}
sub usage {
print <<EOF
Usage: ./run.pl [ options ... ]
Options:
--verbose verbose to see each case
--case= run single case
--PR= priority (h for high, m for medium, l for low)
--help print this list
--shared allow multiple instances
Examples:
./run.pl --verbose --case=t/sample/basic.t
EOF
}