KCC: docstring for kcc.graph.kruskal()
[Samba.git] / selftest / selftest.pl
blobbbd81d635bb2a9af2db36f0be2e35998851850c0
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;
31 eval {
32 require Time::HiRes;
33 Time::HiRes->import("time");
35 if ($@) {
36 print "You don't have Time::Hires installed !\n";
39 my $opt_help = 0;
40 my $opt_target = "samba";
41 my $opt_quick = 0;
42 my $opt_socket_wrapper = 0;
43 my $opt_socket_wrapper_pcap = undef;
44 my $opt_socket_wrapper_keep_pcap = undef;
45 my $opt_random_order = 0;
46 my $opt_one = 0;
47 my @opt_exclude = ();
48 my @opt_include = ();
49 my $opt_testenv = 0;
50 my $opt_list = 0;
51 my $ldap = undef;
52 my $opt_resetup_env = undef;
53 my $opt_load_list = undef;
54 my $opt_libnss_wrapper_so_path = "";
55 my $opt_libresolv_wrapper_so_path = "";
56 my $opt_libsocket_wrapper_so_path = "";
57 my $opt_libuid_wrapper_so_path = "";
58 my $opt_use_dns_faking = 0;
59 my @testlists = ();
61 my $srcdir = ".";
62 my $bindir = "./bin";
63 my $prefix = "./st";
65 my @includes = ();
66 my @excludes = ();
68 sub find_in_list($$)
70 my ($list, $fullname) = @_;
72 foreach (@$list) {
73 if ($fullname =~ /$$_[0]/) {
74 return ($$_[1]) if ($$_[1]);
75 return "";
79 return undef;
82 sub skip($)
84 my ($name) = @_;
86 return find_in_list(\@excludes, $name);
89 sub getlog_env($);
91 sub setup_pcap($)
93 my ($name) = @_;
95 return unless ($opt_socket_wrapper_pcap);
96 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
98 my $fname = $name;
99 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
101 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
103 SocketWrapper::setup_pcap($pcap_file);
105 return $pcap_file;
108 sub cleanup_pcap($$)
110 my ($pcap_file, $exitcode) = @_;
112 return unless ($opt_socket_wrapper_pcap);
113 return if ($opt_socket_wrapper_keep_pcap);
114 return unless ($exitcode == 0);
115 return unless defined($pcap_file);
117 unlink($pcap_file);
120 # expand strings from %ENV
121 sub expand_environment_strings($)
123 my $s = shift;
124 # we use a reverse sort so we do the longer ones first
125 foreach my $k (sort { $b cmp $a } keys %ENV) {
126 $s =~ s/\$$k/$ENV{$k}/g;
128 return $s;
131 sub run_testsuite($$$$$)
133 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
134 my $pcap_file = setup_pcap($name);
136 Subunit::start_testsuite($name);
137 Subunit::progress_push();
138 Subunit::report_time(time());
139 system($cmd);
140 Subunit::report_time(time());
141 Subunit::progress_pop();
143 if ($? == -1) {
144 Subunit::progress_pop();
145 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
146 exit(1);
147 } elsif ($? & 127) {
148 Subunit::end_testsuite($name, "error",
149 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
150 exit(1);
153 my $exitcode = $? >> 8;
155 my $envlog = getlog_env($envname);
156 if ($envlog ne "") {
157 print "envlog: $envlog\n";
160 print "command: $cmd\n";
161 printf "expanded command: %s\n", expand_environment_strings($cmd);
163 if ($exitcode == 0) {
164 Subunit::end_testsuite($name, "success");
165 } else {
166 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
169 cleanup_pcap($pcap_file, $exitcode);
171 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
172 print "PCAP FILE: $pcap_file\n";
175 if ($exitcode != 0) {
176 exit(1) if ($opt_one);
179 return $exitcode;
182 sub ShowHelp()
184 print "Samba test runner
185 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
186 Copyright (C) Stefan Metzmacher <metze\@samba.org>
188 Usage: $Script [OPTIONS] TESTNAME-REGEX
190 Generic options:
191 --help this help page
192 --target=samba[3]|win Samba version to target
193 --testlist=FILE file to read available tests from
194 --exclude=FILE Exclude tests listed in the file
195 --include=FILE Include tests listed in the file
197 Paths:
198 --prefix=DIR prefix to run tests in [st]
199 --srcdir=DIR source directory [.]
200 --bindir=DIR binaries directory [./bin]
202 Preload cwrap:
203 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
204 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
205 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
206 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
208 DNS:
209 --use-dns-faking Fake DNS entries rather than talking to our
210 DNS implementation.
212 Target Specific:
213 --socket-wrapper-pcap save traffic to pcap directories
214 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
215 failed
216 --socket-wrapper enable socket wrapper
218 Samba4 Specific:
219 --ldap=openldap|fedora-ds back samba onto specified ldap server
221 Behaviour:
222 --quick run quick overall test
223 --one abort when the first test fails
224 --testenv run a shell in the requested test environment
225 --list list available tests
227 exit(0);
230 my $result = GetOptions (
231 'help|h|?' => \$opt_help,
232 'target=s' => \$opt_target,
233 'prefix=s' => \$prefix,
234 'socket-wrapper' => \$opt_socket_wrapper,
235 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
236 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
237 'quick' => \$opt_quick,
238 'one' => \$opt_one,
239 'exclude=s' => \@opt_exclude,
240 'include=s' => \@opt_include,
241 'srcdir=s' => \$srcdir,
242 'bindir=s' => \$bindir,
243 'testenv' => \$opt_testenv,
244 'list' => \$opt_list,
245 'ldap:s' => \$ldap,
246 'resetup-environment' => \$opt_resetup_env,
247 'testlist=s' => \@testlists,
248 'random-order' => \$opt_random_order,
249 'load-list=s' => \$opt_load_list,
250 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
251 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
252 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
253 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
254 'use-dns-faking' => \$opt_use_dns_faking
257 exit(1) if (not $result);
259 ShowHelp() if ($opt_help);
261 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
263 # we want unbuffered output
264 $| = 1;
266 my @tests = @ARGV;
268 # quick hack to disable rpc validation when using valgrind - its way too slow
269 unless (defined($ENV{VALGRIND})) {
270 $ENV{VALIDATE} = "validate";
271 $ENV{MALLOC_CHECK_} = 3;
274 # make all our python scripts unbuffered
275 $ENV{PYTHONUNBUFFERED} = 1;
277 my $bindir_abs = abs_path($bindir);
279 # Backwards compatibility:
280 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
281 if (defined($ENV{FEDORA_DS_ROOT})) {
282 $ldap = "fedora-ds";
283 } else {
284 $ldap = "openldap";
288 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
289 if ($ldap) {
290 # LDAP is slow
291 $torture_maxtime *= 2;
294 $prefix =~ s+//+/+;
295 $prefix =~ s+/./+/+;
296 $prefix =~ s+/$++;
298 die("using an empty prefix isn't allowed") unless $prefix ne "";
300 # Ensure we have the test prefix around.
302 # We need restrictive
303 # permissions on this as some subdirectories in this tree will have
304 # wider permissions (ie 0777) and this would allow other users on the
305 # host to subvert the test process.
306 mkdir($prefix, 0700) unless -d $prefix;
307 chmod 0700, $prefix;
309 my $prefix_abs = abs_path($prefix);
310 my $tmpdir_abs = abs_path("$prefix/tmp");
311 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
313 my $srcdir_abs = abs_path($srcdir);
315 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
316 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
318 $ENV{PREFIX} = $prefix;
319 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
320 $ENV{PREFIX_ABS} = $prefix_abs;
321 $ENV{SRCDIR} = $srcdir;
322 $ENV{SRCDIR_ABS} = $srcdir_abs;
323 $ENV{BINDIR} = $bindir_abs;
325 my $tls_enabled = not $opt_quick;
326 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
328 sub prefix_pathvar($$)
330 my ($name, $newpath) = @_;
331 if (defined($ENV{$name})) {
332 $ENV{$name} = "$newpath:$ENV{$name}";
333 } else {
334 $ENV{$name} = $newpath;
337 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
338 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
340 if ($opt_socket_wrapper_keep_pcap) {
341 # Socket wrapper keep pcap implies socket wrapper pcap
342 $opt_socket_wrapper_pcap = 1;
345 if ($opt_socket_wrapper_pcap) {
346 # Socket wrapper pcap implies socket wrapper
347 $opt_socket_wrapper = 1;
350 my $ld_preload = $ENV{LD_PRELOAD};
352 if ($opt_libnss_wrapper_so_path) {
353 if ($ld_preload) {
354 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
355 } else {
356 $ld_preload = "$opt_libnss_wrapper_so_path";
360 if ($opt_libresolv_wrapper_so_path) {
361 if ($ld_preload) {
362 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
363 } else {
364 $ld_preload = "$opt_libresolv_wrapper_so_path";
368 if ($opt_libsocket_wrapper_so_path) {
369 if ($ld_preload) {
370 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
371 } else {
372 $ld_preload = "$opt_libsocket_wrapper_so_path";
376 if ($opt_libuid_wrapper_so_path) {
377 if ($ld_preload) {
378 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
379 } else {
380 $ld_preload = "$opt_libuid_wrapper_so_path";
384 $ENV{LD_PRELOAD} = $ld_preload;
385 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
387 # Enable uid_wrapper globally
388 $ENV{UID_WRAPPER} = 1;
390 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
392 # This is needed in order to allow the ldb_*ldap module
393 # to work with a preloaded socket wrapper.
394 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
396 my $socket_wrapper_dir;
397 if ($opt_socket_wrapper) {
398 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
399 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
400 } elsif (not $opt_list) {
401 unless ($< == 0) {
402 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
406 if ($opt_use_dns_faking) {
407 print "DNS: Faking nameserver\n";
408 $ENV{SAMBA_DNS_FAKING} = 1;
411 my $target;
412 my $testenv_default = "none";
414 # After this many seconds, the server will self-terminate. All tests
415 # must terminate in this time, and testenv will only stay alive this
416 # long
418 my $server_maxtime = 9000;
419 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
420 $server_maxtime = $ENV{SMBD_MAXTIME};
423 unless ($opt_list) {
424 if ($opt_target eq "samba") {
425 $testenv_default = "ad_dc_ntvfs";
426 require target::Samba;
427 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
428 } elsif ($opt_target eq "samba3") {
429 $testenv_default = "nt4_member";
430 require target::Samba3;
431 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
435 sub read_test_regexes($)
437 my ($name) = @_;
438 my @ret = ();
439 open(LF, "<$name") or die("unable to read $name: $!");
440 while (<LF>) {
441 chomp;
442 next if (/^#/);
443 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
444 push (@ret, [$1, $4]);
445 } else {
446 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
447 push (@ret, [$_, undef]);
450 close(LF);
451 return @ret;
454 foreach (@opt_exclude) {
455 push (@excludes, read_test_regexes($_));
458 foreach (@opt_include) {
459 push (@includes, read_test_regexes($_));
462 my $interfaces = join(',', ("127.0.0.11/8",
463 "127.0.0.12/8",
464 "127.0.0.13/8",
465 "127.0.0.14/8",
466 "127.0.0.15/8",
467 "127.0.0.16/8"));
469 my $clientdir = "$prefix_abs/client";
471 my $conffile = "$clientdir/client.conf";
472 $ENV{SMB_CONF_PATH} = $conffile;
474 sub write_clientconf($$$)
476 my ($conffile, $clientdir, $vars) = @_;
478 mkdir("$clientdir", 0777) unless -d "$clientdir";
480 if ( -d "$clientdir/private" ) {
481 unlink <$clientdir/private/*>;
482 } else {
483 mkdir("$clientdir/private", 0777);
486 if ( -d "$clientdir/lockdir" ) {
487 unlink <$clientdir/lockdir/*>;
488 } else {
489 mkdir("$clientdir/lockdir", 0777);
492 if ( -d "$clientdir/statedir" ) {
493 unlink <$clientdir/statedir/*>;
494 } else {
495 mkdir("$clientdir/statedir", 0777);
498 if ( -d "$clientdir/cachedir" ) {
499 unlink <$clientdir/cachedir/*>;
500 } else {
501 mkdir("$clientdir/cachedir", 0777);
504 # this is ugly, but the ncalrpcdir needs exactly 0755
505 # otherwise tests fail.
506 my $mask = umask;
507 umask 0022;
508 if ( -d "$clientdir/ncalrpcdir/np" ) {
509 unlink <$clientdir/ncalrpcdir/np/*>;
510 rmdir "$clientdir/ncalrpcdir/np";
512 if ( -d "$clientdir/ncalrpcdir" ) {
513 unlink <$clientdir/ncalrpcdir/*>;
514 rmdir "$clientdir/ncalrpcdir";
516 mkdir("$clientdir/ncalrpcdir", 0755);
517 umask $mask;
519 open(CF, ">$conffile");
520 print CF "[global]\n";
521 print CF "\tnetbios name = client\n";
522 if (defined($vars->{DOMAIN})) {
523 print CF "\tworkgroup = $vars->{DOMAIN}\n";
525 if (defined($vars->{REALM})) {
526 print CF "\trealm = $vars->{REALM}\n";
528 if ($opt_socket_wrapper) {
529 print CF "\tinterfaces = $interfaces\n";
531 print CF "
532 private dir = $clientdir/private
533 lock dir = $clientdir/lockdir
534 state directory = $clientdir/statedir
535 cache directory = $clientdir/cachedir
536 ncalrpc dir = $clientdir/ncalrpcdir
537 panic action = $RealBin/gdb_backtrace \%d
538 max xmit = 32K
539 notify:inotify = false
540 ldb:nosync = true
541 system:anonymous = true
542 client lanman auth = Yes
543 log level = 1
544 torture:basedir = $clientdir
545 #We don't want to pass our self-tests if the PAC code is wrong
546 gensec:require_pac = true
547 #We don't want to run 'speed' tests for very long
548 torture:timelimit = 1
549 winbind separator = /
551 close(CF);
554 my @todo = ();
556 sub should_run_test($)
558 my $name = shift;
559 if ($#tests == -1) {
560 return 1;
562 for (my $i=0; $i <= $#tests; $i++) {
563 if ($name =~ /$tests[$i]/i) {
564 return 1;
567 return 0;
570 sub read_testlist($)
572 my ($filename) = @_;
574 my @ret = ();
575 open(IN, $filename) or die("Unable to open $filename: $!");
577 while (<IN>) {
578 if (/-- TEST(-LOADLIST|) --\n/) {
579 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
580 my $name = <IN>;
581 $name =~ s/\n//g;
582 my $env = <IN>;
583 $env =~ s/\n//g;
584 my $loadlist;
585 if ($supports_loadlist) {
586 $loadlist = <IN>;
587 $loadlist =~ s/\n//g;
589 my $cmdline = <IN>;
590 $cmdline =~ s/\n//g;
591 if (should_run_test($name) == 1) {
592 push (@ret, [$name, $env, $cmdline, $loadlist]);
594 } else {
595 print;
598 close(IN) or die("Error creating recipe from $filename");
599 return @ret;
602 if ($#testlists == -1) {
603 die("No testlists specified");
606 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
607 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
608 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
609 if ($opt_socket_wrapper) {
610 $ENV{SELFTEST_INTERFACES} = $interfaces;
611 } else {
612 $ENV{SELFTEST_INTERFACES} = "";
614 if ($opt_quick) {
615 $ENV{SELFTEST_QUICK} = "1";
616 } else {
617 $ENV{SELFTEST_QUICK} = "";
619 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
621 my @available = ();
622 foreach my $fn (@testlists) {
623 foreach (read_testlist($fn)) {
624 my $name = $$_[0];
625 next if (@includes and not defined(find_in_list(\@includes, $name)));
626 push (@available, $_);
630 my $restricted = undef;
631 my $restricted_used = {};
633 if ($opt_load_list) {
634 $restricted = [];
635 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
636 while (<LOAD_LIST>) {
637 chomp;
638 push (@$restricted, $_);
640 close(LOAD_LIST);
643 my $individual_tests = undef;
644 $individual_tests = {};
646 foreach my $testsuite (@available) {
647 my $name = $$testsuite[0];
648 my $skipreason = skip($name);
649 if (defined($restricted)) {
650 # Find the testsuite for this test
651 my $match = undef;
652 foreach my $r (@$restricted) {
653 if ($r eq $name) {
654 $individual_tests->{$name} = [];
655 $match = $r;
656 $restricted_used->{$r} = 1;
657 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
658 push(@{$individual_tests->{$name}}, $r);
659 $match = $r;
660 $restricted_used->{$r} = 1;
663 if ($match) {
664 if (defined($skipreason)) {
665 if (not $opt_list) {
666 Subunit::skip_testsuite($name, $skipreason);
668 } else {
669 push(@todo, $testsuite);
672 } elsif (defined($skipreason)) {
673 if (not $opt_list) {
674 Subunit::skip_testsuite($name, $skipreason);
676 } else {
677 push(@todo, $testsuite);
681 if (defined($restricted)) {
682 foreach (@$restricted) {
683 unless (defined($restricted_used->{$_})) {
684 print "No test or testsuite found matching $_\n";
687 } elsif ($#todo == -1) {
688 print STDERR "No tests to run\n";
689 exit(1);
692 my $suitestotal = $#todo + 1;
694 unless ($opt_list) {
695 Subunit::progress($suitestotal);
696 Subunit::report_time(time());
699 my $i = 0;
700 $| = 1;
702 my %running_envs = ();
704 sub get_running_env($)
706 my ($name) = @_;
708 my $envname = $name;
710 $envname =~ s/:.*//;
712 return $running_envs{$envname};
715 my @exported_envvars = (
716 # domain stuff
717 "DOMAIN",
718 "REALM",
720 # domain controller stuff
721 "DC_SERVER",
722 "DC_SERVER_IP",
723 "DC_SERVER_IPV6",
724 "DC_NETBIOSNAME",
725 "DC_NETBIOSALIAS",
727 # domain member
728 "MEMBER_SERVER",
729 "MEMBER_SERVER_IP",
730 "MEMBER_SERVER_IPV6",
731 "MEMBER_NETBIOSNAME",
732 "MEMBER_NETBIOSALIAS",
734 # rpc proxy controller stuff
735 "RPC_PROXY_SERVER",
736 "RPC_PROXY_SERVER_IP",
737 "RPC_PROXY_SERVER_IPV6",
738 "RPC_PROXY_NETBIOSNAME",
739 "RPC_PROXY_NETBIOSALIAS",
741 # domain controller stuff for Vampired DC
742 "VAMPIRE_DC_SERVER",
743 "VAMPIRE_DC_SERVER_IP",
744 "VAMPIRE_DC_SERVER_IPV6",
745 "VAMPIRE_DC_NETBIOSNAME",
746 "VAMPIRE_DC_NETBIOSALIAS",
748 "PROMOTED_DC_SERVER",
749 "PROMOTED_DC_SERVER_IP",
750 "PROMOTED_DC_SERVER_IPV6",
751 "PROMOTED_DC_NETBIOSNAME",
752 "PROMOTED_DC_NETBIOSALIAS",
754 # server stuff
755 "SERVER",
756 "SERVER_IP",
757 "SERVER_IPV6",
758 "NETBIOSNAME",
759 "NETBIOSALIAS",
761 # user stuff
762 "USERNAME",
763 "USERID",
764 "PASSWORD",
765 "DC_USERNAME",
766 "DC_PASSWORD",
768 # UID/GID for rfc2307 mapping tests
769 "UID_RFC2307TEST",
770 "GID_RFC2307TEST",
772 # misc stuff
773 "KRB5_CONFIG",
774 "SELFTEST_WINBINDD_SOCKET_DIR",
775 "WINBINDD_PRIV_PIPE_DIR",
776 "NMBD_SOCKET_DIR",
777 "LOCAL_PATH",
779 # nss_wrapper
780 "NSS_WRAPPER_PASSWD",
781 "NSS_WRAPPER_GROUP",
782 "NSS_WRAPPER_HOSTS",
783 "NSS_WRAPPER_MODULE_SO_PATH",
784 "NSS_WRAPPER_MODULE_FN_PREFIX",
786 # resolv_wrapper
787 "RESOLV_WRAPPER_CONF",
788 "RESOLV_WRAPPER_HOSTS",
791 sub sighandler($)
793 my $signame = shift;
795 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
796 $SIG{PIPE} = 'IGNORE';
798 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
800 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
801 teardown_env($_) foreach(keys %running_envs);
802 system("pstree -p $$");
803 print "$0: PID[$$]: Exiting...\n";
804 exit(1);
807 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
809 sub setup_env($$)
811 my ($name, $prefix) = @_;
813 my $testenv_vars = undef;
815 my $envname = $name;
816 my $option = $name;
818 $envname =~ s/:.*//;
819 $option =~ s/^[^:]*//;
820 $option =~ s/^://;
822 $option = "client" if $option eq "";
824 if ($envname eq "none") {
825 $testenv_vars = {};
826 } elsif (defined(get_running_env($envname))) {
827 $testenv_vars = get_running_env($envname);
828 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
829 print $testenv_vars->{target}->getlog_env($testenv_vars);
830 $testenv_vars = undef;
832 } else {
833 $testenv_vars = $target->setup_env($envname, $prefix);
834 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
835 return $testenv_vars;
836 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
837 $testenv_vars->{target} = $target;
839 if (not defined($testenv_vars)) {
840 warn("$opt_target can't start up known environment '$envname'");
844 return undef unless defined($testenv_vars);
846 $running_envs{$envname} = $testenv_vars;
848 if ($option eq "local") {
849 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
850 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
851 } elsif ($option eq "client") {
852 SocketWrapper::set_default_iface(11);
853 write_clientconf($conffile, $clientdir, $testenv_vars);
854 $ENV{SMB_CONF_PATH} = $conffile;
855 } else {
856 die("Unknown option[$option] for envname[$envname]");
859 foreach (@exported_envvars) {
860 if (defined($testenv_vars->{$_})) {
861 $ENV{$_} = $testenv_vars->{$_};
862 } else {
863 delete $ENV{$_};
867 return $testenv_vars;
870 sub exported_envvars_str($)
872 my ($testenv_vars) = @_;
873 my $out = "";
875 foreach (@exported_envvars) {
876 next unless defined($testenv_vars->{$_});
877 $out .= $_."=".$testenv_vars->{$_}."\n";
880 return $out;
883 sub getlog_env($)
885 my ($envname) = @_;
886 return "" if ($envname eq "none");
887 my $env = get_running_env($envname);
888 return $env->{target}->getlog_env($env);
891 sub check_env($)
893 my ($envname) = @_;
894 return 1 if ($envname eq "none");
895 my $env = get_running_env($envname);
896 return $env->{target}->check_env($env);
899 sub teardown_env($)
901 my ($envname) = @_;
902 return if ($envname eq "none");
903 print STDERR "teardown_env($envname)\n";
904 my $env = get_running_env($envname);
905 $env->{target}->teardown_env($env);
906 delete $running_envs{$envname};
909 # This 'global' file needs to be empty when we start
910 unlink("$prefix_abs/dns_host_file");
911 unlink("$prefix_abs/hosts");
913 if ($opt_random_order) {
914 require List::Util;
915 my @newtodo = List::Util::shuffle(@todo);
916 @todo = @newtodo;
919 if ($opt_testenv) {
920 my $testenv_name = $ENV{SELFTEST_TESTENV};
921 $testenv_name = $testenv_default unless defined($testenv_name);
923 my $testenv_vars = setup_env($testenv_name, $prefix);
925 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
926 die("Unable to setup environment $testenv_name");
929 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
930 $ENV{ENVNAME} = $testenv_name;
932 my $envvarstr = exported_envvars_str($testenv_vars);
934 my @term_args = ("echo -e \"
935 Welcome to the Samba4 Test environment '$testenv_name'
937 This matches the client environment used in make test
938 server is pid `cat \$PIDDIR/samba.pid`
940 Some useful environment variables:
941 TORTURE_OPTIONS=\$TORTURE_OPTIONS
942 SMB_CONF_PATH=\$SMB_CONF_PATH
944 $envvarstr
945 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
946 my @term = ();
947 if ($ENV{TERMINAL}) {
948 @term = ($ENV{TERMINAL});
949 } else {
950 @term = ("xterm", "-e");
951 unshift(@term_args, ("bash", "-c"));
954 system(@term, @term_args);
956 teardown_env($testenv_name);
957 } elsif ($opt_list) {
958 foreach (@todo) {
959 my $name = $$_[0];
960 my $envname = $$_[1];
961 my $cmd = $$_[2];
962 my $listcmd = $$_[3];
964 unless (defined($listcmd)) {
965 warn("Unable to list tests in $name");
966 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
967 # a single "test".
968 print "$name\n";
969 next;
972 system($listcmd);
974 if ($? == -1) {
975 die("Unable to run $listcmd: $!");
976 } elsif ($? & 127) {
977 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
980 my $exitcode = $? >> 8;
981 if ($exitcode != 0) {
982 die("$cmd exited with exit code $exitcode");
985 } else {
986 foreach (@todo) {
987 $i++;
988 my $cmd = $$_[2];
989 my $name = $$_[0];
990 my $envname = $$_[1];
992 my $envvars = setup_env($envname, $prefix);
993 if (not defined($envvars)) {
994 Subunit::start_testsuite($name);
995 Subunit::end_testsuite($name, "error",
996 "unable to set up environment $envname - exiting");
997 next;
998 } elsif ($envvars eq "UNKNOWN") {
999 Subunit::start_testsuite($name);
1000 Subunit::end_testsuite($name, "skip",
1001 "environment $envname is unknown in this test backend - skipping");
1002 next;
1005 # Generate a file with the individual tests to run, if the
1006 # test runner for this test suite supports it.
1007 if ($individual_tests and $individual_tests->{$name}) {
1008 if ($$_[3]) {
1009 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1010 foreach my $test (@{$individual_tests->{$name}}) {
1011 print $fh substr($test, length($name)+1) . "\n";
1013 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1014 } else {
1015 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1019 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1021 teardown_env($envname) if ($opt_resetup_env);
1025 print "\n";
1027 teardown_env($_) foreach (keys %running_envs);
1029 my $failed = 0;
1031 # if there were any valgrind failures, show them
1032 foreach (<$prefix/valgrind.log*>) {
1033 next unless (-s $_);
1034 print "VALGRIND FAILURE\n";
1035 $failed++;
1036 system("cat $_");
1038 exit 0;