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