]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
testcases: always start i3 through i3test::launch_with_config
[i3/i3] / testcases / complete-run.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 # © 2010-2011 Michael Stapelberg and contributors
4
5 use strict;
6 use warnings;
7 use v5.10;
8 # the following are modules which ship with Perl (>= 5.10):
9 use Pod::Usage;
10 use Cwd qw(abs_path);
11 use File::Basename qw(basename);
12 use File::Temp qw(tempfile tempdir);
13 use Getopt::Long;
14 use IO::Socket::UNIX;
15 use POSIX ();
16 use Time::HiRes qw(sleep gettimeofday tv_interval);
17 use TAP::Harness;
18 use TAP::Parser;
19 use TAP::Parser::Aggregator;
20 # these are shipped with the testsuite
21 use lib qw(lib);
22 use StartXDummy;
23 use StatusLine;
24 # the following modules are not shipped with Perl
25 use AnyEvent;
26 use AnyEvent::Util;
27 use AnyEvent::Handle;
28 use AnyEvent::I3 qw(:all);
29 use X11::XCB;
30
31 # Close superfluous file descriptors which were passed by running in a VIM
32 # subshell or situations like that.
33 AnyEvent::Util::close_all_fds_except(0, 1, 2);
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 # convinience wrapper to write to the log file
46 my $log;
47 sub Log { say $log "@_" }
48
49 my $coverage_testing = 0;
50 my $valgrind = 0;
51 my $strace = 0;
52 my $help = 0;
53 # Number of tests to run in parallel. Important to know how many Xdummy
54 # instances we need to start (unless @displays are given). Defaults to
55 # num_cores * 2.
56 my $parallel = undef;
57 my @displays = ();
58 my @childpids = ();
59
60 my $result = GetOptions(
61     "coverage-testing" => \$coverage_testing,
62     "valgrind" => \$valgrind,
63     "strace" => \$strace,
64     "display=s" => \@displays,
65     "parallel=i" => \$parallel,
66     "help|?" => \$help,
67 );
68
69 pod2usage(-verbose => 2, -exitcode => 0) if $help;
70
71 @displays = split(/,/, join(',', @displays));
72 @displays = map { s/ //g; $_ } @displays;
73
74 # No displays specified, let’s start some Xdummy instances.
75 if (@displays == 0) {
76     my ($displays, $pids) = start_xdummy($parallel);
77     @displays = @$displays;
78     @childpids = @$pids;
79 }
80
81 # connect to all displays for two reasons:
82 # 1: check if the display actually works
83 # 2: keep the connection open so that i3 is not the only client. this prevents
84 #    the X server from exiting (Xdummy will restart it, but not quick enough
85 #    sometimes)
86 my @conns;
87 my @wdisplays;
88 for my $display (@displays) {
89     my $screen;
90     my $x = X11::XCB->new($display, $screen);
91     if ($x->has_error) {
92         Log "WARNING: Not using X11 display $display, could not connect";
93     } else {
94         push @conns, $x;
95         push @wdisplays, $display;
96     }
97 }
98
99 die "No usable displays found" if @wdisplays == 0;
100
101 # 1: get a list of all testcases
102 my @testfiles = @ARGV;
103
104 # if no files were passed on command line, run all tests from t/
105 @testfiles = <t/*.t> if @testfiles == 0;
106
107 # 2: create an output directory for this test-run
108 my $outdir = "testsuite-";
109 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
110 $outdir .= `git describe --tags`;
111 chomp($outdir);
112 mkdir($outdir) or die "Could not create $outdir";
113 unlink("latest") if -e "latest";
114 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
115
116 my $logfile = "$outdir/complete-run.log";
117 open $log, '>', $logfile or die "Could not create '$logfile': $!";
118 say "Writing logfile to '$logfile'...";
119
120 # 3: run all tests
121 my @done;
122 my $num = @testfiles;
123 my $harness = TAP::Harness->new({ });
124
125 my $aggregator = TAP::Parser::Aggregator->new();
126 $aggregator->start();
127
128 status_init(displays => \@wdisplays, tests => $num);
129
130 # We start tests concurrently: For each display, one test gets started. Every
131 # test starts another test after completing.
132 for (@wdisplays) { $cv->begin; take_job($_) }
133
134 #
135 # Takes a test from the beginning of @testfiles and runs it.
136 #
137 # The TAP::Parser (which reads the test output) will get called as soon as
138 # there is some activity on the stdout file descriptor of the test process
139 # (using an AnyEvent->io watcher).
140 #
141 # When a test completes and @done contains $num entries, the $cv condvar gets
142 # triggered to finish testing.
143 #
144 sub take_job {
145     my ($display) = @_;
146
147     my $test = shift @testfiles
148         or return $cv->end;
149
150     my $basename = basename($test);
151
152     Log status($display, "Starting $test");
153
154     my $output;
155     open(my $spool, '>', \$output);
156     my $parser = TAP::Parser->new({
157         exec => [ 'sh', '-c', qq|DISPLAY=$display TESTNAME="$basename" OUTDIR="$outdir" VALGRIND=$valgrind STRACE=$strace COVERAGE=$coverage_testing /usr/bin/perl -Ilib $test| ],
158         spool => $spool,
159         merge => 1,
160     });
161
162     my $tests_completed;
163
164     my @watchers;
165     my ($stdout, $stderr) = $parser->get_select_handles;
166     for my $handle ($parser->get_select_handles) {
167         my $w;
168         $w = AnyEvent->io(
169             fh => $handle,
170             poll => 'r',
171             cb => sub {
172                 # Ignore activity on stderr (unnecessary with merge => 1,
173                 # but let’s keep it in here if we want to use merge => 0
174                 # for some reason in the future).
175                 return if defined($stderr) and $handle == $stderr;
176
177                 my $result = $parser->next;
178                 if (defined($result)) {
179                     $tests_completed++;
180                     status($display, "Running $test: [$tests_completed/??]");
181                     # TODO: check if we should bail out
182                     return;
183                 }
184
185                 # $result is not defined, we are done parsing
186                 Log status($display, "$test finished");
187                 close($parser->delete_spool);
188                 $aggregator->add($test, $parser);
189                 push @done, [ $test, $output ];
190
191                 status_completed(scalar @done);
192
193                 undef $_ for @watchers;
194                 if (@done == $num) {
195                     $cv->end;
196                 } else {
197                     take_job($display);
198                 }
199             }
200         );
201         push @watchers, $w;
202     }
203 }
204
205 $cv->recv;
206
207 $aggregator->stop();
208
209 # print empty lines to seperate failed tests from statuslines
210 print "\n\n";
211
212 for (@done) {
213     my ($test, $output) = @$_;
214     Log "output for $test:";
215     Log $output;
216     # print error messages of failed tests
217     say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
218 }
219
220 # 4: print summary
221 $harness->summary($aggregator);
222
223 close $log;
224
225 kill(15, $_) for @childpids;
226
227 __END__
228
229 =head1 NAME
230
231 complete-run.pl - Run the i3 testsuite
232
233 =head1 SYNOPSIS
234
235 complete-run.pl [files...]
236
237 =head1 EXAMPLE
238
239 To run the whole testsuite on a reasonable number of Xdummy instances (your
240 running X11 will not be touched), run:
241   ./complete-run.pl
242
243 To run only a specific test (useful when developing a new feature), run:
244   ./complete-run t/100-fullscreen.t
245
246 =head1 OPTIONS
247
248 =over 8
249
250 =item B<--display>
251
252 Specifies which X11 display should be used. Can be specified multiple times and
253 will parallelize the tests:
254
255   # Run tests on the second X server
256   ./complete-run.pl -d :1
257
258   # Run four tests in parallel on some Xdummy servers
259   ./complete-run.pl -d :1,:2,:3,:4
260
261 Note that it is not necessary to specify this anymore. If omitted,
262 complete-run.pl will start (num_cores * 2) Xdummy instances.
263
264 =item B<--valgrind>
265
266 Runs i3 under valgrind to find memory problems. The output will be available in
267 C<latest/valgrind-for-$test.log>.
268
269 =item B<--strace>
270
271 Runs i3 under strace to trace system calls. The output will be available in
272 C<latest/strace-for-$test.log>.
273
274 =item B<--coverage-testing>
275
276 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
277
278 =item B<--parallel>
279
280 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
281 instances for some reason).
282
283   # Run all tests on a single Xdummy instance
284   ./complete-run.pl -p 1