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