pyauth: Use pytalloc_BaseObject_PyType_Ready()
[Samba.git] / selftest / selftest.pl
blob0827376527981d3fe15e33f2cbf554a175486076
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;
419 if ($opt_testenv) {
420 # 1 year should be enough :-)
421 $server_maxtime = 365 * 24 * 60 * 60;
422 } else {
423 # make test should run under 3 hours
424 $server_maxtime = 3 * 60 * 60;
427 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
428 $server_maxtime = $ENV{SMBD_MAXTIME};
431 unless ($opt_list) {
432 if ($opt_target eq "samba") {
433 $testenv_default = "ad_dc_ntvfs";
434 require target::Samba;
435 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
436 } elsif ($opt_target eq "samba3") {
437 $testenv_default = "nt4_member";
438 require target::Samba3;
439 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
443 sub read_test_regexes($)
445 my ($name) = @_;
446 my @ret = ();
447 open(LF, "<$name") or die("unable to read $name: $!");
448 while (<LF>) {
449 chomp;
450 next if (/^#/);
451 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
452 push (@ret, [$1, $4]);
453 } else {
454 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
455 push (@ret, [$_, undef]);
458 close(LF);
459 return @ret;
462 foreach (@opt_exclude) {
463 push (@excludes, read_test_regexes($_));
466 foreach (@opt_include) {
467 push (@includes, read_test_regexes($_));
470 my $interfaces = join(',', ("127.0.0.11/8",
471 "127.0.0.12/8",
472 "127.0.0.13/8",
473 "127.0.0.14/8",
474 "127.0.0.15/8",
475 "127.0.0.16/8"));
477 my $clientdir = "$prefix_abs/client";
479 my $conffile = "$clientdir/client.conf";
480 $ENV{SMB_CONF_PATH} = $conffile;
482 sub write_clientconf($$$)
484 my ($conffile, $clientdir, $vars) = @_;
486 mkdir("$clientdir", 0777) unless -d "$clientdir";
488 if ( -d "$clientdir/private" ) {
489 unlink <$clientdir/private/*>;
490 } else {
491 mkdir("$clientdir/private", 0777);
494 if ( -d "$clientdir/lockdir" ) {
495 unlink <$clientdir/lockdir/*>;
496 } else {
497 mkdir("$clientdir/lockdir", 0777);
500 if ( -d "$clientdir/statedir" ) {
501 unlink <$clientdir/statedir/*>;
502 } else {
503 mkdir("$clientdir/statedir", 0777);
506 if ( -d "$clientdir/cachedir" ) {
507 unlink <$clientdir/cachedir/*>;
508 } else {
509 mkdir("$clientdir/cachedir", 0777);
512 # this is ugly, but the ncalrpcdir needs exactly 0755
513 # otherwise tests fail.
514 my $mask = umask;
515 umask 0022;
516 if ( -d "$clientdir/ncalrpcdir/np" ) {
517 unlink <$clientdir/ncalrpcdir/np/*>;
518 rmdir "$clientdir/ncalrpcdir/np";
520 if ( -d "$clientdir/ncalrpcdir" ) {
521 unlink <$clientdir/ncalrpcdir/*>;
522 rmdir "$clientdir/ncalrpcdir";
524 mkdir("$clientdir/ncalrpcdir", 0755);
525 umask $mask;
527 open(CF, ">$conffile");
528 print CF "[global]\n";
529 print CF "\tnetbios name = client\n";
530 if (defined($vars->{DOMAIN})) {
531 print CF "\tworkgroup = $vars->{DOMAIN}\n";
533 if (defined($vars->{REALM})) {
534 print CF "\trealm = $vars->{REALM}\n";
536 if ($opt_socket_wrapper) {
537 print CF "\tinterfaces = $interfaces\n";
539 print CF "
540 private dir = $clientdir/private
541 lock dir = $clientdir/lockdir
542 state directory = $clientdir/statedir
543 cache directory = $clientdir/cachedir
544 ncalrpc dir = $clientdir/ncalrpcdir
545 panic action = $RealBin/gdb_backtrace \%d
546 max xmit = 32K
547 notify:inotify = false
548 ldb:nosync = true
549 system:anonymous = true
550 client lanman auth = Yes
551 log level = 1
552 torture:basedir = $clientdir
553 #We don't want to pass our self-tests if the PAC code is wrong
554 gensec:require_pac = true
555 #We don't want to run 'speed' tests for very long
556 torture:timelimit = 1
557 winbind separator = /
559 close(CF);
562 my @todo = ();
564 sub should_run_test($)
566 my $name = shift;
567 if ($#tests == -1) {
568 return 1;
570 for (my $i=0; $i <= $#tests; $i++) {
571 if ($name =~ /$tests[$i]/i) {
572 return 1;
575 return 0;
578 sub read_testlist($)
580 my ($filename) = @_;
582 my @ret = ();
583 open(IN, $filename) or die("Unable to open $filename: $!");
585 while (<IN>) {
586 if (/-- TEST(-LOADLIST|) --\n/) {
587 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
588 my $name = <IN>;
589 $name =~ s/\n//g;
590 my $env = <IN>;
591 $env =~ s/\n//g;
592 my $loadlist;
593 if ($supports_loadlist) {
594 $loadlist = <IN>;
595 $loadlist =~ s/\n//g;
597 my $cmdline = <IN>;
598 $cmdline =~ s/\n//g;
599 if (should_run_test($name) == 1) {
600 push (@ret, [$name, $env, $cmdline, $loadlist]);
602 } else {
603 print;
606 close(IN) or die("Error creating recipe from $filename");
607 return @ret;
610 if ($#testlists == -1) {
611 die("No testlists specified");
614 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
615 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
616 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
617 if ($opt_socket_wrapper) {
618 $ENV{SELFTEST_INTERFACES} = $interfaces;
619 } else {
620 $ENV{SELFTEST_INTERFACES} = "";
622 if ($opt_quick) {
623 $ENV{SELFTEST_QUICK} = "1";
624 } else {
625 $ENV{SELFTEST_QUICK} = "";
627 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
629 my @available = ();
630 foreach my $fn (@testlists) {
631 foreach (read_testlist($fn)) {
632 my $name = $$_[0];
633 next if (@includes and not defined(find_in_list(\@includes, $name)));
634 push (@available, $_);
638 my $restricted = undef;
639 my $restricted_used = {};
641 if ($opt_load_list) {
642 $restricted = [];
643 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
644 while (<LOAD_LIST>) {
645 chomp;
646 push (@$restricted, $_);
648 close(LOAD_LIST);
651 my $individual_tests = undef;
652 $individual_tests = {};
654 foreach my $testsuite (@available) {
655 my $name = $$testsuite[0];
656 my $skipreason = skip($name);
657 if (defined($restricted)) {
658 # Find the testsuite for this test
659 my $match = undef;
660 foreach my $r (@$restricted) {
661 if ($r eq $name) {
662 $individual_tests->{$name} = [];
663 $match = $r;
664 $restricted_used->{$r} = 1;
665 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
666 push(@{$individual_tests->{$name}}, $r);
667 $match = $r;
668 $restricted_used->{$r} = 1;
671 if ($match) {
672 if (defined($skipreason)) {
673 if (not $opt_list) {
674 Subunit::skip_testsuite($name, $skipreason);
676 } else {
677 push(@todo, $testsuite);
680 } elsif (defined($skipreason)) {
681 if (not $opt_list) {
682 Subunit::skip_testsuite($name, $skipreason);
684 } else {
685 push(@todo, $testsuite);
689 if (defined($restricted)) {
690 foreach (@$restricted) {
691 unless (defined($restricted_used->{$_})) {
692 print "No test or testsuite found matching $_\n";
695 } elsif ($#todo == -1) {
696 print STDERR "No tests to run\n";
697 exit(1);
700 my $suitestotal = $#todo + 1;
702 unless ($opt_list) {
703 Subunit::progress($suitestotal);
704 Subunit::report_time(time());
707 my $i = 0;
708 $| = 1;
710 my %running_envs = ();
712 sub get_running_env($)
714 my ($name) = @_;
716 my $envname = $name;
718 $envname =~ s/:.*//;
720 return $running_envs{$envname};
723 my @exported_envvars = (
724 # domain stuff
725 "DOMAIN",
726 "REALM",
728 # stuff related to a trusted domain
729 "TRUST_SERVER",
730 "TRUST_SERVER_IP",
731 "TRUST_SERVER_IPV6",
732 "TRUST_NETBIOSNAME",
733 "TRUST_USERNAME",
734 "TRUST_PASSWORD",
735 "TRUST_DOMAIN",
736 "TRUST_REALM",
738 # domain controller stuff
739 "DC_SERVER",
740 "DC_SERVER_IP",
741 "DC_SERVER_IPV6",
742 "DC_NETBIOSNAME",
743 "DC_NETBIOSALIAS",
745 # domain member
746 "MEMBER_SERVER",
747 "MEMBER_SERVER_IP",
748 "MEMBER_SERVER_IPV6",
749 "MEMBER_NETBIOSNAME",
750 "MEMBER_NETBIOSALIAS",
752 # rpc proxy controller stuff
753 "RPC_PROXY_SERVER",
754 "RPC_PROXY_SERVER_IP",
755 "RPC_PROXY_SERVER_IPV6",
756 "RPC_PROXY_NETBIOSNAME",
757 "RPC_PROXY_NETBIOSALIAS",
759 # domain controller stuff for Vampired DC
760 "VAMPIRE_DC_SERVER",
761 "VAMPIRE_DC_SERVER_IP",
762 "VAMPIRE_DC_SERVER_IPV6",
763 "VAMPIRE_DC_NETBIOSNAME",
764 "VAMPIRE_DC_NETBIOSALIAS",
766 "PROMOTED_DC_SERVER",
767 "PROMOTED_DC_SERVER_IP",
768 "PROMOTED_DC_SERVER_IPV6",
769 "PROMOTED_DC_NETBIOSNAME",
770 "PROMOTED_DC_NETBIOSALIAS",
772 # server stuff
773 "SERVER",
774 "SERVER_IP",
775 "SERVER_IPV6",
776 "NETBIOSNAME",
777 "NETBIOSALIAS",
779 # user stuff
780 "USERNAME",
781 "USERID",
782 "PASSWORD",
783 "DC_USERNAME",
784 "DC_PASSWORD",
786 # UID/GID for rfc2307 mapping tests
787 "UID_RFC2307TEST",
788 "GID_RFC2307TEST",
790 # misc stuff
791 "KRB5_CONFIG",
792 "SELFTEST_WINBINDD_SOCKET_DIR",
793 "WINBINDD_PRIV_PIPE_DIR",
794 "NMBD_SOCKET_DIR",
795 "LOCAL_PATH",
797 # nss_wrapper
798 "NSS_WRAPPER_PASSWD",
799 "NSS_WRAPPER_GROUP",
800 "NSS_WRAPPER_HOSTS",
801 "NSS_WRAPPER_MODULE_SO_PATH",
802 "NSS_WRAPPER_MODULE_FN_PREFIX",
804 # resolv_wrapper
805 "RESOLV_WRAPPER_CONF",
806 "RESOLV_WRAPPER_HOSTS",
809 sub sighandler($)
811 my $signame = shift;
813 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
814 $SIG{PIPE} = 'IGNORE';
816 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
818 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
819 teardown_env($_) foreach(keys %running_envs);
820 system("pstree -p $$");
821 print "$0: PID[$$]: Exiting...\n";
822 exit(1);
825 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
827 sub setup_env($$)
829 my ($name, $prefix) = @_;
831 my $testenv_vars = undef;
833 my $envname = $name;
834 my $option = $name;
836 $envname =~ s/:.*//;
837 $option =~ s/^[^:]*//;
838 $option =~ s/^://;
840 $option = "client" if $option eq "";
842 if (defined(get_running_env($envname))) {
843 $testenv_vars = get_running_env($envname);
844 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
845 print $testenv_vars->{target}->getlog_env($testenv_vars);
846 $testenv_vars = undef;
848 } else {
849 $testenv_vars = $target->setup_env($envname, $prefix);
850 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
851 return $testenv_vars;
852 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
853 $testenv_vars->{target} = $target;
855 if (not defined($testenv_vars)) {
856 warn("$opt_target can't start up known environment '$envname'");
860 return undef unless defined($testenv_vars);
862 $running_envs{$envname} = $testenv_vars;
864 if ($option eq "local") {
865 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
866 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
867 } elsif ($option eq "client") {
868 SocketWrapper::set_default_iface(11);
869 write_clientconf($conffile, $clientdir, $testenv_vars);
870 $ENV{SMB_CONF_PATH} = $conffile;
871 } else {
872 die("Unknown option[$option] for envname[$envname]");
875 foreach (@exported_envvars) {
876 if (defined($testenv_vars->{$_})) {
877 $ENV{$_} = $testenv_vars->{$_};
878 } else {
879 delete $ENV{$_};
883 return $testenv_vars;
886 sub exported_envvars_str($)
888 my ($testenv_vars) = @_;
889 my $out = "";
891 foreach (@exported_envvars) {
892 next unless defined($testenv_vars->{$_});
893 $out .= $_."=".$testenv_vars->{$_}."\n";
896 return $out;
899 sub getlog_env($)
901 my ($envname) = @_;
902 return "" if ($envname eq "none");
903 my $env = get_running_env($envname);
904 return $env->{target}->getlog_env($env);
907 sub check_env($)
909 my ($envname) = @_;
910 my $env = get_running_env($envname);
911 return $env->{target}->check_env($env);
914 sub teardown_env($)
916 my ($envname) = @_;
917 return if ($envname eq "none");
918 print STDERR "teardown_env($envname)\n";
919 my $env = get_running_env($envname);
920 $env->{target}->teardown_env($env);
921 delete $running_envs{$envname};
924 # This 'global' file needs to be empty when we start
925 unlink("$prefix_abs/dns_host_file");
926 unlink("$prefix_abs/hosts");
928 if ($opt_random_order) {
929 require List::Util;
930 my @newtodo = List::Util::shuffle(@todo);
931 @todo = @newtodo;
934 if ($opt_testenv) {
935 my $testenv_name = $ENV{SELFTEST_TESTENV};
936 $testenv_name = $testenv_default unless defined($testenv_name);
938 my $testenv_vars = setup_env($testenv_name, $prefix);
940 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
941 die("Unable to setup environment $testenv_name");
944 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
945 $ENV{ENVNAME} = $testenv_name;
947 my $envvarstr = exported_envvars_str($testenv_vars);
949 my @term_args = ("echo -e \"
950 Welcome to the Samba4 Test environment '$testenv_name'
952 This matches the client environment used in make test
953 server is pid `cat \$PIDDIR/samba.pid`
955 Some useful environment variables:
956 TORTURE_OPTIONS=\$TORTURE_OPTIONS
957 SMB_CONF_PATH=\$SMB_CONF_PATH
959 $envvarstr
960 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
961 my @term = ();
962 if ($ENV{TERMINAL}) {
963 @term = ($ENV{TERMINAL});
964 } else {
965 @term = ("xterm", "-e");
966 unshift(@term_args, ("bash", "-c"));
969 system(@term, @term_args);
971 teardown_env($testenv_name);
972 } elsif ($opt_list) {
973 foreach (@todo) {
974 my $name = $$_[0];
975 my $envname = $$_[1];
976 my $cmd = $$_[2];
977 my $listcmd = $$_[3];
979 unless (defined($listcmd)) {
980 warn("Unable to list tests in $name");
981 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
982 # a single "test".
983 print "$name\n";
984 next;
987 system($listcmd);
989 if ($? == -1) {
990 die("Unable to run $listcmd: $!");
991 } elsif ($? & 127) {
992 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
995 my $exitcode = $? >> 8;
996 if ($exitcode != 0) {
997 die("$cmd exited with exit code $exitcode");
1000 } else {
1001 foreach (@todo) {
1002 $i++;
1003 my $cmd = $$_[2];
1004 my $name = $$_[0];
1005 my $envname = $$_[1];
1007 my $envvars = setup_env($envname, $prefix);
1008 if (not defined($envvars)) {
1009 Subunit::start_testsuite($name);
1010 Subunit::end_testsuite($name, "error",
1011 "unable to set up environment $envname - exiting");
1012 next;
1013 } elsif ($envvars eq "UNKNOWN") {
1014 Subunit::start_testsuite($name);
1015 Subunit::end_testsuite($name, "skip",
1016 "environment $envname is unknown in this test backend - skipping");
1017 next;
1020 # Generate a file with the individual tests to run, if the
1021 # test runner for this test suite supports it.
1022 if ($individual_tests and $individual_tests->{$name}) {
1023 if ($$_[3]) {
1024 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1025 foreach my $test (@{$individual_tests->{$name}}) {
1026 print $fh substr($test, length($name)+1) . "\n";
1028 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1029 } else {
1030 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1034 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1036 teardown_env($envname) if ($opt_resetup_env);
1040 print "\n";
1042 teardown_env($_) foreach (keys %running_envs);
1044 my $failed = 0;
1046 # if there were any valgrind failures, show them
1047 foreach (<$prefix/valgrind.log*>) {
1048 next unless (-s $_);
1049 print "VALGRIND FAILURE\n";
1050 $failed++;
1051 system("cat $_");
1053 exit 0;