2 # vim:ts=4:sw=4:expandtab
3 # © 2010-2011 Michael Stapelberg and contributors
8 # the following are modules which ship with Perl (>= 5.10):
11 use File::Basename qw(basename);
12 use File::Temp qw(tempfile tempdir);
16 use Time::HiRes qw(sleep gettimeofday tv_interval);
19 use TAP::Parser::Aggregator;
20 # these are shipped with the testsuite
25 # the following modules are not shipped with Perl
29 use AnyEvent::I3 qw(:all);
32 # Close superfluous file descriptors which were passed by running in a VIM
33 # subshell or situations like that.
34 AnyEvent::Util::close_all_fds_except(0, 1, 2);
36 # We actually use AnyEvent to make sure it loads an event loop implementation.
37 # Afterwards, we overwrite SIGCHLD:
38 my $cv = AnyEvent->condvar;
40 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
41 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
42 # needs to get the exit status to determine if a test is successful).
46 # reads in a whole file
48 open(my $fh, '<', shift);
53 # convinience wrapper to write to the log file
55 sub Log { say $log "@_" }
57 my $coverage_testing = 0;
61 # Number of tests to run in parallel. Important to know how many Xdummy
62 # instances we need to start (unless @displays are given). Defaults to
68 my $result = GetOptions(
69 "coverage-testing" => \$coverage_testing,
70 "valgrind" => \$valgrind,
72 "display=s" => \@displays,
73 "parallel=i" => \$parallel,
77 pod2usage(-verbose => 2, -exitcode => 0) if $help;
79 @displays = split(/,/, join(',', @displays));
80 @displays = map { s/ //g; $_ } @displays;
82 # No displays specified, let’s start some Xdummy instances.
84 my ($displays, $pids) = start_xdummy($parallel);
85 @displays = @$displays;
89 # connect to all displays for two reasons:
90 # 1: check if the display actually works
91 # 2: keep the connection open so that i3 is not the only client. this prevents
92 # the X server from exiting (Xdummy will restart it, but not quick enough
96 for my $display (@displays) {
98 my $x = X11::XCB->new($display, $screen);
100 Log "WARNING: Not using X11 display $display, could not connect";
103 push @wdisplays, $display;
107 die "No usable displays found" if @wdisplays == 0;
109 my $config = slurp('i3-test.config');
111 # 1: get a list of all testcases
112 my @testfiles = @ARGV;
114 # if no files were passed on command line, run all tests from t/
115 @testfiles = <t/*.t> if @testfiles == 0;
117 # 2: create an output directory for this test-run
118 my $outdir = "testsuite-";
119 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
120 $outdir .= `git describe --tags`;
122 mkdir($outdir) or die "Could not create $outdir";
123 unlink("latest") if -e "latest";
124 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
126 my $logfile = "$outdir/complete-run.log";
127 open $log, '>', $logfile or die "Could not create '$logfile': $!";
128 say "Writing logfile to '$logfile'...";
132 my $num = @testfiles;
133 my $harness = TAP::Harness->new({ });
135 my $aggregator = TAP::Parser::Aggregator->new();
136 $aggregator->start();
138 status_init(displays => \@wdisplays, tests => $num);
140 # We start tests concurrently: For each display, one test gets started. Every
141 # test starts another test after completing.
142 for (@wdisplays) { $cv->begin; take_job($_) }
145 # Takes a test from the beginning of @testfiles and runs it.
147 # The TAP::Parser (which reads the test output) will get called as soon as
148 # there is some activity on the stdout file descriptor of the test process
149 # (using an AnyEvent->io watcher).
151 # When a test completes and @done contains $num entries, the $cv condvar gets
152 # triggered to finish testing.
157 my $test = shift @testfiles
160 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
161 my $basename = basename($test);
162 my $logpath = "$outdir/i3-log-for-$basename";
164 my ($fh, $tmpfile) = tempfile("i3-cfg-for-$basename.XXXXXX", UNLINK => 1);
166 say $fh "ipc-socket /tmp/nested-$display";
169 my $activate_cv = AnyEvent->condvar;
170 my $time_before_start = [gettimeofday];
174 $activate_cv->send(1);
177 unix_socket_path => "/tmp/nested-$display-activation",
179 configfile => $tmpfile,
181 testname => $basename,
182 valgrind => $valgrind,
188 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
189 Log status($display, "child died. pid = $pid");
190 undef $child_watcher;
195 my $kill_cv = AnyEvent->condvar;
197 # Don’t bother killing i3 when we haven’t started it
203 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
204 # files are not written) and fallback to killing it
205 if ($coverage_testing || $valgrind) {
207 Log status($display, 'Exiting i3 cleanly...');
208 my $i3 = i3("/tmp/nested-$display");
209 $i3->connect->cb(sub {
211 # Could not connect to i3, just kill -9 it
212 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
215 # Connected. Now send exit and continue once that’s acked.
216 $i3->command('exit')->cb(sub {
222 Log status($display, 'killing i3');
224 # No coverage testing or valgrind? Just kill -9 i3.
225 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
232 # This will be called as soon as i3 is running and answered to our
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;
239 Log status($display, 'Not starting i3, testcase does that');
241 my $duration = sprintf("%.2f", $start_duration);
242 Log status($display, "i3 startup: took $duration sec, status = $status");
245 Log status($display, "Starting $test");
248 open(my $spool, '>', \$output);
249 my $parser = TAP::Parser->new({
250 exec => [ 'sh', '-c', qq|DISPLAY=$display TESTNAME="$basename" OUTDIR="$outdir" VALGRIND=$valgrind STRACE=$strace /usr/bin/perl -Ilib $test| ],
258 my ($stdout, $stderr) = $parser->get_select_handles;
259 for my $handle ($parser->get_select_handles) {
265 # Ignore activity on stderr (unnecessary with merge => 1,
266 # but let’s keep it in here if we want to use merge => 0
267 # for some reason in the future).
268 return if defined($stderr) and $handle == $stderr;
270 my $result = $parser->next;
271 if (defined($result)) {
273 status($display, "Running $test: [$tests_completed/??]");
274 # TODO: check if we should bail out
278 # $result is not defined, we are done parsing
279 Log status($display, "$test finished");
280 close($parser->delete_spool);
281 $aggregator->add($test, $parser);
282 push @done, [ $test, $output ];
284 status_completed(scalar @done);
286 my $exitcv = $kill_i3->();
289 undef $_ for @watchers;
307 # print empty lines to seperate failed tests from statuslines
311 my ($test, $output) = @$_;
312 Log "output for $test:";
314 # print error messages of failed tests
315 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
319 $harness->summary($aggregator);
323 kill(15, $_) for @childpids;
329 complete-run.pl - Run the i3 testsuite
333 complete-run.pl [files...]
337 To run the whole testsuite on a reasonable number of Xdummy instances (your
338 running X11 will not be touched), run:
341 To run only a specific test (useful when developing a new feature), run:
342 ./complete-run t/100-fullscreen.t
350 Specifies which X11 display should be used. Can be specified multiple times and
351 will parallelize the tests:
353 # Run tests on the second X server
354 ./complete-run.pl -d :1
356 # Run four tests in parallel on some Xdummy servers
357 ./complete-run.pl -d :1,:2,:3,:4
359 Note that it is not necessary to specify this anymore. If omitted,
360 complete-run.pl will start (num_cores * 2) Xdummy instances.
364 Runs i3 under valgrind to find memory problems. The output will be available in
365 C<latest/valgrind-for-$test.log>.
369 Runs i3 under strace to trace system calls. The output will be available in
370 C<latest/strace-for-$test.log>.
372 =item B<--coverage-testing>
374 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
378 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
379 instances for some reason).
381 # Run all tests on a single Xdummy instance
382 ./complete-run.pl -p 1