]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
04130ec8c489e33b9e15f0685938451a61e710f5
[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 EV;
15 use AnyEvent;
16 use IO::Scalar; # not in core :\
17 use File::Temp qw(tempfile tempdir);
18 use v5.10;
19 use Data::Dumper;
20 use Cwd qw(abs_path);
21 use Proc::Background;
22 use TAP::Harness;
23 use TAP::Parser;
24 use TAP::Parser::Aggregator;
25 use File::Basename qw(basename);
26 use AnyEvent::I3 qw(:all);
27 use Try::Tiny;
28 use Getopt::Long;
29 use Time::HiRes qw(sleep gettimeofday tv_interval);
30 use X11::XCB;
31 use IO::Socket::UNIX; # core
32 use POSIX; # core
33 use AnyEvent::Handle;
34
35 # install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent / EV
36 # XXX: we could maybe also use a different loop than the default loop in EV?
37 $SIG{CHLD} = sub {
38 };
39
40 # reads in a whole file
41 sub slurp {
42     open my $fh, '<', shift;
43     local $/;
44     <$fh>;
45 }
46
47 my $coverage_testing = 0;
48 my @displays = ();
49
50 my $result = GetOptions(
51     "coverage-testing" => \$coverage_testing,
52     "display=s" => \@displays,
53 );
54
55 @displays = split(/,/, join(',', @displays));
56 @displays = map { s/ //g; $_ } @displays;
57
58 @displays = qw(:1) if @displays == 0;
59
60 # connect to all displays for two reasons:
61 # 1: check if the display actually works
62 # 2: keep the connection open so that i3 is not the only client. this prevents
63 #    the X server from exiting (Xdummy will restart it, but not quick enough
64 #    sometimes)
65 my @conns;
66 my @wdisplays;
67 for my $display (@displays) {
68     my $screen;
69     my $x = X11::XCB->new($display, $screen);
70     if ($x->has_error) {
71         say STDERR "WARNING: Not using X11 display $display, could not connect";
72     } else {
73         push @conns, $x;
74         push @wdisplays, $display;
75     }
76 }
77
78 my $config = slurp('i3-test.config');
79
80 # 1: get a list of all testcases
81 my @testfiles = @ARGV;
82
83 # if no files were passed on command line, run all tests from t/
84 @testfiles = <t/*.t> if @testfiles == 0;
85
86 # 2: create an output directory for this test-run
87 my $outdir = "testsuite-";
88 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
89 $outdir .= `git describe --tags`;
90 chomp($outdir);
91 mkdir($outdir) or die "Could not create $outdir";
92 unlink("latest") if -e "latest";
93 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
94
95 # 3: run all tests
96 my @done;
97 my $num = @testfiles;
98 my $harness = TAP::Harness->new({ });
99
100 my $aggregator = TAP::Parser::Aggregator->new();
101 $aggregator->start();
102
103 my $cv = AnyEvent->condvar;
104
105 # We start tests concurrently: For each display, one test gets started. Every
106 # test starts another test after completing.
107 take_job($_) for @wdisplays;
108
109 #
110 # Takes a test from the beginning of @testfiles and runs it.
111 #
112 # The TAP::Parser (which reads the test output) will get called as soon as
113 # there is some activity on the stdout file descriptor of the test process
114 # (using an AnyEvent->io watcher).
115 #
116 # When a test completes and @done contains $num entries, the $cv condvar gets
117 # triggered to finish testing.
118 #
119 sub take_job {
120     my ($display) = @_;
121
122     my $test = shift @testfiles;
123     return unless $test;
124     my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
125     my $logpath = "$outdir/i3-log-for-" . basename($test);
126
127     my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
128     say $fh $config;
129     say $fh "ipc-socket /tmp/nested-$display";
130     close($fh);
131
132     my $activate_cv = AnyEvent->condvar;
133     my $time_before_start = [gettimeofday];
134     my $start_i3 = sub {
135         # remove the old unix socket
136         unlink("/tmp/nested-$display-activation");
137
138         # pass all file descriptors up to three to the children.
139         # we need to set this flag before opening the socket.
140         open(my $fdtest, '<', '/dev/null');
141         $^F = fileno($fdtest);
142         close($fdtest);
143         my $socket = IO::Socket::UNIX->new(
144             Listen => 1,
145             Local => "/tmp/nested-$display-activation",
146         );
147
148         my $pid = fork;
149         if (!defined($pid)) {
150             die "could not fork()";
151         }
152         if ($pid == 0) {
153             $ENV{LISTEN_PID} = $$;
154             $ENV{LISTEN_FDS} = 1;
155             $ENV{DISPLAY} = $display;
156             $^F = 3;
157
158             POSIX::close(3);
159             POSIX::dup2(fileno($socket), 3);
160
161             # now execute i3
162             my $i3cmd = abs_path("../i3") . " -V -d all --disable-signalhandler";
163             my $cmd = "exec $i3cmd -c $tmpfile >$logpath 2>&1";
164             exec "/bin/sh", '-c', $cmd;
165
166             # if we are still here, i3 could not be found or exec failed. bail out.
167             exit 1;
168         }
169
170         my $child_watcher;
171         $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
172             say "child died. pid = $pid";
173             undef $child_watcher;
174         });
175
176         # close the socket, the child process should be the only one which keeps a file
177         # descriptor on the listening socket.
178         $socket->close;
179
180         # We now connect (will succeed immediately) and send a request afterwards.
181         # As soon as the reply is there, i3 is considered ready.
182         my $cl = IO::Socket::UNIX->new(Peer => "/tmp/nested-$display-activation");
183         my $hdl;
184         $hdl = AnyEvent::Handle->new(fh => $cl, on_error => sub { $activate_cv->send(0) });
185
186         # send a get_tree message without payload
187         $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
188
189         # wait for the reply
190         $hdl->push_read(chunk => 1, => sub {
191             my ($h, $line) = @_;
192             $activate_cv->send(1);
193             undef $hdl;
194         });
195
196         return $pid;
197     };
198
199     my $pid;
200     $pid = $start_i3->() unless $dont_start;
201
202     my $kill_i3 = sub {
203         # Don’t bother killing i3 when we haven’t started it
204         return if $dont_start;
205
206         # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
207         # files are not written) and fallback to killing it
208         if ($coverage_testing) {
209             my $exited = 0;
210             try {
211                 say "Exiting i3 cleanly...";
212                 i3("/tmp/nested-$display")->command('exit')->recv;
213                 $exited = 1;
214             };
215             return if $exited;
216         }
217
218         say "[$display] killing i3";
219         kill(9, $pid) or die "could not kill i3";
220     };
221
222     # This will be called as soon as i3 is running and answered to our
223     # IPC request
224     $activate_cv->cb(sub {
225         my $time_activating = [gettimeofday];
226         my $start_duration = tv_interval($time_before_start, $time_activating);
227         my ($status) = $activate_cv->recv;
228         if ($dont_start) {
229             say "[$display] Not starting i3, testcase does that";
230         } else {
231             say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
232         }
233
234         say "[$display] Running $test with logfile $logpath";
235
236         my $output;
237         my $parser = TAP::Parser->new({
238             exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -It/lib $test| ],
239             spool => IO::Scalar->new(\$output),
240             merge => 1,
241         });
242
243         my @watchers;
244         my ($stdout, $stderr) = $parser->get_select_handles;
245         for my $handle ($parser->get_select_handles) {
246             my $w;
247             $w = AnyEvent->io(
248                 fh => $handle,
249                 poll => 'r',
250                 cb => sub {
251                     # Ignore activity on stderr (unnecessary with merge => 1,
252                     # but let’s keep it in here if we want to use merge => 0
253                     # for some reason in the future).
254                     return if defined($stderr) and $handle == $stderr;
255
256                     my $result = $parser->next;
257                     if (defined($result)) {
258                         # TODO: check if we should bail out
259                         return;
260                     }
261
262                     # $result is not defined, we are done parsing
263                     say "[$display] $test finished";
264                     close($parser->delete_spool);
265                     $aggregator->add($test, $parser);
266                     push @done, [ $test, $output ];
267
268                     $kill_i3->();
269
270                     undef $_ for @watchers;
271                     if (@done == $num) {
272                         $cv->send;
273                     } else {
274                         take_job($display);
275                     }
276                 }
277             );
278             push @watchers, $w;
279         }
280     });
281
282     $activate_cv->send(1) if $dont_start;
283 }
284
285 $cv->recv;
286
287 $aggregator->stop();
288
289 for (@done) {
290     my ($test, $output) = @$_;
291     say "output for $test:";
292     say $output;
293 }
294
295 # 4: print summary
296 $harness->summary($aggregator);