]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl.in
Update debian/changelog
[i3/i3] / testcases / complete-run.pl.in
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 # © 2010 Michael Stapelberg and contributors
4 package complete_run;
5 use strict;
6 use warnings;
7 use v5.10;
8 use utf8;
9 # the following are modules which ship with Perl (>= 5.10):
10 use Pod::Usage;
11 use File::Temp qw(tempfile tempdir);
12 use Getopt::Long;
13 use POSIX ();
14 use TAP::Harness;
15 use TAP::Parser;
16 use TAP::Parser::Aggregator;
17 use Time::HiRes qw(time);
18 use IO::Handle;
19
20 # these are shipped with the testsuite
21 use lib qw(@abs_top_builddir@/testcases/lib @abs_top_srcdir@/testcases/lib);
22 use i3test::Util qw(slurp);
23 use StartXServer;
24 use StatusLine;
25 use TestWorker;
26 # the following modules are not shipped with Perl
27 use AnyEvent;
28 use AnyEvent::Util;
29 use AnyEvent::Handle;
30 use AnyEvent::I3 qw(:all);
31 use X11::XCB::Connection;
32 use JSON::XS; # AnyEvent::I3 depends on it, too.
33
34 binmode STDOUT, ':utf8';
35 binmode STDERR, ':utf8';
36
37 # Close superfluous file descriptors which were passed by running in a VIM
38 # subshell or situations like that.
39 AnyEvent::Util::close_all_fds_except(0, 1, 2);
40
41 # convenience wrapper to write to the log file
42 my $log;
43 sub Log { say $log "@_" }
44
45 my %timings;
46 my $help = 0;
47 # Number of tests to run in parallel. Important to know how many Xephyr
48 # instances we need to start (unless @displays are given). Defaults to
49 # num_cores * 2.
50 my $parallel = undef;
51 my @displays = ();
52 my %options = (
53     valgrind => 0,
54     strace => 0,
55     xtrace => 0,
56     coverage => 0,
57     restart => 0,
58 );
59 my $keep_xserver_output = 0;
60
61 my $result = GetOptions(
62     "coverage-testing" => \$options{coverage},
63     "keep-xserver-output" => \$keep_xserver_output,
64     "valgrind" => \$options{valgrind},
65     "strace" => \$options{strace},
66     "xtrace" => \$options{xtrace},
67     "display=s" => \@displays,
68     "parallel=i" => \$parallel,
69     "help|?" => \$help,
70 );
71
72 pod2usage(-verbose => 2, -exitcode => 0) if $help;
73
74 # Check for missing executables
75 my @binaries = qw(
76                    @abs_top_builddir@/i3
77                    @abs_top_builddir@/i3bar/i3bar
78                    @abs_top_builddir@/i3-config-wizard/i3-config-wizard
79                    @abs_top_builddir@/i3-dump-log/i3-dump-log
80                    @abs_top_builddir@/i3-input/i3-input
81                    @abs_top_builddir@/i3-msg/i3-msg
82                    @abs_top_builddir@/i3-nagbar/i3-nagbar
83                );
84
85 foreach my $binary (@binaries) {
86     die "$binary executable not found, did you run “make”?" unless -e $binary;
87     die "$binary is not an executable" unless -x $binary;
88 }
89
90 $ENV{PATH} = join(':',
91     '@abs_top_builddir@/i3-nagbar',
92     '@abs_top_builddir@/i3-msg',
93     '@abs_top_builddir@/i3-input',
94     '@abs_top_builddir@/i3-dump-log',
95     '@abs_top_builddir@/i3-config-wizard',
96     '@abs_top_builddir@/i3bar',
97     '@abs_top_builddir@',
98     '@abs_top_srcdir@',
99     $ENV{PATH});
100
101 qx(Xephyr -help 2>&1);
102 die "Xephyr was not found in your path. Please install Xephyr (xserver-xephyr on Debian)." if $?;
103
104 @displays = split(/,/, join(',', @displays));
105 @displays = map { s/ //g; $_ } @displays;
106
107 # 2: get a list of all testcases
108 my @testfiles = @ARGV;
109
110 # if no files were passed on command line, run all tests from t/
111 if (scalar @testfiles == 0) {
112     @testfiles = <@abs_top_srcdir@/testcases/t/*.t> if @testfiles == 0;
113 } else {
114     @testfiles = map {
115         # Fully qualify each specified file if necessary
116         if (! -e $_) {
117             $_ = "@abs_top_srcdir@/testcases/$_";
118         }
119         $_
120     } @testfiles;
121 }
122
123 my $numtests = scalar @testfiles;
124
125 # No displays specified, let’s start some Xephyr instances.
126 if (@displays == 0) {
127     @displays = start_xserver($parallel, $numtests, $keep_xserver_output);
128 }
129
130 # 1: create an output directory for this test-run
131 my $outdir = "testsuite-";
132 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
133 $outdir .= `git describe --tags`;
134 chomp($outdir);
135 mkdir($outdir) or die "Could not create $outdir";
136 unlink("latest") if -l "latest";
137 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
138
139
140 # connect to all displays for two reasons:
141 # 1: check if the display actually works
142 # 2: keep the connection open so that i3 is not the only client. this prevents
143 #    the X server from exiting
144 my @single_worker;
145 for my $display (@displays) {
146     my $screen;
147     my $x = X11::XCB::Connection->new(display => $display);
148     if ($x->has_error) {
149         die "Could not connect to display $display\n";
150     } else {
151         # start a TestWorker for each display
152         push @single_worker, worker($display, $x, $outdir, \%options);
153     }
154 }
155
156 # Read previous timing information, if available. We will be able to roughly
157 # predict the test duration and schedule a good order for the tests.
158 my $timingsjson = slurp('.last_run_timings.json') if -e '.last_run_timings.json';
159 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
160
161 # Re-order the files so that those which took the longest time in the previous
162 # run will be started at the beginning to not delay the whole run longer than
163 # necessary.
164 @testfiles = map  { $_->[0] }
165              sort { $b->[1] <=> $a->[1] }
166              map  { [$_, $timings{$_} // 999] } @testfiles;
167
168 # Run 000-load-deps.t first to bail out early when dependencies are missing.
169 my $loadtest = "t/000-load-deps.t";
170 if ((scalar grep { $_ eq $loadtest } @testfiles) > 0) {
171     @testfiles = ($loadtest, grep { $_ ne $loadtest } @testfiles);
172 }
173
174 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
175     if exists($timings{GLOBAL});
176
177 # Forget the old timings, we don’t necessarily run the same set of tests as
178 # before. Otherwise we would end up with left-overs.
179 %timings = (GLOBAL => time());
180
181 my $logfile = "$outdir/complete-run.log";
182 open $log, '>', $logfile or die "Could not create '$logfile': $!";
183 $log->autoflush(1);
184 say "Writing logfile to '$logfile'...";
185
186 # 3: run all tests
187 my @done;
188 my $num = @testfiles;
189 my $harness = TAP::Harness->new({ });
190
191 my $aggregator = TAP::Parser::Aggregator->new();
192 $aggregator->start();
193
194 status_init(displays => \@displays, tests => $num);
195
196 my $single_cv = AE::cv;
197
198 # We start tests concurrently: For each display, one test gets started. Every
199 # test starts another test after completing.
200 for (@single_worker) {
201     $single_cv->begin;
202     take_job($_, $single_cv, \@testfiles);
203 }
204
205 $single_cv->recv;
206
207 $aggregator->stop();
208
209 # print empty lines to separate failed tests from statuslines
210 print "\n\n";
211
212 for (@done) {
213     my ($test, $output) = @$_;
214     say "no output for $test" unless $output;
215     Log "output for $test:";
216     Log $output;
217     # print error messages of failed tests
218     say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
219 }
220
221 # 4: print summary
222 $harness->summary($aggregator);
223
224 close $log;
225
226 # 5: Save the timings for better scheduling/prediction next run.
227 $timings{GLOBAL} = time() - $timings{GLOBAL};
228 open(my $fh, '>', '.last_run_timings.json');
229 print $fh encode_json(\%timings);
230 close($fh);
231
232 # 6: Print the slowest test files.
233 my @slowest = map  { $_->[0] }
234               sort { $b->[1] <=> $a->[1] }
235               map  { [$_, $timings{$_}] }
236               grep { !/^GLOBAL$/ } keys %timings;
237 say '';
238 say 'The slowest tests are:';
239 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
240     for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
241
242 # When we are running precisely one test, print the output. Makes developing
243 # with a single testcase easier.
244 if ($numtests == 1) {
245     say '';
246     say 'Test output:';
247     say slurp($logfile);
248 }
249
250 END { cleanup() }
251
252 # Report logfiles that match “(Leak|Address)Sanitizer:”.
253 my @logs_with_leaks;
254 for my $log (<$outdir/i3-log-for-*>) {
255     if (slurp($log) =~ /(Leak|Address)Sanitizer:/) {
256         push @logs_with_leaks, $log;
257     }
258 }
259 if (scalar @logs_with_leaks > 0) {
260     say "\nThe following test logfiles contain AddressSanitizer or LeakSanitizer reports:";
261     for my $log (sort @logs_with_leaks) {
262         say "\t$log";
263     }
264 }
265
266 exit ($aggregator->failed > 0);
267
268 #
269 # Takes a test from the beginning of @testfiles and runs it.
270 #
271 # The TAP::Parser (which reads the test output) will get called as soon as
272 # there is some activity on the stdout file descriptor of the test process
273 # (using an AnyEvent->io watcher).
274 #
275 # When a test completes and @done contains $num entries, the $cv condvar gets
276 # triggered to finish testing.
277 #
278 sub take_job {
279     my ($worker, $cv, $tests) = @_;
280
281     my $test = shift @$tests
282         or return $cv->end;
283
284     my $display = $worker->{display};
285
286     Log status($display, "$test: starting");
287     $timings{$test} = time();
288     worker_next($worker, $test);
289
290     # create a TAP::Parser with an in-memory fh
291     my $output;
292     my $parser = TAP::Parser->new({
293         source => do { open(my $fh, '<', \$output); $fh },
294     });
295
296     my $ipc = $worker->{ipc};
297
298     my $w;
299     $w = AnyEvent->io(
300         fh => $ipc,
301         poll => 'r',
302         cb => sub {
303             state $tests_completed = 0;
304             state $partial = '';
305
306             sysread($ipc, my $buf, 4096) or die "sysread: $!";
307
308             if ($partial) {
309                 $buf = $partial . $buf;
310                 $partial = '';
311             }
312
313             # make sure we feed TAP::Parser complete lines so it doesn't blow up
314             if (substr($buf, -1, 1) ne "\n") {
315                 my $nl = rindex($buf, "\n");
316                 if ($nl == -1) {
317                     $partial = $buf;
318                     return;
319                 }
320
321                 # strip partial from buffer
322                 $partial = substr($buf, $nl + 1, '');
323             }
324
325             # count lines before stripping eof-marker otherwise we might
326             # end up with for (1 .. 0) { } which would effectivly skip the loop
327             my $lines = $buf =~ tr/\n//;
328             my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
329
330             $output .= $buf;
331
332             for (1 .. $lines) {
333                 my $result = $parser->next;
334                 next unless defined($result);
335                 if ($result->is_test) {
336                     $tests_completed++;
337                     status($display, "$test: [$tests_completed/??] ");
338                 } elsif ($result->is_bailout) {
339                     Log status($display, "$test: BAILOUT");
340                     status_completed(scalar @done);
341                     say "";
342                     say "test $test bailed out: " . $result->explanation;
343                     exit 1;
344                 }
345             }
346
347             return unless $t_eof;
348
349             Log status($display, "$test: finished");
350             $timings{$test} = time() - $timings{$test};
351             status_completed(scalar @done);
352
353             $aggregator->add($test, $parser);
354             push @done, [ $test, $output ];
355
356             undef $w;
357             take_job($worker, $cv, $tests);
358         }
359     );
360 }
361
362 sub cleanup {
363     my $exitcode = $?;
364     $_->() for our @CLEANUP;
365     exit $exitcode;
366 }
367
368 # must be in a begin block because we C<exit 0> above
369 BEGIN {
370     $SIG{$_} = sub {
371         require Carp; Carp::cluck("Caught SIG$_[0]\n");
372         cleanup();
373     } for qw(INT TERM QUIT KILL PIPE)
374 }
375
376 __END__
377
378 =head1 NAME
379
380 complete-run.pl - Run the i3 testsuite
381
382 =head1 SYNOPSIS
383
384 complete-run.pl [files...]
385
386 =head1 EXAMPLE
387
388 To run the whole testsuite on a reasonable number of Xephyr instances (your
389 running X11 will not be touched), run:
390   ./complete-run.pl
391
392 To run only a specific test (useful when developing a new feature), run:
393   ./complete-run t/100-fullscreen.t
394
395 =head1 OPTIONS
396
397 =over 8
398
399 =item B<--display>
400
401 Specifies which X11 display should be used. Can be specified multiple times and
402 will parallelize the tests:
403
404   # Run tests on the second X server
405   ./complete-run.pl -d :1
406
407   # Run four tests in parallel on some Xephyr servers
408   ./complete-run.pl -d :1,:2,:3,:4
409
410 Note that it is not necessary to specify this anymore. If omitted,
411 complete-run.pl will start (num_cores * 2) Xephyr instances.
412
413 =item B<--valgrind>
414
415 Runs i3 under valgrind to find memory problems. The output will be available in
416 C<latest/valgrind-for-$test.log>.
417
418 =item B<--strace>
419
420 Runs i3 under strace to trace system calls. The output will be available in
421 C<latest/strace-for-$test.log>.
422
423 =item B<--xtrace>
424
425 Runs i3 under xtrace to trace X11 requests/replies. The output will be
426 available in C<latest/xtrace-for-$test.log>.
427
428 =item B<--coverage-testing>
429
430 Generates a test coverage report at C<latest/i3-coverage>. Exits i3 cleanly
431 during tests (instead of kill -9) to make coverage testing work properly.
432
433 =item B<--parallel>
434
435 Number of Xephyr instances to start (if you don't want to start num_cores * 2
436 instances for some reason).
437
438   # Run all tests on a single Xephyr instance
439   ./complete-run.pl -p 1
440
441 =back