summaryrefslogtreecommitdiff
path: root/cesar/common/tests/run-test.pl
blob: 04c1bfed62c8d33fcd26515205183c8b0761cd80 (plain)
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
#!/usr/bin/perl
#
# Run all defined tests.
#
use strict;
use warnings;
use Cwd;
use Getopt::Long qw(:config no_ignore_case bundling);
use Pod::Usage;

# Option parsing.
my $help;
my $timeout = 5 * 60;
my @include;
my @exclude;
my @include_dir;
GetOptions (
    'help|h' => \$help,
    'timeout|t=i' => \$timeout,
    'include|I=s' => \@include,
    'exclude|X=s' => \@exclude,
    'directory|d=s' => \@include_dir,
) or pod2usage (2);
pod2usage (1) if $help;

@ARGV == 1 or pod2usage (2);

my $base = getcwd . '/' . shift @ARGV;

my %include;
@include{@include} = ();
my %exclude;
@exclude{@exclude} = ();

# Initialise signal number to name table.
use Config;
my @signame;
my %signum;;
my $i = 0;
defined $Config{sig_name} || die "No sigs?";
foreach (split (' ', $Config{sig_name}))
{
    $signame[$i] = $_;
    $signum{$_} = $i;
    $i++;
}

# Test macros.
my %macros = (
    'cov' => \&cov_macro,
    'cov-target' => \&cov_target_macro,
);
my $lcov;

# Read tests file.
my $dir;
my $fail = 0;
while (<STDIN>)
{
    chomp;
    # Drop comments.
    next if /^\s*(?:#.*)?$/;
    # This table will enable an unget system to fake input lines.
    my @ungeted;
    push @ungeted, $_;
    while (@ungeted)
    {
	$_ = shift @ungeted;
	if (/^(.*):(:)?$/)
	{
	    # Directory line.
	    $dir = $1;
	    chdir "$base/$dir"
		or die "cannot change directory";
	    # Push automatic clean test.
	    push @ungeted, '-clean: make -s clean' unless $2;
	}
	else
	{
	    # Test line.
	    defined $dir
		or die "bad format";
	    # Read '-' and '!' flags.
	    my ($dontcare, $expected) = (0, '');
	    $dontcare = 1 if s/^-//;
	    $expected = ' (expected)' if s/^!//;
	    # Decode name and command.
	    my ($name, $cmd);
	    /^(.*?): (.*)$/
		and ($name, $cmd) = ($1, $2)
		or ($name, $cmd) = ($_, $_);
	    # Decode macros.
	    $name =~ /^(.*?) (.*)$/ && exists $macros{$1}
		and ($name, $cmd) = $macros{$1} ($1, $2, $cmd);
	    # Skip test?
	    next if @include_dir && !grep { $dir =~ /^$_/ } @include_dir;
	    next if %include && !exists $include{$name};
	    next if exists $exclude{$name};
	    # Start test.
	    my $t = "=> $dir - $name";
	    print "\n$t\n";
	    my $status = timed_system ($cmd);
	    # Check result.
	    if ($dontcare)
	    {
		print "=$t: DONE\n";
	    }
	    else
	    {
		if ($status != 0)
		{
		    $fail++ unless $expected;
		    if ($status == -1 || $status == 128 << 8) {
			print "=$t: FAIL$expected command not found\n";
		    } elsif ($status & 127) {
			printf "=$t: FAIL$expected killed with signal %s\n",
			    $signame[$status & 127];
			die "interrupted" if ($signame[$status & 127] eq 'INT');
		    } else {
			printf "=$t: FAIL$expected exited with value %d\n",
			    ($status >> 8);
		    }
		}
		else
		{
		    print "=$t: PASS\n";
		}
	    }
	}
    }
}
# Print summary.
if ($fail)
{
    print "\n===> FAIL $fail unexpected tests\n";
    exit 1;
}
else
{
    print "\n===> PASS all tests\n";
    exit 0;
}

sub cov_macro
{
    my ($name, $arg, $cmd, $objdir, $gcov) = @_;
    if (not defined $lcov)
    {
	system ('lcov --version > /dev/null');
	$lcov = $? == 0 ? 1 : 0;
    }
    if ($lcov)
    {
	$objdir = 'obj' unless defined $objdir;
	$gcov = '' unless defined $gcov;
	return ($name,
	    "rm -f obj/$arg.info && "
	    . "lcov -q -d $objdir -b . -z $gcov && "
	    . "$cmd && "
	    . "lcov -q -d $objdir -b . -c -t $arg -o obj/$arg.info $gcov");
    }
    else
    {
	return ('run', $cmd);
    }
}

sub cov_target_macro
{
    my ($name, $arg, $cmd) = @_;
    if (exists $ENV{CROSS_COMPILE_})
    {
	return cov_macro ($name, $arg, $cmd, '.', " -g $ENV{CROSS_COMPILE_}gcov");
    }
    else
    {
	return ('run', $cmd);
    }
}

sub timed_system
{
    my $cmd = shift;
    my $pid = fork;
    defined $pid or die "fork: $!,";
    if ($pid == 0)
    {
	# Son.
	exec $cmd;
	exit 128;
    }
    else
    {
	# Father.
	eval {
	    local $SIG{ALRM} = sub { die "alarm\n" };
	    alarm $timeout;
	    do { $_ = wait } until $_ == $pid;
	    alarm 0;
	};
	if ($@)
	{
	    die unless $@ eq "alarm\n";
	    kill INT => $pid;
	    sleep 3;
	    kill KILL => $pid;
	    print "Timed out!\n";
	    return $signum{ALRM};
	}
	else
	{
	    return $?;
	}
    }
}

__END__

=head1 NAME

run-test.pl - Read a tests file and run all configured tests

=head1 SYNOPSIS

run-test.pl [options] base

 Options:
   -h, --help		brief help message
   -I, --include=NAME	only include named tests, can be issued multiple times
   -X, --exclude=NAME	do not include named tests
   -d, --directory=DIR	only include tests below given directories
   -t, --timeout=SEC	timeout for each test (seconds)

=cut