]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
complete-run: Use Carp::Always to get nice stacktraces in case of errors
[i3/i3] / testcases / complete-run.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # © 2010-2011 Michael Stapelberg and contributors
5 #
6 # syntax: ./complete-run.pl --display :1 --display :2
7 # to run the test suite on the X11 displays :1 and :2
8 # use 'Xdummy :1' and 'Xdummy :2' before to start two
9 # headless X11 servers
10 #
11
12 use strict;
13 use warnings;
14 use v5.10;
15 # the following are modules which ship with Perl (>= 5.10):
16 use Carp::Always;
17 use Cwd qw(abs_path);
18 use File::Basename qw(basename);
19 use File::Temp qw(tempfile tempdir);
20 use Getopt::Long;
21 use IO::Socket::UNIX;
22 use POSIX;
23 use Time::HiRes qw(sleep gettimeofday tv_interval);
24 use TAP::Harness;
25 use TAP::Parser;
26 use TAP::Parser::Aggregator;
27 # these are shipped with the testsuite
28 use lib qw(lib);
29 use SocketActivation;
30 # the following modules are not shipped with Perl
31 use AnyEvent;
32 use AnyEvent::Handle;
33 use AnyEvent::I3 qw(:all);
34 use X11::XCB;
35
36 # We actually use AnyEvent to make sure it loads an event loop implementation.
37 # Afterwards, we overwrite SIGCHLD:
38 my $cv = AnyEvent->condvar;
39
40 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
41 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
42 # needs to get the exit status to determine if a test is successful).
43 $SIG{CHLD} = sub {
44 };
45
46 # reads in a whole file
47 sub slurp {
48     open my $fh, '<', shift;
49     local $/;
50     <$fh>;
51 }
52
53 my $coverage_testing = 0;
54 my $valgrind = 0;
55 my @displays = ();
56
57 my $result = GetOptions(
58     "coverage-testing" => \$coverage_testing,
59     "valgrind" => \$valgrind,
60     "display=s" => \@displays,
61 );
62
63 @displays = split(/,/, join(',', @displays));
64 @displays = map { s/ //g; $_ } @displays;
65
66 @displays = qw(:1) if @displays == 0;
67
68 # connect to all displays for two reasons:
69 # 1: check if the display actually works
70 # 2: keep the connection open so that i3 is not the only client. this prevents
71 #    the X server from exiting (Xdummy will restart it, but not quick enough
72 #    sometimes)
73 my @conns;
74 my @wdisplays;
75 for my $display (@displays) {
76     my $screen;
77     my $x = X11::XCB->new($display, $screen);
78     if ($x->has_error) {
79         say STDERR "WARNING: Not using X11 display $display, could not connect";
80     } else {
81         push @conns, $x;
82         push @wdisplays, $display;
83     }
84 }
85
86 my $config = slurp('i3-test.config');
87
88 # 1: get a list of all testcases
89 my @testfiles = @ARGV;
90
91 # if no files were passed on command line, run all tests from t/
92 @testfiles = <t/*.t> if @testfiles == 0;
93
94 # 2: create an output directory for this test-run
95 my $outdir = "testsuite-";
96 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
97 $outdir .= `git describe --tags`;
98 chomp($outdir);
99 mkdir($outdir) or die "Could not create $outdir";
100 unlink("latest") if -e "latest";
101 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
102
103 # 3: run all tests
104 my @done;
105 my $num = @testfiles;
106 my $harness = TAP::Harness->new({ });
107
108 my $aggregator = TAP::Parser::Aggregator->new();
109 $aggregator->start();
110
111 # We start tests concurrently: For each display, one test gets started. Every
112 # test starts another test after completing.
113 take_job($_) for @wdisplays;
114
115 #
116 # Takes a test from the beginning of @testfiles and runs it.
117 #
118 # The TAP::Parser (which reads the test output) will get called as soon as
119 # there is some activity on the stdout file descriptor of the test process
120 # (using an AnyEvent->io watcher).
121 #
122 # When a test completes and @done contains $num entries, the $cv condvar gets
123 # triggered to finish testing.
124 #
125 sub take_job {
126     my ($display) = @_;
127
128     my $test = shift @testfiles;
129     return unless $test;
130     my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
131     my $logpath = "$outdir/i3-log-for-" . basename($test);
132
133     my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
134     say $fh $config;
135     say $fh "ipc-socket /tmp/nested-$display";
136     close($fh);
137
138     my $activate_cv = AnyEvent->condvar;
139     my $time_before_start = [gettimeofday];
140
141     my $pid;
142     if (!$dont_start) {
143         $pid = activate_i3(
144             unix_socket_path => "/tmp/nested-$display-activation",
145             display => $display,
146             configfile => $tmpfile,
147             outdir => $outdir,
148             logpath => $logpath,
149             valgrind => $valgrind,
150             cv => $activate_cv
151         );
152
153         my $child_watcher;
154         $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
155             say "child died. pid = $pid";
156             undef $child_watcher;
157         });
158     }
159
160     my $kill_i3 = sub {
161         # Don’t bother killing i3 when we haven’t started it
162         return if $dont_start;
163
164         # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
165         # files are not written) and fallback to killing it
166         if ($coverage_testing) {
167             my $exited = 0;
168             eval {
169                 say "Exiting i3 cleanly...";
170                 i3("/tmp/nested-$display")->command('exit')->recv;
171                 $exited = 1;
172             };
173             return if $exited;
174         }
175
176         say "[$display] killing i3";
177         kill(9, $pid) or die "could not kill i3";
178     };
179
180     # This will be called as soon as i3 is running and answered to our
181     # IPC request
182     $activate_cv->cb(sub {
183         my $time_activating = [gettimeofday];
184         my $start_duration = tv_interval($time_before_start, $time_activating);
185         my ($status) = $activate_cv->recv;
186         if ($dont_start) {
187             say "[$display] Not starting i3, testcase does that";
188         } else {
189             say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
190         }
191
192         say "[$display] Running $test with logfile $logpath";
193
194         my $output;
195         open(my $spool, '>', \$output);
196         my $parser = TAP::Parser->new({
197             exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" OUTDIR="$outdir" VALGRIND=$valgrind /usr/bin/perl -Ilib $test| ],
198             spool => $spool,
199             merge => 1,
200         });
201
202         my @watchers;
203         my ($stdout, $stderr) = $parser->get_select_handles;
204         for my $handle ($parser->get_select_handles) {
205             my $w;
206             $w = AnyEvent->io(
207                 fh => $handle,
208                 poll => 'r',
209                 cb => sub {
210                     # Ignore activity on stderr (unnecessary with merge => 1,
211                     # but let’s keep it in here if we want to use merge => 0
212                     # for some reason in the future).
213                     return if defined($stderr) and $handle == $stderr;
214
215                     my $result = $parser->next;
216                     if (defined($result)) {
217                         # TODO: check if we should bail out
218                         return;
219                     }
220
221                     # $result is not defined, we are done parsing
222                     say "[$display] $test finished";
223                     close($parser->delete_spool);
224                     $aggregator->add($test, $parser);
225                     push @done, [ $test, $output ];
226
227                     $kill_i3->();
228
229                     undef $_ for @watchers;
230                     if (@done == $num) {
231                         $cv->send;
232                     } else {
233                         take_job($display);
234                     }
235                 }
236             );
237             push @watchers, $w;
238         }
239     });
240
241     $activate_cv->send(1) if $dont_start;
242 }
243
244 $cv->recv;
245
246 $aggregator->stop();
247
248 for (@done) {
249     my ($test, $output) = @$_;
250     say "output for $test:";
251     say $output;
252 }
253
254 # 4: print summary
255 $harness->summary($aggregator);