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