ctdb-daemon: Improve error handling consistency
[Samba.git] / selftest / selftest.pl
blob0e56e6a13ef13a318c43cec2f0f0653e443fdd0b
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;
32 eval {
33 require Time::HiRes;
34 Time::HiRes->import("time");
36 if ($@) {
37 print "You don't have Time::Hires installed !\n";
40 my $opt_help = 0;
41 my $opt_target = "samba";
42 my $opt_quick = 0;
43 my $opt_socket_wrapper = 0;
44 my $opt_socket_wrapper_pcap = undef;
45 my $opt_socket_wrapper_keep_pcap = undef;
46 my $opt_random_order = 0;
47 my $opt_one = 0;
48 my @opt_exclude = ();
49 my @opt_include = ();
50 my @opt_exclude_env = ();
51 my @opt_include_env = ();
52 my $opt_testenv = 0;
53 my $opt_list = 0;
54 my $opt_mitkrb5 = 0;
55 my $ldap = undef;
56 my $opt_resetup_env = undef;
57 my $opt_load_list = undef;
58 my $opt_libnss_wrapper_so_path = "";
59 my $opt_libresolv_wrapper_so_path = "";
60 my $opt_libsocket_wrapper_so_path = "";
61 my $opt_libuid_wrapper_so_path = "";
62 my $opt_use_dns_faking = 0;
63 my @testlists = ();
65 my $srcdir = ".";
66 my $bindir = "./bin";
67 my $prefix = "./st";
69 my @includes = ();
70 my @excludes = ();
72 sub find_in_list($$)
74 my ($list, $fullname) = @_;
76 foreach (@$list) {
77 if ($fullname =~ /$$_[0]/) {
78 return ($$_[1]) if ($$_[1]);
79 return "";
83 return undef;
86 sub skip($)
88 my ($name) = @_;
90 return find_in_list(\@excludes, $name);
93 sub getlog_env($);
95 sub setup_pcap($)
97 my ($name) = @_;
99 return unless ($opt_socket_wrapper_pcap);
100 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
102 my $fname = $name;
103 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
105 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
107 SocketWrapper::setup_pcap($pcap_file);
109 return $pcap_file;
112 sub cleanup_pcap($$)
114 my ($pcap_file, $exitcode) = @_;
116 return unless ($opt_socket_wrapper_pcap);
117 return if ($opt_socket_wrapper_keep_pcap);
118 return unless ($exitcode == 0);
119 return unless defined($pcap_file);
121 unlink($pcap_file);
124 # expand strings from %ENV
125 sub expand_environment_strings($)
127 my $s = shift;
128 # we use a reverse sort so we do the longer ones first
129 foreach my $k (sort { $b cmp $a } keys %ENV) {
130 $s =~ s/\$$k/$ENV{$k}/g;
132 return $s;
135 sub run_testsuite($$$$$)
137 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
138 my $pcap_file = setup_pcap($name);
140 Subunit::start_testsuite($name);
141 Subunit::progress_push();
142 Subunit::report_time(time());
143 system($cmd);
144 Subunit::report_time(time());
145 Subunit::progress_pop();
147 if ($? == -1) {
148 print "command: $cmd\n";
149 printf "expanded command: %s\n", expand_environment_strings($cmd);
150 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
151 exit(1);
152 } elsif ($? & 127) {
153 print "command: $cmd\n";
154 printf "expanded command: %s\n", expand_environment_strings($cmd);
155 Subunit::end_testsuite($name, "error",
156 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
157 exit(1);
160 my $exitcode = $? >> 8;
162 my $envlog = getlog_env($envname);
163 if ($envlog ne "") {
164 print "envlog: $envlog\n";
167 print "command: $cmd\n";
168 printf "expanded command: %s\n", expand_environment_strings($cmd);
170 if ($exitcode == 0) {
171 Subunit::end_testsuite($name, "success");
172 } else {
173 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
176 cleanup_pcap($pcap_file, $exitcode);
178 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
179 print "PCAP FILE: $pcap_file\n";
182 if ($exitcode != 0) {
183 exit(1) if ($opt_one);
186 return $exitcode;
189 sub ShowHelp()
191 print "Samba test runner
192 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
193 Copyright (C) Stefan Metzmacher <metze\@samba.org>
195 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
197 Generic options:
198 --help this help page
199 --target=samba[3]|win Samba version to target
200 --testlist=FILE file to read available tests from
201 --exclude=FILE Exclude tests listed in the file
202 --include=FILE Include tests listed in the file
203 --exclude-env=ENV Exclude tests for the specified environment
204 --include-env=ENV Include tests for the specified environment
206 Paths:
207 --prefix=DIR prefix to run tests in [st]
208 --srcdir=DIR source directory [.]
209 --bindir=DIR binaries directory [./bin]
211 Preload cwrap:
212 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
213 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
214 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
215 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
217 DNS:
218 --use-dns-faking Fake DNS entries rather than talking to our
219 DNS implementation.
221 Target Specific:
222 --socket-wrapper-pcap save traffic to pcap directories
223 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
224 failed
225 --socket-wrapper enable socket wrapper
227 Samba4 Specific:
228 --ldap=openldap|fedora-ds back samba onto specified ldap server
230 Behaviour:
231 --quick run quick overall test
232 --one abort when the first test fails
233 --testenv run a shell in the requested test environment
234 --list list available tests
236 exit(0);
239 my $result = GetOptions (
240 'help|h|?' => \$opt_help,
241 'target=s' => \$opt_target,
242 'prefix=s' => \$prefix,
243 'socket-wrapper' => \$opt_socket_wrapper,
244 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
245 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
246 'quick' => \$opt_quick,
247 'one' => \$opt_one,
248 'exclude=s' => \@opt_exclude,
249 'include=s' => \@opt_include,
250 'exclude-env=s' => \@opt_exclude_env,
251 'include-env=s' => \@opt_include_env,
252 'srcdir=s' => \$srcdir,
253 'bindir=s' => \$bindir,
254 'testenv' => \$opt_testenv,
255 'list' => \$opt_list,
256 'mitkrb5' => \$opt_mitkrb5,
257 'ldap:s' => \$ldap,
258 'resetup-environment' => \$opt_resetup_env,
259 'testlist=s' => \@testlists,
260 'random-order' => \$opt_random_order,
261 'load-list=s' => \$opt_load_list,
262 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
263 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
264 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
265 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
266 'use-dns-faking' => \$opt_use_dns_faking
269 exit(1) if (not $result);
271 ShowHelp() if ($opt_help);
273 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
275 # we want unbuffered output
276 $| = 1;
278 my @tests = @ARGV;
280 # quick hack to disable rpc validation when using valgrind - its way too slow
281 unless (defined($ENV{VALGRIND})) {
282 $ENV{VALIDATE} = "validate";
283 $ENV{MALLOC_CHECK_} = 3;
286 # make all our python scripts unbuffered
287 $ENV{PYTHONUNBUFFERED} = 1;
289 my $bindir_abs = abs_path($bindir);
291 # Backwards compatibility:
292 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
293 if (defined($ENV{FEDORA_DS_ROOT})) {
294 $ldap = "fedora-ds";
295 } else {
296 $ldap = "openldap";
300 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
301 if ($ldap) {
302 # LDAP is slow
303 $torture_maxtime *= 2;
306 $prefix =~ s+//+/+;
307 $prefix =~ s+/./+/+;
308 $prefix =~ s+/$++;
310 die("using an empty prefix isn't allowed") unless $prefix ne "";
312 # Ensure we have the test prefix around.
314 # We need restrictive
315 # permissions on this as some subdirectories in this tree will have
316 # wider permissions (ie 0777) and this would allow other users on the
317 # host to subvert the test process.
318 umask 0077;
319 mkdir($prefix, 0700) unless -d $prefix;
320 chmod 0700, $prefix;
321 # We need to have no umask limitations for the tests.
322 umask 0000;
324 my $prefix_abs = abs_path($prefix);
325 my $tmpdir_abs = abs_path("$prefix/tmp");
326 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
328 my $srcdir_abs = abs_path($srcdir);
330 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
331 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
333 $ENV{SAMBA_SELFTEST} = "1";
335 $ENV{PREFIX} = $prefix;
336 $ENV{PREFIX_ABS} = $prefix_abs;
337 $ENV{SRCDIR} = $srcdir;
338 $ENV{SRCDIR_ABS} = $srcdir_abs;
339 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
340 $ENV{BINDIR} = $bindir_abs;
342 my $tls_enabled = not $opt_quick;
343 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
345 sub prefix_pathvar($$)
347 my ($name, $newpath) = @_;
348 if (defined($ENV{$name})) {
349 $ENV{$name} = "$newpath:$ENV{$name}";
350 } else {
351 $ENV{$name} = $newpath;
354 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
355 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
357 if ($opt_socket_wrapper_keep_pcap) {
358 # Socket wrapper keep pcap implies socket wrapper pcap
359 $opt_socket_wrapper_pcap = 1;
362 if ($opt_socket_wrapper_pcap) {
363 # Socket wrapper pcap implies socket wrapper
364 $opt_socket_wrapper = 1;
367 my $ld_preload = $ENV{LD_PRELOAD};
369 if ($opt_libnss_wrapper_so_path) {
370 if ($ld_preload) {
371 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
372 } else {
373 $ld_preload = "$opt_libnss_wrapper_so_path";
377 if ($opt_libresolv_wrapper_so_path) {
378 if ($ld_preload) {
379 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
380 } else {
381 $ld_preload = "$opt_libresolv_wrapper_so_path";
385 if ($opt_libsocket_wrapper_so_path) {
386 if ($ld_preload) {
387 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
388 } else {
389 $ld_preload = "$opt_libsocket_wrapper_so_path";
393 if ($opt_libuid_wrapper_so_path) {
394 if ($ld_preload) {
395 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
396 } else {
397 $ld_preload = "$opt_libuid_wrapper_so_path";
401 $ENV{LD_PRELOAD} = $ld_preload;
402 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
404 # Enable uid_wrapper globally
405 $ENV{UID_WRAPPER} = 1;
407 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
409 # This is needed in order to allow the ldb_*ldap module
410 # to work with a preloaded socket wrapper.
411 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
413 my $socket_wrapper_dir;
414 if ($opt_socket_wrapper) {
415 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
416 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
417 } elsif (not $opt_list) {
418 unless ($< == 0) {
419 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
423 if ($opt_use_dns_faking) {
424 print "DNS: Faking nameserver\n";
425 $ENV{SAMBA_DNS_FAKING} = 1;
428 my $target;
429 my $testenv_default = "none";
431 if ($opt_mitkrb5 == 1) {
432 $ENV{MITKRB5} = $opt_mitkrb5;
435 # After this many seconds, the server will self-terminate. All tests
436 # must terminate in this time, and testenv will only stay alive this
437 # long
439 my $server_maxtime;
440 if ($opt_testenv) {
441 # 1 year should be enough :-)
442 $server_maxtime = 365 * 24 * 60 * 60;
443 } else {
444 # make test should run under 5 hours
445 $server_maxtime = 5 * 60 * 60;
448 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
449 $server_maxtime = $ENV{SMBD_MAXTIME};
452 unless ($opt_list) {
453 if ($opt_target eq "samba") {
454 $testenv_default = "ad_dc";
455 require target::Samba;
456 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
457 } elsif ($opt_target eq "samba3") {
458 $testenv_default = "nt4_member";
459 require target::Samba3;
460 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
464 sub read_test_regexes($)
466 my ($name) = @_;
467 my @ret = ();
468 open(LF, "<$name") or die("unable to read $name: $!");
469 while (<LF>) {
470 chomp;
471 next if (/^#/);
472 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
473 push (@ret, [$1, $4]);
474 } else {
475 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
476 push (@ret, [$_, undef]);
479 close(LF);
480 return @ret;
483 foreach (@opt_exclude) {
484 push (@excludes, read_test_regexes($_));
487 foreach (@opt_include) {
488 push (@includes, read_test_regexes($_));
491 my $interfaces = join(',', ("127.0.0.11/8",
492 "127.0.0.12/8",
493 "127.0.0.13/8",
494 "127.0.0.14/8",
495 "127.0.0.15/8",
496 "127.0.0.16/8"));
498 my $clientdir = "$prefix_abs/client";
500 my $conffile = "$clientdir/client.conf";
501 $ENV{SMB_CONF_PATH} = $conffile;
503 sub write_clientconf($$$)
505 my ($conffile, $clientdir, $vars) = @_;
507 mkdir("$clientdir", 0777) unless -d "$clientdir";
509 if ( -d "$clientdir/private" ) {
510 unlink <$clientdir/private/*>;
511 } else {
512 mkdir("$clientdir/private", 0777);
515 if ( -d "$clientdir/bind-dns" ) {
516 unlink <$clientdir/bind-dns/*>;
517 } else {
518 mkdir("$clientdir/bind-dns", 0777);
521 if ( -d "$clientdir/lockdir" ) {
522 unlink <$clientdir/lockdir/*>;
523 } else {
524 mkdir("$clientdir/lockdir", 0777);
527 if ( -d "$clientdir/statedir" ) {
528 unlink <$clientdir/statedir/*>;
529 } else {
530 mkdir("$clientdir/statedir", 0777);
533 if ( -d "$clientdir/cachedir" ) {
534 unlink <$clientdir/cachedir/*>;
535 } else {
536 mkdir("$clientdir/cachedir", 0777);
539 # this is ugly, but the ncalrpcdir needs exactly 0755
540 # otherwise tests fail.
541 my $mask = umask;
542 umask 0022;
543 if ( -d "$clientdir/ncalrpcdir/np" ) {
544 unlink <$clientdir/ncalrpcdir/np/*>;
545 rmdir "$clientdir/ncalrpcdir/np";
547 if ( -d "$clientdir/ncalrpcdir" ) {
548 unlink <$clientdir/ncalrpcdir/*>;
549 rmdir "$clientdir/ncalrpcdir";
551 mkdir("$clientdir/ncalrpcdir", 0755);
552 umask $mask;
554 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
555 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
556 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
557 my $ca_users_dir = "$cadir/Users";
559 if ( -d "$clientdir/pkinit" ) {
560 unlink <$clientdir/pkinit/*>;
561 } else {
562 mkdir("$clientdir/pkinit", 0700);
565 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
566 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
567 # We make a copy here and make the certificated easily
568 # accessable in the client environment.
569 my $mask = umask;
570 umask 0077;
571 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
572 for my $d (readdir USERS) {
573 my $user_dir = "${ca_users_dir}/${d}";
574 next if ${d} =~ /^\./;
575 next if (! -d "${user_dir}");
576 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
577 for my $l (readdir USER) {
578 my $user_link = "${user_dir}/${l}";
579 next if ${l} =~ /^\./;
580 next if (! -l "${user_link}");
582 my $dest = "${clientdir}/pkinit/${l}";
583 Samba::copy_file_content(${user_link}, ${dest});
585 closedir USER;
587 closedir USERS;
588 umask $mask;
590 open(CF, ">$conffile");
591 print CF "[global]\n";
592 print CF "\tnetbios name = client\n";
593 if (defined($vars->{DOMAIN})) {
594 print CF "\tworkgroup = $vars->{DOMAIN}\n";
596 if (defined($vars->{REALM})) {
597 print CF "\trealm = $vars->{REALM}\n";
599 if ($opt_socket_wrapper) {
600 print CF "\tinterfaces = $interfaces\n";
602 print CF "
603 private dir = $clientdir/private
604 binddns dir = $clientdir/bind-dns
605 lock dir = $clientdir/lockdir
606 state directory = $clientdir/statedir
607 cache directory = $clientdir/cachedir
608 ncalrpc dir = $clientdir/ncalrpcdir
609 panic action = $RealBin/gdb_backtrace \%d
610 max xmit = 32K
611 notify:inotify = false
612 ldb:nosync = true
613 system:anonymous = true
614 client lanman auth = Yes
615 log level = 1
616 torture:basedir = $clientdir
617 #We don't want to pass our self-tests if the PAC code is wrong
618 gensec:require_pac = true
619 #We don't want to run 'speed' tests for very long
620 torture:timelimit = 1
621 winbind separator = /
622 tls cafile = ${cacert}
623 tls crlfile = ${cacrl_pem}
624 tls verify peer = no_check
625 include system krb5 conf = no
627 close(CF);
630 my @todo = ();
632 sub should_run_test($)
634 my $name = shift;
635 if ($#tests == -1) {
636 return 1;
638 for (my $i=0; $i <= $#tests; $i++) {
639 if ($name =~ /$tests[$i]/i) {
640 return 1;
643 return 0;
646 sub read_testlist($)
648 my ($filename) = @_;
650 my @ret = ();
651 open(IN, $filename) or die("Unable to open $filename: $!");
653 while (<IN>) {
654 if (/-- TEST(-LOADLIST|) --\n/) {
655 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
656 my $name = <IN>;
657 $name =~ s/\n//g;
658 my $env = <IN>;
659 $env =~ s/\n//g;
660 my $loadlist;
661 if ($supports_loadlist) {
662 $loadlist = <IN>;
663 $loadlist =~ s/\n//g;
665 my $cmdline = <IN>;
666 $cmdline =~ s/\n//g;
667 if (should_run_test($name) == 1) {
668 push (@ret, [$name, $env, $cmdline, $loadlist]);
670 } else {
671 print;
674 close(IN) or die("Error creating recipe from $filename");
675 return @ret;
678 if ($#testlists == -1) {
679 die("No testlists specified");
682 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
683 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
684 $ENV{TMPDIR} = "$tmpdir_abs";
685 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
686 if ($opt_socket_wrapper) {
687 $ENV{SELFTEST_INTERFACES} = $interfaces;
688 } else {
689 $ENV{SELFTEST_INTERFACES} = "";
691 if ($opt_quick) {
692 $ENV{SELFTEST_QUICK} = "1";
693 } else {
694 $ENV{SELFTEST_QUICK} = "";
696 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
698 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
699 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
701 my @available = ();
702 foreach my $fn (@testlists) {
703 foreach (read_testlist($fn)) {
704 my $name = $$_[0];
705 next if (@includes and not defined(find_in_list(\@includes, $name)));
706 push (@available, $_);
710 my $restricted = undef;
711 my $restricted_used = {};
713 if ($opt_load_list) {
714 $restricted = [];
715 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
716 while (<LOAD_LIST>) {
717 chomp;
718 push (@$restricted, $_);
720 close(LOAD_LIST);
723 my $individual_tests = undef;
724 $individual_tests = {};
726 foreach my $testsuite (@available) {
727 my $name = $$testsuite[0];
728 my $skipreason = skip($name);
729 if (defined($restricted)) {
730 # Find the testsuite for this test
731 my $match = undef;
732 foreach my $r (@$restricted) {
733 if ($r eq $name) {
734 $individual_tests->{$name} = [];
735 $match = $r;
736 $restricted_used->{$r} = 1;
737 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
738 push(@{$individual_tests->{$name}}, $r);
739 $match = $r;
740 $restricted_used->{$r} = 1;
743 if ($match) {
744 if (defined($skipreason)) {
745 if (not $opt_list) {
746 Subunit::skip_testsuite($name, $skipreason);
748 } else {
749 push(@todo, $testsuite);
752 } elsif (defined($skipreason)) {
753 if (not $opt_list) {
754 Subunit::skip_testsuite($name, $skipreason);
756 } else {
757 push(@todo, $testsuite);
761 if (defined($restricted)) {
762 foreach (@$restricted) {
763 unless (defined($restricted_used->{$_})) {
764 print "No test or testsuite found matching $_\n";
767 } elsif ($#todo == -1) {
768 print STDERR "No tests to run\n";
769 exit(1);
772 my $suitestotal = $#todo + 1;
774 unless ($opt_list) {
775 Subunit::progress($suitestotal);
776 Subunit::report_time(time());
779 my $i = 0;
780 $| = 1;
782 my %running_envs = ();
784 sub get_running_env($)
786 my ($name) = @_;
788 my $envname = $name;
790 $envname =~ s/:.*//;
792 return $running_envs{$envname};
795 my @exported_envvars = (
796 # domain stuff
797 "DOMAIN",
798 "REALM",
800 # stuff related to a trusted domain
801 "TRUST_SERVER",
802 "TRUST_SERVER_IP",
803 "TRUST_SERVER_IPV6",
804 "TRUST_NETBIOSNAME",
805 "TRUST_USERNAME",
806 "TRUST_PASSWORD",
807 "TRUST_DOMAIN",
808 "TRUST_REALM",
810 # domain controller stuff
811 "DC_SERVER",
812 "DC_SERVER_IP",
813 "DC_SERVER_IPV6",
814 "DC_NETBIOSNAME",
815 "DC_NETBIOSALIAS",
817 # domain member
818 "MEMBER_SERVER",
819 "MEMBER_SERVER_IP",
820 "MEMBER_SERVER_IPV6",
821 "MEMBER_NETBIOSNAME",
822 "MEMBER_NETBIOSALIAS",
824 # rpc proxy controller stuff
825 "RPC_PROXY_SERVER",
826 "RPC_PROXY_SERVER_IP",
827 "RPC_PROXY_SERVER_IPV6",
828 "RPC_PROXY_NETBIOSNAME",
829 "RPC_PROXY_NETBIOSALIAS",
831 # domain controller stuff for Vampired DC
832 "VAMPIRE_DC_SERVER",
833 "VAMPIRE_DC_SERVER_IP",
834 "VAMPIRE_DC_SERVER_IPV6",
835 "VAMPIRE_DC_NETBIOSNAME",
836 "VAMPIRE_DC_NETBIOSALIAS",
838 # domain controller stuff for RODC
839 "RODC_DC_SERVER",
840 "RODC_DC_SERVER_IP",
841 "RODC_DC_SERVER_IPV6",
842 "RODC_DC_NETBIOSNAME",
844 # domain controller stuff for FL 2000 Vampired DC
845 "VAMPIRE_2000_DC_SERVER",
846 "VAMPIRE_2000_DC_SERVER_IP",
847 "VAMPIRE_2000_DC_SERVER_IPV6",
848 "VAMPIRE_2000_DC_NETBIOSNAME",
849 "VAMPIRE_2000_DC_NETBIOSALIAS",
851 "PROMOTED_DC_SERVER",
852 "PROMOTED_DC_SERVER_IP",
853 "PROMOTED_DC_SERVER_IPV6",
854 "PROMOTED_DC_NETBIOSNAME",
855 "PROMOTED_DC_NETBIOSALIAS",
857 # server stuff
858 "SERVER",
859 "SERVER_IP",
860 "SERVER_IPV6",
861 "NETBIOSNAME",
862 "NETBIOSALIAS",
864 # user stuff
865 "USERNAME",
866 "USERID",
867 "PASSWORD",
868 "DC_USERNAME",
869 "DC_PASSWORD",
871 # UID/GID for rfc2307 mapping tests
872 "UID_RFC2307TEST",
873 "GID_RFC2307TEST",
875 # misc stuff
876 "KRB5_CONFIG",
877 "KRB5CCNAME",
878 "SELFTEST_WINBINDD_SOCKET_DIR",
879 "NMBD_SOCKET_DIR",
880 "LOCAL_PATH",
881 "DNS_FORWARDER1",
882 "DNS_FORWARDER2",
883 "RESOLV_CONF",
884 "UNACCEPTABLE_PASSWORD",
885 "LOCK_DIR",
886 "SMBD_TEST_LOG",
888 # nss_wrapper
889 "NSS_WRAPPER_PASSWD",
890 "NSS_WRAPPER_GROUP",
891 "NSS_WRAPPER_HOSTS",
892 "NSS_WRAPPER_HOSTNAME",
893 "NSS_WRAPPER_MODULE_SO_PATH",
894 "NSS_WRAPPER_MODULE_FN_PREFIX",
896 # resolv_wrapper
897 "RESOLV_WRAPPER_CONF",
898 "RESOLV_WRAPPER_HOSTS",
901 sub sighandler($)
903 my $signame = shift;
905 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
906 $SIG{PIPE} = 'IGNORE';
908 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
910 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
911 teardown_env($_) foreach(keys %running_envs);
912 system("pstree -p $$");
913 print "$0: PID[$$]: Exiting...\n";
914 exit(1);
917 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
919 sub setup_env($$)
921 my ($name, $prefix) = @_;
923 my $testenv_vars = undef;
925 my $envname = $name;
926 my $option = $name;
928 $envname =~ s/:.*//;
929 $option =~ s/^[^:]*//;
930 $option =~ s/^://;
932 $option = "client" if $option eq "";
934 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
936 if (defined(get_running_env($envname))) {
937 $testenv_vars = get_running_env($envname);
938 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
939 print $testenv_vars->{target}->getlog_env($testenv_vars);
940 $testenv_vars = undef;
942 } else {
943 $testenv_vars = $target->setup_env($envname, $prefix);
944 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
945 return $testenv_vars;
946 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
947 $testenv_vars->{target} = $target;
949 if (not defined($testenv_vars)) {
950 warn("$opt_target can't start up known environment '$envname'");
954 return undef unless defined($testenv_vars);
956 $running_envs{$envname} = $testenv_vars;
958 if ($option eq "local") {
959 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
960 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
961 } elsif ($option eq "client") {
962 SocketWrapper::set_default_iface(11);
963 write_clientconf($conffile, $clientdir, $testenv_vars);
964 $ENV{SMB_CONF_PATH} = $conffile;
965 } else {
966 die("Unknown option[$option] for envname[$envname]");
969 foreach (@exported_envvars) {
970 if (defined($testenv_vars->{$_})) {
971 $ENV{$_} = $testenv_vars->{$_};
972 } else {
973 delete $ENV{$_};
977 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
978 unlink($krb5_ccache_path);
979 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
980 return $testenv_vars;
983 sub exported_envvars_str($)
985 my ($testenv_vars) = @_;
986 my $out = "";
988 foreach (@exported_envvars) {
989 next unless defined($testenv_vars->{$_});
990 $out .= $_."=".$testenv_vars->{$_}."\n";
993 return $out;
996 sub getlog_env($)
998 my ($envname) = @_;
999 return "" if ($envname eq "none");
1000 my $env = get_running_env($envname);
1001 return $env->{target}->getlog_env($env);
1004 sub check_env($)
1006 my ($envname) = @_;
1007 my $env = get_running_env($envname);
1008 return $env->{target}->check_env($env);
1011 sub teardown_env($)
1013 my ($envname) = @_;
1014 return if ($envname eq "none");
1015 print STDERR "teardown_env($envname)\n";
1016 my $env = get_running_env($envname);
1017 $env->{target}->teardown_env($env);
1018 delete $running_envs{$envname};
1021 # This 'global' file needs to be empty when we start
1022 unlink("$prefix_abs/dns_host_file");
1023 unlink("$prefix_abs/hosts");
1025 if ($opt_random_order) {
1026 require List::Util;
1027 my @newtodo = List::Util::shuffle(@todo);
1028 @todo = @newtodo;
1031 if ($opt_testenv) {
1032 my $testenv_name = $ENV{SELFTEST_TESTENV};
1033 $testenv_name = $testenv_default unless defined($testenv_name);
1035 my $testenv_vars = setup_env($testenv_name, $prefix);
1037 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1038 die("Unable to setup environment $testenv_name");
1041 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1042 $ENV{ENVNAME} = $testenv_name;
1044 my $envvarstr = exported_envvars_str($testenv_vars);
1046 my @term_args = ("echo -e \"
1047 Welcome to the Samba4 Test environment '$testenv_name'
1049 This matches the client environment used in make test
1050 server is pid `cat \$PIDDIR/samba.pid`
1052 Some useful environment variables:
1053 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1054 SMB_CONF_PATH=\$SMB_CONF_PATH
1056 $envvarstr
1057 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1058 my @term = ();
1059 if ($ENV{TERMINAL}) {
1060 @term = ($ENV{TERMINAL});
1061 } else {
1062 @term = ("xterm", "-e");
1063 unshift(@term_args, ("bash", "-c"));
1066 system(@term, @term_args);
1068 teardown_env($testenv_name);
1069 } elsif ($opt_list) {
1070 foreach (@todo) {
1071 my $name = $$_[0];
1072 my $envname = $$_[1];
1073 my $cmd = $$_[2];
1074 my $listcmd = $$_[3];
1076 unless (defined($listcmd)) {
1077 warn("Unable to list tests in $name");
1078 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1079 # a single "test".
1080 print "$name\n";
1081 next;
1084 system($listcmd);
1086 if ($? == -1) {
1087 die("Unable to run $listcmd: $!");
1088 } elsif ($? & 127) {
1089 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1092 my $exitcode = $? >> 8;
1093 if ($exitcode != 0) {
1094 die("$cmd exited with exit code $exitcode");
1097 } else {
1098 foreach (@todo) {
1099 $i++;
1100 my $cmd = $$_[2];
1101 my $name = $$_[0];
1102 my $envname = $$_[1];
1103 my ($env_basename, $env_localpart) = split(/:/, $envname);
1104 my $envvars = "SKIP";
1106 if (@opt_include_env) {
1107 foreach my $env (@opt_include_env) {
1108 if ($env_basename eq $env) {
1109 $envvars = setup_env($envname, $prefix);
1112 } elsif (@opt_exclude_env) {
1113 my $excluded = 0;
1114 foreach my $env (@opt_exclude_env) {
1115 if ($env_basename eq $env) {
1116 $excluded = 1;
1119 if ($excluded == 0) {
1120 $envvars = setup_env($envname, $prefix);
1122 } else {
1123 $envvars = setup_env($envname, $prefix);
1126 if (not defined($envvars)) {
1127 Subunit::start_testsuite($name);
1128 Subunit::end_testsuite($name, "error",
1129 "unable to set up environment $envname - exiting");
1130 next;
1131 } elsif ($envvars eq "SKIP") {
1132 Subunit::start_testsuite($name);
1133 Subunit::end_testsuite($name, "skip",
1134 "environment $envname is disabled (via --exclude-env / --include-env command line options) in this test run - skipping");
1135 next;
1136 } elsif ($envvars eq "UNKNOWN") {
1137 Subunit::start_testsuite($name);
1138 Subunit::end_testsuite($name, "skip",
1139 "environment $envname is unknown in this test backend - skipping");
1140 next;
1143 # Generate a file with the individual tests to run, if the
1144 # test runner for this test suite supports it.
1145 if ($individual_tests and $individual_tests->{$name}) {
1146 if ($$_[3]) {
1147 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1148 foreach my $test (@{$individual_tests->{$name}}) {
1149 print $fh substr($test, length($name)+1) . "\n";
1151 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1152 } else {
1153 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1157 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1159 teardown_env($envname) if ($opt_resetup_env);
1163 print "\n";
1165 teardown_env($_) foreach (keys %running_envs);
1167 my $failed = 0;
1169 # if there were any valgrind failures, show them
1170 foreach (<$prefix/valgrind.log*>) {
1171 next unless (-s $_);
1172 print "VALGRIND FAILURE\n";
1173 $failed++;
1174 system("cat $_");
1176 exit 0;