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