]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
Merge branch 'master' into next
[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 gettimeofday tv_interval);
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('i3-run-cfg.XXXXXX', UNLINK => 1);
138     say $fh $config;
139     say $fh "ipc-socket /tmp/nested-$display";
140     close($fh);
141
142     my $activate_cv = AnyEvent->condvar;
143     my $time_before_start = [gettimeofday];
144     my $start_i3 = sub {
145         # remove the old unix socket
146         unlink("/tmp/nested-$display-activation");
147
148         # pass all file descriptors up to three to the children.
149         # we need to set this flag before opening the socket.
150         open(my $fdtest, '<', '/dev/null');
151         $^F = fileno($fdtest);
152         close($fdtest);
153         my $socket = IO::Socket::UNIX->new(
154             Listen => 1,
155             Local => "/tmp/nested-$display-activation",
156         );
157
158         my $pid = fork;
159         if (!defined($pid)) {
160             die "could not fork()";
161         }
162         if ($pid == 0) {
163             $ENV{LISTEN_PID} = $$;
164             $ENV{LISTEN_FDS} = 1;
165             $ENV{DISPLAY} = $display;
166             $^F = 3;
167
168             close($reserved);
169             POSIX::dup2(fileno($socket), 3);
170
171             # now execute i3
172             my $i3cmd = abs_path("../i3") . " -V -d all --disable-signalhandler";
173             my $cmd = "exec $i3cmd -c $tmpfile >$logpath 2>&1";
174             exec "/bin/sh", '-c', $cmd;
175
176             # if we are still here, i3 could not be found or exec failed. bail out.
177             exit 1;
178         }
179
180         my $child_watcher;
181         $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
182             say "child died. pid = $pid";
183             undef $child_watcher;
184         });
185
186         # close the socket, the child process should be the only one which keeps a file
187         # descriptor on the listening socket.
188         $socket->close;
189
190         # We now connect (will succeed immediately) and send a request afterwards.
191         # As soon as the reply is there, i3 is considered ready.
192         my $cl = IO::Socket::UNIX->new(Peer => "/tmp/nested-$display-activation");
193         my $hdl;
194         $hdl = AnyEvent::Handle->new(fh => $cl, on_error => sub { $activate_cv->send(0) });
195
196         # send a get_tree message without payload
197         $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
198
199         # wait for the reply
200         $hdl->push_read(chunk => 1, => sub {
201             my ($h, $line) = @_;
202             $activate_cv->send(1);
203             undef $hdl;
204         });
205
206         return $pid;
207     };
208
209     my $pid;
210     $pid = $start_i3->() unless $dont_start;
211
212     my $kill_i3 = sub {
213         # Don’t bother killing i3 when we haven’t started it
214         return if $dont_start;
215
216         # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
217         # files are not written) and fallback to killing it
218         if ($coverage_testing) {
219             my $exited = 0;
220             try {
221                 say "Exiting i3 cleanly...";
222                 i3("/tmp/nested-$display")->command('exit')->recv;
223                 $exited = 1;
224             };
225             return if $exited;
226         }
227
228         say "[$display] killing i3";
229         kill(9, $pid) or die "could not kill i3";
230     };
231
232     # This will be called as soon as i3 is running and answered to our
233     # IPC request
234     $activate_cv->cb(sub {
235         my $time_activating = [gettimeofday];
236         my $start_duration = tv_interval($time_before_start, $time_activating);
237         my ($status) = $activate_cv->recv;
238         if ($dont_start) {
239             say "[$display] Not starting i3, testcase does that";
240         } else {
241             say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
242         }
243
244         say "[$display] Running $test with logfile $logpath";
245
246         my $output;
247         my $parser = TAP::Parser->new({
248             exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -It/lib $test| ],
249             spool => IO::Scalar->new(\$output),
250             merge => 1,
251         });
252
253         my @watchers;
254         my ($stdout, $stderr) = $parser->get_select_handles;
255         for my $handle ($parser->get_select_handles) {
256             my $w;
257             $w = AnyEvent->io(
258                 fh => $handle,
259                 poll => 'r',
260                 cb => sub {
261                     # Ignore activity on stderr (unnecessary with merge => 1,
262                     # but let’s keep it in here if we want to use merge => 0
263                     # for some reason in the future).
264                     return if defined($stderr) and $handle == $stderr;
265
266                     my $result = $parser->next;
267                     if (defined($result)) {
268                         # TODO: check if we should bail out
269                         return;
270                     }
271
272                     # $result is not defined, we are done parsing
273                     say "[$display] $test finished";
274                     close($parser->delete_spool);
275                     $aggregator->add($test, $parser);
276                     push @done, [ $test, $output ];
277
278                     $kill_i3->();
279
280                     undef $_ for @watchers;
281                     if (@done == $num) {
282                         $cv->send;
283                     } else {
284                         take_job($display);
285                     }
286                 }
287             );
288             push @watchers, $w;
289         }
290     });
291
292     $activate_cv->send(1) if $dont_start;
293 }
294
295 $cv->recv;
296
297 $aggregator->stop();
298
299 for (@done) {
300     my ($test, $output) = @$_;
301     say "output for $test:";
302     say $output;
303 }
304
305 # 4: print summary
306 $harness->summary($aggregator);