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