s3:spoolss: Exit if fork call fails
[Samba.git] / selftest / selftest.pl
blob5d4fcf257342cae09afb9b0e3d97994f47b79186
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2010 Jelmer Vernooij <jelmer@samba.org>
4 # Copyright (C) 2007-2009 Stefan Metzmacher <metze@samba.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 use strict;
21 use FindBin qw($RealBin $Script);
22 use File::Spec;
23 use File::Temp qw(tempfile);
24 use Getopt::Long;
25 use POSIX;
26 use Cwd qw(abs_path);
27 use lib "$RealBin";
28 use Subunit;
29 use SocketWrapper;
30 use target::Samba;
31 use Time::HiRes qw(time);
33 eval {
34 require Time::HiRes;
35 Time::HiRes->import("time");
37 if ($@) {
38 print "You don't have Time::Hires installed !\n";
41 my $opt_help = 0;
42 my $opt_target = "samba";
43 my $opt_quick = 0;
44 my $opt_socket_wrapper = 0;
45 my $opt_socket_wrapper_pcap = undef;
46 my $opt_socket_wrapper_keep_pcap = undef;
47 my $opt_random_order = 0;
48 my $opt_one = 0;
49 my @opt_exclude = ();
50 my @opt_include = ();
51 my @opt_exclude_env = ();
52 my @opt_include_env = ();
53 my $opt_testenv = 0;
54 my $opt_list = 0;
55 my $opt_mitkrb5 = 0;
56 my $ldap = undef;
57 my $opt_resetup_env = undef;
58 my $opt_load_list = undef;
59 my $opt_libnss_wrapper_so_path = "";
60 my $opt_libresolv_wrapper_so_path = "";
61 my $opt_libsocket_wrapper_so_path = "";
62 my $opt_libuid_wrapper_so_path = "";
63 my $opt_libasan_so_path = "";
64 my $opt_use_dns_faking = 0;
65 my @testlists = ();
67 my $srcdir = ".";
68 my $bindir = "./bin";
69 my $prefix = "./st";
71 my @includes = ();
72 my @excludes = ();
74 sub find_in_list($$)
76 my ($list, $fullname) = @_;
78 foreach (@$list) {
79 if ($fullname =~ /$$_[0]/) {
80 return ($$_[1]) if ($$_[1]);
81 return "";
85 return undef;
88 sub skip
90 my ($name, $envname) = @_;
91 my ($env_basename, $env_localpart) = split(/:/, $envname);
93 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
94 return "environment $envname is disabled as this build does not include an AD DC";
97 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
98 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
99 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
100 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
103 return find_in_list(\@excludes, $name);
106 sub getlog_env($);
108 sub setup_pcap($)
110 my ($name) = @_;
112 return unless ($opt_socket_wrapper_pcap);
113 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
115 my $fname = $name;
116 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
118 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
120 SocketWrapper::setup_pcap($pcap_file);
122 return $pcap_file;
125 sub cleanup_pcap($$)
127 my ($pcap_file, $exitcode) = @_;
129 return unless ($opt_socket_wrapper_pcap);
130 return if ($opt_socket_wrapper_keep_pcap);
131 return unless ($exitcode == 0);
132 return unless defined($pcap_file);
134 unlink($pcap_file);
137 # expand strings from %ENV
138 sub expand_environment_strings($)
140 my $s = shift;
141 # we use a reverse sort so we do the longer ones first
142 foreach my $k (sort { $b cmp $a } keys %ENV) {
143 $s =~ s/\$$k/$ENV{$k}/g;
145 return $s;
148 sub run_testsuite($$$$$)
150 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
151 my $pcap_file = setup_pcap($name);
153 Subunit::start_testsuite($name);
154 Subunit::progress_push();
155 Subunit::report_time();
156 system($cmd);
157 Subunit::report_time();
158 Subunit::progress_pop();
160 if ($? == -1) {
161 print "command: $cmd\n";
162 printf "expanded command: %s\n", expand_environment_strings($cmd);
163 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
164 exit(1);
165 } elsif ($? & 127) {
166 print "command: $cmd\n";
167 printf "expanded command: %s\n", expand_environment_strings($cmd);
168 Subunit::end_testsuite($name, "error",
169 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
170 exit(1);
173 my $exitcode = $? >> 8;
175 my $envlog = getlog_env($envname);
176 if ($envlog ne "") {
177 print "envlog: $envlog\n";
180 print "command: $cmd\n";
181 printf "expanded command: %s\n", expand_environment_strings($cmd);
183 if ($exitcode == 0) {
184 Subunit::end_testsuite($name, "success");
185 } else {
186 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
189 cleanup_pcap($pcap_file, $exitcode);
191 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
192 print "PCAP FILE: $pcap_file\n";
195 if ($exitcode != 0) {
196 exit(1) if ($opt_one);
199 return $exitcode;
202 sub ShowHelp()
204 print "Samba test runner
205 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
206 Copyright (C) Stefan Metzmacher <metze\@samba.org>
208 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
210 Generic options:
211 --help this help page
212 --target=samba[3]|win Samba version to target
213 --testlist=FILE file to read available tests from
214 --exclude=FILE Exclude tests listed in the file
215 --include=FILE Include tests listed in the file
216 --exclude-env=ENV Exclude tests for the specified environment
217 --include-env=ENV Include tests for the specified environment
219 Paths:
220 --prefix=DIR prefix to run tests in [st]
221 --srcdir=DIR source directory [.]
222 --bindir=DIR binaries directory [./bin]
224 Preload cwrap:
225 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
226 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
227 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
228 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
229 --asan_so_path=FILE the asan library to preload
231 DNS:
232 --use-dns-faking Fake DNS entries rather than talking to our
233 DNS implementation.
235 Target Specific:
236 --socket-wrapper-pcap save traffic to pcap directories
237 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
238 failed
239 --socket-wrapper enable socket wrapper
241 Samba4 Specific:
242 --ldap=openldap|fedora-ds back samba onto specified ldap server
244 Behaviour:
245 --quick run quick overall test
246 --one abort when the first test fails
247 --testenv run a shell in the requested test environment
248 --list list available tests
250 exit(0);
253 my $result = GetOptions (
254 'help|h|?' => \$opt_help,
255 'target=s' => \$opt_target,
256 'prefix=s' => \$prefix,
257 'socket-wrapper' => \$opt_socket_wrapper,
258 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
259 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
260 'quick' => \$opt_quick,
261 'one' => \$opt_one,
262 'exclude=s' => \@opt_exclude,
263 'include=s' => \@opt_include,
264 'exclude-env=s' => \@opt_exclude_env,
265 'include-env=s' => \@opt_include_env,
266 'srcdir=s' => \$srcdir,
267 'bindir=s' => \$bindir,
268 'testenv' => \$opt_testenv,
269 'list' => \$opt_list,
270 'mitkrb5' => \$opt_mitkrb5,
271 'ldap:s' => \$ldap,
272 'resetup-environment' => \$opt_resetup_env,
273 'testlist=s' => \@testlists,
274 'random-order' => \$opt_random_order,
275 'load-list=s' => \$opt_load_list,
276 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
277 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
278 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
279 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
280 'asan_so_path=s' => \$opt_libasan_so_path,
281 'use-dns-faking' => \$opt_use_dns_faking
284 exit(1) if (not $result);
286 ShowHelp() if ($opt_help);
288 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
290 # we want unbuffered output
291 $| = 1;
293 my @tests = @ARGV;
295 # quick hack to disable rpc validation when using valgrind - its way too slow
296 unless (defined($ENV{VALGRIND})) {
297 $ENV{VALIDATE} = "validate";
298 $ENV{MALLOC_CHECK_} = 3;
301 # make all our python scripts unbuffered
302 $ENV{PYTHONUNBUFFERED} = 1;
304 # do not depend on the users setup
305 $ENV{TZ} = "UTC";
307 my $bindir_abs = abs_path($bindir);
309 # Backwards compatibility:
310 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
311 if (defined($ENV{FEDORA_DS_ROOT})) {
312 $ldap = "fedora-ds";
313 } else {
314 $ldap = "openldap";
318 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
319 if ($ldap) {
320 # LDAP is slow
321 $torture_maxtime *= 2;
324 $prefix =~ s+//+/+;
325 $prefix =~ s+/./+/+;
326 $prefix =~ s+/$++;
328 die("using an empty prefix isn't allowed") unless $prefix ne "";
330 # Ensure we have the test prefix around.
332 # We need restrictive
333 # permissions on this as some subdirectories in this tree will have
334 # wider permissions (ie 0777) and this would allow other users on the
335 # host to subvert the test process.
336 umask 0077;
337 mkdir($prefix, 0700) unless -d $prefix;
338 chmod 0700, $prefix;
339 # We need to have no umask limitations for the tests.
340 umask 0000;
342 my $prefix_abs = abs_path($prefix);
343 my $tmpdir_abs = abs_path("$prefix/tmp");
344 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
346 my $srcdir_abs = abs_path($srcdir);
348 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
349 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
351 $ENV{SAMBA_SELFTEST} = "1";
353 $ENV{PREFIX} = $prefix;
354 $ENV{PREFIX_ABS} = $prefix_abs;
355 $ENV{SRCDIR} = $srcdir;
356 $ENV{SRCDIR_ABS} = $srcdir_abs;
357 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
358 $ENV{BINDIR} = $bindir_abs;
360 my $tls_enabled = not $opt_quick;
361 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
363 sub prefix_pathvar($$)
365 my ($name, $newpath) = @_;
366 if (defined($ENV{$name})) {
367 $ENV{$name} = "$newpath:$ENV{$name}";
368 } else {
369 $ENV{$name} = $newpath;
372 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
373 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
375 if ($opt_socket_wrapper_keep_pcap) {
376 # Socket wrapper keep pcap implies socket wrapper pcap
377 $opt_socket_wrapper_pcap = 1;
380 if ($opt_socket_wrapper_pcap) {
381 # Socket wrapper pcap implies socket wrapper
382 $opt_socket_wrapper = 1;
385 my $ld_preload = $ENV{LD_PRELOAD};
387 if ($opt_libasan_so_path) {
388 if ($ld_preload) {
389 $ld_preload = "$ld_preload:$opt_libasan_so_path";
390 } else {
391 $ld_preload = "$opt_libasan_so_path";
395 if ($opt_libnss_wrapper_so_path) {
396 if ($ld_preload) {
397 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
398 } else {
399 $ld_preload = "$opt_libnss_wrapper_so_path";
403 if ($opt_libresolv_wrapper_so_path) {
404 if ($ld_preload) {
405 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
406 } else {
407 $ld_preload = "$opt_libresolv_wrapper_so_path";
411 if ($opt_libsocket_wrapper_so_path) {
412 if ($ld_preload) {
413 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
414 } else {
415 $ld_preload = "$opt_libsocket_wrapper_so_path";
419 if ($opt_libuid_wrapper_so_path) {
420 if ($ld_preload) {
421 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
422 } else {
423 $ld_preload = "$opt_libuid_wrapper_so_path";
427 if (defined($ENV{USE_NAMESPACES})) {
428 print "Using linux containerization for selftest testenv(s)...\n";
430 # Create a common bridge to connect up the testenv namespaces. We give
431 # it the client's IP address, as this is where the tests will run from
432 my $ipv4_addr = Samba::get_ipv4_addr("client");
433 my $ipv6_addr = Samba::get_ipv6_addr("client");
434 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
437 $ENV{LD_PRELOAD} = $ld_preload;
438 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
440 # Enable uid_wrapper globally
441 $ENV{UID_WRAPPER} = 1;
443 # We are already hitting the limit, so double it.
444 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
446 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
448 # This is needed in order to allow the ldb_*ldap module
449 # to work with a preloaded socket wrapper.
450 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
452 my $socket_wrapper_dir;
453 if ($opt_socket_wrapper) {
454 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
455 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
456 } elsif (not $opt_list) {
457 unless ($< == 0) {
458 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
462 if ($opt_use_dns_faking) {
463 print "DNS: Faking nameserver\n";
464 $ENV{SAMBA_DNS_FAKING} = 1;
467 my $target;
468 my $testenv_default = "none";
470 if ($opt_mitkrb5 == 1) {
471 $ENV{MITKRB5} = $opt_mitkrb5;
474 # After this many seconds, the server will self-terminate. All tests
475 # must terminate in this time, and testenv will only stay alive this
476 # long
478 my $server_maxtime;
479 if ($opt_testenv) {
480 # 1 year should be enough :-)
481 $server_maxtime = 365 * 24 * 60 * 60;
482 } else {
483 # make test should run under 5 hours
484 $server_maxtime = 5 * 60 * 60;
487 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
488 $server_maxtime = $ENV{SMBD_MAXTIME};
491 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
492 unless ($opt_list) {
493 if ($opt_target eq "samba") {
494 $testenv_default = "ad_dc";
495 } elsif ($opt_target eq "samba3") {
496 $testenv_default = "nt4_member";
500 sub read_test_regexes($)
502 my ($name) = @_;
503 my @ret = ();
504 open(LF, "<$name") or die("unable to read $name: $!");
505 while (<LF>) {
506 chomp;
507 next if (/^#/);
508 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
509 push (@ret, [$1, $4]);
510 } else {
511 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
512 push (@ret, [$_, undef]);
515 close(LF);
516 return @ret;
519 foreach (@opt_exclude) {
520 push (@excludes, read_test_regexes($_));
523 foreach (@opt_include) {
524 push (@includes, read_test_regexes($_));
527 # We give the selftest client 6 different IPv4 addresses to use. Most tests
528 # only use the first (.11) IP. Note that winsreplication.c is one test that
529 # uses the other IPs (search for iface_list_count()).
530 my $interfaces = Samba::get_interfaces_config("client", 6);
532 my $clientdir = "$prefix_abs/client";
534 my $conffile = "$clientdir/client.conf";
535 $ENV{SMB_CONF_PATH} = $conffile;
537 sub write_clientconf($$$)
539 my ($conffile, $clientdir, $vars) = @_;
541 mkdir("$clientdir", 0777) unless -d "$clientdir";
543 if ( -d "$clientdir/private" ) {
544 unlink <$clientdir/private/*>;
545 } else {
546 mkdir("$clientdir/private", 0777);
549 if ( -d "$clientdir/bind-dns" ) {
550 unlink <$clientdir/bind-dns/*>;
551 } else {
552 mkdir("$clientdir/bind-dns", 0777);
555 if ( -d "$clientdir/lockdir" ) {
556 unlink <$clientdir/lockdir/*>;
557 } else {
558 mkdir("$clientdir/lockdir", 0777);
561 if ( -d "$clientdir/statedir" ) {
562 unlink <$clientdir/statedir/*>;
563 } else {
564 mkdir("$clientdir/statedir", 0777);
567 if ( -d "$clientdir/cachedir" ) {
568 unlink <$clientdir/cachedir/*>;
569 } else {
570 mkdir("$clientdir/cachedir", 0777);
573 # this is ugly, but the ncalrpcdir needs exactly 0755
574 # otherwise tests fail.
575 my $mask = umask;
576 umask 0022;
577 if ( -d "$clientdir/ncalrpcdir/np" ) {
578 unlink <$clientdir/ncalrpcdir/np/*>;
579 rmdir "$clientdir/ncalrpcdir/np";
581 if ( -d "$clientdir/ncalrpcdir" ) {
582 unlink <$clientdir/ncalrpcdir/*>;
583 rmdir "$clientdir/ncalrpcdir";
585 mkdir("$clientdir/ncalrpcdir", 0755);
586 umask $mask;
588 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
589 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
590 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
591 my $ca_users_dir = "$cadir/Users";
593 if ( -d "$clientdir/pkinit" ) {
594 unlink <$clientdir/pkinit/*>;
595 } else {
596 mkdir("$clientdir/pkinit", 0700);
599 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
600 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
601 # We make a copy here and make the certificated easily
602 # accessable in the client environment.
603 my $mask = umask;
604 umask 0077;
605 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
606 for my $d (readdir USERS) {
607 my $user_dir = "${ca_users_dir}/${d}";
608 next if ${d} =~ /^\./;
609 next if (! -d "${user_dir}");
610 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
611 for my $l (readdir USER) {
612 my $user_link = "${user_dir}/${l}";
613 next if ${l} =~ /^\./;
614 next if (! -l "${user_link}");
616 my $dest = "${clientdir}/pkinit/${l}";
617 Samba::copy_file_content(${user_link}, ${dest});
619 closedir USER;
621 closedir USERS;
622 umask $mask;
624 open(CF, ">$conffile");
625 print CF "[global]\n";
626 print CF "\tnetbios name = client\n";
627 if (defined($vars->{DOMAIN})) {
628 print CF "\tworkgroup = $vars->{DOMAIN}\n";
630 if (defined($vars->{REALM})) {
631 print CF "\trealm = $vars->{REALM}\n";
633 if ($opt_socket_wrapper) {
634 print CF "\tinterfaces = $interfaces\n";
636 print CF "
637 private dir = $clientdir/private
638 binddns dir = $clientdir/bind-dns
639 lock dir = $clientdir/lockdir
640 state directory = $clientdir/statedir
641 cache directory = $clientdir/cachedir
642 ncalrpc dir = $clientdir/ncalrpcdir
643 panic action = $RealBin/gdb_backtrace \%d
644 max xmit = 32K
645 notify:inotify = false
646 ldb:nosync = true
647 system:anonymous = true
648 client lanman auth = Yes
649 client min protocol = CORE
650 log level = 1
651 torture:basedir = $clientdir
652 #We don't want to pass our self-tests if the PAC code is wrong
653 gensec:require_pac = true
654 #We don't want to run 'speed' tests for very long
655 torture:timelimit = 1
656 winbind separator = /
657 tls cafile = ${cacert}
658 tls crlfile = ${cacrl_pem}
659 tls verify peer = no_check
660 include system krb5 conf = no
662 close(CF);
665 my @todo = ();
667 sub should_run_test($)
669 my $name = shift;
670 if ($#tests == -1) {
671 return 1;
673 for (my $i=0; $i <= $#tests; $i++) {
674 if ($name =~ /$tests[$i]/i) {
675 return 1;
678 return 0;
681 sub read_testlist($)
683 my ($filename) = @_;
685 my @ret = ();
686 open(IN, $filename) or die("Unable to open $filename: $!");
688 while (<IN>) {
689 if (/-- TEST(-LOADLIST|) --\n/) {
690 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
691 my $name = <IN>;
692 $name =~ s/\n//g;
693 my $env = <IN>;
694 $env =~ s/\n//g;
695 my $loadlist;
696 if ($supports_loadlist) {
697 $loadlist = <IN>;
698 $loadlist =~ s/\n//g;
700 my $cmdline = <IN>;
701 $cmdline =~ s/\n//g;
702 if (should_run_test($name) == 1) {
703 push (@ret, [$name, $env, $cmdline, $loadlist]);
705 } else {
706 print;
709 close(IN) or die("Error creating recipe from $filename");
710 return @ret;
713 if ($#testlists == -1) {
714 die("No testlists specified");
717 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
718 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
719 $ENV{TMPDIR} = "$tmpdir_abs";
720 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
721 if ($opt_quick) {
722 $ENV{SELFTEST_QUICK} = "1";
723 } else {
724 $ENV{SELFTEST_QUICK} = "";
726 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
728 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
729 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
731 my @available = ();
732 foreach my $fn (@testlists) {
733 foreach (read_testlist($fn)) {
734 my $name = $$_[0];
735 next if (@includes and not defined(find_in_list(\@includes, $name)));
736 push (@available, $_);
740 my $restricted = undef;
741 my $restricted_used = {};
743 if ($opt_load_list) {
744 $restricted = [];
745 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
746 while (<LOAD_LIST>) {
747 chomp;
748 push (@$restricted, $_);
750 close(LOAD_LIST);
753 my $individual_tests = undef;
754 $individual_tests = {};
756 foreach my $testsuite (@available) {
757 my $name = $$testsuite[0];
758 my $skipreason = skip(@$testsuite);
759 if (defined($restricted)) {
760 # Find the testsuite for this test
761 my $match = undef;
762 foreach my $r (@$restricted) {
763 if ($r eq $name) {
764 $individual_tests->{$name} = [];
765 $match = $r;
766 $restricted_used->{$r} = 1;
767 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
768 push(@{$individual_tests->{$name}}, $r);
769 $match = $r;
770 $restricted_used->{$r} = 1;
773 if ($match) {
774 if (defined($skipreason)) {
775 if (not $opt_list) {
776 Subunit::skip_testsuite($name, $skipreason);
778 } else {
779 push(@todo, $testsuite);
782 } elsif (defined($skipreason)) {
783 if (not $opt_list) {
784 Subunit::skip_testsuite($name, $skipreason);
786 } else {
787 push(@todo, $testsuite);
791 if (defined($restricted)) {
792 foreach (@$restricted) {
793 unless (defined($restricted_used->{$_})) {
794 print "No test or testsuite found matching $_\n";
797 } elsif ($#todo == -1) {
798 print STDERR "No tests to run\n";
799 exit(1);
802 my $suitestotal = $#todo + 1;
804 unless ($opt_list) {
805 Subunit::progress($suitestotal);
806 Subunit::report_time();
809 my $i = 0;
810 $| = 1;
812 my %running_envs = ();
814 sub get_running_env($)
816 my ($name) = @_;
818 my $envname = $name;
820 $envname =~ s/:.*//;
822 return $running_envs{$envname};
825 sub sighandler($)
827 my $signame = shift;
829 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
830 $SIG{PIPE} = 'IGNORE';
832 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
834 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
835 teardown_env($_) foreach(keys %running_envs);
836 system("pstree -p $$");
837 print "$0: PID[$$]: Exiting...\n";
838 exit(1);
841 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
843 sub setup_env($$)
845 my ($name, $prefix) = @_;
847 my $testenv_vars = undef;
849 my $envname = $name;
850 my $option = $name;
852 $envname =~ s/:.*//;
853 $option =~ s/^[^:]*//;
854 $option =~ s/^://;
856 $option = "client" if $option eq "";
858 # Initially clear out the environment for the provision, so previous envs'
859 # variables don't leak in. Provisioning steps must explicitly set their
860 # necessary variables when calling out to other executables
861 Samba::clear_exported_envvars();
862 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
863 delete $ENV{SMB_CONF_PATH};
865 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
867 if (defined(get_running_env($envname))) {
868 $testenv_vars = get_running_env($envname);
869 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
870 print $testenv_vars->{target}->getlog_env($testenv_vars);
871 $testenv_vars = undef;
873 } else {
874 $testenv_vars = $target->setup_env($envname, $prefix);
875 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
876 return $testenv_vars;
877 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
878 $testenv_vars->{target} = $target;
880 if (not defined($testenv_vars)) {
881 if ($opt_one) {
882 die("$opt_target can't start up known environment '$envname'");
883 } else {
884 warn("$opt_target can't start up known environment '$envname'");
889 return undef unless defined($testenv_vars);
891 $running_envs{$envname} = $testenv_vars;
893 if ($option eq "local") {
894 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
895 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
896 } elsif ($option eq "client") {
897 SocketWrapper::set_default_iface(11);
898 write_clientconf($conffile, $clientdir, $testenv_vars);
899 $ENV{SMB_CONF_PATH} = $conffile;
900 } else {
901 die("Unknown option[$option] for envname[$envname]");
904 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
905 Samba::export_envvars($testenv_vars);
907 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
908 unlink($krb5_ccache_path);
909 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
910 return $testenv_vars;
913 sub getlog_env($)
915 my ($envname) = @_;
916 return "" if ($envname eq "none");
917 my $env = get_running_env($envname);
918 return $env->{target}->getlog_env($env);
921 sub check_env($)
923 my ($envname) = @_;
924 my $env = get_running_env($envname);
925 return $env->{target}->check_env($env);
928 sub teardown_env($)
930 my ($envname) = @_;
931 return if ($envname eq "none");
932 print STDERR "teardown_env($envname)\n";
933 my $env = get_running_env($envname);
934 $env->{target}->teardown_env($env);
935 delete $running_envs{$envname};
938 # This 'global' file needs to be empty when we start
939 unlink("$prefix_abs/dns_host_file");
940 unlink("$prefix_abs/hosts");
942 if ($opt_random_order) {
943 require List::Util;
944 my @newtodo = List::Util::shuffle(@todo);
945 @todo = @newtodo;
948 if ($opt_testenv) {
949 my $testenv_name = $ENV{SELFTEST_TESTENV};
950 $testenv_name = $testenv_default unless defined($testenv_name);
952 my $testenv_vars = setup_env($testenv_name, $prefix);
954 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
955 die("Unable to setup environment $testenv_name");
958 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
959 $ENV{ENVNAME} = $testenv_name;
961 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
963 my @term_args = ("echo -e \"
964 Welcome to the Samba4 Test environment '$testenv_name'
966 This matches the client environment used in make test
967 server is pid `cat \$PIDDIR/samba.pid`
969 Some useful environment variables:
970 TORTURE_OPTIONS=\$TORTURE_OPTIONS
971 SMB_CONF_PATH=\$SMB_CONF_PATH
973 $envvarstr
974 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
975 my @term = ();
976 if ($ENV{TERMINAL}) {
977 @term = ($ENV{TERMINAL});
978 # override the default terminal args (if specified)
979 if (defined($ENV{TERMINAL_ARGS})) {
980 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
982 } else {
983 @term = ("xterm", "-e");
984 unshift(@term_args, ("bash", "-c"));
987 system(@term, @term_args);
989 teardown_env($testenv_name);
990 } elsif ($opt_list) {
991 foreach (@todo) {
992 my $name = $$_[0];
993 my $envname = $$_[1];
994 my $cmd = $$_[2];
995 my $listcmd = $$_[3];
997 unless (defined($listcmd)) {
998 warn("Unable to list tests in $name");
999 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1000 # a single "test".
1001 print "$name\n";
1002 next;
1005 system($listcmd);
1007 if ($? == -1) {
1008 die("Unable to run $listcmd: $!");
1009 } elsif ($? & 127) {
1010 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1013 my $exitcode = $? >> 8;
1014 if ($exitcode != 0) {
1015 die("$cmd exited with exit code $exitcode");
1018 } else {
1019 foreach (@todo) {
1020 $i++;
1021 my $cmd = $$_[2];
1022 my $name = $$_[0];
1023 my $envname = $$_[1];
1024 my $envvars = setup_env($envname, $prefix);
1026 if (not defined($envvars)) {
1027 Subunit::start_testsuite($name);
1028 Subunit::end_testsuite($name, "error",
1029 "unable to set up environment $envname - exiting");
1030 next;
1031 } elsif ($envvars eq "UNKNOWN") {
1032 Subunit::start_testsuite($name);
1033 Subunit::end_testsuite($name, "error",
1034 "environment $envname is unknown - exiting");
1035 next;
1038 # Generate a file with the individual tests to run, if the
1039 # test runner for this test suite supports it.
1040 if ($individual_tests and $individual_tests->{$name}) {
1041 if ($$_[3]) {
1042 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1043 foreach my $test (@{$individual_tests->{$name}}) {
1044 print $fh substr($test, length($name)+1) . "\n";
1046 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1047 } else {
1048 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1052 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1054 teardown_env($envname) if ($opt_resetup_env);
1058 print "\n";
1060 teardown_env($_) foreach (keys %running_envs);
1062 my $failed = 0;
1064 # if there were any valgrind failures, show them
1065 foreach (<$prefix/valgrind.log*>) {
1066 next unless (-s $_);
1067 print "VALGRIND FAILURE\n";
1068 $failed++;
1069 system("cat $_");
1071 exit 0;