2 # vim:ts=4:sw=4:expandtab
3 # © 2010 Michael Stapelberg and contributors
9 # the following are modules which ship with Perl (>= 5.10):
11 use File::Temp qw(tempfile tempdir);
16 use TAP::Parser::Aggregator;
17 use Time::HiRes qw(time);
20 # these are shipped with the testsuite
21 use lib qw(@abs_top_builddir@/testcases/lib @abs_top_srcdir@/testcases/lib @abs_top_srcdir@/AnyEvent-I3/blib/lib);
22 use i3test::Util qw(slurp);
26 # the following modules are not shipped with Perl
30 use AnyEvent::I3 qw(:all);
31 use X11::XCB::Connection;
32 use JSON::XS; # AnyEvent::I3 depends on it, too.
34 binmode STDOUT, ':utf8';
35 binmode STDERR, ':utf8';
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);
41 # convenience wrapper to write to the log file
43 sub Log { say $log "@_" }
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
59 my $keep_xserver_output = 0;
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,
72 pod2usage(-verbose => 2, -exitcode => 0) if $help;
74 # Check for missing executables
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
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;
90 my @test_binaries = qw(
91 @abs_top_builddir@/test.commands_parser
92 @abs_top_builddir@/test.config_parser
93 @abs_top_builddir@/test.inject_randr15
96 foreach my $binary (@test_binaries) {
97 die "$binary executable not found, did you run “make check”?" unless -e $binary;
98 die "$binary is not an executable" unless -x $binary;
101 $ENV{PATH} = join(':',
102 '@abs_top_builddir@/i3-nagbar',
103 '@abs_top_builddir@/i3-msg',
104 '@abs_top_builddir@/i3-input',
105 '@abs_top_builddir@/i3-dump-log',
106 '@abs_top_builddir@/i3-config-wizard',
107 '@abs_top_builddir@/i3bar',
108 '@abs_top_builddir@',
112 qx(Xephyr -help 2>&1);
113 die "Xephyr was not found in your path. Please install Xephyr (xserver-xephyr on Debian)." if $?;
115 @displays = split(/,/, join(',', @displays));
116 @displays = map { s/ //g; $_ } @displays;
118 # 2: get a list of all testcases
119 my @testfiles = @ARGV;
121 # if no files were passed on command line, run all tests from t/
122 if (scalar @testfiles == 0) {
123 @testfiles = <@abs_top_srcdir@/testcases/t/*.t> if @testfiles == 0;
126 # Fully qualify each specified file if necessary
128 $_ = "@abs_top_srcdir@/testcases/$_";
134 my $numtests = scalar @testfiles;
136 # No displays specified, let’s start some Xephyr instances.
137 if (@displays == 0) {
138 @displays = start_xserver($parallel, $numtests, $keep_xserver_output);
141 # 1: create an output directory for this test-run
142 my $outdir = "testsuite-";
143 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
144 $outdir .= `git describe --tags`;
146 mkdir($outdir) or die "Could not create $outdir";
147 unlink("latest") if -l "latest";
148 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
151 # connect to all displays for two reasons:
152 # 1: check if the display actually works
153 # 2: keep the connection open so that i3 is not the only client. this prevents
154 # the X server from exiting
156 for my $display (@displays) {
158 my $x = X11::XCB::Connection->new(display => $display);
160 die "Could not connect to display $display\n";
162 # start a TestWorker for each display
163 push @single_worker, worker($display, $x, $outdir, \%options);
167 # Read previous timing information, if available. We will be able to roughly
168 # predict the test duration and schedule a good order for the tests.
169 my $timingsjson = slurp('.last_run_timings.json') if -e '.last_run_timings.json';
170 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
172 # Re-order the files so that those which took the longest time in the previous
173 # run will be started at the beginning to not delay the whole run longer than
175 @testfiles = map { $_->[0] }
176 sort { $b->[1] <=> $a->[1] }
177 map { [$_, $timings{$_} // 999] } @testfiles;
179 # Run 000-load-deps.t first to bail out early when dependencies are missing.
180 my ($loadtest) = grep { $_ =~ m,t/000-load-deps.t$, } @testfiles;
181 if (defined($loadtest)) {
182 @testfiles = ($loadtest, grep { $_ ne $loadtest } @testfiles);
185 # Run 533-randr15.t last because it destructively modifies the RandR
186 # configuration of the X session, interfering with any test started afterwards.
187 my ($randrtest) = grep { $_ =~ m,t/533-randr15.t$, } @testfiles;
188 if (defined($randrtest)) {
189 @testfiles = ((grep { $_ ne $randrtest } @testfiles), $randrtest);
192 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
193 if exists($timings{GLOBAL});
195 # Forget the old timings, we don’t necessarily run the same set of tests as
196 # before. Otherwise we would end up with left-overs.
197 %timings = (GLOBAL => time());
199 my $logfile = "$outdir/complete-run.log";
200 open $log, '>', $logfile or die "Could not create '$logfile': $!";
202 say "Writing logfile to '$logfile'...";
206 my $num = @testfiles;
207 my $harness = TAP::Harness->new({ });
209 my $aggregator = TAP::Parser::Aggregator->new();
210 $aggregator->start();
212 status_init(displays => \@displays, tests => $num);
214 my $single_cv = AE::cv;
216 # We start tests concurrently: For each display, one test gets started. Every
217 # test starts another test after completing.
218 for (@single_worker) {
220 take_job($_, $single_cv, \@testfiles);
227 # print empty lines to separate failed tests from statuslines
231 my ($test, $output) = @$_;
232 say "no output for $test" unless $output;
233 Log "output for $test:";
235 # print error messages of failed tests
236 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
240 $harness->summary($aggregator);
244 # 5: Save the timings for better scheduling/prediction next run.
245 $timings{GLOBAL} = time() - $timings{GLOBAL};
246 open(my $fh, '>', '.last_run_timings.json');
247 print $fh encode_json(\%timings);
250 # 6: Print the slowest test files.
251 my @slowest = map { $_->[0] }
252 sort { $b->[1] <=> $a->[1] }
253 map { [$_, $timings{$_}] }
254 grep { !/^GLOBAL$/ } keys %timings;
256 say 'The slowest tests are:';
257 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
258 for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
260 # When we are running precisely one test, print the output. Makes developing
261 # with a single testcase easier.
262 if ($numtests == 1) {
270 # Report logfiles that match “(Leak|Address)Sanitizer:”.
272 for my $log (<$outdir/i3-log-for-*>) {
273 if (slurp($log) =~ /(Leak|Address)Sanitizer:/) {
274 push @logs_with_leaks, $log;
277 if (scalar @logs_with_leaks > 0) {
278 say "\nThe following test logfiles contain AddressSanitizer or LeakSanitizer reports:";
279 for my $log (sort @logs_with_leaks) {
284 exit ($aggregator->failed > 0);
287 # Takes a test from the beginning of @testfiles and runs it.
289 # The TAP::Parser (which reads the test output) will get called as soon as
290 # there is some activity on the stdout file descriptor of the test process
291 # (using an AnyEvent->io watcher).
293 # When a test completes and @done contains $num entries, the $cv condvar gets
294 # triggered to finish testing.
297 my ($worker, $cv, $tests) = @_;
299 my $test = shift @$tests
302 my $display = $worker->{display};
304 Log status($display, "$test: starting");
305 $timings{$test} = time();
306 worker_next($worker, $test);
308 # create a TAP::Parser with an in-memory fh
310 my $parser = TAP::Parser->new({
311 source => do { open(my $fh, '<', \$output); $fh },
314 my $ipc = $worker->{ipc};
321 state $tests_completed = 0;
324 sysread($ipc, my $buf, 4096) or die "sysread: $!";
327 $buf = $partial . $buf;
331 # make sure we feed TAP::Parser complete lines so it doesn't blow up
332 if (substr($buf, -1, 1) ne "\n") {
333 my $nl = rindex($buf, "\n");
339 # strip partial from buffer
340 $partial = substr($buf, $nl + 1, '');
343 # count lines before stripping eof-marker otherwise we might
344 # end up with for (1 .. 0) { } which would effectivly skip the loop
345 my $lines = $buf =~ tr/\n//;
346 my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
351 my $result = $parser->next;
352 next unless defined($result);
353 if ($result->is_test) {
355 status($display, "$test: [$tests_completed/??] ");
356 } elsif ($result->is_bailout) {
357 Log status($display, "$test: BAILOUT");
358 status_completed(scalar @done);
360 say "test $test bailed out: " . $result->explanation;
365 return unless $t_eof;
367 Log status($display, "$test: finished");
368 $timings{$test} = time() - $timings{$test};
369 status_completed(scalar @done);
371 $aggregator->add($test, $parser);
372 push @done, [ $test, $output ];
375 take_job($worker, $cv, $tests);
382 $_->() for our @CLEANUP;
386 # must be in a begin block because we C<exit 0> above
389 require Carp; Carp::cluck("Caught SIG$_[0]\n");
391 } for qw(INT TERM QUIT KILL PIPE)
398 complete-run.pl - Run the i3 testsuite
402 complete-run.pl [files...]
406 To run the whole testsuite on a reasonable number of Xephyr instances (your
407 running X11 will not be touched), run:
410 To run only a specific test (useful when developing a new feature), run:
411 ./complete-run t/100-fullscreen.t
419 Specifies which X11 display should be used. Can be specified multiple times and
420 will parallelize the tests:
422 # Run tests on the second X server
423 ./complete-run.pl -d :1
425 # Run four tests in parallel on some Xephyr servers
426 ./complete-run.pl -d :1,:2,:3,:4
428 Note that it is not necessary to specify this anymore. If omitted,
429 complete-run.pl will start (num_cores * 2) Xephyr instances.
433 Runs i3 under valgrind to find memory problems. The output will be available in
434 C<latest/valgrind-for-$test.log>.
438 Runs i3 under strace to trace system calls. The output will be available in
439 C<latest/strace-for-$test.log>.
443 Runs i3 under xtrace to trace X11 requests/replies. The output will be
444 available in C<latest/xtrace-for-$test.log>.
446 =item B<--coverage-testing>
448 Generates a test coverage report at C<latest/i3-coverage>. Exits i3 cleanly
449 during tests (instead of kill -9) to make coverage testing work properly.
453 Number of Xephyr instances to start (if you don't want to start num_cores * 2
454 instances for some reason).
456 # Run all tests on a single Xephyr instance
457 ./complete-run.pl -p 1