replace: add checks for atomic_thread_fence(memory_order_seq_cst) and add possible...
[Samba.git] / selftest / selftest.pl
blobbd72841199f4b7b6e4111fac70b3c958fd868e35
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2010 Jelmer Vernooij <jelmer@samba.org>
4 # Copyright (C) 2007-2009 Stefan Metzmacher <metze@samba.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 use strict;
21 use FindBin qw($RealBin $Script);
22 use File::Spec;
23 use File::Temp qw(tempfile);
24 use Getopt::Long;
25 use POSIX;
26 use Cwd qw(abs_path);
27 use lib "$RealBin";
28 use Subunit;
29 use SocketWrapper;
30 use target::Samba;
31 use Time::HiRes qw(time);
33 eval {
34 require Time::HiRes;
35 Time::HiRes->import("time");
37 if ($@) {
38 print "You don't have Time::Hires installed !\n";
41 my $opt_help = 0;
42 my $opt_target = "samba";
43 my $opt_quick = 0;
44 my $opt_socket_wrapper = 0;
45 my $opt_socket_wrapper_pcap = undef;
46 my $opt_socket_wrapper_keep_pcap = undef;
47 my $opt_random_order = 0;
48 my $opt_one = 0;
49 my @opt_exclude = ();
50 my @opt_include = ();
51 my @opt_exclude_env = ();
52 my @opt_include_env = ();
53 my $opt_testenv = 0;
54 my $opt_list = 0;
55 my $opt_mitkrb5 = 0;
56 my $ldap = undef;
57 my $opt_resetup_env = undef;
58 my $opt_load_list = undef;
59 my $opt_libnss_wrapper_so_path = "";
60 my $opt_libresolv_wrapper_so_path = "";
61 my $opt_libsocket_wrapper_so_path = "";
62 my $opt_libuid_wrapper_so_path = "";
63 my $opt_libasan_so_path = "";
64 my $opt_use_dns_faking = 0;
65 my @testlists = ();
67 my $srcdir = ".";
68 my $bindir = "./bin";
69 my $prefix = "./st";
71 my @includes = ();
72 my @excludes = ();
74 sub find_in_list($$)
76 my ($list, $fullname) = @_;
78 foreach (@$list) {
79 if ($fullname =~ /$$_[0]/) {
80 return ($$_[1]) if ($$_[1]);
81 return "";
85 return undef;
88 sub skip
90 my ($name, $envname) = @_;
91 my ($env_basename, $env_localpart) = split(/:/, $envname);
93 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
94 return "environment $envname is disabled as this build does not include an AD DC";
97 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
98 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
99 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
100 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
103 return find_in_list(\@excludes, $name);
106 sub getlog_env($);
108 sub setup_pcap($)
110 my ($name) = @_;
112 return unless ($opt_socket_wrapper_pcap);
113 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
115 my $fname = $name;
116 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
118 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
120 SocketWrapper::setup_pcap($pcap_file);
122 return $pcap_file;
125 sub cleanup_pcap($$)
127 my ($pcap_file, $exitcode) = @_;
129 return unless ($opt_socket_wrapper_pcap);
130 return if ($opt_socket_wrapper_keep_pcap);
131 return unless ($exitcode == 0);
132 return unless defined($pcap_file);
134 unlink($pcap_file);
137 # expand strings from %ENV
138 sub expand_environment_strings($)
140 my $s = shift;
141 # we use a reverse sort so we do the longer ones first
142 foreach my $k (sort { $b cmp $a } keys %ENV) {
143 $s =~ s/\$$k/$ENV{$k}/g;
145 return $s;
148 sub run_testsuite($$$$$)
150 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
151 my $pcap_file = setup_pcap($name);
153 Subunit::start_testsuite($name);
154 Subunit::progress_push();
155 Subunit::report_time();
156 system($cmd);
157 Subunit::report_time();
158 Subunit::progress_pop();
160 if ($? == -1) {
161 print "command: $cmd\n";
162 printf "expanded command: %s\n", expand_environment_strings($cmd);
163 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
164 exit(1);
165 } elsif ($? & 127) {
166 print "command: $cmd\n";
167 printf "expanded command: %s\n", expand_environment_strings($cmd);
168 Subunit::end_testsuite($name, "error",
169 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
170 exit(1);
173 my $exitcode = $? >> 8;
175 my $envlog = getlog_env($envname);
176 if ($envlog ne "") {
177 print "envlog: $envlog\n";
180 print "command: $cmd\n";
181 printf "expanded command: %s\n", expand_environment_strings($cmd);
183 if ($exitcode == 0) {
184 Subunit::end_testsuite($name, "success");
185 } else {
186 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
189 cleanup_pcap($pcap_file, $exitcode);
191 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
192 print "PCAP FILE: $pcap_file\n";
195 if ($exitcode != 0) {
196 exit(1) if ($opt_one);
199 return $exitcode;
202 sub ShowHelp()
204 print "Samba test runner
205 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
206 Copyright (C) Stefan Metzmacher <metze\@samba.org>
208 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
210 Generic options:
211 --help this help page
212 --target=samba[3]|win Samba version to target
213 --testlist=FILE file to read available tests from
214 --exclude=FILE Exclude tests listed in the file
215 --include=FILE Include tests listed in the file
216 --exclude-env=ENV Exclude tests for the specified environment
217 --include-env=ENV Include tests for the specified environment
219 Paths:
220 --prefix=DIR prefix to run tests in [st]
221 --srcdir=DIR source directory [.]
222 --bindir=DIR binaries directory [./bin]
224 Preload cwrap:
225 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
226 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
227 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
228 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
229 --asan_so_path=FILE the asan library to preload
231 DNS:
232 --use-dns-faking Fake DNS entries rather than talking to our
233 DNS implementation.
235 Target Specific:
236 --socket-wrapper-pcap save traffic to pcap directories
237 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
238 failed
239 --socket-wrapper enable socket wrapper
241 Samba4 Specific:
242 --ldap=openldap|fedora-ds back samba onto specified ldap server
244 Behaviour:
245 --quick run quick overall test
246 --one abort when the first test fails
247 --testenv run a shell in the requested test environment
248 --list list available tests
250 exit(0);
253 my $result = GetOptions (
254 'help|h|?' => \$opt_help,
255 'target=s' => \$opt_target,
256 'prefix=s' => \$prefix,
257 'socket-wrapper' => \$opt_socket_wrapper,
258 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
259 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
260 'quick' => \$opt_quick,
261 'one' => \$opt_one,
262 'exclude=s' => \@opt_exclude,
263 'include=s' => \@opt_include,
264 'exclude-env=s' => \@opt_exclude_env,
265 'include-env=s' => \@opt_include_env,
266 'srcdir=s' => \$srcdir,
267 'bindir=s' => \$bindir,
268 'testenv' => \$opt_testenv,
269 'list' => \$opt_list,
270 'mitkrb5' => \$opt_mitkrb5,
271 'ldap:s' => \$ldap,
272 'resetup-environment' => \$opt_resetup_env,
273 'testlist=s' => \@testlists,
274 'random-order' => \$opt_random_order,
275 'load-list=s' => \$opt_load_list,
276 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
277 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
278 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
279 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
280 'asan_so_path=s' => \$opt_libasan_so_path,
281 'use-dns-faking' => \$opt_use_dns_faking
284 exit(1) if (not $result);
286 ShowHelp() if ($opt_help);
288 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
290 # we want unbuffered output
291 $| = 1;
293 my @tests = @ARGV;
295 # quick hack to disable rpc validation when using valgrind - its way too slow
296 unless (defined($ENV{VALGRIND})) {
297 $ENV{VALIDATE} = "validate";
298 $ENV{MALLOC_CHECK_} = 3;
301 # make all our python scripts unbuffered
302 $ENV{PYTHONUNBUFFERED} = 1;
304 my $bindir_abs = abs_path($bindir);
306 # Backwards compatibility:
307 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
308 if (defined($ENV{FEDORA_DS_ROOT})) {
309 $ldap = "fedora-ds";
310 } else {
311 $ldap = "openldap";
315 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
316 if ($ldap) {
317 # LDAP is slow
318 $torture_maxtime *= 2;
321 $prefix =~ s+//+/+;
322 $prefix =~ s+/./+/+;
323 $prefix =~ s+/$++;
325 die("using an empty prefix isn't allowed") unless $prefix ne "";
327 # Ensure we have the test prefix around.
329 # We need restrictive
330 # permissions on this as some subdirectories in this tree will have
331 # wider permissions (ie 0777) and this would allow other users on the
332 # host to subvert the test process.
333 umask 0077;
334 mkdir($prefix, 0700) unless -d $prefix;
335 chmod 0700, $prefix;
336 # We need to have no umask limitations for the tests.
337 umask 0000;
339 my $prefix_abs = abs_path($prefix);
340 my $tmpdir_abs = abs_path("$prefix/tmp");
341 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
343 my $srcdir_abs = abs_path($srcdir);
345 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
346 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
348 $ENV{SAMBA_SELFTEST} = "1";
350 $ENV{PREFIX} = $prefix;
351 $ENV{PREFIX_ABS} = $prefix_abs;
352 $ENV{SRCDIR} = $srcdir;
353 $ENV{SRCDIR_ABS} = $srcdir_abs;
354 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
355 $ENV{BINDIR} = $bindir_abs;
357 my $tls_enabled = not $opt_quick;
358 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
360 sub prefix_pathvar($$)
362 my ($name, $newpath) = @_;
363 if (defined($ENV{$name})) {
364 $ENV{$name} = "$newpath:$ENV{$name}";
365 } else {
366 $ENV{$name} = $newpath;
369 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
370 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
372 if ($opt_socket_wrapper_keep_pcap) {
373 # Socket wrapper keep pcap implies socket wrapper pcap
374 $opt_socket_wrapper_pcap = 1;
377 if ($opt_socket_wrapper_pcap) {
378 # Socket wrapper pcap implies socket wrapper
379 $opt_socket_wrapper = 1;
382 my $ld_preload = $ENV{LD_PRELOAD};
384 if ($opt_libasan_so_path) {
385 if ($ld_preload) {
386 $ld_preload = "$ld_preload:$opt_libasan_so_path";
387 } else {
388 $ld_preload = "$opt_libasan_so_path";
392 if ($opt_libnss_wrapper_so_path) {
393 if ($ld_preload) {
394 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
395 } else {
396 $ld_preload = "$opt_libnss_wrapper_so_path";
400 if ($opt_libresolv_wrapper_so_path) {
401 if ($ld_preload) {
402 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
403 } else {
404 $ld_preload = "$opt_libresolv_wrapper_so_path";
408 if ($opt_libsocket_wrapper_so_path) {
409 if ($ld_preload) {
410 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
411 } else {
412 $ld_preload = "$opt_libsocket_wrapper_so_path";
416 if ($opt_libuid_wrapper_so_path) {
417 if ($ld_preload) {
418 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
419 } else {
420 $ld_preload = "$opt_libuid_wrapper_so_path";
424 $ENV{LD_PRELOAD} = $ld_preload;
425 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
427 # Enable uid_wrapper globally
428 $ENV{UID_WRAPPER} = 1;
430 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
432 # This is needed in order to allow the ldb_*ldap module
433 # to work with a preloaded socket wrapper.
434 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
436 my $socket_wrapper_dir;
437 if ($opt_socket_wrapper) {
438 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
439 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
440 } elsif (not $opt_list) {
441 unless ($< == 0) {
442 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
446 if ($opt_use_dns_faking) {
447 print "DNS: Faking nameserver\n";
448 $ENV{SAMBA_DNS_FAKING} = 1;
451 my $target;
452 my $testenv_default = "none";
454 if ($opt_mitkrb5 == 1) {
455 $ENV{MITKRB5} = $opt_mitkrb5;
458 # After this many seconds, the server will self-terminate. All tests
459 # must terminate in this time, and testenv will only stay alive this
460 # long
462 my $server_maxtime;
463 if ($opt_testenv) {
464 # 1 year should be enough :-)
465 $server_maxtime = 365 * 24 * 60 * 60;
466 } else {
467 # make test should run under 5 hours
468 $server_maxtime = 5 * 60 * 60;
471 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
472 $server_maxtime = $ENV{SMBD_MAXTIME};
475 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
476 unless ($opt_list) {
477 if ($opt_target eq "samba") {
478 $testenv_default = "ad_dc";
479 } elsif ($opt_target eq "samba3") {
480 $testenv_default = "nt4_member";
484 sub read_test_regexes($)
486 my ($name) = @_;
487 my @ret = ();
488 open(LF, "<$name") or die("unable to read $name: $!");
489 while (<LF>) {
490 chomp;
491 next if (/^#/);
492 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
493 push (@ret, [$1, $4]);
494 } else {
495 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
496 push (@ret, [$_, undef]);
499 close(LF);
500 return @ret;
503 foreach (@opt_exclude) {
504 push (@excludes, read_test_regexes($_));
507 foreach (@opt_include) {
508 push (@includes, read_test_regexes($_));
511 my $interfaces = join(',', ("127.0.0.11/8",
512 "127.0.0.12/8",
513 "127.0.0.13/8",
514 "127.0.0.14/8",
515 "127.0.0.15/8",
516 "127.0.0.16/8"));
518 my $clientdir = "$prefix_abs/client";
520 my $conffile = "$clientdir/client.conf";
521 $ENV{SMB_CONF_PATH} = $conffile;
523 sub write_clientconf($$$)
525 my ($conffile, $clientdir, $vars) = @_;
527 mkdir("$clientdir", 0777) unless -d "$clientdir";
529 if ( -d "$clientdir/private" ) {
530 unlink <$clientdir/private/*>;
531 } else {
532 mkdir("$clientdir/private", 0777);
535 if ( -d "$clientdir/bind-dns" ) {
536 unlink <$clientdir/bind-dns/*>;
537 } else {
538 mkdir("$clientdir/bind-dns", 0777);
541 if ( -d "$clientdir/lockdir" ) {
542 unlink <$clientdir/lockdir/*>;
543 } else {
544 mkdir("$clientdir/lockdir", 0777);
547 if ( -d "$clientdir/statedir" ) {
548 unlink <$clientdir/statedir/*>;
549 } else {
550 mkdir("$clientdir/statedir", 0777);
553 if ( -d "$clientdir/cachedir" ) {
554 unlink <$clientdir/cachedir/*>;
555 } else {
556 mkdir("$clientdir/cachedir", 0777);
559 # this is ugly, but the ncalrpcdir needs exactly 0755
560 # otherwise tests fail.
561 my $mask = umask;
562 umask 0022;
563 if ( -d "$clientdir/ncalrpcdir/np" ) {
564 unlink <$clientdir/ncalrpcdir/np/*>;
565 rmdir "$clientdir/ncalrpcdir/np";
567 if ( -d "$clientdir/ncalrpcdir" ) {
568 unlink <$clientdir/ncalrpcdir/*>;
569 rmdir "$clientdir/ncalrpcdir";
571 mkdir("$clientdir/ncalrpcdir", 0755);
572 umask $mask;
574 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
575 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
576 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
577 my $ca_users_dir = "$cadir/Users";
579 if ( -d "$clientdir/pkinit" ) {
580 unlink <$clientdir/pkinit/*>;
581 } else {
582 mkdir("$clientdir/pkinit", 0700);
585 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
586 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
587 # We make a copy here and make the certificated easily
588 # accessable in the client environment.
589 my $mask = umask;
590 umask 0077;
591 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
592 for my $d (readdir USERS) {
593 my $user_dir = "${ca_users_dir}/${d}";
594 next if ${d} =~ /^\./;
595 next if (! -d "${user_dir}");
596 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
597 for my $l (readdir USER) {
598 my $user_link = "${user_dir}/${l}";
599 next if ${l} =~ /^\./;
600 next if (! -l "${user_link}");
602 my $dest = "${clientdir}/pkinit/${l}";
603 Samba::copy_file_content(${user_link}, ${dest});
605 closedir USER;
607 closedir USERS;
608 umask $mask;
610 open(CF, ">$conffile");
611 print CF "[global]\n";
612 print CF "\tnetbios name = client\n";
613 if (defined($vars->{DOMAIN})) {
614 print CF "\tworkgroup = $vars->{DOMAIN}\n";
616 if (defined($vars->{REALM})) {
617 print CF "\trealm = $vars->{REALM}\n";
619 if ($opt_socket_wrapper) {
620 print CF "\tinterfaces = $interfaces\n";
622 print CF "
623 private dir = $clientdir/private
624 binddns dir = $clientdir/bind-dns
625 lock dir = $clientdir/lockdir
626 state directory = $clientdir/statedir
627 cache directory = $clientdir/cachedir
628 ncalrpc dir = $clientdir/ncalrpcdir
629 panic action = $RealBin/gdb_backtrace \%d
630 max xmit = 32K
631 notify:inotify = false
632 ldb:nosync = true
633 system:anonymous = true
634 client lanman auth = Yes
635 log level = 1
636 torture:basedir = $clientdir
637 #We don't want to pass our self-tests if the PAC code is wrong
638 gensec:require_pac = true
639 #We don't want to run 'speed' tests for very long
640 torture:timelimit = 1
641 winbind separator = /
642 tls cafile = ${cacert}
643 tls crlfile = ${cacrl_pem}
644 tls verify peer = no_check
645 include system krb5 conf = no
647 close(CF);
650 my @todo = ();
652 sub should_run_test($)
654 my $name = shift;
655 if ($#tests == -1) {
656 return 1;
658 for (my $i=0; $i <= $#tests; $i++) {
659 if ($name =~ /$tests[$i]/i) {
660 return 1;
663 return 0;
666 sub read_testlist($)
668 my ($filename) = @_;
670 my @ret = ();
671 open(IN, $filename) or die("Unable to open $filename: $!");
673 while (<IN>) {
674 if (/-- TEST(-LOADLIST|) --\n/) {
675 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
676 my $name = <IN>;
677 $name =~ s/\n//g;
678 my $env = <IN>;
679 $env =~ s/\n//g;
680 my $loadlist;
681 if ($supports_loadlist) {
682 $loadlist = <IN>;
683 $loadlist =~ s/\n//g;
685 my $cmdline = <IN>;
686 $cmdline =~ s/\n//g;
687 if (should_run_test($name) == 1) {
688 push (@ret, [$name, $env, $cmdline, $loadlist]);
690 } else {
691 print;
694 close(IN) or die("Error creating recipe from $filename");
695 return @ret;
698 if ($#testlists == -1) {
699 die("No testlists specified");
702 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
703 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
704 $ENV{TMPDIR} = "$tmpdir_abs";
705 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
706 if ($opt_socket_wrapper) {
707 $ENV{SELFTEST_INTERFACES} = $interfaces;
708 } else {
709 $ENV{SELFTEST_INTERFACES} = "";
711 if ($opt_quick) {
712 $ENV{SELFTEST_QUICK} = "1";
713 } else {
714 $ENV{SELFTEST_QUICK} = "";
716 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
718 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
719 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
721 my @available = ();
722 foreach my $fn (@testlists) {
723 foreach (read_testlist($fn)) {
724 my $name = $$_[0];
725 next if (@includes and not defined(find_in_list(\@includes, $name)));
726 push (@available, $_);
730 my $restricted = undef;
731 my $restricted_used = {};
733 if ($opt_load_list) {
734 $restricted = [];
735 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
736 while (<LOAD_LIST>) {
737 chomp;
738 push (@$restricted, $_);
740 close(LOAD_LIST);
743 my $individual_tests = undef;
744 $individual_tests = {};
746 foreach my $testsuite (@available) {
747 my $name = $$testsuite[0];
748 my $skipreason = skip(@$testsuite);
749 if (defined($restricted)) {
750 # Find the testsuite for this test
751 my $match = undef;
752 foreach my $r (@$restricted) {
753 if ($r eq $name) {
754 $individual_tests->{$name} = [];
755 $match = $r;
756 $restricted_used->{$r} = 1;
757 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
758 push(@{$individual_tests->{$name}}, $r);
759 $match = $r;
760 $restricted_used->{$r} = 1;
763 if ($match) {
764 if (defined($skipreason)) {
765 if (not $opt_list) {
766 Subunit::skip_testsuite($name, $skipreason);
768 } else {
769 push(@todo, $testsuite);
772 } elsif (defined($skipreason)) {
773 if (not $opt_list) {
774 Subunit::skip_testsuite($name, $skipreason);
776 } else {
777 push(@todo, $testsuite);
781 if (defined($restricted)) {
782 foreach (@$restricted) {
783 unless (defined($restricted_used->{$_})) {
784 print "No test or testsuite found matching $_\n";
787 } elsif ($#todo == -1) {
788 print STDERR "No tests to run\n";
789 exit(1);
792 my $suitestotal = $#todo + 1;
794 unless ($opt_list) {
795 Subunit::progress($suitestotal);
796 Subunit::report_time();
799 my $i = 0;
800 $| = 1;
802 my %running_envs = ();
804 sub get_running_env($)
806 my ($name) = @_;
808 my $envname = $name;
810 $envname =~ s/:.*//;
812 return $running_envs{$envname};
815 my @exported_envvars = (
816 # domain stuff
817 "DOMAIN",
818 "REALM",
819 "DOMSID",
821 # stuff related to a trusted domain
822 "TRUST_SERVER",
823 "TRUST_SERVER_IP",
824 "TRUST_SERVER_IPV6",
825 "TRUST_NETBIOSNAME",
826 "TRUST_USERNAME",
827 "TRUST_PASSWORD",
828 "TRUST_DOMAIN",
829 "TRUST_REALM",
830 "TRUST_DOMSID",
832 # domain controller stuff
833 "DC_SERVER",
834 "DC_SERVER_IP",
835 "DC_SERVER_IPV6",
836 "DC_NETBIOSNAME",
837 "DC_NETBIOSALIAS",
839 # domain member
840 "MEMBER_SERVER",
841 "MEMBER_SERVER_IP",
842 "MEMBER_SERVER_IPV6",
843 "MEMBER_NETBIOSNAME",
844 "MEMBER_NETBIOSALIAS",
846 # rpc proxy controller stuff
847 "RPC_PROXY_SERVER",
848 "RPC_PROXY_SERVER_IP",
849 "RPC_PROXY_SERVER_IPV6",
850 "RPC_PROXY_NETBIOSNAME",
851 "RPC_PROXY_NETBIOSALIAS",
853 # domain controller stuff for Vampired DC
854 "VAMPIRE_DC_SERVER",
855 "VAMPIRE_DC_SERVER_IP",
856 "VAMPIRE_DC_SERVER_IPV6",
857 "VAMPIRE_DC_NETBIOSNAME",
858 "VAMPIRE_DC_NETBIOSALIAS",
860 # domain controller stuff for RODC
861 "RODC_DC_SERVER",
862 "RODC_DC_SERVER_IP",
863 "RODC_DC_SERVER_IPV6",
864 "RODC_DC_NETBIOSNAME",
866 # domain controller stuff for FL 2000 Vampired DC
867 "VAMPIRE_2000_DC_SERVER",
868 "VAMPIRE_2000_DC_SERVER_IP",
869 "VAMPIRE_2000_DC_SERVER_IPV6",
870 "VAMPIRE_2000_DC_NETBIOSNAME",
871 "VAMPIRE_2000_DC_NETBIOSALIAS",
873 "PROMOTED_DC_SERVER",
874 "PROMOTED_DC_SERVER_IP",
875 "PROMOTED_DC_SERVER_IPV6",
876 "PROMOTED_DC_NETBIOSNAME",
877 "PROMOTED_DC_NETBIOSALIAS",
879 # server stuff
880 "SERVER",
881 "SERVER_IP",
882 "SERVER_IPV6",
883 "NETBIOSNAME",
884 "NETBIOSALIAS",
885 "SAMSID",
887 # user stuff
888 "USERNAME",
889 "USERID",
890 "PASSWORD",
891 "DC_USERNAME",
892 "DC_PASSWORD",
894 # UID/GID for rfc2307 mapping tests
895 "UID_RFC2307TEST",
896 "GID_RFC2307TEST",
898 # misc stuff
899 "KRB5_CONFIG",
900 "KRB5CCNAME",
901 "SELFTEST_WINBINDD_SOCKET_DIR",
902 "NMBD_SOCKET_DIR",
903 "LOCAL_PATH",
904 "DNS_FORWARDER1",
905 "DNS_FORWARDER2",
906 "RESOLV_CONF",
907 "UNACCEPTABLE_PASSWORD",
908 "LOCK_DIR",
909 "SMBD_TEST_LOG",
911 # nss_wrapper
912 "NSS_WRAPPER_PASSWD",
913 "NSS_WRAPPER_GROUP",
914 "NSS_WRAPPER_HOSTS",
915 "NSS_WRAPPER_HOSTNAME",
916 "NSS_WRAPPER_MODULE_SO_PATH",
917 "NSS_WRAPPER_MODULE_FN_PREFIX",
919 # resolv_wrapper
920 "RESOLV_WRAPPER_CONF",
921 "RESOLV_WRAPPER_HOSTS",
924 sub sighandler($)
926 my $signame = shift;
928 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
929 $SIG{PIPE} = 'IGNORE';
931 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
933 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
934 teardown_env($_) foreach(keys %running_envs);
935 system("pstree -p $$");
936 print "$0: PID[$$]: Exiting...\n";
937 exit(1);
940 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
942 sub setup_env($$)
944 my ($name, $prefix) = @_;
946 my $testenv_vars = undef;
948 my $envname = $name;
949 my $option = $name;
951 $envname =~ s/:.*//;
952 $option =~ s/^[^:]*//;
953 $option =~ s/^://;
955 $option = "client" if $option eq "";
957 # Initially clear out the environment for the provision, so previous envs'
958 # variables don't leak in. Provisioning steps must explicitly set their
959 # necessary variables when calling out to other executables
960 foreach (@exported_envvars) {
961 unless ($_ == "NSS_WRAPPER_HOSTS" ||
962 $_ == "RESOLV_WRAPPER_HOSTS")
964 delete $ENV{$_};
967 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
968 delete $ENV{SMB_CONF_PATH};
970 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
972 if (defined(get_running_env($envname))) {
973 $testenv_vars = get_running_env($envname);
974 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
975 print $testenv_vars->{target}->getlog_env($testenv_vars);
976 $testenv_vars = undef;
978 } else {
979 $testenv_vars = $target->setup_env($envname, $prefix);
980 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
981 return $testenv_vars;
982 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
983 $testenv_vars->{target} = $target;
985 if (not defined($testenv_vars)) {
986 warn("$opt_target can't start up known environment '$envname'");
990 return undef unless defined($testenv_vars);
992 $running_envs{$envname} = $testenv_vars;
994 if ($option eq "local") {
995 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
996 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
997 } elsif ($option eq "client") {
998 SocketWrapper::set_default_iface(11);
999 write_clientconf($conffile, $clientdir, $testenv_vars);
1000 $ENV{SMB_CONF_PATH} = $conffile;
1001 } else {
1002 die("Unknown option[$option] for envname[$envname]");
1005 foreach (@exported_envvars) {
1006 if (defined($testenv_vars->{$_})) {
1007 $ENV{$_} = $testenv_vars->{$_};
1008 } else {
1009 delete $ENV{$_};
1013 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
1014 unlink($krb5_ccache_path);
1015 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
1016 return $testenv_vars;
1019 sub exported_envvars_str($)
1021 my ($testenv_vars) = @_;
1022 my $out = "";
1024 foreach (@exported_envvars) {
1025 next unless defined($testenv_vars->{$_});
1026 $out .= $_."=".$testenv_vars->{$_}."\n";
1029 return $out;
1032 sub getlog_env($)
1034 my ($envname) = @_;
1035 return "" if ($envname eq "none");
1036 my $env = get_running_env($envname);
1037 return $env->{target}->getlog_env($env);
1040 sub check_env($)
1042 my ($envname) = @_;
1043 my $env = get_running_env($envname);
1044 return $env->{target}->check_env($env);
1047 sub teardown_env($)
1049 my ($envname) = @_;
1050 return if ($envname eq "none");
1051 print STDERR "teardown_env($envname)\n";
1052 my $env = get_running_env($envname);
1053 $env->{target}->teardown_env($env);
1054 delete $running_envs{$envname};
1057 # This 'global' file needs to be empty when we start
1058 unlink("$prefix_abs/dns_host_file");
1059 unlink("$prefix_abs/hosts");
1061 if ($opt_random_order) {
1062 require List::Util;
1063 my @newtodo = List::Util::shuffle(@todo);
1064 @todo = @newtodo;
1067 if ($opt_testenv) {
1068 my $testenv_name = $ENV{SELFTEST_TESTENV};
1069 $testenv_name = $testenv_default unless defined($testenv_name);
1071 my $testenv_vars = setup_env($testenv_name, $prefix);
1073 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1074 die("Unable to setup environment $testenv_name");
1077 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1078 $ENV{ENVNAME} = $testenv_name;
1080 my $envvarstr = exported_envvars_str($testenv_vars);
1082 my @term_args = ("echo -e \"
1083 Welcome to the Samba4 Test environment '$testenv_name'
1085 This matches the client environment used in make test
1086 server is pid `cat \$PIDDIR/samba.pid`
1088 Some useful environment variables:
1089 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1090 SMB_CONF_PATH=\$SMB_CONF_PATH
1092 $envvarstr
1093 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1094 my @term = ();
1095 if ($ENV{TERMINAL}) {
1096 @term = ($ENV{TERMINAL});
1097 } else {
1098 @term = ("xterm", "-e");
1099 unshift(@term_args, ("bash", "-c"));
1102 system(@term, @term_args);
1104 teardown_env($testenv_name);
1105 } elsif ($opt_list) {
1106 foreach (@todo) {
1107 my $name = $$_[0];
1108 my $envname = $$_[1];
1109 my $cmd = $$_[2];
1110 my $listcmd = $$_[3];
1112 unless (defined($listcmd)) {
1113 warn("Unable to list tests in $name");
1114 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1115 # a single "test".
1116 print "$name\n";
1117 next;
1120 system($listcmd);
1122 if ($? == -1) {
1123 die("Unable to run $listcmd: $!");
1124 } elsif ($? & 127) {
1125 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1128 my $exitcode = $? >> 8;
1129 if ($exitcode != 0) {
1130 die("$cmd exited with exit code $exitcode");
1133 } else {
1134 foreach (@todo) {
1135 $i++;
1136 my $cmd = $$_[2];
1137 my $name = $$_[0];
1138 my $envname = $$_[1];
1139 my $envvars = setup_env($envname, $prefix);
1141 if (not defined($envvars)) {
1142 Subunit::start_testsuite($name);
1143 Subunit::end_testsuite($name, "error",
1144 "unable to set up environment $envname - exiting");
1145 next;
1146 } elsif ($envvars eq "UNKNOWN") {
1147 Subunit::start_testsuite($name);
1148 Subunit::end_testsuite($name, "error",
1149 "environment $envname is unknown - exiting");
1150 next;
1153 # Generate a file with the individual tests to run, if the
1154 # test runner for this test suite supports it.
1155 if ($individual_tests and $individual_tests->{$name}) {
1156 if ($$_[3]) {
1157 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1158 foreach my $test (@{$individual_tests->{$name}}) {
1159 print $fh substr($test, length($name)+1) . "\n";
1161 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1162 } else {
1163 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1167 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1169 teardown_env($envname) if ($opt_resetup_env);
1173 print "\n";
1175 teardown_env($_) foreach (keys %running_envs);
1177 my $failed = 0;
1179 # if there were any valgrind failures, show them
1180 foreach (<$prefix/valgrind.log*>) {
1181 next unless (-s $_);
1182 print "VALGRIND FAILURE\n";
1183 $failed++;
1184 system("cat $_");
1186 exit 0;