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