s3/py_passdb: initialize optional parameters earlier
[Samba.git] / selftest / selftest.pl
blob995abd71a04f17c4eff9db97248acb3d1e4f0b1d
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_use_dns_faking = 0;
64 my @testlists = ();
66 my $srcdir = ".";
67 my $bindir = "./bin";
68 my $prefix = "./st";
70 my @includes = ();
71 my @excludes = ();
73 sub find_in_list($$)
75 my ($list, $fullname) = @_;
77 foreach (@$list) {
78 if ($fullname =~ /$$_[0]/) {
79 return ($$_[1]) if ($$_[1]);
80 return "";
84 return undef;
87 sub skip
89 my ($name, $envname) = @_;
90 my ($env_basename, $env_localpart) = split(/:/, $envname);
92 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
93 return "environment $envname is disabled as this build does not include an AD DC";
96 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
97 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
98 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
99 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
102 return find_in_list(\@excludes, $name);
105 sub getlog_env($);
107 sub setup_pcap($)
109 my ($name) = @_;
111 return unless ($opt_socket_wrapper_pcap);
112 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
114 my $fname = $name;
115 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
117 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
119 SocketWrapper::setup_pcap($pcap_file);
121 return $pcap_file;
124 sub cleanup_pcap($$)
126 my ($pcap_file, $exitcode) = @_;
128 return unless ($opt_socket_wrapper_pcap);
129 return if ($opt_socket_wrapper_keep_pcap);
130 return unless ($exitcode == 0);
131 return unless defined($pcap_file);
133 unlink($pcap_file);
136 # expand strings from %ENV
137 sub expand_environment_strings($)
139 my $s = shift;
140 # we use a reverse sort so we do the longer ones first
141 foreach my $k (sort { $b cmp $a } keys %ENV) {
142 $s =~ s/\$$k/$ENV{$k}/g;
144 return $s;
147 sub run_testsuite($$$$$)
149 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
150 my $pcap_file = setup_pcap($name);
152 Subunit::start_testsuite($name);
153 Subunit::progress_push();
154 Subunit::report_time();
155 system($cmd);
156 Subunit::report_time();
157 Subunit::progress_pop();
159 if ($? == -1) {
160 print "command: $cmd\n";
161 printf "expanded command: %s\n", expand_environment_strings($cmd);
162 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
163 exit(1);
164 } elsif ($? & 127) {
165 print "command: $cmd\n";
166 printf "expanded command: %s\n", expand_environment_strings($cmd);
167 Subunit::end_testsuite($name, "error",
168 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
169 exit(1);
172 my $exitcode = $? >> 8;
174 my $envlog = getlog_env($envname);
175 if ($envlog ne "") {
176 print "envlog: $envlog\n";
179 print "command: $cmd\n";
180 printf "expanded command: %s\n", expand_environment_strings($cmd);
182 if ($exitcode == 0) {
183 Subunit::end_testsuite($name, "success");
184 } else {
185 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
188 cleanup_pcap($pcap_file, $exitcode);
190 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
191 print "PCAP FILE: $pcap_file\n";
194 if ($exitcode != 0) {
195 exit(1) if ($opt_one);
198 return $exitcode;
201 sub ShowHelp()
203 print "Samba test runner
204 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
205 Copyright (C) Stefan Metzmacher <metze\@samba.org>
207 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
209 Generic options:
210 --help this help page
211 --target=samba[3]|win Samba version to target
212 --testlist=FILE file to read available tests from
213 --exclude=FILE Exclude tests listed in the file
214 --include=FILE Include tests listed in the file
215 --exclude-env=ENV Exclude tests for the specified environment
216 --include-env=ENV Include tests for the specified environment
218 Paths:
219 --prefix=DIR prefix to run tests in [st]
220 --srcdir=DIR source directory [.]
221 --bindir=DIR binaries directory [./bin]
223 Preload cwrap:
224 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
225 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
226 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
227 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
229 DNS:
230 --use-dns-faking Fake DNS entries rather than talking to our
231 DNS implementation.
233 Target Specific:
234 --socket-wrapper-pcap save traffic to pcap directories
235 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
236 failed
237 --socket-wrapper enable socket wrapper
239 Samba4 Specific:
240 --ldap=openldap|fedora-ds back samba onto specified ldap server
242 Behaviour:
243 --quick run quick overall test
244 --one abort when the first test fails
245 --testenv run a shell in the requested test environment
246 --list list available tests
248 exit(0);
251 my $result = GetOptions (
252 'help|h|?' => \$opt_help,
253 'target=s' => \$opt_target,
254 'prefix=s' => \$prefix,
255 'socket-wrapper' => \$opt_socket_wrapper,
256 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
257 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
258 'quick' => \$opt_quick,
259 'one' => \$opt_one,
260 'exclude=s' => \@opt_exclude,
261 'include=s' => \@opt_include,
262 'exclude-env=s' => \@opt_exclude_env,
263 'include-env=s' => \@opt_include_env,
264 'srcdir=s' => \$srcdir,
265 'bindir=s' => \$bindir,
266 'testenv' => \$opt_testenv,
267 'list' => \$opt_list,
268 'mitkrb5' => \$opt_mitkrb5,
269 'ldap:s' => \$ldap,
270 'resetup-environment' => \$opt_resetup_env,
271 'testlist=s' => \@testlists,
272 'random-order' => \$opt_random_order,
273 'load-list=s' => \$opt_load_list,
274 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
275 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
276 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
277 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
278 'use-dns-faking' => \$opt_use_dns_faking
281 exit(1) if (not $result);
283 ShowHelp() if ($opt_help);
285 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
287 # we want unbuffered output
288 $| = 1;
290 my @tests = @ARGV;
292 # quick hack to disable rpc validation when using valgrind - its way too slow
293 unless (defined($ENV{VALGRIND})) {
294 $ENV{VALIDATE} = "validate";
295 $ENV{MALLOC_CHECK_} = 3;
298 # make all our python scripts unbuffered
299 $ENV{PYTHONUNBUFFERED} = 1;
301 my $bindir_abs = abs_path($bindir);
303 # Backwards compatibility:
304 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
305 if (defined($ENV{FEDORA_DS_ROOT})) {
306 $ldap = "fedora-ds";
307 } else {
308 $ldap = "openldap";
312 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
313 if ($ldap) {
314 # LDAP is slow
315 $torture_maxtime *= 2;
318 $prefix =~ s+//+/+;
319 $prefix =~ s+/./+/+;
320 $prefix =~ s+/$++;
322 die("using an empty prefix isn't allowed") unless $prefix ne "";
324 # Ensure we have the test prefix around.
326 # We need restrictive
327 # permissions on this as some subdirectories in this tree will have
328 # wider permissions (ie 0777) and this would allow other users on the
329 # host to subvert the test process.
330 umask 0077;
331 mkdir($prefix, 0700) unless -d $prefix;
332 chmod 0700, $prefix;
333 # We need to have no umask limitations for the tests.
334 umask 0000;
336 my $prefix_abs = abs_path($prefix);
337 my $tmpdir_abs = abs_path("$prefix/tmp");
338 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
340 my $srcdir_abs = abs_path($srcdir);
342 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
343 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
345 $ENV{SAMBA_SELFTEST} = "1";
347 $ENV{PREFIX} = $prefix;
348 $ENV{PREFIX_ABS} = $prefix_abs;
349 $ENV{SRCDIR} = $srcdir;
350 $ENV{SRCDIR_ABS} = $srcdir_abs;
351 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
352 $ENV{BINDIR} = $bindir_abs;
354 my $tls_enabled = not $opt_quick;
355 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
357 sub prefix_pathvar($$)
359 my ($name, $newpath) = @_;
360 if (defined($ENV{$name})) {
361 $ENV{$name} = "$newpath:$ENV{$name}";
362 } else {
363 $ENV{$name} = $newpath;
366 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
367 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
369 if ($opt_socket_wrapper_keep_pcap) {
370 # Socket wrapper keep pcap implies socket wrapper pcap
371 $opt_socket_wrapper_pcap = 1;
374 if ($opt_socket_wrapper_pcap) {
375 # Socket wrapper pcap implies socket wrapper
376 $opt_socket_wrapper = 1;
379 my $ld_preload = $ENV{LD_PRELOAD};
381 if ($opt_libnss_wrapper_so_path) {
382 if ($ld_preload) {
383 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
384 } else {
385 $ld_preload = "$opt_libnss_wrapper_so_path";
389 if ($opt_libresolv_wrapper_so_path) {
390 if ($ld_preload) {
391 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
392 } else {
393 $ld_preload = "$opt_libresolv_wrapper_so_path";
397 if ($opt_libsocket_wrapper_so_path) {
398 if ($ld_preload) {
399 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
400 } else {
401 $ld_preload = "$opt_libsocket_wrapper_so_path";
405 if ($opt_libuid_wrapper_so_path) {
406 if ($ld_preload) {
407 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
408 } else {
409 $ld_preload = "$opt_libuid_wrapper_so_path";
413 $ENV{LD_PRELOAD} = $ld_preload;
414 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
416 # Enable uid_wrapper globally
417 $ENV{UID_WRAPPER} = 1;
419 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
421 # This is needed in order to allow the ldb_*ldap module
422 # to work with a preloaded socket wrapper.
423 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
425 my $socket_wrapper_dir;
426 if ($opt_socket_wrapper) {
427 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
428 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
429 } elsif (not $opt_list) {
430 unless ($< == 0) {
431 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
435 if ($opt_use_dns_faking) {
436 print "DNS: Faking nameserver\n";
437 $ENV{SAMBA_DNS_FAKING} = 1;
440 my $target;
441 my $testenv_default = "none";
443 if ($opt_mitkrb5 == 1) {
444 $ENV{MITKRB5} = $opt_mitkrb5;
447 # After this many seconds, the server will self-terminate. All tests
448 # must terminate in this time, and testenv will only stay alive this
449 # long
451 my $server_maxtime;
452 if ($opt_testenv) {
453 # 1 year should be enough :-)
454 $server_maxtime = 365 * 24 * 60 * 60;
455 } else {
456 # make test should run under 5 hours
457 $server_maxtime = 5 * 60 * 60;
460 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
461 $server_maxtime = $ENV{SMBD_MAXTIME};
464 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
465 unless ($opt_list) {
466 if ($opt_target eq "samba") {
467 $testenv_default = "ad_dc";
468 } elsif ($opt_target eq "samba3") {
469 $testenv_default = "nt4_member";
473 sub read_test_regexes($)
475 my ($name) = @_;
476 my @ret = ();
477 open(LF, "<$name") or die("unable to read $name: $!");
478 while (<LF>) {
479 chomp;
480 next if (/^#/);
481 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
482 push (@ret, [$1, $4]);
483 } else {
484 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
485 push (@ret, [$_, undef]);
488 close(LF);
489 return @ret;
492 foreach (@opt_exclude) {
493 push (@excludes, read_test_regexes($_));
496 foreach (@opt_include) {
497 push (@includes, read_test_regexes($_));
500 my $interfaces = join(',', ("127.0.0.11/8",
501 "127.0.0.12/8",
502 "127.0.0.13/8",
503 "127.0.0.14/8",
504 "127.0.0.15/8",
505 "127.0.0.16/8"));
507 my $clientdir = "$prefix_abs/client";
509 my $conffile = "$clientdir/client.conf";
510 $ENV{SMB_CONF_PATH} = $conffile;
512 sub write_clientconf($$$)
514 my ($conffile, $clientdir, $vars) = @_;
516 mkdir("$clientdir", 0777) unless -d "$clientdir";
518 if ( -d "$clientdir/private" ) {
519 unlink <$clientdir/private/*>;
520 } else {
521 mkdir("$clientdir/private", 0777);
524 if ( -d "$clientdir/bind-dns" ) {
525 unlink <$clientdir/bind-dns/*>;
526 } else {
527 mkdir("$clientdir/bind-dns", 0777);
530 if ( -d "$clientdir/lockdir" ) {
531 unlink <$clientdir/lockdir/*>;
532 } else {
533 mkdir("$clientdir/lockdir", 0777);
536 if ( -d "$clientdir/statedir" ) {
537 unlink <$clientdir/statedir/*>;
538 } else {
539 mkdir("$clientdir/statedir", 0777);
542 if ( -d "$clientdir/cachedir" ) {
543 unlink <$clientdir/cachedir/*>;
544 } else {
545 mkdir("$clientdir/cachedir", 0777);
548 # this is ugly, but the ncalrpcdir needs exactly 0755
549 # otherwise tests fail.
550 my $mask = umask;
551 umask 0022;
552 if ( -d "$clientdir/ncalrpcdir/np" ) {
553 unlink <$clientdir/ncalrpcdir/np/*>;
554 rmdir "$clientdir/ncalrpcdir/np";
556 if ( -d "$clientdir/ncalrpcdir" ) {
557 unlink <$clientdir/ncalrpcdir/*>;
558 rmdir "$clientdir/ncalrpcdir";
560 mkdir("$clientdir/ncalrpcdir", 0755);
561 umask $mask;
563 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
564 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
565 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
566 my $ca_users_dir = "$cadir/Users";
568 if ( -d "$clientdir/pkinit" ) {
569 unlink <$clientdir/pkinit/*>;
570 } else {
571 mkdir("$clientdir/pkinit", 0700);
574 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
575 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
576 # We make a copy here and make the certificated easily
577 # accessable in the client environment.
578 my $mask = umask;
579 umask 0077;
580 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
581 for my $d (readdir USERS) {
582 my $user_dir = "${ca_users_dir}/${d}";
583 next if ${d} =~ /^\./;
584 next if (! -d "${user_dir}");
585 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
586 for my $l (readdir USER) {
587 my $user_link = "${user_dir}/${l}";
588 next if ${l} =~ /^\./;
589 next if (! -l "${user_link}");
591 my $dest = "${clientdir}/pkinit/${l}";
592 Samba::copy_file_content(${user_link}, ${dest});
594 closedir USER;
596 closedir USERS;
597 umask $mask;
599 open(CF, ">$conffile");
600 print CF "[global]\n";
601 print CF "\tnetbios name = client\n";
602 if (defined($vars->{DOMAIN})) {
603 print CF "\tworkgroup = $vars->{DOMAIN}\n";
605 if (defined($vars->{REALM})) {
606 print CF "\trealm = $vars->{REALM}\n";
608 if ($opt_socket_wrapper) {
609 print CF "\tinterfaces = $interfaces\n";
611 print CF "
612 private dir = $clientdir/private
613 binddns dir = $clientdir/bind-dns
614 lock dir = $clientdir/lockdir
615 state directory = $clientdir/statedir
616 cache directory = $clientdir/cachedir
617 ncalrpc dir = $clientdir/ncalrpcdir
618 panic action = $RealBin/gdb_backtrace \%d
619 max xmit = 32K
620 notify:inotify = false
621 ldb:nosync = true
622 system:anonymous = true
623 client lanman auth = Yes
624 log level = 1
625 torture:basedir = $clientdir
626 #We don't want to pass our self-tests if the PAC code is wrong
627 gensec:require_pac = true
628 #We don't want to run 'speed' tests for very long
629 torture:timelimit = 1
630 winbind separator = /
631 tls cafile = ${cacert}
632 tls crlfile = ${cacrl_pem}
633 tls verify peer = no_check
634 include system krb5 conf = no
636 close(CF);
639 my @todo = ();
641 sub should_run_test($)
643 my $name = shift;
644 if ($#tests == -1) {
645 return 1;
647 for (my $i=0; $i <= $#tests; $i++) {
648 if ($name =~ /$tests[$i]/i) {
649 return 1;
652 return 0;
655 sub read_testlist($)
657 my ($filename) = @_;
659 my @ret = ();
660 open(IN, $filename) or die("Unable to open $filename: $!");
662 while (<IN>) {
663 if (/-- TEST(-LOADLIST|) --\n/) {
664 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
665 my $name = <IN>;
666 $name =~ s/\n//g;
667 my $env = <IN>;
668 $env =~ s/\n//g;
669 my $loadlist;
670 if ($supports_loadlist) {
671 $loadlist = <IN>;
672 $loadlist =~ s/\n//g;
674 my $cmdline = <IN>;
675 $cmdline =~ s/\n//g;
676 if (should_run_test($name) == 1) {
677 push (@ret, [$name, $env, $cmdline, $loadlist]);
679 } else {
680 print;
683 close(IN) or die("Error creating recipe from $filename");
684 return @ret;
687 if ($#testlists == -1) {
688 die("No testlists specified");
691 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
692 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
693 $ENV{TMPDIR} = "$tmpdir_abs";
694 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
695 if ($opt_socket_wrapper) {
696 $ENV{SELFTEST_INTERFACES} = $interfaces;
697 } else {
698 $ENV{SELFTEST_INTERFACES} = "";
700 if ($opt_quick) {
701 $ENV{SELFTEST_QUICK} = "1";
702 } else {
703 $ENV{SELFTEST_QUICK} = "";
705 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
707 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
708 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
710 my @available = ();
711 foreach my $fn (@testlists) {
712 foreach (read_testlist($fn)) {
713 my $name = $$_[0];
714 next if (@includes and not defined(find_in_list(\@includes, $name)));
715 push (@available, $_);
719 my $restricted = undef;
720 my $restricted_used = {};
722 if ($opt_load_list) {
723 $restricted = [];
724 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
725 while (<LOAD_LIST>) {
726 chomp;
727 push (@$restricted, $_);
729 close(LOAD_LIST);
732 my $individual_tests = undef;
733 $individual_tests = {};
735 foreach my $testsuite (@available) {
736 my $name = $$testsuite[0];
737 my $skipreason = skip(@$testsuite);
738 if (defined($restricted)) {
739 # Find the testsuite for this test
740 my $match = undef;
741 foreach my $r (@$restricted) {
742 if ($r eq $name) {
743 $individual_tests->{$name} = [];
744 $match = $r;
745 $restricted_used->{$r} = 1;
746 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
747 push(@{$individual_tests->{$name}}, $r);
748 $match = $r;
749 $restricted_used->{$r} = 1;
752 if ($match) {
753 if (defined($skipreason)) {
754 if (not $opt_list) {
755 Subunit::skip_testsuite($name, $skipreason);
757 } else {
758 push(@todo, $testsuite);
761 } elsif (defined($skipreason)) {
762 if (not $opt_list) {
763 Subunit::skip_testsuite($name, $skipreason);
765 } else {
766 push(@todo, $testsuite);
770 if (defined($restricted)) {
771 foreach (@$restricted) {
772 unless (defined($restricted_used->{$_})) {
773 print "No test or testsuite found matching $_\n";
776 } elsif ($#todo == -1) {
777 print STDERR "No tests to run\n";
778 exit(1);
781 my $suitestotal = $#todo + 1;
783 unless ($opt_list) {
784 Subunit::progress($suitestotal);
785 Subunit::report_time();
788 my $i = 0;
789 $| = 1;
791 my %running_envs = ();
793 sub get_running_env($)
795 my ($name) = @_;
797 my $envname = $name;
799 $envname =~ s/:.*//;
801 return $running_envs{$envname};
804 my @exported_envvars = (
805 # domain stuff
806 "DOMAIN",
807 "REALM",
808 "DOMSID",
810 # stuff related to a trusted domain
811 "TRUST_SERVER",
812 "TRUST_SERVER_IP",
813 "TRUST_SERVER_IPV6",
814 "TRUST_NETBIOSNAME",
815 "TRUST_USERNAME",
816 "TRUST_PASSWORD",
817 "TRUST_DOMAIN",
818 "TRUST_REALM",
819 "TRUST_DOMSID",
821 # domain controller stuff
822 "DC_SERVER",
823 "DC_SERVER_IP",
824 "DC_SERVER_IPV6",
825 "DC_NETBIOSNAME",
826 "DC_NETBIOSALIAS",
828 # domain member
829 "MEMBER_SERVER",
830 "MEMBER_SERVER_IP",
831 "MEMBER_SERVER_IPV6",
832 "MEMBER_NETBIOSNAME",
833 "MEMBER_NETBIOSALIAS",
835 # rpc proxy controller stuff
836 "RPC_PROXY_SERVER",
837 "RPC_PROXY_SERVER_IP",
838 "RPC_PROXY_SERVER_IPV6",
839 "RPC_PROXY_NETBIOSNAME",
840 "RPC_PROXY_NETBIOSALIAS",
842 # domain controller stuff for Vampired DC
843 "VAMPIRE_DC_SERVER",
844 "VAMPIRE_DC_SERVER_IP",
845 "VAMPIRE_DC_SERVER_IPV6",
846 "VAMPIRE_DC_NETBIOSNAME",
847 "VAMPIRE_DC_NETBIOSALIAS",
849 # domain controller stuff for RODC
850 "RODC_DC_SERVER",
851 "RODC_DC_SERVER_IP",
852 "RODC_DC_SERVER_IPV6",
853 "RODC_DC_NETBIOSNAME",
855 # domain controller stuff for FL 2000 Vampired DC
856 "VAMPIRE_2000_DC_SERVER",
857 "VAMPIRE_2000_DC_SERVER_IP",
858 "VAMPIRE_2000_DC_SERVER_IPV6",
859 "VAMPIRE_2000_DC_NETBIOSNAME",
860 "VAMPIRE_2000_DC_NETBIOSALIAS",
862 "PROMOTED_DC_SERVER",
863 "PROMOTED_DC_SERVER_IP",
864 "PROMOTED_DC_SERVER_IPV6",
865 "PROMOTED_DC_NETBIOSNAME",
866 "PROMOTED_DC_NETBIOSALIAS",
868 # server stuff
869 "SERVER",
870 "SERVER_IP",
871 "SERVER_IPV6",
872 "NETBIOSNAME",
873 "NETBIOSALIAS",
874 "SAMSID",
876 # user stuff
877 "USERNAME",
878 "USERID",
879 "PASSWORD",
880 "DC_USERNAME",
881 "DC_PASSWORD",
883 # UID/GID for rfc2307 mapping tests
884 "UID_RFC2307TEST",
885 "GID_RFC2307TEST",
887 # misc stuff
888 "KRB5_CONFIG",
889 "KRB5CCNAME",
890 "SELFTEST_WINBINDD_SOCKET_DIR",
891 "NMBD_SOCKET_DIR",
892 "LOCAL_PATH",
893 "DNS_FORWARDER1",
894 "DNS_FORWARDER2",
895 "RESOLV_CONF",
896 "UNACCEPTABLE_PASSWORD",
897 "LOCK_DIR",
898 "SMBD_TEST_LOG",
900 # nss_wrapper
901 "NSS_WRAPPER_PASSWD",
902 "NSS_WRAPPER_GROUP",
903 "NSS_WRAPPER_HOSTS",
904 "NSS_WRAPPER_HOSTNAME",
905 "NSS_WRAPPER_MODULE_SO_PATH",
906 "NSS_WRAPPER_MODULE_FN_PREFIX",
908 # resolv_wrapper
909 "RESOLV_WRAPPER_CONF",
910 "RESOLV_WRAPPER_HOSTS",
913 sub sighandler($)
915 my $signame = shift;
917 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
918 $SIG{PIPE} = 'IGNORE';
920 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
922 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
923 teardown_env($_) foreach(keys %running_envs);
924 system("pstree -p $$");
925 print "$0: PID[$$]: Exiting...\n";
926 exit(1);
929 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
931 sub setup_env($$)
933 my ($name, $prefix) = @_;
935 my $testenv_vars = undef;
937 my $envname = $name;
938 my $option = $name;
940 $envname =~ s/:.*//;
941 $option =~ s/^[^:]*//;
942 $option =~ s/^://;
944 $option = "client" if $option eq "";
946 # Initially clear out the environment for the provision, so previous envs'
947 # variables don't leak in. Provisioning steps must explicitly set their
948 # necessary variables when calling out to other executables
949 foreach (@exported_envvars) {
950 unless ($_ == "NSS_WRAPPER_HOSTS" ||
951 $_ == "RESOLV_WRAPPER_HOSTS")
953 delete $ENV{$_};
956 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
957 delete $ENV{SMB_CONF_PATH};
959 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
961 if (defined(get_running_env($envname))) {
962 $testenv_vars = get_running_env($envname);
963 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
964 print $testenv_vars->{target}->getlog_env($testenv_vars);
965 $testenv_vars = undef;
967 } else {
968 $testenv_vars = $target->setup_env($envname, $prefix);
969 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
970 return $testenv_vars;
971 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
972 $testenv_vars->{target} = $target;
974 if (not defined($testenv_vars)) {
975 warn("$opt_target can't start up known environment '$envname'");
979 return undef unless defined($testenv_vars);
981 $running_envs{$envname} = $testenv_vars;
983 if ($option eq "local") {
984 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
985 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
986 } elsif ($option eq "client") {
987 SocketWrapper::set_default_iface(11);
988 write_clientconf($conffile, $clientdir, $testenv_vars);
989 $ENV{SMB_CONF_PATH} = $conffile;
990 } else {
991 die("Unknown option[$option] for envname[$envname]");
994 foreach (@exported_envvars) {
995 if (defined($testenv_vars->{$_})) {
996 $ENV{$_} = $testenv_vars->{$_};
997 } else {
998 delete $ENV{$_};
1002 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
1003 unlink($krb5_ccache_path);
1004 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
1005 return $testenv_vars;
1008 sub exported_envvars_str($)
1010 my ($testenv_vars) = @_;
1011 my $out = "";
1013 foreach (@exported_envvars) {
1014 next unless defined($testenv_vars->{$_});
1015 $out .= $_."=".$testenv_vars->{$_}."\n";
1018 return $out;
1021 sub getlog_env($)
1023 my ($envname) = @_;
1024 return "" if ($envname eq "none");
1025 my $env = get_running_env($envname);
1026 return $env->{target}->getlog_env($env);
1029 sub check_env($)
1031 my ($envname) = @_;
1032 my $env = get_running_env($envname);
1033 return $env->{target}->check_env($env);
1036 sub teardown_env($)
1038 my ($envname) = @_;
1039 return if ($envname eq "none");
1040 print STDERR "teardown_env($envname)\n";
1041 my $env = get_running_env($envname);
1042 $env->{target}->teardown_env($env);
1043 delete $running_envs{$envname};
1046 # This 'global' file needs to be empty when we start
1047 unlink("$prefix_abs/dns_host_file");
1048 unlink("$prefix_abs/hosts");
1050 if ($opt_random_order) {
1051 require List::Util;
1052 my @newtodo = List::Util::shuffle(@todo);
1053 @todo = @newtodo;
1056 if ($opt_testenv) {
1057 my $testenv_name = $ENV{SELFTEST_TESTENV};
1058 $testenv_name = $testenv_default unless defined($testenv_name);
1060 my $testenv_vars = setup_env($testenv_name, $prefix);
1062 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1063 die("Unable to setup environment $testenv_name");
1066 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1067 $ENV{ENVNAME} = $testenv_name;
1069 my $envvarstr = exported_envvars_str($testenv_vars);
1071 my @term_args = ("echo -e \"
1072 Welcome to the Samba4 Test environment '$testenv_name'
1074 This matches the client environment used in make test
1075 server is pid `cat \$PIDDIR/samba.pid`
1077 Some useful environment variables:
1078 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1079 SMB_CONF_PATH=\$SMB_CONF_PATH
1081 $envvarstr
1082 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1083 my @term = ();
1084 if ($ENV{TERMINAL}) {
1085 @term = ($ENV{TERMINAL});
1086 } else {
1087 @term = ("xterm", "-e");
1088 unshift(@term_args, ("bash", "-c"));
1091 system(@term, @term_args);
1093 teardown_env($testenv_name);
1094 } elsif ($opt_list) {
1095 foreach (@todo) {
1096 my $name = $$_[0];
1097 my $envname = $$_[1];
1098 my $cmd = $$_[2];
1099 my $listcmd = $$_[3];
1101 unless (defined($listcmd)) {
1102 warn("Unable to list tests in $name");
1103 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1104 # a single "test".
1105 print "$name\n";
1106 next;
1109 system($listcmd);
1111 if ($? == -1) {
1112 die("Unable to run $listcmd: $!");
1113 } elsif ($? & 127) {
1114 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1117 my $exitcode = $? >> 8;
1118 if ($exitcode != 0) {
1119 die("$cmd exited with exit code $exitcode");
1122 } else {
1123 foreach (@todo) {
1124 $i++;
1125 my $cmd = $$_[2];
1126 my $name = $$_[0];
1127 my $envname = $$_[1];
1128 my $envvars = setup_env($envname, $prefix);
1130 if (not defined($envvars)) {
1131 Subunit::start_testsuite($name);
1132 Subunit::end_testsuite($name, "error",
1133 "unable to set up environment $envname - exiting");
1134 next;
1135 } elsif ($envvars eq "UNKNOWN") {
1136 Subunit::start_testsuite($name);
1137 Subunit::end_testsuite($name, "error",
1138 "environment $envname is unknown - exiting");
1139 next;
1142 # Generate a file with the individual tests to run, if the
1143 # test runner for this test suite supports it.
1144 if ($individual_tests and $individual_tests->{$name}) {
1145 if ($$_[3]) {
1146 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1147 foreach my $test (@{$individual_tests->{$name}}) {
1148 print $fh substr($test, length($name)+1) . "\n";
1150 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1151 } else {
1152 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1156 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1158 teardown_env($envname) if ($opt_resetup_env);
1162 print "\n";
1164 teardown_env($_) foreach (keys %running_envs);
1166 my $failed = 0;
1168 # if there were any valgrind failures, show them
1169 foreach (<$prefix/valgrind.log*>) {
1170 next unless (-s $_);
1171 print "VALGRIND FAILURE\n";
1172 $failed++;
1173 system("cat $_");
1175 exit 0;